File indexing completed on 2026-05-10 08:42:52
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLDB_SYMBOL_SYMTAB_H
0010 #define LLDB_SYMBOL_SYMTAB_H
0011
0012 #include "lldb/Core/UniqueCStringMap.h"
0013 #include "lldb/Symbol/Symbol.h"
0014 #include "lldb/Utility/RangeMap.h"
0015 #include "lldb/lldb-private.h"
0016 #include <map>
0017 #include <mutex>
0018 #include <vector>
0019
0020 namespace lldb_private {
0021
0022 class Symtab {
0023 public:
0024 typedef std::vector<uint32_t> IndexCollection;
0025 typedef UniqueCStringMap<uint32_t> NameToIndexMap;
0026
0027 enum Debug {
0028 eDebugNo,
0029 eDebugYes,
0030 eDebugAny
0031 };
0032
0033 enum Visibility { eVisibilityAny, eVisibilityExtern, eVisibilityPrivate };
0034
0035 Symtab(ObjectFile *objfile);
0036 ~Symtab();
0037
0038 void PreloadSymbols();
0039 void Reserve(size_t count);
0040 Symbol *Resize(size_t count);
0041 uint32_t AddSymbol(const Symbol &symbol);
0042 size_t GetNumSymbols() const;
0043 void SectionFileAddressesChanged();
0044 void
0045 Dump(Stream *s, Target *target, SortOrder sort_type,
0046 Mangled::NamePreference name_preference = Mangled::ePreferDemangled);
0047 void Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes,
0048 Mangled::NamePreference name_preference =
0049 Mangled::ePreferDemangled) const;
0050 uint32_t GetIndexForSymbol(const Symbol *symbol) const;
0051 std::recursive_mutex &GetMutex() { return m_mutex; }
0052 Symbol *FindSymbolByID(lldb::user_id_t uid) const;
0053 Symbol *SymbolAtIndex(size_t idx);
0054 const Symbol *SymbolAtIndex(size_t idx) const;
0055 Symbol *FindSymbolWithType(lldb::SymbolType symbol_type,
0056 Debug symbol_debug_type,
0057 Visibility symbol_visibility, uint32_t &start_idx);
0058
0059
0060
0061
0062
0063 const Symbol *GetParent(Symbol *symbol) const;
0064 uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type,
0065 std::vector<uint32_t> &indexes,
0066 uint32_t start_idx = 0,
0067 uint32_t end_index = UINT32_MAX) const;
0068 uint32_t AppendSymbolIndexesWithTypeAndFlagsValue(
0069 lldb::SymbolType symbol_type, uint32_t flags_value,
0070 std::vector<uint32_t> &indexes, uint32_t start_idx = 0,
0071 uint32_t end_index = UINT32_MAX) const;
0072 uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type,
0073 Debug symbol_debug_type,
0074 Visibility symbol_visibility,
0075 std::vector<uint32_t> &matches,
0076 uint32_t start_idx = 0,
0077 uint32_t end_index = UINT32_MAX) const;
0078 uint32_t AppendSymbolIndexesWithName(ConstString symbol_name,
0079 std::vector<uint32_t> &matches);
0080 uint32_t AppendSymbolIndexesWithName(ConstString symbol_name,
0081 Debug symbol_debug_type,
0082 Visibility symbol_visibility,
0083 std::vector<uint32_t> &matches);
0084 uint32_t AppendSymbolIndexesWithNameAndType(ConstString symbol_name,
0085 lldb::SymbolType symbol_type,
0086 std::vector<uint32_t> &matches);
0087 uint32_t AppendSymbolIndexesWithNameAndType(ConstString symbol_name,
0088 lldb::SymbolType symbol_type,
0089 Debug symbol_debug_type,
0090 Visibility symbol_visibility,
0091 std::vector<uint32_t> &matches);
0092 uint32_t AppendSymbolIndexesMatchingRegExAndType(
0093 const RegularExpression ®ex, lldb::SymbolType symbol_type,
0094 std::vector<uint32_t> &indexes,
0095 Mangled::NamePreference name_preference = Mangled::ePreferDemangled);
0096 uint32_t AppendSymbolIndexesMatchingRegExAndType(
0097 const RegularExpression ®ex, lldb::SymbolType symbol_type,
0098 Debug symbol_debug_type, Visibility symbol_visibility,
0099 std::vector<uint32_t> &indexes,
0100 Mangled::NamePreference name_preference =
0101 Mangled::NamePreference::ePreferDemangled);
0102 void FindAllSymbolsWithNameAndType(ConstString name,
0103 lldb::SymbolType symbol_type,
0104 std::vector<uint32_t> &symbol_indexes);
0105 void FindAllSymbolsWithNameAndType(ConstString name,
0106 lldb::SymbolType symbol_type,
0107 Debug symbol_debug_type,
0108 Visibility symbol_visibility,
0109 std::vector<uint32_t> &symbol_indexes);
0110 void FindAllSymbolsMatchingRexExAndType(
0111 const RegularExpression ®ex, lldb::SymbolType symbol_type,
0112 Debug symbol_debug_type, Visibility symbol_visibility,
0113 std::vector<uint32_t> &symbol_indexes,
0114 Mangled::NamePreference name_preference = Mangled::ePreferDemangled);
0115 Symbol *FindFirstSymbolWithNameAndType(ConstString name,
0116 lldb::SymbolType symbol_type,
0117 Debug symbol_debug_type,
0118 Visibility symbol_visibility);
0119 Symbol *FindSymbolAtFileAddress(lldb::addr_t file_addr);
0120 Symbol *FindSymbolContainingFileAddress(lldb::addr_t file_addr);
0121 void ForEachSymbolContainingFileAddress(
0122 lldb::addr_t file_addr, std::function<bool(Symbol *)> const &callback);
0123 void FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
0124 SymbolContextList &sc_list);
0125
0126 void SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
0127 bool remove_duplicates) const;
0128
0129 static void DumpSymbolHeader(Stream *s);
0130
0131 void Finalize();
0132
0133 void AppendSymbolNamesToMap(const IndexCollection &indexes,
0134 bool add_demangled, bool add_mangled,
0135 NameToIndexMap &name_to_index_map) const;
0136
0137 ObjectFile *GetObjectFile() const { return m_objfile; }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
0156 bool &uuid_mismatch);
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 bool Encode(DataEncoder &encoder) const;
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 std::string GetCacheKey();
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203 void SaveToCache();
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216 bool LoadFromCache();
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 bool GetWasLoadedFromCache() const {
0229 return m_loaded_from_cache;
0230 }
0231 void SetWasLoadedFromCache() {
0232 m_loaded_from_cache = true;
0233 }
0234 bool GetWasSavedToCache() const {
0235 return m_saved_to_cache;
0236 }
0237 void SetWasSavedToCache() {
0238 m_saved_to_cache = true;
0239 }
0240
0241
0242 protected:
0243 typedef std::vector<Symbol> collection;
0244 typedef collection::iterator iterator;
0245 typedef collection::const_iterator const_iterator;
0246 class FileRangeToIndexMapCompare {
0247 public:
0248 FileRangeToIndexMapCompare(const Symtab &symtab) : m_symtab(symtab) {}
0249 bool operator()(const uint32_t a_data, const uint32_t b_data) const {
0250 return rank(a_data) > rank(b_data);
0251 }
0252
0253 private:
0254
0255 int rank(const uint32_t data) const {
0256 const Symbol &symbol = *m_symtab.SymbolAtIndex(data);
0257 if (symbol.IsExternal())
0258 return 3;
0259 if (symbol.IsWeak())
0260 return 2;
0261 if (symbol.IsDebug())
0262 return 0;
0263 return 1;
0264 }
0265 const Symtab &m_symtab;
0266 };
0267 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t, 0,
0268 FileRangeToIndexMapCompare>
0269 FileRangeToIndexMap;
0270 void InitNameIndexes();
0271 void InitAddressIndexes();
0272
0273 ObjectFile *m_objfile;
0274 collection m_symbols;
0275 FileRangeToIndexMap m_file_addr_to_index;
0276
0277
0278 std::map<lldb::FunctionNameType, UniqueCStringMap<uint32_t>>
0279 m_name_to_symbol_indices;
0280 mutable std::recursive_mutex
0281 m_mutex;
0282 bool m_file_addr_to_index_computed : 1, m_name_indexes_computed : 1,
0283 m_loaded_from_cache : 1, m_saved_to_cache : 1;
0284
0285 private:
0286 UniqueCStringMap<uint32_t> &
0287 GetNameToSymbolIndexMap(lldb::FunctionNameType type) {
0288 auto map = m_name_to_symbol_indices.find(type);
0289 assert(map != m_name_to_symbol_indices.end());
0290 return map->second;
0291 }
0292 bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type,
0293 Visibility symbol_visibility) const {
0294 switch (symbol_debug_type) {
0295 case eDebugNo:
0296 if (m_symbols[idx].IsDebug())
0297 return false;
0298 break;
0299
0300 case eDebugYes:
0301 if (!m_symbols[idx].IsDebug())
0302 return false;
0303 break;
0304
0305 case eDebugAny:
0306 break;
0307 }
0308
0309 switch (symbol_visibility) {
0310 case eVisibilityAny:
0311 return true;
0312
0313 case eVisibilityExtern:
0314 return m_symbols[idx].IsExternal();
0315
0316 case eVisibilityPrivate:
0317 return !m_symbols[idx].IsExternal();
0318 }
0319 return false;
0320 }
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339 uint32_t GetNameIndexes(ConstString symbol_name,
0340 std::vector<uint32_t> &indexes);
0341
0342 void SymbolIndicesToSymbolContextList(std::vector<uint32_t> &symbol_indexes,
0343 SymbolContextList &sc_list);
0344
0345 void RegisterMangledNameEntry(
0346 uint32_t value, std::set<const char *> &class_contexts,
0347 std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog,
0348 RichManglingContext &rmc);
0349
0350 void RegisterBacklogEntry(const NameToIndexMap::Entry &entry,
0351 const char *decl_context,
0352 const std::set<const char *> &class_contexts);
0353
0354 Symtab(const Symtab &) = delete;
0355 const Symtab &operator=(const Symtab &) = delete;
0356 };
0357
0358 }
0359
0360 #endif