从信号处理程序打印堆栈跟踪 [英] Printing stack trace from a signal handler

查看:170
本文介绍了从信号处理程序打印堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从64位辑阵线程C ++在Linux上运行的应用程序的信号处理程序打印堆栈跟踪。虽然我发现了几个code的例子,他们没有编制。我堵点越来越主叫方(其中生成的信号点)从ucontext_t结构解决。所有的信息,我能找到的,指向EIP寄存器或者ucontext.gregs [REG_EIP]或ucontext.eip。它看起来像他们两个是x86相关的。我需要64位兼容的code为英特尔和AMD的CPU。任何人都可以帮忙吗?

I need to print stack trace from a signal handler of 64-bit mutli-threaded C++ application running on Linux. Although I found several code examples, none of them compiles. My blocking point is getting the caller's (the point where the signal was generated) address from the ucontext_t structure. All of the information I could find, points to the EIP register as either ucontext.gregs[REG_EIP] or ucontext.eip. It looks like both of them are x86-specific. I need 64-bit compliant code for both Intel and AMD CPUs. Can anybody help?

推荐答案

还有一个glibc的功能回溯。该名男子页面列出了一个例子,该呼叫:

there is a glibc function backtrace. The man page lists an example the the call:

#define SIZE 100
void myfunc3(void) {
       int j, nptrs;

       void *buffer[100];
       char **strings;

       nptrs = backtrace(buffer, SIZE);
       printf("backtrace() returned %d addresses\n", nptrs);

       /* The call backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO)
          would produce similar output to the following: */

       strings = backtrace_symbols(buffer, nptrs);
       if (strings == NULL) {
           perror("backtrace_symbols");
           exit(EXIT_FAILURE);
       }

       for (j = 0; j < nptrs; j++)
           printf("%s\n", strings[j]);

       free(strings);
   }

请参阅该手册页了解更多内容。

See the man page for more context.

这是很难说,如果这真的是保证从信号处理程序的工作,因为只有POSIX列出了保证工作的几个重入函数。记住:一个信号处理程序可以被称作而你的过程的其余部分是正确的,一个malloc调用中间

it's difficult to tell if this really is guaranteed to work from a signal handler, since posix lists only a few reentrant functions that are guaranteed to work. Remember: a signal handler may be called while the rest of your process is right in the middle of an malloc call.

我的猜到的是,这通常工作,但它可能无法不时。为了调试,这可能是不够好。

My guess is, that this usually works, but it may fail from time to time. For debugging this may be good enough.

这篇关于从信号处理程序打印堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆