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 SymbolFileCreateInstance create_callback;
63 for (uint32_t idx = 0;
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 should
75 // 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 is
83 // wrapped with SymbolFileOnDemand so that hydration of the debug info
84 // 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 are
90 // 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 completely
101 // now that it has been chosen
102 best_symfile_up->InitializeObject();
103 }
104 }
105 return best_symfile_up.release();
106}
107
108uint32_t
110 lldb::SymbolContextItem resolve_scope,
111 SymbolContextList &sc_list) {
112 return 0;
113}
114
116 const CompilerDeclContext &parent_decl_ctx,
117 uint32_t max_matches,
118 VariableList &variables) {}
119
121 uint32_t max_matches,
122 VariableList &variables) {}
123
125 const CompilerDeclContext &parent_decl_ctx,
126 bool include_inlines,
127 SymbolContextList &sc_list) {}
128
129void SymbolFile::FindFunctions(llvm::ArrayRef<Module::LookupInfo> lookup_infos,
130 const CompilerDeclContext &parent_decl_ctx,
131 bool include_inlines,
132 SymbolContextList &sc_list) {
133 for (const auto &lookup_info : lookup_infos)
134 FindFunctions(lookup_info, parent_decl_ctx, include_inlines, sc_list);
135}
136
138 bool include_inlines,
139 SymbolContextList &sc_list) {}
140
142 const std::string &scope_qualified_name,
143 std::vector<ConstString> &mangled_names) {}
144
146 // The code below is too expensive to leave enabled in release builds. It's
147 // enabled in debug builds or when the correct macro is set.
148#if defined(LLDB_CONFIGURATION_DEBUG)
149 // We assert that we have to module lock by trying to acquire the lock from a
150 // different thread. Note that we must abort if the result is true to
151 // guarantee correctness.
152 assert(std::async(
153 std::launch::async,
154 [this] {
155 return this->GetModuleMutex().try_lock();
156 }).get() == false &&
157 "Module is not locked");
158#endif
159}
160
162
164 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
165 // Fetch the symtab from the main object file.
166 auto *symtab = GetMainObjectFile()->GetSymtab(can_create);
167 if (m_symtab != symtab) {
168 m_symtab = symtab;
169
170 // Then add our symbols to it.
171 if (m_symtab)
173 }
174 return m_symtab;
175}
176
178 return m_objfile_sp->GetModule()->GetObjectFile();
179}
180
182 ObjectFile *module_objfile = GetMainObjectFile();
183 ObjectFile *symfile_objfile = GetObjectFile();
184 if (symfile_objfile != module_objfile)
185 symfile_objfile->SectionFileAddressesChanged();
186 if (auto *symtab = GetSymtab())
187 symtab->SectionFileAddressesChanged();
188}
189
191 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
192 if (!m_compile_units) {
193 // Create an array of compile unit shared pointers -- which will each
194 // remain NULL until someone asks for the actual compile unit information.
196 }
197 return m_compile_units->size();
198}
199
201 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
202 uint32_t num = GetNumCompileUnits();
203 if (idx >= num)
204 return nullptr;
205 lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
206 if (!cu_sp)
207 cu_sp = ParseCompileUnitAtIndex(idx);
208 return cu_sp;
209}
210
212 const CompUnitSP &cu_sp) {
213 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
214 const size_t num_compile_units = GetNumCompileUnits();
215 assert(idx < num_compile_units);
216 UNUSED_IF_ASSERT_DISABLED(num_compile_units);
217
218 // Fire off an assertion if this compile unit already exists for now. The
219 // partial parsing should take care of only setting the compile unit
220 // once, so if this assertion fails, we need to make sure that we don't
221 // have a race condition, or have a second parse of the same compile
222 // unit.
223 assert((*m_compile_units)[idx] == nullptr);
224 (*m_compile_units)[idx] = cu_sp;
225}
226
227llvm::Expected<TypeSystemSP>
229 auto type_system_or_err =
230 m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
231 if (type_system_or_err) {
232 if (auto ts = *type_system_or_err)
233 ts->SetSymbolFile(this);
234 }
235 return type_system_or_err;
236}
237
238uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {
239 if (!m_objfile_sp)
240 return 0;
241 ModuleSP module_sp(m_objfile_sp->GetModule());
242 if (!module_sp)
243 return 0;
244 const SectionList *section_list = module_sp->GetSectionList();
245 if (section_list)
246 return section_list->GetDebugInfoSize();
247 return 0;
248}
249
251 s.Format("SymbolFile {0} ({1})\n", GetPluginName(),
252 GetMainObjectFile()->GetFileSpec());
253 s.PutCString("Types:\n");
254 m_type_list.Dump(&s, /*show_context*/ false);
255 s.PutChar('\n');
256
257 s.PutCString("Compile units:\n");
258 if (m_compile_units) {
259 for (const CompUnitSP &cu_sp : *m_compile_units) {
260 // We currently only dump the compile units that have been parsed
261 if (cu_sp)
262 cu_sp->Dump(&s, /*show_context*/ false);
263 }
264 }
265 s.PutChar('\n');
266
267 if (Symtab *symtab = GetSymtab())
268 symtab->Dump(&s, nullptr, eSortOrderNone);
269}
270
271std::string SymbolFile::GetObjectName() const {
272 if (const ObjectFile *object_file = GetObjectFile())
273 return object_file->GetObjectName();
274 return "";
275}
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 SymbolFileCreateInstance GetSymbolFileCreateCallbackAtIndex(uint32_t idx)
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition Section.cpp:680
"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:569
std::optional< std::vector< lldb::CompUnitSP > > m_compile_units
Definition SymbolFile.h:649
lldb::ObjectFileSP m_objfile_sp
Definition SymbolFile.h:645
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:666
static char ID
LLVM RTTI support.
Definition SymbolFile.h:542
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.
SymbolFile *(* SymbolFileCreateInstance)(lldb::ObjectFileSP objfile_sp)
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