gdb fork 多进程调试

来源:互联网 发布:stc15f104w数据手册 编辑:程序博客网 时间:2024/06/05 10:20

实例代码

[cpp] view plain copy
  1.  1 #include <unistd.h>  
  2.  2 #include <stdio.h>   
  3.  3 int main ()   
  4.  4 {   
  5.  5     pid_t fpid;  
  6.  <span style="color:#3333ff;">6     printf("aaaaaaaaaaaaaaaaaa!\n");</span>  
  7.  7     fpid=fork();   
  8.  8     printf("bbbbbbbbbbbbbbbbbb!\n");  
  9.  9     if (fpid < 0)   
  10. 10         printf("error in fork!\n");  
  11. 11     else if (fpid == 0) {  
  12. lt;span style="color:#3333ff;"> 12         printf("i am the child process, my process id is %d\n",getpid());</span>  
  13. 13     } else {  
  14. lt;span style="color:#3333ff;"> 14         printf("i am the parent process, my process id is %d, my child process id is %d\n",getpid(), fpid);</span>  
  15. 15     }     
  16. 16     return 0;  
  17. 17 }    

fork多进程调试一般有一下3种方法:


1. follow-fork-mode

用法:set follow-fork-mode [parent|child] 
进入gdb后,直接设置,默认是parent
所以如果想要调试子进程,进入gdb后设置set follow-fork-mode child,然后设置子进程的断点
可用使用show follow-fork-mode 来查询当前fork模式
如果在6.12.14行加断点,r后会停在6行处

使用follow-fork-mode,只能调试一个进程,不能同时调试父子进程

2. detach-on-fork mode
用法:set detach-on-fork [on|off]
on: 只调试父进程或子进程的其中一个(根据follow-fork-mode来决定),这是默认的模式。
off: 父子进程都在gdb的控制之下,其中一个进程正常调试(根据follow-fork-mode来决定),另一个进程会被设置为暂停状态。
如果设置了set detach-on-fork off且follow-fork-mode为parent,fork后子进程并不运行,而是处于暂停状态。
看一下log就明白了

注意,最好使用GDB 6.6或以上版本,如果你使用的是GDB6.4,就只有follow-fork-mode模式。

3. attach后多进程调试
    上述方法还是不能同时调试两个进程,假设调试主进程,fork后,子进程已经不知道运行到何处了,所以这就需要让子进程先暂停一下。暂停的方法有很多
    1> 最简单的就是sleep一下
    else if(fpid == 0)  {// child process
          sleep(10); // 给你足够的时间去find子进程id(pstree -ap |  grep a.out,然后attach 
    }
  
    2> 加一段等待代码
    
[plain] view plain copy
  1. void debug_wait(char *tag)  
  2. {  
  3.     while(1)  
  4.     {  
  5.         if (tag存在) <span style="color:#ff0000;">// tag可以是一个环境变量,也可以是一个文件等</span>  
  6.             睡眠一段时间;  
  7.         else  
  8.             break;  
  9.     }  
  10. }  
   
    3> chromium中
     zygote进程在创建render进程时,正好需要zygote的real PID
[plain] view plain copy
  1. if (pid == 0) {  
  2.   // In the child process.  
  3.   LOG(ERROR) << "child process(type="<<process_type<<") pid="<<syscall(SYS_gettid);  
  4.   write_pipe.reset();  
  5.   
  6.   // Ping the PID oracle socket so the browser can find our PID.  
  7.   CHECK(SendZygoteChildPing(pid_oracle.get()));  
  8.   
  9.   // Now read back our real PID from the zygote.  
  10.   base::ProcessId real_pid;  
  11.   if (!base::ReadFromFD(read_pipe.get(),    <span style="color:#ff0000;">// 等待</span>  
  12.                         reinterpret_cast<char*>(&real_pid),  
  13.                         sizeof(real_pid))) {  
  14.     LOG(FATAL) << "Failed to synchronise with parent zygote process";  
  15.   }  




参考
http://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/

原创粉丝点击