gdb在默认设置下调试多进程程序时只会去调试主进程。gdb版本高于v7.0支持多进程的同时调试。只需要设置follow-fork-mode和detach-on-fork即可。
Socket服务端调试过程:
[root@localhost bin]# gdb socketserver
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /opt/testgdb/bin/socketserver...done.
(gdb) b main
Breakpoint 1 at 0x401515: file /opt/testgdb/src/socketserver.c, line 58.
(gdb) set args /opt/testgdb/config/socket.cfg server 1 /opt/testgdb/log/
#设置入参
(gdb) show follow-fork mode
Debugger response to a program call of fork or vfork is "parent".
(gdb) set follow-fork-mode child #修改gdb参数
(gdb) show follow-fork mode
Debugger response to a program call of fork or vfork is "child".
(gdb) show follow-fork-mode
Debugger response to a program call of fork or vfork is "child".
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is on.
(gdb) set detach-on-fork off #修改gdb参数
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) r
Starting program: /opt/testgdb/bin/socketserver /opt/testgdb/config/socket.cfg server 1 /opt/testgdb/log/
Breakpoint 1, main (argc=5, argv=0x7fffffffe4a8) at /opt/testgdb/src/socketserver.c:58
58 long lRet = 0L;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64
(gdb) b /opt/testgdb/src/socketserver.c:110 #子进程断点
Breakpoint 2 at 0x40178e: file /opt/testgdb/src/socketserver.c, line 110.
(gdb) b /opt/testgdb/src/socketserver.c:114 #父进程断点
Breakpoint 3 at 0x4017ba: file /opt/testgdb/src/socketserver.c, line 114.
(gdb) info inferiors #查看进程信息
Num Description Executable
* 1 process 3511 /opt/testgdb/bin/socketserver
(gdb) c #先执行到下一个断点,这时候服务端在等待客户端连接,这时候启动客户端程序,./socketclient /opt/testgdb/config/socket.cfg client 1 /opt/testgdb/log/
Continuing.
/opt/testgdb/log/server.20201210
Listen at Port: 8888
[New process 3854]
[Switching to process 3854]
Breakpoint 2, main (argc=5, argv=0x7fffffffe4a8) at /opt/testgdb/src/socketserver.c:110
110 log(0,"child process\n");
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64
(gdb) info inferiors #这时候能看到有两个进程
Num Description Executable
* 2 process 3854 /opt/testgdb/bin/socketserver
1 process 3511 /opt/testgdb/bin/socketserver
(gdb) inferior 1 #切换进程
[Switching to inferior 1 [process 3511] (/opt/testgdb/bin/socketserver)]
[Switching to thread 1 (process 3511)]
#0 0x00007ffff7ad2f42 in fork () from /lib64/libc.so.6
(gdb) list
105 log(0, "fork error[%ld]\n", errno);
106 return -1;
107 }
108
109 if (!pid) {
110 log(0,"child process\n");
111 lRet = RequestHandler(iSocketId);
112 exit(0);
113 }
114 log(0,"main process\n");
(gdb) inferior 2 #进程切换
[Switching to inferior 2 [process 3854] (/opt/testgdb/bin/socketserver)]
[Switching to thread 2 (process 3854)]
#0 main (argc=5, argv=0x7fffffffe4a8) at /opt/testgdb/src/socketserver.c:110
110 log(0,"child process\n");
(gdb) l
105 log(0, "fork error[%ld]\n", errno);
106 return -1;
107 }
108
109 if (!pid) {
110 log(0,"child process\n");
111 lRet = RequestHandler(iSocketId);
112 exit(0);
113 }
114 log(0,"main process\n");
(gdb) quit
A debugging session is active.
Inferior 2 [process 3854] will be killed.
Inferior 1 [process 3511] will be killed.
Quit anyway? (y or n) y
[root@localhost bin]#