#include <time.h>
#include <unistd.h>
#include <sys/syscall.h>
+#include <execinfo.h>
#include "sig-monitor.h"
#include "verbose.h"
+#define SIG_FOR_TIMER SIGVTALRM
+
/* local handler */
static _Thread_local sigjmp_buf *error_handler;
static _Thread_local int thread_timer_set;
static _Thread_local timer_t thread_timerid;
+/*
+ * Dumps the current stack
+ */
+static void dumpstack(int crop)
+{
+ int idx, count;
+ void *addresses[1000];
+ char **locations;
+
+ count = backtrace(addresses, sizeof addresses / sizeof *addresses);
+ locations = backtrace_symbols(addresses, count);
+ if (locations == NULL)
+ ERROR("can't get the backtrace (returned %d addresses)", count);
+ else {
+ for (idx = crop; idx < count; idx++)
+ ERROR("[BACKTRACE %d/%d] %s", idx - crop + 1, count - crop, locations[idx]);
+ free(locations);
+ }
+}
+
/*
* Creates a timer for the current thread
*
rc = 0;
else {
sevp.sigev_notify = SIGEV_THREAD_ID;
- sevp.sigev_signo = SIGALRM;
+ sevp.sigev_signo = SIG_FOR_TIMER;
sevp.sigev_value.sival_ptr = NULL;
#if defined(sigev_notify_thread_id)
sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid);
static void on_signal_terminate (int signum)
{
ERROR("Terminating signal %d received: %s", signum, strsignal(signum));
+ if (signum == SIGABRT)
+ dumpstack(3);
exit(1);
}
sigset_t sigset;
ERROR("ALERT! signal %d received: %s", signum, strsignal(signum));
+ if (error_handler == NULL && signum == SIG_FOR_TIMER)
+ return;
+
+ dumpstack(3);
// unlock signal to allow a new signal to come
if (error_handler != NULL) {
sigprocmask(SIG_UNBLOCK, &sigset, 0);
longjmp(*error_handler, signum);
}
- if (signum == SIGALRM)
- return;
ERROR("Unmonitored signal %d received: %s", signum, strsignal(signum));
exit(2);
}
int sig_monitor_init()
{
- static int sigerr[] = { SIGALRM, SIGSEGV, SIGFPE, 0 };
- static int sigterm[] = { SIGINT, SIGABRT, 0 };
+ static int sigerr[] = { SIG_FOR_TIMER, SIGSEGV, SIGFPE, SIGILL, SIGBUS, 0 };
+ static int sigterm[] = { SIGINT, SIGABRT, SIGTERM, 0 };
return (install(on_signal_error, sigerr) & install(on_signal_terminate, sigterm)) - 1;
}
}
-
-
-