Add fatal signal handlers for unit tests
This way debug assertions print a full stacktrace for unit tests.
This commit is contained in:
		@ -22,6 +22,7 @@
 | 
				
			|||||||
#include <maxscale/query_classifier.hh>
 | 
					#include <maxscale/query_classifier.hh>
 | 
				
			||||||
#include <maxscale/paths.h>
 | 
					#include <maxscale/paths.h>
 | 
				
			||||||
#include <maxbase/alloc.h>
 | 
					#include <maxbase/alloc.h>
 | 
				
			||||||
 | 
					#include <maxbase/stacktrace.hh>
 | 
				
			||||||
#include <maxscale/routingworker.hh>
 | 
					#include <maxscale/routingworker.hh>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <sys/stat.h>
 | 
					#include <sys/stat.h>
 | 
				
			||||||
@ -40,8 +41,56 @@ void preload_module(const char* name, const char* path, const char* type)
 | 
				
			|||||||
    set_libdir(MXS_STRDUP(old_libdir.c_str()));
 | 
					    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)
 | 
					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;
 | 
					    config_get_global_options()->n_threads = 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!mxs_log_init(NULL, NULL, MXS_LOG_TARGET_STDOUT))
 | 
					    if (!mxs_log_init(NULL, NULL, MXS_LOG_TARGET_STDOUT))
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user