From 3563ece47b25751a1fa2300bdd251234271aa953 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 28 Jun 2023 04:48:11 +0100 Subject: [PATCH] [*] Improve non-fatal exception handling --- Include/Aurora/Runtime.hpp | 4 +++ Source/Debug/ExceptionWatcher.NT.cpp | 19 +++++++++-- Source/Debug/ExceptionWatcher.Win32.cpp | 45 ++++++++++++------------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/Include/Aurora/Runtime.hpp b/Include/Aurora/Runtime.hpp index 54e96eb6..6d908428 100644 --- a/Include/Aurora/Runtime.hpp +++ b/Include/Aurora/Runtime.hpp @@ -318,6 +318,10 @@ namespace Aurora */ bool bIsExceptionThrowFatal { false }; + bool bSaveAllExceptionsAsMinidumpInBg { false }; + + bool bRaiseDebuggerToAllExceptionsInStageAndDbg { true }; + bool bPrintExceptionStackTracesOut { true }; bool bIsApplicationClientSoftwareOnJitteryMemorySystem { false }; // enable me to enable padding from system out of memory conditions. diff --git a/Source/Debug/ExceptionWatcher.NT.cpp b/Source/Debug/ExceptionWatcher.NT.cpp index 9d833613..f7e93d53 100644 --- a/Source/Debug/ExceptionWatcher.NT.cpp +++ b/Source/Debug/ExceptionWatcher.NT.cpp @@ -170,15 +170,28 @@ namespace Aurora::Debug #if defined(AU_ENABLE_NATIVE_MINIDUMP) if (!fatal) { - if (IsDebuggerPresent()) __debugbreak(); - else BlackboxReport(pExceptionInfo, false); + if (gRuntimeConfig.debug.bRaiseDebuggerToAllExceptionsInStageAndDbg && + IsDebuggerPresent()) + { + __debugbreak(); + } + else + { + if (gRuntimeConfig.debug.bSaveAllExceptionsAsMinidumpInBg) + { + BlackboxReport(pExceptionInfo, false); + } + } } else { SaveMinidump(pExceptionInfo, true); } #else - BlackboxReport(pExceptionInfo, true); + if (fatal) + { + BlackboxReport(pExceptionInfo, true); + } #endif } diff --git a/Source/Debug/ExceptionWatcher.Win32.cpp b/Source/Debug/ExceptionWatcher.Win32.cpp index 65b09d11..8971a5b7 100644 --- a/Source/Debug/ExceptionWatcher.Win32.cpp +++ b/Source/Debug/ExceptionWatcher.Win32.cpp @@ -432,9 +432,10 @@ namespace Aurora::Debug void BlackboxReport(_EXCEPTION_POINTERS *ExceptionInfo, bool fatal) { - AuString path; HANDLE hFile; + std::wstring path; AuString utf8Path; + bool ok { true }; MINIDUMP_EXCEPTION_INFORMATION info; if (fatal) @@ -442,11 +443,7 @@ namespace Aurora::Debug AuDebug::AddMemoryCrunch(); } - auto ok = AuIOFS::GetProfileDomain(path); // < could throw inside - if (!ok) - { - path = ".\\"; - } + auto dumpName = GetDumpName(); info.ClientPointers = false; info.ThreadId = GetCurrentThreadId(); @@ -458,23 +455,22 @@ namespace Aurora::Debug MiniDumpWithUnloadedModules | MiniDumpWithThreadInfo; + while (path.empty()) + { + try + { + utf8Path = AuIOFS::NormalizePathRet("./Logs/Crashes/" + dumpName); + path = Locale::ConvertFromUTF8(utf8Path); + } + catch (...) + { - static std::wstring pathStorage(8192, L' '); - - int index {}; - - index += MultiByteToWideChar(CP_UTF8, 0, path.c_str(), path.length(), pathStorage.data() + index, pathStorage.size() - index); - static const std::string crashesSlash = "Crashes\\"; - index += MultiByteToWideChar(CP_UTF8, 0, crashesSlash.c_str(), crashesSlash.length(), pathStorage.data() + index, pathStorage.size() - index); - - auto dumpName = GetDumpName();; - index += MultiByteToWideChar(CP_UTF8, 0, dumpName.c_str(), dumpName.length(), pathStorage.data() + index, pathStorage.size() - index); - - pathStorage.resize(index); + } + } AuIOFS::CreateDirectories(utf8Path, true); // potentially unsafe / could throw inside - hFile = CreateFileW(pathStorage.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + hFile = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile != INVALID_HANDLE_VALUE) { AuLogWarn("[1] Couldn't open minidump file. Has a debugger locked the .dmp file?"); @@ -491,14 +487,15 @@ namespace Aurora::Debug CloseHandle(hFile); miniMiniDumpOut: - Telemetry::NewBlackBoxEntryMinidump report {}; - report.includesRx = false; - report.resource.path = dumpName; //