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
130 bool include_inlines,
131 SymbolContextList &sc_list) {}
132
134 const std::string &scope_qualified_name,
135 std::vector<ConstString> &mangled_names) {}
136
138 // The code below is too expensive to leave enabled in release builds. It's
139 // 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 a
142 // different thread. Note that we must abort if the result is true to
143 // 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#endif
151}
152
154
156 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
157 // Fetch the symtab from the main object file.
158 auto *symtab = GetMainObjectFile()->GetSymtab();
159 if (m_symtab != symtab) {
160 m_symtab = symtab;
161
162 // Then add our symbols to it.
163 if (m_symtab)
165 }
166 return m_symtab;
167}
168
170 return m_objfile_sp->GetModule()->GetObjectFile();
171}
172
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
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 each
186 // remain NULL until someone asks for the actual compile unit information.
188 }
189 return m_compile_units->size();
190}
191
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
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. The
211 // partial parsing should take care of only setting the compile unit
212 // once, so if this assertion fails, we need to make sure that we don't
213 // have a race condition, or have a second parse of the same compile
214 // unit.
215 assert((*m_compile_units)[idx] == nullptr);
216 (*m_compile_units)[idx] = cu_sp;
217}
218
219llvm::Expected<TypeSystemSP>
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
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 parsed
253 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}
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.
Definition: ModuleChild.cpp:24
static ModuleListProperties & GetGlobalModuleListProperties()
Definition: ModuleList.cpp:763
A class that encapsulates name lookup information.
Definition: Module.h:904
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:44
Symtab * GetSymtab()
Gets the symbol table for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:737
@ eTypeExecutable
A normal executable.
Definition: ObjectFile.h:53
@ eTypeDebugInfo
An object file that contains only debug information.
Definition: ObjectFile.h:55
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition: ObjectFile.h:61
virtual void SectionFileAddressesChanged()
Notify the ObjectFile that the file addresses in the Sections for this module have been changed.
Definition: ObjectFile.h:305
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:600
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:669
"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:353
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
Definition: SymbolFile.cpp:192
ObjectFile * GetObjectFile() override
Definition: SymbolFile.h:533
std::optional< std::vector< lldb::CompUnitSP > > m_compile_units
Definition: SymbolFile.h:613
lldb::ObjectFileSP m_objfile_sp
Definition: SymbolFile.h:609
Symtab * GetSymtab() override
Definition: SymbolFile.cpp:155
virtual lldb::CompUnitSP ParseCompileUnitAtIndex(uint32_t idx)=0
ObjectFile * GetMainObjectFile() override
Definition: SymbolFile.cpp:169
Symtab * m_symtab
Do not use m_symtab directly, as it may be freed.
Definition: SymbolFile.h:630
static char ID
LLVM RTTI support.
Definition: SymbolFile.h:506
void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp)
Definition: SymbolFile.cpp:203
llvm::Expected< lldb::TypeSystemSP > GetTypeSystemForLanguage(lldb::LanguageType language) override
Definition: SymbolFile.cpp:220
uint32_t GetNumCompileUnits() override
Definition: SymbolFile.cpp:182
void SectionFileAddressesChanged() override
Notify the SymbolFile that the file addresses in the Sections for this module have been changed.
Definition: SymbolFile.cpp:173
virtual uint32_t CalculateNumCompileUnits()=0
void Dump(Stream &s) override
Definition: SymbolFile.cpp:242
uint64_t GetDebugInfoSize(bool load_all_debug_info=false) override
Metrics gathering functions.
Definition: SymbolFile.cpp:230
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:50
virtual void PreloadSymbols()
Definition: SymbolFile.cpp:32
virtual void FindGlobalVariables(ConstString name, const CompilerDeclContext &parent_decl_ctx, uint32_t max_matches, VariableList &variables)
Definition: SymbolFile.cpp:115
static SymbolFile * FindPlugin(lldb::ObjectFileSP objfile_sp)
Definition: SymbolFile.cpp:40
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
Definition: SymbolFile.cpp:36
virtual void AddSymbols(Symtab &symtab)
Definition: SymbolFile.h:363
static char ID
LLVM RTTI support.
Definition: SymbolFile.h:52
virtual void FindFunctions(const Module::LookupInfo &lookup_info, const CompilerDeclContext &parent_decl_ctx, bool include_inlines, SymbolContextList &sc_list)
Definition: SymbolFile.cpp:124
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)
Definition: SymbolFile.cpp:133
void Dump(Stream *s, bool show_context)
Definition: TypeList.cpp:94
#define UNUSED_IF_ASSERT_DISABLED(x)
Definition: lldb-defines.h:140
A class that represents a running process on the host machine.
SymbolFile *(* SymbolFileCreateInstance)(lldb::ObjectFileSP objfile_sp)
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
Definition: lldb-forward.h:373
LanguageType
Programming language type.
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:371
std::shared_ptr< lldb_private::CompileUnit > CompUnitSP
Definition: lldb-forward.h:333