From cca7b461149bf0f95a95c1dae27213d44aaae0a8 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 25 May 2024 16:56:50 +1000 Subject: [PATCH] Assertions: Write crash dump on assertion fail --- src/common/assert.cpp | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/common/assert.cpp b/src/common/assert.cpp index 60e5b1d47..d69653ddf 100644 --- a/src/common/assert.cpp +++ b/src/common/assert.cpp @@ -1,7 +1,8 @@ -// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin +// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) #include "assert.h" +#include "crash_handler.h" #include #include #include @@ -84,7 +85,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* FreezeThreads(&pHandle); char szMsg[512]; - std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)", szMessage, szFunction, szFile, uLine); + std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)\n", szMessage, szFunction, szFile, uLine); #if defined(_WIN32) SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); @@ -93,19 +94,25 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* std::snprintf( szMsg, sizeof(szMsg), - "%s in function %s (%s:%u)\n\nPress Abort to exit, Retry to break to debugger, or Ignore to attempt to continue.", + "%s in function %s (%s:%u)\nPress Abort to exit, Retry to break to debugger, or Ignore to attempt to continue.", szMessage, szFunction, szFile, uLine); int result = MessageBoxA(NULL, szMsg, NULL, MB_ABORTRETRYIGNORE | MB_ICONERROR); if (result == IDRETRY) + { __debugbreak(); + } else if (result != IDIGNORE) + { + CrashHandler::WriteDumpForCaller(); TerminateProcess(GetCurrentProcess(), 0xBAADC0DE); + } #else - fputs(szMsg, stderr); - fputs("\nAborting application.\n", stderr); - fflush(stderr); - abort(); + std::fputs(szMsg, stderr); + CrashHandler::WriteDumpForCaller(); + std::fputs("Aborting application.\n", stderr); + std::fflush(stderr); + std::abort(); #endif ResumeThreads(pHandle); @@ -119,7 +126,7 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* FreezeThreads(&pHandle); char szMsg[512]; - std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)", szMessage, szFunction, szFile, uLine); + std::snprintf(szMsg, sizeof(szMsg), "%s in function %s (%s:%u)\n", szMessage, szFunction, szFile, uLine); #if defined(_WIN32) SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY); @@ -127,20 +134,23 @@ void Y_OnAssertFailed(const char* szMessage, const char* szFunction, const char* OutputDebugStringA(szMsg); std::snprintf(szMsg, sizeof(szMsg), - "%s in function %s (%s:%u)\n\nDo you want to attempt to break into a debugger? Pressing Cancel will " + "%s in function %s (%s:%u)\nDo you want to attempt to break into a debugger? Pressing Cancel will " "abort the application.", szMessage, szFunction, szFile, uLine); int result = MessageBoxA(NULL, szMsg, NULL, MB_OKCANCEL | MB_ICONERROR); if (result == IDOK) __debugbreak(); + else + CrashHandler::WriteDumpForCaller(); TerminateProcess(GetCurrentProcess(), 0xBAADC0DE); #else - fputs(szMsg, stderr); - fputs("\nAborting application.\n", stderr); - fflush(stderr); - abort(); + std::fputs(szMsg, stderr); + CrashHandler::WriteDumpForCaller(); + std::fputs("Aborting application.\n", stderr); + std::fflush(stderr); + std::abort(); #endif ResumeThreads(pHandle);