使用 GDB 挂起线程的命令
我对 GDB 有点陌生。我希望有人可以帮助我做一些应该很简单的事情,我已经使用过 Google/docs 但我只是错过了一些东西。
人们使用 GDB 调试线程应用程序的“正常”方式是什么?我正在使用 pthreads。我只想观看一个线程 - 我看到的两个选项是
a) 告诉调试器以某种方式附加到特定线程,这样单步执行不会导致在每个上下文切换上跳转线程
b) 告诉调试器挂起/释放任何“无趣”的线程
我更愿意走路线 b) - 阅读 GDB 的帮助我没有看到这方面的命令,提示?
I'm a little new to GDB. I'm hoping someone can help me with something that should be quite simple, I've used Google/docs but I'm just missing something.
What is the 'normal' way folks debug threaded apps with GDB? I'm using pthreads. I'm wanting to watch only one thread - the two options I see are
a) tell the debugger somehow to attach to a particular thread, such that stepping wont result in jumping threads on each context switch
b) tell the debugger to suspend/free any 'uninteresting' threads
I'd prefer to go route b) - reading the help for GDB I dont see a command for this, tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅有关
设置调度程序锁定
的文档。请注意:如果您挂起其他线程,并且其中一个线程持有锁,并且您感兴趣的线程在单步执行时的某个时刻需要该锁,则会出现死锁。
您永远无法调试线程的正确性,您只能设计它。根据我的经验,线程应用程序的大多数调试都是放入断言,并在其中之一时检查世界状态断言被违反。
See documentation for
set scheduler-locking on
.Beware: if you suspend other threads, and if one of them holds a lock, and if your interesting thread needs that lock at some point while stepping, you'll deadlock.
You can never debug thread correctness, you can only design it in. In my experience, most of debugging of threaded apps is putting in assertions, and examining state of the world when one of the assertions is violated.
首先,您需要使用以下命令启用多线程调试器行为。不知道为什么默认情况下它被禁用。
您必须将它们放入 .gdbinit 文件中,因为不允许在调试期间更改它们。它们使每个命令仅适用于当前聚焦的线程。注意:线程可能正在运行,因此您必须暂停它。
查看焦点线程:
线程
。要切换到另一个,请附加其编号,例如
线程2
。查看所有线程及其编号:
info thread
。将命令应用于特定线程类似于
thread apply; <命令>。例如
thread apply 4 bt
将向编号为 4 的线程应用“backtrace”命令。thread apply all continue
继续所有暂停的线程(或者您可以使用continue -a
)。但有一个小问题——许多命令需要暂停线程。我知道有几种方法可以做到这一点:
interrupt
命令:中断线程执行,接受多个要暂停的线程,不带参数会中断焦点线程。break;线程
。例如中断 25 线程 4
。您可能还会发现非常有用,您可以设置要执行的命令列表通过命令
commands
命中断点 - 例如,您可以快速打印有趣的值,然后继续执行。First, you need to enable comfortable for multi-threading debugger behavior with the following commands. No idea why it's disabled by default.
You have to put these into .gdbinit file, because changing them during debugging is not allowed. They make every command applicable only to the currently focused thread. Note: the thread might be running, so you have to pause it.
to see the focused thread:
thread
.to switch to another one append its number, e.g.
thread 2
.to see all threads with their numbers:
info thread
.applying a command to a particular thread is something like
thread apply <threadnum> <command>
. E.g.thread apply 4 bt
will apply "backtrace" command to a thread with number 4.thread apply all continue
continues all paused threads (or you can usecontinue -a
).There is a small problem though — many commands needs the thread to be paused. I know a few ways of doing that:
interrupt
command: interrupts the thread execution, accepts a number of a thread to pause, without an argument breaks the focused one.break <linenum> thread <threadnum>
. E.g.break 25 thread 4
.You may also find very useful that you can set a list of commands to be executed when a breakpoint hit through the command
commands
— so e.g. you may quickly print interesting values, then continue execution.