gdb多线程调试

本文介绍了在Linux环境下如何查看和调试多线程程序。通过示例代码展示了创建线程的基本步骤,并提供了使用`ps`、`pstree`命令观察线程,以及使用`gdb`进行线程信息查看和调试的方法。同时,详细列举了`gdb`调试多线程程序的相关命令,包括切换线程、设置断点、查看线程栈结构等。
摘要由CSDN通过智能技术生成

更多linux知识点:linux目录索引


1. 线程的查看

首先创建两个线程:

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

void* pthread_run1(void* arg)
{
(void)arg;

<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)
{
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"I am thread1,ID: %d\n"</span>,pthread_self());
    sleep(<span class="hljs-number">1</span>);
}

}

void* pthread_run2(void* arg)
{
(void)arg;

<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)
{
    <span class="hljs-built_in">printf</span>(<span class="hljs-string">"I am thread2,ID: %d\n"</span>,pthread_self());
    sleep(<span class="hljs-number">1</span>);
}

}

int main()
{

pthread_t tid1;
pthread_t tid2;

pthread_create(&amp;tid1,NULL,pthread_run1,NULL);
pthread_create(&amp;tid2,NULL,pthread_run2,NULL);

<span class="hljs-built_in">printf</span>(<span class="hljs-string">"I am main thread\n"</span>);

pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

分析:上面程序中创建了两个线程,程序执行起来,main函数所在程序为主线程,在这个主线程中有两个新线程运行

命令行查看:

//查看当前运行的进程
ps aux|grep a.out
//查看当前运行的轻量级进程
ps -aL|grep a.out
//查看主线程和新线程的关系
pstree -p 主线程id
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这里写图片描述

2. 线程栈结构的查看
1. 获取线程ID
2. 通过命令查看栈结构 ps stack 线程ID
 
 
  • 1
  • 2

这里写图片描述

3. 利用gdb查看线程信息
  1. 将进程附加到gdb调试器当中,查看是否创建了新线程:gdb attach 主线程ID

    这里写图片描述

  2. 查看线程的一些信息

//1.查看进程:info inferiors
//2.查看线程:info threads
//3.查看线程栈结构:bt
//4.切换线程:thread n(n代表第几个线程)
 
 
  • 1
  • 2
  • 3
  • 4

这里写图片描述

4. 利用gdb调试多线程

  当程序没有启动,线程还没有执行,此时利用gdb调试多线程和调试普通程序一样,通过设置断点,运行,查看信息等等,在这里不在演示,最后会加上调试线程的命令

  1. 设置断点

    //1. 设置断点:break 行号/函数名
    //2. 查看断点:info b
       
       
    • 1
    • 2

    这里写图片描述

  2. 执行线程2的函数,指行完毕继续运行到断点处

    1. 继续使某一线程运行:thread apply 1-n(第几个线程) n
    2. 重新启动程序运行到断点处:r
       
       
    • 1
    • 2

    这里写图片描述

  3. 只运行当前线程

    1. 设置:set scheduler-locking on
    2. 运行:n
       
       
    • 1
    • 2

    这里写图片描述

  4. 所有线程并发执行

        1. 设置:set scheduler-locking off
        2. 运行:n
       
       
    • 1
    • 2

    这里写图片描述

总结调试多线程的命令

命令用法
info threads显示当前可调试的所有线程,每个线程会有一个GDB为其分配的ID,后面操作线程的时候会用到这个ID。 前面有*的是当前调试的线程
thread ID(1,2,3…)切换当前调试的线程为指定ID的线程
break thread_test.c:123 thread all(例:在相应函数的位置设置断点break pthread_run1)在所有线程中相应的行上设置断点
thread apply ID1 ID2 command让一个或者多个线程执行GDB命令command
thread apply all command让所有被调试线程执行GDB命令command
set scheduler-locking 选项 command设置线程是以什么方式来执行命令
set scheduler-locking off不锁定任何线程,也就是所有线程都执行,这是默认值
set scheduler-locking on只有当前被调试程序会执行
set scheduler-locking on step在单步的时候,除了next过一个函数的情况(熟悉情况的人可能知道,这其实是一个设置断点然后continue的行为)以外,只有当前线程会执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值