gdb调试多线程

xiaoxiao2021-02-28  39

 下文参考以下链接:http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html

首先先介绍多线程调试的基本命令:

 1.info threads,显示当前可调试的所有线程。每个线程会有gdb为其分配的ID,后面的操作会用到这个ID。前面带'*'号的是当前正在调试的线程。

 2.thread ID,切换当前调试的线程为指定ID的线程。

 3.break threa锁定d_test.c:123 thread all ,在所有线程中相应的行上设置断点

 4.thread apply ID1 ID2 command ,让一个或者多个线程执行gdb命令command。 

 5.thread apply all command ,让所有被调试线程执行gdb命令command。

 6.set scheduler-locking [off|on|step]

  值得注意的是,在使用step或者continue命令调试当前被调试线程的时候,其他线程也是同时执行的,怎么只让被调试程序执行呢?通过这个命令就可以实现这个需求。

  off:不锁定任何线程,也就是所有线程都执行,这是默认值。

  on:只有当前被调试程序会执行。 

  step:在单步的时候,除了next过一个函数的情况(熟悉情况的人可能知道,这其实是一个设置断点然后continue的行为)以外,只有当前线程会执行。

 7.show scheduler-locking,查看当前锁定线程的模式。

  介绍完gdb调试多线程的基本命令,下面就让我们以一个简单的栗子来使用并熟悉这些命令吧!

  

[cpp]  view plain  copy #include<stdio.h>  #include<pthread.h>    void *pthread1_run(void *arg)  {      int count=10;      while(count--)      {          sleep(1);          printf("i am new pthread1 %lu\n",pthread_self());      }      pthread_exit(NULL);      return 0;  }    void *pthread2_run(void *arg)  {      int count=10;      while(count--)      {          sleep(1);          printf("i am new pthread2 %lu\n",pthread_self());      }      pthread_exit(NULL);      return 0;  }    int main()  {      pthread_t tid1;      pthread_t tid2;      pthread_create(&tid1,NULL,pthread1_run,NULL);      pthread_create(&tid2,NULL,pthread2_run,NULL);      pthread_join(tid1,NULL);      pthread_join(tid2,NULL);      return 0;  }    Makefile    testtid:testtid.c      gcc -o $@ $^ -lpthread -g   //-g是说明要使用gdb调试该代码,-lpthread是与线程有关函数编译链接的时候必须添加的  .PHONY:clean  clean:      rm -f testtid  

 

  1.查看当前正在调试的线程并切换线程

  

      前面的1,2,3是gdb分配的线程号,当切换线程的时候使用该线程号。最前面的'*'号说明当前正在调试的线程。

  2.让所有被调试的线程都执行同一个命令,就是打印堆栈信息

  

 

3.只让线程编号为1的线程打印堆栈信息

  

 4.锁定线程并查看当前锁定的线程

  

  

   在这里就分享结束了~~~~

转载请注明原文地址: https://www.6miu.com/read-2632432.html

最新回复(0)