From 79e3665abe6c7fa33563c7e50961f7cbe8b6c64e Mon Sep 17 00:00:00 2001 From: Mathias Kraus Date: Mon, 19 Dec 2022 20:09:21 +0100 Subject: [PATCH] iox-#1755 Flush TestingLogger on SIGSEGV --- iceoryx_hoofs/testing/testing_logger.cpp | 27 ++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/iceoryx_hoofs/testing/testing_logger.cpp b/iceoryx_hoofs/testing/testing_logger.cpp index 7af7c8c7d83..c5171dc9fe3 100644 --- a/iceoryx_hoofs/testing/testing_logger.cpp +++ b/iceoryx_hoofs/testing/testing_logger.cpp @@ -19,6 +19,11 @@ #include +#include +#include +#include +#include + namespace iox { namespace testing @@ -102,6 +107,16 @@ void TestingLogger::flush() noexcept Base::assumeFlushed(); } +sigjmp_buf point; + +static void sigsegvHandler(int sig, siginfo_t*, void*) +{ + std::cout << "SIGSEGV\n" << std::flush; + dynamic_cast(log::Logger::get()).printLogBuffer(); + + longjmp(point, 1); +} + void LogPrinter::OnTestStart(const ::testing::TestInfo&) { dynamic_cast(log::Logger::get()).clearLogBuffer(); @@ -113,10 +128,14 @@ void LogPrinter::OnTestStart(const ::testing::TestInfo&) std::abort(); }); - /// @todo iox-#1755 register signal handler for sigterm to flush to logger; - /// there might be tests to register a handler itself and when this is - /// done at each start of the test only the tests who use their - /// own signal handler are affected and don't get an log output on termination + struct sigaction action; + memset(&action, 0, sizeof(struct sigaction)); + sigemptyset(&action.sa_mask); + + action.sa_flags = SA_NODEFER; + action.sa_sigaction = sigsegvHandler; + + sigaction(SIGSEGV, &action, nullptr); } void LogPrinter::OnTestPartResult(const ::testing::TestPartResult& result)