Skip to content

Conversation

@HamzaHassanain
Copy link
Contributor

@llvmbot
Copy link
Member

llvmbot commented Dec 24, 2025

@llvm/pr-subscribers-lldb

Author: Hamza Hassanain (HamzaHassanain)

Changes

#168920


Full diff: https://github.com/llvm/llvm-project/pull/173473.diff

3 Files Affected:

  • (added) lldb/test/API/python_api/sbtarget_extensions/Makefile (+3)
  • (added) lldb/test/API/python_api/sbtarget_extensions/TestSBTargetExtensions.py (+137)
  • (added) lldb/test/API/python_api/sbtarget_extensions/main.c (+7)
diff --git a/lldb/test/API/python_api/sbtarget_extensions/Makefile b/lldb/test/API/python_api/sbtarget_extensions/Makefile
new file mode 100644
index 0000000000000..10495940055b6
--- /dev/null
+++ b/lldb/test/API/python_api/sbtarget_extensions/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules
diff --git a/lldb/test/API/python_api/sbtarget_extensions/TestSBTargetExtensions.py b/lldb/test/API/python_api/sbtarget_extensions/TestSBTargetExtensions.py
new file mode 100644
index 0000000000000..d1b7ba1eabd0e
--- /dev/null
+++ b/lldb/test/API/python_api/sbtarget_extensions/TestSBTargetExtensions.py
@@ -0,0 +1,137 @@
+import re
+import uuid
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class SBTargetExtensionsTestCase(TestBase):
+
+    def test_equality(self):
+        """Test the equality operator for SBTarget."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        self.assertEqual(target, target)
+        self.assertNotEqual(target, lldb.SBTarget())
+
+    def test_module_access(self):
+        """Test the module access extension properties and methods."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        self.assertTrue(len(target.modules) > 0)
+        module = target.module[0]
+        self.assertTrue(module.IsValid())
+
+        self.assertEqual(target.module["a.out"], module)
+        self.assertEqual(target.module[module.file.fullpath], module)
+
+        # UUID strings on Linux might not be standard UUIDs (they are Build IDs).
+        # We try to convert, but if it fails, we skip the UUID object check.
+        uuid_str = module.GetUUIDString()
+        if uuid_str:
+            try:
+                uuid_obj = uuid.UUID(uuid_str)
+                self.assertEqual(target.module[uuid_obj], module)
+            except ValueError:
+                # The UUID string wasn't a standard UUID format, which is fine on Linux.
+                pass
+
+        self.assertEqual(len(target.module[re.compile("a.out")]), 1)
+        self.assertEqual(target.module[re.compile("a.out")][0], module)
+
+    def test_process_creation(self):
+        """Test process creation via extensions."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        self.assertFalse(target.process.IsValid())
+
+        process = target.LaunchSimple(None, None, self.get_process_working_directory())
+        self.assertTrue(process.IsValid())
+
+        # SBProcess objects don't support direct equality (==), compare IDs.
+        self.assertEqual(target.process.GetProcessID(), process.GetProcessID())
+
+    def test_breakpoints(self):
+        """Test breakpoint access via extensions."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        breakpoint = target.BreakpointCreateBySourceRegex("Set breakpoint here", lldb.SBFileSpec("main.c"))
+        self.assertTrue(breakpoint.IsValid())
+
+        self.assertEqual(target.num_breakpoints, 1)
+        self.assertEqual(len(target.breakpoints), 1)
+
+        # target.breakpoint[i] uses INDEX, not ID.
+        self.assertEqual(target.breakpoint[0].GetID(), target.breakpoints[0].GetID())
+
+        # To verify ID lookup works via the standard API:
+        self.assertEqual(target.FindBreakpointByID(breakpoint.GetID()).GetID(), breakpoint.GetID())
+
+    def test_watchpoints(self):
+        """Test watchpoint access via extensions."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        # 1. Set a breakpoint so the process stops and stays alive.
+        breakpoint = target.BreakpointCreateBySourceRegex("Set breakpoint here", lldb.SBFileSpec("main.c"))
+        self.assertTrue(breakpoint.IsValid())
+
+        # 2. Launch the process.
+        process = target.LaunchSimple(None, None, self.get_process_working_directory())
+        self.assertTrue(process.IsValid())
+
+        # 3. Ensure we are stopped.
+        self.assertEqual(process.GetState(), lldb.eStateStopped)
+
+        variables = target.FindGlobalVariables("g_var", 1)
+        self.assertTrue(variables.GetSize() > 0)
+
+        global_variable = variables.GetValueAtIndex(0)
+        error = lldb.SBError()
+
+        # 4. Now we can set the watchpoint.
+        watchpoint = target.WatchAddress(global_variable.GetLoadAddress(), 4, False, True, error)
+        self.assertTrue(error.Success(), f"Watchpoint failed: {error.GetCString()}")
+
+        self.assertTrue(target.num_watchpoints > 0)
+        self.assertEqual(len(target.watchpoints), target.num_watchpoints)
+
+        self.assertEqual(target.watchpoint[0].GetID(), target.watchpoints[0].GetID())
+        self.assertEqual(target.watchpoint[0].GetID(), watchpoint.GetID())
+
+    def test_other_properties(self):
+        """Test miscellaneous properties of SBTarget."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target.IsValid())
+
+        self.assertTrue(target.executable.IsValid())
+
+        self.assertEqual(target.debugger.GetID(), self.dbg.GetID())
+
+        self.assertTrue(target.broadcaster.IsValid())
+        self.assertIn(target.byte_order, [lldb.eByteOrderLittle, lldb.eByteOrderBig, lldb.eByteOrderInvalid])
+        self.assertTrue(target.addr_size > 0)
+        self.assertIsNotNone(target.triple)
+        self.assertIsNotNone(target.arch_name)
+
+        self.assertTrue(target.data_byte_size > 0)
+        self.assertTrue(target.code_byte_size > 0)
+
+        self.assertTrue(target.platform.IsValid())
\ No newline at end of file
diff --git a/lldb/test/API/python_api/sbtarget_extensions/main.c b/lldb/test/API/python_api/sbtarget_extensions/main.c
new file mode 100644
index 0000000000000..6ef2ce65d0ae9
--- /dev/null
+++ b/lldb/test/API/python_api/sbtarget_extensions/main.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int g_var = 10;
+
+int main() {
+    return g_var; // Set breakpoint here
+}

@github-actions
Copy link

github-actions bot commented Dec 24, 2025

✅ With the latest revision this PR passed the Python code formatter.

@github-actions
Copy link

github-actions bot commented Dec 24, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@HamzaHassanain
Copy link
Contributor Author

Hello @JDevlieghere!
Can you take a look at this?

Copy link
Member

@JDevlieghere JDevlieghere left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Just a few small nits.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think these comments are adding much value. It's often sufficient to use newlines to delineate the areas (as is the case now) at which point these comments just add noise and become a maintenance burden. Let's remove them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had the same Idea, but I saw other test files with comments. I will remove them!

Copy link
Member

Choose a reason for hiding this comment

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

Missing newline at the end of the file.

@github-actions
Copy link

github-actions bot commented Jan 4, 2026

🐧 Linux x64 Test Results

  • 33246 tests passed
  • 503 tests skipped

✅ The build succeeded and all tests passed.

@HamzaHassanain
Copy link
Contributor Author

I think I broke something. will fix it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants