Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74374
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 67
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-23 11:45
文章分类

全部博文(17)

文章存档

2015年(17)

我的朋友

分类: LINUX

2015-04-17 12:03:55

        ------最近看Linux程序设计的时候,看到了进程这一章,所以就上网学习了下GDB对于多进程的调试,本篇只是为了进行了一些初步的调试学习,还存在很多疑问,有不对的地方希望大家积极指出。
        
        原文在:http://blog.csdn.net/tedious/article/details/6224349 By tedious(本文是在该文的基础上做了自己的实验记录和自己的一些理解,其中源码都是来自链接中的博文)
        
       


       1.attach---调试外部进程
       先来看一段程序吧:
        muti_program.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>

  4. int wib(int no1,int no2)
  5. {
  6.         int result,diff;
  7.         diff = no1-no2;
  8.         result = no1 / diff;
  9.         return result;
  10. }

  11. int main()
  12. {
  13.         pid_t pid;

  14.         pid = fork();

  15.         if(pid<0){
  16.                 perror("fork");
  17.                 exit(-1);
  18.         }
  19.         else if(pid == 0){
  20.                 sleep(60);
  21.                 int value = 10;
  22.                 int div = 6;
  23.                 int total = 0;
  24.                 int i = 0;
  25.                 int result = 0;

  26.                 for(i=0;i<10;i++){
  27.                 result = wib(value,div);
  28.                 total +=result;
  29.                 div++;
  30.                 value--;
  31.                 }

  32.                 printf("%d wibed by %d equals %d\n",value,div,total);
  33.         }
  34.         else{
  35.                 sleep(4);
  36.                 wait(-1);//wait the son program stop
  37.                 exit(0);
  38.         }
  39. }
        可以很明显的看到在wib()函数中会有一个除0的错误;
        调试原理:在fork()函数之后,进入子进程,有一个sleep()函数。此时,子进程调用sleep(60)沉睡60秒。这个空隙是给我们
利用shell命令获得子进程PID而准备的。然后在利用GDB调试外部进程的指令attach,通过该子进程PID链接到该进程进行调试。
        调试工具:gdb-7.2-50.el6.i686
        调试过程:
        gcc -o ./bin/muti_program -g ./src/muti_program     //编译连接
        
        在后台运行muti_program,获得子进程PID,在GDB中用attach指令连接该外部进程。
        指令:
              ./bin/muti_program &    //后台运行该程序,产生一个进程,该指令会返回一个当前进程的进程号(在另外一个终端执行)
              ps aux | grep muti_program    //获得子进程号(在另外一个终端执行)
              gdb -q    //进入GDB调试环境,-q参数是选择安静模式,即不打印GDB的版本号等信息
              (gdb) attach XXXX    //连接该进程
              (gdb) stop    //暂停该进程,以便我们设置断点
              (gdb) break 32    //设置断点,可以通过list来查代码行数
              (gdb) continue    //继续执行程序,直到断电处停止
        (此处本来想贴上运行结果图,无奈不知道为什么显示不出来)
        
        
            
        2. set follow-fork-mode (parent|son)
        上面讲的attach指令是用GDB调试外部进程,意思就是程序已经在外部执行,产生了进程号,而attach指令把该正在运行的进程连接到GDB中进行调试
        而set follow-fork-mode(parent|son)则是在GDB加载了运行程序之后,在程序运行之前,指定跟踪调试父进程还是子进程。
        

点击(此处)折叠或打开

  1. #incldue <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>

  4. int main()
  5. {
  6.     pid_t pid;
  7.     pid = fork()
  8.     if(pid < 0){
  9.         perror("fork");
  10.         exit(EXIT_FAILURE);
  11.     }
  12.     else if(pid == 0)
  13.     {
  14.         printf("this is in son program\n");
  15.         exit(EXIT_SUCCESS);
  16.     }
  17.     else
  18.     {
  19.         printf("this is in parent program\n");
  20.         exit(EXIT_SUCCESS);
  21.     }    
  22.     return 0;
  23. }
        gdb) b 15
           Breakpoint 1 at 0x8048481: file main.c, line 17.
           (gdb) set follow-fork-mode child
           (gdb) r
           Starting program: /home/djkings/a.out 
           this is in parent program
           [Switching to process 30725]



        
        
         

            
        
阅读(1509) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~