[Lldb-commits] [lldb] [lldb][PlatformDarwin] Make PlatformDarwin define a safe-path for auto-loading scripting resources (PR #191454)

Michael Buch via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 17 03:01:19 PDT 2026


https://github.com/Michael137 updated https://github.com/llvm/llvm-project/pull/191454

>From 66e971de6f69c90ab33cdbd8fc0b7feb48fcb9f7 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 10 Apr 2026 14:58:20 +0100
Subject: [PATCH 1/4] [lldb][Utility] Add FileSpecList::Append(const
 FileSpecList &) API

Adds a new API to `FileSpecList` that allows appending another `FileSpecList`.

This is used in another PR where I didn't want to iterate over the list and push_back manually.
---
 lldb/include/lldb/Utility/FileSpecList.h    |  4 ++
 lldb/unittests/Utility/FileSpecListTest.cpp | 52 +++++++++++++++++++++
 2 files changed, 56 insertions(+)

diff --git a/lldb/include/lldb/Utility/FileSpecList.h b/lldb/include/lldb/Utility/FileSpecList.h
index 69c5b49841a12..ff2db60e21598 100644
--- a/lldb/include/lldb/Utility/FileSpecList.h
+++ b/lldb/include/lldb/Utility/FileSpecList.h
@@ -132,6 +132,10 @@ class FileSpecList {
   ///     A new file to append to this file list.
   void Append(const FileSpec &file);
 
+  void Append(const FileSpecList &other) {
+    m_files.insert(end(), std::begin(other), std::end(other));
+  }
+
   /// Append a FileSpec object if unique.
   ///
   /// Appends \a file to the end of the file list if it doesn't already exist
diff --git a/lldb/unittests/Utility/FileSpecListTest.cpp b/lldb/unittests/Utility/FileSpecListTest.cpp
index d3f89ad0dfcb3..4a09d48a47fb3 100644
--- a/lldb/unittests/Utility/FileSpecListTest.cpp
+++ b/lldb/unittests/Utility/FileSpecListTest.cpp
@@ -301,6 +301,58 @@ TEST(SupportFileListTest, DifferentBasename) {
   EXPECT_EQ(ret, UINT32_MAX);
 }
 
+TEST(FileSpecListTest, AppendFileSpecList) {
+  // Test appending a FileSpecList to an existing FileSpecList.
+
+  FileSpecList list_a;
+  list_a.Append(PosixSpec("/a/foo.h"));
+  list_a.Append(PosixSpec("/a/bar.h"));
+
+  FileSpecList list_b;
+  list_b.Append(PosixSpec("/b/baz.h"));
+  list_b.Append(PosixSpec("/b/qux.h"));
+
+  // Duplicate gets appended too.
+  list_b.Append(PosixSpec("/a/foo.h"));
+
+  list_a.Append(list_b);
+  ASSERT_EQ(list_a.GetSize(), 5u);
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(0), PosixSpec("/a/foo.h"));
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(1), PosixSpec("/a/bar.h"));
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(2), PosixSpec("/b/baz.h"));
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(3), PosixSpec("/b/qux.h"));
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(4), PosixSpec("/a/foo.h"));
+}
+
+TEST(FileSpecListTest, AppendEmptyFileSpecList) {
+  // Test appending an empty FileSpecList to an existing FileSpecList.
+
+  FileSpecList list_a;
+  list_a.Append(PosixSpec("/a/foo.h"));
+
+  FileSpecList empty;
+  list_a.Append(empty);
+
+  ASSERT_EQ(list_a.GetSize(), 1u);
+  EXPECT_EQ(list_a.GetFileSpecAtIndex(0), PosixSpec("/a/foo.h"));
+}
+
+TEST(FileSpecListTest, AppendToEmptyFileSpecList) {
+  // Test appending to an empty FileSpecList to an existing FileSpecList.
+
+  FileSpecList list_a;
+  FileSpecList list_b;
+  list_b.Append(list_a);
+
+  ASSERT_EQ(list_b.GetSize(), 0u);
+
+  list_a.Append(PosixSpec("/a/foo.h"));
+  list_b.Append(list_a);
+
+  ASSERT_EQ(list_b.GetSize(), 1u);
+  EXPECT_EQ(list_b.GetFileSpecAtIndex(0), PosixSpec("/a/foo.h"));
+}
+
 // No prefixes are configured.
 // The support file and the breakpoint file are different.
 // Should find it incompatible.

>From 36e6f181a47ba4bd225cd23613ed08d54ff57830 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 27 Mar 2026 12:02:01 +0000
Subject: [PATCH 2/4] [lldb][Platform] Allow Platform to define safe-paths for
 auto-loading scripting resources

Depends on:
* https://github.com/llvm/llvm-project/pull/191446

Assisted-by: Claude
- Used Claude to write the skeleton of the test before manually cleaning it up.
---
 lldb/include/lldb/Core/Debugger.h             |  2 +-
 lldb/include/lldb/Target/Platform.h           |  4 +++
 lldb/source/Core/Debugger.cpp                 | 13 ++++++++
 .../Host/macosx/objcxx/HostInfoMacOSX.mm      |  1 +
 .../Platform/MacOSX/PlatformDarwin.cpp        | 32 +++++++++++++++++++
 .../Plugins/Platform/MacOSX/PlatformDarwin.h  |  2 ++
 lldb/source/Target/Platform.cpp               |  2 +-
 lldb/unittests/Platform/CMakeLists.txt        |  1 +
 .../unittests/Platform/PlatformDarwinTest.cpp | 16 ++++++++++
 9 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h
index 5f1f598a051a2..1b821b2a09e24 100644
--- a/lldb/include/lldb/Core/Debugger.h
+++ b/lldb/include/lldb/Core/Debugger.h
@@ -149,7 +149,7 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
   /// scripting resources from. Currently whether to load scripts
   /// unconditionally is controlled via the
   /// `target.load-script-from-symbol-file` setting.
-  static FileSpecList GetSafeAutoLoadPaths();
+  FileSpecList GetSafeAutoLoadPaths();
 
   void Clear();
 
diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h
index 001ff7e112909..920684855681f 100644
--- a/lldb/include/lldb/Target/Platform.h
+++ b/lldb/include/lldb/Target/Platform.h
@@ -1009,6 +1009,10 @@ class Platform : public PluginInterface {
 
   LocateModuleCallback GetLocateModuleCallback() const;
 
+  /// Returns a \c FileSpecList of safe paths to auto-load scripting resources
+  /// from for a particular platform.
+  virtual llvm::Expected<FileSpecList> GetSafeAutoLoadPaths(const Target &target) { return FileSpecList(); }
+
 protected:
   /// Create a list of ArchSpecs with the given OS and a architectures. The
   /// vendor field is left as an "unspecified unknown".
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 9ea298d07804b..2f7421588a8aa 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -2605,6 +2605,19 @@ StructuredData::DictionarySP Debugger::GetBuildConfiguration() {
 FileSpecList Debugger::GetSafeAutoLoadPaths() {
   FileSpecList fspecs = GetDefaultSafeAutoLoadPaths();
 
+  // Add platform-specific safe-paths.
+  if (TargetSP target_sp = GetSelectedTarget()) {
+    if (PlatformSP platform_sp = GetPlatformList().GetSelectedPlatform()) {
+      if (auto platform_fspecs_or_err = platform_sp->GetSafeAutoLoadPaths(*target_sp))
+        fspecs.Append(*platform_fspecs_or_err);
+      else
+        LLDB_LOG_ERROR(
+            GetLog(LLDBLog::Modules | LLDBLog::Platform), platform_fspecs_or_err.takeError(),
+            "Skipping safe auto-load path: {0}");
+    }
+  }
+
+  // Properties for testing get added last so they take priority.
 #ifndef NDEBUG
   for (const auto &fspec :
        TestingProperties::GetGlobalTestingProperties().GetSafeAutoLoadPaths())
diff --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
index 2214678d392b5..9f1335ee2946d 100644
--- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
+++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm
@@ -14,6 +14,7 @@
 #include "lldb/Utility/ConstString.h"
 #include "lldb/Utility/DataBuffer.h"
 #include "lldb/Utility/DataExtractor.h"
+#include "lldb/Utility/FileSpecList.h"
 #include "lldb/Utility/LLDBLog.h"
 #include "lldb/Utility/Log.h"
 #include "lldb/Utility/Timer.h"
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index e2d8670a6acae..bbd29aa607181 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1564,3 +1564,35 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
 
   return path_or_err->str();
 }
+
+llvm::Expected<FileSpecList> PlatformDarwin::GetSafeAutoLoadPaths(const Target &target) {
+  Log *log = GetLog(LLDBLog::Modules | LLDBLog::Platform);
+
+  XcodeSDK::Type sdk_type =
+      XcodeSDK::GetSDKTypeForTriple(target.GetArchitecture().GetTriple());
+  XcodeSDK sdk(XcodeSDK::Info{sdk_type, {}});
+
+  auto sdk_root_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
+  if (!sdk_root_or_err) {
+    LLDB_LOG_ERROR(log, sdk_root_or_err.takeError(),
+                   "Failed to resolve SDK root for triple '{1}': {0}",
+                   target.GetArchitecture().GetTriple().str());
+
+    // Fall back to any macOS SDK.
+    sdk = XcodeSDK::GetAnyMacOS();
+    LLDB_LOG(log, "Falling back to SDK '{0}'", sdk.GetString());
+    sdk_root_or_err = HostInfo::GetSDKRoot(HostInfo::SDKOptions{sdk});
+  }
+
+  if (!sdk_root_or_err)
+    return sdk_root_or_err.takeError();
+
+  // $SDKROOT/usr/share/lldb is an auto-loadable path.
+  llvm::SmallString<256> resolved(*sdk_root_or_err);
+  llvm::sys::path::append(resolved, "usr", "share", "lldb");
+
+  FileSpecList fspecs;
+  fspecs.Append(FileSpec(resolved));
+
+  return fspecs;
+}
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
index fd5207e82b6db..9e446d65ea29a 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
@@ -159,6 +159,8 @@ class PlatformDarwin : public PlatformPOSIX {
                                              const Target &target,
                                              const FileSpec &symfile_spec);
 
+  llvm::Expected<FileSpecList> GetSafeAutoLoadPaths(const Target &target) override;
+
 protected:
   static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx);
 
diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index eaf461392d669..20c23b03075f7 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -190,7 +190,7 @@ Platform::LocateExecutableScriptingResourcesFromSafePaths(
           ->GetSanitizedScriptingModuleName(
               module_spec.GetFileNameStrippingExtension().GetStringRef());
 
-  FileSpecList paths = Debugger::GetSafeAutoLoadPaths();
+  FileSpecList paths = target.GetDebugger().GetSafeAutoLoadPaths();
 
   // Iterate in reverse so we consider the latest appended path first.
   for (FileSpec path : llvm::reverse(paths)) {
diff --git a/lldb/unittests/Platform/CMakeLists.txt b/lldb/unittests/Platform/CMakeLists.txt
index f8755432bf6d7..b3c87b33527bd 100644
--- a/lldb/unittests/Platform/CMakeLists.txt
+++ b/lldb/unittests/Platform/CMakeLists.txt
@@ -14,6 +14,7 @@ add_lldb_unittest(LLDBPlatformTests
     lldbPluginPlatformMacOSX
     lldbPluginPlatformNetBSD
     lldbUtilityHelpers
+    LLVMTestingSupport
   )
 
 add_subdirectory(Android)
diff --git a/lldb/unittests/Platform/PlatformDarwinTest.cpp b/lldb/unittests/Platform/PlatformDarwinTest.cpp
index 16920c13b46a5..68902f1a2065d 100644
--- a/lldb/unittests/Platform/PlatformDarwinTest.cpp
+++ b/lldb/unittests/Platform/PlatformDarwinTest.cpp
@@ -22,6 +22,7 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Testing/Support/Error.h"
 
 #include <memory>
 #include <tuple>
@@ -767,3 +768,18 @@ INSTANTIATE_TEST_SUITE_P(PlatformDarwinLocateWithSpecialCharsTest,
                          PlatformDarwinLocateWithSpecialCharsTestFixture,
                          testing::ValuesIn(std::vector<SpecialCharTestCase>{
                              {' ', '_'}, {'.', '_'}, {'-', '_'}, {'+', 'x'}}));
+
+TEST_F(PlatformDarwinLocateTest, GetSafeAutoLoadPaths) {
+  // Tests PlatformDarwin::GetSafeAutoLoadPaths returns a path into the SDK on Darwin platforms.
+
+  auto paths_or_err = std::static_pointer_cast<PlatformDarwin>(m_platform_sp)->GetSafeAutoLoadPaths(*m_target_sp);
+
+  ASSERT_THAT_EXPECTED(paths_or_err, llvm::Succeeded());
+
+  ASSERT_EQ(paths_or_err->GetSize(), 1u);
+
+  // The returned path should be $SDKROOT/usr/share/lldb.
+  FileSpec path = paths_or_err->GetFileSpecAtIndex(0);
+  EXPECT_TRUE(llvm::StringRef(path.GetPath()).ends_with("/usr/share/lldb"))
+      << "Unexpected path: " << path.GetPath();
+}

>From 64c0ada35ba14d2bf46759afdf55b7f0e8c07d3b Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 10 Apr 2026 17:37:45 +0100
Subject: [PATCH 3/4] fixup! clang-format

---
 lldb/include/lldb/Target/Platform.h                    | 5 ++++-
 lldb/source/Core/Debugger.cpp                          | 9 +++++----
 lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp | 3 ++-
 lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h   | 3 ++-
 lldb/unittests/Platform/PlatformDarwinTest.cpp         | 6 ++++--
 5 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h
index 920684855681f..7f56b83ad75b5 100644
--- a/lldb/include/lldb/Target/Platform.h
+++ b/lldb/include/lldb/Target/Platform.h
@@ -1011,7 +1011,10 @@ class Platform : public PluginInterface {
 
   /// Returns a \c FileSpecList of safe paths to auto-load scripting resources
   /// from for a particular platform.
-  virtual llvm::Expected<FileSpecList> GetSafeAutoLoadPaths(const Target &target) { return FileSpecList(); }
+  virtual llvm::Expected<FileSpecList>
+  GetSafeAutoLoadPaths(const Target &target) {
+    return FileSpecList();
+  }
 
 protected:
   /// Create a list of ArchSpecs with the given OS and a architectures. The
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index 2f7421588a8aa..b6a5edbb77bda 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -2608,12 +2608,13 @@ FileSpecList Debugger::GetSafeAutoLoadPaths() {
   // Add platform-specific safe-paths.
   if (TargetSP target_sp = GetSelectedTarget()) {
     if (PlatformSP platform_sp = GetPlatformList().GetSelectedPlatform()) {
-      if (auto platform_fspecs_or_err = platform_sp->GetSafeAutoLoadPaths(*target_sp))
+      if (auto platform_fspecs_or_err =
+              platform_sp->GetSafeAutoLoadPaths(*target_sp))
         fspecs.Append(*platform_fspecs_or_err);
       else
-        LLDB_LOG_ERROR(
-            GetLog(LLDBLog::Modules | LLDBLog::Platform), platform_fspecs_or_err.takeError(),
-            "Skipping safe auto-load path: {0}");
+        LLDB_LOG_ERROR(GetLog(LLDBLog::Modules | LLDBLog::Platform),
+                       platform_fspecs_or_err.takeError(),
+                       "Skipping safe auto-load path: {0}");
     }
   }
 
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index bbd29aa607181..79686b07e6345 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1565,7 +1565,8 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
   return path_or_err->str();
 }
 
-llvm::Expected<FileSpecList> PlatformDarwin::GetSafeAutoLoadPaths(const Target &target) {
+llvm::Expected<FileSpecList>
+PlatformDarwin::GetSafeAutoLoadPaths(const Target &target) {
   Log *log = GetLog(LLDBLog::Modules | LLDBLog::Platform);
 
   XcodeSDK::Type sdk_type =
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
index 9e446d65ea29a..c4aa840f74887 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
@@ -159,7 +159,8 @@ class PlatformDarwin : public PlatformPOSIX {
                                              const Target &target,
                                              const FileSpec &symfile_spec);
 
-  llvm::Expected<FileSpecList> GetSafeAutoLoadPaths(const Target &target) override;
+  llvm::Expected<FileSpecList>
+  GetSafeAutoLoadPaths(const Target &target) override;
 
 protected:
   static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx);
diff --git a/lldb/unittests/Platform/PlatformDarwinTest.cpp b/lldb/unittests/Platform/PlatformDarwinTest.cpp
index 68902f1a2065d..04af8081a274e 100644
--- a/lldb/unittests/Platform/PlatformDarwinTest.cpp
+++ b/lldb/unittests/Platform/PlatformDarwinTest.cpp
@@ -770,9 +770,11 @@ INSTANTIATE_TEST_SUITE_P(PlatformDarwinLocateWithSpecialCharsTest,
                              {' ', '_'}, {'.', '_'}, {'-', '_'}, {'+', 'x'}}));
 
 TEST_F(PlatformDarwinLocateTest, GetSafeAutoLoadPaths) {
-  // Tests PlatformDarwin::GetSafeAutoLoadPaths returns a path into the SDK on Darwin platforms.
+  // Tests PlatformDarwin::GetSafeAutoLoadPaths returns a path into the SDK on
+  // Darwin platforms.
 
-  auto paths_or_err = std::static_pointer_cast<PlatformDarwin>(m_platform_sp)->GetSafeAutoLoadPaths(*m_target_sp);
+  auto paths_or_err = std::static_pointer_cast<PlatformDarwin>(m_platform_sp)
+                          ->GetSafeAutoLoadPaths(*m_target_sp);
 
   ASSERT_THAT_EXPECTED(paths_or_err, llvm::Succeeded());
 

>From 09fda5aa60df59ef3204955b426fa38133eb8fa6 Mon Sep 17 00:00:00 2001
From: Michael Buch <michaelbuch12 at gmail.com>
Date: Fri, 17 Apr 2026 11:01:00 +0100
Subject: [PATCH 4/4] fixup! move getters into Target

---
 lldb/include/lldb/Core/Debugger.h             | 11 +---
 lldb/include/lldb/Target/Platform.h           |  2 +-
 lldb/include/lldb/Target/Target.h             |  6 ++
 lldb/source/Core/Debugger.cpp                 | 58 +++++--------------
 .../Platform/MacOSX/PlatformDarwin.cpp        |  2 +-
 .../Plugins/Platform/MacOSX/PlatformDarwin.h  |  2 +-
 lldb/source/Target/Platform.cpp               |  2 +-
 lldb/source/Target/Target.cpp                 | 24 ++++++++
 8 files changed, 53 insertions(+), 54 deletions(-)

diff --git a/lldb/include/lldb/Core/Debugger.h b/lldb/include/lldb/Core/Debugger.h
index 1b821b2a09e24..ce6964231f764 100644
--- a/lldb/include/lldb/Core/Debugger.h
+++ b/lldb/include/lldb/Core/Debugger.h
@@ -69,7 +69,6 @@ class Process;
 class Stream;
 class SymbolContext;
 class Target;
-class Debugger;
 
 #ifndef NDEBUG
 /// Global properties used in the LLDB testsuite.
@@ -85,7 +84,7 @@ struct TestingProperties : public Properties {
   void AppendSafeAutoLoadPaths(FileSpec path);
 
 private:
-  friend Debugger;
+  friend Target;
 
   /// Callers should use Debugger::GetSafeAutoLoadPaths since it
   /// accounts for default paths configured via CMake.
@@ -145,12 +144,6 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
   static void AssertCallback(llvm::StringRef message, llvm::StringRef backtrace,
                              llvm::StringRef prompt);
 
-  /// Get the list of paths that LLDB will consider automatically loading
-  /// scripting resources from. Currently whether to load scripts
-  /// unconditionally is controlled via the
-  /// `target.load-script-from-symbol-file` setting.
-  FileSpecList GetSafeAutoLoadPaths();
-
   void Clear();
 
   void DispatchClientTelemetry(const lldb_private::StructuredDataImpl &entry);
@@ -655,6 +648,8 @@ class Debugger : public std::enable_shared_from_this<Debugger>,
   };
   std::optional<ProgressReport> GetCurrentProgressReport() const;
 
+  static const FileSpecList &GetDefaultSafeAutoLoadPaths();
+
 protected:
   friend class CommandInterpreter;
   friend class REPL;
diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h
index 7f56b83ad75b5..4db1cd328bc08 100644
--- a/lldb/include/lldb/Target/Platform.h
+++ b/lldb/include/lldb/Target/Platform.h
@@ -1012,7 +1012,7 @@ class Platform : public PluginInterface {
   /// Returns a \c FileSpecList of safe paths to auto-load scripting resources
   /// from for a particular platform.
   virtual llvm::Expected<FileSpecList>
-  GetSafeAutoLoadPaths(const Target &target) {
+  GetSafeAutoLoadPaths(const Target &target) const {
     return FileSpecList();
   }
 
diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h
index 67f373aa5a325..ac214ad2da6df 100644
--- a/lldb/include/lldb/Target/Target.h
+++ b/lldb/include/lldb/Target/Target.h
@@ -1888,6 +1888,12 @@ class Target : public std::enable_shared_from_this<Target>,
 
   TargetStats &GetStatistics() { return m_stats; }
 
+  /// Get the list of paths that LLDB will consider automatically loading
+  /// scripting resources from. Currently whether to load scripts
+  /// unconditionally is controlled via the
+  /// `target.load-script-from-symbol-file` setting.
+  FileSpecList GetSafeAutoLoadPaths() const;
+
 protected:
   /// Construct with optional file and arch.
   ///
diff --git a/lldb/source/Core/Debugger.cpp b/lldb/source/Core/Debugger.cpp
index b6a5edbb77bda..3acde9340b037 100644
--- a/lldb/source/Core/Debugger.cpp
+++ b/lldb/source/Core/Debugger.cpp
@@ -213,27 +213,6 @@ enum {
 };
 #endif
 
-static const FileSpecList &GetDefaultSafeAutoLoadPaths() {
-  static const FileSpecList sSafePaths = [] {
-    // FIXME: in c++20 this could be a std::array (with CTAD deduced size)
-    // and we could statically assert that all members are non-empty.
-    const llvm::SmallVector<llvm::StringRef> kVendorSafePaths = {
-#include "SafeAutoloadPaths.inc"
-    };
-    FileSpecList fspecs;
-    for (auto path : kVendorSafePaths) {
-      assert(!path.empty());
-      LLDB_LOG(GetLog(SystemLog::System), "Safe auto-load path configured: {0}",
-               path);
-      fspecs.EmplaceBack(path);
-    }
-
-    return fspecs;
-  }();
-
-  return sSafePaths;
-}
-
 #ifndef NDEBUG
 TestingProperties::TestingProperties() {
   m_collection_sp = std::make_shared<OptionValueProperties>("testing");
@@ -2602,28 +2581,23 @@ StructuredData::DictionarySP Debugger::GetBuildConfiguration() {
   return config_up;
 }
 
-FileSpecList Debugger::GetSafeAutoLoadPaths() {
-  FileSpecList fspecs = GetDefaultSafeAutoLoadPaths();
-
-  // Add platform-specific safe-paths.
-  if (TargetSP target_sp = GetSelectedTarget()) {
-    if (PlatformSP platform_sp = GetPlatformList().GetSelectedPlatform()) {
-      if (auto platform_fspecs_or_err =
-              platform_sp->GetSafeAutoLoadPaths(*target_sp))
-        fspecs.Append(*platform_fspecs_or_err);
-      else
-        LLDB_LOG_ERROR(GetLog(LLDBLog::Modules | LLDBLog::Platform),
-                       platform_fspecs_or_err.takeError(),
-                       "Skipping safe auto-load path: {0}");
+const FileSpecList &Debugger::GetDefaultSafeAutoLoadPaths() {
+  static const FileSpecList sSafePaths = [] {
+    // FIXME: in c++20 this could be a std::array (with CTAD deduced size)
+    // and we could statically assert that all members are non-empty.
+    const llvm::SmallVector<llvm::StringRef> kVendorSafePaths = {
+#include "SafeAutoloadPaths.inc"
+    };
+    FileSpecList fspecs;
+    for (auto path : kVendorSafePaths) {
+      assert(!path.empty());
+      LLDB_LOG(GetLog(SystemLog::System), "Safe auto-load path configured: {0}",
+               path);
+      fspecs.EmplaceBack(path);
     }
-  }
 
-  // Properties for testing get added last so they take priority.
-#ifndef NDEBUG
-  for (const auto &fspec :
-       TestingProperties::GetGlobalTestingProperties().GetSafeAutoLoadPaths())
-    fspecs.Append(fspec);
-#endif
+    return fspecs;
+  }();
 
-  return fspecs;
+  return sSafePaths;
 }
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index 79686b07e6345..6e1a359ffe595 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -1566,7 +1566,7 @@ PlatformDarwin::ResolveSDKPathFromDebugInfo(CompileUnit &unit) {
 }
 
 llvm::Expected<FileSpecList>
-PlatformDarwin::GetSafeAutoLoadPaths(const Target &target) {
+PlatformDarwin::GetSafeAutoLoadPaths(const Target &target) const {
   Log *log = GetLog(LLDBLog::Modules | LLDBLog::Platform);
 
   XcodeSDK::Type sdk_type =
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
index c4aa840f74887..f3c176f09cb68 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.h
@@ -160,7 +160,7 @@ class PlatformDarwin : public PlatformPOSIX {
                                              const FileSpec &symfile_spec);
 
   llvm::Expected<FileSpecList>
-  GetSafeAutoLoadPaths(const Target &target) override;
+  GetSafeAutoLoadPaths(const Target &target) const override;
 
 protected:
   static const char *GetCompatibleArch(ArchSpec::Core core, size_t idx);
diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index 20c23b03075f7..4786674c6ec36 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -190,7 +190,7 @@ Platform::LocateExecutableScriptingResourcesFromSafePaths(
           ->GetSanitizedScriptingModuleName(
               module_spec.GetFileNameStrippingExtension().GetStringRef());
 
-  FileSpecList paths = target.GetDebugger().GetSafeAutoLoadPaths();
+  FileSpecList paths = target.GetSafeAutoLoadPaths();
 
   // Iterate in reverse so we consider the latest appended path first.
   for (FileSpec path : llvm::reverse(paths)) {
diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp
index cad86bf956e38..5b307be89bcb9 100644
--- a/lldb/source/Target/Target.cpp
+++ b/lldb/source/Target/Target.cpp
@@ -5431,6 +5431,30 @@ void Target::NotifyBreakpointChanged(
     BroadcastEvent(Target::eBroadcastBitBreakpointChanged, breakpoint_data_sp);
 }
 
+FileSpecList Target::GetSafeAutoLoadPaths() const {
+  FileSpecList fspecs = Debugger::GetDefaultSafeAutoLoadPaths();
+
+  // Add platform-specific safe-paths.
+  if (m_platform_sp) {
+    if (auto platform_fspecs_or_err =
+            m_platform_sp->GetSafeAutoLoadPaths(*this))
+      fspecs.Append(*platform_fspecs_or_err);
+    else
+      LLDB_LOG_ERROR(GetLog(LLDBLog::Modules | LLDBLog::Platform),
+                     platform_fspecs_or_err.takeError(),
+                     "Skipping safe auto-load path: {0}");
+  }
+
+  // Properties for testing get added last so they take priority.
+#ifndef NDEBUG
+  for (const auto &fspec :
+       TestingProperties::GetGlobalTestingProperties().GetSafeAutoLoadPaths())
+    fspecs.Append(fspec);
+#endif
+
+  return fspecs;
+}
+
 // FIXME: the language plugin should expression options dynamically and
 // we should validate here (by asking the language plugin) that the options
 // being set/retrieved are actually valid options.



More information about the lldb-commits mailing list