Skip to content

Commit

Permalink
impr: Better OS-specific handling of system exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
WerWolv committed Jan 21, 2025
1 parent 242b100 commit 8978e19
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions main/gui/source/crash_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <exception>
#include <typeinfo>

#if defined (OS_MACOS)
#if defined (OS_WINDOWS)
#include <windows.h>
#elif defined (OS_MACOS)
#include <sys/utsname.h>
#endif

Expand Down Expand Up @@ -166,22 +168,45 @@ namespace hex::crash {

// Register signal handlers
{
#define HANDLE_SIGNAL(name) \
std::signal(name, [](int signalNumber) { \
signalHandler(signalNumber, #name); \
})

HANDLE_SIGNAL(SIGSEGV);
HANDLE_SIGNAL(SIGILL);
HANDLE_SIGNAL(SIGABRT);
HANDLE_SIGNAL(SIGFPE);
HANDLE_SIGNAL(SIGINT);

#if defined (SIGBUS)
HANDLE_SIGNAL(SIGBUS);
#endif
#if defined(OS_WINDOWS)
#define HANDLE_SIGNAL(name) case name: signalHandler(name, #name); break
SetUnhandledExceptionFilter([](EXCEPTION_POINTERS* exceptionInfo) -> LONG {
switch (exceptionInfo->ExceptionRecord->ExceptionCode) {
HANDLE_SIGNAL(EXCEPTION_ACCESS_VIOLATION);
HANDLE_SIGNAL(EXCEPTION_ILLEGAL_INSTRUCTION);
HANDLE_SIGNAL(EXCEPTION_INT_DIVIDE_BY_ZERO);
HANDLE_SIGNAL(EXCEPTION_STACK_OVERFLOW);
HANDLE_SIGNAL(EXCEPTION_DATATYPE_MISALIGNMENT);
HANDLE_SIGNAL(EXCEPTION_ARRAY_BOUNDS_EXCEEDED);
}

return EXCEPTION_CONTINUE_SEARCH;
});
#undef HANDLE_SIGNAL
#else
#define HANDLE_SIGNAL(name) \
{ \
struct sigaction action = { }; \
action.sa_handler = [](int signalNumber) { \
signalHandler(signalNumber, #name); \
}; \
sigemptyset(&action.sa_mask); \
action.sa_flags = 0; \
sigaction(name, &action, nullptr); \
}

#undef HANDLE_SIGNAL
HANDLE_SIGNAL(SIGSEGV);
HANDLE_SIGNAL(SIGILL);
HANDLE_SIGNAL(SIGABRT);
HANDLE_SIGNAL(SIGFPE);
HANDLE_SIGNAL(SIGINT);

#if defined (SIGBUS)
HANDLE_SIGNAL(SIGBUS);
#endif

#undef HANDLE_SIGNAL
#endif
}

// Configure the uncaught exception handler
Expand Down

0 comments on commit 8978e19

Please sign in to comment.