1 //===-- SymbolFile.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Symbol/SymbolFile.h"
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"
26 using namespace lldb_private
;
30 char SymbolFileCommon::ID
;
32 void SymbolFile::PreloadSymbols() {
33 // No-op for most implementations.
36 std::recursive_mutex
&SymbolFile::GetModuleMutex() const {
37 return GetObjectFile()->GetModule()->GetMutex();
40 SymbolFile
*SymbolFile::FindPlugin(ObjectFileSP objfile_sp
) {
41 std::unique_ptr
<SymbolFile
> best_symfile_up
;
42 if (objfile_sp
!= nullptr) {
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());
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());
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.
60 uint32_t best_symfile_abilities
= 0;
62 SymbolFileCreateInstance create_callback
;
63 for (uint32_t idx
= 0;
64 (create_callback
= PluginManager::GetSymbolFileCreateCallbackAtIndex(
67 std::unique_ptr
<SymbolFile
> curr_symfile_up(create_callback(objfile_sp
));
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
76 if ((kAllAbilities
& sym_file_abilities
) == kAllAbilities
)
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.
86 // Currently the supported on-demand symbol files include:
87 // executables, shared libraries and debug info files.
89 // To reduce unnecessary wrapping files with zero debug abilities are
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
)) {
98 std::make_unique
<SymbolFileOnDemand
>(std::move(best_symfile_up
));
100 // Let the winning symbol file parser initialize itself more completely
101 // now that it has been chosen
102 best_symfile_up
->InitializeObject();
105 return best_symfile_up
.release();
109 SymbolFile::ResolveSymbolContext(const SourceLocationSpec
&src_location_spec
,
110 lldb::SymbolContextItem resolve_scope
,
111 SymbolContextList
&sc_list
) {
115 void SymbolFile::FindGlobalVariables(ConstString name
,
116 const CompilerDeclContext
&parent_decl_ctx
,
117 uint32_t max_matches
,
118 VariableList
&variables
) {}
120 void SymbolFile::FindGlobalVariables(const RegularExpression
®ex
,
121 uint32_t max_matches
,
122 VariableList
&variables
) {}
124 void SymbolFile::FindFunctions(const Module::LookupInfo
&lookup_info
,
125 const CompilerDeclContext
&parent_decl_ctx
,
126 bool include_inlines
,
127 SymbolContextList
&sc_list
) {}
129 void SymbolFile::FindFunctions(const RegularExpression
®ex
,
130 bool include_inlines
,
131 SymbolContextList
&sc_list
) {}
133 void SymbolFile::GetMangledNamesForFunction(
134 const std::string
&scope_qualified_name
,
135 std::vector
<ConstString
> &mangled_names
) {}
137 void SymbolFile::FindTypes(
138 ConstString name
, const CompilerDeclContext
&parent_decl_ctx
,
139 uint32_t max_matches
,
140 llvm::DenseSet
<lldb_private::SymbolFile
*> &searched_symbol_files
,
143 void SymbolFile::FindTypes(llvm::ArrayRef
<CompilerContext
> pattern
,
144 LanguageSet languages
,
145 llvm::DenseSet
<SymbolFile
*> &searched_symbol_files
,
148 void SymbolFile::AssertModuleLock() {
149 // The code below is too expensive to leave enabled in release builds. It's
150 // enabled in debug builds or when the correct macro is set.
151 #if defined(LLDB_CONFIGURATION_DEBUG)
152 // We assert that we have to module lock by trying to acquire the lock from a
153 // different thread. Note that we must abort if the result is true to
154 // guarantee correctness.
158 return this->GetModuleMutex().try_lock();
160 "Module is not locked");
164 SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;
166 Symtab
*SymbolFileCommon::GetSymtab() {
167 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
168 // Fetch the symtab from the main object file.
169 auto *symtab
= GetMainObjectFile()->GetSymtab();
170 if (m_symtab
!= symtab
) {
173 // Then add our symbols to it.
175 AddSymbols(*m_symtab
);
180 ObjectFile
*SymbolFileCommon::GetMainObjectFile() {
181 return m_objfile_sp
->GetModule()->GetObjectFile();
184 void SymbolFileCommon::SectionFileAddressesChanged() {
185 ObjectFile
*module_objfile
= GetMainObjectFile();
186 ObjectFile
*symfile_objfile
= GetObjectFile();
187 if (symfile_objfile
!= module_objfile
)
188 symfile_objfile
->SectionFileAddressesChanged();
189 if (auto *symtab
= GetSymtab())
190 symtab
->SectionFileAddressesChanged();
193 uint32_t SymbolFileCommon::GetNumCompileUnits() {
194 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
195 if (!m_compile_units
) {
196 // Create an array of compile unit shared pointers -- which will each
197 // remain NULL until someone asks for the actual compile unit information.
198 m_compile_units
.emplace(CalculateNumCompileUnits());
200 return m_compile_units
->size();
203 CompUnitSP
SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx
) {
204 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
205 uint32_t num
= GetNumCompileUnits();
208 lldb::CompUnitSP
&cu_sp
= (*m_compile_units
)[idx
];
210 cu_sp
= ParseCompileUnitAtIndex(idx
);
214 void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx
,
215 const CompUnitSP
&cu_sp
) {
216 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
217 const size_t num_compile_units
= GetNumCompileUnits();
218 assert(idx
< num_compile_units
);
219 (void)num_compile_units
;
221 // Fire off an assertion if this compile unit already exists for now. The
222 // partial parsing should take care of only setting the compile unit
223 // once, so if this assertion fails, we need to make sure that we don't
224 // have a race condition, or have a second parse of the same compile
226 assert((*m_compile_units
)[idx
] == nullptr);
227 (*m_compile_units
)[idx
] = cu_sp
;
230 llvm::Expected
<TypeSystemSP
>
231 SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language
) {
232 auto type_system_or_err
=
233 m_objfile_sp
->GetModule()->GetTypeSystemForLanguage(language
);
234 if (type_system_or_err
) {
235 if (auto ts
= *type_system_or_err
)
236 ts
->SetSymbolFile(this);
238 return type_system_or_err
;
241 uint64_t SymbolFileCommon::GetDebugInfoSize() {
244 ModuleSP
module_sp(m_objfile_sp
->GetModule());
247 const SectionList
*section_list
= module_sp
->GetSectionList();
249 return section_list
->GetDebugInfoSize();
253 void SymbolFileCommon::Dump(Stream
&s
) {
254 s
.Format("SymbolFile {0} ({1})\n", GetPluginName(),
255 GetMainObjectFile()->GetFileSpec());
256 s
.PutCString("Types:\n");
257 m_type_list
.Dump(&s
, /*show_context*/ false);
260 s
.PutCString("Compile units:\n");
261 if (m_compile_units
) {
262 for (const CompUnitSP
&cu_sp
: *m_compile_units
) {
263 // We currently only dump the compile units that have been parsed
265 cu_sp
->Dump(&s
, /*show_context*/ false);
270 if (Symtab
*symtab
= GetSymtab())
271 symtab
->Dump(&s
, nullptr, eSortOrderNone
);