GDB调试-多进程

来源:互联网 发布:四象探测器 算法 编辑:程序博客网 时间:2024/05/29 13:48

一、gdb常用命令

二、GDB与多进程

#include<stdio.h>#include<sys/types.h>#include<stdlib.h>int main(){    pid_t id = fork();    if(id < 0 )    {        perror("fork");        exit(-1);    }else if(id == 0)    {        printf("child id is %d, my father id is %d\n", getpid(), getppid());    }else    {        sleep(1);        printf("father id id %d\n", getpid());        wait(NULL);    }    return 0;}

默认设置下,在调试多进程程序时GDB只会调用主进程,但是GDB(>V7.0)支持多进程的分别与同步调试。即GDB支持同时调试多个进程。只需要设置follow-fork-mode(默认为 parent)和detach-on-fork(默认为:on)即可。

follow-fork-mode    detach-on-fork       说明

       parent                     on                 只调试主进程(GDB默认)

       child                        on                 只调试子进程

       parent                     off                 同时调试两个进程,gdb跟主进程,子进程block(阻塞)在fork位置

       child                        off                 同时调试两个进程,gdb跟子进程,主进程block在fork位置

设置方法: set follow-fork-mode[parent|child]      set detach-on-fork[on|off]

显示:show follow-fork-mode        show detach-on-fork

查询正在调试的进程:info    inferiors

显示GDB调试的所有inferior,GDB为他们分配ID。其中带*的进程是正在调试的进程。

       (GDB将每一个被调试程序的执行状态记录在一个名为inferior的结构中。一般情况下一个inferior对应一个进程,每一个inferior都有自己的地址空间。inferior有时候会在进程没有启动时就存在)

切换调试的进程:inferior <inferior number>

通过该指令可以切换到ID为number的inferior进行调试。

调加新的调试进程:add-inferior[-copies n][-exec executable]

可以用file+executable来分配给inferior可执行文件。+增加n个inferior并执行程序为executable。如果不指定n只增加一个inferior。如果不指定executable,则执行程序留空,增加后可使用file命令重新指定执行程序。这时候创建的inferior其关联的进程并没启动。

删除一个infnum 的inferior:remove-inferior infnum (如果inferior正在运行,则不能删除,删除之间需先kill或detach掉该inferior)

detach掉infnum的inferior:detach inferior infnum注意(inferior仍然存在,可以用run等命令执行)

kill 掉infnum的inferior:kill inferior infnum  注意(inferior仍然存在,可以用run等命令执行)

cfc:/app/tmp>gcc some.c -o some -gcfc:/app/tmp>gdb some             GNU gdb (GDB) Red Hat Enterprise Linux (7.2-90.el6)Copyright (C) 2010 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law.  Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-redhat-linux-gnu".For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>...Reading symbols from /app/tmp/some...done.(gdb) set follow-fork-modeRequires an argument. Valid arguments are child, parent.(gdb) set follow-fork-mode child(gdb) set detach-on-fork off(gdb) show follow-fork-modeDebugger response to a program call of fork or vfork is "child".(gdb) show detach-on-forkWhether gdb will detach the child of a fork is off.(gdb) info inferiors  Num  Description       Executable        * 1    <null>            /app/tmp/some     (gdb) break some.c:14Breakpoint 1 at 0x4006e5: file some.c, line 14.(gdb) add-inferiors -copies 3 -exec /app/tmp/someUndefined command: "add-inferiors".  Try "help".(gdb) add-inferior -copies 3 -exec /app/tmp/some Added inferior 2Reading symbols from /app/tmp/some...done.Added inferior 3Reading symbols from /app/tmp/some...done.Added inferior 4Reading symbols from /app/tmp/some...done.(gdb) info inferiors                               Num  Description       Executable          4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    <null>            /app/tmp/some     * 1    <null>            /app/tmp/some     (gdb) runStarting program: /app/tmp/some [New process 16964]Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64[Switching to process 16964]Breakpoint 1, main () at some.c:1414                      printf("child id is %d, my father id is %d\n", getpid(), getppid());Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64(gdb) nchild id is 16964, my father id is 1696121              return 0;(gdb) info inferiors  Num  Description       Executable        * 5    process 16964     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    <null>            /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) remove inferiors 5No symbol "inferiors" in current context.(gdb) remove-inferiors 5 Undefined command: "remove-inferiors".  Try "help".(gdb) remove-inferiors infnumUndefined command: "remove-inferiors".  Try "help".(gdb) detach inferior 5Detaching from program: /app/tmp/some, process 16964(gdb) info inferiors      Num  Description       Executable        * 5    <null>            /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    <null>            /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) kill inferior 5Inferior has no threads.(gdb) inferior 2[Switching to inferior 2 [process 0] (/app/tmp/some)](gdb) info bNum     Type           Disp Enb Address            What1       breakpoint     keep y   <MULTIPLE>                 breakpoint already hit 1 time1.1                         y     0x00000000004006e5 in main at some.c:14 inf 51.2                         y     0x00000000004006e5 in main at some.c:14 inf 41.3                         y     0x00000000004006e5 in main at some.c:14 inf 31.4                         y     0x00000000004006e5 in main at some.c:14 inf 21.5                         y     0x00000000004006e5 in main at some.c:14 inf 1(gdb) info inferiors   Num  Description       Executable          5    <null>            /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some     * 2    <null>            /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) runStarting program: /app/tmp/some [New process 17010][Switching to process 17010]Breakpoint 1, main () at some.c:1414                      printf("child id is %d, my father id is %d\n", getpid(), getppid());Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64(gdb) info inferiors  Num  Description       Executable        * 6    process 17010     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) kill inferior 6(gdb) info inferiors   Num  Description       Executable        * 6    <null>            /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) runStarting program: /app/tmp/some [New process 17012][Switching to process 17012]Breakpoint 1, main () at some.c:1414                      printf("child id is %d, my father id is %d\n", getpid(), getppid());Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64(gdb) info inferiors  Num  Description       Executable        * 7    process 17012     /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) runThe program being debugged has been started already.Start it from the beginning? (y or n) nProgram not restarted.(gdb) nchild id is 17012, my father id is 1701121              return 0;(gdb) n22      }(gdb) n0x0000003d8a21ed1d in __libc_start_main () from /lib64/libc.so.6(gdb) nSingle stepping until exit from function __libc_start_main,which has no line number information.Program exited normally.(gdb) The program is not being run.(gdb) The program is not being run.(gdb) info inferiors  Num  Description       Executable        * 7    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) inferior 2[Switching to inferior 2 [process 17009] (/app/tmp/some)][Switching to thread 3 (process 17009)] #0  0x0000003d8a2accbd in fork () from /lib64/libc.so.6(gdb) inferior 3[Switching to inferior 3 [process 0] (/app/tmp/some)](gdb) info inferiors  Num  Description       Executable          7    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some     * 3    <null>            /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) runStarting program: /app/tmp/some [New process 17036][Switching to process 17036]Breakpoint 1, main () at some.c:1414                      printf("child id is %d, my father id is %d\n", getpid(), getppid());Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.192.el6.x86_64(gdb) info inferiors  Num  Description       Executable        * 8    process 17036     /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    process 17035     /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) schild id is 17036, my father id is 1703521              return 0;(gdb) s22      }(gdb) s0x0000003d8a21ed1d in __libc_start_main () from /lib64/libc.so.6(gdb) sSingle stepping until exit from function __libc_start_main,which has no line number information.Program exited normally.(gdb) info inferiors  Num  Description       Executable        * 8    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    process 17035     /app/tmp/some       2    process 17009     /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) kill inferior 2(gdb) info inferiors   Num  Description       Executable          8    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    process 17035     /app/tmp/some     * 2    <null>            /app/tmp/some       1    process 16961     /app/tmp/some     (gdb) kill inferior 3(gdb) kill inferior 1(gdb) info inferiors   Num  Description       Executable          8    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some       2    <null>            /app/tmp/some     * 1    <null>            /app/tmp/some     (gdb) remove-inferior 2(gdb) info inferiors     Num  Description       Executable          8    <null>            /app/tmp/some       6    process 17011     /app/tmp/some       4    <null>            /app/tmp/some       3    <null>            /app/tmp/some     * 1    <null>            /app/tmp/some     (gdb) 


原创粉丝点击