blob: c7557f9d651c2c87b34fe4274a871093094cafc0 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFile.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Chris Lattner30fdc8d2010-06-08 16:52:24 +00009#include "lldb/Symbol/SymbolFile.h"
Greg Claytonc982b3d2011-11-28 01:45:00 +000010
Greg Clayton2d95dc9b2010-11-10 04:57:04 +000011#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/PluginManager.h"
Pavel Labathe0119902019-07-23 09:24:02 +000013#include "lldb/Symbol/CompileUnit.h"
Greg Clayton2d95dc9b2010-11-10 04:57:04 +000014#include "lldb/Symbol/ObjectFile.h"
Ravitheja Addepally40697302015-10-08 09:45:41 +000015#include "lldb/Symbol/TypeMap.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000016#include "lldb/Symbol/TypeSystem.h"
Greg Clayton99558cc42015-08-24 23:46:31 +000017#include "lldb/Symbol/VariableList.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/StreamString.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000020#include "lldb/lldb-private.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +000022#include <future>
23
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024using namespace lldb_private;
Pavel Labathe0119902019-07-23 09:24:02 +000025using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Jim Ingham7fca8c02017-04-28 00:51:06 +000027void SymbolFile::PreloadSymbols() {
28 // No-op for most implementations.
29}
30
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +000031std::recursive_mutex &SymbolFile::GetModuleMutex() const {
32 return GetObjectFile()->GetModule()->GetMutex();
33}
Pavel Labathc2409ba2019-07-29 15:53:36 +000034ObjectFile *SymbolFile::GetMainObjectFile() {
35 return m_obj_file->GetModule()->GetObjectFile();
36}
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038SymbolFile *SymbolFile::FindPlugin(ObjectFile *obj_file) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000039 std::unique_ptr<SymbolFile> best_symfile_up;
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 if (obj_file != nullptr) {
Greg Clayton3046e662013-07-10 01:23:25 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 // We need to test the abilities of this section list. So create what it
Adrian Prantl05097242018-04-30 16:49:04 +000043 // would be with this new obj_file.
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 lldb::ModuleSP module_sp(obj_file->GetModule());
45 if (module_sp) {
46 // Default to the main module section list.
47 ObjectFile *module_obj_file = module_sp->GetObjectFile();
48 if (module_obj_file != obj_file) {
49 // Make sure the main object file's sections are created
50 module_obj_file->GetSectionList();
51 obj_file->CreateSections(*module_sp->GetUnifiedSectionList());
52 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000054
55 // TODO: Load any plug-ins in the appropriate plug-in search paths and
56 // iterate over all of them to find the best one for the job.
57
58 uint32_t best_symfile_abilities = 0;
59
60 SymbolFileCreateInstance create_callback;
61 for (uint32_t idx = 0;
62 (create_callback = PluginManager::GetSymbolFileCreateCallbackAtIndex(
63 idx)) != nullptr;
64 ++idx) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000065 std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(obj_file));
Kate Stoneb9c1b512016-09-06 20:57:50 +000066
Jonas Devlieghered5b44032019-02-13 06:25:41 +000067 if (curr_symfile_up) {
68 const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 if (sym_file_abilities > best_symfile_abilities) {
70 best_symfile_abilities = sym_file_abilities;
Jonas Devlieghered5b44032019-02-13 06:25:41 +000071 best_symfile_up.reset(curr_symfile_up.release());
Adrian Prantl05097242018-04-30 16:49:04 +000072 // If any symbol file parser has all of the abilities, then we should
73 // just stop looking.
Kate Stoneb9c1b512016-09-06 20:57:50 +000074 if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
75 break;
76 }
77 }
78 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +000079 if (best_symfile_up) {
Adrian Prantl05097242018-04-30 16:49:04 +000080 // Let the winning symbol file parser initialize itself more completely
81 // now that it has been chosen
Jonas Devlieghered5b44032019-02-13 06:25:41 +000082 best_symfile_up->InitializeObject();
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 }
84 }
Jonas Devlieghered5b44032019-02-13 06:25:41 +000085 return best_symfile_up.release();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088TypeSystem *SymbolFile::GetTypeSystemForLanguage(lldb::LanguageType language) {
89 TypeSystem *type_system =
90 m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
91 if (type_system)
92 type_system->SetSymbolFile(this);
93 return type_system;
Greg Clayton8b4edba2015-08-14 20:02:05 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096uint32_t SymbolFile::ResolveSymbolContext(const FileSpec &file_spec,
97 uint32_t line, bool check_inlines,
Zachary Turner991e4452018-10-25 20:45:19 +000098 lldb::SymbolContextItem resolve_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 SymbolContextList &sc_list) {
100 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000101}
102
Pavel Labath34cda142018-05-31 09:46:26 +0000103uint32_t
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000104SymbolFile::FindGlobalVariables(ConstString name,
Pavel Labath34cda142018-05-31 09:46:26 +0000105 const CompilerDeclContext *parent_decl_ctx,
106 uint32_t max_matches, VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110uint32_t SymbolFile::FindGlobalVariables(const RegularExpression &regex,
Pavel Labath34cda142018-05-31 09:46:26 +0000111 uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 VariableList &variables) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000114}
115
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000116uint32_t SymbolFile::FindFunctions(ConstString name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 const CompilerDeclContext *parent_decl_ctx,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000118 lldb::FunctionNameType name_type_mask,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000119 bool include_inlines, bool append,
120 SymbolContextList &sc_list) {
121 if (!append)
122 sc_list.Clear();
123 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000124}
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126uint32_t SymbolFile::FindFunctions(const RegularExpression &regex,
127 bool include_inlines, bool append,
128 SymbolContextList &sc_list) {
129 if (!append)
130 sc_list.Clear();
131 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000132}
133
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134void SymbolFile::GetMangledNamesForFunction(
135 const std::string &scope_qualified_name,
136 std::vector<ConstString> &mangled_names) {
137 return;
Siva Chandra9293fc42016-01-07 23:32:34 +0000138}
139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140uint32_t SymbolFile::FindTypes(
Adrian Prantl0e4c4822019-03-06 21:22:25 +0000141 ConstString name, const CompilerDeclContext *parent_decl_ctx,
Zachary Turner576495e2019-01-14 22:41:21 +0000142 bool append, uint32_t max_matches,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
144 TypeMap &types) {
145 if (!append)
146 types.Clear();
147 return 0;
Greg Clayton99558cc42015-08-24 23:46:31 +0000148}
149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150size_t SymbolFile::FindTypes(const std::vector<CompilerContext> &context,
151 bool append, TypeMap &types) {
152 if (!append)
153 types.Clear();
154 return 0;
Greg Claytone6b36cd2015-12-08 01:02:08 +0000155}
Jonas Devlieghere4f78c4f2018-10-22 20:14:36 +0000156
157void SymbolFile::AssertModuleLock() {
158 // The code below is too expensive to leave enabled in release builds. It's
159 // enabled in debug builds or when the correct macro is set.
160#if defined(LLDB_CONFIGURATION_DEBUG)
161 // We assert that we have to module lock by trying to acquire the lock from a
162 // different thread. Note that we must abort if the result is true to
163 // guarantee correctness.
164 assert(std::async(std::launch::async,
165 [this] { return this->GetModuleMutex().try_lock(); })
166 .get() == false &&
167 "Module is not locked");
168#endif
169}
Pavel Labath22bbd7d2019-05-10 07:54:37 +0000170
Pavel Labathe0119902019-07-23 09:24:02 +0000171uint32_t SymbolFile::GetNumCompileUnits() {
172 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
173 if (!m_compile_units) {
174 // Create an array of compile unit shared pointers -- which will each
175 // remain NULL until someone asks for the actual compile unit information.
176 m_compile_units.emplace(CalculateNumCompileUnits());
177 }
178 return m_compile_units->size();
179}
180
181CompUnitSP SymbolFile::GetCompileUnitAtIndex(uint32_t idx) {
Pavel Labath656ddeb2019-07-30 08:20:05 +0000182 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
Pavel Labathe0119902019-07-23 09:24:02 +0000183 uint32_t num = GetNumCompileUnits();
184 if (idx >= num)
185 return nullptr;
186 lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
187 if (!cu_sp)
188 cu_sp = ParseCompileUnitAtIndex(idx);
189 return cu_sp;
190}
191
192void SymbolFile::SetCompileUnitAtIndex(uint32_t idx, const CompUnitSP &cu_sp) {
193 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
194 const size_t num_compile_units = GetNumCompileUnits();
195 assert(idx < num_compile_units);
Fangrui Song2e959412019-07-25 09:56:45 +0000196 (void)num_compile_units;
Pavel Labathe0119902019-07-23 09:24:02 +0000197
198 // Fire off an assertion if this compile unit already exists for now. The
199 // partial parsing should take care of only setting the compile unit
200 // once, so if this assertion fails, we need to make sure that we don't
201 // have a race condition, or have a second parse of the same compile
202 // unit.
203 assert((*m_compile_units)[idx] == nullptr);
204 (*m_compile_units)[idx] = cu_sp;
205}
206
Pavel Labath84a68562019-07-26 07:03:28 +0000207Symtab *SymbolFile::GetSymtab() {
208 std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
209 if (m_symtab)
210 return m_symtab;
211
212 // Fetch the symtab from the main object file.
Pavel Labathc2409ba2019-07-29 15:53:36 +0000213 m_symtab = GetMainObjectFile()->GetSymtab();
Pavel Labath84a68562019-07-26 07:03:28 +0000214
215 // Then add our symbols to it.
216 if (m_symtab)
217 AddSymbols(*m_symtab);
218
219 return m_symtab;
220}
221
Pavel Labathc2409ba2019-07-29 15:53:36 +0000222void SymbolFile::SectionFileAddressesChanged() {
223 ObjectFile *module_objfile = GetMainObjectFile();
224 ObjectFile *symfile_objfile = GetObjectFile();
225 if (symfile_objfile != module_objfile)
226 symfile_objfile->SectionFileAddressesChanged();
227 if (m_symtab)
228 m_symtab->SectionFileAddressesChanged();
229}
230
Pavel Labathe0119902019-07-23 09:24:02 +0000231void SymbolFile::Dump(Stream &s) {
Pavel Labathf46e8972019-07-25 08:22:05 +0000232 s.PutCString("Types:\n");
233 m_type_list.Dump(&s, /*show_context*/ false);
234 s.PutChar('\n');
235
Pavel Labathe0119902019-07-23 09:24:02 +0000236 s.PutCString("Compile units:\n");
237 if (m_compile_units) {
238 for (const CompUnitSP &cu_sp : *m_compile_units) {
239 // We currently only dump the compile units that have been parsed
240 if (cu_sp)
241 cu_sp->Dump(&s, /*show_context*/ false);
242 }
243 }
244 s.PutChar('\n');
245}
246
Pavel Labath22bbd7d2019-05-10 07:54:37 +0000247SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default;