简体   繁体   English

信号处理程序内部的回溯

[英]Backtrace inside Signal Handler

I'm trying to following the code from this post to have signal handlers print a backtrace on errors such as floating point and segmentation faults. 我想从下面的代码这篇文章有信号处理程序打印错误回溯如浮点和段故障。 I'm using seg fault signals as a starting point. 我将seg故障信号用作起点。 Here is the code: 这是代码:

#include <cstdlib>      //for exit()
#include <signal.h>     //signal handling   
#include <execinfo.h>   //backtrace, backtrace_symbols and backtrace_fd
#include <iostream>
#include <string.h>
#include <stdio.h>

#define TRACE_MSG fprintf(stderr, "TRACE at: %s() [%s:%d]\n", \
        __FUNCTION__, __FILE__, __LINE__)

void show_stackframe()
{
    void *trace[1024];
    char **messages = (char **) NULL;
    int i, trace_size = 0;
    TRACE_MSG;
    trace_size = backtrace(trace, 1024);    // segfault here???
    // More code here to print backtrace, but not needed at the moment..
    TRACE_MSG;
}

void sigSegvHandler( int signum, siginfo_t* info, void* arg )
{
    TRACE_MSG;
    show_stackframe();
    return;
}

double func_b()
{
    show_stackframe();  // Show that backtrace works without being 
                    // called inside sighandler.

    TRACE_MSG;
    int int_a[5];
    int_a[0] = 4;
    int_a[11] = 10;         // cause a segfault on purpose to see
                    // how the signal handling performs.

    return 1.1;
}

int main()
{
    // Examine and change the seg fault signal
    struct sigaction segvAction;   // File: /usr/include/bits/sigaction.h

   // Initialize segvAction struct to all zeros for initialiation
   memset( &segvAction,  0, sizeof( segvAction ) );

   segvAction.sa_sigaction = sigSegvHandler;
   segvAction.sa_flags = SA_SIGINFO;    //Invoke signal catching function with 3 arguments instead of 1

   // Set the action for the SIGSEGV signal
   sigaction( SIGSEGV, &segvAction,  NULL );

   func_b();    // Produce a SIGSEGV error
}

I am compiling using: 我正在使用以下代码进行编译:

 g++ -rdynamic testprogram.cpp -o testprogram

I receive the following output from the program: 我从程序收到以下输出:

TRACE at: show_stackframe() [stackoverflow.cpp:15]
TRACE at: show_stackframe() [stackoverflow.cpp:17]
TRACE at: func_b() [stackoverflow.cpp:33]
TRACE at: sigSegvHandler() [stackoverflow.cpp:22]
TRACE at: show_stackframe() [stackoverflow.cpp:15]
Segmentation fault

My question is why does show_stackframe() cause a segmentation fault inside of sigaction but works fine when not inside of the sigaction handler? 我的问题是,为什么show_stackframe()会在sigaction内导致分段错误,但在不在sigaction处理程序内时能正常工作? I obviously seem to be setting up the signal handler/action incorrect but I haven't been able to find it all day. 我显然似乎在设置信号处理程序/操作不正确,但是我整天都找不到它。 GDB doesn't seem to be any help in this case. 在这种情况下,GDB似乎没有任何帮助。

As stated here , the backtrace function is AS-Unsafe, which means it is unsafe to call from an asynchronous signal handler. 如所陈述在这里 ,所述backtrace功能是AS-不安全,这意味着它是不安全的从异步信号处理程序调用。 Doing so invokes undefined behavior. 这样做会引起未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM