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"
16 #include "lldb/Symbol/TypeMap.h"
17 #include "lldb/Symbol/TypeSystem.h"
19 #include "lldb/Utility/Log.h"
21 #include "lldb/lldb-private.h"
22 
23 #include <future>
24 
25 using namespace lldb_private;
26 using namespace lldb;
27 
28 char SymbolFile::ID;
30 
32  // No-op for most implementations.
33 }
34 
35 std::recursive_mutex &SymbolFile::GetModuleMutex() const {
36  return GetObjectFile()->GetModule()->GetMutex();
37 }
38 
39 SymbolFile *SymbolFile::FindPlugin(ObjectFileSP objfile_sp) {
40  std::unique_ptr<SymbolFile> best_symfile_up;
41  if (objfile_sp != nullptr) {
42 
43  // We need to test the abilities of this section list. So create what it
44  // would be with this new objfile_sp.
45  lldb::ModuleSP module_sp(objfile_sp->GetModule());
46  if (module_sp) {
47  // Default to the main module section list.
48  ObjectFile *module_obj_file = module_sp->GetObjectFile();
49  if (module_obj_file != objfile_sp.get()) {
50  // Make sure the main object file's sections are created
51  module_obj_file->GetSectionList();
52  objfile_sp->CreateSections(*module_sp->GetUnifiedSectionList());
53  }
54  }
55 
56  // TODO: Load any plug-ins in the appropriate plug-in search paths and
57  // iterate over all of them to find the best one for the job.
58 
59  uint32_t best_symfile_abilities = 0;
60 
61  SymbolFileCreateInstance create_callback;
62  for (uint32_t idx = 0;
64  idx)) != nullptr;
65  ++idx) {
66  std::unique_ptr<SymbolFile> curr_symfile_up(create_callback(objfile_sp));
67 
68  if (curr_symfile_up) {
69  const uint32_t sym_file_abilities = curr_symfile_up->GetAbilities();
70  if (sym_file_abilities > best_symfile_abilities) {
71  best_symfile_abilities = sym_file_abilities;
72  best_symfile_up.reset(curr_symfile_up.release());
73  // If any symbol file parser has all of the abilities, then we should
74  // just stop looking.
75  if ((kAllAbilities & sym_file_abilities) == kAllAbilities)
76  break;
77  }
78  }
79  }
80  if (best_symfile_up) {
81  // If symbol on-demand is enabled the winning symbol file parser is
82  // wrapped with SymbolFileOnDemand so that hydration of the debug info
83  // can be controlled to improve performance.
84  //
85  // Currently the supported on-demand symbol files include:
86  // executables, shared libraries and debug info files.
87  //
88  // To reduce unnecessary wrapping files with zero debug abilities are
89  // skipped.
90  ObjectFile::Type obj_file_type = objfile_sp->CalculateType();
91  if (ModuleList::GetGlobalModuleListProperties().GetLoadSymbolOnDemand() &&
92  best_symfile_abilities > 0 &&
93  (obj_file_type == ObjectFile::eTypeExecutable ||
94  obj_file_type == ObjectFile::eTypeSharedLibrary ||
95  obj_file_type == ObjectFile::eTypeDebugInfo)) {
96  best_symfile_up =
97  std::make_unique<SymbolFileOnDemand>(std::move(best_symfile_up));
98  }
99  // Let the winning symbol file parser initialize itself more completely
100  // now that it has been chosen
101  best_symfile_up->InitializeObject();
102  }
103  }
104  return best_symfile_up.release();
105 }
106 
107 uint32_t
109  lldb::SymbolContextItem resolve_scope,
110  SymbolContextList &sc_list) {
111  return 0;
112 }
113 
115  const CompilerDeclContext &parent_decl_ctx,
116  uint32_t max_matches,
117  VariableList &variables) {}
118 
120  uint32_t max_matches,
121  VariableList &variables) {}
122 
124  const CompilerDeclContext &parent_decl_ctx,
125  lldb::FunctionNameType name_type_mask,
126  bool include_inlines,
127  SymbolContextList &sc_list) {}
128 
130  bool include_inlines,
131  SymbolContextList &sc_list) {}
132 
134  const std::string &scope_qualified_name,
135  std::vector<ConstString> &mangled_names) {}
136 
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) {}
142 
143 void SymbolFile::FindTypes(llvm::ArrayRef<CompilerContext> pattern,
144  LanguageSet languages,
145  llvm::DenseSet<SymbolFile *> &searched_symbol_files,
146  TypeMap &types) {}
147 
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
162 }
163 
165 
167  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
168  if (m_symtab)
169  return m_symtab;
170 
171  // Fetch the symtab from the main object file.
172  m_symtab = GetMainObjectFile()->GetSymtab();
173 
174  // Then add our symbols to it.
175  if (m_symtab)
176  AddSymbols(*m_symtab);
177 
178  return m_symtab;
179 }
180 
182  return m_objfile_sp->GetModule()->GetObjectFile();
183 }
184 
186  ObjectFile *module_objfile = GetMainObjectFile();
187  ObjectFile *symfile_objfile = GetObjectFile();
188  if (symfile_objfile != module_objfile)
189  symfile_objfile->SectionFileAddressesChanged();
190  if (m_symtab)
191  m_symtab->SectionFileAddressesChanged();
192 }
193 
195  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
196  if (!m_compile_units) {
197  // Create an array of compile unit shared pointers -- which will each
198  // remain NULL until someone asks for the actual compile unit information.
199  m_compile_units.emplace(CalculateNumCompileUnits());
200  }
201  return m_compile_units->size();
202 }
203 
205  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
206  uint32_t num = GetNumCompileUnits();
207  if (idx >= num)
208  return nullptr;
209  lldb::CompUnitSP &cu_sp = (*m_compile_units)[idx];
210  if (!cu_sp)
211  cu_sp = ParseCompileUnitAtIndex(idx);
212  return cu_sp;
213 }
214 
216  const CompUnitSP &cu_sp) {
217  std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
218  const size_t num_compile_units = GetNumCompileUnits();
219  assert(idx < num_compile_units);
220  (void)num_compile_units;
221 
222  // Fire off an assertion if this compile unit already exists for now. The
223  // partial parsing should take care of only setting the compile unit
224  // once, so if this assertion fails, we need to make sure that we don't
225  // have a race condition, or have a second parse of the same compile
226  // unit.
227  assert((*m_compile_units)[idx] == nullptr);
228  (*m_compile_units)[idx] = cu_sp;
229 }
230 
231 llvm::Expected<TypeSystem &>
233  auto type_system_or_err =
234  m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
235  if (type_system_or_err) {
236  type_system_or_err->SetSymbolFile(this);
237  }
238  return type_system_or_err;
239 }
240 
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;
251 }
252 
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');
259 
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);
266  }
267  }
268  s.PutChar('\n');
269 
270  if (Symtab *symtab = GetSymtab())
271  symtab->Dump(&s, nullptr, eSortOrderNone);
272 }
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:39
lldb_private::SymbolFile
Provides public interface for all SymbolFiles.
Definition: SymbolFile.h:45
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:137
lldb_private::ModuleList::GetGlobalModuleListProperties
static ModuleListProperties & GetGlobalModuleListProperties()
Definition: ModuleList.cpp:755
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::SymbolFileCommon::GetSymtab
Symtab * GetSymtab() override
Definition: SymbolFile.cpp:166
CompileUnit.h
TypeMap.h
lldb_private::SourceLocationSpec
Definition: SourceLocationSpec.h:27
lldb_private::ObjectFile::eTypeExecutable
@ eTypeExecutable
A normal executable.
Definition: ObjectFile.h:71
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:35
Module.h
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::SymbolContextList
Definition: SymbolContext.h:379
lldb_private::SectionList
Definition: Section.h:34
lldb_private::Stream
Definition: Stream.h:28
lldb_private::ObjectFile::Type
Type
Definition: ObjectFile.h:66
lldb_private::eSortOrderNone
@ eSortOrderNone
Definition: lldb-private-enumerations.h:110
lldb_private::VariableList
Definition: VariableList.h:18
lldb_private::SymbolFileCommon::SectionFileAddressesChanged
void SectionFileAddressesChanged() override
Notify the SymbolFile that the file addresses in the Sections for this module have been changed.
Definition: SymbolFile.cpp:185
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:588
lldb_private::SymbolFile::AssertModuleLock
void AssertModuleLock()
Definition: SymbolFile.cpp:148
Log.h
lldb_private::SymbolFileCommon::Dump
void Dump(Stream &s) override
Definition: SymbolFile.cpp:253
lldb_private::SymbolFile::ID
static char ID
LLVM RTTI support.
Definition: SymbolFile.h:47
lldb_private::SymbolFileCommon::ID
static char ID
LLVM RTTI support.
Definition: SymbolFile.h:376
StreamString.h
lldb_private::SectionList::GetDebugInfoSize
uint64_t GetDebugInfoSize() const
Get the debug information size from all sections that contain debug information.
Definition: Section.cpp:665
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::SymbolFileCommon::GetTypeSystemForLanguage
llvm::Expected< lldb_private::TypeSystem & > GetTypeSystemForLanguage(lldb::LanguageType language) override
Definition: SymbolFile.cpp:232
lldb_private::ObjectFile::eTypeSharedLibrary
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition: ObjectFile.h:79
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:123
lldb_private::SymbolFileCommon::GetNumCompileUnits
uint32_t GetNumCompileUnits() override
Definition: SymbolFile.cpp:194
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
SymbolFileOnDemand.h
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:31
lldb_private::SymbolFileCommon::GetMainObjectFile
ObjectFile * GetMainObjectFile() override
Definition: SymbolFile.cpp:181
lldb_private::Symtab
Definition: Symtab.h:22
uint32_t
lldb_private::ObjectFile::eTypeDebugInfo
@ eTypeDebugInfo
An object file that contains only debug information.
Definition: ObjectFile.h:73
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:1002
PluginManager.h
lldb_private::SymbolFileCommon::GetCompileUnitAtIndex
lldb::CompUnitSP GetCompileUnitAtIndex(uint32_t idx) override
Definition: SymbolFile.cpp:204
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:322
lldb_private::SymbolFileCommon::GetDebugInfoSize
uint64_t GetDebugInfoSize() override
Metrics gathering functions.
Definition: SymbolFile.cpp:241
lldb_private::SymbolFile::GetMangledNamesForFunction
virtual void GetMangledNamesForFunction(const std::string &scope_qualified_name, std::vector< ConstString > &mangled_names)
Definition: SymbolFile.cpp:133
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:114
SymbolFile.h
lldb_private::ObjectFile
Definition: ObjectFile.h:60
lldb_private::SymbolFileCommon::SetCompileUnitAtIndex
void SetCompileUnitAtIndex(uint32_t idx, const lldb::CompUnitSP &cu_sp)
Definition: SymbolFile.cpp:215