From 7fe18a048872afe341dcbb220e27e440619b8321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 11 Jun 2019 13:53:24 +0300 Subject: [PATCH] Add fatal signal handlers for unit tests This way debug assertions print a full stacktrace for unit tests. --- server/core/test/test_utils.hh | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/server/core/test/test_utils.hh b/server/core/test/test_utils.hh index c2d39fe4c..bb1c9644c 100644 --- a/server/core/test/test_utils.hh +++ b/server/core/test/test_utils.hh @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -40,8 +41,56 @@ void preload_module(const char* name, const char* path, const char* type) set_libdir(MXS_STRDUP(old_libdir.c_str())); } +static int set_signal(int sig, void (* handler)(int)); + +static void sigfatal_handler(int i) +{ + set_signal(i, SIG_DFL); + mxb::dump_stacktrace( + [](const char* symbol, const char* cmd) { + MXS_ALERT(" %s: %s", symbol, cmd); + }); + raise(i); +} + +static int set_signal(int sig, void (* handler)(int)) +{ + int rc = 0; + + struct sigaction sigact = {}; + sigact.sa_handler = handler; + + int err; + + do + { + errno = 0; + err = sigaction(sig, &sigact, NULL); + } + while (errno == EINTR); + + if (err < 0) + { + MXS_ERROR("Failed call sigaction() in %s due to %d, %s.", + program_invocation_short_name, + errno, + mxs_strerror(errno)); + rc = 1; + } + + return rc; +} + void init_test_env(char* __attribute((unused))path = nullptr, uint32_t init_type = QC_INIT_BOTH) { + set_signal(SIGSEGV, sigfatal_handler); + set_signal(SIGABRT, sigfatal_handler); + set_signal(SIGILL, sigfatal_handler); + set_signal(SIGFPE, sigfatal_handler); +#ifdef SIGBUS + set_signal(SIGBUS, sigfatal_handler); +#endif + config_get_global_options()->n_threads = 1; if (!mxs_log_init(NULL, NULL, MXS_LOG_TARGET_STDOUT))