c++ - 在断点处打印线程 ID

标签 c++ multithreading debugging gdb

我正在调试一些 C++ 代码。当我在断点处暂停时,如果我执行 info thread 操作,gdb 会显示进程中所有线程的列表,并在断点处执行的线程旁边放置一个星号。是否有一个 gdb 命令可以让 gdb 在断点时告诉你线程 id?

我正在执行 catch throwcatch catch,以便在线程 1 上引发异常时进行调试。但是,线程 2 同时也引发和捕获异常。由于我只对 thread1 上的 throwcatch 感兴趣,因此我计划向 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/

相关文章:

c++ - 如何在运行时找到当前函数的名称?

c++ - 如何在 C++ 中实现线程安全引用计数

c++ - 无需 nm 或其他外部工具即可查找全局变量

Python PyQt4 信号未触发连接方法

node.js - 如何将参数传递给nodemon(或node-supervisor)中的可执行文件?

c++ - 将参数转发到可变参数模板函数时如何添加参数值?

Android,如何在启动其他 Activity 时保持 CountDownTimer 运行

java - 为什么线程创建被认为是一个昂贵的过程?

node.js - nightwatch - 使用 Node.js 和 Chrome 进行调试

android - 应用无响应而崩溃