Linux(十四)线程控制(gdb调试多线程程序)

GDB默认支持调试多线程,跟主线程
查询线程:info threads
切换调试线程: thread +线程编号
测试函数

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>

void *rout(void *arg)
{
    int i;
    for(;;)
    {
        printf("i am thread 1\n");
        sleep(1);
    }
}
int main()
{
    pthread_t tid;
    int ret;
    if((ret = pthread_create(&tid,NULL,rout,NULL)) !=0)
    {
        fprintf(stderr,"prhread_create :%s\n",strerror(ret));
        exit(1);
    }
    int i;
    for(;;)
    {
        printf("i am main thread\n");
        sleep(1);
    }
}

首先我们gcc -g 编译我们的程序
产生一个a.out的可执行程序

这里写图片描述

我们输入l 可以查看代码
这里写图片描述

我们在线程创建后打一个断点
这里写图片描述

然后我们 r 让程序跑起来
这里写图片描述
下面我们就可以输入 info threads
查看线程
这里写图片描述

现在* 在线程一的前面
我们要是想调试线程2 就需要输入thread 2
这里写图片描述
此时我们就可以看到,我们到了线程二的调试


gdb调试很重要,也是比较困难的,需要我们经常练习才能熟悉,所以加油

猜你喜欢

转载自blog.csdn.net/mignatian/article/details/80354121