Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SymbolFile.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 9 | #include "lldb/Symbol/SymbolFile.h" |
Greg Clayton | c982b3d | 2011-11-28 01:45:00 +0000 | [diff] [blame] | 10 | |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 11 | #include "lldb/Core/Module.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/PluginManager.h" |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 13 | #include "lldb/Symbol/CompileUnit.h" |
Greg Clayton | 2d95dc9b | 2010-11-10 04:57:04 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/ObjectFile.h" |
Ravitheja Addepally | 4069730 | 2015-10-08 09:45:41 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/TypeMap.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/TypeSystem.h" |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/VariableList.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/StreamString.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include "lldb/lldb-private.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
Jonas Devlieghere | 4f78c4f | 2018-10-22 20:14:36 +0000 | [diff] [blame] | 22 | #include <future> |
| 23 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | using namespace lldb_private; |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 25 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | |
Jim Ingham | 7fca8c0 | 2017-04-28 00:51:06 +0000 | [diff] [blame] | 27 | void SymbolFile::PreloadSymbols() { |
| 28 | // No-op for most implementations. |
| 29 | } |
| 30 | |
Jonas Devlieghere | 4f78c4f | 2018-10-22 20:14:36 +0000 | [diff] [blame] | 31 | std::recursive_mutex &SymbolFile::GetModuleMutex() const { |
| 32 | return GetObjectFile()->GetModule()->GetMutex(); |
| 33 | } |
Pavel Labath | c2409ba | 2019-07-29 15:53:36 +0000 | [diff] [blame] | 34 | ObjectFile *SymbolFile::GetMainObjectFile() { |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 35 | return m_objfile_sp->GetModule()->GetObjectFile(); |
Pavel Labath | c2409ba | 2019-07-29 15:53:36 +0000 | [diff] [blame] | 36 | } |
Jonas Devlieghere | 4f78c4f | 2018-10-22 20:14:36 +0000 | [diff] [blame] | 37 | |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 38 | SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 39 | std::unique_ptr<SymbolFile> best_symfile_up; |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 40 | if (objfile_sp != nullptr) { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | // We need to test the abilities of this section list. So create what it |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 43 | // would be with this new objfile_sp. |
| 44 | lldb::ModuleSP module_sp(objfile_sp->GetModule()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | if (module_sp) { |
| 46 | // Default to the main module section list. |
| 47 | ObjectFile *module_obj_file = module_sp->GetObjectFile(); |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 48 | if (module_obj_file != objfile_sp.get()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | // Make sure the main object file's sections are created |
| 50 | module_obj_file->GetSectionList(); |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 51 | objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | |
| 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) { |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 65 | std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 67 | if (curr_symfile_up) { |
| 68 | const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | if (sym_file_abilities > best_symfile_abilities) { |
| 70 | best_symfile_abilities = sym_file_abilities; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 71 | best_symfile_up.reset(curr_symfile_up.release()); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 72 | // If any symbol file parser has all of the abilities, then we should |
| 73 | // just stop looking. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | if ((kAllAbilities & sym_file_abilities) == kAllAbilities) |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 79 | if (best_symfile_up) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 80 | // Let the winning symbol file parser initialize itself more completely |
| 81 | // now that it has been chosen |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 82 | best_symfile_up->InitializeObject(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 85 | return best_symfile_up.release(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Alex Langford | 0e252e3 | 2019-07-30 22:12:34 +0000 | [diff] [blame] | 88 | llvm::Expected<TypeSystem &> |
| 89 | SymbolFile::GetTypeSystemForLanguage(lldb::LanguageType language) { |
| 90 | auto type_system_or_err = |
Pavel Labath | d2deeb4 | 2019-07-31 08:25:25 +0000 | [diff] [blame] | 91 | m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language); |
Alex Langford | 0e252e3 | 2019-07-30 22:12:34 +0000 | [diff] [blame] | 92 | if (type_system_or_err) { |
| 93 | type_system_or_err->SetSymbolFile(this); |
| 94 | } |
| 95 | return type_system_or_err; |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 98 | uint32_t SymbolFile::ResolveSymbolContext(const FileSpec &file_spec, |
| 99 | uint32_t line, bool check_inlines, |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 100 | lldb::SymbolContextItem resolve_scope, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | SymbolContextList &sc_list) { |
| 102 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 105 | uint32_t |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 106 | SymbolFile::FindGlobalVariables(ConstString name, |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 107 | const CompilerDeclContext *parent_decl_ctx, |
| 108 | uint32_t max_matches, VariableList &variables) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | uint32_t SymbolFile::FindGlobalVariables(const RegularExpression ®ex, |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 113 | uint32_t max_matches, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | VariableList &variables) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 115 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 118 | uint32_t SymbolFile::FindFunctions(ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | const CompilerDeclContext *parent_decl_ctx, |
Zachary Turner | 117b1fa | 2018-10-25 20:45:40 +0000 | [diff] [blame] | 120 | lldb::FunctionNameType name_type_mask, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | bool include_inlines, bool append, |
| 122 | SymbolContextList &sc_list) { |
| 123 | if (!append) |
| 124 | sc_list.Clear(); |
| 125 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | uint32_t SymbolFile::FindFunctions(const RegularExpression ®ex, |
| 129 | bool include_inlines, bool append, |
| 130 | SymbolContextList &sc_list) { |
| 131 | if (!append) |
| 132 | sc_list.Clear(); |
| 133 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | void SymbolFile::GetMangledNamesForFunction( |
| 137 | const std::string &scope_qualified_name, |
| 138 | std::vector<ConstString> &mangled_names) { |
| 139 | return; |
Siva Chandra | 9293fc4 | 2016-01-07 23:32:34 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | uint32_t SymbolFile::FindTypes( |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 143 | ConstString name, const CompilerDeclContext *parent_decl_ctx, |
Zachary Turner | 576495e | 2019-01-14 22:41:21 +0000 | [diff] [blame] | 144 | bool append, uint32_t max_matches, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 145 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
| 146 | TypeMap &types) { |
| 147 | if (!append) |
| 148 | types.Clear(); |
| 149 | return 0; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Adrian Prantl | 330ae19a | 2019-08-21 18:06:56 +0000 | [diff] [blame] | 152 | size_t SymbolFile::FindTypes(llvm::ArrayRef<CompilerContext> pattern, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | bool append, TypeMap &types) { |
| 154 | if (!append) |
| 155 | types.Clear(); |
| 156 | return 0; |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 157 | } |
Jonas Devlieghere | 4f78c4f | 2018-10-22 20:14:36 +0000 | [diff] [blame] | 158 | |
| 159 | void SymbolFile::AssertModuleLock() { |
| 160 | // The code below is too expensive to leave enabled in release builds. It's |
| 161 | // enabled in debug builds or when the correct macro is set. |
| 162 | #if defined(LLDB_CONFIGURATION_DEBUG) |
| 163 | // We assert that we have to module lock by trying to acquire the lock from a |
| 164 | // different thread. Note that we must abort if the result is true to |
| 165 | // guarantee correctness. |
| 166 | assert(std::async(std::launch::async, |
| 167 | [this] { return this->GetModuleMutex().try_lock(); }) |
| 168 | .get() == false && |
| 169 | "Module is not locked"); |
| 170 | #endif |
| 171 | } |
Pavel Labath | 22bbd7d | 2019-05-10 07:54:37 +0000 | [diff] [blame] | 172 | |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 173 | uint32_t SymbolFile::GetNumCompileUnits() { |
| 174 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
| 175 | if (!m_compile_units) { |
| 176 | // Create an array of compile unit shared pointers -- which will each |
| 177 | // remain NULL until someone asks for the actual compile unit information. |
| 178 | m_compile_units.emplace(CalculateNumCompileUnits()); |
| 179 | } |
| 180 | return m_compile_units->size(); |
| 181 | } |
| 182 | |
| 183 | CompUnitSP SymbolFile::GetCompileUnitAtIndex(uint32_t idx) { |
Pavel Labath | 656ddeb | 2019-07-30 08:20:05 +0000 | [diff] [blame] | 184 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 185 | uint32_t num = GetNumCompileUnits(); |
| 186 | if (idx >= num) |
| 187 | return nullptr; |
| 188 | lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx]; |
| 189 | if (!cu_sp) |
| 190 | cu_sp = ParseCompileUnitAtIndex(idx); |
| 191 | return cu_sp; |
| 192 | } |
| 193 | |
| 194 | void SymbolFile::SetCompileUnitAtIndex(uint32_t idx, const CompUnitSP &cu_sp) { |
| 195 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
| 196 | const size_t num_compile_units = GetNumCompileUnits(); |
| 197 | assert(idx < num_compile_units); |
Fangrui Song | 2e95941 | 2019-07-25 09:56:45 +0000 | [diff] [blame] | 198 | (void)num_compile_units; |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 199 | |
| 200 | // Fire off an assertion if this compile unit already exists for now. The |
| 201 | // partial parsing should take care of only setting the compile unit |
| 202 | // once, so if this assertion fails, we need to make sure that we don't |
| 203 | // have a race condition, or have a second parse of the same compile |
| 204 | // unit. |
| 205 | assert((*m_compile_units)[idx] == nullptr); |
| 206 | (*m_compile_units)[idx] = cu_sp; |
| 207 | } |
| 208 | |
Pavel Labath | 84a6856 | 2019-07-26 07:03:28 +0000 | [diff] [blame] | 209 | Symtab *SymbolFile::GetSymtab() { |
| 210 | std::lock_guard<std::recursive_mutex> guard(GetModuleMutex()); |
| 211 | if (m_symtab) |
| 212 | return m_symtab; |
| 213 | |
| 214 | // Fetch the symtab from the main object file. |
Pavel Labath | c2409ba | 2019-07-29 15:53:36 +0000 | [diff] [blame] | 215 | m_symtab = GetMainObjectFile()->GetSymtab(); |
Pavel Labath | 84a6856 | 2019-07-26 07:03:28 +0000 | [diff] [blame] | 216 | |
| 217 | // Then add our symbols to it. |
| 218 | if (m_symtab) |
| 219 | AddSymbols(*m_symtab); |
| 220 | |
| 221 | return m_symtab; |
| 222 | } |
| 223 | |
Pavel Labath | c2409ba | 2019-07-29 15:53:36 +0000 | [diff] [blame] | 224 | void SymbolFile::SectionFileAddressesChanged() { |
| 225 | ObjectFile *module_objfile = GetMainObjectFile(); |
| 226 | ObjectFile *symfile_objfile = GetObjectFile(); |
| 227 | if (symfile_objfile != module_objfile) |
| 228 | symfile_objfile->SectionFileAddressesChanged(); |
| 229 | if (m_symtab) |
| 230 | m_symtab->SectionFileAddressesChanged(); |
| 231 | } |
| 232 | |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 233 | void SymbolFile::Dump(Stream &s) { |
Pavel Labath | 5a7e1e9 | 2019-08-05 11:29:01 +0000 | [diff] [blame] | 234 | s.Format("SymbolFile {0} ({1})\n", GetPluginName(), |
| 235 | GetMainObjectFile()->GetFileSpec()); |
Pavel Labath | f46e897 | 2019-07-25 08:22:05 +0000 | [diff] [blame] | 236 | s.PutCString("Types:\n"); |
| 237 | m_type_list.Dump(&s, /*show_context*/ false); |
| 238 | s.PutChar('\n'); |
| 239 | |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 240 | s.PutCString("Compile units:\n"); |
| 241 | if (m_compile_units) { |
| 242 | for (const CompUnitSP &cu_sp : *m_compile_units) { |
| 243 | // We currently only dump the compile units that have been parsed |
| 244 | if (cu_sp) |
| 245 | cu_sp->Dump(&s, /*show_context*/ false); |
| 246 | } |
| 247 | } |
| 248 | s.PutChar('\n'); |
Pavel Labath | d5d47a3 | 2019-08-05 09:21:47 +0000 | [diff] [blame] | 249 | |
| 250 | if (Symtab *symtab = GetSymtab()) |
| 251 | symtab->Dump(&s, nullptr, eSortOrderNone); |
Pavel Labath | e011990 | 2019-07-23 09:24:02 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Pavel Labath | 22bbd7d | 2019-05-10 07:54:37 +0000 | [diff] [blame] | 254 | SymbolFile::RegisterInfoResolver::~RegisterInfoResolver() = default; |