llvm-project
261 строка · 9.2 Кб
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-exception
6//
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 {37return GetObjectFile()->GetModule()->GetMutex();38}
39
40SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {41std::unique_ptr<SymbolFile> best_symfile_up;42if (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.46lldb::ModuleSP module_sp(objfile_sp->GetModule());47if (module_sp) {48// Default to the main module section list.49ObjectFile *module_obj_file = module_sp->GetObjectFile();50if (module_obj_file != objfile_sp.get()) {51// Make sure the main object file's sections are created52module_obj_file->GetSectionList();53objfile_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
60uint32_t best_symfile_abilities = 0;61
62SymbolFileCreateInstance create_callback;63for (uint32_t idx = 0;64(create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(65idx)) != nullptr;66++idx) {67std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));68
69if (curr_symfile_up) {70const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();71if (sym_file_abilities > best_symfile_abilities) {72best_symfile_abilities = sym_file_abilities;73best_symfile_up.reset(curr_symfile_up.release());74// If any symbol file parser has all of the abilities, then we should75// just stop looking.76if ((kAllAbilities & sym_file_abilities) == kAllAbilities)77break;78}79}80}81if (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.91ObjectFile::Type obj_file_type = objfile_sp->CalculateType();92if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&93best_symfile_abilities > 0 &&94(obj_file_type == ObjectFile::eTypeExecutable ||95obj_file_type == ObjectFile::eTypeSharedLibrary ||96obj_file_type == ObjectFile::eTypeDebugInfo)) {97best_symfile_up =98std::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 chosen102best_symfile_up->InitializeObject();103}104}105return best_symfile_up.release();106}
107
108uint32_t
109SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,110lldb::SymbolContextItem resolve_scope,111SymbolContextList &sc_list) {112return 0;113}
114
115void SymbolFile::FindGlobalVariables(ConstString name,116const CompilerDeclContext &parent_decl_ctx,117uint32_t max_matches,118VariableList &variables) {}119
120void SymbolFile::FindGlobalVariables(const RegularExpression ®ex,121uint32_t max_matches,122VariableList &variables) {}123
124void SymbolFile::FindFunctions(const Module::LookupInfo &lookup_info,125const CompilerDeclContext &parent_decl_ctx,126bool include_inlines,127SymbolContextList &sc_list) {}128
129void SymbolFile::FindFunctions(const RegularExpression ®ex,130bool include_inlines,131SymbolContextList &sc_list) {}132
133void SymbolFile::GetMangledNamesForFunction(134const std::string &scope_qualified_name,135std::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.144assert(std::async(145std::launch::async,146[this] {147return this->GetModuleMutex().try_lock();148}).get() == false &&149"Module is not locked");150#endif151}
152
153SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;154
155Symtab *SymbolFileCommon::GetSymtab() {156std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());157// Fetch the symtab from the main object file.158auto *symtab = GetMainObjectFile()->GetSymtab();159if (m_symtab != symtab) {160m_symtab = symtab;161
162// Then add our symbols to it.163if (m_symtab)164AddSymbols(*m_symtab);165}166return m_symtab;167}
168
169ObjectFile *SymbolFileCommon::GetMainObjectFile() {170return m_objfile_sp->GetModule()->GetObjectFile();171}
172
173void SymbolFileCommon::SectionFileAddressesChanged() {174ObjectFile *module_objfile = GetMainObjectFile();175ObjectFile *symfile_objfile = GetObjectFile();176if (symfile_objfile != module_objfile)177symfile_objfile->SectionFileAddressesChanged();178if (auto *symtab = GetSymtab())179symtab->SectionFileAddressesChanged();180}
181
182uint32_t SymbolFileCommon::GetNumCompileUnits() {183std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());184if (!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.187m_compile_units.emplace(CalculateNumCompileUnits());188}189return m_compile_units->size();190}
191
192CompUnitSP SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx) {193std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());194uint32_t num = GetNumCompileUnits();195if (idx >= num)196return nullptr;197lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];198if (!cu_sp)199cu_sp = ParseCompileUnitAtIndex(idx);200return cu_sp;201}
202
203void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx,204const CompUnitSP &cu_sp) {205std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());206const size_t num_compile_units = GetNumCompileUnits();207assert(idx < num_compile_units);208UNUSED_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.215assert((*m_compile_units)[idx] == nullptr);216(*m_compile_units)[idx] = cu_sp;217}
218
219llvm::Expected<TypeSystemSP>220SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language) {221auto type_system_or_err =222m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);223if (type_system_or_err) {224if (auto ts = *type_system_or_err)225ts->SetSymbolFile(this);226}227return type_system_or_err;228}
229
230uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {231if (!m_objfile_sp)232return 0;233ModuleSP module_sp(m_objfile_sp->GetModule());234if (!module_sp)235return 0;236const SectionList *section_list = module_sp->GetSectionList();237if (section_list)238return section_list->GetDebugInfoSize();239return 0;240}
241
242void SymbolFileCommon::Dump(Stream &s) {243s.Format("SymbolFile {0} ({1})\n", GetPluginName(),244GetMainObjectFile()->GetFileSpec());245s.PutCString("Types:\n");246m_type_list.Dump(&s, /*show_context*/ false);247s.PutChar('\n');248
249s.PutCString("Compile units:\n");250if (m_compile_units) {251for (const CompUnitSP &cu_sp : *m_compile_units) {252// We currently only dump the compile units that have been parsed253if (cu_sp)254cu_sp->Dump(&s, /*show_context*/ false);255}256}257s.PutChar('\n');258
259if (Symtab *symtab = GetSymtab())260symtab->Dump(&s, nullptr, eSortOrderNone);261}
262