Path: blob/main/contrib/llvm-project/lldb/source/Symbol/SymbolFile.cpp
39587 views
//===-- SymbolFile.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Symbol/SymbolFile.h"910#include "lldb/Core/Module.h"11#include "lldb/Core/PluginManager.h"12#include "lldb/Symbol/CompileUnit.h"13#include "lldb/Symbol/ObjectFile.h"14#include "lldb/Symbol/SymbolFileOnDemand.h"15#include "lldb/Symbol/TypeMap.h"16#include "lldb/Symbol/TypeSystem.h"17#include "lldb/Symbol/VariableList.h"18#include "lldb/Utility/Log.h"19#include "lldb/Utility/StreamString.h"20#include "lldb/Utility/StructuredData.h"21#include "lldb/lldb-private.h"2223#include <future>2425using namespace lldb_private;26using namespace lldb;2728char SymbolFile::ID;29char SymbolFileCommon::ID;3031void SymbolFile::PreloadSymbols() {32// No-op for most implementations.33}3435std::recursive_mutex &SymbolFile::GetModuleMutex() const {36return GetObjectFile()->GetModule()->GetMutex();37}3839SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {40std::unique_ptr<SymbolFile> best_symfile_up;41if (objfile_sp != nullptr) {4243// We need to test the abilities of this section list. So create what it44// would be with this new objfile_sp.45lldb::ModuleSP module_sp(objfile_sp->GetModule());46if (module_sp) {47// Default to the main module section list.48ObjectFile *module_obj_file = module_sp->GetObjectFile();49if (module_obj_file != objfile_sp.get()) {50// Make sure the main object file's sections are created51module_obj_file->GetSectionList();52objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList());53}54}5556// TODO: Load any plug-ins in the appropriate plug-in search paths and57// iterate over all of them to find the best one for the job.5859uint32_t best_symfile_abilities = 0;6061SymbolFileCreateInstance create_callback;62for (uint32_t idx = 0;63(create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(64idx)) != nullptr;65++idx) {66std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));6768if (curr_symfile_up) {69const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();70if (sym_file_abilities > best_symfile_abilities) {71best_symfile_abilities = sym_file_abilities;72best_symfile_up.reset(curr_symfile_up.release());73// If any symbol file parser has all of the abilities, then we should74// just stop looking.75if ((kAllAbilities & sym_file_abilities) == kAllAbilities)76break;77}78}79}80if (best_symfile_up) {81// If symbol on-demand is enabled the winning symbol file parser is82// wrapped with SymbolFileOnDemand so that hydration of the debug info83// can be controlled to improve performance.84//85// Currently the supported on-demand symbol files include:86// executables, shared libraries and debug info files.87//88// To reduce unnecessary wrapping files with zero debug abilities are89// skipped.90ObjectFile::Type obj_file_type = objfile_sp->CalculateType();91if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&92best_symfile_abilities > 0 &&93(obj_file_type == ObjectFile::eTypeExecutable ||94obj_file_type == ObjectFile::eTypeSharedLibrary ||95obj_file_type == ObjectFile::eTypeDebugInfo)) {96best_symfile_up =97std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));98}99// Let the winning symbol file parser initialize itself more completely100// now that it has been chosen101best_symfile_up->InitializeObject();102}103}104return best_symfile_up.release();105}106107uint32_t108SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,109lldb::SymbolContextItem resolve_scope,110SymbolContextList &sc_list) {111return 0;112}113114void SymbolFile::FindGlobalVariables(ConstString name,115const CompilerDeclContext &parent_decl_ctx,116uint32_t max_matches,117VariableList &variables) {}118119void SymbolFile::FindGlobalVariables(const RegularExpression ®ex,120uint32_t max_matches,121VariableList &variables) {}122123void SymbolFile::FindFunctions(const Module::LookupInfo &lookup_info,124const CompilerDeclContext &parent_decl_ctx,125bool include_inlines,126SymbolContextList &sc_list) {}127128void SymbolFile::FindFunctions(const RegularExpression ®ex,129bool include_inlines,130SymbolContextList &sc_list) {}131132void SymbolFile::GetMangledNamesForFunction(133const std::string &scope_qualified_name,134std::vector<ConstString> &mangled_names) {}135136void SymbolFile::AssertModuleLock() {137// The code below is too expensive to leave enabled in release builds. It's138// enabled in debug builds or when the correct macro is set.139#if defined(LLDB_CONFIGURATION_DEBUG)140// We assert that we have to module lock by trying to acquire the lock from a141// different thread. Note that we must abort if the result is true to142// guarantee correctness.143assert(std::async(144std::launch::async,145[this] {146return this->GetModuleMutex().try_lock();147}).get() == false &&148"Module is not locked");149#endif150}151152SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;153154Symtab *SymbolFileCommon::GetSymtab() {155std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());156// Fetch the symtab from the main object file.157auto *symtab = GetMainObjectFile()->GetSymtab();158if (m_symtab != symtab) {159m_symtab = symtab;160161// Then add our symbols to it.162if (m_symtab)163AddSymbols(*m_symtab);164}165return m_symtab;166}167168ObjectFile *SymbolFileCommon::GetMainObjectFile() {169return m_objfile_sp->GetModule()->GetObjectFile();170}171172void SymbolFileCommon::SectionFileAddressesChanged() {173ObjectFile *module_objfile = GetMainObjectFile();174ObjectFile *symfile_objfile = GetObjectFile();175if (symfile_objfile != module_objfile)176symfile_objfile->SectionFileAddressesChanged();177if (auto *symtab = GetSymtab())178symtab->SectionFileAddressesChanged();179}180181uint32_t SymbolFileCommon::GetNumCompileUnits() {182std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());183if (!m_compile_units) {184// Create an array of compile unit shared pointers -- which will each185// remain NULL until someone asks for the actual compile unit information.186m_compile_units.emplace(CalculateNumCompileUnits());187}188return m_compile_units->size();189}190191CompUnitSP SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx) {192std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());193uint32_t num = GetNumCompileUnits();194if (idx >= num)195return nullptr;196lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];197if (!cu_sp)198cu_sp = ParseCompileUnitAtIndex(idx);199return cu_sp;200}201202void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx,203const CompUnitSP &cu_sp) {204std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());205const size_t num_compile_units = GetNumCompileUnits();206assert(idx < num_compile_units);207UNUSED_IF_ASSERT_DISABLED(num_compile_units);208209// Fire off an assertion if this compile unit already exists for now. The210// partial parsing should take care of only setting the compile unit211// once, so if this assertion fails, we need to make sure that we don't212// have a race condition, or have a second parse of the same compile213// unit.214assert((*m_compile_units)[idx] == nullptr);215(*m_compile_units)[idx] = cu_sp;216}217218llvm::Expected<TypeSystemSP>219SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language) {220auto type_system_or_err =221m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);222if (type_system_or_err) {223if (auto ts = *type_system_or_err)224ts->SetSymbolFile(this);225}226return type_system_or_err;227}228229uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {230if (!m_objfile_sp)231return 0;232ModuleSP module_sp(m_objfile_sp->GetModule());233if (!module_sp)234return 0;235const SectionList *section_list = module_sp->GetSectionList();236if (section_list)237return section_list->GetDebugInfoSize();238return 0;239}240241void SymbolFileCommon::Dump(Stream &s) {242s.Format("SymbolFile {0} ({1})\n", GetPluginName(),243GetMainObjectFile()->GetFileSpec());244s.PutCString("Types:\n");245m_type_list.Dump(&s, /*show_context*/ false);246s.PutChar('\n');247248s.PutCString("Compile units:\n");249if (m_compile_units) {250for (const CompUnitSP &cu_sp : *m_compile_units) {251// We currently only dump the compile units that have been parsed252if (cu_sp)253cu_sp->Dump(&s, /*show_context*/ false);254}255}256s.PutChar('\n');257258if (Symtab *symtab = GetSymtab())259symtab->Dump(&s, nullptr, eSortOrderNone);260}261262263