
Fix a [test failure](https://github.com/llvm/llvm-project/pull/136236#issuecomment-2819772879) in #136236, apply a minor renaming of statistics, and remerge. See details below. # Changes in #136236 Currently, `DebuggerStats::ReportStatistics()` calls `Module::GetSymtab(/*can_create=*/false)`, but then the latter calls `SymbolFile::GetSymtab()`. This will load symbols if haven't yet. See stacktrace below. The problem is that `DebuggerStats::ReportStatistics` should be read-only. This is especially important because it reports stats for symtab parsing/indexing time, which could be affected by the reporting itself if it's not read-only. This patch fixes this problem by adding an optional parameter `SymbolFile::GetSymtab(bool can_create = true)` and receiving the `false` value passed down from `Module::GetSymtab(/*can_create=*/false)` when the call is initiated from `DebuggerStats::ReportStatistics()`. --- Notes about the following stacktrace: 1. This can be reproduced. Create a helloworld program on **macOS** with dSYM, add `settings set target.preload-symbols false` to `~/.lldbinit`, do `lldb a.out`, then `statistics dump`. 2. `ObjectFile::GetSymtab` has `llvm::call_once`. So the fact that it called into `ObjectFileMachO::ParseSymtab` means that the symbol table is actually being parsed. ``` (lldb) bt * thread #1, queue = 'com.apple.main-thread', stop reason = step over frame #0: 0x0000000124c4d5a0 LLDB`ObjectFileMachO::ParseSymtab(this=0x0000000111504e40, symtab=0x0000600000a05e00) at ObjectFileMachO.cpp:2259:44 * frame #1: 0x0000000124fc50a0 LLDB`lldb_private::ObjectFile::GetSymtab()::$_0::operator()(this=0x000000016d35c858) const at ObjectFile.cpp:761:9 frame #5: 0x0000000124fc4e68 LLDB`void std::__1::__call_once_proxy[abi:v160006]<std::__1::tuple<lldb_private::ObjectFile::GetSymtab()::$_0&&>>(__vp=0x000000016d35c7f0) at mutex:652:5 frame #6: 0x0000000198afb99c libc++.1.dylib`std::__1::__call_once(unsigned long volatile&, void*, void (*)(void*)) + 196 frame #7: 0x0000000124fc4dd0 LLDB`void std::__1::call_once[abi:v160006]<lldb_private::ObjectFile::GetSymtab()::$_0>(__flag=0x0000600003920080, __func=0x000000016d35c858) at mutex:670:9 frame #8: 0x0000000124fc3cb0 LLDB`void llvm::call_once<lldb_private::ObjectFile::GetSymtab()::$_0>(flag=0x0000600003920080, F=0x000000016d35c858) at Threading.h:88:5 frame #9: 0x0000000124fc2bc4 LLDB`lldb_private::ObjectFile::GetSymtab(this=0x0000000111504e40) at ObjectFile.cpp:755:5 frame #10: 0x0000000124fe0a28 LLDB`lldb_private::SymbolFileCommon::GetSymtab(this=0x0000000104865200) at SymbolFile.cpp:158:39 frame #11: 0x0000000124d8fedc LLDB`lldb_private::Module::GetSymtab(this=0x00000001113041a8, can_create=false) at Module.cpp:1027:21 frame #12: 0x0000000125125bdc LLDB`lldb_private::DebuggerStats::ReportStatistics(debugger=0x000000014284d400, target=0x0000000115808200, options=0x000000014195d6d1) at Statistics.cpp:329:30 frame #13: 0x0000000125672978 LLDB`CommandObjectStatsDump::DoExecute(this=0x000000014195d540, command=0x000000016d35d820, result=0x000000016d35e150) at CommandObjectStats.cpp:144:18 frame #14: 0x0000000124f29b40 LLDB`lldb_private::CommandObjectParsed::Execute(this=0x000000014195d540, args_string="", result=0x000000016d35e150) at CommandObject.cpp:832:9 frame #15: 0x0000000124efbd70 LLDB`lldb_private::CommandInterpreter::HandleCommand(this=0x0000000141b22f30, command_line="statistics dump", lazy_add_to_history=eLazyBoolCalculate, result=0x000000016d35e150, force_repeat_command=false) at CommandInterpreter.cpp:2134:14 frame #16: 0x0000000124f007f4 LLDB`lldb_private::CommandInterpreter::IOHandlerInputComplete(this=0x0000000141b22f30, io_handler=0x00000001419b2aa8, line="statistics dump") at CommandInterpreter.cpp:3251:3 frame #17: 0x0000000124d7b5ec LLDB`lldb_private::IOHandlerEditline::Run(this=0x00000001419b2aa8) at IOHandler.cpp:588:22 frame #18: 0x0000000124d1e8fc LLDB`lldb_private::Debugger::RunIOHandlers(this=0x000000014284d400) at Debugger.cpp:1225:16 frame #19: 0x0000000124f01f74 LLDB`lldb_private::CommandInterpreter::RunCommandInterpreter(this=0x0000000141b22f30, options=0x000000016d35e63c) at CommandInterpreter.cpp:3543:16 frame #20: 0x0000000122840294 LLDB`lldb::SBDebugger::RunCommandInterpreter(this=0x000000016d35ebd8, auto_handle_events=true, spawn_thread=false) at SBDebugger.cpp:1212:42 frame #21: 0x0000000102aa6d28 lldb`Driver::MainLoop(this=0x000000016d35ebb8) at Driver.cpp:621:18 frame #22: 0x0000000102aa75b0 lldb`main(argc=1, argv=0x000000016d35f548) at Driver.cpp:829:26 frame #23: 0x0000000198858274 dyld`start + 2840 ``` # Changes in this PR top of the above Fix a [test failure](https://github.com/llvm/llvm-project/pull/136236#issuecomment-2819772879) in `TestStats.py`. The original version of the added test checks that all modules have symbol count zero when `target.preload-symbols == false`. The test failed on macOS. Due to various reasons, on macOS, symbols can be loaded for dylibs even with that setting, but not for the main module. For now, the fix of the test is to limit the assertion to only the main module. The test now passes on macOS. In the future, when we have a way to control a specific list of plug-ins to be loaded, there may be a configuration that this test can use to assert that all modules have symbol count zero. Apply a minor renaming of statistics, per the [suggestion](https://github.com/llvm/llvm-project/pull/136226#issuecomment-2825080275) in #136226 after merge.
268 lines
9.4 KiB
C++
268 lines
9.4 KiB
C++
//===-- SymbolFile.cpp ----------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lldb/Symbol/SymbolFile.h"
|
|
|
|
#include "lldb/Core/Module.h"
|
|
#include "lldb/Core/PluginManager.h"
|
|
#include "lldb/Symbol/CompileUnit.h"
|
|
#include "lldb/Symbol/ObjectFile.h"
|
|
#include "lldb/Symbol/SymbolFileOnDemand.h"
|
|
#include "lldb/Symbol/TypeMap.h"
|
|
#include "lldb/Symbol/TypeSystem.h"
|
|
#include "lldb/Symbol/VariableList.h"
|
|
#include "lldb/Utility/Log.h"
|
|
#include "lldb/Utility/StreamString.h"
|
|
#include "lldb/Utility/StructuredData.h"
|
|
#include "lldb/lldb-private.h"
|
|
|
|
#include <future>
|
|
|
|
using namespace lldb_private;
|
|
using namespace lldb;
|
|
|
|
char SymbolFile::ID;
|
|
char SymbolFileCommon::ID;
|
|
|
|
void SymbolFile::PreloadSymbols() {
|
|
// No-op for most implementations.
|
|
}
|
|
|
|
std::recursive_mutex &SymbolFile::GetModuleMutex() const {
|
|
return GetObjectFile()->GetModule()->GetMutex();
|
|
}
|
|
|
|
SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {
|
|
std::unique_ptr<SymbolFile> best_symfile_up;
|
|
if (objfile_sp != nullptr) {
|
|
|
|
// We need to test the abilities of this section list. So create what it
|
|
// would be with this new objfile_sp.
|
|
lldb::ModuleSP module_sp(objfile_sp->GetModule());
|
|
if (module_sp) {
|
|
// Default to the main module section list.
|
|
ObjectFile *module_obj_file = module_sp->GetObjectFile();
|
|
if (module_obj_file != objfile_sp.get()) {
|
|
// Make sure the main object file's sections are created
|
|
module_obj_file->GetSectionList();
|
|
objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList());
|
|
}
|
|
}
|
|
|
|
// TODO: Load any plug-ins in the appropriate plug-in search paths and
|
|
// iterate over all of them to find the best one for the job.
|
|
|
|
uint32_t best_symfile_abilities = 0;
|
|
|
|
SymbolFileCreateInstance create_callback;
|
|
for (uint32_t idx = 0;
|
|
(create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(
|
|
idx)) != nullptr;
|
|
++idx) {
|
|
std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));
|
|
|
|
if (curr_symfile_up) {
|
|
const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();
|
|
if (sym_file_abilities > best_symfile_abilities) {
|
|
best_symfile_abilities = sym_file_abilities;
|
|
best_symfile_up.reset(curr_symfile_up.release());
|
|
// If any symbol file parser has all of the abilities, then we should
|
|
// just stop looking.
|
|
if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (best_symfile_up) {
|
|
// If symbol on-demand is enabled the winning symbol file parser is
|
|
// wrapped with SymbolFileOnDemand so that hydration of the debug info
|
|
// can be controlled to improve performance.
|
|
//
|
|
// Currently the supported on-demand symbol files include:
|
|
// executables, shared libraries and debug info files.
|
|
//
|
|
// To reduce unnecessary wrapping files with zero debug abilities are
|
|
// skipped.
|
|
ObjectFile::Type obj_file_type = objfile_sp->CalculateType();
|
|
if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&
|
|
best_symfile_abilities > 0 &&
|
|
(obj_file_type == ObjectFile::eTypeExecutable ||
|
|
obj_file_type == ObjectFile::eTypeSharedLibrary ||
|
|
obj_file_type == ObjectFile::eTypeDebugInfo)) {
|
|
best_symfile_up =
|
|
std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));
|
|
}
|
|
// Let the winning symbol file parser initialize itself more completely
|
|
// now that it has been chosen
|
|
best_symfile_up->InitializeObject();
|
|
}
|
|
}
|
|
return best_symfile_up.release();
|
|
}
|
|
|
|
uint32_t
|
|
SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
|
|
lldb::SymbolContextItem resolve_scope,
|
|
SymbolContextList &sc_list) {
|
|
return 0;
|
|
}
|
|
|
|
void SymbolFile::FindGlobalVariables(ConstString name,
|
|
const CompilerDeclContext &parent_decl_ctx,
|
|
uint32_t max_matches,
|
|
VariableList &variables) {}
|
|
|
|
void SymbolFile::FindGlobalVariables(const RegularExpression ®ex,
|
|
uint32_t max_matches,
|
|
VariableList &variables) {}
|
|
|
|
void SymbolFile::FindFunctions(const Module::LookupInfo &lookup_info,
|
|
const CompilerDeclContext &parent_decl_ctx,
|
|
bool include_inlines,
|
|
SymbolContextList &sc_list) {}
|
|
|
|
void SymbolFile::FindFunctions(const RegularExpression ®ex,
|
|
bool include_inlines,
|
|
SymbolContextList &sc_list) {}
|
|
|
|
void SymbolFile::GetMangledNamesForFunction(
|
|
const std::string &scope_qualified_name,
|
|
std::vector<ConstString> &mangled_names) {}
|
|
|
|
void SymbolFile::AssertModuleLock() {
|
|
// The code below is too expensive to leave enabled in release builds. It's
|
|
// enabled in debug builds or when the correct macro is set.
|
|
#if defined(LLDB_CONFIGURATION_DEBUG)
|
|
// We assert that we have to module lock by trying to acquire the lock from a
|
|
// different thread. Note that we must abort if the result is true to
|
|
// guarantee correctness.
|
|
assert(std::async(
|
|
std::launch::async,
|
|
[this] {
|
|
return this->GetModuleMutex().try_lock();
|
|
}).get() == false &&
|
|
"Module is not locked");
|
|
#endif
|
|
}
|
|
|
|
SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;
|
|
|
|
Symtab *SymbolFileCommon::GetSymtab(bool can_create) {
|
|
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
|
|
// Fetch the symtab from the main object file.
|
|
auto *symtab = GetMainObjectFile()->GetSymtab(can_create);
|
|
if (m_symtab != symtab) {
|
|
m_symtab = symtab;
|
|
|
|
// Then add our symbols to it.
|
|
if (m_symtab)
|
|
AddSymbols(*m_symtab);
|
|
}
|
|
return m_symtab;
|
|
}
|
|
|
|
ObjectFile *SymbolFileCommon::GetMainObjectFile() {
|
|
return m_objfile_sp->GetModule()->GetObjectFile();
|
|
}
|
|
|
|
void SymbolFileCommon::SectionFileAddressesChanged() {
|
|
ObjectFile *module_objfile = GetMainObjectFile();
|
|
ObjectFile *symfile_objfile = GetObjectFile();
|
|
if (symfile_objfile != module_objfile)
|
|
symfile_objfile->SectionFileAddressesChanged();
|
|
if (auto *symtab = GetSymtab())
|
|
symtab->SectionFileAddressesChanged();
|
|
}
|
|
|
|
uint32_t SymbolFileCommon::GetNumCompileUnits() {
|
|
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
|
|
if (!m_compile_units) {
|
|
// Create an array of compile unit shared pointers -- which will each
|
|
// remain NULL until someone asks for the actual compile unit information.
|
|
m_compile_units.emplace(CalculateNumCompileUnits());
|
|
}
|
|
return m_compile_units->size();
|
|
}
|
|
|
|
CompUnitSP SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx) {
|
|
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
|
|
uint32_t num = GetNumCompileUnits();
|
|
if (idx >= num)
|
|
return nullptr;
|
|
lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
|
|
if (!cu_sp)
|
|
cu_sp = ParseCompileUnitAtIndex(idx);
|
|
return cu_sp;
|
|
}
|
|
|
|
void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx,
|
|
const CompUnitSP &cu_sp) {
|
|
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
|
|
const size_t num_compile_units = GetNumCompileUnits();
|
|
assert(idx < num_compile_units);
|
|
UNUSED_IF_ASSERT_DISABLED(num_compile_units);
|
|
|
|
// Fire off an assertion if this compile unit already exists for now. The
|
|
// partial parsing should take care of only setting the compile unit
|
|
// once, so if this assertion fails, we need to make sure that we don't
|
|
// have a race condition, or have a second parse of the same compile
|
|
// unit.
|
|
assert((*m_compile_units)[idx] == nullptr);
|
|
(*m_compile_units)[idx] = cu_sp;
|
|
}
|
|
|
|
llvm::Expected<TypeSystemSP>
|
|
SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language) {
|
|
auto type_system_or_err =
|
|
m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
|
|
if (type_system_or_err) {
|
|
if (auto ts = *type_system_or_err)
|
|
ts->SetSymbolFile(this);
|
|
}
|
|
return type_system_or_err;
|
|
}
|
|
|
|
uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {
|
|
if (!m_objfile_sp)
|
|
return 0;
|
|
ModuleSP module_sp(m_objfile_sp->GetModule());
|
|
if (!module_sp)
|
|
return 0;
|
|
const SectionList *section_list = module_sp->GetSectionList();
|
|
if (section_list)
|
|
return section_list->GetDebugInfoSize();
|
|
return 0;
|
|
}
|
|
|
|
void SymbolFileCommon::Dump(Stream &s) {
|
|
s.Format("SymbolFile {0} ({1})\n", GetPluginName(),
|
|
GetMainObjectFile()->GetFileSpec());
|
|
s.PutCString("Types:\n");
|
|
m_type_list.Dump(&s, /*show_context*/ false);
|
|
s.PutChar('\n');
|
|
|
|
s.PutCString("Compile units:\n");
|
|
if (m_compile_units) {
|
|
for (const CompUnitSP &cu_sp : *m_compile_units) {
|
|
// We currently only dump the compile units that have been parsed
|
|
if (cu_sp)
|
|
cu_sp->Dump(&s, /*show_context*/ false);
|
|
}
|
|
}
|
|
s.PutChar('\n');
|
|
|
|
if (Symtab *symtab = GetSymtab())
|
|
symtab->Dump(&s, nullptr, eSortOrderNone);
|
|
}
|
|
|
|
std::string SymbolFile::GetObjectName() const {
|
|
if (const ObjectFile *object_file = GetObjectFile())
|
|
return object_file->GetObjectName();
|
|
return "";
|
|
}
|