Adding build option to print stack traces on segfault.

This commit is contained in:
Alex Shinn 2014-07-06 21:47:36 +09:00
parent b127607cda
commit 3031b50406
2 changed files with 22 additions and 0 deletions

View file

@ -112,6 +112,10 @@
/* may be very slow and using CFLAGS=-O0 is recommended. */
/* #define SEXP_USE_SAFE_ACCESSORS 1 */
/* uncomment to install a default signal handler in main() for segfaults */
/* This will print a helpful backtrace. */
/* #define SEXP_USE_PRINT_BACKTRACE_ON_SEGFAULT 1 */
/* uncomment this to make the heap common to all contexts */
/* By default separate contexts can have separate heaps, */
/* and are thus thread-safe and independant. */

18
main.c
View file

@ -57,6 +57,21 @@ void sexp_usage(int err) {
#define sexp_usage(err) (err ? exit_failure() : exit_success())
#endif
#if SEXP_USE_PRINT_BACKTRACE_ON_SEGFAULT
#include <execinfo.h>
#include <signal.h>
void sexp_segfault_handler(int sig) {
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
#endif
#if SEXP_USE_IMAGE_LOADING
#include <sys/types.h>
@ -634,6 +649,9 @@ void run_main (int argc, char **argv) {
}
int main (int argc, char **argv) {
#if SEXP_USE_PRINT_BACKTRACE_ON_SEGFAULT
signal(SIGSEGV, sexp_segfault_handler);
#endif
sexp_scheme_init();
run_main(argc, argv);
exit_success();