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

问题描述 投票:25回答:3

我有一个使用两个线程的程序。我已经在两个线程中都设置了断点。在gdb下运行程序时,我想在线程之间切换并使它们运行。 (线程t1处于活动状态且正在运行,线程t2;在断点处暂停时。我想停止T1运行并运行T2)。

有什么办法可以在gdb中安排线程吗?

linux multithreading gdb pthreads
3个回答
35
投票

默认情况下,GDB会在遇到任何断点时停止所有线程,并在发出任何命令(例如continuenextstepfinish等)时恢复所有线程,这些命令要求下级进程(您正在调试的进程)启动执行。

但是,您可以告诉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,然后continuefinish在线程1(线程2仍然停止),然后Ctrl-C重新获得对GDB的控制,切换到线程2,continue(线程1仍然停止)等

注意:通过设置scheduler-locking on,很容易导致劣质过程自我死锁。


4
投票

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

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

前面提到的“scheduler-locking on”命令允许您在其他线程停止的情况下执行一个线程。不间断模式允许您步进一个线程,其他线程处于活动状态。


1
投票

使用休息条件

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

Debugging with GDB

编辑:

(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
(gdb) thread 2
(gdb) continue

您可以将这些命令放在.gdbrc文件中。

© www.soinside.com 2019 - 2024. All rights reserved.