Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Jul 2017 17:02:59 -0400
From:      Marc Branchaud <marcnarc@xiplink.com>
To:        freebsd-hackers@freebsd.org
Subject:   libexecinfo backtrace() in a signal handler
Message-ID:  <4d662753-98bc-1275-9394-0cda95eedc65@xiplink.com>

next in thread | raw e-mail | index | archive | help
Howdy,

(Please CC replies to me, as I am not subscribed.  Apologies if I'm in 
the wrong forum!)

I'm trying to use libexecinfo's backtrace() in a SIGSEGV handler.  It 
only finds one single frame in the stack, for the signal handler 
function.  (Outside of the signal handler backtrace() does return the 
full stack from the call point.)

Is there any way to get the stack from where the SIGSEGV arose?  A few 
hours of Googling has proved fruitless.

My overall goal is to intercept core-dumping signals to try to save a 
textual backtrace instead of a full dump (as they can be quite large in 
my application).  So I'd also like to catch SIGBUS, and maybe SIGFPE too.

Details:
	FreeBSD 9 (yes, ouch, I know, but I see the same problem in 10)
	libexecinfo is copied from STABLE-10
	amd64
	gcc

Here's a little test app.  Even when compiled with -O0, backtrace() 
still only finds the frame for the handler() function.

----->8----->8----->8----->8------>8---->8----->8----->8-----
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

void handler(int sig) {
   void *frames[10];
   size_t size = backtrace(frames, sizeof(frames));
   fprintf(stderr, "Signal %d:\n", sig);
   fprintf(stderr, "%zu FRAMES:\n", size);
   backtrace_symbols_fd(frames, size, STDERR_FILENO);
   exit(1);
}

void baz() {
   int *foo = (int*)-1;
   printf("%d\n", *foo);
}

void bar() { baz(); }

void foo() { bar(); }

int main(int argc, char **argv) {
   signal(SIGSEGV, handler);
   foo();
   return 0;
}
----->8----->8----->8----->8------>8---->8----->8----->8-----

Thanks!

		M.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4d662753-98bc-1275-9394-0cda95eedc65>