From a68a23d1cb71c372e651fd76337742fdbed0f462 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Tue, 12 Sep 2023 23:24:23 +0200 Subject: [PATCH] port: print module base in crash message --- port/src/crash.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/port/src/crash.c b/port/src/crash.c index c87a2b05c..ff3ebcd97 100644 --- a/port/src/crash.c +++ b/port/src/crash.c @@ -28,6 +28,13 @@ static LPTOP_LEVEL_EXCEPTION_FILTER prevExFilter; +static void *crashGetModuleBase(const void *addr) +{ + HMODULE h = NULL; + GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, addr, &h); + return (void *)h; +} + static void crashStackTrace(char *msg, PEXCEPTION_POINTERS exinfo) { CONTEXT context = *exinfo->ContextRecord; @@ -72,7 +79,9 @@ static void crashStackTrace(char *msg, PEXCEPTION_POINTERS exinfo) if (SymGetLineFromAddr64(process, (uintptr_t)exinfo->ExceptionRecord->ExceptionAddress, &disp, &line)) { CRASH_MSG(": %s:%lu+%lu", line.FileName, line.LineNumber, disp); } - CRASH_MSG("\n\nBACKTRACE:\n"); + CRASH_MSG("\nMODULE: %p\n", crashGetModuleBase(exinfo->ExceptionRecord->ExceptionAddress)); + CRASH_MSG("MAIN MODULE: %p\n", crashGetModuleBase(crashInit)); + CRASH_MSG("\nBACKTRACE:\n"); char symbuf[sizeof(SYMBOL_INFO) + CRASH_MAX_SYM * sizeof(TCHAR)]; PSYMBOL_INFO sym = (PSYMBOL_INFO)symbuf; @@ -138,6 +147,7 @@ static long __stdcall crashHandler(PEXCEPTION_POINTERS exinfo) #include #include #include +#include #include static struct sigaction prevSigAction; @@ -187,6 +197,15 @@ static s32 crashIsDebuggerPresent(void) return result; } +static void *crashGetModuleBase(const void *addr) +{ + Dl_info info; + if (dladdr(addr, &info)) { + return info.dli_fbase; + } + return NULL; +} + static void crashStackTrace(char *msg, s32 sig, void *pc) { u32 msglen = 0; @@ -210,6 +229,8 @@ static void crashStackTrace(char *msg, s32 sig, void *pc) CRASH_MSG("%p\n", frames[0]); } + CRASH_MSG("MODULE: %p\n", crashGetModuleBase(frames[0])); + CRASH_MSG("MAIN MODULE: %p\n", crashGetModuleBase(crashInit)); CRASH_MSG("\nBACKTRACE:\n"); s32 i;