Skip to content
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

Breakpoint improvements #1809

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
10 changes: 9 additions & 1 deletion src/core/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Debug {
uint32_t normalizeAddress(uint32_t address);
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 @@ -64,7 +65,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 @@ -76,6 +77,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 @@ -93,6 +98,8 @@ class Debug {
}

const BreakpointType m_type;
BreakpointCondition m_condition = BreakpointCondition::Always;
uint32_t m_conditionData;
Comment on lines +103 to +104
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Initialize m_conditionData member

The m_conditionData member is not initialized, which could lead to undefined behavior. Initialize it to 0 since it's unused for the default Always condition.

         BreakpointCondition m_condition = BreakpointCondition::Always;
-        uint32_t m_conditionData;
+        uint32_t m_conditionData = 0;
📝 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
BreakpointCondition m_condition = BreakpointCondition::Always;
uint32_t m_conditionData;
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 @@ -155,6 +162,7 @@ class Debug {
if (m_lastBP == bp) m_lastBP = nullptr;
delete const_cast<Breakpoint*>(bp);
}
void removeAllBreakpoints() { m_breakpoints.clear(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Reset m_lastBP in removeAllBreakpoints

The removeAllBreakpoints method should reset m_lastBP to maintain consistency with removeBreakpoint.

-    void removeAllBreakpoints() { m_breakpoints.clear(); }
+    void removeAllBreakpoints() {
+        m_lastBP = nullptr;
+        m_breakpoints.clear();
+    }
📝 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
void removeAllBreakpoints() { m_breakpoints.clear(); }
void removeAllBreakpoints() {
m_lastBP = nullptr;
m_breakpoints.clear();
}


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
@@ -1,4 +1,4 @@
/***************************************************************************

Check notice on line 1 in src/gui/widgets/assembly.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1013 to 1019, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in src/gui/widgets/assembly.cc

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 7.60 to 7.67, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
* Copyright (C) 2019 PCSX-Redux authors *
* *
* This program is free software; you can redistribute it and/or modify *
Expand Down 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