[Lldb-commits] [lldb] [lldb] Add ScriptedSymbolLocator plugin for source file resolution (PR #181334)

Med Ismail Bennani via lldb-commits lldb-commits at lists.llvm.org
Fri Feb 13 08:09:41 PST 2026


================
@@ -0,0 +1,156 @@
+"""
+Test the ScriptedSymbolLocator plugin for source file resolution.
+"""
+
+import os
+import shutil
+import tempfile
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class ScriptedSymbolLocatorTestCase(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def setUp(self):
+        TestBase.setUp(self)
+        self.main_source_file = lldb.SBFileSpec("main.c")
+
+    def import_locator(self):
+        self.runCmd(
+            "command script import "
+            + os.path.join(self.getSourceDir(), "source_locator.py")
+        )
+
+    def set_locator_class(self, class_name):
+        self.runCmd(
+            "settings set plugin.symbol-locator.scripted.script-class " + class_name
+        )
+
+    def clear_locator_class(self):
+        self.runCmd('settings set plugin.symbol-locator.scripted.script-class ""')
+
+    def script(self, expr):
+        """Execute a Python expression in LLDB's script interpreter and return
+        the result as a string."""
+        ret = lldb.SBCommandReturnObject()
+        self.dbg.GetCommandInterpreter().HandleCommand("script " + expr, ret)
+        return ret.GetOutput().strip() if ret.Succeeded() else ""
+
+    @skipUnlessPlatform(["linux", "freebsd"])
+    def test_locate_source_file(self):
+        """Test that the scripted locator resolves source files and receives
+        an SBModule with a valid UUID."""
+        self.build()
+
+        # Copy main.c to a temp directory so the locator can "resolve" to it.
+        tmp_dir = tempfile.mkdtemp()
+        self.addTearDownHook(lambda: shutil.rmtree(tmp_dir))
+        shutil.copy(os.path.join(self.getSourceDir(), "main.c"), tmp_dir)
+
+        # Create the target BEFORE setting the script class, so module loading
+        # (which may run on worker threads) does not trigger the Python locator.
+        target = self.dbg.CreateTarget(self.getBuildArtifact("a.out"))
+        self.assertTrue(target and target.IsValid(), VALID_TARGET)
+
+        # Now set up the scripted locator. LocateSourceFile will only be called
+        # from the main thread when we access a frame's line entry.
+        self.import_locator()
+        self.script("source_locator.SourceLocator.resolved_dir = '%s'" % tmp_dir)
----------------
medismailben wrote:

This works in your python test but it won't work for the user. That's why I'd rather not have it be set as a catch-all setting and rather have it be registered with a command or API.

https://github.com/llvm/llvm-project/pull/181334


More information about the lldb-commits mailing list