在GDB中调试多线程程序时,如何一次继续一个线程?

39 人关注

我有一个使用两个线程的程序。我在两个线程中都设置了中断点。在gdb下运行程序时,我想在两个线程之间进行切换,并使它们运行。 (线程t1正在运行,线程t2;当暂停在断点上时。我想让T1停止运行并运行T2)。

有什么办法能让我在gdb中安排线程吗?

1 个评论
参见GDB手册中的 调试多线程的程序 ,特别是你想用 thread 命令来切换活动线程。
linux
multithreading
gdb
pthreads
Arpit
Arpit
发布于 2010-04-15
3 个回答
Employed Russian
Employed Russian
发布于 2010-04-15
已采纳
0 人赞同

默认情况下,当任何断点被击中时,GDB会停止所有线程,并恢复 所有 当你发出任何命令(如 continue next step finish 等),要求下级进程(你正在调试的进程)开始执行时,线程。

然而,你可以告诉GDB不要这样做。

(gdb) help set scheduler-locking 
Set mode for locking scheduler during execution.
off  == no locking (threads may preempt at any time)
on   == full locking (no thread except the current thread may run)
step == scheduler locked during every single-step operation.
    In this mode, no other thread may run during a step command.
    Other threads may run while stepping over a function call ('next').

所以你要设置断点,然后set scheduler-locking on,然后在线程1中continuefinish(线程2仍然停止)。然后按Ctrl-C重新获得GDB的控制权,切换到线程2,continue(线程1仍然停止),等等。

注意:通过设置scheduler-locking on,很容易导致低级程序自我停顿。

PFee
PFee
发布于 2010-04-15
0 人赞同

如果你使用的是GDB 7或更高版本,请尝试 "不间断模式"。

http://sourceware.org/gdb/current/onlinedocs/gdb/Non_002dStop-Mode.html

之前提到的 "scheduler-locking on "命令允许你在其他线程停止的情况下步入一个线程。 不停顿模式允许你在其他线程处于活动状态的情况下步入一个线程。

这似乎与问题相反,但无论如何,知道这个命令的存在是件好事。
fabrizioM
fabrizioM
发布于 2010-04-15
0 人赞同

使用中断条件

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

see Debugging with GDB

Edit:

(gdb) break <thread_function_entry_point> thread 2
(gdb) break <thread_function_entry_point> thread 1
(gdb) thread 1
(gdb) continue
(gdb) ... thread 1 finishes