在 C 语言中创建多进程程序需要使用 fork 相关的一些函数,调用一次 fork 函数就会创建一个进程。多进程调试时,我们需要对调试的进程和未调试的进程进行设置。下面介绍的一些命令是我们在调试时经常使用到的。

    § 20.GDB调试多进程程序 - 图1 GDB默认调试的是父进程,我们可以设置调试的进程,使用命令:

    1. set follow-fork-mode <mode>

    其中 mode 为设置调试的进程:可以是 child,也可以是 parent。当 mode 为 parent 时,程序在调用 fork 后调试父进程,子进程不会受到影响。当 mode 为 child 时,程序在调用 fork 后调试子进程,父进程不会受到影响。

    § 20.GDB调试多进程程序 - 图2 查看 GDB 中设置的 follow-fork-mode 可以使用命令:

    1. show follow-fork-mode

    § 20.GDB调试多进程程序 - 图3 在 GDB 中调试多进程时,可以只调试一个进程,也可以同时调试两个进程,这个和 GDB 中的 detach-on-fork 的设置有关,相关的命令格式展示如下:

    1. set detach-on-fork <mode>

    mode 可以为 on,也可以为off。当 mode 为 on 时,表示程序只调试一个进程(可以是父进程、子进程),这是 GDB 的默认设置。当 mode 为 off 时,父子进程都在gdb的控制之下,其中一个进程正常的调试,另一个会被设置为暂停状态。

    § 20.GDB调试多进程程序 - 图4 查看 GDB 中设置的 detach-on-fork 可以使用命令:

    1. show detach-on-fork

    GDB 将每一个被调试程序的执行状态记录在一个名为 inferior 的结构中。一般情况下一个 inferior 对应一个进程,每个不同的 inferior 有不同的地址空间。inferior有时候会在进程没有启动的时候就存在。

    § 20.GDB调试多进程程序 - 图5 查看当前调试的所有的 inferior,使用命令:

    1. info inferiors

    当前调试的进程前有 “*”。

    § 20.GDB调试多进程程序 - 图6 切换进程使用命令 inferior,使用方式展示如下:

    1. inferior <num>

    表示切换到 id 为 num 的 inferior。实例:

    1. inferior 2

    切换到 2 号进程。

    实例:创建一个多进程的源文件。

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <sys/types.h>
    4. #include <unistd.h>
    5. #include <sys/wait.h>
    6. int main(void)
    7. {
    8. pid_t pid;
    9. pid = fork();
    10. if(pid < 0) {
    11. perror("fork()");
    12. }
    13. if(pid == 0) {
    14. printf("this is child,pid = %d\n",getpid());
    15. }
    16. else {
    17. printf("this is parent,pid = %d\n",getpid());
    18. }
    19. exit(0);
    20. }

    我们之前在《GDB程序产生中断》中讲到过,使用捕获点 catch 命令可以捕获,当调用 fork 函数会产生中断。

    相关调试信息如下:

    1. (gdb) show follow-fork-mode //显示默认的 follow-fork-mode 配置
    2. Debugger response to a program call of fork or vfork is "parent".
    3. (gdb) show detach-on-fork //显示默认的 detach-on-fork 配置
    4. Whether gdb will detach the child of a fork is on.
    5. (gdb) set follow-fork-mode child //设置 follow-fork-mode child
    6. (gdb) set detach-on-fork off //设置 detach-on-fork off off
    7. (gdb) catch fork //设置捕获点中断
    8. Catchpoint 1 (fork)
    9. (gdb) run //运行程序
    10. Starting program: /home/wjc/test/test4/a.out Catchpoint 1 (forked process 2073), 0x00007ffff7ac8b1c in __libc_fork ()
    11. at ../sysdeps/nptl/fork.c:135
    12. warning: Source file is more recent than executable.
    13. (gdb) s
    14. [New process 2073]
    15. Reading symbols from /home/wjc/test/test4/a.out...done.
    16. Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so...done.
    17. Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.27.so...done.
    18. __libc_fork () at ../sysdeps/nptl/fork.c:142
    19. warning: Source file is more recent than executable.
    20. (gdb) info inferiors //显示程序运行的进程
    21. Num Description Executable
    22. 1 process 2069 /home/wjc/test/test4/a.out
    23. * 2 process 2073 /home/wjc/test/test4/a.out
    24. (gdb) inferior 1 //切换到一号进程
    25. [Switching to inferior 1 [process 2069] (/home/wjc/test/test4/a.out)]
    26. [Switching to thread 1.1 (process 2069)]
    27. #0 0x00007ffff7ac8b1c in __libc_fork () at ../sysdeps/nptl/fork.c:135
    28. (gdb) info inferiors
    29. Num Description Executable
    30. * 1 process 2069 /home/wjc/test/test4/a.out
    31. 2 process 2073 /home/wjc/test/test4/a.out
    32. (gdb) c //执行第一个进程
    33. Continuing.
    34. this is parent,pid = 2069
    35. [Inferior 1 (process 2069) exited normally]
    36. (gdb) info inferiors
    37. Num Description Executable
    38. 1 <null> /home/wjc/test/test4/a.out
    39. * 2 process 2073 /home/wjc/test/test4/a.out //第一个进程执行结束,走动切换到第二个进程
    40. (gdb) c //执行第二个进程
    41. Continuing.
    42. this is child,pid = 2073
    43. [Inferior 2 (process 2073) exited normally]
    44. (gdb) info inferiors
    45. Num Description Executable
    46. 1 <null> /home/wjc/test/test4/a.out
    47. * 2 <null> /home/wjc/test/test4/a.out