[Lldb-commits] [lldb] [lldb] End to end test for address spaces using lldb-server mock accelerator plugin (PR #217092)

satyanarayana reddy janga via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 2 10:08:27 PDT 2026


https://github.com/satyajanga updated https://github.com/llvm/llvm-project/pull/217092

>From 8e87d390210517af3d6301fd9c2409aac5efd6be Mon Sep 17 00:00:00 2001
From: satya janga <satyajanga at fb.com>
Date: Tue, 18 Aug 2026 10:41:08 -0700
Subject: [PATCH] [lldb] Test address spaces reported by an lldb-server process
 plugin

Nothing in tree overrides NativeProcessProtocol::GetAddressSpaces(), so the
server side of the feature was only covered for the empty case: qSupported
never advertised "address-spaces+" and jAddressSpacesInfo always answered
unimplemented.

Report two address spaces from the mock accelerator process, one of them
thread specific, and answer reads with bytes identifying the address space and
thread the request arrived with. A test then drives a real lldb-server end to
end: resolving spaces by name, reading the same numeric address from two
spaces, and checking that the thread rides along only for the thread specific
one. Advertising the feature needs both the manager's
Extension::address_spaces and a non-empty list from the process.
---
 .../API/accelerator/address_spaces/Makefile   |  3 +
 .../TestMockAcceleratorAddressSpaces.py       | 94 +++++++++++++++++++
 .../API/accelerator/address_spaces/main.c     | 20 ++++
 .../Mock/ProcessMockAccelerator.cpp           | 21 ++++-
 .../Accelerator/Mock/ProcessMockAccelerator.h |  8 ++
 5 files changed, 144 insertions(+), 2 deletions(-)
 create mode 100644 lldb/test/API/accelerator/address_spaces/Makefile
 create mode 100644 lldb/test/API/accelerator/address_spaces/TestMockAcceleratorAddressSpaces.py
 create mode 100644 lldb/test/API/accelerator/address_spaces/main.c

diff --git a/lldb/test/API/accelerator/address_spaces/Makefile b/lldb/test/API/accelerator/address_spaces/Makefile
new file mode 100644
index 0000000000000..10495940055b6
--- /dev/null
+++ b/lldb/test/API/accelerator/address_spaces/Makefile
@@ -0,0 +1,3 @@
+C_SOURCES := main.c
+
+include Makefile.rules
diff --git a/lldb/test/API/accelerator/address_spaces/TestMockAcceleratorAddressSpaces.py b/lldb/test/API/accelerator/address_spaces/TestMockAcceleratorAddressSpaces.py
new file mode 100644
index 0000000000000..8fb523ae5f69a
--- /dev/null
+++ b/lldb/test/API/accelerator/address_spaces/TestMockAcceleratorAddressSpaces.py
@@ -0,0 +1,94 @@
+"""
+End-to-end test for address spaces served by an lldb-server process plugin.
+
+The mock accelerator process reports two address spaces, so this exercises the
+server side of the feature that has no in-tree producer otherwise: advertising
+"address-spaces+" in qSupported and answering "jAddressSpacesInfo". The client
+then resolves those spaces by name through SBProcess::GetAddressSpaceID().
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import configuration
+
+
+class MockAcceleratorAddressSpacesTestCase(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def setUp(self):
+        super().setUp()
+        if "mock-accelerator" not in configuration.enabled_plugins:
+            self.skipTest("mock-accelerator plugin is not enabled")
+
+    def accelerator_target(self, native_target):
+        for i in range(self.dbg.GetNumTargets()):
+            candidate = self.dbg.GetTargetAtIndex(i)
+            if candidate != native_target:
+                return candidate
+        return None
+
+    @skipIfRemote
+    @add_test_categories(["llgs"])
+    def test_address_spaces_from_process_plugin(self):
+        """The accelerator process reports its address spaces to the client."""
+        self.build()
+        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        self.assertTrue(target, VALID_TARGET)
+
+        # Launch stops at the plugin's initialize breakpoint; continuing reaches
+        # the connection hook that creates the accelerator target.
+        process = target.LaunchSimple(None, None, self.get_process_working_directory())
+        self.assertTrue(process, PROCESS_IS_VALID)
+        self.assertState(process.GetState(), lldb.eStateStopped)
+        process.Continue()
+        self.assertState(process.GetState(), lldb.eStateStopped)
+
+        self.assertEqual(self.dbg.GetNumTargets(), 2)
+        accelerator_process = self.accelerator_target(target).GetProcess()
+        self.assertTrue(accelerator_process.IsValid())
+
+        # ProcessMockAccelerator reports "global" (id 1) and "local" (id 2).
+        error = lldb.SBError()
+        self.assertEqual(accelerator_process.GetAddressSpaceID("global", error), 1)
+        self.assertSuccess(error)
+        self.assertEqual(accelerator_process.GetAddressSpaceID("local", error), 2)
+        self.assertSuccess(error)
+
+        # An unknown name is an error rather than a silent default.
+        accelerator_process.GetAddressSpaceID("nonexistent", error)
+        self.assertTrue(error.Fail())
+
+        # The native process has no address spaces, so the same lookup fails
+        # there even though both targets talk to an lldb-server.
+        process.GetAddressSpaceID("global", error)
+        self.assertTrue(error.Fail())
+
+        # Read the same numeric address from both spaces. The mock process
+        # answers with the address space in byte 0 and the thread in byte 1, so
+        # these show what actually reached the far side of the connection.
+        thread = accelerator_process.GetThreadAtIndex(0)
+        self.assertTrue(thread.IsValid())
+        tid = thread.GetThreadID()
+
+        # "global" is not thread specific, so no thread is sent.
+        global_bytes = accelerator_process.ReadMemory(
+            lldb.SBProcessAddress(0x1000, 1), 4, error
+        )
+        self.assertSuccess(error)
+        self.assertEqual(global_bytes, bytes([1, 0, 0, 0]))
+
+        # "local" is thread specific, so the thread rides along with the read.
+        local_bytes = accelerator_process.ReadMemory(
+            lldb.SBProcessAddress(0x1000, 2, thread), 4, error
+        )
+        self.assertSuccess(error)
+        self.assertEqual(local_bytes, bytes([2, tid & 0xFF, 0, 0]))
+
+        self.assertNotEqual(global_bytes, local_bytes)
+
+        # Reading a thread specific space without a thread never reaches the
+        # server.
+        accelerator_process.ReadMemory(lldb.SBProcessAddress(0x1000, 2), 4, error)
+        self.assertTrue(error.Fail())
+        self.assertIn("thread specific", error.GetCString())
diff --git a/lldb/test/API/accelerator/address_spaces/main.c b/lldb/test/API/accelerator/address_spaces/main.c
new file mode 100644
index 0000000000000..18aed944b8d8f
--- /dev/null
+++ b/lldb/test/API/accelerator/address_spaces/main.c
@@ -0,0 +1,20 @@
+// The mock accelerator plugin sets its breakpoints on these dedicated, uniquely
+// named functions.
+void mock_gpu_accelerator_initialize(void) {}
+
+int mock_gpu_accelerator_compute(int x) { return x * 2; }
+
+int mock_gpu_accelerator_finish(void) { return 0; }
+
+// When the plugin's connection-trigger breakpoint on this function is hit, it
+// asks the client to create a second target and connect to the mock accelerator
+// GDB server.
+void mock_gpu_accelerator_connect(void) {}
+
+int main(void) {
+  mock_gpu_accelerator_initialize();
+  mock_gpu_accelerator_connect();
+  int result = mock_gpu_accelerator_compute(21);
+  mock_gpu_accelerator_finish();
+  return result == 42 ? 0 : 1;
+}
diff --git a/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.cpp b/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.cpp
index f179b9c6b8781..6ae74d290d715 100644
--- a/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.cpp
+++ b/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.cpp
@@ -13,6 +13,8 @@
 #include "lldb/Host/ProcessLaunchInfo.h"
 #include "llvm/Support/Error.h"
 
+#include <cstring>
+
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::lldb_server;
@@ -62,8 +64,18 @@ Status ProcessMockAccelerator::Kill() { return Status(); }
 Status ProcessMockAccelerator::ReadMemory(const ProcessAddress &process_addr,
                                           void *buf, size_t size,
                                           size_t &bytes_read) {
-  bytes_read = 0;
-  return Status::FromErrorString("unimplemented");
+  // No real memory to read, so synthesize bytes that identify the address the
+  // request arrived with: byte 0 is the address space and byte 1 is the thread
+  // (zero when the read was not thread specific). That lets a test tell reads
+  // of the same numeric address apart.
+  uint8_t *bytes = static_cast<uint8_t *>(buf);
+  std::memset(bytes, 0, size);
+  if (size > 0)
+    bytes[0] = static_cast<uint8_t>(process_addr.GetAddressSpace());
+  if (size > 1)
+    bytes[1] = static_cast<uint8_t>(process_addr.GetThreadID().value_or(0));
+  bytes_read = size;
+  return Status();
 }
 
 Status ProcessMockAccelerator::DoWriteMemory(lldb::addr_t addr, const void *buf,
@@ -112,3 +124,8 @@ ProcessMockAccelerator::GetFileLoadAddress(const llvm::StringRef &file_name,
                                            lldb::addr_t &load_addr) {
   return Status::FromErrorString("unimplemented");
 }
+
+std::vector<AddressSpaceInfo> ProcessMockAccelerator::GetAddressSpaces() {
+  return {{"global", 1, /*is_thread_specific=*/false},
+          {"local", 2, /*is_thread_specific=*/true}};
+}
diff --git a/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.h b/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.h
index 7245e4aeca5d5..5a61aaff2fc7d 100644
--- a/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.h
+++ b/lldb/tools/lldb-server/Plugins/Accelerator/Mock/ProcessMockAccelerator.h
@@ -30,6 +30,10 @@ class ProcessMockAccelerator : public NativeProcessProtocol {
 
     llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
     Attach(lldb::pid_t pid, NativeDelegate &native_delegate) override;
+
+    Extension GetSupportedExtensions() const override {
+      return Extension::address_spaces;
+    }
   };
 
   ProcessMockAccelerator(lldb::pid_t pid, NativeDelegate &delegate);
@@ -60,6 +64,10 @@ class ProcessMockAccelerator : public NativeProcessProtocol {
   Status GetFileLoadAddress(const llvm::StringRef &file_name,
                             lldb::addr_t &load_addr) override;
 
+  /// Two fixed spaces, so the server side of "jAddressSpacesInfo" and the
+  /// "address-spaces+" feature have something to report.
+  std::vector<AddressSpaceInfo> GetAddressSpaces() override;
+
 private:
   mutable ArchSpec m_arch;
 };



More information about the lldb-commits mailing list