diff --git a/include/chibi/features.h b/include/chibi/features.h index a5cda9c6..02f66314 100644 --- a/include/chibi/features.h +++ b/include/chibi/features.h @@ -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. */ diff --git a/main.c b/main.c index 0a855063..acd94031 100644 --- a/main.c +++ b/main.c @@ -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 +#include +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 @@ -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();