Skip to content

Breakpoint improvements #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/core/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Debug {
static bool isInKernel(uint32_t address, bool biosIsKernel = true);
static inline std::function<const char*()> s_breakpoint_type_names[] = {l_("Exec"), l_("Read"), l_("Write")};
enum class BreakpointType { Exec, Read, Write };
enum class BreakpointCondition { Always, Change, Greater, Less, Equal };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add documentation and Range condition

The new BreakpointCondition enum should include:

  1. Documentation explaining the behavior of each condition
  2. The Range condition mentioned in the PR objectives
+    /// Breakpoint conditions that determine when a breakpoint should trigger
     enum class BreakpointCondition {
+        /// Always trigger the breakpoint
         Always,
+        /// Trigger when the value changes
         Change,
+        /// Trigger when the value is greater than conditionData
         Greater,
+        /// Trigger when the value is less than conditionData
         Less,
+        /// Trigger when the value equals conditionData
         Equal,
+        /// Trigger when the value is within a range (requires two values in conditionData)
+        Range
     };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
enum class BreakpointCondition { Always, Change, Greater, Less, Equal };
/// Breakpoint conditions that determine when a breakpoint should trigger
enum class BreakpointCondition {
/// Always trigger the breakpoint
Always,
/// Trigger when the value changes
Change,
/// Trigger when the value is greater than conditionData
Greater,
/// Trigger when the value is less than conditionData
Less,
/// Trigger when the value equals conditionData
Equal,
/// Trigger when the value is within a range (requires two values in conditionData)
Range
};


void checkDMAread(unsigned c, uint32_t address, uint32_t len) {
std::string cause = fmt::format("DMA channel {} read", c);
Expand Down Expand Up @@ -66,7 +67,7 @@ class Debug {
struct InternalTemporaryList {};
typedef Intrusive::List<Breakpoint, InternalTemporaryList> BreakpointTemporaryListType;

typedef std::function<bool(const Breakpoint*, uint32_t address, unsigned width, const char* cause)>
typedef std::function<bool(Breakpoint*, uint32_t address, unsigned width, const char* cause)>
BreakpointInvoker;

class Breakpoint : public BreakpointTreeType::Node,
Expand All @@ -78,6 +79,10 @@ class Debug {
: m_type(type), m_source(source), m_invoker(invoker), m_base(base), m_label(label) {}
std::string name() const;
BreakpointType type() const { return m_type; }
BreakpointCondition condition() const { return m_condition; }
void setCondition(BreakpointCondition condition) { m_condition = condition; }
uint32_t conditionData() const { return m_conditionData; }
void setConditionData(uint32_t data) { m_conditionData = data; }
Comment on lines +82 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add validation and documentation for condition methods

The condition methods need:

  1. Documentation explaining how conditionData is used for each condition type
  2. Validation in setConditionData based on the current condition
+        /// @returns The current condition for this breakpoint
         BreakpointCondition condition() const { return m_condition; }
+        /// @param condition The new condition to set
         void setCondition(BreakpointCondition condition) { m_condition = condition; }
+        /// @returns The condition-specific data:
+        /// - For Greater/Less/Equal: The value to compare against
+        /// - For Range: The upper bound (lower bound stored internally)
+        /// - For Always/Change: Unused
         uint32_t conditionData() const { return m_conditionData; }
+        /// @param data The condition-specific data to set
+        /// @throws std::invalid_argument if data is invalid for current condition
         void setConditionData(uint32_t data) {
+            switch (m_condition) {
+                case BreakpointCondition::Range:
+                    if (data <= getLow()) throw std::invalid_argument("Range upper bound must be greater than lower bound");
+                    break;
+                case BreakpointCondition::Always:
+                case BreakpointCondition::Change:
+                    if (data != 0) throw std::invalid_argument("Condition data must be 0 for Always/Change conditions");
+                    break;
+            }
             m_conditionData = data;
         }

Committable suggestion skipped: line range outside the PR's diff.

unsigned width() const { return getHigh() - getLow() + 1; }
uint32_t address() const { return getLow(); }
bool enabled() const { return m_enabled; }
Expand All @@ -95,6 +100,8 @@ class Debug {
}

const BreakpointType m_type;
BreakpointCondition m_condition = BreakpointCondition::Always;
uint32_t m_conditionData = 0;
const std::string m_source;
const BreakpointInvoker m_invoker;
mutable std::string m_label;
Expand Down Expand Up @@ -158,6 +165,10 @@ class Debug {
if (m_lastBP == bp) m_lastBP = nullptr;
delete const_cast<Breakpoint*>(bp);
}
void removeAllBreakpoints() {
m_breakpoints.clear();
m_lastBP = nullptr;
}

private:
bool triggerBP(Breakpoint* bp, uint32_t address, unsigned width, const char* reason = "");
Expand Down
6 changes: 6 additions & 0 deletions src/gui/widgets/assembly.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@
itemLabel = fmt::format(f_("Go to in Memory Editor #{}"), i + 1);
if (ImGui::MenuItem(itemLabel.c_str())) jumpToMemory(addr, size, i, true);
}
if (ImGui::MenuItem(_("Create Memory Read Breakpoint"))) {
g_emulator->m_debug->addBreakpoint(addr, Debug::BreakpointType::Read, size, _("GUI"));
}
if (ImGui::MenuItem(_("Create Memory Write Breakpoint"))) {
g_emulator->m_debug->addBreakpoint(addr, Debug::BreakpointType::Write, size, _("GUI"));
}

Check warning on line 348 in src/gui/widgets/assembly.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

PCSX::Widgets::Assembly::addMemoryEditorContext has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
ImGui::EndPopup();
}
}
Expand Down
Loading
Loading