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

Arp*_*pit 30 linux multithreading gdb pthreads

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

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

Emp*_*ian 43

默认情况下,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').
Run Code Online (Sandbox Code Playgroud)

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

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


小智 6

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

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

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