From 3cba1135d57f8054a6b8a7ebaaf62e7f4ebbe1f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 24 Nov 2024 16:48:38 +0100 Subject: [PATCH] Add basic breakpoint editing window. Need UI refinement --- Core/Debugger/Breakpoints.cpp | 46 ++++---- Core/Debugger/Breakpoints.h | 15 ++- UI/ImDebugger/ImDebugger.cpp | 151 +++++++++++++++++++++++--- UI/ImDebugger/ImDebugger.h | 1 + UI/ImDebugger/ImDisasmView.h | 2 +- Windows/Debugger/BreakpointWindow.cpp | 1 + Windows/Debugger/BreakpointWindow.h | 4 +- Windows/Debugger/Debugger_Lists.cpp | 2 +- 8 files changed, 177 insertions(+), 45 deletions(-) diff --git a/Core/Debugger/Breakpoints.cpp b/Core/Debugger/Breakpoints.cpp index 09c04f79bb..eec7e08a57 100644 --- a/Core/Debugger/Breakpoints.cpp +++ b/Core/Debugger/Breakpoints.cpp @@ -151,12 +151,10 @@ bool BreakpointManager::RangeContainsBreakPoint(u32 addr, u32 size) return false; } -void BreakpointManager::AddBreakPoint(u32 addr, bool temp) -{ +int BreakpointManager::AddBreakPoint(u32 addr, bool temp) { std::unique_lock guard(breakPointsMutex_); size_t bp = FindBreakpoint(addr, true, temp); - if (bp == INVALID_BREAKPOINT) - { + if (bp == INVALID_BREAKPOINT) { BreakPoint pt; pt.result |= BREAK_ACTION_PAUSE; pt.temporary = temp; @@ -166,22 +164,24 @@ void BreakpointManager::AddBreakPoint(u32 addr, bool temp) anyBreakPoints_ = true; guard.unlock(); Update(addr); - } - else if (!breakPoints_[bp].IsEnabled()) - { + + return (int)breakPoints_.size() - 1; + } else if (!breakPoints_[bp].IsEnabled()) { breakPoints_[bp].result |= BREAK_ACTION_PAUSE; breakPoints_[bp].hasCond = false; guard.unlock(); Update(addr); + return (int)bp; + } else { + // nothing to do, just return the already-existing breakpoint index + return (int)bp; } } -void BreakpointManager::RemoveBreakPoint(u32 addr) -{ +void BreakpointManager::RemoveBreakPoint(u32 addr) { std::unique_lock guard(breakPointsMutex_); size_t bp = FindBreakpoint(addr); - if (bp != INVALID_BREAKPOINT) - { + if (bp != INVALID_BREAKPOINT) { breakPoints_.erase(breakPoints_.begin() + bp); // Check again, there might've been an overlapping temp breakpoint. @@ -195,12 +195,10 @@ void BreakpointManager::RemoveBreakPoint(u32 addr) } } -void BreakpointManager::ChangeBreakPoint(u32 addr, bool status) -{ +void BreakpointManager::ChangeBreakPoint(u32 addr, bool status) { std::unique_lock guard(breakPointsMutex_); size_t bp = FindBreakpoint(addr); - if (bp != INVALID_BREAKPOINT) - { + if (bp != INVALID_BREAKPOINT) { if (status) breakPoints_[bp].result |= BREAK_ACTION_PAUSE; else @@ -211,12 +209,10 @@ void BreakpointManager::ChangeBreakPoint(u32 addr, bool status) } } -void BreakpointManager::ChangeBreakPoint(u32 addr, BreakAction result) -{ +void BreakpointManager::ChangeBreakPoint(u32 addr, BreakAction result) { std::unique_lock guard(breakPointsMutex_); size_t bp = FindBreakpoint(addr); - if (bp != INVALID_BREAKPOINT) - { + if (bp != INVALID_BREAKPOINT) { breakPoints_[bp].result = result; guard.unlock(); Update(addr); @@ -336,13 +332,11 @@ BreakAction BreakpointManager::ExecBreakPoint(u32 addr) { return BREAK_ACTION_IGNORE; } -void BreakpointManager::AddMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result) -{ +int BreakpointManager::AddMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result) { std::unique_lock guard(memCheckMutex_); size_t mc = FindMemCheck(start, end); - if (mc == INVALID_MEMCHECK) - { + if (mc == INVALID_MEMCHECK) { MemCheck check; check.start = start; check.end = end; @@ -355,9 +349,8 @@ void BreakpointManager::AddMemCheck(u32 start, u32 end, MemCheckCondition cond, MemBlockOverrideDetailed(); guard.unlock(); Update(); - } - else - { + return (int)memChecks_.size() - 1; + } else { memChecks_[mc].cond = (MemCheckCondition)(memChecks_[mc].cond | cond); memChecks_[mc].result = (BreakAction)(memChecks_[mc].result | result); bool hadAny = anyMemChecks_.exchange(true); @@ -365,6 +358,7 @@ void BreakpointManager::AddMemCheck(u32 start, u32 end, MemCheckCondition cond, MemBlockOverrideDetailed(); guard.unlock(); Update(); + return (int)mc; } } diff --git a/Core/Debugger/Breakpoints.h b/Core/Debugger/Breakpoints.h index c6e40a14d7..282abb76d9 100644 --- a/Core/Debugger/Breakpoints.h +++ b/Core/Debugger/Breakpoints.h @@ -21,7 +21,7 @@ #include "Core/Debugger/DebugInterface.h" -enum BreakAction { +enum BreakAction : u32 { BREAK_ACTION_IGNORE = 0x00, BREAK_ACTION_LOG = 0x01, BREAK_ACTION_PAUSE = 0x02, @@ -124,7 +124,7 @@ public: bool IsAddressBreakPoint(u32 addr, bool* enabled); bool IsTempBreakPoint(u32 addr); bool RangeContainsBreakPoint(u32 addr, u32 size); - void AddBreakPoint(u32 addr, bool temp = false); + int AddBreakPoint(u32 addr, bool temp = false); // Returns the breakpoint index. void RemoveBreakPoint(u32 addr); void ChangeBreakPoint(u32 addr, bool enable); void ChangeBreakPoint(u32 addr, BreakAction result); @@ -140,7 +140,7 @@ public: BreakAction ExecBreakPoint(u32 addr); - void AddMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result); + int AddMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result); void RemoveMemCheck(u32 start, u32 end); void ChangeMemCheck(u32 start, u32 end, MemCheckCondition cond, BreakAction result); void ClearAllMemChecks(); @@ -165,6 +165,15 @@ public: std::vector GetMemChecks(); std::vector GetBreakpoints(); + // For editing through the imdebugger. + // Since it's on the main thread, we don't need to fear threading clashes. + std::vector &GetBreakpointRefs() { + return breakPoints_; + } + std::vector &GetMemCheckRefs() { + return memChecks_; + } + bool HasBreakPoints(); bool HasMemChecks(); diff --git a/UI/ImDebugger/ImDebugger.cpp b/UI/ImDebugger/ImDebugger.cpp index b24aae80f3..ef672c7da4 100644 --- a/UI/ImDebugger/ImDebugger.cpp +++ b/UI/ImDebugger/ImDebugger.cpp @@ -182,25 +182,150 @@ static const char *MemCheckConditionToString(MemCheckCondition cond) { } } -void DrawBreakpointsView(MIPSDebugInterface *mipsDebug, bool *open) { - if (!ImGui::Begin("Breakpoints", open)) { +static void DrawBreakpointsView(MIPSDebugInterface *mipsDebug, ImConfig &cfg) { + if (!ImGui::Begin("Breakpoints", &cfg.breakpointsOpen)) { ImGui::End(); return; } - if (ImGui::BeginTable("breakpoints", 8, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersH)) { - ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed); - ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed); - ImGui::TableSetupColumn("value_i", ImGuiTableColumnFlags_WidthStretch); - ImGui::TableHeadersRow(); + if (ImGui::BeginTable("bp_window", 2, ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersInnerV)) { + ImGui::TableSetupColumn("left", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn("right", ImGuiTableColumnFlags_WidthFixed, 150.0f); + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + auto &bps = g_breakpoints.GetBreakpointRefs(); + auto &mcs = g_breakpoints.GetMemCheckRefs(); - for (int i = 0; i < 32; i++) { - ImGui::TableNextRow(); - ImGui::TableNextColumn(); - ImGui::Text(""); - ImGui::TableNextColumn(); + if (cfg.selectedBreakpoint >= bps.size()) { + cfg.selectedBreakpoint = -1; + } + if (cfg.selectedMemCheck >= mcs.size()) { + cfg.selectedMemCheck = -1; + } + + if (ImGui::Button("Add Breakpoint")) { + cfg.selectedBreakpoint = g_breakpoints.AddBreakPoint(0); + cfg.selectedMemCheck = -1; + } + ImGui::SameLine(); + if (ImGui::Button("Add MemCheck")) { + cfg.selectedMemCheck = g_breakpoints.AddMemCheck(0, 0, MemCheckCondition::MEMCHECK_WRITE, BreakAction::BREAK_ACTION_PAUSE); + cfg.selectedBreakpoint = -1; + } + + if (ImGui::BeginTable("breakpoints", 8, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersH)) { + ImGui::TableSetupColumn("Enabled", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Address", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Size/Label", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("OpCode", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Cond", ImGuiTableColumnFlags_WidthFixed); + ImGui::TableSetupColumn("Hits", ImGuiTableColumnFlags_WidthStretch); + ImGui::TableHeadersRow(); + + for (int i = 0; i < (int)bps.size(); i++) { + auto &bp = bps[i]; + if (bp.temporary) { + // Probably won't happen a lot.. maybe a wrong Step Over + continue; + } + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::PushID(i); + // TODO: This clashes with the checkbox! + if (ImGui::Selectable("", cfg.selectedBreakpoint == i, ImGuiSelectableFlags_SpanAllColumns)) { + cfg.selectedBreakpoint = i; + cfg.selectedMemCheck = -1; + } + ImGui::SameLine(); + ImGui::CheckboxFlags("##enabled", (int *)&bp.result, (int)BREAK_ACTION_PAUSE); + ImGui::TableNextColumn(); + ImGui::TextUnformatted("Exec"); + ImGui::TableNextColumn(); + ImGui::Text("%08x", bp.addr); + ImGui::TableNextColumn(); + const std::string sym = g_symbolMap->GetLabelString(bp.addr); + if (!sym.empty()) { + ImGui::TextUnformatted(sym.c_str()); // size/label + } else { + ImGui::TextUnformatted("-"); // size/label + } + ImGui::TableNextColumn(); + // disasm->getOpcodeText(displayedBreakPoints_[index].addr, temp, sizeof(temp)); + ImGui::TextUnformatted("-"); // opcode + ImGui::TableNextColumn(); + if (bp.hasCond) { + ImGui::Text("%s", bp.cond.expressionString.c_str()); + } else { + ImGui::TextUnformatted("-"); // condition + } + ImGui::TableNextColumn(); + ImGui::Text("-"); // hits not available on exec bps yet + ImGui::PopID(); + } + + // OK, now list the memchecks. + for (int i = 0; i < (int)mcs.size(); i++) { + auto &mc = mcs[i]; + ImGui::TableNextRow(); + ImGui::TableNextColumn(); + ImGui::PushID(i + 10000); + if (ImGui::Selectable("", cfg.selectedMemCheck == i, ImGuiSelectableFlags_SpanAllColumns)) { + cfg.selectedBreakpoint = -1; + cfg.selectedMemCheck = i; + } + ImGui::SameLine(); + ImGui::CheckboxFlags("", (int *)&mc.result, BREAK_ACTION_PAUSE); + ImGui::TableNextColumn(); + ImGui::Text("%s", MemCheckConditionToString(mc.cond)); + ImGui::TableNextColumn(); + ImGui::Text("%08x", mc.start); + ImGui::TableNextColumn(); + ImGui::Text("%08x", mc.end ? (mc.end - mc.start) : 1); + ImGui::TableNextColumn(); + ImGui::TextUnformatted("-"); // opcode + ImGui::TableNextColumn(); + ImGui::TextUnformatted("-"); // cond + ImGui::TableNextColumn(); + ImGui::Text("%d", mc.numHits); + ImGui::PopID(); + } + + ImGui::EndTable(); + } + + ImGui::TableNextColumn(); + + if (cfg.selectedBreakpoint >= 0) { + // Add edit form for breakpoints + if (ImGui::BeginChild("bp_edit")) { + auto &bp = bps[cfg.selectedBreakpoint]; + ImGui::TextUnformatted("Edit breakpoint"); + ImGui::CheckboxFlags("Enabled", (int *)&bp.result, (int)BREAK_ACTION_PAUSE); + ImGui::InputScalar("Address", ImGuiDataType_U32, &bp.addr, nullptr, nullptr, "%08x", ImGuiInputTextFlags_CharsHexadecimal); + if (ImGui::Button("Delete")) { + g_breakpoints.RemoveBreakPoint(bp.addr); + } + ImGui::EndChild(); + } + } + + if (cfg.selectedMemCheck >= 0) { + // Add edit form for memchecks + if (ImGui::BeginChild("mc_edit")) { + auto &mc = mcs[cfg.selectedMemCheck]; + ImGui::TextUnformatted("Edit memcheck"); + ImGui::CheckboxFlags("Enabled", (int *)&mc.result, (int)BREAK_ACTION_PAUSE); + ImGui::InputScalar("Start", ImGuiDataType_U32, &mc.start); + ImGui::InputScalar("End", ImGuiDataType_U32, &mc.end); + if (ImGui::Button("Delete")) { + g_breakpoints.RemoveMemCheck(mcs[cfg.selectedMemCheck].start, mcs[cfg.selectedMemCheck].end); + } + ImGui::EndChild(); + } } ImGui::EndTable(); } + ImGui::End(); } @@ -454,7 +579,7 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu } if (cfg_.breakpointsOpen) { - DrawBreakpointsView(mipsDebug, &cfg_.breakpointsOpen); + DrawBreakpointsView(mipsDebug, cfg_); } if (cfg_.threadsOpen) { diff --git a/UI/ImDebugger/ImDebugger.h b/UI/ImDebugger/ImDebugger.h index dda2ed03bf..933f272a35 100644 --- a/UI/ImDebugger/ImDebugger.h +++ b/UI/ImDebugger/ImDebugger.h @@ -76,6 +76,7 @@ struct ImConfig { int selectedThread = 0; int selectedFramebuffer = -1; int selectedBreakpoint = -1; + int selectedMemCheck = -1; }; enum ImUiCmd { diff --git a/UI/ImDebugger/ImDisasmView.h b/UI/ImDebugger/ImDisasmView.h index 6de54b5098..7ff9ad7932 100644 --- a/UI/ImDebugger/ImDisasmView.h +++ b/UI/ImDebugger/ImDisasmView.h @@ -39,7 +39,7 @@ public: void ScanVisibleFunctions(); void clearFunctions() { manager.clear(); }; - void getOpcodeText(u32 address, char* dest, int bufsize); + void getOpcodeText(u32 address, char *dest, int bufsize); u32 yToAddress(float y); void setDebugger(DebugInterface *deb) { diff --git a/Windows/Debugger/BreakpointWindow.cpp b/Windows/Debugger/BreakpointWindow.cpp index 6a9b5786b9..0af371cd18 100644 --- a/Windows/Debugger/BreakpointWindow.cpp +++ b/Windows/Debugger/BreakpointWindow.cpp @@ -3,6 +3,7 @@ #include "Common/Data/Encoding/Utf8.h" #include "Common/Math/expression_parser.h" +#include "Core/Debugger/Breakpoints.h" #include "BreakpointWindow.h" #include "../resource.h" diff --git a/Windows/Debugger/BreakpointWindow.h b/Windows/Debugger/BreakpointWindow.h index a367e7da4f..06024e7c2f 100644 --- a/Windows/Debugger/BreakpointWindow.h +++ b/Windows/Debugger/BreakpointWindow.h @@ -3,7 +3,9 @@ #include "Common/CommonWindows.h" #include "Common/CommonTypes.h" #include "Core/Debugger/DebugInterface.h" -#include "Core/Debugger/Breakpoints.h" + +struct BreakPoint; +struct MemCheck; class BreakpointWindow { diff --git a/Windows/Debugger/Debugger_Lists.cpp b/Windows/Debugger/Debugger_Lists.cpp index 58535d4ed9..e07aa38fc3 100644 --- a/Windows/Debugger/Debugger_Lists.cpp +++ b/Windows/Debugger/Debugger_Lists.cpp @@ -489,7 +489,7 @@ void CtrlBreakpointList::GetColumnText(wchar_t* dest, int row, int col) break; } } else { - wcscpy(dest,L"Execute"); + wcscpy(dest, L"Execute"); } } break;