[llvm-branch-commits] [lldb] 8461e2a - [lldb] Don't cache symtabs for memory modules w/ corefile (#237050)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 01:52:00 PST 2026


Author: Jason Molenda
Date: 2026-11-10T19:38:31-08:00
New Revision: 8461e2a8cafbf543d6b3fdb3eb69a8139103c1b0

URL: https://github.com/llvm/llvm-project/commit/8461e2a8cafbf543d6b3fdb3eb69a8139103c1b0
DIFF: https://github.com/llvm/llvm-project/commit/8461e2a8cafbf543d6b3fdb3eb69a8139103c1b0.diff

LOG: [lldb] Don't cache symtabs for memory modules w/ corefile (#237050)

In https://github.com/llvm/llvm-project/pull/229305 I added a new
setting, `symbols.enable-lldb-index-cache-memory-modules` which creates
a DataFileCache symtab file for memory-read modules. Reading the symbol
table out of memory is a serious performance issue, and caching those
tables on the debug host is worth doing by default.

This is not the correct tradeoff if we are doing corefile debugging,
where reading memory is cheap. In that case we need to ignore the
`symbols.enable-lldb-index-cache-memory-modules` setting and not create
the symtabs, unless the user has set
`symbols.enable-lldb-index-cache` themselves.

This patchset is a little tricky because the layers where we can make
this judgement (DynamicLoader or Process) is pretty far away from the
place where we create the Symtab, `ObjectFile::GetSymtab` and there is
another scenario I want to override the default for in the future that
is only knowable to the DynamicLoader (if the DynamicLoader believes ALL
binaries are being read from memory, which indicates an error of the
target device setup, then we will avoid creating symtab caches for all
binaries, expecting the user to set up the target device properly. A
future PR).

The DynamicLoader methods that call `Process::ReadModuleFromMemory` are
the place where this decision is made. This argument is passed through
`Module::GetMemoryObjectFile`, then through `ObjectFile::FindPlugin`.
All ObjectFile memory-module subclass methods receive the new override
parameter, and set an ObjectFile ivar. When ObjectFile creates a Symtab
in ObjectFile::GetSymtab, it passes that setting through Symtab. Which
is, in turn, passed to `Module::GetIndexCache()` which has the logic
that decides whether to use the DataFileCache or not, based on whether
this is a memory module, whether we are (now) overriding memory-module
caching, and the user lldb-index settings.

The PR is 95% all mechanical passing around of the new argument; the
DynamicLoader methods calling `Process::ReadModuleFromMemory` and
`Module::GetIndexCache` have the logic of interest, everything else is
trivial and obvious.

There is a new test case that creates a corefile with a binary, clears
lldb's global module cache store, removes the binary from disk (so lldb
must read it from corefile memory), then loads the corefile and confirms
that a DataFileCache symtab was not created for the binary.

rdar://190698277

Assisted-by: Claude

Added: 
    lldb/test/API/functionalities/module_cache/corefile_memory_module/Makefile
    lldb/test/API/functionalities/module_cache/corefile_memory_module/TestModuleCacheCorefileMemoryModule.py
    lldb/test/API/functionalities/module_cache/corefile_memory_module/main.cpp

Modified: 
    lldb/include/lldb/Core/Module.h
    lldb/include/lldb/Symbol/ObjectFile.h
    lldb/source/Core/Module.cpp
    lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
    lldb/source/Symbol/ObjectFile.cpp
    lldb/source/Symbol/Symtab.cpp
    lldb/source/Target/Process.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Core/Module.h b/lldb/include/lldb/Core/Module.h
index 0525a0ea9b829b..266ea3e522b547 100644
--- a/lldb/include/lldb/Core/Module.h
+++ b/lldb/include/lldb/Core/Module.h
@@ -596,11 +596,16 @@ class Module : public std::enable_shared_from_this<Module>,
   /// enable the ObjectFile plugins to read the header of the object file
   /// without going back to the process.
   ///
+  /// If \a disable_memory_symtab_caching is true, the symbol table for the
+  /// object file will not be loaded from or saved to the index cache, even if
+  /// caching of memory modules is enabled.
+  ///
   /// \return
   ///     The object file loaded from memory or nullptr, if the operation
   ///     failed (see the `error` for more information in that case).
   ObjectFile *GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
                                   lldb::addr_t header_addr, Status &error,
+                                  bool disable_memory_symtab_caching,
                                   size_t size_to_read = 512);
 
   /// Get the module's symbol file
@@ -1044,20 +1049,32 @@ class Module : public std::enable_shared_from_this<Module>,
   /// 
diff erentiate these 
diff erent files.
   std::string GetCacheKey();
 
+  enum LLDBIndexType {
+    eLLDBIndexTypeSymtab,
+    eLLDBIndexTypeDwarfIndex,
+    eLLDBIndexTypeMemoryModuleSymtab
+  };
+
   /// Get the global index file cache.
   ///
   /// LLDB can cache data for a module between runs. This cache directory can be
   /// used to stored data that previously was manually created each time you
-  /// debug. Examples include debug information indexes, symbol tables, symbol
-  /// table indexes, and more.
+  /// debug. Examples include debug information indexes, symbol tables, memory
+  /// module headerbytes, and more.
+  ///
+  /// The argument provided indicates the type of file that is being
+  /// read/written to in DataFileCache, and the user's settings for that
+  /// type of file will be consulted to decide whether it should be cached
+  /// or not.
   ///
-  /// \param[in] memory_module
-  ///     True if this binary exists only in memory.
+  /// \param[in] type
+  ///     The type of file this code will read/write into the DataFileCache.
   ///
   /// \returns
-  ///   If caching is enabled in the lldb settings, return a pointer to the data
-  ///   file cache. If caching is not enabled, return NULL.
-  static DataFileCache *GetIndexCache(bool memory_module);
+  ///   If lldb settings have caching enabled for this type of file, return
+  ///   a pointer to the data file cache. If caching is not enabled,
+  ///   return nullptr.
+  static DataFileCache *GetIndexCache(LLDBIndexType type);
 
 protected:
   // Member Variables

diff  --git a/lldb/include/lldb/Symbol/ObjectFile.h b/lldb/include/lldb/Symbol/ObjectFile.h
index a2f8dc2b619ee1..bde4100a3eec90 100644
--- a/lldb/include/lldb/Symbol/ObjectFile.h
+++ b/lldb/include/lldb/Symbol/ObjectFile.h
@@ -694,6 +694,17 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
   /// Returns true if the object file exists only in memory.
   bool IsInMemory() const { return m_memory_addr != LLDB_INVALID_ADDRESS; }
 
+  /// Returns true if the symbol table for this in-memory object file should
+  /// not be loaded from the index cache or saved to the index cache, even
+  /// if caching of memory modules is enabled.
+  bool GetDisableMemorySymtabCaching() const {
+    return m_disable_memory_symtab_caching;
+  }
+
+  void SetDisableMemorySymtabCaching(bool disable) {
+    m_disable_memory_symtab_caching = disable;
+  }
+
   // Strip linker annotations (such as @@VERSION) from symbol names.
   virtual llvm::StringRef
   StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const {
@@ -785,6 +796,9 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
   lldb::ProcessWP m_process_wp;
   /// Set if the object file only exists in memory.
   const lldb::addr_t m_memory_addr;
+  /// Set if the symbol table for an in-memory object file should not be
+  /// loaded from the index cache.
+  bool m_disable_memory_symtab_caching;
 
   std::unique_ptr<lldb_private::SectionList> m_sections_up;
   std::recursive_mutex m_sections_mutex;

diff  --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index 4e58a4986fbb8d..0d57ed8b9245a3 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -289,6 +289,7 @@ Module::~Module() {
 
 ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
                                         lldb::addr_t header_addr, Status &error,
+                                        bool disable_memory_symtab_caching,
                                         size_t size_to_read) {
   if (m_objfile_sp) {
     error = Status::FromErrorString("object file already exists");
@@ -309,6 +310,8 @@ ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
                                               header_addr, data_sp);
         if (m_objfile_sp) {
           m_memory_module_addr = header_addr;
+          m_objfile_sp->SetDisableMemorySymtabCaching(
+              disable_memory_symtab_caching);
 
           // Once we get the object file, update our module with the object
           // file's architecture since it might 
diff er in vendor/os if some
@@ -1681,12 +1684,14 @@ std::string Module::GetCacheKey() {
   return key;
 }
 
-DataFileCache *Module::GetIndexCache(bool memory_module) {
+DataFileCache *Module::GetIndexCache(LLDBIndexType type) {
   bool lldb_index_enabled = false;
-  if (ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache())
+  if (ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache() &&
+      (type == eLLDBIndexTypeSymtab || type == eLLDBIndexTypeDwarfIndex))
     lldb_index_enabled = true;
-  if (memory_module && ModuleList::GetGlobalModuleListProperties()
-                           .GetEnableLLDBIndexCacheMemoryModules())
+  if (ModuleList::GetGlobalModuleListProperties()
+          .GetEnableLLDBIndexCacheMemoryModules() &&
+      type == eLLDBIndexTypeMemoryModuleSymtab)
     lldb_index_enabled = true;
   if (!lldb_index_enabled)
     return nullptr;

diff  --git a/lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp b/lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
index c3fff3d3d1486c..5511b05941bdd0 100644
--- a/lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
+++ b/lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
@@ -620,7 +620,8 @@ bool ManualDWARFIndex::LoadFromCache() {
   ObjectFile *objfile = m_dwarf->GetObjectFile();
   if (!objfile)
     return false;
-  DataFileCache *cache = Module::GetIndexCache(objfile->IsInMemory());
+  Module::LLDBIndexType type = Module::LLDBIndexType::eLLDBIndexTypeDwarfIndex;
+  DataFileCache *cache = Module::GetIndexCache(type);
   if (!cache)
     return false;
   std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
@@ -643,7 +644,8 @@ void ManualDWARFIndex::SaveToCache() {
   ObjectFile *objfile = m_dwarf->GetObjectFile();
   if (!objfile)
     return;
-  DataFileCache *cache = Module::GetIndexCache(objfile->IsInMemory());
+  Module::LLDBIndexType type = Module::LLDBIndexType::eLLDBIndexTypeDwarfIndex;
+  DataFileCache *cache = Module::GetIndexCache(type);
   if (!cache)
     return; // Caching is not enabled.
   DataEncoder file(endian::InlHostByteOrder(), objfile->GetAddressByteSize());

diff  --git a/lldb/source/Symbol/ObjectFile.cpp b/lldb/source/Symbol/ObjectFile.cpp
index b921b145e489eb..192f8bd37ddef1 100644
--- a/lldb/source/Symbol/ObjectFile.cpp
+++ b/lldb/source/Symbol/ObjectFile.cpp
@@ -256,7 +256,8 @@ ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
       m_type(eTypeInvalid), m_strata(eStrataInvalid),
       m_file_offset(file_offset), m_length(length),
       m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(),
-      m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(),
+      m_memory_addr(LLDB_INVALID_ADDRESS),
+      m_disable_memory_symtab_caching(false), m_sections_up(), m_symtab_up(),
       m_symtab_once_up(new llvm::once_flag()) {
   if (file_spec_ptr)
     m_file = *file_spec_ptr;
@@ -283,8 +284,8 @@ ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
     : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
       m_strata(eStrataInvalid), m_file_offset(0), m_length(0),
       m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(process_sp),
-      m_memory_addr(header_addr), m_sections_up(), m_symtab_up(),
-      m_symtab_once_up(new llvm::once_flag()) {
+      m_memory_addr(header_addr), m_disable_memory_symtab_caching(false),
+      m_sections_up(), m_symtab_up(), m_symtab_once_up(new llvm::once_flag()) {
   if (header_extractor_sp && header_extractor_sp->HasData())
     m_data_nsp = header_extractor_sp;
   Log *log = GetLog(LLDBLog::Object);

diff  --git a/lldb/source/Symbol/Symtab.cpp b/lldb/source/Symbol/Symtab.cpp
index ce164a38b9c58b..45ec3382e77bb1 100644
--- a/lldb/source/Symbol/Symtab.cpp
+++ b/lldb/source/Symbol/Symtab.cpp
@@ -1149,7 +1149,15 @@ std::string Symtab::GetCacheKey() {
 }
 
 void Symtab::SaveToCache() {
-  DataFileCache *cache = Module::GetIndexCache(m_objfile->IsInMemory());
+  Module::LLDBIndexType type = Module::LLDBIndexType::eLLDBIndexTypeSymtab;
+  if (m_objfile->IsInMemory()) {
+    if (m_objfile->GetDisableMemorySymtabCaching())
+      return;
+    else
+      type = Module::LLDBIndexType::eLLDBIndexTypeMemoryModuleSymtab;
+  }
+
+  DataFileCache *cache = Module::GetIndexCache(type);
   if (!cache)
     return; // Caching is not enabled.
 
@@ -1332,7 +1340,14 @@ bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
 }
 
 bool Symtab::LoadFromCache() {
-  DataFileCache *cache = Module::GetIndexCache(m_objfile->IsInMemory());
+  Module::LLDBIndexType type = Module::LLDBIndexType::eLLDBIndexTypeSymtab;
+  if (m_objfile->IsInMemory()) {
+    if (m_objfile->GetDisableMemorySymtabCaching())
+      return false;
+    else
+      type = Module::LLDBIndexType::eLLDBIndexTypeMemoryModuleSymtab;
+  }
+  DataFileCache *cache = Module::GetIndexCache(type);
   if (!cache)
     return false;
 

diff  --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index ad2ae1a5b682f9..ebc6b5d624a911 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -2826,7 +2826,12 @@ Process::ReadModuleFromMemory(const FileSpec &file_spec,
     progress_up = std::make_unique<Progress>("Reading binary from memory",
                                              file_spec.GetFilename().str());
 
+  // Don't use the index cache for symbol tables of binaries read out of a
+  // corefile.
+  const bool disable_memory_symtab_caching = (bool)GetCoreFile();
+
   if (module_sp->GetMemoryObjectFile(shared_from_this(), header_addr, error,
+                                     disable_memory_symtab_caching,
                                      size_to_read))
     return module_sp;
 

diff  --git a/lldb/test/API/functionalities/module_cache/corefile_memory_module/Makefile b/lldb/test/API/functionalities/module_cache/corefile_memory_module/Makefile
new file mode 100644
index 00000000000000..99998b20bcb050
--- /dev/null
+++ b/lldb/test/API/functionalities/module_cache/corefile_memory_module/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules

diff  --git a/lldb/test/API/functionalities/module_cache/corefile_memory_module/TestModuleCacheCorefileMemoryModule.py b/lldb/test/API/functionalities/module_cache/corefile_memory_module/TestModuleCacheCorefileMemoryModule.py
new file mode 100644
index 00000000000000..b996888e3df019
--- /dev/null
+++ b/lldb/test/API/functionalities/module_cache/corefile_memory_module/TestModuleCacheCorefileMemoryModule.py
@@ -0,0 +1,110 @@
+"""Test that symbol tables of binaries read out of a corefile's memory
+are not saved in the LLDB index cache."""
+
+import glob
+import os
+import shutil
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class ModuleCacheCorefileMemoryModuleTestCase(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+    SHARED_BUILD_TESTCASE = False
+
+    def setUp(self):
+        TestBase.setUp(self)
+        # Set the cache path before anything can touch the index cache,
+        # which latches the path on first use.
+        self.cache_dir = os.path.join(self.getBuildDir(), "lldb-module-cache")
+        self.runCmd(
+            'settings set -g symbols.lldb-index-cache-path "%s"' % self.cache_dir
+        )
+
+    def get_symtab_cache_files(self, basename):
+        module_file_glob = os.path.join(
+            self.cache_dir, "llvmcache-*%s*-symtab-*" % basename
+        )
+        return glob.glob(module_file_glob)
+
+    @requireDarwin
+    @skipIfRemote
+    def test_corefile_memory_module_not_cached(self):
+        """Create a corefile, then remove the binary from disk so lldb
+        must read it out of the corefile's memory. With caching of memory
+        modules enabled, confirm that no symbol table for the binary is
+        written to the index cache."""
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        corefile = self.getBuildArtifact("process.core")
+
+        # Enable the index cache for the live session as a positive control:
+        # it shows that this cache path and glob do find the binary's symbol
+        # table when it is cached.
+        self.runCmd("settings set -g symbols.enable-lldb-index-cache true")
+
+        target, process, _, _ = lldbutil.run_to_source_breakpoint(
+            self, "// break here", lldb.SBFileSpec("main.cpp")
+        )
+        exe_module = target.FindModule(lldb.SBFileSpec("a.out"))
+        self.assertGreater(exe_module.GetNumSymbols(), 0)
+        cache_files = self.get_symtab_cache_files("a.out")
+        self.assertEqual(
+            len(cache_files), 1, "expected a symtab cache file for the live binary"
+        )
+        for cache_file in cache_files:
+            os.unlink(cache_file)
+
+        # Save a stack-only corefile, plus the binary's __TEXT and
+        # __LINKEDIT so it can be read from the corefile's memory. A
+        # full corefile would include the entire shared cache.
+        options = lldb.SBSaveCoreOptions()
+        options.SetOutputFile(lldb.SBFileSpec(corefile))
+        options.SetStyle(lldb.eSaveCoreStackOnly)
+        for segname in ["__TEXT", "__LINKEDIT"]:
+            segment = exe_module.FindSection(segname)
+            self.assertTrue(segment.IsValid())
+            region = lldb.SBMemoryRegionInfo()
+            self.assertSuccess(
+                process.GetMemoryRegionInfo(segment.GetLoadAddress(target), region)
+            )
+            self.assertSuccess(options.AddMemoryRegionToSave(region))
+        self.assertSuccess(process.SaveCore(options))
+        self.assertTrue(os.path.isfile(corefile))
+
+        self.assertSuccess(process.Kill())
+        self.dbg.DeleteTarget(target)
+        self.assertEqual(self.dbg.GetNumTargets(), 0)
+        self.dbg.MemoryPressureDetected()
+
+        # Remove the binary and its dSYM so lldb can only get the binary
+        # from the corefile's memory.
+        os.unlink(exe)
+        dsym = exe + ".dSYM"
+        if os.path.exists(dsym):
+            shutil.rmtree(dsym)
+
+        self.runCmd("settings set -g symbols.enable-lldb-index-cache false")
+        self.runCmd(
+            "settings set -g symbols.enable-lldb-index-cache-memory-modules true"
+        )
+
+        target = self.dbg.CreateTarget(None)
+        self.assertTrue(target.IsValid())
+        process = target.LoadCore(corefile)
+        self.assertTrue(process.IsValid(), PROCESS_IS_VALID)
+
+        # Confirm the binary was loaded in the corefile process.
+        self.expect("image list", substrs=["a.out"])
+        exe_module = target.FindModule(lldb.SBFileSpec("a.out"))
+        self.assertTrue(exe_module.IsValid())
+
+        # Force the symbol table to be created, which is when it would be
+        # saved to the index cache.
+        self.runCmd("bt")
+        self.assertGreater(exe_module.GetNumSymbols(), 0)
+
+        # Confirm no symbol table was saved in the index cache for the binary.
+        self.assertEqual(len(self.get_symtab_cache_files("a.out")), 0)

diff  --git a/lldb/test/API/functionalities/module_cache/corefile_memory_module/main.cpp b/lldb/test/API/functionalities/module_cache/corefile_memory_module/main.cpp
new file mode 100644
index 00000000000000..d16f1d2d4ed5d0
--- /dev/null
+++ b/lldb/test/API/functionalities/module_cache/corefile_memory_module/main.cpp
@@ -0,0 +1,19 @@
+int func1(int in) { return in + 1; }
+
+int func2(int in) { return in * 2; }
+
+int func3(int in) { return in - 3; }
+
+int func4(int in) { return in * 4; }
+
+int func5(int in) { return in + 5; }
+
+int main() {
+  int val = 10;
+  val = func1(val);
+  val = func2(val);
+  val = func3(val);
+  val = func4(val);
+  val = func5(val); // break here
+  return val;
+}


        


More information about the llvm-branch-commits mailing list