Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / source / Symbol / SymbolFile.cpp
blob7dcee8ced0ea11b1a79bc16e21e7c2e4a2a0f0ef
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 //===----------------------------------------------------------------------===//
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"
24 #include <future>
26 using namespace lldb_private;
27 using namespace lldb;
29 char SymbolFile::ID;
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());
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());
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(
65 idx)) != nullptr;
66 ++idx) {
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
75 // just stop looking.
76 if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
77 break;
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
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));
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();
108 uint32_t
109 SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
110 lldb::SymbolContextItem resolve_scope,
111 SymbolContextList &sc_list) {
112 return 0;
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 &regex,
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 &regex,
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,
141 TypeMap &types) {}
143 void SymbolFile::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
144 LanguageSet languages,
145 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
146 TypeMap &types) {}
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.
155 assert(std::async(
156 std::launch::async,
157 [this] {
158 return this->GetModuleMutex().try_lock();
159 }).get() == false &&
160 "Module is not locked");
161 #endif
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) {
171 m_symtab = symtab;
173 // Then add our symbols to it.
174 if (m_symtab)
175 AddSymbols(*m_symtab);
177 return 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();
206 if (idx >= num)
207 return nullptr;
208 lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
209 if (!cu_sp)
210 cu_sp = ParseCompileUnitAtIndex(idx);
211 return cu_sp;
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
225 // unit.
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() {
242 if (!m_objfile_sp)
243 return 0;
244 ModuleSP module_sp(m_objfile_sp->GetModule());
245 if (!module_sp)
246 return 0;
247 const SectionList *section_list = module_sp->GetSectionList();
248 if (section_list)
249 return section_list->GetDebugInfoSize();
250 return 0;
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);
258 s.PutChar('\n');
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
264 if (cu_sp)
265 cu_sp->Dump(&s, /*show_context*/ false);
268 s.PutChar('\n');
270 if (Symtab *symtab = GetSymtab())
271 symtab->Dump(&s, nullptr, eSortOrderNone);