Linux多进程和多线程的一次gdb调试实例( 三 )


Linux多进程和多线程的一次gdb调试实例

文章插图
 
同时,使用命令cat /proc/23869/status查看当前进程的详细信息:进程PID为23869,它的父进程(即GDB进程)为23859,同时这也是追踪进程ID,线程数Threads为2(当前父进程23869+线程24024) 。
Linux多进程和多线程的一次gdb调试实例

文章插图
 
3.8 查看第二个断点处的调试信息(gdb) info inferiors #####显示正在调试的进程Num Description Executable2 process 23873 /home/vfhky/bin/gdb_pthread ###子进程* 1 process 23869 /home/vfhky/bin/gdb_pthread ###父进程 (gdb) info threads ####查看所有运行的线程,父进程23869、子进程23873、线程24024,由星号可以发现目前调试已经切换到了线程24024了 。Id Target Id Frame * 3 Thread 0x7ffff6fdd700 (LWP 24024) "gdb_pthread" ParentDo (argv=0x7fffffffe390 "Thread") at gdb_pthread.c:50 2 Thread 0x7ffff7fe1740 (LWP 23873) "gdb_pthread" 0x00007ffff709b50c in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:130 1 Thread 0x7ffff7fe1740 (LWP 23869) "gdb_pthread" ParentDo (argv=0x7fffffffe3a0 "Parent") at gdb_pthread.c:50(gdb) info b #####查看设置的所有的断点breakpoint和捕捉点catchpoint(共3个):Num Type Disp Enb Address What1 catchpoint keep y fork, process 23873catchpoint already hit 1 time2 breakpoint keep y <MULTIPLE>breakpoint already hit 1 time2.1 y 0x00000000004007b7 in main at gdb_pthread.c:18 inf 22.2 y 0x00000000004007b7 in main at gdb_pthread.c:18 inf 13 breakpoint keep y <MULTIPLE>breakpoint already hit 1 time3.1 y 0x00000000004008a7 in ParentDo at gdb_pthread.c:50 inf 23.2 y 0x00000000004008a7 in ParentDo at gdb_pthread.c:50 inf 1(gdb)3.9 如果手动切换到线程24024(gdb) thread 3[Switching to thread 3 (Thread 0x7ffff6fdd700 (LWP 24024))]#0 ParentDo (argv=0x7fffffffe390 "Thread") at gdb_pthread.c:5050 printf( "[%s]: [%d] [%s] [%lu] [%s]n", argv, pid, tprefix, tid, "step2" );(gdb) info threads #####查看所有运行的线程 Id Target Id Frame * 3 Thread 0x7ffff6fdd700 (LWP 24024) "gdb_pthread" ParentDo (argv=0x7fffffffe390 "Thread") at gdb_pthread.c:50 2 Thread 0x7ffff7fe1740 (LWP 23873) "gdb_pthread" 0x00007ffff709b50c in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:130 1 Thread 0x7ffff7fe1740 (LWP 23869) "gdb_pthread" ParentDo (argv=0x7fffffffe3a0 "Parent") at gdb_pthread.c:50(gdb) info inferiors #####显示正在调试的进程Num Description Executable2 process 23873 /home/vfhky/bin/gdb_pthread * 1 process 23869 /home/vfhky/bin/gdb_pthread (gdb)3.10 开始执行第二个断点处的代码(gdb) cContinuing.[Thread]: [23869] [thread] [140737337218816] [step2] #####线程24024执行第50行处,打印数据[Thread]: [23869] [thread] [140737337218816] [step3] #####线程24024执行第51行处,打印数据[Thread 0x7ffff6fdd700 (LWP 24024) exited] #####线程24024退出[Switching to Thread 0x7ffff7fe1740 (LWP 23869)] #####切换到父进程中去Breakpoint 3, ParentDo (argv=0x7fffffffe3a0 "Parent") at gdb_pthread.c:50 #####父进程继续停在第50行处的断点50 printf( "[%s]: [%d] [%s] [%lu] [%s]n", argv, pid, tprefix, tid, "step2" );(gdb) info inferiors ######列出正在调试进程(父进程23869和子进程23873),1前面的星号表示当前调试的进程(父进程23869) 。Num Description Executable2 process 23873 /home/vfhky/bin/gdb_pthread * 1 process 23869 /home/vfhky/bin/gdb_pthread (gdb) info threads ######查看所有运行的线程 Id Target Id Frame2 Thread 0x7ffff7fe1740 (LWP 23873) "gdb_pthread" 0x00007ffff709b50c in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:130 #####子进程23873* 1 Thread 0x7ffff7fe1740 (LWP 23869) "gdb_pthread" ParentDo (argv=0x7fffffffe3a0 "Parent") at gdb_pthread.c:50 #####父进程23869(gdb)这时使用命令查看当前系统进程的状态:存在父进程23869和子进程23873,其中线程24024已经结束了 。
[vfhky@typecodes ~]$ pstree -pul
Linux多进程和多线程的一次gdb调试实例

文章插图
 
3.11 继续调试父进程此时,由于线程的退出,父进程作为自动选择的要调试的线程 。
(gdb) cContinuing.[Parent]: [23869] [thread] [140737354012480] [step2] #####父进程23869执行第50行[Parent]: [23869] [thread] [140737354012480] [step3] #####父进程23869执行第51行[Inferior 1 (process 23869) exited normally] #####正在调试的父进程23869退出(gdb) info inferiors ######显示正在调试的进程 Num Description Executable2 process 23873 /home/vfhky/bin/gdb_pthread #####fork创建的子进程23873* 1 <null> /home/vfhky/bin/gdb_pthread #####fork创建的父进程23869已经退出 (gdb) info threads ####显示正在运行的线程:只存在子进程23873,父进程23869已经退出Id Target Id Frame2 Thread 0x7ffff7fe1740 (LWP 23873) "gdb_pthread" 0x00007ffff709b50c in __libc_fork () at ../nptl/sysdeps/unix/sysv/linux/fork.c:130No selected thread. See `help thread'. #####提示没有被选中的要调试的线程(gdb) info b #####查看所有的断点Num Type Disp Enb Address What1 catchpoint keep y fork, process 23873catchpoint already hit 1 time2 breakpoint keep y <MULTIPLE>breakpoint already hit 1 time2.1 y 0x00000000004007b7 in main at gdb_pthread.c:18 inf 22.2 y 0x00000000004007b7 in main at gdb_pthread.c:18 inf 13 breakpoint keep y <MULTIPLE>breakpoint already hit 2 times3.1 y 0x00000000004008a7 in ParentDo at gdb_pthread.c:50 inf 2 #####子进程238733.2 y 0x00000000004008a7 in ParentDo at gdb_pthread.c:50 inf 1 #####父进程23869(gdb)


推荐阅读