GDB 调试总结

来源:互联网 发布:排序算法总结 编辑:程序博客网 时间:2024/04/30 22:25

总结和区别(我的理解): 

1、默认当某个线程遇到break后所有的其他线程也会停止,该线程continue后所有其他线程也会一起执行,如果不想让其他线程一起执行

那么可以使用线程锁模式(该模式指管continue的时候其他线程是否会同步一起执行),该模式的开启必须在已经停在某个断点后开可以。

2、默认当某个线程遇到break后所有的其他线程也会停止,加入不想让其他没有遇到断点的线程停止,那么需要开启non-stop模式。

3、线程锁是控制是否一起执行,开启后只有一个会执行;non-stop是控制遇到break后其他没有遇到断点的线程是否停止。两者是有区别的。

4、这两个是不是一般调试只能用一个呢,还是可以一起用我还不太清楚。。。。。。


下面是摘抄网上的,有些改动自己加上了。

1.线程锁

set scheduler-locking on/off 打开关闭
7.0以前的gdb默认当程序继续运行的时候如果有断点,那么就把所有的线程都
停下来,直到你指定某个线程继续执行(thread thread_no apply continue).
但是如果直接在当前线程执行continue的话,默认是会启动所有线程。
所以如果有多个线程都是断在同一个断点(函数)上,就是说这个函数是多线程的,
那么调试的时候就会出现异常情况。所以需要打开线程锁。
只让当前的线程执行,这意味着其他所有线程都不能执行了!

设置要求:需要在设置完断点并且gdb已经停在断点位置后设置,否则出现Target 'None' cannot support this command.
//把gdb固定在当前线程

set scheduler-locking on   


2. non-stop模式
set target-async 1
set pagination off
set non-stop on

同1不一样,gdb启动了不停模式,其实就是说,除了断点有关的线程会被停下来,
其他线程会执行执行。在网络程序调试的时候比较有用!

说白了就是,一个线程break在一个断点后,其他的线程如果没有断点是会继续运行的。

需要注意着个设置需要在run之前就设置好,否则不行。

3.所以可以根据gdb的版本及你的实际代码情况选择到底是线程锁还是non-stop模式

4. 屏蔽SIGPIPE
gdb中的套接字等关闭了,往里面写就会报错!
 handle SIGPIPE nostop noprint ignore

最后附上一段代码可以供大家测试使用,我自己的测试就懒得再写上,关键是尝试才会有理解!!!!

#include "stdio.h"#include "stdlib.h"#include "unistd.h"#include "fcntl.h"#include "sys/types.h" #include <pthread.h>  int a(void){      sleep(2);      return 0;  }    int b(){      a();      return 0;  }    int c(){      b();      return 0;  }  void *myThread1(void)  {      int i=9;      while(i>0)      {          printf("Our 1st pthread,created by chn89.\n");          sleep(2);          i--;          c();      }      pthread_exit(0);  }    void *myThread2(void)  {      int i=5;      while(i>0)      {          printf("Our 2st pthread,created by chn89.\n");          sleep(2);          i--;      }      pthread_exit(0);  }    int main()  {      int  ret=0;      pthread_t thread_id1,thread_id2;          ret = pthread_create(&thread_id1, NULL, (void*)myThread1, NULL); //这里笔误,应为thread_id1 就是调试这里的错误      if (ret)      {          printf("Create pthread error!\n");          return 1;      }          ret = pthread_create(&thread_id2, NULL, (void*)myThread2, NULL);      if (ret)      {          printf("Create pthread error!\n");          return 1;      }          pthread_join(thread_id1, NULL);      pthread_join(thread_id2, NULL);          return 0;  }

编译时使用命令 gcc -g test.c -o test -lpthread

0 0