Linux 获取其他线程的回溯

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6402160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:36:29  来源:igfitidea点击:

Getting a backtrace of other thread

linuxmultithreadingbacktrace

提问by Alexander Sandler

In Linux, to get a backtrace you can use backtrace() library call, but it only returns backtrace of current thread. Is there any way to get a backtrace of some other thread, assuming I know it's TID (or pthread_t) and I can guarantee it sleeps?

在 Linux 中,要获得回溯,您可以使用 backtrace() 库调用,但它只返回当前线程的回溯。有没有办法获得其他线程的回溯,假设我知道它是 TID(或 pthread_t)并且我可以保证它休眠?

It seems that libunwind (http://www.nongnu.org/libunwind/) project can help. The problem is that it is not supported by CentOS, so I prefer not to use it.

似乎 libunwind (http://www.nongnu.org/libunwind/) 项目可以提供帮助。问题是 CentOS 不支持它,所以我宁愿不使用它。

Any other ideas? Thanks.

还有其他想法吗?谢谢。

采纳答案by sandeep

Signal Handling with the help of backtrace can solve your purpose.

借助回溯的信号处理可以解决您的目的。

I mean if you have a PID of the Thread, you can raise a signal for that thread. and in the handler you can use the backtrace. since the handler would be executing in that partucular thread, the backtrace there would be the output what you are needed.

我的意思是,如果您有线程的 PID,则可以为该线程发出信号。在处理程序中,您可以使用回溯。由于处理程序将在该特定线程中执行,因此回溯将输出您需要的内容。

回答by Albert

I implemented that myself here.

在这里自己实现

Initially, I wanted to implement something similar as suggested here, i.e. getting somehow the top frame pointer of the thread and unwinding it manually (the linked source is derived from Apples backtraceimplementation, thus might be Apple-specific, but the idea is generic).

最初,我想实现类似于此处建议的内容,即以某种方式获取线程的顶部帧指针并手动展开它(链接源源自 Applebacktrace实现,因此可能是 Apple 特定的,但该想法是通用的)。

However, to have that safe (and the source above is not and may even be broken anyway), you must suspend the thread while you access its stack. I searched around for different ways to suspend a thread and found this, thisand this. Basically, there is no really good way. The common hack, also used by the Hotspot JAVA VM, is to use signals and sending a custom signal to your thread via pthread_kill.

但是,为了确保安全(并且上面的源代码不是,甚至可能会被破坏),您必须在访问其堆栈时挂起线程。我四处寻找挂起线程的不同方法,并找到了thisthisthis。基本上,没有真正的好方法。Hotspot JAVA VM使用的常见技巧是使用信号并通过pthread_kill向您的线程发送自定义信号。

So, as I would need such signal-hack anyway, I can have it a bit simpler and just use backtraceinside the called signal handler which is executed in the target thread (as also suggested here by sandeep). This is basically what my implementation is doing.

因此,由于无论如何我都需要这样的信号破解,我可以让它更简单一点,只需backtrace在目标线程中执行的被调用信号处理程序中使用(正如sandeep 在此处建议的那样)。这基本上就是我的实现所做的。

If you are also interested in printing the backtrace, i.e. get some useful debugging information (function name, source code filename, source code line number, ...), read hereabout an extended backtrace_symbolsbased on libbfd. Or just see the source here.

如果您也对打印回溯感兴趣,即获取一些有用的调试信息(函数名称、源代码文件名、源代码行号等),请阅读此处了解backtrace_symbols基于 libbfd的扩展。或者只是在这里查看来源。

回答by Manoj

gdb provides these facilities for debugging multi-thread programs:

gdb 提供了这些用于调试多线程程序的工具:

  • automatic notification of new threads
  • ‘thread thread-id', a command to switch among threads
  • ‘info threads', a command to inquire about existing threads
  • ‘thread apply [thread-id-list] [all] args', a command to apply a command to a list of threads
  • thread-specific breakpoints
  • ‘set print thread-events', which controls printing of messages on thread start and exit.
  • ‘set libthread-db-search-path path', which lets the user specify which libthread_db to use if the default choice isn't compatible with the program.
  • 新线程的自动通知
  • 'thread thread-id',一个在线程之间切换的命令
  • “信息线程”,一个查询现有线程的命令
  • 'thread apply [thread-id-list] [all] args',一个将命令应用于线程列表的命令
  • 线程特定的断点
  • 'set print thread-events',它控制在线程启动和退出时打印消息。
  • 'set libthread-db-search-path path',如果默认选择与程序不兼容,它允许用户指定使用哪个 libthread_db。

So just goto required thread in GDB by cmd: 'thread thread-id'. Then do 'bt' in that thread context to print the thread backtrace.

因此,只需通过 cmd 转到 GDB 中所需的线程:'thread thread-id'。然后在该线程上下文中执行 'bt' 以打印线程回溯。