在 C 语言中创建多进程程序需要使用 fork 相关的一些函数,调用一次 fork 函数就会创建一个进程。多进程调试时,我们需要对调试的进程和未调试的进程进行设置。下面介绍的一些命令是我们在调试时经常使用到的。
GDB默认调试的是父进程,我们可以设置调试的进程,使用命令:
set follow-fork-mode <mode>
其中 mode 为设置调试的进程:可以是 child,也可以是 parent。当 mode 为 parent 时,程序在调用 fork 后调试父进程,子进程不会受到影响。当 mode 为 child 时,程序在调用 fork 后调试子进程,父进程不会受到影响。
查看 GDB 中设置的 follow-fork-mode 可以使用命令:
show follow-fork-mode
在 GDB 中调试多进程时,可以只调试一个进程,也可以同时调试两个进程,这个和 GDB 中的 detach-on-fork 的设置有关,相关的命令格式展示如下:
set detach-on-fork <mode>
mode 可以为 on,也可以为off。当 mode 为 on 时,表示程序只调试一个进程(可以是父进程、子进程),这是 GDB 的默认设置。当 mode 为 off 时,父子进程都在gdb的控制之下,其中一个进程正常的调试,另一个会被设置为暂停状态。
查看 GDB 中设置的 detach-on-fork 可以使用命令:
show detach-on-fork
GDB 将每一个被调试程序的执行状态记录在一个名为 inferior 的结构中。一般情况下一个 inferior 对应一个进程,每个不同的 inferior 有不同的地址空间。inferior有时候会在进程没有启动的时候就存在。
查看当前调试的所有的
inferior,使用命令:
info inferiors
当前调试的进程前有 “*”。
切换进程使用命令
inferior,使用方式展示如下:
inferior <num>
表示切换到 id 为 num 的 inferior。实例:
inferior 2
切换到 2 号进程。
实例:创建一个多进程的源文件。
#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <unistd.h>#include <sys/wait.h>int main(void){pid_t pid;pid = fork();if(pid < 0) {perror("fork()");}if(pid == 0) {printf("this is child,pid = %d\n",getpid());}else {printf("this is parent,pid = %d\n",getpid());}exit(0);}
我们之前在《GDB程序产生中断》中讲到过,使用捕获点 catch 命令可以捕获,当调用 fork 函数会产生中断。
相关调试信息如下:
(gdb) show follow-fork-mode //显示默认的 follow-fork-mode 配置Debugger response to a program call of fork or vfork is "parent".(gdb) show detach-on-fork //显示默认的 detach-on-fork 配置Whether gdb will detach the child of a fork is on.(gdb) set follow-fork-mode child //设置 follow-fork-mode 为 child(gdb) set detach-on-fork off //设置 detach-on-fork off 为 off(gdb) catch fork //设置捕获点中断Catchpoint 1 (fork)(gdb) run //运行程序Starting program: /home/wjc/test/test4/a.out Catchpoint 1 (forked process 2073), 0x00007ffff7ac8b1c in __libc_fork ()at ../sysdeps/nptl/fork.c:135warning: Source file is more recent than executable.(gdb) s[New process 2073]Reading symbols from /home/wjc/test/test4/a.out...done.Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.27.so...done.Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.27.so...done.__libc_fork () at ../sysdeps/nptl/fork.c:142warning: Source file is more recent than executable.(gdb) info inferiors //显示程序运行的进程Num Description Executable1 process 2069 /home/wjc/test/test4/a.out* 2 process 2073 /home/wjc/test/test4/a.out(gdb) inferior 1 //切换到一号进程[Switching to inferior 1 [process 2069] (/home/wjc/test/test4/a.out)][Switching to thread 1.1 (process 2069)]#0 0x00007ffff7ac8b1c in __libc_fork () at ../sysdeps/nptl/fork.c:135(gdb) info inferiorsNum Description Executable* 1 process 2069 /home/wjc/test/test4/a.out2 process 2073 /home/wjc/test/test4/a.out(gdb) c //执行第一个进程Continuing.this is parent,pid = 2069[Inferior 1 (process 2069) exited normally](gdb) info inferiorsNum Description Executable1 <null> /home/wjc/test/test4/a.out* 2 process 2073 /home/wjc/test/test4/a.out //第一个进程执行结束,走动切换到第二个进程(gdb) c //执行第二个进程Continuing.this is child,pid = 2073[Inferior 2 (process 2073) exited normally](gdb) info inferiorsNum Description Executable1 <null> /home/wjc/test/test4/a.out* 2 <null> /home/wjc/test/test4/a.out
