monkey: Catch some signals and backtrace

Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
This commit is contained in:
Daniel Silverstone 2020-04-25 21:04:41 +01:00
parent 24ec30359b
commit 309fcd6d42
No known key found for this signature in database
GPG Key ID: C30DF439F2987D74
1 changed files with 32 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "utils/config.h"
#include "utils/sys_time.h"
@ -340,6 +341,28 @@ __assert_fail(const char *__assertion, const char *__file,
abort();
}
static void
signal_handler(int sig)
{
int frames;
fprintf(stderr, "Caught signal %s (%d)\n",
((sig == SIGSEGV) ? "SIGSEGV" :
((sig == SIGILL) ? "SIGILL" :
((sig == SIGFPE) ? "SIGFPE" :
((sig == SIGBUS) ? "SIGBUS" :
"unknown signal")))),
sig);
frames = backtrace(&backtrace_buffer[0], 4096);
if (frames > 0 && frames < 4096) {
fprintf(stderr, "Backtrace:\n");
fflush(stderr);
backtrace_symbols_fd(&backtrace_buffer[0], frames, 2);
}
abort();
}
#endif
int
@ -359,6 +382,15 @@ main(int argc, char **argv)
.llcache = filesystem_llcache_table,
};
#if (!defined(NDEBUG) && defined(HAVE_EXECINFO))
/* Catch segfault, illegal instructions and fp exceptions */
signal(SIGSEGV, signal_handler);
signal(SIGILL, signal_handler);
signal(SIGFPE, signal_handler);
/* It's unlikely, but SIGBUS could happen on some platforms */
signal(SIGBUS, signal_handler);
#endif
ret = netsurf_register(&monkey_table);
if (ret != NSERROR_OK) {
die("NetSurf operation table failed registration");