gdb调试多进程多线程程序

来源:互联网 发布:钩尖江湖直销淘宝店 编辑:程序博客网 时间:2024/05/05 07:03

1.gdb的使用

(1)Linux环境下对于代码的调试都是使用gdb进行调试,使用的时候在源代码生成的时候加上-g选项。

(2)开始使用:gdb binFile(binFile为源文件名)

         退出:ctrl+d或quit

(3)常用命令

        

2.多进程/多线程代码

(1)编写多进程/多线程代码

           

(2)编写Makefile

             

3.gdb一些说明

(1)show follow-fork-mode

          通过show follow-fork-mode命令查看当前模式:

         

          gdb是默认执行父进程的。

(2)set follow-fork-mode[child|parent]、set detach-on-fork[on|off]

         通过set follow-fork-mode[child|parent]、set detach-on-fork[on|off]修改gdb调试模式:

         

              parent                   on               只调试主进程(GDB默认)
              child                     on               只调试子进程
              parent                   off              同时调试两个进程,gdb跟主进程,子进程block在fork位置
              child                     off              同时调试两个进程,gdb跟子进程,主进程block在fork位置

(3)info inferiors

         查询正在调试的进程

(4)inferior <infer number>

         切换调试的进程

  4.调试      

(1)调试主进程,block子进程。

(gdb) set detach-on-fork off
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) catch fork
Catchpoint 1 (fork)
(gdb) r
[Thread debugging using libthread_db enabled]

Catchpoint 1 (forked process 3475), 0x00110424 in __kernel_vsyscall ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) break test.c:14
Breakpoint 2 at 0x8048546: file test.c, line 14.
(gdb) cont
[New process 3475]
[Thread debugging using libthread_db enabled]

Breakpoint 2, main (argc=1, argv=0xbffff364) at test.c:14
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) info inferiors
  Num  Description       Executable       
  2    process 3475      /home/superficial/code/gdb调试
* 1    process 3472    /home/superficial/code/gdb调试

(2) 切换到子进程:
(gdb) inferior 2
[Switching to inferior 2 [process 3475] ( /home/superficial/code/gdb调试)]
[Switching to thread 2 (Thread 0xb7fe86c0 (LWP 3475))]
#0  0x00110424 in ?? ()
(gdb) info inferiors
  Num  Description       Executable       
* 2    process 3475       /home/superficial/code/gdb调试
  1    process 3472       /home/superficial/code/gdb调试
(gdb) inferior 1
[Switching to inferior 1 [process 3472] ( /home/superficial/code/gdb调试)]
[Switching to thread 1 (Thread 0xb7fe86c0 (LWP 3472))]
#0  main (argc=1, argv=0xbffff364) at test.c:14
(gdb) info inferiors
  Num  Description       Executable       
  2    process 3475       /home/superficial/code/gdb调试
* 1    process 3472      /home/superficial/code/gdb调试

原创粉丝点击