LLDB mainline
SymbolFile.cpp
Go to the documentation of this file.
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
10
11#include "lldb/Core/Module.h"
16#include "lldb/Symbol/TypeMap.h"
19#include "lldb/Utility/Log.h"
22#include "lldb/lldb-private.h"
23
24#include <future>
25
26using namespace lldb_private;
27using namespace lldb;
28
31
33 // No-op for most implementations.
34}
35
36std::recursive_mutex &SymbolFile::GetModuleMutex() const {
37 return GetObjectFile()->GetModule()->GetMutex();
38}
39
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 it
45 // 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 created
52 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 and
58 // iterate over all of them to find the best one for the job.
59
60 uint32_t best_symfile_abilities = 0;
61
62 for (auto create_callback : PluginManager::GetSymbolFileCreateCallbacks()) {
63 std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));
64
65 if (curr_symfile_up) {
66 const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();
67 if (sym_file_abilities > best_symfile_abilities) {
68 best_symfile_abilities = sym_file_abilities;
69 best_symfile_up.reset(curr_symfile_up.release());
70 // If any symbol file parser has all of the abilities, then we should
71 // just stop looking.
72 if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
73 break;
74 }
75 }
76 }
77 if (best_symfile_up) {
78 // If symbol on-demand is enabled the winning symbol file parser is
79 // wrapped with SymbolFileOnDemand so that hydration of the debug info
80 // can be controlled to improve performance.
81 //
82 // Currently the supported on-demand symbol files include:
83 // executables, shared libraries and debug info files.
84 //
85 // To reduce unnecessary wrapping files with zero debug abilities are
86 // skipped.
87 ObjectFile::Type obj_file_type = objfile_sp->CalculateType();
88 if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&
89 best_symfile_abilities > 0 &&
90 (obj_file_type == ObjectFile::eTypeExecutable ||
91 obj_file_type == ObjectFile::eTypeSharedLibrary ||
92 obj_file_type == ObjectFile::eTypeDebugInfo)) {
93 best_symfile_up =
94 std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));
95 }
96 // Let the winning symbol file parser initialize itself more completely
97 // now that it has been chosen
98 best_symfile_up->InitializeObject();
99 }
100 }
101 return best_symfile_up.release();
102}
103
104uint32_t
106 lldb::SymbolContextItem resolve_scope,
107 SymbolContextList &sc_list) {
108 return 0;
109}
110
112 const CompilerDeclContext &parent_decl_ctx,
113 uint32_t max_matches,
114 VariableList &variables) {}
115
117 uint32_t max_matches,
118 VariableList &variables) {}
119
121 const CompilerDeclContext &parent_decl_ctx,
122 bool include_inlines,
123 SymbolContextList &sc_list) {}
124
125void SymbolFile::FindFunctions(llvm::ArrayRef<Module::LookupInfo> lookup_infos,
126 const CompilerDeclContext &parent_decl_ctx,
127 bool include_inlines,
128 SymbolContextList &sc_list) {
129 for (const auto &lookup_info : lookup_infos)
130 FindFunctions(lookup_info, parent_decl_ctx, include_inlines, sc_list);
131}
132
134 bool include_inlines,
135 SymbolContextList &sc_list) {}
136
138 const std::string &scope_qualified_name,
139 std::vector<ConstString> &mangled_names) {}
140
142 // The code below is too expensive to leave enabled in release builds. It's
143 // enabled in debug builds or when the correct macro is set.
144#if defined(LLDB_CONFIGURATION_DEBUG)
145 // We assert that we have to module lock by trying to acquire the lock from a
146 // different thread. Note that we must abort if the result is true to
147 // guarantee correctness.
148 assert(std::async(
149 std::launch::async,
150 [this] {
151 return this->GetModuleMutex().try_lock();
152 }).get() == false &&
153 "Module is not locked");
154#endif
155}
156
158
160 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
161 // Fetch the symtab from the main object file.
162 auto *symtab = GetMainObjectFile()->GetSymtab(can_create);
163 if (m_symtab != symtab) {
164 m_symtab = symtab;
165
166 // Then add our symbols to it.
167 if (m_symtab)
169 }
170 return m_symtab;
171}
172
174 return m_objfile_sp->GetModule()->GetObjectFile();
175}
176
178 ObjectFile *module_objfile = GetMainObjectFile();
179 ObjectFile *symfile_objfile = GetObjectFile();
180 if (symfile_objfile != module_objfile)
181 symfile_objfile->SectionFileAddressesChanged();
182 if (auto *symtab = GetSymtab())
183 symtab->SectionFileAddressesChanged();
184}
185
187 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
188 if (!m_compile_units) {
189 // Create an array of compile unit shared pointers -- which will each
190 // remain NULL until someone asks for the actual compile unit information.
192 }
193 return m_compile_units->size();
194}
195
197 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
198 uint32_t num = GetNumCompileUnits();
199 if (idx >= num)
200 return nullptr;
201 lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
202 if (!cu_sp)
203 cu_sp = ParseCompileUnitAtIndex(idx);
204 return cu_sp;
205}
206
208 const CompUnitSP &cu_sp) {
209 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
210 const size_t num_compile_units = GetNumCompileUnits();
211 assert(idx < num_compile_units);
212 UNUSED_IF_ASSERT_DISABLED(num_compile_units);
213
214 // Fire off an assertion if this compile unit already exists for now. The
215 // partial parsing should take care of only setting the compile unit
216 // once, so if this assertion fails, we need to make sure that we don't
217 // have a race condition, or have a second parse of the same compile
218 // unit.
219 assert((*m_compile_units)[idx] == nullptr);
220 (*m_compile_units)[idx] = cu_sp;
221}
222
223llvm::Expected<TypeSystemSP>
225 auto type_system_or_err =
226 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
227 if (type_system_or_err) {
228 if (auto ts = *type_system_or_err)
229 ts->SetSymbolFile(this);
230 }
231 return type_system_or_err;
232}
233
234uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {
235 if (!m_objfile_sp)
236 return 0;
237 ModuleSP module_sp(m_objfile_sp->GetModule());
238 if (!module_sp)
239 return 0;
240 const SectionList *section_list = module_sp->GetSectionList();
241 if (section_list)
242 return section_list->GetDebugInfoSize();
243 return 0;
244}
245
247 s.Format("SymbolFile {0} ({1})\n", GetPluginName(),
248 GetMainObjectFile()->GetFileSpec());
249 s.PutCString("Types:\n");
250 m_type_list.Dump(&s, /*show_context*/ false);
251 s.PutChar('\n');
252
253 s.PutCString("Compile units:\n");
254 if (m_compile_units) {
255 for (const CompUnitSP &cu_sp : *m_compile_units) {
256 // We currently only dump the compile units that have been parsed
257 if (cu_sp)
258 cu_sp->Dump(&s, /*show_context*/ false);
259 }
260 }
261 s.PutChar('\n');
262
263 if (Symtab *symtab = GetSymtab())
264 symtab->Dump(&s, nullptr, eSortOrderNone);
265}
266
267std::string SymbolFile::GetObjectName() const {
268 if (const ObjectFile *object_file = GetObjectFile())
269 return object_file->GetObjectName();
270 return "";
271}
Represents a generic declaration context in a program.
A uniqued constant string class.
Definition ConstString.h:40
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
static ModuleListProperties & GetGlobalModuleListProperties()
A class that encapsulates name lookup information.
Definition Module.h:908
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
Symtab * GetSymtab(bool can_create=true)
Gets the symbol table for the currently selected architecture (and object for archives).
@ eTypeExecutable
A normal executable.
Definition ObjectFile.h:55
@ eTypeDebugInfo
An object file that contains only debug information.
Definition ObjectFile.h:57
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition ObjectFile.h:63
virtual void SectionFileAddressesChanged()
Notify the ObjectFile that the file addresses in the Sections for this module have been changed.
Definition ObjectFile.h:312
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
virtual llvm::StringRef GetPluginName()=0
static llvm::SmallVector< SymbolFileCreateInstance > GetSymbolFileCreateCallbacks()
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition Section.cpp:676
"lldb/Core/SourceLocationSpec.h" A source location specifier class.
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Definition Stream.h:364
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:65
size_t PutChar(char ch)
Definition Stream.cpp:131
Defines a list of symbol context objects.
lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx) override
ObjectFile * GetObjectFile() override
Definition SymbolFile.h:570
std::optional< std::vector< lldb::CompUnitSP > > m_compile_units
Definition SymbolFile.h:650
lldb::ObjectFileSP m_objfile_sp
Definition SymbolFile.h:646
virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx)=0
ObjectFile * GetMainObjectFile() override
Symtab * m_symtab
Do not use m_symtab directly, as it may be freed.
Definition SymbolFile.h:667
static char ID
LLVM RTTI support.
Definition SymbolFile.h:543
void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp)
Symtab * GetSymtab(bool can_create=true) override
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemForLanguage(lldb::LanguageType language) override
uint32_t GetNumCompileUnits() override
void SectionFileAddressesChanged() override
Notify the SymbolFile that the file addresses in the Sections for this module have been changed.
virtual uint32_t CalculateNumCompileUnits()=0
void Dump(Stream &s) override
uint64_t GetDebugInfoSize(bool load_all_debug_info=false) override
Metrics gathering functions.
virtual void PreloadSymbols()
virtual void FindGlobalVariables(ConstString name, const CompilerDeclContext &parent_decl_ctx, uint32_t max_matches, VariableList &variables)
static SymbolFile * FindPlugin(lldb::ObjectFileSP objfile_sp)
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
virtual void AddSymbols(Symtab &symtab)
Definition SymbolFile.h:380
static char ID
LLVM RTTI support.
Definition SymbolFile.h:53
virtual void FindFunctions(const Module::LookupInfo &lookup_info, const CompilerDeclContext &parent_decl_ctx, bool include_inlines, SymbolContextList &sc_list)
virtual ObjectFile * GetObjectFile()=0
virtual uint32_t ResolveSymbolContext(const Address &so_addr, lldb::SymbolContextItem resolve_scope, SymbolContext &sc)=0
virtual void GetMangledNamesForFunction(const std::string &scope_qualified_name, std::vector< ConstString > &mangled_names)
std::string GetObjectName() const
#define UNUSED_IF_ASSERT_DISABLED(x)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Module > ModuleSP
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP