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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2643884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 01:08:03  来源:igfitidea点击:

How to continue one thread at a time when debugging a multithreaded program in GDB?

linuxmultithreadinggdbpthreads

提问by Arpit

I have a program which uses two threads. I have put the break point in both the threads. While running the program under gdb I want to switch between the threads and make them run. (thread t1 is active and running and thread t2; when paused on the breakpoint. I want to stop T1 running and run the T2).

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

Is there any way that I can schedule the threads in gdb?

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

采纳答案by PFee

If you're using GDB 7 or later, try "non-stop mode".

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

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

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

The "scheduler-locking on" command previously mentioned allows you step one thread with the others stopped. Non-stop mode allows you to step one thread with the others active.

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

回答by Employed Russian

By default, GDB stops all threads when any breakpoint is hit, and resumes allthreads when you issue any command (such as continue, next, step, finish, etc.) which requires that the inferior process (the one you are debugging) start to execute.

默认情况下,GDB停止时任何断点命中所有线程,并恢复所有线程时发出任何命令(如continuenextstepfinish,等)要求下突(正在调试的一个)开始执行。

However, you can tell GDB not to do that:

但是,您可以告诉 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').

So you'll want to set breakpoints, then set scheduler-locking on, then continueor finishin thread 1 (thread 2 is still stopped), then Ctrl-C to regain control of GDB, switch to thread 2, continue(thread 1 is still stopped), etc.

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

Beware: by setting scheduler-locking onit is very easy to cause the inferior process to self-deadlock.

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

回答by fabrizioM

use break conditions

使用中断条件

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

see Debugging with GDB

请参阅使用 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
(gdb) thread 2
(gdb) continue

You can put these commands inside a .gdbrc file.

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