gdb调试 fork程序 设置跟踪

雨点打透心脏的1/2处 2022-07-13 01:59 94阅读 0赞

使用gdb调试的时候,gdb只能跟踪一个进程。

可以在fork函数调用之前,通过指令设置gdb调试工具跟踪父进程或子进程。

默认情况下gdb是跟踪父进程的。

set follow-fork-mode child 命令设置gdb在fork之后跟踪子进程。

set follow-fork-mode parent设置跟踪父进程。

注意:一定要在fork函数调用之前设置。

程序测试:

#include

#include

#include

int main(void)

{

pid_t pid;

printf(“你是我的削苹果\n”);

pid = fork();

if(pid==-1){

perror(“fork error”);

exit(1);

}else if(pid == 0){

printf(“child is here. pid = %u,ppid = %u\n”, getpid(), getppid());

}else{

printf(“parent is here. pid = %u,ppid = %u\n”, getpid(), getppid());

sleep(1);

}

return 0;

}

编译、调试:

yu@ubuntu:~/cplusplus/fork创建进程$ gcc fork.c -o fork -g

yu@ubuntu:~/cplusplus/fork创建进程$ gdb fork

开始调试,设置跟踪子进程:

(gdb)set follow-fork-mode child

(gdb)start

Temporarybreakpoint 1 at 0x40071f: file fork.c, line 7.

Starting program: /home/yu/cplusplus/fork创建进程/fork

Temporary breakpoint 1, main () at fork.c:7

7printf(“你是我的削苹果\n”);

(gdb)n

你是我的削苹果

9pid = fork();

(gdb)n

parentis here. pid = 2170, ppid = 2168

[Newprocess 2174]

[Switching to process 2174] //此处已经转向跟踪子进程程序

main() at fork.c:10

10if(pid==-1){

(gdb)n

13}else if(pid == 0){

(gdb)n

14printf(“child is here. pid = %u,ppid = %u\n”, getpid(), getppid());

(gdb)c

Continuing.

child is here. pid = 2174, ppid = 1

[Inferior2 (process 2174) exited normally]

发表评论

表情:
评论列表 (有 0 条评论,94人围观)

还没有评论,来说两句吧...

相关阅读

    相关 调试方法总结之gdb跟踪

    1,当我们发现我们预想的值和实际的数值不一样时,这个时候应该怎么办呢!首先我们需要确认是否调到了这个接口,如果掉到了这个预期结果的接口,那么说明可能在我们达到了预定结果之后结果

    相关 GDB调试程序(四)

    查看栈信息 ————— 当程序被停住了,你需要做的第一件事就是查看程序是在哪里停住的。当你的程序调用了一个函数,函数的地址,函数参数,函数内的局部变量都会被压入“栈”(S

    相关 GDB调试程序详解

    GDB是一个强大的命令行调试工具。大家知道命令行的强大就是在于,其可以形成执行序列,形成脚本。UNIX下的软件全是命令行的,这给程序开发提代供了极大的便利,命令行软件的优势在于