【问题标题】:How do I get the backtrace for all the threads in GDB?如何获取 GDB 中所有线程的回溯?
【发布时间】:2013-08-22 22:27:55
【问题描述】:

GDB 中是否有与 WinDbg 的 !process 0 7 等效的命令?

我想提取转储文件中的所有线程以及它们在 GDB 中的回溯。 info threads 不输出堆栈跟踪。那么,有没有这样的命令呢?

【问题讨论】:

  • LLDB is bt all 的相应命令——以防有人通过 Google 找到这个(认为 GDB == LLDB)。
  • 使用 python 可以在 gdb.selected_inferior().threads() 中为线程工作" (gdb) python: thread.switch(); print(thread.num);gdb.execute('where ')"
  • @kennytm 这足以提出问题 IMO
  • 事实上,我创建了确切的问题:stackoverflow.com/questions/67820406/…

标签: multithreading debugging gdb backtrace


【解决方案1】:

一般情况下,backtrace用来获取当前线程的栈,但是如果有必要获取所有线程的栈跟踪,使用下面的命令。

thread apply all bt

【讨论】:

  • 将输出保存到文件:gdb <binary> <coredump> -ex "thread apply all bt" -ex "quit" > output.log
  • 您可以将其缩短为t a a bt
  • @Doomsday 评论中的命令为我挂起。最好尝试使用gdb <binary> <coredump> -ex "thread apply all bt" -ex "detach" -ex "quit" > output.log,以避免来自 gdb 的问题阻止等待输入的命令。
  • 更通用的语法是thread apply [threadno] [all] args,其中[threadno]也可以是一个以空格分隔的线程标识符列表(例如2 3 4),或一个范围(例如2-6)。
  • @MarianoPaniga,或使用--batch
【解决方案2】:

有没有这样的命令?

thread apply all where

【讨论】:

  • backtrace 的同义词。
  • 刚试过,这不是回溯的同义词。
  • 来自info gdb where名称“where”和“info stack”(缩写为“info s”)是“backtrace”的附加别名。
【解决方案3】:

当使用多个线程进行调试时,切换到特定线程号并仅获取该线程的回溯也很有用。

来自 GNU GDB 线程 documentation

出于调试目的,GDB 将其自己的线程号(按线程创建顺序分配的一个小整数)与程序中的每个线程相关联。

用法:

info threads

然后确定您要查看的线程。

thread <thread_id>

最后,只对那个线程使用回溯:

bt

【讨论】:

    【解决方案4】:

    如果您的进程正在运行:

    pstack $pid
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    相关资源
    最近更新 更多