From fb267a32ff4b9ff6dc8688610b936cb8450edd8e Mon Sep 17 00:00:00 2001 From: Adam Higerd Date: Mon, 27 Jul 2020 22:30:43 -0500 Subject: [PATCH] Stack trace: coding style cleanup --- include/mgba/internal/arm/arm.h | 4 ++-- include/mgba/internal/arm/isa-inlines.h | 13 +++++------- include/mgba/internal/debugger/stack-trace.h | 2 +- include/mgba/internal/sm83/sm83.h | 2 +- src/arm/debugger/debugger.c | 22 ++++++++------------ src/debugger/cli-debugger.c | 2 +- src/debugger/stack-trace.c | 2 +- 7 files changed, 20 insertions(+), 27 deletions(-) diff --git a/include/mgba/internal/arm/arm.h b/include/mgba/internal/arm/arm.h index f7424bf01..f9eeafdb6 100644 --- a/include/mgba/internal/arm/arm.h +++ b/include/mgba/internal/arm/arm.h @@ -70,7 +70,7 @@ struct ARMCore; union PSR { struct { -#if defined(__BIG_ENDIAN__) +#ifdef __BIG_ENDIAN__ unsigned n : 1; unsigned z : 1; unsigned c : 1; @@ -94,7 +94,7 @@ union PSR { }; struct { -#if defined(__BIG_ENDIAN__) +#ifdef __BIG_ENDIAN__ uint8_t flags; uint8_t status; uint8_t extension; diff --git a/include/mgba/internal/arm/isa-inlines.h b/include/mgba/internal/arm/isa-inlines.h index 6bb825c91..0cabcfee2 100644 --- a/include/mgba/internal/arm/isa-inlines.h +++ b/include/mgba/internal/arm/isa-inlines.h @@ -99,15 +99,12 @@ static inline void _ARMReadCPSR(struct ARMCore* cpu) { cpu->irqh.readCPSR(cpu); } +static inline uint32_t _ARMInstructionLength(struct ARMCore* cpu) { + return cpu->cpsr.t == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB; +} + static inline uint32_t _ARMPCAddress(struct ARMCore* cpu) { - int instructionLength; - enum ExecutionMode mode = cpu->cpsr.t; - if (mode == MODE_ARM) { - instructionLength = WORD_SIZE_ARM; - } else { - instructionLength = WORD_SIZE_THUMB; - } - return cpu->gprs[ARM_PC] - instructionLength * 2; + return cpu->gprs[ARM_PC] - _ARMInstructionLength(cpu) * 2; } #endif diff --git a/include/mgba/internal/debugger/stack-trace.h b/include/mgba/internal/debugger/stack-trace.h index 672450d03..443faa64c 100644 --- a/include/mgba/internal/debugger/stack-trace.h +++ b/include/mgba/internal/debugger/stack-trace.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2019 Jeffrey Pfau +/* Copyright (c) 2013-2020 Jeffrey Pfau * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this diff --git a/include/mgba/internal/sm83/sm83.h b/include/mgba/internal/sm83/sm83.h index dfd3bedbc..10bbf8bc4 100644 --- a/include/mgba/internal/sm83/sm83.h +++ b/include/mgba/internal/sm83/sm83.h @@ -74,7 +74,7 @@ struct SM83InterruptHandler { void (*hitIllegal)(struct SM83Core* cpu); }; -#if defined(__BIG_ENDIAN__) +#ifdef __BIG_ENDIAN__ #define SM83_REGISTER_PAIR(HIGH, LOW) union { \ struct { \ uint8_t HIGH; \ diff --git a/src/arm/debugger/debugger.c b/src/arm/debugger/debugger.c index bce6a4408..8a79a5ad8 100644 --- a/src/arm/debugger/debugger.c +++ b/src/arm/debugger/debugger.c @@ -13,14 +13,9 @@ #include #include #include -#include DEFINE_VECTOR(ARMDebugBreakpointList, struct ARMDebugBreakpoint); -static inline uint32_t ARMDebuggerGetInstructionLength(struct ARMCore* cpu) { - return cpu->cpsr.t == MODE_ARM ? WORD_SIZE_ARM : WORD_SIZE_THUMB; -} - static bool ARMDebuggerUpdateStackTraceInternal(struct mDebuggerPlatform* d, uint32_t pc) { struct ARMDebugger* debugger = (struct ARMDebugger*) d; struct ARMCore* cpu = debugger->cpu; @@ -34,7 +29,7 @@ static bool ARMDebuggerUpdateStackTraceInternal(struct mDebuggerPlatform* d, uin struct mStackFrame* irqFrame = mStackTraceGetFrame(stack, 0); // TODO: uint32_t ivtBase = ARMControlRegIsVE(cpu->cp15.r1.c0) ? 0xFFFF0000 : 0x00000000; uint32_t ivtBase = 0x00000000; - if (ivtBase <= pc && pc < ivtBase + 0x20 && !(irqFrame && _ARMModeHasSPSR(((struct ARMRegisterFile*)irqFrame->regs)->cpsr.priv))) { + if (ivtBase <= pc && pc < ivtBase + 0x20 && !(irqFrame && _ARMModeHasSPSR(((struct ARMRegisterFile*) irqFrame->regs)->cpsr.priv))) { // TODO: Potential enhancement opportunity: add break-on-exception mode irqFrame = mStackTracePush(stack, pc, pc, cpu->gprs[ARM_SP], &cpu->regs); irqFrame->interrupt = true; @@ -79,7 +74,8 @@ static bool ARMDebuggerUpdateStackTraceInternal(struct mDebuggerPlatform* d, uin } destAddress = cpu->gprs[info.op1.reg]; } else { - abort(); // Should be unreachable + mLOG(DEBUGGER, ERROR, "Unknown branch operand in stack trace"); + return false; } if (info.branchType & ARM_BRANCH_INDIRECT) { @@ -87,7 +83,7 @@ static bool ARMDebuggerUpdateStackTraceInternal(struct mDebuggerPlatform* d, uin } if (isCall) { - int instructionLength = ARMDebuggerGetInstructionLength(debugger->cpu); + int instructionLength = _ARMInstructionLength(debugger->cpu); frame = mStackTracePush(stack, pc, destAddress + instructionLength, cpu->gprs[ARM_SP], &cpu->regs); if (!(debugger->stackTraceMode & STACK_TRACE_BREAK_ON_CALL)) { return false; @@ -138,7 +134,7 @@ static void _destroyWatchpoint(struct mWatchpoint* watchpoint) { static void ARMDebuggerCheckBreakpoints(struct mDebuggerPlatform* d) { struct ARMDebugger* debugger = (struct ARMDebugger*) d; - int instructionLength = ARMDebuggerGetInstructionLength(debugger->cpu); + int instructionLength = _ARMInstructionLength(debugger->cpu); uint32_t pc = debugger->cpu->gprs[ARM_PC] - instructionLength; if (debugger->stackTraceMode != STACK_TRACE_DISABLED && ARMDebuggerUpdateStackTraceInternal(d, pc)) { return; @@ -241,7 +237,6 @@ void ARMDebuggerDeinit(struct mDebuggerPlatform* platform) { } ARMDebugBreakpointListDeinit(&debugger->swBreakpoints); mWatchpointListDeinit(&debugger->watchpoints); - mStackTraceDeinit(&platform->p->stackTrace); } @@ -432,7 +427,8 @@ static void ARMDebuggerTrace(struct mDebuggerPlatform* d, char* out, size_t* len size_t regStringLen = *length; ARMDebuggerFormatRegisters(&cpu->regs, out, ®StringLen); - *length = regStringLen + snprintf(out + regStringLen, *length - regStringLen, " | %s", disassembly); + regStringLen += snprintf(out + regStringLen, *length - regStringLen, " | %s", disassembly); + *length = regStringLen; } static void ARMDebuggerFormatRegisters(struct ARMRegisterFile* regs, char* out, size_t* length) { @@ -445,7 +441,7 @@ static void ARMDebuggerFormatRegisters(struct ARMRegisterFile* regs, char* out, } static void ARMDebuggerFrameFormatRegisters(struct mStackFrame* frame, char* out, size_t* length) { - ARMDebuggerFormatRegisters((struct ARMRegisterFile*)frame->regs, out, length); + ARMDebuggerFormatRegisters(frame->regs, out, length); } bool ARMDebuggerGetRegister(struct mDebuggerPlatform* d, const char* name, int32_t* value) { @@ -540,7 +536,7 @@ static void ARMDebuggerSetStackTraceMode(struct mDebuggerPlatform* d, uint32_t m static bool ARMDebuggerUpdateStackTrace(struct mDebuggerPlatform* d) { struct ARMDebugger* debugger = (struct ARMDebugger*) d; - int instructionLength = ARMDebuggerGetInstructionLength(debugger->cpu); + int instructionLength = _ARMInstructionLength(debugger->cpu); uint32_t pc = debugger->cpu->gprs[ARM_PC] - instructionLength; if (debugger->stackTraceMode != STACK_TRACE_DISABLED) { return ARMDebuggerUpdateStackTraceInternal(d, pc); diff --git a/src/debugger/cli-debugger.c b/src/debugger/cli-debugger.c index cd5900b03..af80a50cc 100644 --- a/src/debugger/cli-debugger.c +++ b/src/debugger/cli-debugger.c @@ -178,8 +178,8 @@ static void _continue(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { } static void _next(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { - struct mDebuggerPlatform* platform = debugger->d.platform; UNUSED(dv); + struct mDebuggerPlatform* platform = debugger->d.platform; debugger->d.core->step(debugger->d.core); if (platform->getStackTraceMode && platform->getStackTraceMode(platform) != STACK_TRACE_DISABLED) { platform->updateStackTrace(platform); diff --git a/src/debugger/stack-trace.c b/src/debugger/stack-trace.c index c91655e42..acb7b505e 100644 --- a/src/debugger/stack-trace.c +++ b/src/debugger/stack-trace.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2013-2016 Jeffrey Pfau +/* Copyright (c) 2013-2020 Jeffrey Pfau * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this