(gdb) set follow-fork-mode child
(gdb) start
Temporary breakpoint 1 at 0x40056e: file 1.c, line 5.
Starting program: /root/tst
Temporary breakpoint 1, main () at 1.c:5
5 while((p1=fork())==-1);
(gdb) n
[New process 24]
ac
[Switching to process 24]
main () at 1.c:6
6 if(p1==0)
(gdb)
7 putchar('b');
(gdb)
16 return 0;
(gdb)
17 }
(gdb)
__libc_start_main (main=0x400566 , argc=1, argv=0x7ffffffde398, init=,
fini=, rtld_fini=, stack_end=0x7ffffffde388)
at ../csu/libc-start.c:325
325 ../csu/libc-start.c: No such file or directory.
(gdb)
b[Inferior 2 (process 24) exited normally]
(gdb)
The program is not being run.
(gdb) set detach-on-fork off
(gdb) start
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.23.so...done.
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.23.so...done.
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.23.so...done.
Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.23.so...done.
Temporary breakpoint 2 at 0x40056e: main. (3 locations)
Starting program: /root/tst
Thread 1.1 "tst" hit Temporary breakpoint 2, main () at 1.c:5
5 while((p1=fork())==-1);
(gdb) n
[New process 26]
6 if(p1==0)
(gdb) i inferior
Num Description Executable
* 1 process 25 /root/tst
2 process 23 /root/tst
3 process 24 /root/tst
4 process 26 /root/tst
(gdb) n
9 while((p2=fork())==-1);
(gdb)
[New process 27]
10 if(p2==0){
(gdb)
14 else putchar('a');
(gdb)
16 return 0;
(gdb)
17 }
(gdb)
__libc_start_main (main=0x400566 , argc=1, argv=0x7ffffffde398, init=,
fini=, rtld_fini=, stack_end=0x7ffffffde388)
at ../csu/libc-start.c:325
325 ../csu/libc-start.c: No such file or directory.
(gdb)
a[Inferior 1 (process 25) exited normally]
(gdb)
The program is not being run.
(gdb) inferior 2
[Switching to inferior 2 [process 23] (/root/tst)]
[Switching to thread 2.1 (process 23)]
#0 0x00007fffff0fc41a in __libc_fork () at ../sysdeps/nptl/fork.c:145
145 ../sysdeps/nptl/fork.c: No such file or directory.
(gdb)
[Switching to inferior 2 [process 23] (/root/tst)]
[Switching to thread 2.1 (process 23)]
#0 0x00007fffff0fc41a in __libc_fork () at ../sysdeps/nptl/fork.c:145
145 in ../sysdeps/nptl/fork.c
.
.
.
168 in ../sysdeps/nptl/fork.c
(gdb) bt
#0 __libc_fork () at ../sysdeps/nptl/fork.c:168
#1 0x0000000000400573 in main () at 1.c:5
(gdb) n
169 in ../sysdeps/nptl/fork.c
.
.
.
(gdb)
264 in ../sysdeps/nptl/fork.c
(gdb)
main () at 1.c:6
6 if(p1==0)
(gdb) i inferior
Num Description Executable
1 /root/tst
* 2 process 23 /root/tst
3 process 24 /root/tst
4 process 26 /root/tst
5 process 27 /root/tst
(gdb) inferior 3
[Switching to inferior 3 [process 24] (/root/tst)]
[Switching to thread 3.1 (process 24)]
#0 0x00007fffff0fc41a in __libc_fork () at ../sysdeps/nptl/fork.c:145
145 ../sysdeps/nptl/fork.c: No such file or directory.
(gdb) i inferior
Num Description Executable
1 /root/tst
2 process 23 /root/tst
* 3 process 24 /root/tst
4 process 26 /root/tst
5 process 27 /root/tst
(gdb) n
152 in ../sysdeps/nptl/fork.c
.
.
.
(gdb)
264 in ../sysdeps/nptl/fork.c
(gdb)
main () at 1.c:10
10 if(p2==0){
(gdb)
11 putchar('c');
(gdb)
12 printf("\n");
(gdb)
c
16 return 0;
(gdb)
17 }
(gdb)
__libc_start_main (main=0x400566 , argc=1, argv=0x7ffffffde398, init=,
fini=, rtld_fini=, stack_end=0x7ffffffde388)
at ../csu/libc-start.c:325
325 ../csu/libc-start.c: No such file or directory.
(gdb)
[Inferior 3 (process 24) exited normally]
(gdb)
7 putchar('b');
(gdb)
16 return 0;
(gdb)
17 }
(gdb)
__libc_start_main (main=0x400566 , argc=1, argv=0x7ffffffde398, init=,
fini=, rtld_fini=, stack_end=0x7ffffffde388)
at ../csu/libc-start.c:325
325 ../csu/libc-start.c: No such file or directory.
(gdb)
b[Inferior 2 (process 23) exited normally]
(gdb)
The program is not being run.
实验内容
其中,a、b、c 的显示顺序是随机的,取决与进程的调度顺序。fork()创建进程之后,各 个进程的时间片的获得不一定是顺序的,所以输出的顺序会变化。
GDB多进程调试
调试子进程
可以看到执行到第5行,父进程打印“a” 并创建了子进程 24,子进程打印“c”。正常退出后打印“b”。 (但这里是不是只监视了一个子进程24 ?)
同时调试父进程和子进程
父进程
Inferior 1(process 28)
创建Inferior 2(process 32)
打印b
,并正常退出;再创建Inferior 3(process 33)
打印c
,并正常退出,之后父进程打印a
,退出。总结
第一次上手GDB调试,有助于对进程控制的理解