linux下gdb调试多线程

来源:互联网 发布:二叉树层次遍历 java 编辑:程序博客网 时间:2024/06/06 08:40

一、多线程的调试命令
1、info threads:
这条命令显示的是当前可调试的所有线程,GDB会给每一个线程都分配一个ID。前面有*的线程是当前正在调试的线程。
2、thread ID:
切换到当前调试的线程为指定为ID的线程。
3、thread apply all command:
让所有被调试的线程都执行command命令
4、thread apply ID1 ID2 … command:
这条命令是让线程编号是ID1,ID2…等等的线程都执行command命令
5、set scheduler-locking off|on|step:
在使用step或continue命令调试当前被调试线程的时候,其他线程也是同时执行的,如果我们只想要被调试的线程执行,而其他线程停止等待,那就要锁定要调试的线程,只让它运行。
off:不锁定任何线程,所有线程都执行。
on:只有当前被调试的线程会执行。
step:阻止其他线程在当前线程单步调试的时候抢占当前线程。只有当next、continue、util以及finish的时候,其他线程才会获得重新运行的。
6、show scheduler-locking:
这条命令是为了查看当前锁定线程的模式。
二、多线程的调试的代码

#include<stdio.h>#include<pthread.h>void* thread_run1(){    printf("i am thread_run1\n");    printf("thread_run1 : %u\n",pthread_self());    return NULL;}void* thread_run2(){    printf("i am thread_run2\n");    printf("thread_run2 : %u\n",pthread_self());    return NULL;}int main(){    pthread_t tid1;    pthread_t tid2;    pthread_create(&tid1,NULL,thread_run1,NULL);    pthread_create(&tid2,NULL,thread_run2,NULL);    pthread_join(tid1,NULL);    pthread_join(tid2,NULL);    return 0;}

一、多线程的调试的过程
1、编译的时候需要注意的是,别忘了加动态链接库和-g。
这里写图片描述
2、打开gdb准备进行调试
这里写图片描述
3、在线程创建的地方打断点,然后运行至断点处
这里写图片描述
4、查看被调试的进程
这里写图片描述
5、让所有的线程都打印出堆栈的信息
这里写图片描述
6、锁定线程以及查看锁定的模式
这里写图片描述
7、实现线程间的切换
这里写图片描述

原创粉丝点击