268 lines · cpp
1//===-- SymbolFile.cpp ----------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Symbol/SymbolFile.h"10 11#include "lldb/Core/Module.h"12#include "lldb/Core/PluginManager.h"13#include "lldb/Symbol/CompileUnit.h"14#include "lldb/Symbol/ObjectFile.h"15#include "lldb/Symbol/SymbolFileOnDemand.h"16#include "lldb/Symbol/TypeMap.h"17#include "lldb/Symbol/TypeSystem.h"18#include "lldb/Symbol/VariableList.h"19#include "lldb/Utility/Log.h"20#include "lldb/Utility/StreamString.h"21#include "lldb/Utility/StructuredData.h"22#include "lldb/lldb-private.h"23 24#include <future>25 26using namespace lldb_private;27using namespace lldb;28 29char SymbolFile::ID;30char SymbolFileCommon::ID;31 32void SymbolFile::PreloadSymbols() {33 // No-op for most implementations.34}35 36std::recursive_mutex &SymbolFile::GetModuleMutex() const {37 return GetObjectFile()->GetModule()->GetMutex();38}39 40SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {41 std::unique_ptr<SymbolFile> best_symfile_up;42 if (objfile_sp != nullptr) {43 44 // We need to test the abilities of this section list. So create what it45 // would be with this new objfile_sp.46 lldb::ModuleSP module_sp(objfile_sp->GetModule());47 if (module_sp) {48 // Default to the main module section list.49 ObjectFile *module_obj_file = module_sp->GetObjectFile();50 if (module_obj_file != objfile_sp.get()) {51 // Make sure the main object file's sections are created52 module_obj_file->GetSectionList();53 objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList());54 }55 }56 57 // TODO: Load any plug-ins in the appropriate plug-in search paths and58 // iterate over all of them to find the best one for the job.59 60 uint32_t best_symfile_abilities = 0;61 62 SymbolFileCreateInstance create_callback;63 for (uint32_t idx = 0;64 (create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(65 idx)) != nullptr;66 ++idx) {67 std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));68 69 if (curr_symfile_up) {70 const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();71 if (sym_file_abilities > best_symfile_abilities) {72 best_symfile_abilities = sym_file_abilities;73 best_symfile_up.reset(curr_symfile_up.release());74 // If any symbol file parser has all of the abilities, then we should75 // just stop looking.76 if ((kAllAbilities & sym_file_abilities) == kAllAbilities)77 break;78 }79 }80 }81 if (best_symfile_up) {82 // If symbol on-demand is enabled the winning symbol file parser is83 // wrapped with SymbolFileOnDemand so that hydration of the debug info84 // can be controlled to improve performance.85 //86 // Currently the supported on-demand symbol files include:87 // executables, shared libraries and debug info files.88 //89 // To reduce unnecessary wrapping files with zero debug abilities are90 // skipped.91 ObjectFile::Type obj_file_type = objfile_sp->CalculateType();92 if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&93 best_symfile_abilities > 0 &&94 (obj_file_type == ObjectFile::eTypeExecutable ||95 obj_file_type == ObjectFile::eTypeSharedLibrary ||96 obj_file_type == ObjectFile::eTypeDebugInfo)) {97 best_symfile_up =98 std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));99 }100 // Let the winning symbol file parser initialize itself more completely101 // now that it has been chosen102 best_symfile_up->InitializeObject();103 }104 }105 return best_symfile_up.release();106}107 108uint32_t109SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,110 lldb::SymbolContextItem resolve_scope,111 SymbolContextList &sc_list) {112 return 0;113}114 115void SymbolFile::FindGlobalVariables(ConstString name,116 const CompilerDeclContext &parent_decl_ctx,117 uint32_t max_matches,118 VariableList &variables) {}119 120void SymbolFile::FindGlobalVariables(const RegularExpression ®ex,121 uint32_t max_matches,122 VariableList &variables) {}123 124void SymbolFile::FindFunctions(const Module::LookupInfo &lookup_info,125 const CompilerDeclContext &parent_decl_ctx,126 bool include_inlines,127 SymbolContextList &sc_list) {}128 129void SymbolFile::FindFunctions(const RegularExpression ®ex,130 bool include_inlines,131 SymbolContextList &sc_list) {}132 133void SymbolFile::GetMangledNamesForFunction(134 const std::string &scope_qualified_name,135 std::vector<ConstString> &mangled_names) {}136 137void SymbolFile::AssertModuleLock() {138 // The code below is too expensive to leave enabled in release builds. It's139 // enabled in debug builds or when the correct macro is set.140#if defined(LLDB_CONFIGURATION_DEBUG)141 // We assert that we have to module lock by trying to acquire the lock from a142 // different thread. Note that we must abort if the result is true to143 // guarantee correctness.144 assert(std::async(145 std::launch::async,146 [this] {147 return this->GetModuleMutex().try_lock();148 }).get() == false &&149 "Module is not locked");150#endif151}152 153SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;154 155Symtab *SymbolFileCommon::GetSymtab(bool can_create) {156 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());157 // Fetch the symtab from the main object file.158 auto *symtab = GetMainObjectFile()->GetSymtab(can_create);159 if (m_symtab != symtab) {160 m_symtab = symtab;161 162 // Then add our symbols to it.163 if (m_symtab)164 AddSymbols(*m_symtab);165 }166 return m_symtab;167}168 169ObjectFile *SymbolFileCommon::GetMainObjectFile() {170 return m_objfile_sp->GetModule()->GetObjectFile();171}172 173void SymbolFileCommon::SectionFileAddressesChanged() {174 ObjectFile *module_objfile = GetMainObjectFile();175 ObjectFile *symfile_objfile = GetObjectFile();176 if (symfile_objfile != module_objfile)177 symfile_objfile->SectionFileAddressesChanged();178 if (auto *symtab = GetSymtab())179 symtab->SectionFileAddressesChanged();180}181 182uint32_t SymbolFileCommon::GetNumCompileUnits() {183 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());184 if (!m_compile_units) {185 // Create an array of compile unit shared pointers -- which will each186 // remain NULL until someone asks for the actual compile unit information.187 m_compile_units.emplace(CalculateNumCompileUnits());188 }189 return m_compile_units->size();190}191 192CompUnitSP SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx) {193 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());194 uint32_t num = GetNumCompileUnits();195 if (idx >= num)196 return nullptr;197 lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];198 if (!cu_sp)199 cu_sp = ParseCompileUnitAtIndex(idx);200 return cu_sp;201}202 203void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx,204 const CompUnitSP &cu_sp) {205 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());206 const size_t num_compile_units = GetNumCompileUnits();207 assert(idx < num_compile_units);208 UNUSED_IF_ASSERT_DISABLED(num_compile_units);209 210 // Fire off an assertion if this compile unit already exists for now. The211 // partial parsing should take care of only setting the compile unit212 // once, so if this assertion fails, we need to make sure that we don't213 // have a race condition, or have a second parse of the same compile214 // unit.215 assert((*m_compile_units)[idx] == nullptr);216 (*m_compile_units)[idx] = cu_sp;217}218 219llvm::Expected<TypeSystemSP>220SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language) {221 auto type_system_or_err =222 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);223 if (type_system_or_err) {224 if (auto ts = *type_system_or_err)225 ts->SetSymbolFile(this);226 }227 return type_system_or_err;228}229 230uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {231 if (!m_objfile_sp)232 return 0;233 ModuleSP module_sp(m_objfile_sp->GetModule());234 if (!module_sp)235 return 0;236 const SectionList *section_list = module_sp->GetSectionList();237 if (section_list)238 return section_list->GetDebugInfoSize();239 return 0;240}241 242void SymbolFileCommon::Dump(Stream &s) {243 s.Format("SymbolFile {0} ({1})\n", GetPluginName(),244 GetMainObjectFile()->GetFileSpec());245 s.PutCString("Types:\n");246 m_type_list.Dump(&s, /*show_context*/ false);247 s.PutChar('\n');248 249 s.PutCString("Compile units:\n");250 if (m_compile_units) {251 for (const CompUnitSP &cu_sp : *m_compile_units) {252 // We currently only dump the compile units that have been parsed253 if (cu_sp)254 cu_sp->Dump(&s, /*show_context*/ false);255 }256 }257 s.PutChar('\n');258 259 if (Symtab *symtab = GetSymtab())260 symtab->Dump(&s, nullptr, eSortOrderNone);261}262 263std::string SymbolFile::GetObjectName() const {264 if (const ObjectFile *object_file = GetObjectFile())265 return object_file->GetObjectName();266 return "";267}268