首页 > 系统相关 >GDB多进程调试

GDB多进程调试

时间:2023-01-27 18:45:46浏览次数:48  
标签:process gdb GDB chapter02 nowcoder 进程 hello 调试

内容来自牛客C++高薪面试项目2.5节,视频链接:https://www.nowcoder.com/study/live/504/2/5

命令简介

使用GDB 调试的时候,GDB 默认只能跟踪一个进程,可以在fork 函数调用之前,通过指令设置GDB 调试工具跟踪父进程或者是跟踪子进程,默认跟踪父进程。

  • 设置调试父进程或者子进程:set follow-fork-mode [parent(默认)| child]
  • 设置调试模式:set detach-on-fork [on | off]
    • 默认为on,表示调试当前进程的时候,其它的进程继续运行
    • 如果为off,调试当前进程的时候,其它进程被GDB 挂起。
  • 查看调试的进程:info inferiors
  • 切换当前调试的进程:inferior id
  • 使进程脱离GDB 调试:detach inferiors id

实例

  • 编译文件:gcc hello.c -o hello -g
  • 启动GDB调试:gdb hello
  • 分别在父进程和子进程里面打断点,并查看断点信息:
    (gdb) b 10
    Breakpoint 1 at 0x4006b9: file hello.c, line 10.
    (gdb) b 20
    Breakpoint 2 at 0x40070b: file hello.c, line 20.
    (gdb) i b
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x00000000004006b9 in main at hello.c:10
    2       breakpoint     keep y   0x000000000040070b in main at hello.c:20
    
  • 启动调试程序,默认是对父进程代码进行的调试,使用 set follow-fork-mode child切换到子进程:
    (gdb) r
    Starting program: /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
    begin
    [Attaching after process 21220 fork to child process 21220]
    [New inferior 3 (process 21220)]
    [Detaching after fork from parent process 21219]
    [Inferior 2 (process 21219) detached]
    我是父进程:pid = 21219, ppid = 20241
    i = 0
    [Switching to process 21220]
    
    Breakpoint 2, main () at hello.c:20
    20              printf("我是子进程:pid = %d, ppid = %d\n", getpid(), getppid());
    (gdb) i = 1
    i = 2
    i = 3
    i = 4
    i = 5
    i = 6
    i = 7
    i = 8
    i = 9
    n
    我是子进程:pid = 21220, ppid = 1
    23              for(j = 0; j < 10; j++) {
    (gdb) n
    24                  printf("j = %d\n", j);
    (gdb) n
    j = 0
    25                  sleep(1);
    (gdb) n
    23              for(j = 0; j < 10; j++) {
    (gdb) n
    24                  printf("j = %d\n", j);
    (gdb) c
    Continuing.
    j = 1
    j = 2
    j = 3
    j = 4
    j = 5
    j = 6
    j = 7
    j = 8
    j = 9
    [Inferior 3 (process 21220) exited normally]
    
  • 在对子进程进行调试时,父进程是会继续运行的,使用 set detach-on-fork off挂起父进程:
    (gdb) set detach-on-fork off
    (gdb) r
    Starting program: /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
    begin
    [Attaching after process 22326 fork to child process 22326]
    [New inferior 4 (process 22326)]
    [Switching to process 22326]
    
    Breakpoint 2, main () at hello.c:20
    20              printf("我是子进程:pid = %d, ppid = %d\n", getpid(), getppid());
    Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
    (gdb) n
    我是子进程:pid = 22326, ppid = 22325
    23              for(j = 0; j < 10; j++) {
    (gdb) n
    24                  printf("j = %d\n", j);
    (gdb) n
    j = 0
    25                  sleep(1);
    (gdb) n
    23              for(j = 0; j < 10; j++) {
    (gdb) n
    24                  printf("j = %d\n", j);
    (gdb) c
    Continuing.
    j = 1
    j = 2
    j = 3
    j = 4
    j = 5
    j = 6
    j = 7
    j = 8
    j = 9
    [Inferior 4 (process 22326) exited normally]
    
  • 使用 info inferiors查看当前调试的进程,并使用 inferior id切换到父进程,注意这里的id是 info inferiors显示出来的num:
    (gdb) r
    Starting program: /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
    begin
    [Attaching after process 22853 fork to child process 22853]
    [New inferior 5 (process 22853)]
    Reading symbols from /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello...done.
    [Switching to process 22853]
    
    Breakpoint 2, main () at hello.c:20
    20              printf("我是子进程:pid = %d, ppid = %d\n", getpid(), getppid());
    Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64
    (gdb) info inferiors 
      Num  Description       Executable  
    * 5    process 22853     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      4    process 22852     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      3    process 22325     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      2    <null>            /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      1    <null>            /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
    (gdb) n
    我是子进程:pid = 22853, ppid = 22852
    23              for(j = 0; j < 10; j++) {
    (gdb) inferior 4
    [Switching to inferior 4 [process 22852] (/home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello)]
    [Switching to thread 3 (process 22852)] 
    #0  0x00007ffff7ad2a02 in fork () from /lib64/libc.so.6
    (gdb) n
    Single stepping until exit from function fork,
    which has no line number information.
    
    Breakpoint 1, main () at hello.c:10
    10              printf("我是父进程:pid = %d, ppid = %d\n", getpid(), getppid());
    (gdb) info inferiors 
      Num  Description       Executable  
      5    process 22853     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
    * 4    process 22852     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      3    process 22325     /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      2    <null>            /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello 
      1    <null>            /home/wzy1999/cpp/nowcoder/chapter02/lesson03-05/hello
    
  • 使用 detach inferiors id可以使进程脱离GDB调试,和continue的效果类似,进程执行完之后,Description会变成 <null>

标签:process,gdb,GDB,chapter02,nowcoder,进程,hello,调试
From: https://www.cnblogs.com/Friends-A/p/17069155.html

相关文章

  • Windows上使用VSCode结合mingw编译和调试C程序
    Windows上使用VSCode结合mingw编译和调试C程序目标是在Win7上,用VSCode使用mingw调试C程序.要设置终端为bash.exe.Windows7已经安装了VSCode(及相关插件),MSYS64,......
  • VScode中调试Unity【Debugger for Unity】
    我遇到的情况:在点击运行和调试时,出现中间栏让你选择调试器,我点击UnityDebugger没反应。单击创建Launch文件也无效,没有UnityDebugger的选项删除.vscode/launch.json如......
  • C#之结束指定进程!...
    主要代码如下://结束进程函数publicboolendProcess(StringstrPro){//创建进程数组,用于保存系统中所有的进程Process[]p=......
  • 函数的封装之获取进程句柄...
    现在写程序,经常要获取进程的句柄,为此我将它封装成了一个函数,呵呵,以后直接Copy就可以用了...HANDLEGetProHandle::getProcessHandle(CStringcaption){//用于保存指定窗口......
  • (调试)图像调试基本术语
    图像调试基本术语镜头光学镜头光学焦距与光轴平行的光线射入到凸透镜时,理想的镜头应该是所有的光线聚集到一点,再以锥形扩散开来,这个聚集所有光线的一点,就叫做焦点。焦点......
  • adb无线连接手机(红米)解除安卓12进程限制
    1、首先是adb版本需要新的,老的adb不支持无线连接,如:“adbpair192.168.3.x:xxxxx”不支持pair这个参数,这个参数表示验证配对码!!!新的adb才支持。 无法识别pair参数 2、......
  • Day12 - 多任务编程【进程】
    0.多任务的概念多任务是指在同一时间内执行多个任务,例如:现在电脑安装的操作系统都是多任务操作系统,可以同时运行着多个软件。1.多任务介绍多任务为提高程序的执行效......
  • Pytorch:单卡多进程并行训练
    1导引我们在博客《Python:多进程并行编程与进程池》中介绍了如何使用Python的multiprocessing模块进行并行编程。不过在深度学习的项目中,我们进行单机多进程编程时一般不......
  • 进程间通信常见方式(4种,这里说管道、fifo、共享存储映射)
    学习地址IPC即进程间通信进程间通信框架如下:进程间通信方式如下:管道得有血缘关系管道前提作用于有血缘关系的进程间插曲mkfifof1实现原理内核借助环形......
  • python进程绑定CPU的意义
        ===================================== 相关:python进程绑定CPU的一些Demo【转载】python进程绑定CPU......