[lldb] Don't cache symtabs for memory modules w/ corefile #237050
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Jason Molenda (jasonmolenda)
<details>
<summary>Changes</summary>
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 setsymbols.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
---
Patch is 55.31 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/237050.diff
46 Files Affected:
````````````diff --git a/lldb/include/lldb/Core/Module.h b/lldb/include/lldb/Core/Module.h
index 04aee9c101b444..5c4fe9fd2beb0a 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
@@ -1047,10 +1052,15 @@ class Module : public std::enable_shared_from_this<Module>,
/// \param[in] memory_module
/// True if this binary exists only in memory.
///
+ /// \param[in] disable_memory_symtab_caching
+ /// If true, ignore the setting that enables the index cache for memory
+ /// modules.
+ ///
/// \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);
+ static DataFileCache *
+ GetIndexCache(bool memory_module, bool disable_memory_symtab_caching = false);
protected:
// Member Variables
diff --git a/lldb/include/lldb/Expression/ObjectFileJIT.h b/lldb/include/lldb/Expression/ObjectFileJIT.h
index 61e364fda4240d..d86c1d3051c4a5 100644
--- a/lldb/include/lldb/Expression/ObjectFileJIT.h
+++ b/lldb/include/lldb/Expression/ObjectFileJIT.h
@@ -59,7 +59,8 @@ class ObjectFileJIT : public ObjectFile {
static lldb_private::ObjectFile *CreateMemoryInstance(
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
- const lldb::ProcessSP &process_sp, lldb::addr_t header_addr);
+ const lldb::ProcessSP &process_sp, lldb::addr_t header_addr,
+ bool disable_memory_symtab_caching);
static ModuleSpecList
GetModuleSpecifications(const lldb_private::FileSpec &file,
diff --git a/lldb/include/lldb/Symbol/ObjectFile.h b/lldb/include/lldb/Symbol/ObjectFile.h
index f626d0395ecf6a..a9e8b1a0c9d3c5 100644
--- a/lldb/include/lldb/Symbol/ObjectFile.h
+++ b/lldb/include/lldb/Symbol/ObjectFile.h
@@ -169,10 +169,16 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
///
/// \param[in] header_addr
/// The address of the header for the object file in memory.
+ ///
+ /// \param[in] disable_memory_symtab_caching
+ /// If true, the symbol table for this object file will not be loaded
+ /// from or saved to the index cache, even if caching of memory modules
+ /// is enabled. This is used for binaries read out of a corefile.
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp,
const lldb::ProcessSP &process_sp,
lldb::addr_t header_addr,
- lldb::WritableDataBufferSP file_data_sp);
+ lldb::WritableDataBufferSP file_data_sp,
+ bool disable_memory_symtab_caching);
static ModuleSpecList
GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset,
@@ -691,6 +697,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, 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 {
@@ -782,6 +799,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/include/lldb/Symbol/Symtab.h b/lldb/include/lldb/Symbol/Symtab.h
index ea5e2d8044402b..99b6585ff5d568 100644
--- a/lldb/include/lldb/Symbol/Symtab.h
+++ b/lldb/include/lldb/Symbol/Symtab.h
@@ -32,7 +32,7 @@ class Symtab {
enum Visibility { eVisibilityAny, eVisibilityExtern, eVisibilityPrivate };
- Symtab(ObjectFile *objfile);
+ Symtab(ObjectFile *objfile, bool disable_memory_symtab_caching);
~Symtab();
void PreloadSymbols();
@@ -284,6 +284,7 @@ class Symtab {
bool m_name_indexes_computed = false;
bool m_loaded_from_cache = false;
bool m_saved_to_cache = false;
+ bool m_disable_memory_symtab_caching = false;
private:
UniqueCStringMap<uint32_t> &
diff --git a/lldb/include/lldb/Target/Process.h b/lldb/include/lldb/Target/Process.h
index 226c58034dd655..95bac144c93c9c 100644
--- a/lldb/include/lldb/Target/Process.h
+++ b/lldb/include/lldb/Target/Process.h
@@ -2072,11 +2072,16 @@ class Process : public std::enable_shared_from_this<Process>,
/// \param[in] header_addr
/// The address pointing to the beginning of the object file's header.
///
+ /// \param[in] disable_memory_symtab_caching
+ /// If true, the symbol table for this module will not be loaded from or
+ /// saved to the index cache, even if caching of memory modules is enabled.
+ ///
/// \param[in] size_to_read
/// The number of bytes to read from memory. This should be large enough to
/// identify the object file format. Defaults to 512.
llvm::Expected<lldb::ModuleSP>
ReadModuleFromMemory(const FileSpec &file_spec, lldb::addr_t header_addr,
+ bool disable_memory_symtab_caching,
size_t size_to_read = 512);
/// Attempt to get the attributes for a region of memory in the process.
diff --git a/lldb/include/lldb/lldb-private-interfaces.h b/lldb/include/lldb/lldb-private-interfaces.h
index 276774a2ec3fde..16769e9367bb42 100644
--- a/lldb/include/lldb/lldb-private-interfaces.h
+++ b/lldb/include/lldb/lldb-private-interfaces.h
@@ -56,7 +56,8 @@ typedef ObjectFile *(*ObjectFileCreateInstance)(
lldb::offset_t file_offset, lldb::offset_t length);
typedef ObjectFile *(*ObjectFileCreateMemoryInstance)(
const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp,
- const lldb::ProcessSP &process_sp, lldb::addr_t offset);
+ const lldb::ProcessSP &process_sp, lldb::addr_t offset,
+ bool disable_memory_symtab_caching);
typedef bool (*ObjectFileSaveCore)(const lldb::ProcessSP &process_sp,
lldb_private::SaveCoreOptions &options,
Status &error);
diff --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp
index 4bc3df9992a72e..0ae426775d39ff 100644
--- a/lldb/source/API/SBModule.cpp
+++ b/lldb/source/API/SBModule.cpp
@@ -53,8 +53,12 @@ SBModule::SBModule(lldb::SBProcess &process, lldb::addr_t header_addr) {
ProcessSP process_sp(process.GetSP());
if (process_sp) {
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process_sp->GetCoreFile();
llvm::Expected<ModuleSP> module_sp_or_err =
- process_sp->ReadModuleFromMemory(FileSpec(), header_addr);
+ process_sp->ReadModuleFromMemory(FileSpec(), header_addr,
+ disable_memory_symtab_caching);
if (auto err = module_sp_or_err.takeError()) {
llvm::consumeError(std::move(err));
return;
diff --git a/lldb/source/Core/DynamicLoader.cpp b/lldb/source/Core/DynamicLoader.cpp
index 18226fb2be887a..94ab706300dce5 100644
--- a/lldb/source/Core/DynamicLoader.cpp
+++ b/lldb/source/Core/DynamicLoader.cpp
@@ -183,8 +183,12 @@ ModuleSP DynamicLoader::LoadModuleAtAddress(const FileSpec &file,
// We have a core file, try to load the image from memory if we didn't find
// the module.
if (!module_sp && !m_process->IsLiveDebugSession()) {
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)m_process->GetCoreFile();
llvm::Expected<ModuleSP> memory_module_sp_or_err =
- m_process->ReadModuleFromMemory(file, base_addr);
+ m_process->ReadModuleFromMemory(file, base_addr,
+ disable_memory_symtab_caching);
if (auto err = memory_module_sp_or_err.takeError())
LLDB_LOG_ERROR(GetLog(LLDBLog::DynamicLoader), std::move(err),
"Failed to read module from memory: {0}");
@@ -206,8 +210,11 @@ static ModuleSP ReadUnnamedMemoryModule(Process *process, addr_t addr,
snprintf(namebuf, sizeof(namebuf), "memory-image-0x%" PRIx64, addr);
name = namebuf;
}
- llvm::Expected<ModuleSP> module_sp_or_err =
- process->ReadModuleFromMemory(FileSpec(name), addr);
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process->GetCoreFile();
+ llvm::Expected<ModuleSP> module_sp_or_err = process->ReadModuleFromMemory(
+ FileSpec(name), addr, disable_memory_symtab_caching);
if (auto err = module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(GetLog(LLDBLog::DynamicLoader), std::move(err),
"Failed to read module from memory: {0}");
diff --git a/lldb/source/Core/Module.cpp b/lldb/source/Core/Module.cpp
index d59c0fbaca97cd..4b0e80bfcca5de 100644
--- a/lldb/source/Core/Module.cpp
+++ b/lldb/source/Core/Module.cpp
@@ -288,6 +288,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");
@@ -304,8 +305,9 @@ ObjectFile *Module::GetMemoryObjectFile(const lldb::ProcessSP &process_sp,
if (bytes_read < size_to_read)
data_sp->SetByteSize(bytes_read);
if (data_sp->GetByteSize() > 0) {
- m_objfile_sp = ObjectFile::FindPlugin(shared_from_this(), process_sp,
- header_addr, data_sp);
+ m_objfile_sp =
+ ObjectFile::FindPlugin(shared_from_this(), process_sp, header_addr,
+ data_sp, disable_memory_symtab_caching);
if (m_objfile_sp) {
m_memory_module_addr = header_addr;
@@ -1666,12 +1668,14 @@ std::string Module::GetCacheKey() {
return key;
}
-DataFileCache *Module::GetIndexCache(bool memory_module) {
+DataFileCache *Module::GetIndexCache(bool memory_module,
+ bool disable_memory_symtab_caching) {
bool lldb_index_enabled = false;
if (ModuleList::GetGlobalModuleListProperties().GetEnableLLDBIndexCache())
lldb_index_enabled = true;
- if (memory_module && ModuleList::GetGlobalModuleListProperties()
- .GetEnableLLDBIndexCacheMemoryModules())
+ if (memory_module && !disable_memory_symtab_caching &&
+ ModuleList::GetGlobalModuleListProperties()
+ .GetEnableLLDBIndexCacheMemoryModules())
lldb_index_enabled = true;
if (!lldb_index_enabled)
return nullptr;
diff --git a/lldb/source/Expression/ObjectFileJIT.cpp b/lldb/source/Expression/ObjectFileJIT.cpp
index b4fe84e868e766..022768236ad0dd 100644
--- a/lldb/source/Expression/ObjectFileJIT.cpp
+++ b/lldb/source/Expression/ObjectFileJIT.cpp
@@ -51,10 +51,10 @@ ObjectFile *ObjectFileJIT::CreateInstance(const lldb::ModuleSP &module_sp,
return nullptr;
}
-ObjectFile *ObjectFileJIT::CreateMemoryInstance(const lldb::ModuleSP &module_sp,
- WritableDataBufferSP data_sp,
- const ProcessSP &process_sp,
- lldb::addr_t header_addr) {
+ObjectFile *ObjectFileJIT::CreateMemoryInstance(
+ const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
+ const ProcessSP &process_sp, lldb::addr_t header_addr,
+ bool disable_memory_symtab_caching) {
// JIT'ed object file is backed by the ObjectFileJITDelegate, never read from
// memory
return nullptr;
diff --git a/lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp b/lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp
index 7428523ae763b4..85c9ac0d65eccd 100644
--- a/lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp
+++ b/lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp
@@ -468,8 +468,12 @@ DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress(lldb::addr_t addr,
if (header.filetype == llvm::MachO::MH_EXECUTE &&
(header.flags & llvm::MachO::MH_DYLDLINK) == 0) {
// Create a full module to get the UUID
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process->GetCoreFile();
llvm::Expected<ModuleSP> memory_module_sp_or_err =
- process->ReadModuleFromMemory(FileSpec("temp_mach_kernel"), addr);
+ process->ReadModuleFromMemory(FileSpec("temp_mach_kernel"), addr,
+ disable_memory_symtab_caching);
if (auto err = memory_module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(log, std::move(err),
"DynamicLoaderDarwinKernel::CheckForKernelImageAtAddress: "
@@ -673,8 +677,13 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::ReadMemoryModule(
size_to_read = sizeof(llvm::MachO::mach_header_64) + mh.sizeofcmds;
}
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process->GetCoreFile();
llvm::Expected<ModuleSP> memory_module_sp_or_err =
- process->ReadModuleFromMemory(file_spec, m_load_address, size_to_read);
+ process->ReadModuleFromMemory(file_spec, m_load_address,
+ disable_memory_symtab_caching,
+ size_to_read);
if (auto err = memory_module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(log, std::move(err),
"KextImageInfo::ReadMemoryModule failed to read module from "
diff --git a/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp b/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
index eb08683f4f1e67..f2520266c3d1c0 100644
--- a/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
+++ b/lldb/source/Plugins/DynamicLoader/FreeBSD-Kernel/DynamicLoaderFreeBSDKernel.cpp
@@ -296,8 +296,12 @@ lldb_private::UUID DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress(
if (header.e_type != expected_type)
return UUID();
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process->GetCoreFile();
llvm::Expected<ModuleSP> memory_module_sp_or_err =
- process->ReadModuleFromMemory(FileSpec("temp_freebsd_kernel"), addr);
+ process->ReadModuleFromMemory(FileSpec("temp_freebsd_kernel"), addr,
+ disable_memory_symtab_caching);
if (auto err = memory_module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(log, std::move(err),
"DynamicLoaderFreeBSDKernel::CheckForKernelImageAtAddress: "
@@ -394,8 +398,13 @@ bool DynamicLoaderFreeBSDKernel::KModImageInfo::ReadMemoryModule(
}
}
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)process->GetCoreFile();
llvm::Expected<ModuleSP> memory_module_sp_or_err =
- process->ReadModuleFromMemory(file_spec, m_load_address, size_to_read);
+ process->ReadModuleFromMemory(file_spec, m_load_address,
+ disable_memory_symtab_caching,
+ size_to_read);
if (auto err = memory_module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(log, std::move(err),
"KextImageInfo::ReadMemoryModule: Failed to read module "
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
index 6afda72dbbfff2..de94de7d6a75cd 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp
@@ -179,8 +179,11 @@ ModuleSP DynamicLoaderDarwin::FindTargetModuleForImageInfo(
// We'll call Target::ModulesDidLoad after all the modules have been
// added to the target, don't let it be called for every one.
if (!module_sp || module_sp->GetObjectFile() == nullptr) {
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)m_process->GetCoreFile();
llvm::Expected<ModuleSP> module_sp_or_err = m_process->ReadModuleFromMemory(
- image_info.file_spec, image_info.address,
+ image_info.file_spec, image_info.address, disable_memory_symtab_caching,
image_info.mh_and_load_cmd_size);
if (auto err = module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(GetLog(LLDBLog::DynamicLoader), std::move(err),
@@ -785,9 +788,12 @@ bool DynamicLoaderDarwin::AddModulesUsingPreloadedModules(
true /* notify */);
if (!commpage_image_module_sp ||
commpage_image_module_sp->GetObjectFile() == nullptr) {
+ const bool disable_memory_symtab_caching =
+ (bool)m_process->GetCoreFile();
llvm::Expected<ModuleSP> module_sp_or_err =
- m_process->ReadModuleFromMemory(image_info.file_spec,
- image_info.address);
+ m_process->ReadModuleFromMemory(
+ image_info.file_spec, image_info.address,
+ disable_memory_symtab_caching);
if (auto err = module_sp_or_err.takeError()) {
LLDB_LOG_ERROR(log, std::move(err),
"Failed to read module from memory: {0}");
diff --git a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
index 0ecb4528771a7f..a04496d4959e04 100644
--- a/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -609,8 +609,12 @@ void DynamicLoaderPOSIXDYLD::LoadVDSO() {
return;
}
+ // Don't use the index cache for symbol tables of binaries read out of a
+ // corefile.
+ const bool disable_memory_symtab_caching = (bool)m_process->GetCoreFile();
llvm::Expected<ModuleSP> module_sp_or_err = m_process->ReadModuleFromMemory(
- file, m_vdso_base, info.GetRange().GetByteSize());
+ file, m_vdso_base, disable_memory_symtab_caching,
+...
[truncated]
</details>
I can't tell from this comment and the one above GetMemoryObjectFile what this actually does. I can't tell whether, if disable_memory_symtab_caching is on, you will cache and re-read everything else from the memory object files, just not the symtab.
But then this comment makes it sound like this just overrides the global setting for caching memory modules.
I guess you need to explain what the symtab in disable_memory_symtab_caching means.
What is the difference between calling this function with disable_memory_symtab_caching = true and just not calling it?
An object file is not just a symtab, so using disable_memory_symtab_caching to mean disable_this_module_memory_caching is confusing. Are you naming the part for the whole?
SBModule seems like the wrong place to be checking the process type and turning off symtab caching. Seems like the process API ReadModuleFromMemory should be the one doing this check. You do the same check everywhere you use this so far as I can see which also makes it seem like this is checked at too high a level.
Also, unless you are convinced that there will be no cases other than CoreFiles where you will want to suppress this, there should really be some API like Process::ShouldCacheModule(Module *) which would return true by default and false for CoreFile plugins and whatever other logic is good when there's other logic.
I guess this is just style. I think checking memory_module & disable_memory_symtab_caching up-front and not calling GetIndexCache when they say not to is easier to read than passing the parameters here and having to go read the function to figure out which cases are no-ops.
But it was this way with just memory_module before so...
Yes, seeing this check over and over again makes me more convinced this belongs in Process::ReadModuleFromMemory...
It seems to add flexibility to have every client of Process::ReadModuleFromMemory get's to check whether it is a CoreFile process and turn off caching but since EVERY client does that, that doesn't seem like the right place to make that decision. I think having the Process check some API that the Process plugins can override would (a) make what was going on more obvious since it would centralize the decision and (b) be less error prone when adding new clients and (c) still give the Process a chance to customize this in a easy to read manner.
This is a good point. This PR (and the existing functionality) are only caching the symbol table of in-memory binaries by default, and this is an override switch to disable that default behavior. But over in https://github.com/llvm/llvm-project/pull/231344 (which I'm going to need to rewrite once this lands anyway, don't look) I'm also caching the header bytes of binaries. That PR currently does this in DynamicLoaderDarwin, but it would much more naturally fit in Process:: ReadModuleFromMemory and key off of the same setting.
Which argues for renaming the setting, at the very least. With that PR, symbols.enable-lldb-index-cache-memory-modules now controls caching the header bytes AND the symbol table for in-memory binaries. It's a brand new setting so I don't feel bad about changing it (normally removing/renaming a setting will introduce an error if people have this in their .lldbinit files or recommended people use it)
I wanted to centralize the logic of whether to enable DataFileCache symtab reading/writing in GetIndexCache entirely. We pass in bool memory_module, it would be just as easy to pass is_memory_module && !disable_memory_module_caching as the value to that parameter, but I thought it was clearer to make all the decisions in that one method.
One argument for the setting name symbols.enable-lldb-index-cache-memory-modules is that (1) all of the settings related to DataFileCache have lldb-index in the names, and (2) it doesn't actually SAY symtab here, although being under the symbols. setting hierarchy implies it.
As this code exists today, you're completely right. I'm generalizing because I know I want to make a further change soon: I want DynamicLoaderDarwin to be able to suppress symtab caching of memory-modules if all Shared Cache binaries are being read from memory. This is indicative of an improperly set-up debug host system, and we don't want to paper over that (and burn a couple GB of disk space in their DataFileCache for a week, at least) issue. That logic needs to reside in the DynamicLoader plugin.
It might be best to move the "if corefile" check in to Process:: ReadModuleFromMemory and have a defaulted argument for DynamicLoaderDarwin to set when it wants to suppress this caching.
Then at least add that info to the docs: this function is a no-op if memory_module == false or disable_memory_symtab_caching == true
It seems to add flexibility to have every client of Process::ReadModuleFromMemory get's to check whether it is a CoreFile process and turn off caching but since EVERY client does that, that doesn't seem like the right place to make that decision.
Agree, I'll centralize this in Process, for corefiles you're right. But I do need a way for the DynamicLoader to override the behavior - that's why I put the logic in the DynamicLoader to begin with.
Either that or have the base Process::ShouldCacheModule(ModuleSP module_sp) method check with the DynamicLoader.
Seems like having the process organize this, and then ask anyone that's relevant is more coherent.
Good idea.
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:white_check_mark: With the latest revision this PR passed the C/C++ code formatter.
This is a good point. I updated the headerdoc to include this bit:
/// The arguments to this method give it context about the type of file we
/// are currently handling. It will combine that context with the user's
/// settings, to decide if a DataFileCache should be used.
as well as ca few other small tweaks.
Just to get back to this: Yes I am calling
DataFileCache *cache = Module::GetIndexCache(m_objfile->IsInMemory(),
m_disable_memory_symtab_caching);
with the idea that Module::GetIndexCache implements all the policy decisions about when we enable caching or not, given the context provided in the arguments (Module::GetIndexCache is a static method, it has nothing) and the user settings. At the two callers in Symtab.cpp I could do
if (m_objfile->IsInMemory() && m_disable_memory_symtab_caching)
return;
but I _think_ it's easier to centralize all of the "should we cache things" in Module::GetIndexCache() and give it all the context needed to do that.
I opened an RFC where I talk about the settings structure, and how I'll be adding a new type of file in DataFileCache soon, bloom filters, and so I imagine Module::GetIndexCache() will soon look like
GetIndexCache(bool memory_module, bool bloom_filter, bool disable_memory_symtab_caching = false);
as we need to provide this additional piece of context.
Another approach would be to have separate named GetIndexCache methods for each different context, like
Module::GetIndexCache()
Module::GetIndexCacheMemoryModule(bool disable_memory_symtab_caching = false)
Module::GetIndexCacheBloomFilter()
I think the one outstanding point being discussed is about Module::GetIndexCache, which I saw as the central authority for deciding if we should create a DataFileCache for a given binary we are examining. e.g. I am currently passing in "is this a memory module" and "should memory module caching be allowed" -- which clearly the caller could compare those two and avoid making a call to Module::GetIndexCache altogether. But I thought it was cleaner to have all of the logic about whether to cache or not centralized in that static method.
I also mention, https://github.com/llvm/llvm-project/pull/237050#discussion_r4450834703 , how maybe I should have different GetIndexCache methods for the different classes of files I'm creating/memory modules.
I think everything else is addressed here. Thanks for the close reading, Jim.
I created an RFC for how we might rationalize the DataFileCache settings based on how I'm extending the feature, https://discourse.llvm.org/t/settings-for-lldbs-datafilecache-as-i-add-new-things-in-there/92701/5 . My idea is that we have 6 different settings to enable or disable different types of DataFileCache files (plus a master setting to disable all DataFileCache content creation). So we could have
enum { eLLDBIndexTypeSymtab, eLLDBIndexTypeDwarfIndex, eLLDBIndexTypeMemoryModuleSymtab, eLLDBIndexMemoryModuleHeaderbytes, eLLDBIndexTypeBloomFilter } LLDBIndexType;
DataFileCache *GetIndexCache(LLDBIndexType type);
With this, it seems more reasonable to me to keep the disable_memory_symtab_caching logic in the caller, instead of passing it into GetIndexCache(). Passing that as an argument would only have any meaning with eLLDBIndexTypeMemoryModuleSymtab and eLLDBIndexMemoryModuleHeaderbytes.
I implemented this idea, I think the way Module::GetIndexCache() is easier to understand, and will extend well to my view of how the settings should be restructured. Thanks for pushing me on this part.
I've addressed all of Jim's comments on this PR, thanks for that helpful feedback.
Ping on this one again, I addressed Jim's initial feedback and then spent a bunch of time going over everything again carefully. I'm happy with this PR at this point.
LGTM. Do you expect to pass any other caching related options to ObjectFile? If so, I would pass a CachingPolicy struct (by value?) to future proof it.
LGTM. Do you expect to pass any other caching related options to ObjectFile? If so, I would pass a CachingPolicy struct (by value?) to future proof it.
Besides the binary header and symbol table, the only thing left are the text instructions. We do need to read those out of memory lazily as we backtrace through functions in a memory module, but I think we're opening ourselves to some very big reads if we tried to cache the whole text section, I don't see us ever adding that.
JDevlieghere
jasonmolenda
In https://github.com/llvm/llvm-project/pull/229305 I added a new setting,
symbols.enable-lldb-index-cache-memory-moduleswhich 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-modulessetting and not create the symtabs, unless the user has setsymbols.enable-lldb-index-cachethemselves.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::GetSymtaband 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::ReadModuleFromMemoryare the place where this decision is made. This argument is passed throughModule::GetMemoryObjectFile, then throughObjectFile::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 toModule::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::ReadModuleFromMemoryandModule::GetIndexCachehave 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