从 C 中的任何线程获取调用堆栈
在 Solaris 10 上的 C 中,我想从进程内的任意线程获取调用堆栈。
我有许多工作线程和一个监视所有线程以检测紧密循环和死锁的线程。我想要实现的功能是让监视线程在杀死“挂起”线程之前多次打印调用堆栈。
我知道如何通过让监视线程执行 pstack (使用 system() 或通过分叉)来实现这一点。但我希望能够用C实现这个功能。有什么办法可以做到这一点吗?
我知道如何让线程通过遍历堆栈来打印其自己的调用堆栈,如果它命中断言,这很有用,但不知道如何为同一进程中的另一个线程执行此操作。
感谢您的任何帮助。 尼克B
In C on Solaris 10, I'd like to get the call stack from an arbitrary thread within a process.
I have many worker threads and one thread which monitors them all to detect tight loops and deadlocks. The function I'd like to implement is for the monitoring thread to print the call stack from the "hung" thread several times before it kills it.
I know how to implement this by having the monitoring thread execute pstack (with system() or by forking). But I would like to be able to implement this function in C. Is there any way to do this?
I know how to make a thread print its OWN call stack, by walking the stack, which is useful if it hits an assert, but not how to do this for another thread within the same process.
Thanks for any help.
NickB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
walkcontext()
使用dladdr()
/dladdr1()
将地址转换为函数名称。walkcontext()
采用 ucontext 为线程。如果您没有该线程的合作,那么您可以通过停止线程来获取它的ucontext(例如使用PCTWSTOP
),然后从pr_oldcontext
字段,从/proc/self/lstatus
。You can use
walkcontext()
to walk the stack, usingdladdr()
/dladdr1()
to convert the addresses to function names.walkcontext()
takes a ucontext for the thread. If you don't have the cooperation of that thread then you can obtain a ucontext for it by stopping the thread (e.g. withPCTWSTOP
) and then reading its address from thepr_oldcontext
field of the lwpstatus structure for that thread, obtained from/proc/self/lstatus
.如果您使用 gcc,则可以使用内置函数 __builtin_return_address。
void * __builtin_return_address (unsigned int level)
该函数返回调用该函数的函数的地址。即函数的调用者。
level 指定了多少个级别。 0表示当前函数,1表示调用者,2表示调用者调用者。
下面的例子将提供用法。通过打印函数的地址,可以确定调用堆栈。
If you are using gcc you can use the inbuilt function __builtin_return_address.
void * __builtin_return_address (unsigned int level)
The function returns the address of the function from which the function is called. i.e the caller of the function.
The level specifies how many levels. 0 means current function 1 means caller, 2 means callers caller.
The following example will provide the usage. By printing the addresses of the function, the call stack can be determined.