diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 34 |
1 files changed, 33 insertions, 1 deletions
@@ -38,6 +38,7 @@ #include <sys/fcntl.h> #include <sys/errno.h> #include <signal.h> +#include <execinfo.h> #ifdef HAVE_CONFIG_H # include <config.h> #endif @@ -428,9 +429,40 @@ sigterm_handler (int sig) { exit (0); } +void +sigsegv_handler (int sig) { + fprintf (stderr, "Segmentation Fault\n"); + int j, nptrs; +#define SIZE 100 + void *buffer[100]; + char **strings; + + nptrs = backtrace(buffer, SIZE); + printf("backtrace() returned %d addresses\n", nptrs); + + /* The call + * backtrace_symbols_fd(buffer, + * nptrs, + * STDOUT_FILENO) + would produce similar output to the following: */ + + strings = backtrace_symbols(buffer, nptrs); + if (strings == NULL) { + perror("backtrace_symbols"); + exit(EXIT_FAILURE); + } + + for (j = 0; j < nptrs; j++) + printf("%s\n", strings[j]); + + free(strings); + exit (0); +} + int main (int argc, char *argv[]) { - setlocale (LC_ALL, ""); + signal (SIGSEGV, sigsegv_handler); + setlocale (LC_ALL, ""); setlocale (LC_NUMERIC, "C"); #ifdef ENABLE_NLS // fprintf (stderr, "enabling gettext support: package=" PACKAGE ", dir=" LOCALEDIR "...\n"); |