[Lldb-commits] [lldb] [lldb] Use ForEachScriptedTarget in LocateSourceFile (PR #181528)

via lldb-commits lldb-commits at lists.llvm.org
Sun Feb 15 00:01:14 PST 2026


https://github.com/rchamala created https://github.com/llvm/llvm-project/pull/181528

## Summary
Replace the manual owning-target lookup in `LocateSourceFile` with `ForEachScriptedTarget`, matching the pattern used by `LocateExecutableObjectFile`, `LocateExecutableSymbolFile`, and `DownloadObjectAndSymbolFile`.

The previous implementation iterated all debuggers/targets and called `FindModule(module_sp.get())` to find the owning target. This fails for APK container modules (e.g., `VrShell.apk` on Android) that get evicted from all target image lists when their embedded `.so` files are loaded during symbol resolution, causing `locate_source_file` to never be called.

`ForEachScriptedTarget` dispatches to all targets with a registered scripted locator without requiring an owning-target lookup.

## Test plan
- [ ] Existing `TestScriptedSymbolLocator.py` tests pass
- [ ] APK container module source resolution works on Android targets

>From e1b422513ba5a84e45549b3b2164ce9291d35477 Mon Sep 17 00:00:00 2001
From: Rahul Reddy Chamala <rachamal at meta.com>
Date: Sun, 15 Feb 2026 00:00:56 -0800
Subject: [PATCH] [lldb] Use ForEachScriptedTarget in LocateSourceFile

Replace the manual owning-target lookup in LocateSourceFile with
ForEachScriptedTarget, matching the pattern used by
LocateExecutableObjectFile, LocateExecutableSymbolFile, and
DownloadObjectAndSymbolFile.

The previous implementation searched for the owning target by iterating
all debuggers/targets and calling FindModule(module_sp.get()). This
fails for APK container modules (e.g., VrShell.apk on Android) that
get evicted from all target image lists when their embedded .so files
are loaded during symbol resolution, causing locate_source_file to
never be called.

Using ForEachScriptedTarget dispatches to all targets with a registered
scripted locator without requiring an owning-target lookup.
---
 .../Scripted/SymbolLocatorScripted.cpp        | 88 ++++++++-----------
 1 file changed, 37 insertions(+), 51 deletions(-)

diff --git a/lldb/source/Plugins/SymbolLocator/Scripted/SymbolLocatorScripted.cpp b/lldb/source/Plugins/SymbolLocator/Scripted/SymbolLocatorScripted.cpp
index cfbb11582a042..f0e04ed3fb0cc 100644
--- a/lldb/source/Plugins/SymbolLocator/Scripted/SymbolLocatorScripted.cpp
+++ b/lldb/source/Plugins/SymbolLocator/Scripted/SymbolLocatorScripted.cpp
@@ -141,61 +141,47 @@ SymbolLocatorScripted::LocateSourceFile(const lldb::ModuleSP &module_sp,
   if (!module_sp)
     return {};
 
-  // Find the target that owns this module.
-  Target *owning_target = nullptr;
-  for (size_t di = 0; di < Debugger::GetNumDebuggers(); di++) {
-    DebuggerSP debugger_sp = Debugger::GetDebuggerAtIndex(di);
-    if (!debugger_sp)
-      continue;
-    TargetList &target_list = debugger_sp->GetTargetList();
-    for (size_t ti = 0; ti < target_list.GetNumTargets(); ti++) {
-      TargetSP target_sp = target_list.GetTargetAtIndex(ti);
-      if (!target_sp)
-        continue;
-      ModuleSP found_module =
-          target_sp->GetImages().FindModule(module_sp.get());
-      if (found_module) {
-        owning_target = target_sp.get();
-        break;
-      }
-    }
-    if (owning_target)
-      break;
-  }
-
-  if (!owning_target)
-    return {};
-
-  auto interface_sp = owning_target->GetScriptedSymbolLocatorInterface();
-  if (!interface_sp)
-    return {};
-
-  // Cache resolved source files to avoid repeated Python calls for the same
-  // (module, source_file) pair.
+  std::optional<FileSpec> result;
   std::string cache_key =
-      module_sp->GetUUID().GetAsString() + ":" + original_source_file.GetPath();
+      module_sp->GetUUID().GetAsString() + ":" +
+      original_source_file.GetPath();
 
-  std::optional<FileSpec> cached;
-  if (owning_target->LookupScriptedSourceFileCache(cache_key, cached))
-    return cached;
+  ForEachScriptedTarget(
+      [&](Target &target,
+          ScriptedSymbolLocatorInterfaceSP &interface_sp) -> bool {
+        // Check the per-target cache first.
+        std::optional<FileSpec> cached;
+        if (target.LookupScriptedSourceFileCache(cache_key, cached)) {
+          result = cached;
+          return result.has_value();
+        }
 
-  Status error;
-  auto located =
-      interface_sp->LocateSourceFile(module_sp, original_source_file, error);
+        Status error;
+        auto located =
+            interface_sp->LocateSourceFile(
+                module_sp, original_source_file, error);
 
-  Log *log = GetLog(LLDBLog::Symbols);
-  if (!error.Success()) {
-    LLDB_LOG(log, "SymbolLocatorScripted: locate_source_file failed: {0}",
-             error);
-  }
+        if (!error.Success()) {
+          Log *log = GetLog(LLDBLog::Symbols);
+          LLDB_LOG(log,
+                   "SymbolLocatorScripted: locate_source_file failed: {0}",
+                   error);
+        }
 
-  owning_target->InsertScriptedSourceFileCache(cache_key, located);
+        target.InsertScriptedSourceFileCache(cache_key, located);
 
-  if (located) {
-    LLDB_LOGF(log,
-              "SymbolLocatorScripted::%s: resolved source file '%s' to '%s'",
-              __FUNCTION__, original_source_file.GetPath().c_str(),
-              located->GetPath().c_str());
-  }
-  return located;
+        if (located) {
+          Log *log = GetLog(LLDBLog::Symbols);
+          LLDB_LOGF(log,
+                    "SymbolLocatorScripted::%s: resolved source file '%s' "
+                    "to '%s'",
+                    __FUNCTION__,
+                    original_source_file.GetPath().c_str(),
+                    located->GetPath().c_str());
+          result = located;
+          return true;
+        }
+        return false;
+      });
+  return result;
 }



More information about the lldb-commits mailing list