From 29d60be3784720f02f2533968d1c961284a4773d 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 | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/iceoryx_hoofs/testing/testing_logger.cpp b/iceoryx_hoofs/testing/testing_logger.cpp index c546ed3640e..a769b9f11e0 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 @@ -112,6 +117,16 @@ std::vector TestingLogger::getLogMessages() noexcept return logger.m_loggerData->buffer; } +std::jmp_buf sigsevJmpPoint; + +static void sigsegvHandler(int /*sig*/, siginfo_t*, void*) +{ + std::cout << "SIGSEGV\n" << std::flush; + dynamic_cast(log::Logger::get()).printLogBuffer(); + + std::longjmp(&sigsevJmpPoint[0], 1); +} + void LogPrinter::OnTestStart(const ::testing::TestInfo&) { dynamic_cast(log::Logger::get()).clearLogBuffer(); @@ -123,10 +138,16 @@ 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)