https://github.com/GeorgeHuyubo created 
https://github.com/llvm/llvm-project/pull/176266

During target creation for launch mode, the locate module callback was not 
invoked for the main executable because the Target doesn't exist yet when 
ModuleList::GetSharedModule is called. This prevented custom module location 
logic from being applied to the main executable.

This change adds a PlatformSP to ModuleSpec so that GetSharedModule can call 
the locate module callback using the Platform directly, even when Target is not 
available. The Platform is now set on the ModuleSpec in 
TargetList::CreateTargetInternal before calling ResolveExecutable.


>From 6fc684d06d0d59c09130871759b4b7789da75085 Mon Sep 17 00:00:00 2001
From: George Hu <[email protected]>
Date: Thu, 15 Jan 2026 15:01:33 -0800
Subject: [PATCH] [lldb] Enable locate module callback for main executable in
 launch mode

---
 lldb/include/lldb/Core/ModuleSpec.h | 15 +++++++++++++++
 lldb/source/Core/ModuleList.cpp     | 30 ++++++++++++++++++-----------
 lldb/source/Target/TargetList.cpp   |  4 ++++
 3 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/lldb/include/lldb/Core/ModuleSpec.h 
b/lldb/include/lldb/Core/ModuleSpec.h
index acbc85b48f02c..1ce857c9765cb 100644
--- a/lldb/include/lldb/Core/ModuleSpec.h
+++ b/lldb/include/lldb/Core/ModuleSpec.h
@@ -138,6 +138,17 @@ class ModuleSpec {
   /// the specified module.
   void SetTarget(std::shared_ptr<Target> target) { m_target_wp = target; }
 
+  lldb::PlatformSP GetPlatformSP() const { return m_platform_wp.lock(); }
+
+  /// Set the platform to be used when resolving a module.
+  ///
+  /// This is useful when a Target is not yet available (e.g., during target
+  /// creation) but a Platform is. The platform can be used to invoke locate
+  /// module callbacks and other platform-specific module resolution logic.
+  void SetPlatform(std::shared_ptr<Platform> platform) {
+    m_platform_wp = platform;
+  }
+
   void Clear() {
     m_file.Clear();
     m_platform_file.Clear();
@@ -150,6 +161,7 @@ class ModuleSpec {
     m_source_mappings.Clear(false);
     m_object_mod_time = llvm::sys::TimePoint<>();
     m_target_wp.reset();
+    m_platform_wp.reset();
   }
 
   explicit operator bool() const {
@@ -283,6 +295,9 @@ class ModuleSpec {
   /// debug info search paths, can be essential. The target's platform can also
   /// be used to locate or download the specified module.
   std::weak_ptr<Target> m_target_wp;
+  /// The platform used when resolving a module. This is useful when a Target
+  /// is not yet available (e.g., during target creation) but a Platform is.
+  std::weak_ptr<Platform> m_platform_wp;
   uint64_t m_object_offset = 0;
   uint64_t m_object_size = 0;
   llvm::sys::TimePoint<> m_object_mod_time;
diff --git a/lldb/source/Core/ModuleList.cpp b/lldb/source/Core/ModuleList.cpp
index be6ff723e0ffa..d811bc6c67bb5 100644
--- a/lldb/source/Core/ModuleList.cpp
+++ b/lldb/source/Core/ModuleList.cpp
@@ -1103,18 +1103,26 @@ ModuleList::GetSharedModule(const ModuleSpec 
&module_spec, ModuleSP &module_sp,
   if (module_sp)
     return error;
 
-  // Try target's platform locate module callback before second attempt.
+  // Try platform's locate module callback before second attempt.
+  // The platform can come from either the Target (if available) or directly
+  // from the ModuleSpec (useful when Target is not yet created, e.g., during
+  // target creation for launch mode).
   if (invoke_locate_callback) {
-    TargetSP target_sp = module_spec.GetTargetSP();
-    if (target_sp && target_sp->IsValid()) {
-      if (PlatformSP platform_sp = target_sp->GetPlatform()) {
-        FileSpec symbol_file_spec;
-        platform_sp->CallLocateModuleCallbackIfSet(
-            module_spec, module_sp, symbol_file_spec, did_create_ptr);
-        if (module_sp) {
-          // The callback found a module.
-          return error;
-        }
+    PlatformSP platform_sp;
+    if (TargetSP target_sp = module_spec.GetTargetSP()) {
+      if (target_sp->IsValid())
+        platform_sp = target_sp->GetPlatform();
+    }
+    if (!platform_sp)
+      platform_sp = module_spec.GetPlatformSP();
+
+    if (platform_sp) {
+      FileSpec symbol_file_spec;
+      platform_sp->CallLocateModuleCallbackIfSet(module_spec, module_sp,
+                                                 symbol_file_spec, 
did_create_ptr);
+      if (module_sp) {
+        // The callback found a module.
+        return error;
       }
     }
   }
diff --git a/lldb/source/Target/TargetList.cpp 
b/lldb/source/Target/TargetList.cpp
index ce04e9c1209b8..623f8c4cf8324 100644
--- a/lldb/source/Target/TargetList.cpp
+++ b/lldb/source/Target/TargetList.cpp
@@ -306,6 +306,10 @@ Status TargetList::CreateTargetInternal(Debugger &debugger,
     if (platform_sp) {
       ModuleSpec module_spec(file, arch);
       module_spec.SetTarget(target_sp);
+      // Set the platform so that GetSharedModule can use it for the locate
+      // module callback, even when Target is not yet available (during target
+      // creation for launch mode).
+      module_spec.SetPlatform(platform_sp);
       error = platform_sp->ResolveExecutable(module_spec, exe_module_sp);
     }
 

_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to