linux gdb多线程,GDB多线程调试

多线程顾名思义就是实现多个线程并发执行,简单的说就是同时处理多项任务。我们在开发过程中会经常使用到多线程,当然出现的问题也是不可避免的。我们在这个章节主要讲述的是使用 GDB 调试多线程的程序。

查看线程的相关信息

使用 GDB 调试多线程的程序时,可以使用下面的命令获取线程的信息,命令展示如下:

info threads

显示可以调试的所有线程,GDB 会为每个线程分配一个ID(和 tid 不同),编号一般从 1 开始,当前调试的线程编号前有“*”。

调试多线程

调试多线程的程序和调试普通的单线程的程序是不同的。当我们调试多线程中的某一个线程时,需要对其他线程的状态做一些设置,包括主线程调试时其他线程是否运行以及运行时需要执行的命令。下面是调试多线程命令的详细介绍。

1. 调试线程时,可以做到切换线程,使用命令:

thread [ID]

通过线程的编号切换到指定的线程。实例:

thread 2         //切换到编号为 2 的线程。

2. 为运行中的线程指定执行命令,命令格式展示如下:

thread apply [ID……] [command]

这个命令可以指定多个 ID,使用 all 表示指定所有的线程。command表示执行的命令。实例:

(gdb) thread apply 1 continue           // 1号线程执行continue命令。

(gdb) thread apply 1 2 continue         //1号和二号线程执行continue命令

(gdb) thread apply all continue         //所有的线程执行continue命令

3. 锁定执行的线程,命令格式展示如下:

set scheduler-locking [mode]

该命令表示当调试一个线程时其他的线程是否继续执行。mode 有三种选项,分别是:off、on、step。当 mode 为 off 时,表示不锁定线程,这是 GDB 的默认选项;当 mode 的值为 on 时,表示锁定其他的线程,只有当前线程执行。当 mode 为 step 时,表示如果使用 step 单步执行,只有被调试的程序执行。

在线程中设置断点

我们可以设置断点在所有的线程上或是在某个特定的线程,在GDB中使用下面的命令可以很容易完成这项工作:

break thread [ID]

break thread [ID] if ...

linespec 指定了断点设置在的源程序的行号。ID表示线程的编号。

我们还可以为某线程指定断点条件。例如:

(gdb)  break frik.c:13 thread 28 if bartab > lim

当程序被 GDB 停住时,所有的运行线程都会被停住。这方便查看运行程序的总体情况。而在恢复程序运行时,所有的线程也会被恢复运行。

实例:

#include

#include

#include

static void *thread_job(void *s)

{

printf("this is 1\n");

}

static void *thread_job1(void *s)

{

printf("this is 2\n");

}

int main(void)

{

pthread_t tid,tid1;

pthread_create(&tid, NULL, thread_job, NULL);

pthread_create(&tid1, NULL, thread_job1, NULL);

pthread_join(tid,NULL);

pthread_join(tid1,NULL);

exit(0);

}

调试信息如下:

(gdb) info breakpoints         //在三个线程中分别设置断点,显示断点信息。

Num     Type           Disp Enb Address            What

1       breakpoint     keep y   0x0000000000000766 in thread_job at test.c:9

2       breakpoint     keep y   0x0000000000000781 in thread_job1 at test.c:13

3       breakpoint     keep y   0x0000000000000803 in main at test.c:25

(gdb) run

Starting program: /home/wjc/hsxy/lianxi/10/test/a.out

[Thread debugging using libthread_db enabled]

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

[New Thread 0x7ffff77c4700 (LWP 3931)]

[New Thread 0x7ffff6fc3700 (LWP 3932)]

[Switching to Thread 0x7ffff77c4700 (LWP 3931)]

Thread 2 "a.out" hit Breakpoint 1, thread_job (s=0x0) at test.c:9

9            printf("this is 1\n");

(gdb) info threads         //显示三个线程信息,当前调试的是2号线程

Id   Target Id                                Frame

1    Thread 0x7ffff7fd1740 (LWP 3927) "a.out" 0x00007ffff7bbed2d in _

* 2    Thread 0x7ffff77c4700 (LWP 3931) "a.out" thread_job (s=0x0) at test.c:9

3    Thread 0x7ffff6fc3700 (LWP 3932) "a.out" thread_job1 (s=0x0)

at test.c:13

(gdb) thread 1         //切换到1号线程

[Switching to thread 1 (Thread 0x7ffff7fd1740 (LWP 3927))]

#0  0x00007ffff7bbed2d in __pthread_timedjoin_ex ()

from /lib/x86_64-linux-gnu/libpthread.so.0

(gdb) info threads

Id   Target Id                                Frame

* 1    Thread 0x7ffff7fd1740 (LWP 3927) "a.out" 0x00007ffff7bbed2d in __pthread_timedjoin_ex () from /lib/x86_64-linux-gnu/libpthread.so.0

2    Thread 0x7ffff77c4700 (LWP 3931) "a.out" thread_job (s=0x0) at test.c:9

3    Thread 0x7ffff6fc3700 (LWP 3932) "a.out" thread_job1 (s=0x0)

at test.c:13

(gdb) thread apply all continue          //三个线程同时执行continue命令

this is 1

Thread 3 (Thread 0x7ffff6fc3700 (LWP 4083)):

Continuing.

[Thread 0x7ffff77c4700 (LWP 4082) exited]

Thread 3 "a.out" hit Breakpoint 2, thread_job1 (s=0x0) at test.c:13

13          printf("this is 2\n");

this is 2

Thread 1 (Thread 0x7ffff7fd1740 (LWP 4081)):

Continuing.

[Thread 0x7ffff6fc3700 (LWP 4083) exited]

Thread 1 "a.out" hit Breakpoint 3, main () at test.c:25

25          exit(0);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值