From 8645d0a82a5c89650e834b898b7b4deb3108c4fb Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 7 Jan 2024 17:07:12 +0100 Subject: [PATCH] port: don't use the existing log system in crashlog in case it crashes --- port/include/system.h | 2 +- port/src/crash.c | 13 ++++++++++--- port/src/system.c | 6 ++---- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/port/include/system.h b/port/include/system.h index e6b75ac3d..94158802a 100644 --- a/port/include/system.h +++ b/port/include/system.h @@ -24,7 +24,7 @@ u64 sysGetMicroseconds(void); void sysFatalError(const char *fmt, ...) __attribute__((noreturn)); -void sysLogStartCrash(void); +s32 sysLogIsOpen(void); void sysLogPrintf(s32 level, const char *fmt, ...); void sysGetExecutablePath(char *outPath, const u32 outLen); diff --git a/port/src/crash.c b/port/src/crash.c index e2b6be079..dc0536589 100644 --- a/port/src/crash.c +++ b/port/src/crash.c @@ -10,6 +10,7 @@ #include "system.h" #include "platform.h" +#define CRASH_LOG_FNAME "pd.crash.log" #define CRASH_MAX_MSG 8192 #define CRASH_MAX_SYM 256 #define CRASH_MAX_FRAMES 32 @@ -132,9 +133,6 @@ static long __stdcall crashHandler(PEXCEPTION_POINTERS exinfo) return EXCEPTION_CONTINUE_EXECUTION; } - // start writing to a crash log file - sysLogStartCrash(); - sysLogPrintf(LOG_ERROR, "FATAL: Crashed: PC=%p CODE=0x%08lx", exinfo->ExceptionRecord->ExceptionAddress, exinfo->ExceptionRecord->ExceptionCode); fflush(stderr); @@ -142,6 +140,15 @@ static long __stdcall crashHandler(PEXCEPTION_POINTERS exinfo) crashStackTrace(msg, exinfo); + // open log file for the crash dump if one hasn't been opened yet + if (!sysLogIsOpen()) { + FILE *f = fopen(CRASH_LOG_FNAME, "wb"); + if (f) { + fprintf(f, "Crash!\n\n%s", msg); + fclose(f); + } + } + sysFatalError("Crash!\n\n%s", msg); return EXCEPTION_CONTINUE_EXECUTION; diff --git a/port/src/system.c b/port/src/system.c index fb24addd0..538dd1e87 100644 --- a/port/src/system.c +++ b/port/src/system.c @@ -103,11 +103,9 @@ u64 sysGetMicroseconds(void) return ((u64)tv.tv_sec * USEC_IN_SEC + (u64)tv.tv_usec) - startTick; } -void sysLogStartCrash(void) +s32 sysLogIsOpen(void) { - if (!logPath[0]) { - sysLogSetPath(CRASHLOG_FNAME); - } + return (logPath[0] != '\0'); } void sysLogPrintf(s32 level, const char *fmt, ...)