From 8adabb86cf20a7cb42d196d3d289b42a9d915c56 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Tue, 10 Jan 2023 05:14:32 +0100 Subject: [PATCH] Debugger: Avoid ppcState global. --- .../Core/Core/Debugger/Debugger_SymbolMap.cpp | 43 +++++++++++-------- .../Core/Core/Debugger/Debugger_SymbolMap.h | 9 +++- .../Core/Core/Debugger/PPCDebugInterface.cpp | 14 +++--- Source/Core/Core/Debugger/PPCDebugInterface.h | 8 +++- Source/Core/Core/PowerPC/Expression.cpp | 3 +- .../Core/PowerPC/Interpreter/Interpreter.cpp | 3 +- Source/Core/Core/PowerPC/PowerPC.cpp | 2 +- Source/Core/DolphinQt/Debugger/CodeWidget.cpp | 3 +- 8 files changed, 54 insertions(+), 31 deletions(-) diff --git a/Source/Core/Core/Debugger/Debugger_SymbolMap.cpp b/Source/Core/Core/Debugger/Debugger_SymbolMap.cpp index 87ba8f44df..4909103976 100644 --- a/Source/Core/Core/Debugger/Debugger_SymbolMap.cpp +++ b/Source/Core/Core/Debugger/Debugger_SymbolMap.cpp @@ -16,6 +16,7 @@ #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PPCSymbolDB.h" #include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" namespace Dolphin_Debugger { @@ -43,11 +44,13 @@ static bool IsStackBottom(u32 addr) return !addr || !PowerPC::HostIsRAMAddress(addr); } -static void WalkTheStack(const std::function& stack_step) +static void WalkTheStack(Core::System& system, const std::function& stack_step) { - if (!IsStackBottom(PowerPC::ppcState.gpr[1])) + auto& ppc_state = system.GetPPCState(); + + if (!IsStackBottom(ppc_state.gpr[1])) { - u32 addr = PowerPC::HostRead_U32(PowerPC::ppcState.gpr[1]); // SP + u32 addr = PowerPC::HostRead_U32(ppc_state.gpr[1]); // SP // Walk the stack chain for (int count = 0; !IsStackBottom(addr + 4) && (count++ < 20); ++count) @@ -66,12 +69,14 @@ static void WalkTheStack(const std::function& stack_step) // Returns callstack "formatted for debugging" - meaning that it // includes LR as the last item, and all items are the last step, // instead of "pointing ahead" -bool GetCallstack(std::vector& output) +bool GetCallstack(Core::System& system, std::vector& output) { - if (!Core::IsRunning() || !PowerPC::HostIsRAMAddress(PowerPC::ppcState.gpr[1])) + auto& ppc_state = system.GetPPCState(); + + if (!Core::IsRunning() || !PowerPC::HostIsRAMAddress(ppc_state.gpr[1])) return false; - if (LR(PowerPC::ppcState) == 0) + if (LR(ppc_state) == 0) { CallstackEntry entry; entry.Name = "(error: LR=0)"; @@ -81,13 +86,12 @@ bool GetCallstack(std::vector& output) } CallstackEntry entry; - entry.Name = - fmt::format(" * {} [ LR = {:08x} ]\n", g_symbolDB.GetDescription(LR(PowerPC::ppcState)), - LR(PowerPC::ppcState) - 4); - entry.vAddress = LR(PowerPC::ppcState) - 4; + entry.Name = fmt::format(" * {} [ LR = {:08x} ]\n", g_symbolDB.GetDescription(LR(ppc_state)), + LR(ppc_state) - 4); + entry.vAddress = LR(ppc_state) - 4; output.push_back(entry); - WalkTheStack([&entry, &output](u32 func_addr) { + WalkTheStack(system, [&entry, &output](u32 func_addr) { std::string func_desc = g_symbolDB.GetDescription(func_addr); if (func_desc.empty() || func_desc == "Invalid") func_desc = "(unknown)"; @@ -99,23 +103,24 @@ bool GetCallstack(std::vector& output) return true; } -void PrintCallstack(Common::Log::LogType type, Common::Log::LogLevel level) +void PrintCallstack(Core::System& system, Common::Log::LogType type, Common::Log::LogLevel level) { - GENERIC_LOG_FMT(type, level, "== STACK TRACE - SP = {:08x} ==", PowerPC::ppcState.gpr[1]); + auto& ppc_state = system.GetPPCState(); - if (LR(PowerPC::ppcState) == 0) + GENERIC_LOG_FMT(type, level, "== STACK TRACE - SP = {:08x} ==", ppc_state.gpr[1]); + + if (LR(ppc_state) == 0) { GENERIC_LOG_FMT(type, level, " LR = 0 - this is bad"); } - if (g_symbolDB.GetDescription(PowerPC::ppcState.pc) != - g_symbolDB.GetDescription(LR(PowerPC::ppcState))) + if (g_symbolDB.GetDescription(ppc_state.pc) != g_symbolDB.GetDescription(LR(ppc_state))) { - GENERIC_LOG_FMT(type, level, " * {} [ LR = {:08x} ]", - g_symbolDB.GetDescription(LR(PowerPC::ppcState)), LR(PowerPC::ppcState)); + GENERIC_LOG_FMT(type, level, " * {} [ LR = {:08x} ]", g_symbolDB.GetDescription(LR(ppc_state)), + LR(ppc_state)); } - WalkTheStack([type, level](u32 func_addr) { + WalkTheStack(system, [type, level](u32 func_addr) { std::string func_desc = g_symbolDB.GetDescription(func_addr); if (func_desc.empty() || func_desc == "Invalid") func_desc = "(unknown)"; diff --git a/Source/Core/Core/Debugger/Debugger_SymbolMap.h b/Source/Core/Core/Debugger/Debugger_SymbolMap.h index 13107c2482..5f334c76c8 100644 --- a/Source/Core/Core/Debugger/Debugger_SymbolMap.h +++ b/Source/Core/Core/Debugger/Debugger_SymbolMap.h @@ -10,6 +10,11 @@ #include "Common/CommonTypes.h" #include "Common/Logging/Log.h" +namespace Core +{ +class System; +} + namespace Dolphin_Debugger { struct CallstackEntry @@ -18,8 +23,8 @@ struct CallstackEntry u32 vAddress = 0; }; -bool GetCallstack(std::vector& output); -void PrintCallstack(Common::Log::LogType type, Common::Log::LogLevel level); +bool GetCallstack(Core::System& system, std::vector& output); +void PrintCallstack(Core::System& system, Common::Log::LogType type, Common::Log::LogLevel level); void PrintDataBuffer(Common::Log::LogType type, const u8* data, size_t size, std::string_view title); void AddAutoBreakpoints(); diff --git a/Source/Core/Core/Debugger/PPCDebugInterface.cpp b/Source/Core/Core/Debugger/PPCDebugInterface.cpp index 404b73fc40..96ee849d50 100644 --- a/Source/Core/Core/Debugger/PPCDebugInterface.cpp +++ b/Source/Core/Core/Debugger/PPCDebugInterface.cpp @@ -23,6 +23,7 @@ #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PPCSymbolDB.h" #include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" void ApplyMemoryPatch(Common::Debug::MemoryPatch& patch, bool store_existing_value) { @@ -81,7 +82,10 @@ void PPCPatches::UnPatch(std::size_t index) PatchEngine::RemoveMemoryPatch(index); } -PPCDebugInterface::PPCDebugInterface() = default; +PPCDebugInterface::PPCDebugInterface(Core::System& system) : m_system(system) +{ +} + PPCDebugInterface::~PPCDebugInterface() = default; std::size_t PPCDebugInterface::SetWatch(u32 address, std::string name) @@ -449,7 +453,7 @@ PPCDebugInterface::GetMemoryAddressFromInstruction(const std::string& instructio if (is_reg == offset_match[0]) { const int register_index = std::stoi(offset_match.substr(1), nullptr, 10); - offset = (register_index == 0 ? 0 : PowerPC::ppcState.gpr[register_index]); + offset = (register_index == 0 ? 0 : m_system.GetPPCState().gpr[register_index]); } else { @@ -468,7 +472,7 @@ PPCDebugInterface::GetMemoryAddressFromInstruction(const std::string& instructio else i = std::stoi(register_match, nullptr, 10); - const u32 base_address = PowerPC::ppcState.gpr[i]; + const u32 base_address = m_system.GetPPCState().gpr[i]; if (!match.str(1).empty()) return base_address - offset; @@ -478,12 +482,12 @@ PPCDebugInterface::GetMemoryAddressFromInstruction(const std::string& instructio u32 PPCDebugInterface::GetPC() const { - return PowerPC::ppcState.pc; + return m_system.GetPPCState().pc; } void PPCDebugInterface::SetPC(u32 address) { - PowerPC::ppcState.pc = address; + m_system.GetPPCState().pc = address; } void PPCDebugInterface::RunToBreakpoint() diff --git a/Source/Core/Core/Debugger/PPCDebugInterface.h b/Source/Core/Core/Debugger/PPCDebugInterface.h index 4d44481a8b..118874fe21 100644 --- a/Source/Core/Core/Debugger/PPCDebugInterface.h +++ b/Source/Core/Core/Debugger/PPCDebugInterface.h @@ -12,6 +12,11 @@ #include "Common/DebugInterface.h" #include "Core/NetworkCaptureLogger.h" +namespace Core +{ +class System; +} + void ApplyMemoryPatch(Common::Debug::MemoryPatch& patch, bool store_existing_value = true); class PPCPatches final : public Common::Debug::MemoryPatches @@ -29,7 +34,7 @@ private: class PPCDebugInterface final : public Common::DebugInterface { public: - PPCDebugInterface(); + explicit PPCDebugInterface(Core::System& system); ~PPCDebugInterface() override; // Watches @@ -102,4 +107,5 @@ private: Common::Debug::Watches m_watches; PPCPatches m_patches; std::shared_ptr m_network_logger; + Core::System& m_system; }; diff --git a/Source/Core/Core/PowerPC/Expression.cpp b/Source/Core/Core/PowerPC/Expression.cpp index e298845da5..67e8104c38 100644 --- a/Source/Core/Core/PowerPC/Expression.cpp +++ b/Source/Core/Core/PowerPC/Expression.cpp @@ -20,6 +20,7 @@ #include "Core/Debugger/Debugger_SymbolMap.h" #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" template static T HostRead(u32 address); @@ -109,7 +110,7 @@ static double CallstackFunc(expr_func* f, vec_expr_t* args, void* c) return 0; std::vector stack; - bool success = Dolphin_Debugger::GetCallstack(stack); + bool success = Dolphin_Debugger::GetCallstack(Core::System::GetInstance(), stack); if (!success) return 0; diff --git a/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp b/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp index 5a2accae18..d26aa2b700 100644 --- a/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp +++ b/Source/Core/Core/PowerPC/Interpreter/Interpreter.cpp @@ -342,7 +342,8 @@ void Interpreter::unknown_instruction(UGeckoInstruction inst) const u32 opcode = PowerPC::HostRead_U32(last_pc); const std::string disasm = Common::GekkoDisassembler::Disassemble(opcode, last_pc); NOTICE_LOG_FMT(POWERPC, "Last PC = {:08x} : {}", last_pc, disasm); - Dolphin_Debugger::PrintCallstack(Common::Log::LogType::POWERPC, Common::Log::LogLevel::LNOTICE); + Dolphin_Debugger::PrintCallstack(Core::System::GetInstance(), Common::Log::LogType::POWERPC, + Common::Log::LogLevel::LNOTICE); NOTICE_LOG_FMT( POWERPC, "\nIntCPU: Unknown instruction {:08x} at PC = {:08x} last_PC = {:08x} LR = {:08x}\n", diff --git a/Source/Core/Core/PowerPC/PowerPC.cpp b/Source/Core/Core/PowerPC/PowerPC.cpp index 8f8a939879..228573b8e6 100644 --- a/Source/Core/Core/PowerPC/PowerPC.cpp +++ b/Source/Core/Core/PowerPC/PowerPC.cpp @@ -45,7 +45,7 @@ static CoreMode s_mode = CoreMode::Interpreter; BreakPoints breakpoints; MemChecks memchecks; -PPCDebugInterface debug_interface; +PPCDebugInterface debug_interface(Core::System::GetInstance()); static CoreTiming::EventType* s_invalidate_cache_thread_safe; diff --git a/Source/Core/DolphinQt/Debugger/CodeWidget.cpp b/Source/Core/DolphinQt/Debugger/CodeWidget.cpp index b7bdba7786..6bcb5396f8 100644 --- a/Source/Core/DolphinQt/Debugger/CodeWidget.cpp +++ b/Source/Core/DolphinQt/Debugger/CodeWidget.cpp @@ -24,6 +24,7 @@ #include "Core/PowerPC/MMU.h" #include "Core/PowerPC/PPCSymbolDB.h" #include "Core/PowerPC/PowerPC.h" +#include "Core/System.h" #include "DolphinQt/Host.h" #include "DolphinQt/Settings.h" @@ -328,7 +329,7 @@ void CodeWidget::UpdateCallstack() std::vector stack; - bool success = Dolphin_Debugger::GetCallstack(stack); + bool success = Dolphin_Debugger::GetCallstack(Core::System::GetInstance(), stack); if (!success) {