From aac3e5394157a0b55a6512cb2b94b20a57c1b98e Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Fri, 18 Oct 2024 14:18:40 +1100 Subject: win: add minidumps to fatal errors --- source/core/StarSignalHandler_windows.cpp | 33 +++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'source/core/StarSignalHandler_windows.cpp') diff --git a/source/core/StarSignalHandler_windows.cpp b/source/core/StarSignalHandler_windows.cpp index 29e93c5..13a36ac 100644 --- a/source/core/StarSignalHandler_windows.cpp +++ b/source/core/StarSignalHandler_windows.cpp @@ -4,11 +4,35 @@ #include "StarLogging.hpp" #include +#include "minidumpapiset.h" namespace Star { String g_sehMessage; +static DWORD WINAPI writeMiniDump(void* ExceptionInfo) { + auto hFile = CreateFileA("starbound.dmp", GENERIC_WRITE, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); + if (hFile == INVALID_HANDLE_VALUE) + return 0; + MINIDUMP_EXCEPTION_INFORMATION dumpExceptionInfo{}; + dumpExceptionInfo.ThreadId = GetCurrentThreadId(); + dumpExceptionInfo.ExceptionPointers = (PEXCEPTION_POINTERS)ExceptionInfo; + dumpExceptionInfo.ClientPointers = FALSE; + MiniDumpWriteDump( + GetCurrentProcess(), + GetCurrentProcessId(), + hFile, + MiniDumpNormal, + &dumpExceptionInfo, + NULL, + NULL); + CloseHandle(hFile); + if (dumpExceptionInfo.ExceptionPointers->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) { + MessageBoxA(NULL, "Stack overflow encountered\nA minidump has been generated", NULL, MB_OK | MB_ICONERROR | MB_SETFOREGROUND); + } + return 0; +}; + struct SignalHandlerImpl { bool handlingFatal; bool handlingInterrupt; @@ -98,9 +122,7 @@ struct SignalHandlerImpl { } static LONG CALLBACK vectoredExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo) { - if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_STACK_OVERFLOW) { - fatalError("Stack overflow encountered", false); - } + auto thread = CreateThread(NULL, 0, writeMiniDump, (void*)ExceptionInfo, 0, NULL); if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { handleFatalError("Access violation detected", ExceptionInfo); return EXCEPTION_CONTINUE_EXECUTION; @@ -143,7 +165,10 @@ struct SignalHandlerImpl { handleFatalError("Error occured", ExceptionInfo); return EXCEPTION_CONTINUE_EXECUTION; } - + if (thread != NULL) { + WaitForSingleObject(thread, 10000); + CloseHandle(thread); + } return EXCEPTION_CONTINUE_SEARCH; } -- cgit v1.2.3