GDB多进程调试

gdb多进程调试

下面以process.c为例

#include<stdio.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
 
int main()
{
    pid_t pid = fork();//创建子进程

    if(pid == -1)
    {
        perror("fork error");
        return -1;
    }
    else if(pid == 0)//child
    {
        printf("i am a child:my pid is %d,my father is %d\n",getpid(),getppid());
    }
    else//father
    {
        printf("i am a father:my pid is %d\n",getpid());
        wait(NULL);//等待子进程
    }

    return 0;

}

1.编译

gcc -g process.c -o process  //一定要加-g 把符号表载入代码

2.启动gdb

gdb process

在这里插入图片描述

3.下断点(我们以man函数为例)

b main

在这里插入图片描述

4.运行

r

在这里插入图片描述

5.选择是走子进程还是父进程

fork()函数
返回值为0:子进程
返回值大于0:父进程
返回值小于0:创建失败
函数运行是他自己是走父进程还是子进程是不确定的,所以我们需要让他按照自己的想法运行,这是我们就按自己的需要是走父进程还是子进程
跟着父进程去运行
set follow-fork-mode parent  
跟着子进程运行
set follow-fork-mode child
设置之后我们就n往下运行就可以进入子进程

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45309916/article/details/107436648