From f0a4320928b80da83ec1f1aa7f86c2c6cffad720 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Sun, 16 Jan 2022 13:42:24 +0100 Subject: [PATCH] #5 install sig handler --- DStarGatewayApp.cpp | 29 +++++++++++++++++++++++++++-- DStarGatewayApp.h | 2 ++ Makefile | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/DStarGatewayApp.cpp b/DStarGatewayApp.cpp index 3a8ae42..1b47533 100644 --- a/DStarGatewayApp.cpp +++ b/DStarGatewayApp.cpp @@ -23,6 +23,11 @@ #include #include #include +#include +#include +#include +#include +#include #include "DStarGatewayDefs.h" #include "DStarGatewayConfig.h" @@ -41,9 +46,15 @@ #include "APRSGPSDIdFrameProvider.h" #include "APRSFixedIdFrameProvider.h" -#ifndef UNIT_TESTS +#ifdef UNIT_TESTS +int fakemain(int argc, char *argv[]) +#else int main(int argc, char *argv[]) +#endif { + // install sigHandler + signal(SIGSEGV, __sigHandler); + setbuf(stdout, NULL); if (2 != argc) { printf("usage: %s path_to_config_file\n", argv[0]); @@ -70,7 +81,21 @@ int main(int argc, char *argv[]) return 0; } -#endif + +void __sigHandler(int sig) +{ + if(sig == SIGSEGV) { + void *array[100]; + size_t size; + // get void*'s for all entries on the stack + size = backtrace(array, 100); + + // print out all the frames to stderr + fprintf(stderr, "Error: signal %d:\n", sig); + backtrace_symbols_fd(array, size, STDERR_FILENO); + exit(1); + } +} CDStarGatewayApp::CDStarGatewayApp(const std::string &configFile) : m_configFile(configFile), m_thread(NULL) { diff --git a/DStarGatewayApp.h b/DStarGatewayApp.h index 2af3de9..3461e88 100644 --- a/DStarGatewayApp.h +++ b/DStarGatewayApp.h @@ -21,6 +21,8 @@ #include "DStarGatewayThread.h" +void __sigHandler(int sig); + class CDStarGatewayApp { private: diff --git a/Makefile b/Makefile index 973fbaa..b057963 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ export LOG_DIR=/var/log/dstargateway/ ifeq ($(ENABLE_DEBUG), 1) # choose this if you want debugging help -export CPPFLAGS=-g -ggdb -W -Wall -Werror -std=c++17 +export CPPFLAGS=-g -rdynamic -ggdb -W -Wall -Werror -std=c++17 else # or, you can choose this for a much smaller executable without debugging help CPPFLAGS=-W -O3 -Wall -Werror -std=c++17