LLDB  mainline
SymbolFile.cpp
Go to the documentation of this file.
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 //===----------------------------------------------------------------------===//
8 
10 
11 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/ObjectFile.h"
15 #include "lldb/Symbol/TypeMap.h"
16 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Utility/Log.h"
20 #include "lldb/lldb-private.h"
21 
22 #include <future>
23 
24 using namespace lldb_private;
25 using namespace lldb;
26 
27 char SymbolFile::ID;
28 
30  // No-op for most implementations.
31 }
32 
33 std::recursive_mutex &SymbolFile::GetModuleMutex() const {
34  return GetObjectFile()->GetModule()->GetMutex();
35 }
37  return m_objfile_sp->GetModule()->GetObjectFile();
38 }
39 
40 SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {
41  std::unique_ptr<SymbolFile> best_symfile_up;
42  if (objfile_sp != nullptr) {
43 
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());
54  }
55  }
56 
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.
59 
60  uint32_t best_symfile_abilities = 0;
61 
62  SymbolFileCreateInstance create_callback;
63  for (uint32_t idx = 0;
65  idx)) != nullptr;
66  ++idx) {
67  std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));
68 
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;
78  }
79  }
80  }
81  if (best_symfile_up) {
82  // Let the winning symbol file parser initialize itself more completely
83  // now that it has been chosen
84  best_symfile_up->InitializeObject();
85  }
86  }
87  return best_symfile_up.release();
88 }
89 
90 llvm::Expected<TypeSystem &>
92  auto type_system_or_err =
93  m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
94  if (type_system_or_err) {
95  type_system_or_err->SetSymbolFile(this);
96  }
97  return type_system_or_err;
98 }
99 
100 uint32_t
102  lldb::SymbolContextItem resolve_scope,
103  SymbolContextList &sc_list) {
104  return 0;
105 }
106 
108  const CompilerDeclContext &parent_decl_ctx,
109  uint32_t max_matches,
110  VariableList &variables) {}
111 
113  uint32_t max_matches,
114  VariableList &variables) {}
115 
117  const CompilerDeclContext &parent_decl_ctx,
118  lldb::FunctionNameType name_type_mask,
119  bool include_inlines,
120  SymbolContextList &sc_list) {}
121 
123  bool include_inlines,
124  SymbolContextList &sc_list) {}
125 
127  const std::string &scope_qualified_name,
128  std::vector<ConstString> &mangled_names) {
129  return;
130 }
131 
133  ConstString name, const CompilerDeclContext &parent_decl_ctx,
134  uint32_t max_matches,
135  llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
136  TypeMap &types) {}
137 
138 void SymbolFile::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
139  LanguageSet languages,
140  llvm::DenseSet<SymbolFile *> &searched_symbol_files,
141  TypeMap &types) {}
142 
144  // The code below is too expensive to leave enabled in release builds. It's
145  // enabled in debug builds or when the correct macro is set.
146 #if defined(LLDB_CONFIGURATION_DEBUG)
147  // We assert that we have to module lock by trying to acquire the lock from a
148  // different thread. Note that we must abort if the result is true to
149  // guarantee correctness.
150  assert(std::async(std::launch::async,
151  [this] { return this->GetModuleMutex().try_lock(); })
152  .get() == false &&
153  "Module is not locked");
154 #endif
155 }
156 
158  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
159  if (!m_compile_units) {
160  // Create an array of compile unit shared pointers -- which will each
161  // remain NULL until someone asks for the actual compile unit information.
162  m_compile_units.emplace(CalculateNumCompileUnits());
163  }
164  return m_compile_units->size();
165 }
166 
168  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
169  uint32_t num = GetNumCompileUnits();
170  if (idx >= num)
171  return nullptr;
172  lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
173  if (!cu_sp)
174  cu_sp = ParseCompileUnitAtIndex(idx);
175  return cu_sp;
176 }
177 
178 void SymbolFile::SetCompileUnitAtIndex(uint32_t idx, const CompUnitSP &cu_sp) {
179  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
180  const size_t num_compile_units = GetNumCompileUnits();
181  assert(idx < num_compile_units);
182  (void)num_compile_units;
183 
184  // Fire off an assertion if this compile unit already exists for now. The
185  // partial parsing should take care of only setting the compile unit
186  // once, so if this assertion fails, we need to make sure that we don't
187  // have a race condition, or have a second parse of the same compile
188  // unit.
189  assert((*m_compile_units)[idx] == nullptr);
190  (*m_compile_units)[idx] = cu_sp;
191 }
192 
194  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
195  if (m_symtab)
196  return m_symtab;
197 
198  // Fetch the symtab from the main object file.
199  m_symtab = GetMainObjectFile()->GetSymtab();
200 
201  // Then add our symbols to it.
202  if (m_symtab)
203  AddSymbols(*m_symtab);
204 
205  return m_symtab;
206 }
207 
209  ObjectFile *module_objfile = GetMainObjectFile();
210  ObjectFile *symfile_objfile = GetObjectFile();
211  if (symfile_objfile != module_objfile)
212  symfile_objfile->SectionFileAddressesChanged();
213  if (m_symtab)
214  m_symtab->SectionFileAddressesChanged();
215 }
216 
218  s.Format("SymbolFile {0} ({1})\n", GetPluginName(),
219  GetMainObjectFile()->GetFileSpec());
220  s.PutCString("Types:\n");
221  m_type_list.Dump(&s, /*show_context*/ false);
222  s.PutChar('\n');
223 
224  s.PutCString("Compile units:\n");
225  if (m_compile_units) {
226  for (const CompUnitSP &cu_sp : *m_compile_units) {
227  // We currently only dump the compile units that have been parsed
228  if (cu_sp)
229  cu_sp->Dump(&s, /*show_context*/ false);
230  }
231  }
232  s.PutChar('\n');
233 
234  if (Symtab *symtab = GetSymtab())
235  symtab->Dump(&s, nullptr, eSortOrderNone);
236 }
237 
lldb_private::Stream::Format
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
lldb_private::SymbolFile::FindPlugin
static SymbolFile * FindPlugin(lldb::ObjectFileSP objfile_sp)
Definition: SymbolFile.cpp:40
lldb_private::SymbolFile::GetNumCompileUnits
uint32_t GetNumCompileUnits()
Definition: SymbolFile.cpp:157
lldb_private::SymbolFile::GetCompileUnitAtIndex
lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx)
Definition: SymbolFile.cpp:167
lldb_private::SymbolFile
Definition: SymbolFile.h:37
lldb_private::SymbolFile::FindTypes
virtual void FindTypes(ConstString name, const CompilerDeclContext &parent_decl_ctx, uint32_t max_matches, llvm::DenseSet< lldb_private::SymbolFile * > &searched_symbol_files, TypeMap &types)
Definition: SymbolFile.cpp:132
lldb_private::RegularExpression
Definition: RegularExpression.h:18
CompileUnit.h
TypeMap.h
lldb_private::SourceLocationSpec
Definition: SourceLocationSpec.h:27
lldb_private::SymbolFile::GetModuleMutex
virtual std::recursive_mutex & GetModuleMutex() const
Symbols file subclasses should override this to return the Module that owns the TypeSystem that this ...
Definition: SymbolFile.cpp:33
Module.h
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::SymbolContextList
Definition: SymbolContext.h:378
lldb_private::SymbolFile::SectionFileAddressesChanged
virtual void SectionFileAddressesChanged()
Notify the SymbolFile that the file addresses in the Sections for this module have been changed.
Definition: SymbolFile.cpp:208
lldb_private::Stream
Definition: Stream.h:28
lldb_private::SymbolFile::Dump
virtual void Dump(Stream &s)
Definition: SymbolFile.cpp:217
lldb_private::eSortOrderNone
@ eSortOrderNone
Definition: lldb-private-enumerations.h:110
lldb_private::VariableList
Definition: VariableList.h:18
lldb_private::SymbolFile::GetSymtab
Symtab * GetSymtab()
Definition: SymbolFile.cpp:193
lldb_private::SymbolFile::SetCompileUnitAtIndex
void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp)
Definition: SymbolFile.cpp:178
lldb_private::ObjectFile::GetSectionList
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
Definition: ObjectFile.cpp:583
lldb_private::SymbolFile::AssertModuleLock
void AssertModuleLock()
Definition: SymbolFile.cpp:143
Log.h
lldb_private::SymbolFile::GetMainObjectFile
ObjectFile * GetMainObjectFile()
Definition: SymbolFile.cpp:36
lldb_private::SymbolFile::ID
static char ID
LLVM RTTI support.
Definition: SymbolFile.h:39
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::SymbolFile::GetTypeSystemForLanguage
virtual llvm::Expected< lldb_private::TypeSystem & > GetTypeSystemForLanguage(lldb::LanguageType language)
Definition: SymbolFile.cpp:91
lldb_private::SymbolFile::FindFunctions
virtual void FindFunctions(ConstString name, const CompilerDeclContext &parent_decl_ctx, lldb::FunctionNameType name_type_mask, bool include_inlines, SymbolContextList &sc_list)
Definition: SymbolFile.cpp:116
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:38
TypeSystem.h
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
VariableList.h
lldb_private::TypeMap
Definition: TypeMap.h:20
lldb_private::CompilerDeclContext
Represents a generic declaration context in a program.
Definition: CompilerDeclContext.h:30
ObjectFile.h
lldb-private.h
lldb_private::SymbolFile::PreloadSymbols
virtual void PreloadSymbols()
Definition: SymbolFile.cpp:29
lldb_private::Symtab
Definition: Symtab.h:21
uint32_t
lldb_private::ModuleChild::GetModule
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
Definition: ModuleChild.cpp:24
lldb_private::PluginManager::GetSymbolFileCreateCallbackAtIndex
static SymbolFileCreateInstance GetSymbolFileCreateCallbackAtIndex(uint32_t idx)
Definition: PluginManager.cpp:979
PluginManager.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::SymbolFile::ResolveSymbolContext
virtual uint32_t ResolveSymbolContext(const Address &so_addr, lldb::SymbolContextItem resolve_scope, SymbolContext &sc)=0
lldb_private::SymbolFile::RegisterInfoResolver::~RegisterInfoResolver
virtual ~RegisterInfoResolver()
lldb_private::LanguageSet
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: TypeSystem.h:40
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::ObjectFile::SectionFileAddressesChanged
virtual void SectionFileAddressesChanged()
Notify the ObjectFile that the file addresses in the Sections for this module have been changed.
Definition: ObjectFile.h:320
lldb_private::SymbolFile::GetMangledNamesForFunction
virtual void GetMangledNamesForFunction(const std::string &scope_qualified_name, std::vector< ConstString > &mangled_names)
Definition: SymbolFile.cpp:126
lldb
Definition: SBAddress.h:15
lldb_private::SymbolFile::FindGlobalVariables
virtual void FindGlobalVariables(ConstString name, const CompilerDeclContext &parent_decl_ctx, uint32_t max_matches, VariableList &variables)
Definition: SymbolFile.cpp:107
SymbolFile.h
lldb_private::ObjectFile
Definition: ObjectFile.h:58