1cfca06d7SDimitry Andric //===-- SymbolFile.cpp ----------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Symbol/SymbolFile.h"
10f034231aSEd Maste
11f034231aSEd Maste #include "lldb/Core/Module.h"
12f034231aSEd Maste #include "lldb/Core/PluginManager.h"
13ead24645SDimitry Andric #include "lldb/Symbol/CompileUnit.h"
14f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
15145449b1SDimitry Andric #include "lldb/Symbol/SymbolFileOnDemand.h"
16e81d9d49SDimitry Andric #include "lldb/Symbol/TypeMap.h"
17e81d9d49SDimitry Andric #include "lldb/Symbol/TypeSystem.h"
18e81d9d49SDimitry Andric #include "lldb/Symbol/VariableList.h"
1974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2074a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
21b1c73532SDimitry Andric #include "lldb/Utility/StructuredData.h"
2214f1b3e8SDimitry Andric #include "lldb/lldb-private.h"
23f034231aSEd Maste
2494994d37SDimitry Andric #include <future>
2594994d37SDimitry Andric
26f034231aSEd Maste using namespace lldb_private;
27ead24645SDimitry Andric using namespace lldb;
28f034231aSEd Maste
29706b4fc4SDimitry Andric char SymbolFile::ID;
30145449b1SDimitry Andric char SymbolFileCommon::ID;
31706b4fc4SDimitry Andric
PreloadSymbols()32773dd0e6SDimitry Andric void SymbolFile::PreloadSymbols() {
33773dd0e6SDimitry Andric // No-op for most implementations.
34773dd0e6SDimitry Andric }
35773dd0e6SDimitry Andric
GetModuleMutex() const3694994d37SDimitry Andric std::recursive_mutex &SymbolFile::GetModuleMutex() const {
3794994d37SDimitry Andric return GetObjectFile()->GetModule()->GetMutex();
3894994d37SDimitry Andric }
3994994d37SDimitry Andric
FindPlugin(ObjectFileSP objfile_sp)40ead24645SDimitry Andric SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {
415f29bb8aSDimitry Andric std::unique_ptr<SymbolFile> best_symfile_up;
42ead24645SDimitry Andric if (objfile_sp != nullptr) {
43f034231aSEd Maste
4414f1b3e8SDimitry Andric // We need to test the abilities of this section list. So create what it
45ead24645SDimitry Andric // would be with this new objfile_sp.
46ead24645SDimitry Andric lldb::ModuleSP module_sp(objfile_sp->GetModule());
4714f1b3e8SDimitry Andric if (module_sp) {
48f034231aSEd Maste // Default to the main module section list.
49f034231aSEd Maste ObjectFile *module_obj_file = module_sp->GetObjectFile();
50ead24645SDimitry Andric if (module_obj_file != objfile_sp.get()) {
51f034231aSEd Maste // Make sure the main object file's sections are created
52f034231aSEd Maste module_obj_file->GetSectionList();
53ead24645SDimitry Andric objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList());
54f034231aSEd Maste }
55f034231aSEd Maste }
56f034231aSEd Maste
57f034231aSEd Maste // TODO: Load any plug-ins in the appropriate plug-in search paths and
58f034231aSEd Maste // iterate over all of them to find the best one for the job.
59f034231aSEd Maste
60f034231aSEd Maste uint32_t best_symfile_abilities = 0;
61f034231aSEd Maste
62f034231aSEd Maste SymbolFileCreateInstance create_callback;
6314f1b3e8SDimitry Andric for (uint32_t idx = 0;
6414f1b3e8SDimitry Andric (create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(
6514f1b3e8SDimitry Andric idx)) != nullptr;
6614f1b3e8SDimitry Andric ++idx) {
67ead24645SDimitry Andric std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));
68f034231aSEd Maste
695f29bb8aSDimitry Andric if (curr_symfile_up) {
705f29bb8aSDimitry Andric const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();
7114f1b3e8SDimitry Andric if (sym_file_abilities > best_symfile_abilities) {
72f034231aSEd Maste best_symfile_abilities = sym_file_abilities;
735f29bb8aSDimitry Andric best_symfile_up.reset(curr_symfile_up.release());
74f73363f1SDimitry Andric // If any symbol file parser has all of the abilities, then we should
75f73363f1SDimitry Andric // just stop looking.
76f034231aSEd Maste if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
77f034231aSEd Maste break;
78f034231aSEd Maste }
79f034231aSEd Maste }
80f034231aSEd Maste }
815f29bb8aSDimitry Andric if (best_symfile_up) {
82145449b1SDimitry Andric // If symbol on-demand is enabled the winning symbol file parser is
83145449b1SDimitry Andric // wrapped with SymbolFileOnDemand so that hydration of the debug info
84145449b1SDimitry Andric // can be controlled to improve performance.
85145449b1SDimitry Andric //
86145449b1SDimitry Andric // Currently the supported on-demand symbol files include:
87145449b1SDimitry Andric // executables, shared libraries and debug info files.
88145449b1SDimitry Andric //
89145449b1SDimitry Andric // To reduce unnecessary wrapping files with zero debug abilities are
90145449b1SDimitry Andric // skipped.
91145449b1SDimitry Andric ObjectFile::Type obj_file_type = objfile_sp->CalculateType();
92145449b1SDimitry Andric if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&
93145449b1SDimitry Andric best_symfile_abilities > 0 &&
94145449b1SDimitry Andric (obj_file_type == ObjectFile::eTypeExecutable ||
95145449b1SDimitry Andric obj_file_type == ObjectFile::eTypeSharedLibrary ||
96145449b1SDimitry Andric obj_file_type == ObjectFile::eTypeDebugInfo)) {
97145449b1SDimitry Andric best_symfile_up =
98145449b1SDimitry Andric std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));
99145449b1SDimitry Andric }
100f73363f1SDimitry Andric // Let the winning symbol file parser initialize itself more completely
101f73363f1SDimitry Andric // now that it has been chosen
1025f29bb8aSDimitry Andric best_symfile_up->InitializeObject();
103f034231aSEd Maste }
104f034231aSEd Maste }
1055f29bb8aSDimitry Andric return best_symfile_up.release();
106f034231aSEd Maste }
107f034231aSEd Maste
108344a3780SDimitry Andric uint32_t
ResolveSymbolContext(const SourceLocationSpec & src_location_spec,lldb::SymbolContextItem resolve_scope,SymbolContextList & sc_list)109344a3780SDimitry Andric SymbolFile::ResolveSymbolContext(const SourceLocationSpec &src_location_spec,
11094994d37SDimitry Andric lldb::SymbolContextItem resolve_scope,
11114f1b3e8SDimitry Andric SymbolContextList &sc_list) {
112e81d9d49SDimitry Andric return 0;
113e81d9d49SDimitry Andric }
114e81d9d49SDimitry Andric
FindGlobalVariables(ConstString name,const CompilerDeclContext & parent_decl_ctx,uint32_t max_matches,VariableList & variables)115ead24645SDimitry Andric void SymbolFile::FindGlobalVariables(ConstString name,
116cfca06d7SDimitry Andric const CompilerDeclContext &parent_decl_ctx,
117f73363f1SDimitry Andric uint32_t max_matches,
118ead24645SDimitry Andric VariableList &variables) {}
119e81d9d49SDimitry Andric
FindGlobalVariables(const RegularExpression & regex,uint32_t max_matches,VariableList & variables)120ead24645SDimitry Andric void SymbolFile::FindGlobalVariables(const RegularExpression ®ex,
121ead24645SDimitry Andric uint32_t max_matches,
122ead24645SDimitry Andric VariableList &variables) {}
123ead24645SDimitry Andric
FindFunctions(const Module::LookupInfo & lookup_info,const CompilerDeclContext & parent_decl_ctx,bool include_inlines,SymbolContextList & sc_list)124e3b55780SDimitry Andric void SymbolFile::FindFunctions(const Module::LookupInfo &lookup_info,
125cfca06d7SDimitry Andric const CompilerDeclContext &parent_decl_ctx,
126ead24645SDimitry Andric bool include_inlines,
127ead24645SDimitry Andric SymbolContextList &sc_list) {}
128e81d9d49SDimitry Andric
FindFunctions(const RegularExpression & regex,bool include_inlines,SymbolContextList & sc_list)129ead24645SDimitry Andric void SymbolFile::FindFunctions(const RegularExpression ®ex,
130ead24645SDimitry Andric bool include_inlines,
131ead24645SDimitry Andric SymbolContextList &sc_list) {}
132e81d9d49SDimitry Andric
GetMangledNamesForFunction(const std::string & scope_qualified_name,std::vector<ConstString> & mangled_names)13314f1b3e8SDimitry Andric void SymbolFile::GetMangledNamesForFunction(
13414f1b3e8SDimitry Andric const std::string &scope_qualified_name,
13577fc4c14SDimitry Andric std::vector<ConstString> &mangled_names) {}
1367fed546dSDimitry Andric
AssertModuleLock()13794994d37SDimitry Andric void SymbolFile::AssertModuleLock() {
13894994d37SDimitry Andric // The code below is too expensive to leave enabled in release builds. It's
13994994d37SDimitry Andric // enabled in debug builds or when the correct macro is set.
14094994d37SDimitry Andric #if defined(LLDB_CONFIGURATION_DEBUG)
14194994d37SDimitry Andric // We assert that we have to module lock by trying to acquire the lock from a
14294994d37SDimitry Andric // different thread. Note that we must abort if the result is true to
14394994d37SDimitry Andric // guarantee correctness.
14477fc4c14SDimitry Andric assert(std::async(
14577fc4c14SDimitry Andric std::launch::async,
14677fc4c14SDimitry Andric [this] {
14777fc4c14SDimitry Andric return this->GetModuleMutex().try_lock();
14877fc4c14SDimitry Andric }).get() == false &&
14994994d37SDimitry Andric "Module is not locked");
15094994d37SDimitry Andric #endif
15194994d37SDimitry Andric }
1525f29bb8aSDimitry Andric
153145449b1SDimitry Andric SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;
154ead24645SDimitry Andric
GetSymtab()155145449b1SDimitry Andric Symtab *SymbolFileCommon::GetSymtab() {
156ead24645SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
157ead24645SDimitry Andric // Fetch the symtab from the main object file.
158e3b55780SDimitry Andric auto *symtab = GetMainObjectFile()->GetSymtab();
159e3b55780SDimitry Andric if (m_symtab != symtab) {
160e3b55780SDimitry Andric m_symtab = symtab;
161ead24645SDimitry Andric
162ead24645SDimitry Andric // Then add our symbols to it.
163ead24645SDimitry Andric if (m_symtab)
164ead24645SDimitry Andric AddSymbols(*m_symtab);
165e3b55780SDimitry Andric }
166ead24645SDimitry Andric return m_symtab;
167ead24645SDimitry Andric }
168ead24645SDimitry Andric
GetMainObjectFile()169145449b1SDimitry Andric ObjectFile *SymbolFileCommon::GetMainObjectFile() {
170145449b1SDimitry Andric return m_objfile_sp->GetModule()->GetObjectFile();
171145449b1SDimitry Andric }
172145449b1SDimitry Andric
SectionFileAddressesChanged()173145449b1SDimitry Andric void SymbolFileCommon::SectionFileAddressesChanged() {
174ead24645SDimitry Andric ObjectFile *module_objfile = GetMainObjectFile();
175ead24645SDimitry Andric ObjectFile *symfile_objfile = GetObjectFile();
176ead24645SDimitry Andric if (symfile_objfile != module_objfile)
177ead24645SDimitry Andric symfile_objfile->SectionFileAddressesChanged();
178e3b55780SDimitry Andric if (auto *symtab = GetSymtab())
179e3b55780SDimitry Andric symtab->SectionFileAddressesChanged();
180ead24645SDimitry Andric }
181ead24645SDimitry Andric
GetNumCompileUnits()182145449b1SDimitry Andric uint32_t SymbolFileCommon::GetNumCompileUnits() {
183145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
184145449b1SDimitry Andric if (!m_compile_units) {
185145449b1SDimitry Andric // Create an array of compile unit shared pointers -- which will each
186145449b1SDimitry Andric // remain NULL until someone asks for the actual compile unit information.
187145449b1SDimitry Andric m_compile_units.emplace(CalculateNumCompileUnits());
188145449b1SDimitry Andric }
189145449b1SDimitry Andric return m_compile_units->size();
190145449b1SDimitry Andric }
191145449b1SDimitry Andric
GetCompileUnitAtIndex(uint32_t idx)192145449b1SDimitry Andric CompUnitSP SymbolFileCommon::GetCompileUnitAtIndex(uint32_t idx) {
193145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
194145449b1SDimitry Andric uint32_t num = GetNumCompileUnits();
195145449b1SDimitry Andric if (idx >= num)
196145449b1SDimitry Andric return nullptr;
197145449b1SDimitry Andric lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
198145449b1SDimitry Andric if (!cu_sp)
199145449b1SDimitry Andric cu_sp = ParseCompileUnitAtIndex(idx);
200145449b1SDimitry Andric return cu_sp;
201145449b1SDimitry Andric }
202145449b1SDimitry Andric
SetCompileUnitAtIndex(uint32_t idx,const CompUnitSP & cu_sp)203145449b1SDimitry Andric void SymbolFileCommon::SetCompileUnitAtIndex(uint32_t idx,
204145449b1SDimitry Andric const CompUnitSP &cu_sp) {
205145449b1SDimitry Andric std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
206145449b1SDimitry Andric const size_t num_compile_units = GetNumCompileUnits();
207145449b1SDimitry Andric assert(idx < num_compile_units);
208b1c73532SDimitry Andric UNUSED_IF_ASSERT_DISABLED(num_compile_units);
209145449b1SDimitry Andric
210145449b1SDimitry Andric // Fire off an assertion if this compile unit already exists for now. The
211145449b1SDimitry Andric // partial parsing should take care of only setting the compile unit
212145449b1SDimitry Andric // once, so if this assertion fails, we need to make sure that we don't
213145449b1SDimitry Andric // have a race condition, or have a second parse of the same compile
214145449b1SDimitry Andric // unit.
215145449b1SDimitry Andric assert((*m_compile_units)[idx] == nullptr);
216145449b1SDimitry Andric (*m_compile_units)[idx] = cu_sp;
217145449b1SDimitry Andric }
218145449b1SDimitry Andric
219e3b55780SDimitry Andric llvm::Expected<TypeSystemSP>
GetTypeSystemForLanguage(lldb::LanguageType language)220145449b1SDimitry Andric SymbolFileCommon::GetTypeSystemForLanguage(lldb::LanguageType language) {
221145449b1SDimitry Andric auto type_system_or_err =
222145449b1SDimitry Andric m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
223145449b1SDimitry Andric if (type_system_or_err) {
224e3b55780SDimitry Andric if (auto ts = *type_system_or_err)
225e3b55780SDimitry Andric ts->SetSymbolFile(this);
226145449b1SDimitry Andric }
227145449b1SDimitry Andric return type_system_or_err;
228145449b1SDimitry Andric }
229145449b1SDimitry Andric
GetDebugInfoSize(bool load_all_debug_info)230ac9a064cSDimitry Andric uint64_t SymbolFileCommon::GetDebugInfoSize(bool load_all_debug_info) {
231145449b1SDimitry Andric if (!m_objfile_sp)
232145449b1SDimitry Andric return 0;
233145449b1SDimitry Andric ModuleSP module_sp(m_objfile_sp->GetModule());
234145449b1SDimitry Andric if (!module_sp)
235145449b1SDimitry Andric return 0;
236145449b1SDimitry Andric const SectionList *section_list = module_sp->GetSectionList();
237145449b1SDimitry Andric if (section_list)
238145449b1SDimitry Andric return section_list->GetDebugInfoSize();
239145449b1SDimitry Andric return 0;
240145449b1SDimitry Andric }
241145449b1SDimitry Andric
Dump(Stream & s)242145449b1SDimitry Andric void SymbolFileCommon::Dump(Stream &s) {
243ead24645SDimitry Andric s.Format("SymbolFile {0} ({1})\n", GetPluginName(),
244ead24645SDimitry Andric GetMainObjectFile()->GetFileSpec());
245ead24645SDimitry Andric s.PutCString("Types:\n");
246ead24645SDimitry Andric m_type_list.Dump(&s, /*show_context*/ false);
247ead24645SDimitry Andric s.PutChar('\n');
248ead24645SDimitry Andric
249ead24645SDimitry Andric s.PutCString("Compile units:\n");
250ead24645SDimitry Andric if (m_compile_units) {
251ead24645SDimitry Andric for (const CompUnitSP &cu_sp : *m_compile_units) {
252ead24645SDimitry Andric // We currently only dump the compile units that have been parsed
253ead24645SDimitry Andric if (cu_sp)
254ead24645SDimitry Andric cu_sp->Dump(&s, /*show_context*/ false);
255ead24645SDimitry Andric }
256ead24645SDimitry Andric }
257ead24645SDimitry Andric s.PutChar('\n');
258ead24645SDimitry Andric
259ead24645SDimitry Andric if (Symtab *symtab = GetSymtab())
260ead24645SDimitry Andric symtab->Dump(&s, nullptr, eSortOrderNone);
261ead24645SDimitry Andric }
262