gdb调试

来源:互联网 发布:oracle数据库代理商 编辑:程序博客网 时间:2024/06/09 22:14

gdb多进程调试

我们可以对fork出来的进程进行设置调试方法。

follow-fork-mode的用法为:

set follow-fork-mode parent/child
  • 1
  • 1

如果你选择了parent,这个时候就是进行gdb调试父进程。 
如果你选择了child,这个时候就是进行gdb调试子进程。

示例程序:

#include<stdio.h>#include<unistd.h>#include<stdlib.h>int main(){    int count =0;    pid_t id=fork();    if(id>0)    {        while(count++)        {            if(count>3)            {                break;            }        }    }    else if(id==0)    {        int i=0;        while(i<3)        {            i++;            count--;        }    }    else    {        perror("fork");        exit(1);    }    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

记得在编译代码的时候要加上-g选项。

然后我们gdb打开调试文件。

我们来进行调试子进程, 
然后我们进行打断点进行调试,观察count的变化。

首先打断点 

(gdb) b 7

然后运行至断点处。 

(gdb) r

然后我们进行单步执行, 

(gdb) n

我们发现调试跳过了父进程。 进入了子进程。 
 
接下来我们就可以调试子进程的代码了。 

(gdb) n

上述就是进行子进程程序代码的调试,父进程的调试当然和子进程是一样的,所以我这里就不多说了。并且记得啊,如果你在调试的过程中更改mode是没有用的,只对下一次fork以后是起作用的。


gdb多线程调试

关于多线程的调试中gdb的一些命令:

1. info threads:显示当前可调式的所有线程,gdb为每一个线程分配ID,这个ID后续操作的时候进行使用。 

2. thread ID :切换到当前调试的线程的指定ID。

3. set scheduler-locking off|on|step :在使用step或者continue命令调试当前被调试线程的时候,其他线程也是同时执行的,通过这个命令就可以就可以实现这个要求,off是不锁定任何线程,也就是所有的线程都执行,这个是默认值。on是只有当前线程执行。step是在单步的时候,除了next过一个函数的情况意外,只有当前线程会执行。

4. set print thread-events :用于设定是否提示线程启动或停止时信息。 

5. thread apply ID command :让ID线程执行命令command。 

6. thread apply all command :让所有线程执行命令command。 

原创粉丝点击