GDB 调试子进程:follow-fork-mode

例子

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void) {
    pid_t pid;

    pid = fork();
    if (pid < 0)
    {
        exit(1);
    }
    else if (pid > 0)
    {
        exit(0);
    }
    printf("hello world\n");
    return 0;
}

技巧

在调试多进程程序时,gdb默认会追踪父进程。例如:

(gdb) start
Temporary breakpoint 1 at 0x40055c: file a.c, line 8.
Starting program: /data2/home/nanxiao/a

Temporary breakpoint 1, main () at a.c:8
8               pid = fork();
(gdb) n
9               if (pid < 0)
(gdb) hello world

13              else if (pid > 0)
(gdb)
15                      exit(0);
(gdb)
[Inferior 1 (process 12786) exited normally]

可以看到程序执行到第15行:父进程退出。

如果要调试子进程,要使用如下命令:set follow-fork-mode child,例如:

(gdb) set follow-fork-mode child
(gdb) start
Temporary breakpoint 1 at 0x40055c: file a.c, line 8.
Starting program: /data2/home/nanxiao/a

Temporary breakpoint 1, main () at a.c:8
8               pid = fork();
(gdb) n
[New process 12241]
[Switching to process 12241]
9               if (pid < 0)
(gdb)
13              else if (pid > 0)
(gdb)
17              printf("hello world\n");
(gdb)
hello world
18              return 0;

可以看到程序执行到第17行:子进程打印“hello world”。

这个命令目前Linux支持,其它很多操作系统都不支持,使用时请注意。参见 gdb手册


   转载规则


《GDB 调试子进程:follow-fork-mode》 由 吴杭沉 采用 知识共享署名 4.0 国际许可协议 进行许可。
 上一篇
GDB 实战:定位多线程死锁 GDB 实战:定位多线程死锁
本文用一个「双线程、双互斥锁、加锁顺序相反」的最小示例,演示如何用 gdb attach 定位程序死锁。死锁是并发编程里最棘手的问题之一,通过这个例子可以掌握一套通用的分析思路:先看线程与栈回溯(backtrace),再顺着栈找到等待的锁,
2021-06-24
下一篇 
GDB attach 报错 Operation not permitted 的解决 GDB attach 报错 Operation not permitted 的解决
在进入gdb后,直接使用 attach ID,出现下面的情况: Could not attach to process. If your uid matches the uid of the target process, check t
2021-05-31
  目录