LLDB  mainline
Symtab.h
Go to the documentation of this file.
1 //===-- Symtab.h ------------------------------------------------*- C++ -*-===//
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 
9 #ifndef LLDB_SYMBOL_SYMTAB_H
10 #define LLDB_SYMBOL_SYMTAB_H
11 
13 #include "lldb/Symbol/Symbol.h"
14 #include "lldb/Utility/RangeMap.h"
15 #include "lldb/lldb-private.h"
16 #include <mutex>
17 #include <vector>
18 
19 namespace lldb_private {
20 
21 class Symtab {
22 public:
23  typedef std::vector<uint32_t> IndexCollection;
25 
26  enum Debug {
27  eDebugNo, // Not a debug symbol
28  eDebugYes, // A debug symbol
30  };
31 
33 
34  Symtab(ObjectFile *objfile);
35  ~Symtab();
36 
37  void PreloadSymbols();
38  void Reserve(size_t count);
39  Symbol *Resize(size_t count);
40  uint32_t AddSymbol(const Symbol &symbol);
41  size_t GetNumSymbols() const;
43  void
44  Dump(Stream *s, Target *target, SortOrder sort_type,
45  Mangled::NamePreference name_preference = Mangled::ePreferDemangled);
46  void Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes,
47  Mangled::NamePreference name_preference =
48  Mangled::ePreferDemangled) const;
49  uint32_t GetIndexForSymbol(const Symbol *symbol) const;
50  std::recursive_mutex &GetMutex() { return m_mutex; }
52  Symbol *SymbolAtIndex(size_t idx);
53  const Symbol *SymbolAtIndex(size_t idx) const;
55  Debug symbol_debug_type,
56  Visibility symbol_visibility, uint32_t &start_idx);
57  /// Get the parent symbol for the given symbol.
58  ///
59  /// Many symbols in symbol tables are scoped by other symbols that
60  /// contain one or more symbol. This function will look for such a
61  /// containing symbol and return it if there is one.
62  const Symbol *GetParent(Symbol *symbol) const;
64  std::vector<uint32_t> &indexes,
65  uint32_t start_idx = 0,
66  uint32_t end_index = UINT32_MAX) const;
68  lldb::SymbolType symbol_type, uint32_t flags_value,
69  std::vector<uint32_t> &indexes, uint32_t start_idx = 0,
70  uint32_t end_index = UINT32_MAX) const;
72  Debug symbol_debug_type,
73  Visibility symbol_visibility,
74  std::vector<uint32_t> &matches,
75  uint32_t start_idx = 0,
76  uint32_t end_index = UINT32_MAX) const;
78  std::vector<uint32_t> &matches);
80  Debug symbol_debug_type,
81  Visibility symbol_visibility,
82  std::vector<uint32_t> &matches);
84  lldb::SymbolType symbol_type,
85  std::vector<uint32_t> &matches);
87  lldb::SymbolType symbol_type,
88  Debug symbol_debug_type,
89  Visibility symbol_visibility,
90  std::vector<uint32_t> &matches);
91  uint32_t
93  lldb::SymbolType symbol_type,
94  std::vector<uint32_t> &indexes);
96  const RegularExpression &regex, lldb::SymbolType symbol_type,
97  Debug symbol_debug_type, Visibility symbol_visibility,
98  std::vector<uint32_t> &indexes);
100  lldb::SymbolType symbol_type,
101  std::vector<uint32_t> &symbol_indexes);
103  lldb::SymbolType symbol_type,
104  Debug symbol_debug_type,
105  Visibility symbol_visibility,
106  std::vector<uint32_t> &symbol_indexes);
108  const RegularExpression &regex, lldb::SymbolType symbol_type,
109  Debug symbol_debug_type, Visibility symbol_visibility,
110  std::vector<uint32_t> &symbol_indexes);
112  lldb::SymbolType symbol_type,
113  Debug symbol_debug_type,
114  Visibility symbol_visibility);
118  lldb::addr_t file_addr, std::function<bool(Symbol *)> const &callback);
119  void FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
120  SymbolContextList &sc_list);
121  void CalculateSymbolSizes();
122 
123  void SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
124  bool remove_duplicates) const;
125 
126  static void DumpSymbolHeader(Stream *s);
127 
128  void Finalize() {
129  // Shrink to fit the symbols so we don't waste memory
130  if (m_symbols.capacity() > m_symbols.size()) {
131  collection new_symbols(m_symbols.begin(), m_symbols.end());
132  m_symbols.swap(new_symbols);
133  }
134  }
135 
136  void AppendSymbolNamesToMap(const IndexCollection &indexes,
137  bool add_demangled, bool add_mangled,
138  NameToIndexMap &name_to_index_map) const;
139 
141 
142 protected:
143  typedef std::vector<Symbol> collection;
144  typedef collection::iterator iterator;
145  typedef collection::const_iterator const_iterator;
147  public:
148  FileRangeToIndexMapCompare(const Symtab &symtab) : m_symtab(symtab) {}
149  bool operator()(const uint32_t a_data, const uint32_t b_data) const {
150  return rank(a_data) > rank(b_data);
151  }
152 
153  private:
154  // How much preferred is this symbol?
155  int rank(const uint32_t data) const {
156  const Symbol &symbol = *m_symtab.SymbolAtIndex(data);
157  if (symbol.IsExternal())
158  return 3;
159  if (symbol.IsWeak())
160  return 2;
161  if (symbol.IsDebug())
162  return 0;
163  return 1;
164  }
165  const Symtab &m_symtab;
166  };
170  void InitNameIndexes();
171  void InitAddressIndexes();
172 
180  mutable std::recursive_mutex
181  m_mutex; // Provide thread safety for this symbol table
183 
184 private:
185  bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type,
186  Visibility symbol_visibility) const {
187  switch (symbol_debug_type) {
188  case eDebugNo:
189  if (m_symbols[idx].IsDebug())
190  return false;
191  break;
192 
193  case eDebugYes:
194  if (!m_symbols[idx].IsDebug())
195  return false;
196  break;
197 
198  case eDebugAny:
199  break;
200  }
201 
202  switch (symbol_visibility) {
203  case eVisibilityAny:
204  return true;
205 
206  case eVisibilityExtern:
207  return m_symbols[idx].IsExternal();
208 
209  case eVisibilityPrivate:
210  return !m_symbols[idx].IsExternal();
211  }
212  return false;
213  }
214 
215  void SymbolIndicesToSymbolContextList(std::vector<uint32_t> &symbol_indexes,
216  SymbolContextList &sc_list);
217 
219  uint32_t value, std::set<const char *> &class_contexts,
220  std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
221  RichManglingContext &rmc);
222 
224  const char *decl_context,
225  const std::set<const char *> &class_contexts);
226 
227  Symtab(const Symtab &) = delete;
228  const Symtab &operator=(const Symtab &) = delete;
229 };
230 
231 } // namespace lldb_private
232 
233 #endif // LLDB_SYMBOL_SYMTAB_H
lldb_private::Symtab::FileRangeToIndexMapCompare::FileRangeToIndexMapCompare
FileRangeToIndexMapCompare(const Symtab &symtab)
Definition: Symtab.h:148
lldb_private::Symtab::m_file_addr_to_index
FileRangeToIndexMap m_file_addr_to_index
Definition: Symtab.h:175
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::Symtab::m_objfile
ObjectFile * m_objfile
Definition: Symtab.h:173
lldb_private::Symtab::AppendSymbolIndexesWithTypeAndFlagsValue
uint32_t AppendSymbolIndexesWithTypeAndFlagsValue(lldb::SymbolType symbol_type, uint32_t flags_value, std::vector< uint32_t > &indexes, uint32_t start_idx=0, uint32_t end_index=UINT32_MAX) const
Definition: Symtab.cpp:456
lldb_private::Symtab::AppendSymbolNamesToMap
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition: Symtab.cpp:410
lldb_private::Symbol
Definition: Symbol.h:20
lldb_private::Symtab::GetParent
const Symbol * GetParent(Symbol *symbol) const
Get the parent symbol for the given symbol.
Definition: Symtab.cpp:1091
lldb_private::Symtab::iterator
collection::iterator iterator
Definition: Symtab.h:144
lldb_private::Symtab::PreloadSymbols
void PreloadSymbols()
Definition: Symtab.cpp:405
lldb_private::Symtab::m_symbols
collection m_symbols
Definition: Symtab.h:174
lldb_private::Symtab::Debug
Debug
Definition: Symtab.h:26
lldb_private::Symtab::FileRangeToIndexMap
RangeDataVector< lldb::addr_t, lldb::addr_t, uint32_t, 0, FileRangeToIndexMapCompare > FileRangeToIndexMap
Definition: Symtab.h:169
lldb_private::Symtab::FileRangeToIndexMapCompare
Definition: Symtab.h:146
lldb_private::Symtab::m_basename_to_index
UniqueCStringMap< uint32_t > m_basename_to_index
Definition: Symtab.h:177
lldb_private::Symtab::~Symtab
~Symtab()
Definition: Symtab.cpp:35
lldb_private::SymbolContextList
Definition: SymbolContext.h:378
lldb_private::Symtab::Symtab
Symtab(ObjectFile *objfile)
Definition: Symtab.cpp:30
lldb_private::Symtab::Finalize
void Finalize()
Definition: Symtab.h:128
lldb_private::Symtab::eVisibilityPrivate
@ eVisibilityPrivate
Definition: Symtab.h:32
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Symtab::SymbolAtIndex
Symbol * SymbolAtIndex(size_t idx)
Definition: Symtab.cpp:200
RangeMap.h
UniqueCStringMap.h
lldb_private::Target
Definition: Target.h:445
lldb_private::Symtab::GetMutex
std::recursive_mutex & GetMutex()
Definition: Symtab.h:50
lldb_private::Symtab::FindFunctionSymbols
void FindFunctionSymbols(ConstString name, uint32_t name_type_mask, SymbolContextList &sc_list)
Definition: Symtab.cpp:1005
lldb_private::Symtab::eDebugNo
@ eDebugNo
Definition: Symtab.h:27
lldb_private::Symtab::NameToIndexMap
UniqueCStringMap< uint32_t > NameToIndexMap
Definition: Symtab.h:24
lldb_private::Symtab::FindFirstSymbolWithNameAndType
Symbol * FindFirstSymbolWithNameAndType(ConstString name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
Definition: Symtab.cpp:780
lldb_private::Symtab::m_mutex
std::recursive_mutex m_mutex
Definition: Symtab.h:181
lldb_private::Symtab::eDebugYes
@ eDebugYes
Definition: Symtab.h:28
lldb_private::Symtab::Dump
void Dump(Stream *s, Target *target, SortOrder sort_type, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition: Symtab.cpp:72
lldb_private::Symtab::DumpSymbolHeader
static void DumpSymbolHeader(Stream *s)
Definition: Symtab.cpp:169
lldb_private::Symtab::m_method_to_index
UniqueCStringMap< uint32_t > m_method_to_index
Definition: Symtab.h:178
lldb_private::Symtab::FileRangeToIndexMapCompare::rank
int rank(const uint32_t data) const
Definition: Symtab.h:155
lldb_private::Symtab::m_file_addr_to_index_computed
bool m_file_addr_to_index_computed
Definition: Symtab.h:182
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Symtab::FindSymbolContainingFileAddress
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition: Symtab.cpp:948
lldb_private::Symbol::IsExternal
bool IsExternal() const
Definition: Symbol.h:162
lldb_private::Symtab::const_iterator
collection::const_iterator const_iterator
Definition: Symtab.h:145
lldb_private::Symtab::eVisibilityExtern
@ eVisibilityExtern
Definition: Symtab.h:32
lldb_private::Symtab::m_selector_to_index
UniqueCStringMap< uint32_t > m_selector_to_index
Definition: Symtab.h:179
lldb_private::Symtab::GetIndexForSymbol
uint32_t GetIndexForSymbol(const Symbol *symbol) const
Definition: Symtab.cpp:499
lldb_private::Symtab::InitAddressIndexes
void InitAddressIndexes()
Definition: Symtab.cpp:844
lldb_private::Symtab::FindSymbolWithType
Symbol * FindSymbolWithType(lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t &start_idx)
Definition: Symtab.cpp:713
lldb_private::Symtab::FindSymbolAtFileAddress
Symbol * FindSymbolAtFileAddress(lldb::addr_t file_addr)
Definition: Symtab.cpp:933
lldb::SymbolType
SymbolType
Symbol types.
Definition: lldb-enumerations.h:611
lldb_private::UniqueCStringMap< uint32_t >
lldb_private::Symtab::RegisterBacklogEntry
void RegisterBacklogEntry(const NameToIndexMap::Entry &entry, const char *decl_context, const std::set< const char * > &class_contexts)
Definition: Symtab.cpp:391
lldb-private.h
Symbol.h
lldb_private::Symtab::GetNumSymbols
size_t GetNumSymbols() const
Definition: Symtab.cpp:62
lldb_private::Symtab::Reserve
void Reserve(size_t count)
Definition: Symtab.cpp:37
lldb_private::Symtab::InitNameIndexes
void InitNameIndexes()
Definition: Symtab.cpp:250
lldb_private::Symbol::IsDebug
bool IsDebug() const
Definition: Symbol.h:158
lldb_private::Symtab::AddSymbol
uint32_t AddSymbol(const Symbol &symbol)
Definition: Symtab.cpp:50
lldb_private::Symtab::CheckSymbolAtIndex
bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
Definition: Symtab.h:185
lldb_private::RichManglingContext
Uniform wrapper for access to rich mangling information from different providers.
Definition: RichManglingContext.h:25
lldb_private::Symtab::CalculateSymbolSizes
void CalculateSymbolSizes()
Definition: Symtab.cpp:927
lldb_private::Symtab::FileRangeToIndexMapCompare::m_symtab
const Symtab & m_symtab
Definition: Symtab.h:165
lldb_private::Symtab::AppendSymbolIndexesWithType
uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes, uint32_t start_idx=0, uint32_t end_index=UINT32_MAX) const
Definition: Symtab.cpp:438
lldb_private::Symtab
Definition: Symtab.h:21
lldb_private::Symtab::AppendSymbolIndexesWithName
uint32_t AppendSymbolIndexesWithName(ConstString symbol_name, std::vector< uint32_t > &matches)
Definition: Symtab.cpp:589
uint32_t
lldb_private::Symtab::operator=
const Symtab & operator=(const Symtab &)=delete
lldb_private::Symtab::GetObjectFile
ObjectFile * GetObjectFile()
Definition: Symtab.h:140
lldb_private::RangeDataVector
Definition: RangeMap.h:398
lldb_private::Symtab::FileRangeToIndexMapCompare::operator()
bool operator()(const uint32_t a_data, const uint32_t b_data) const
Definition: Symtab.h:149
lldb_private::Symtab::FindSymbolByID
Symbol * FindSymbolByID(lldb::user_id_t uid) const
Definition: Symtab.cpp:191
UINT32_MAX
#define UINT32_MAX
Definition: lldb-defines.h:31
lldb_private::Symtab::Visibility
Visibility
Definition: Symtab.h:32
lldb_private::Symtab::AppendSymbolIndexesWithNameAndType
uint32_t AppendSymbolIndexesWithNameAndType(ConstString symbol_name, lldb::SymbolType symbol_type, std::vector< uint32_t > &matches)
Definition: Symtab.cpp:629
lldb_private::Symtab::m_name_to_index
UniqueCStringMap< uint32_t > m_name_to_index
Definition: Symtab.h:176
lldb_private::Symtab::SectionFileAddressesChanged
void SectionFileAddressesChanged()
Definition: Symtab.cpp:67
lldb_private::SortOrder
SortOrder
Definition: lldb-private-enumerations.h:110
lldb_private::Symbol::IsWeak
bool IsWeak() const
Definition: Symbol.h:170
lldb::user_id_t
uint64_t user_id_t
Definition: lldb-types.h:84
lldb_private::Symtab::RegisterMangledNameEntry
void RegisterMangledNameEntry(uint32_t value, std::set< const char * > &class_contexts, std::vector< std::pair< NameToIndexMap::Entry, const char * >> &backlog, RichManglingContext &rmc)
Definition: Symtab.cpp:340
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Symtab::eVisibilityAny
@ eVisibilityAny
Definition: Symtab.h:32
lldb_private::Symtab::AppendSymbolIndexesMatchingRegExAndType
uint32_t AppendSymbolIndexesMatchingRegExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes)
Definition: Symtab.cpp:667
lldb_private::Symtab::eDebugAny
@ eDebugAny
Definition: Symtab.h:29
lldb_private::Symtab::FindAllSymbolsWithNameAndType
void FindAllSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, std::vector< uint32_t > &symbol_indexes)
Definition: Symtab.cpp:733
lldb_private::Symtab::SortSymbolIndexesByValue
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition: Symtab.cpp:564
Entry
FormatEntity::Entry Entry
Definition: FormatEntity.cpp:82
lldb_private::Symtab::m_name_indexes_computed
bool m_name_indexes_computed
Definition: Symtab.h:182
lldb_private::Symtab::Resize
Symbol * Resize(size_t count)
Definition: Symtab.cpp:43
lldb_private::Symtab::collection
std::vector< Symbol > collection
Definition: Symtab.h:143
lldb_private::Symtab::IndexCollection
std::vector< uint32_t > IndexCollection
Definition: Symtab.h:23
lldb_private::Symtab::FindAllSymbolsMatchingRexExAndType
void FindAllSymbolsMatchingRexExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector< uint32_t > &symbol_indexes)
Definition: Symtab.cpp:770
lldb_private::Symtab::SymbolIndicesToSymbolContextList
void SymbolIndicesToSymbolContextList(std::vector< uint32_t > &symbol_indexes, SymbolContextList &sc_list)
Definition: Symtab.cpp:987
lldb_private::ObjectFile
Definition: ObjectFile.h:58
lldb_private::Symtab::ForEachSymbolContainingFileAddress
void ForEachSymbolContainingFileAddress(lldb::addr_t file_addr, std::function< bool(Symbol *)> const &callback)
Definition: Symtab.cpp:964