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::AssertModuleLock() {
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.
147 return this->GetModuleMutex().try_lock();
149 "Module is not locked");
153 SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;
155 Symtab
*SymbolFileCommon::GetSymtab() {
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
) {
162 // Then add our symbols to it.
164 AddSymbols(*m_symtab
);
169 ObjectFile
*SymbolFileCommon::GetMainObjectFile() {
170 return m_objfile_sp
->GetModule()->GetObjectFile();
173 void SymbolFileCommon::SectionFileAddressesChanged() {
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();
182 uint32_t SymbolFileCommon::GetNumCompileUnits() {
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.
187 m_compile_units
.emplace(CalculateNumCompileUnits());
189 return m_compile_units
->size();
192 CompUnitSP
SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx
) {
193 std::lock_guard
<std::recursive_mutex
> guard(GetModuleMutex());
194 uint32_t num
= GetNumCompileUnits();
197 lldb::CompUnitSP
&cu_sp
= (*m_compile_units
)[idx
];
199 cu_sp
= ParseCompileUnitAtIndex(idx
);
203 void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx
,
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
);
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
215 assert((*m_compile_units
)[idx
] == nullptr);
216 (*m_compile_units
)[idx
] = cu_sp
;
219 llvm::Expected
<TypeSystemSP
>
220 SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language
) {
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);
227 return type_system_or_err
;
230 uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info
) {
233 ModuleSP
module_sp(m_objfile_sp
->GetModule());
236 const SectionList
*section_list
= module_sp
->GetSectionList();
238 return section_list
->GetDebugInfoSize();
242 void SymbolFileCommon::Dump(Stream
&s
) {
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);
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
254 cu_sp
->Dump(&s
, /*show_context*/ false);
259 if (Symtab
*symtab
= GetSymtab())
260 symtab
->Dump(&s
, nullptr, eSortOrderNone
);