Take the 2-minute tour ×

I handle SIGSEGV by code:

int C()
  int *i = NULL;
  *i = 10; // Crash there

int B()
  return C();

int A()
   return B();

int main(void)
  struct sigaction handler;
  handler.sa_sigaction = handler_func;
  handler.sa_flags = SA_SIGINFO;

Where handler code are:

static int handler_func(int signal, siginfo_t info, void* rserved)
  const void* stack[MAX_DEPTH];
  StackCrowlState state;
  state.addr = stack;
  state.count = MAX_DEPTH;

  _Unwind_Reason_Code code = _Unwind_Backtrace(trace_func,&state);
  printf("Stack trace count: %d, code: %d\n",MAX_DEPTH - state.count, code);


static _Unwind_Reason_Code trace_func(void* context, void* arg)
  StackCrowlState *state = (StackCrowlState *)arg;
     void *ip = (void *)_Unwind_GetIP(context);
       state->addr[0] = ip;

But trace_func where called only once, and shows only on _Unwind_Backtrace calls. Is it possible to get stacktrace of code which cause SIGSEGV signal using _Unwind_Backtrace?


share|improve this question
add comment

2 Answers

You want to backtrace from the signal triggering function, but you backtrace from the signal handler function. That's two different stacks. (Note, the SA_ONSTACK flag in sigaction is irrelevant to your question.)

To find the stack pointer of the of the triggering function, use the third parameter of the handler, i.e. void *rserved. You can reference to the answer in this question: Getting the saved instruction pointer address from a signal handler

share|improve this answer
Do you know what to do with the stack pointer of the triggering function once it's retrieved from the third argument of the signal handler? Can you still use _Unwind_Backtrace? I have this exact problem and I can't really understand what to do after reading your answer. –  P1r4nh4 Apr 17 '13 at 15:19
add comment

better you use backtrace and backtrace_symbols_fd to get a stacktrace from a signal handler.

share|improve this answer
Except on platforms that use gcc, and thus have _Unwind_Backtrace, but not GNU libc, and thus don't have backtrace and backtrace_symbols_fd. –  Jan Hudec Nov 22 '12 at 13:11
add comment

Your Answer


