From f23d3531a205605a979fd26bf5215f4daf3a3334 Mon Sep 17 00:00:00 2001 From: icex2 Date: Wed, 2 Sep 2020 19:59:55 +0200 Subject: [PATCH] util/signal: Expose signal_exception_code_to_str Can be re-used in inject/debugger to avoid code duplication --- src/main/util/signal.c | 123 +++++++++++++++++++++-------------------- src/main/util/signal.h | 12 +++- 2 files changed, 75 insertions(+), 60 deletions(-) diff --git a/src/main/util/signal.c b/src/main/util/signal.c index 87277b7..a1c5fb2 100644 --- a/src/main/util/signal.c +++ b/src/main/util/signal.c @@ -7,7 +7,8 @@ static signal_shutdown_handler_t shutdown_handler; -static const char* control_code_to_str(DWORD ctrl_code) { +static const char* control_code_to_str(DWORD ctrl_code) +{ switch (ctrl_code) { case CTRL_C_EVENT: return "CTRL_C_EVENT"; @@ -25,8 +26,67 @@ static const char* control_code_to_str(DWORD ctrl_code) { } } -static const char* exception_code_to_str(struct _EXCEPTION_RECORD *exception_record) { - switch (exception_record->ExceptionCode) { +static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType) +{ + log_misc("Console ctrl handler called: %s", control_code_to_str(dwCtrlType)); + + if (dwCtrlType == CTRL_C_EVENT) { + if (shutdown_handler) { + log_misc("Executing shutdown handler"); + shutdown_handler(); + } + + log_info("Exiting process"); + + ExitProcess(0); + } + + return FALSE; +} + +static LONG WINAPI unhandled_exception_filter(struct _EXCEPTION_POINTERS *ExceptionInfo) +{ + // no exception info provided + if (ExceptionInfo != NULL) { + struct _EXCEPTION_RECORD *ExceptionRecord = ExceptionInfo->ExceptionRecord; + + log_warning( + "Exception raised: %s", + signal_exception_code_to_str(ExceptionRecord->ExceptionCode)); + + struct _EXCEPTION_RECORD *record_cause = ExceptionRecord->ExceptionRecord; + + while (record_cause != NULL) { + log_warning("Caused by: %s", signal_exception_code_to_str(record_cause->ExceptionCode)); + record_cause = record_cause->ExceptionRecord; + } + + // TODO print stacktrace + + log_fatal("End exception handler"); + } + + return EXCEPTION_CONTINUE_SEARCH; +} + +void signal_exception_handler_init() +{ + SetConsoleCtrlHandler(console_ctrl_handler, TRUE); + SetUnhandledExceptionFilter(unhandled_exception_filter); + + log_info("Initialized"); +} + +void signal_register_shutdown_handler(signal_shutdown_handler_t handler) +{ + shutdown_handler = handler; + + log_misc("Registered shutdown handler"); +} + +const char* signal_exception_code_to_str(DWORD code) +{ + switch (code) { case EXCEPTION_ACCESS_VIOLATION: return "EXCEPTION_ACCESS_VIOLATION"; case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: @@ -70,62 +130,7 @@ static const char* exception_code_to_str(struct _EXCEPTION_RECORD *exception_rec case DBG_CONTROL_C: return "DBG_CONTROL_C"; default: - log_warning("Unknown exception code: %lX", exception_record->ExceptionCode); + log_warning("Unknown exception code: %lX", code); return "Unknown"; } -} - -static BOOL WINAPI console_ctrl_handler(DWORD dwCtrlType) { - log_misc("Console ctrl handler called: %s", control_code_to_str(dwCtrlType)); - - if (dwCtrlType == CTRL_C_EVENT) { - if (shutdown_handler) { - log_misc("Executing shutdown handler"); - shutdown_handler(); - } - - log_info("Exiting process"); - - ExitProcess(0); - } - - return FALSE; -} - -static LONG WINAPI unhandled_exception_filter(struct _EXCEPTION_POINTERS *ExceptionInfo) { - - // no exception info provided - if (ExceptionInfo != NULL) { - struct _EXCEPTION_RECORD *ExceptionRecord = ExceptionInfo->ExceptionRecord; - - log_warning("Exception raised: %s", exception_code_to_str(ExceptionRecord)); - - struct _EXCEPTION_RECORD *record_cause = ExceptionRecord->ExceptionRecord; - - while (record_cause != NULL) { - log_warning("Caused by: %s", exception_code_to_str(record_cause)); - record_cause = record_cause->ExceptionRecord; - } - - // TODO print stacktrace - - log_fatal("End exception handler"); - } - - return EXCEPTION_CONTINUE_SEARCH; -} - -void signal_exception_handler_init() -{ - SetConsoleCtrlHandler(console_ctrl_handler, TRUE); - SetUnhandledExceptionFilter(unhandled_exception_filter); - - log_info("Initialized"); -} - -void signal_register_shutdown_handler(signal_shutdown_handler_t handler) -{ - shutdown_handler = handler; - - log_misc("Registered shutdown handler"); } \ No newline at end of file diff --git a/src/main/util/signal.h b/src/main/util/signal.h index d513e47..7ab4abf 100644 --- a/src/main/util/signal.h +++ b/src/main/util/signal.h @@ -1,5 +1,7 @@ #pragma once +#include + /** * Function signature of shutdown handler. */ @@ -17,4 +19,12 @@ void signal_exception_handler_init(); * * @param handler Handler function to register. */ -void signal_register_shutdown_handler(signal_shutdown_handler_t handler); \ No newline at end of file +void signal_register_shutdown_handler(signal_shutdown_handler_t handler); + +/** + * Convert a (windows) exception code to a human readable representation. + * + * @param code Exception code + * @return Human readable representation of the given exception code + */ +const char* signal_exception_code_to_str(DWORD code); \ No newline at end of file