From df62e1901ef75fb4fbebcb9833e1fe129b640f60 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Sat, 21 Nov 2020 21:45:36 +0000 Subject: [PATCH 1/2] arm9: mark drawFormattedString as printf-like function --- arm9/source/draw.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arm9/source/draw.h b/arm9/source/draw.h index f1f966ddb..16d734d6d 100644 --- a/arm9/source/draw.h +++ b/arm9/source/draw.h @@ -47,4 +47,6 @@ bool loadSplash(void); void drawCharacter(bool isTopScreen, u32 posX, u32 posY, u32 color, char character); u32 drawString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *string); + +__attribute__((format(printf,5,6))) u32 drawFormattedString(bool isTopScreen, u32 posX, u32 posY, u32 color, const char *fmt, ...); From 87fa35ccfda58418f7d613a177071a9d920e2cc2 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Sun, 22 Nov 2020 00:00:12 +0000 Subject: [PATCH 2/2] arm9 exception handlers: add arm9 mem dumping feature --- arm9/source/arm9_exception_handlers.c | 31 ++++++++++++++++++- arm9/source/exceptions.c | 11 +++++-- .../source/fatalExceptionHandlersMain.c | 2 +- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/arm9/source/arm9_exception_handlers.c b/arm9/source/arm9_exception_handlers.c index 0adf3378e..d7fbed38a 100644 --- a/arm9/source/arm9_exception_handlers.c +++ b/arm9/source/arm9_exception_handlers.c @@ -33,6 +33,16 @@ #define REG_DUMP_SIZE 4 * 17 #define CODE_DUMP_SIZE 48 +static inline void dumpArm9Memory(ExceptionDumpHeader *dumpHeader, u8 *buf) +{ + // Check if n3ds extra arm9 mem is enabled (if it's possible to read CFG9_EXTMEMCNT9) + u8 extmemcnt9 = 0; + safecpy(&extmemcnt9, (const void *)0x10000200, 1); + + u32 size = (extmemcnt9 & 1) ? 0x180000 : 0x100000; + dumpHeader->additionalDataSize += safecpy(buf, (const void *)0x08000000, size); +} + void __attribute__((noreturn)) arm9ExceptionHandlerMain(u32 *registerDump, u32 type) { ExceptionDumpHeader dumpHeader; @@ -42,7 +52,7 @@ void __attribute__((noreturn)) arm9ExceptionHandlerMain(u32 *registerDump, u32 t dumpHeader.magic[0] = 0xDEADC0DE; dumpHeader.magic[1] = 0xDEADCAFE; dumpHeader.versionMajor = 1; - dumpHeader.versionMinor = 2; + dumpHeader.versionMinor = 3; dumpHeader.processor = 9; dumpHeader.core = 0; @@ -68,6 +78,25 @@ void __attribute__((noreturn)) arm9ExceptionHandlerMain(u32 *registerDump, u32 t //Dump stack in place dumpHeader.stackDumpSize = safecpy(final, (const void *)registerDump[13], 0x1000 - (registerDump[13] & 0xFFF)); + final += dumpHeader.stackDumpSize; + + // See if we need to copy Arm9 memory (check for bkpt 0xFFFD / bkpt 0xFD) + if(dumpHeader.codeDumpSize > 0) + { + if(cpsr & 0x20) + { + // Thumb + u16 instr; + safecpy(&instr, codeDump + dumpHeader.codeDumpSize - 2, 2); + if(instr == 0xBEFD) dumpArm9Memory(&dumpHeader, final); + } + else + { + u32 instr; + safecpy(&instr, codeDump + dumpHeader.codeDumpSize - 4, 4); + if(instr == 0xE12FFF7D) dumpArm9Memory(&dumpHeader, final); + } + } dumpHeader.totalSize = sizeof(ExceptionDumpHeader) + dumpHeader.registerDumpSize + dumpHeader.codeDumpSize + dumpHeader.stackDumpSize + dumpHeader.additionalDataSize; diff --git a/arm9/source/exceptions.c b/arm9/source/exceptions.c index e21bb038d..07106840e 100644 --- a/arm9/source/exceptions.c +++ b/arm9/source/exceptions.c @@ -129,8 +129,15 @@ void detectAndProcessExceptionDumps(void) } if(dumpHeader->additionalDataSize != 0) - posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, - "Current process: %.8s (%016llX)", (const char *)additionalData, *(vu64 *)(additionalData + 8)); + { + u32 size = dumpHeader->additionalDataSize; + if(dumpHeader->processor == 11) + posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, + "Current process: %.8s (%016llX)", (const char *)additionalData, *(vu64 *)(additionalData + 8)); + else + posY = drawFormattedString(true, 10, posY + SPACING_Y, COLOR_WHITE, + "Arm9 memory dump at offset %X size %lX", (uintptr_t)additionalData - (uintptr_t)dumpHeader, size); + } posY += SPACING_Y; for(u32 i = 0; i < 17; i += 2) diff --git a/k11_extension/source/fatalExceptionHandlersMain.c b/k11_extension/source/fatalExceptionHandlersMain.c index cbdecc5ec..09a273252 100644 --- a/k11_extension/source/fatalExceptionHandlersMain.c +++ b/k11_extension/source/fatalExceptionHandlersMain.c @@ -81,7 +81,7 @@ void fatalExceptionHandlersMain(u32 *registerDump, u32 type, u32 cpuId) dumpHeader.magic[0] = 0xDEADC0DE; dumpHeader.magic[1] = 0xDEADCAFE; dumpHeader.versionMajor = 1; - dumpHeader.versionMinor = 2; + dumpHeader.versionMinor = 3; dumpHeader.processor = 11; dumpHeader.core = cpuId & 0xF;