我正在调试一些 C++ 代码。当我在断点处暂停时,如果我执行 info thread
操作,gdb 会显示进程中所有线程的列表,并在断点处执行的线程旁边放置一个星号。是否有一个 gdb 命令可以让 gdb 在断点时告诉你线程 id?
我正在执行 catch throw
和 catch catch
,以便在线程 1 上引发异常时进行调试。但是,线程 2 同时也引发和捕获异常。由于我只对 thread1 上的 throw
和 catch
感兴趣,因此我计划向 gdb 询问 threadid,并在 threadid 为 2 时编写断点以继续。
(gdb) catch throw
Catchpoint 7 (throw)
(gdb) catch catch
Catchpoint 8 (catch)
(gdb) command 8
> if threadid == 2
> c
> end
你能告诉我如何编写这一行if threadid == 2
吗?
最佳答案
使用built-in $_thread
convenience variable :
The debugger convenience variables
$_thread
and$_gthread
contain, respectively, the per-inferior thread number and the global thread number of the current thread. You may find this useful in writing breakpoint conditional expressions, command scripts, and so forth. See Convenience Variables, for general information on convenience variables.
catch catch if $_thread == 1
使用Python API :
— Function: gdb.selected_thread () This function returns the thread object for the selected thread. If there is no selected thread, this will return None.
catch catch
command
python
if gdb.selected_thread() != 1:
gdb.execute('continue');
end
end
一般来说,当 GDB 缺少某个功能时,您不太可能无法使用 Python API 来实现它,因为它允许您探索正在运行的程序和上下文。
关于c++ - 在断点处打印线程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41972246/