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"
15#include "lldb/lldb-private.h"
16#include <atomic>
17#include <map>
18#include <mutex>
19#include <optional>
20#include <vector>
21
22namespace lldb_private {
23
24class Symtab {
25public:
26 typedef std::vector<uint32_t> IndexCollection;
28
29 enum Debug {
30 eDebugNo, // Not a debug symbol
31 eDebugYes, // A debug symbol
33 };
34
36
37 Symtab(ObjectFile *objfile);
39
40 void PreloadSymbols();
41 void Reserve(size_t count);
42 Symbol *Resize(size_t count);
43 uint32_t AddSymbol(const Symbol &symbol);
44 size_t GetNumSymbols() const;
45
46 /// Set whether this symbol table has a debug map.
47 ///
48 /// A debug map is a set of symbols that refer to object files containing
49 /// the debug information, such as the N_OSO entries in a Mach-O symbol
50 /// table. Object file plug-ins whose formats have debug maps set this when
51 /// they parse the symbol table, so clients can find out if it is worth
52 /// searching the symbol table for those symbols without needing to iterate
53 /// over all of the symbols themselves. This value is saved in the symbol
54 /// table cache.
55 ///
56 /// AddSymbol() doesn't change this value, so code that adds debug map
57 /// symbols after the symbol table is parsed, such as a SymbolFile's
58 /// AddOrModifySymbols(), must call this as well.
59 void SetHasDebugMap(bool has_debug_map);
60
61 /// Check if this symbol table has a debug map, see SetHasDebugMap().
62 ///
63 /// \return
64 /// True if the object file plug-in found a debug map when it parsed this
65 /// symbol table, false otherwise.
66 bool HasDebugMap() const;
67
68 /// Set whether this symbol table contains any source file symbols.
69 ///
70 /// Object file plug-ins that know this when they parse the symbol table
71 /// can set it, so HasSourceFiles() doesn't need to iterate over all of the
72 /// symbols. This value is saved in the symbol table cache.
73 void SetHasSourceFiles(bool has_source_files);
74
75 /// Check if this symbol table contains any source file symbols.
76 ///
77 /// If the object file plug-in didn't set this value with
78 /// SetHasSourceFiles(), it is calculated the first time it is requested and
79 /// then cached. AddSymbol() updates the value, and Resize() clears it so it
80 /// is calculated again. Modifying symbols directly, through the pointer
81 /// returned by Resize() or SymbolAtIndex(), does not update the value.
82 ///
83 /// \return
84 /// True if any symbol is an eSymbolTypeSourceFile symbol, false otherwise.
85 bool HasSourceFiles() const;
86
88 void
89 Dump(Stream *s, Target *target, SortOrder sort_type,
91 void Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes,
92 Mangled::NamePreference name_preference =
94 uint32_t GetIndexForSymbol(const Symbol *symbol) const;
95 std::recursive_mutex &GetMutex() { return m_mutex; }
97 Symbol *SymbolAtIndex(size_t idx);
98 const Symbol *SymbolAtIndex(size_t idx) const;
100 Debug symbol_debug_type,
101 Visibility symbol_visibility, uint32_t &start_idx);
102 /// Get the parent symbol for the given symbol.
103 ///
104 /// Many symbols in symbol tables are scoped by other symbols that
105 /// contain one or more symbol. This function will look for such a
106 /// containing symbol and return it if there is one.
107 const Symbol *GetParent(Symbol *symbol) const;
109 std::vector<uint32_t> &indexes,
110 uint32_t start_idx = 0,
111 uint32_t end_index = UINT32_MAX) const;
113 lldb::SymbolType symbol_type, uint32_t flags_value,
114 std::vector<uint32_t> &indexes, uint32_t start_idx = 0,
115 uint32_t end_index = UINT32_MAX) const;
117 Debug symbol_debug_type,
118 Visibility symbol_visibility,
119 std::vector<uint32_t> &matches,
120 uint32_t start_idx = 0,
121 uint32_t end_index = UINT32_MAX) const;
122 uint32_t AppendSymbolIndexesWithName(ConstString symbol_name,
123 std::vector<uint32_t> &matches);
124 uint32_t AppendSymbolIndexesWithName(ConstString symbol_name,
125 Debug symbol_debug_type,
126 Visibility symbol_visibility,
127 std::vector<uint32_t> &matches);
129 lldb::SymbolType symbol_type,
130 std::vector<uint32_t> &matches);
132 lldb::SymbolType symbol_type,
133 Debug symbol_debug_type,
134 Visibility symbol_visibility,
135 std::vector<uint32_t> &matches);
137 const RegularExpression &regex, lldb::SymbolType symbol_type,
138 std::vector<uint32_t> &indexes,
141 const RegularExpression &regex, lldb::SymbolType symbol_type,
142 Debug symbol_debug_type, Visibility symbol_visibility,
143 std::vector<uint32_t> &indexes,
144 Mangled::NamePreference name_preference =
147 lldb::SymbolType symbol_type,
148 std::vector<uint32_t> &symbol_indexes);
150 lldb::SymbolType symbol_type,
151 Debug symbol_debug_type,
152 Visibility symbol_visibility,
153 std::vector<uint32_t> &symbol_indexes);
155 const RegularExpression &regex, lldb::SymbolType symbol_type,
156 Debug symbol_debug_type, Visibility symbol_visibility,
157 std::vector<uint32_t> &symbol_indexes,
160 lldb::SymbolType symbol_type,
161 Debug symbol_debug_type,
162 Visibility symbol_visibility);
166 lldb::addr_t file_addr, std::function<bool(Symbol *)> const &callback);
167 void FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
168 SymbolContextList &sc_list);
169
170 void SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
171 bool remove_duplicates) const;
172
173 static void DumpSymbolHeader(Stream *s);
174
175 void Finalize();
176
177 ObjectFile *GetObjectFile() const { return m_objfile; }
178
179 /// Decode a serialized version of this object from data.
180 ///
181 /// \param data
182 /// The decoder object that references the serialized data.
183 ///
184 /// \param offset_ptr
185 /// A pointer that contains the offset from which the data will be decoded
186 /// from that gets updated as data gets decoded.
187 ///
188 /// \return
189 /// True if the symbol table is successfully decoded and can be used,
190 /// false otherwise.
191 bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr);
192
193 /// Encode this object into a data encoder object.
194 ///
195 /// This allows this object to be serialized to disk. The object file must
196 /// have a valid Signature in order to be serialized as it is used to make
197 /// sure the cached information matches when cached data is loaded at a later
198 /// time. If the object file doesn't have a valid signature false will be
199 /// returned and it will indicate we should not cache this data.
200 ///
201 /// \param encoder
202 /// A data encoder object that serialized bytes will be encoded into.
203 ///
204 /// \return
205 /// True if the symbol table's object file can generate a valid signature
206 /// and all data for the symbol table was encoded, false otherwise.
207 bool Encode(DataEncoder &encoder) const;
208
209 /// Get the cache key string for this symbol table.
210 ///
211 /// The cache key must start with the module's cache key and is followed
212 /// by information that indicates this key is for caching the symbol table
213 /// contents and should also include the has of the object file. A module can
214 /// be represented by an ObjectFile object for the main executable, but can
215 /// also have a symbol file that is from the same or a different object file.
216 /// This means we might have two symbol tables cached in the index cache, one
217 /// for the main executable and one for the symbol file.
218 ///
219 /// \return
220 /// The unique cache key used to save and retrieve data from the index cache.
221 std::string GetCacheKey();
222
223 /// Save the symbol table data out into a cache.
224 ///
225 /// The symbol table will only be saved to a cache file if caching is enabled.
226 ///
227 /// We cache the contents of the symbol table since symbol tables in LLDB take
228 /// some time to initialize. This is due to the many sources for data that are
229 /// used to create a symbol table:
230 /// - standard symbol table
231 /// - dynamic symbol table (ELF)
232 /// - compressed debug info sections
233 /// - unwind information
234 /// - function pointers found in runtimes for global constructor/destructors
235 /// - other sources.
236 /// All of the above sources are combined and one symbol table results after
237 /// all sources have been considered.
238 void SaveToCache();
239
240 /// Load the symbol table from the index cache.
241 ///
242 /// Quickly load the finalized symbol table from the index cache. This saves
243 /// time when the debugger starts up. The index cache file for the symbol
244 /// table has the modification time set to the same time as the main module.
245 /// If the cache file exists and the modification times match, we will load
246 /// the symbol table from the serialized cache file.
247 ///
248 /// \return
249 /// True if the symbol table was successfully loaded from the index cache,
250 /// false if the symbol table wasn't cached or was out of date.
251 bool LoadFromCache();
252
253
254 /// Accessors for the bool that indicates if the debug info index was loaded
255 /// from, or saved to the module index cache.
256 ///
257 /// In statistics it is handy to know if a module's debug info was loaded from
258 /// or saved to the cache. When the debug info index is loaded from the cache
259 /// startup times can be faster. When the cache is enabled and the debug info
260 /// index is saved to the cache, debug sessions can be slower. These accessors
261 /// can be accessed by the statistics and emitted to help track these costs.
262 /// \{
264 SetStateFlags(lldb::eSymtabStateLoadedFromCache);
265 }
266 void SetWasSavedToCache() { SetStateFlags(lldb::eSymtabStateSavedToCache); }
267 /// \}
268
269 /// Returns a bitmask of lldb::SymtabState flags describing the state of this
270 /// symbol table.
271 ///
272 /// This doesn't take the symbol table's mutex, so it doesn't wait for a
273 /// parse on another thread to finish.
274 uint32_t GetState() const { return m_state.load(); }
276 SetStateFlags(lldb::eSymtabStateParsedFromObjectFile);
277 }
279 SetStateFlags(lldb::eSymtabStateSymbolFileAddedSymbols);
280 }
281
282protected:
283 typedef std::vector<Symbol> collection;
284 typedef collection::iterator iterator;
285 typedef collection::const_iterator const_iterator;
287 public:
288 FileRangeToIndexMapCompare(const Symtab &symtab) : m_symtab(symtab) {}
289 bool operator()(const uint32_t a_data, const uint32_t b_data) const {
290 return rank(a_data) > rank(b_data);
291 }
292
293 private:
294 // How much preferred is this symbol?
295 int rank(const uint32_t data) const {
296 const Symbol &symbol = *m_symtab.SymbolAtIndex(data);
297 if (symbol.IsExternal())
298 return 3;
299 if (symbol.IsWeak())
300 return 2;
301 if (symbol.IsDebug())
302 return 0;
303 return 1;
304 }
306 };
307 typedef RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t, 0,
310 void InitNameIndexes();
311 void InitAddressIndexes();
312 void CalculateHasSourceFiles() const;
313
314 /// Provide thread safety for this symbol table.
315 mutable std::recursive_mutex m_mutex;
316
320
321 /// Maps function names to symbol indices (grouped by FunctionNameTypes)
322 std::map<lldb::FunctionNameType, UniqueCStringMap<uint32_t>>
326
327 /// Set by the object file plug-in, see SetHasDebugMap().
328 bool m_has_debug_map = false;
329
330 /// Set by the object file plug-in, or calculated and cached by
331 /// HasSourceFiles(). Unset when neither has happened yet.
332 mutable std::optional<bool> m_has_source_files;
333
334 /// A bitmask of lldb::SymtabState flags.
335 std::atomic<uint32_t> m_state = lldb::eSymtabStateNone;
336
337private:
338 void SetStateFlags(lldb::SymtabState state) { m_state.fetch_or(state); }
340 GetNameToSymbolIndexMap(lldb::FunctionNameType type) {
341 auto map = m_name_to_symbol_indices.find(type);
342 assert(map != m_name_to_symbol_indices.end());
343 return map->second;
344 }
345 bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type,
346 Visibility symbol_visibility) const {
347 switch (symbol_debug_type) {
348 case eDebugNo:
349 if (m_symbols[idx].IsDebug())
350 return false;
351 break;
352
353 case eDebugYes:
354 if (!m_symbols[idx].IsDebug())
355 return false;
356 break;
357
358 case eDebugAny:
359 break;
360 }
361
362 switch (symbol_visibility) {
363 case eVisibilityAny:
364 return true;
365
367 return m_symbols[idx].IsExternal();
368
370 return !m_symbols[idx].IsExternal();
371 }
372 return false;
373 }
374
375 /// A helper function that looks up full function names.
376 ///
377 /// We generate unique names for synthetic symbols so that users can look
378 /// them up by name when needed. But because doing so is uncommon in normal
379 /// debugger use, we trade off some performance at lookup time for faster
380 /// symbol table building by detecting these symbols and generating their
381 /// names lazily, rather than adding them to the normal symbol indexes. This
382 /// function does the job of first consulting the name indexes, and if that
383 /// fails it extracts the information it needs from the synthetic name and
384 /// locates the symbol.
385 ///
386 /// @param[in] symbol_name The symbol name to search for.
387 ///
388 /// @param[out] indexes The vector if symbol indexes to update with results.
389 ///
390 /// @returns The number of indexes added to the index vector. Zero if no
391 /// matches were found.
392 uint32_t GetNameIndexes(ConstString symbol_name,
393 std::vector<uint32_t> &indexes);
394
395 void SymbolIndicesToSymbolContextList(std::vector<uint32_t> &symbol_indexes,
396 SymbolContextList &sc_list);
397
399 uint32_t value, const Mangled::FunctionNameParts &parts,
400 std::set<const char *> &class_contexts,
401 std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog);
402
403 void RegisterBacklogEntry(const NameToIndexMap::Entry &entry,
404 const char *decl_context,
405 const std::set<const char *> &class_contexts);
406
407 Symtab(const Symtab &) = delete;
408 const Symtab &operator=(const Symtab &) = delete;
409};
410
411} // namespace lldb_private
412
413#endif // LLDB_SYMBOL_SYMTAB_H
A uniqued constant string class.
Definition ConstString.h:40
An binary data encoding class.
Definition DataEncoder.h:42
An data extractor class.
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:47
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Defines a list of symbol context objects.
bool IsExternal() const
Definition Symbol.h:225
bool IsDebug() const
Definition Symbol.h:221
bool IsWeak() const
Definition Symbol.h:233
bool operator()(const uint32_t a_data, const uint32_t b_data) const
Definition Symtab.h:289
FileRangeToIndexMapCompare(const Symtab &symtab)
Definition Symtab.h:288
int rank(const uint32_t data) const
Definition Symtab.h:295
Symbol * FindSymbolByID(lldb::user_id_t uid) const
Definition Symtab.cpp:253
void RegisterMangledNameEntry(uint32_t value, const Mangled::FunctionNameParts &parts, std::set< const char * > &class_contexts, std::vector< std::pair< NameToIndexMap::Entry, const char * > > &backlog)
Definition Symtab.cpp:410
Symbol * SymbolAtIndex(size_t idx)
Definition Symtab.cpp:262
Symbol * FindFirstSymbolWithNameAndType(ConstString name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
Definition Symtab.cpp:850
bool m_name_indexes_computed
Definition Symtab.h:325
std::atomic< uint32_t > m_state
A bitmask of lldb::SymtabState flags.
Definition Symtab.h:335
void ForEachSymbolContainingFileAddress(lldb::addr_t file_addr, std::function< bool(Symbol *)> const &callback)
Definition Symtab.cpp:1038
Symbol * Resize(size_t count)
Definition Symtab.cpp:55
collection::iterator iterator
Definition Symtab.h:284
Symbol * FindSymbolAtFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1007
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1022
FileRangeToIndexMap m_file_addr_to_index
Definition Symtab.h:319
std::recursive_mutex m_mutex
Provide thread safety for this symbol table.
Definition Symtab.h:315
Symbol * FindSymbolWithType(lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t &start_idx)
Definition Symtab.cpp:784
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition Symtab.cpp:612
uint32_t AddSymbol(const Symbol &symbol)
Definition Symtab.cpp:68
std::optional< bool > m_has_source_files
Set by the object file plug-in, or calculated and cached by HasSourceFiles().
Definition Symtab.h:332
void FindAllSymbolsMatchingRexExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector< uint32_t > &symbol_indexes, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition Symtab.cpp:838
uint32_t GetNameIndexes(ConstString symbol_name, std::vector< uint32_t > &indexes)
A helper function that looks up full function names.
Definition Symtab.cpp:637
bool HasDebugMap() const
Check if this symbol table has a debug map, see SetHasDebugMap().
Definition Symtab.cpp:99
void Dump(Stream *s, Target *target, SortOrder sort_type, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition Symtab.cpp:121
uint32_t AppendSymbolIndexesWithName(ConstString symbol_name, std::vector< uint32_t > &matches)
Definition Symtab.cpp:669
void RegisterBacklogEntry(const NameToIndexMap::Entry &entry, const char *decl_context, const std::set< const char * > &class_contexts)
Definition Symtab.cpp:463
static void DumpSymbolHeader(Stream *s)
Definition Symtab.cpp:231
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:486
void SetHasDebugMap(bool has_debug_map)
Set whether this symbol table has a debug map.
Definition Symtab.cpp:94
void SaveToCache()
Save the symbol table data out into a cache.
Definition Symtab.cpp:1161
std::recursive_mutex & GetMutex()
Definition Symtab.h:95
void SetWasLoadedFromCache()
Accessors for the bool that indicates if the debug info index was loaded from, or saved to the module...
Definition Symtab.h:263
std::vector< uint32_t > IndexCollection
Definition Symtab.h:26
ObjectFile * GetObjectFile() const
Definition Symtab.h:177
bool m_has_debug_map
Set by the object file plug-in, see SetHasDebugMap().
Definition Symtab.h:328
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
Decode a serialized version of this object from data.
Definition Symtab.cpp:1311
size_t GetNumSymbols() const
Definition Symtab.cpp:83
const Symtab & operator=(const Symtab &)=delete
ObjectFile * m_objfile
Definition Symtab.h:317
uint32_t GetState() const
Returns a bitmask of lldb::SymtabState flags describing the state of this symbol table.
Definition Symtab.h:274
uint32_t GetIndexForSymbol(const Symbol *symbol) const
Definition Symtab.cpp:547
Symtab(const Symtab &)=delete
void SectionFileAddressesChanged()
Definition Symtab.cpp:116
bool m_file_addr_to_index_computed
Definition Symtab.h:324
bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
Definition Symtab.h:345
Symtab(ObjectFile *objfile)
Definition Symtab.cpp:35
std::map< lldb::FunctionNameType, UniqueCStringMap< uint32_t > > m_name_to_symbol_indices
Maps function names to symbol indices (grouped by FunctionNameTypes)
Definition Symtab.h:323
void FindAllSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, std::vector< uint32_t > &symbol_indexes)
Definition Symtab.cpp:804
void SetHasSourceFiles(bool has_source_files)
Set whether this symbol table contains any source file symbols.
Definition Symtab.cpp:104
bool HasSourceFiles() const
Check if this symbol table contains any source file symbols.
Definition Symtab.cpp:109
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:504
void InitAddressIndexes()
Definition Symtab.cpp:915
std::string GetCacheKey()
Get the cache key string for this symbol table.
Definition Symtab.cpp:1150
std::vector< Symbol > collection
Definition Symtab.h:283
void Reserve(size_t count)
Definition Symtab.cpp:49
const Symbol * GetParent(Symbol *symbol) const
Get the parent symbol for the given symbol.
Definition Symtab.cpp:1137
void SetStateFlags(lldb::SymtabState state)
Definition Symtab.h:338
RangeDataVector< lldb::addr_t, lldb::addr_t, uint32_t, 0, FileRangeToIndexMapCompare > FileRangeToIndexMap
Definition Symtab.h:309
collection m_symbols
Definition Symtab.h:318
void CalculateHasSourceFiles() const
Definition Symtab.cpp:88
void SetSymbolFileAddedSymbols()
Definition Symtab.h:278
void SetWasSavedToCache()
Definition Symtab.h:266
bool Encode(DataEncoder &encoder) const
Encode this object into a data encoder object.
Definition Symtab.cpp:1256
collection::const_iterator const_iterator
Definition Symtab.h:285
uint32_t AppendSymbolIndexesWithNameAndType(ConstString symbol_name, lldb::SymbolType symbol_type, std::vector< uint32_t > &matches)
Definition Symtab.cpp:706
void SymbolIndicesToSymbolContextList(std::vector< uint32_t > &symbol_indexes, SymbolContextList &sc_list)
Definition Symtab.cpp:1061
bool LoadFromCache()
Load the symbol table from the index cache.
Definition Symtab.cpp:1379
void FindFunctionSymbols(ConstString name, uint32_t name_type_mask, SymbolContextList &sc_list)
Definition Symtab.cpp:1079
void SetWasParsedFromObjectFile()
Definition Symtab.h:275
UniqueCStringMap< uint32_t > NameToIndexMap
Definition Symtab.h:27
UniqueCStringMap< uint32_t > & GetNameToSymbolIndexMap(lldb::FunctionNameType type)
Definition Symtab.h:340
uint32_t AppendSymbolIndexesMatchingRegExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition Symtab.cpp:736
#define UINT32_MAX
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:86
SymbolType
Symbol types.
uint64_t user_id_t
Definition lldb-types.h:83
uint64_t addr_t
Definition lldb-types.h:80
The parts of a mangled function name used in the symbol table's indices.
Definition Mangled.h:63