Files
Library/src/Testing/SignalHandler.cpp
T
faumarayandClaude Opus 5.5 20893d1bf3 Module selection, and test signal handlers on Windows
VOID_ENABLED_MODULES (cache, empty for all) configures only the named
modules and their INTERNAL_DEPS, followed transitively; OPTIONAL_DEPS
are used when named too. A project embedding Void configures what it
links, so the packages of modules it never builds (LibArchive, OpenSSL,
SQLite, libsodium) need not exist, as in a mingw-w64 cross build.

Testing's crash handlers use the C runtime's signal() on Windows, which
has no sigaction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 05:13:42 +03:00

75 lines
2.4 KiB
C++

#include <Void/Testing/Detail/SignalHandler.hpp>
#ifdef VOID_HAS_STACKTRACE
#include <Void/Stacktrace/Stacktrace.hpp>
#endif
#include <csignal>
#include <cstring>
#include <cstdlib>
namespace Void::Testing::Detail {
static CrashCallback g_crash_callback;
static int g_last_signal{0};
#ifndef _WIN32
static sigjmp_buf g_jump_buffer;
static bool g_jump_ready{false};
sigjmp_buf& get_jump_buffer() { return g_jump_buffer; }
void set_jump_ready(bool ready) { g_jump_ready = ready; }
#endif
const char* signal_name(int sig) {
switch (sig) {
case SIGSEGV: return "SIGSEGV (segmentation fault)";
case SIGABRT: return "SIGABRT (abort)";
case SIGFPE: return "SIGFPE (floating point exception)";
case SIGILL: return "SIGILL (illegal instruction)";
#ifdef SIGBUS
case SIGBUS: return "SIGBUS (bus error)";
#endif
default: return "UNKNOWN SIGNAL";
}
}
static void crash_handler(int sig) {
g_last_signal = sig;
#ifdef VOID_HAS_STACKTRACE
auto frames = Void::Stacktrace::capture_stacktrace(1, 16);
auto trace = Void::Stacktrace::format_stacktrace(frames);
#else
std::string trace;
#endif
if (g_crash_callback) g_crash_callback(sig, trace);
#ifndef _WIN32
if (g_jump_ready) { g_jump_ready = false; siglongjmp(g_jump_buffer, sig); }
#endif
std::_Exit(128 + sig);
}
void install_signal_handlers(CrashCallback on_crash) {
g_crash_callback = std::move(on_crash);
#ifdef _WIN32
// No sigaction on Windows: the C runtime's signal() covers these four.
std::signal(SIGSEGV, crash_handler); std::signal(SIGABRT, crash_handler); std::signal(SIGFPE, crash_handler); std::signal(SIGILL, crash_handler);
#else
struct sigaction sa{}; sa.sa_handler = crash_handler; sigemptyset(&sa.sa_mask); sa.sa_flags = 0;
sigaction(SIGSEGV, &sa, nullptr); sigaction(SIGABRT, &sa, nullptr); sigaction(SIGFPE, &sa, nullptr); sigaction(SIGILL, &sa, nullptr);
#ifdef SIGBUS
sigaction(SIGBUS, &sa, nullptr);
#endif
#endif
}
void uninstall_signal_handlers() {
#ifdef _WIN32
std::signal(SIGSEGV, SIG_DFL); std::signal(SIGABRT, SIG_DFL); std::signal(SIGFPE, SIG_DFL); std::signal(SIGILL, SIG_DFL);
#else
struct sigaction sa{}; sa.sa_handler = SIG_DFL;
sigaction(SIGSEGV, &sa, nullptr); sigaction(SIGABRT, &sa, nullptr); sigaction(SIGFPE, &sa, nullptr); sigaction(SIGILL, &sa, nullptr);
#ifdef SIGBUS
sigaction(SIGBUS, &sa, nullptr);
#endif
#endif
g_crash_callback = nullptr;
}
} // namespace Void::Testing::Detail