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: [PATCH] win: add minidumps to fatal errors --- assets/opensb/interface.config.patch | 1 + source/core/StarSignalHandler_windows.cpp | 33 ++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/assets/opensb/interface.config.patch b/assets/opensb/interface.config.patch index eedd728..a43a7bc 100644 --- a/assets/opensb/interface.config.patch +++ b/assets/opensb/interface.config.patch @@ -41,6 +41,7 @@ // Change planet name to support the new internal string formatting. "planetNameFormatString" : "- {} -", + "planetTextOffset" : [0, 120], "planetTextStyle" : { "backDirectives" : "border=5;fff;fff?border=1;fff;fff7?multiply=000", "fontSize" : 24 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; }