【问题标题】:Print thread id at breakpoint在断点处打印线程 id
【发布时间】:2017-02-02 18:33:26
【问题描述】:

我正在调试一些 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吗?

【问题讨论】:

    标签: c++ multithreading debugging gdb


    【解决方案1】:

    使用built-in $_thread convenience variable

    调试器便利变量$_thread$_gthread 包含, 分别是per-inferior线程数和全局线程 当前线程的编号。您可能会发现这在写作中很有用 断点条件表达式、命令脚本等。看 便利变量,关于便利的一般信息 变量。

    catch catch if $_thread == 1
    

    使用Python API

    ——函数:gdb.selected_thread() 这个函数返回线程 选定线程的对象。如果没有选定的线程,这 将返回无。

    catch catch
    command
      python
        if gdb.selected_thread() != 1:
          gdb.execute('continue');
      end
    end
    

    一般来说,当 GDB 缺少某个功能时,您不太可能无法使用 Python API 来实现它,因为它允许您探索正在运行的程序和上下文。

    【讨论】:

    • catch catch 行实际上不起作用,因为 python 不是这样的表达式的有效部分。
    • @TomTromey 没错。我已经修改了答案。谢谢。
    猜你喜欢
    • 2019-06-03
    • 2014-07-07
    • 2012-05-06
    • 2020-01-10
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多