LLDB mainline
Symtab.cpp
Go to the documentation of this file.
1//===-- Symtab.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
9#include <array>
10#include <bitset>
11#include <map>
12#include <set>
13
15#include "lldb/Core/Module.h"
16#include "lldb/Core/Section.h"
18#include "lldb/Symbol/Symbol.h"
20#include "lldb/Symbol/Symtab.h"
23#include "lldb/Utility/Endian.h"
25#include "lldb/Utility/Stream.h"
26#include "lldb/Utility/Timer.h"
27
28#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/Support/DJB.h"
31
32using namespace lldb;
33using namespace lldb_private;
34
36 : m_objfile(objfile), m_file_addr_to_index(*this) {
37 m_name_to_symbol_indices.emplace(std::make_pair(
38 lldb::eFunctionNameTypeNone, UniqueCStringMap<uint32_t>()));
39 m_name_to_symbol_indices.emplace(std::make_pair(
40 lldb::eFunctionNameTypeBase, UniqueCStringMap<uint32_t>()));
41 m_name_to_symbol_indices.emplace(std::make_pair(
42 lldb::eFunctionNameTypeMethod, UniqueCStringMap<uint32_t>()));
43 m_name_to_symbol_indices.emplace(std::make_pair(
44 lldb::eFunctionNameTypeSelector, UniqueCStringMap<uint32_t>()));
45}
46
47Symtab::~Symtab() = default;
48
49void Symtab::Reserve(size_t count) {
50 // Clients should grab the mutex from this symbol table and lock it manually
51 // when calling this function to avoid performance issues.
52 m_symbols.reserve(count);
53}
54
55Symbol *Symtab::Resize(size_t count) {
56 // Clients should grab the mutex from this symbol table and lock it manually
57 // when calling this function to avoid performance issues.
58 m_symbols.resize(count);
59 return m_symbols.empty() ? nullptr : &m_symbols[0];
60}
61
62uint32_t Symtab::AddSymbol(const Symbol &symbol) {
63 // Clients should grab the mutex from this symbol table and lock it manually
64 // when calling this function to avoid performance issues.
65 uint32_t symbol_idx = m_symbols.size();
66 auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
67 name_to_index.Clear();
69 m_symbols.push_back(symbol);
72 return symbol_idx;
73}
74
75size_t Symtab::GetNumSymbols() const {
76 std::lock_guard<std::recursive_mutex> guard(m_mutex);
77 return m_symbols.size();
78}
79
84
85void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order,
86 Mangled::NamePreference name_preference) {
87 std::lock_guard<std::recursive_mutex> guard(m_mutex);
88
89 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
90 s->Indent();
91 const FileSpec &file_spec = m_objfile->GetFileSpec();
92 llvm::StringRef object_name;
93 if (m_objfile->GetModule())
94 object_name = m_objfile->GetModule()->GetObjectName();
95
96 if (file_spec)
97 s->Format("Symtab, file = {0}{1}{2}{3}, num_symbols = {4}",
98 file_spec.GetPath(), !object_name.empty() ? "(" : "",
99 !object_name.empty() ? object_name : "",
100 !object_name.empty() ? ")" : "", (uint64_t)m_symbols.size());
101 else
102 s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size());
103
104 if (!m_symbols.empty()) {
105 switch (sort_order) {
106 case eSortOrderNone: {
107 s->PutCString(":\n");
109 const_iterator begin = m_symbols.begin();
110 const_iterator end = m_symbols.end();
111 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
112 s->Indent();
113 pos->Dump(s, target, std::distance(begin, pos), name_preference);
114 }
115 }
116 break;
117
118 case eSortOrderByName: {
119 // Although we maintain a lookup by exact name map, the table isn't
120 // sorted by name. So we must make the ordered symbol list up ourselves.
121 s->PutCString(" (sorted by name):\n");
123
124 std::multimap<llvm::StringRef, const Symbol *> name_map;
125 for (const Symbol &symbol : m_symbols)
126 name_map.emplace(symbol.GetName().GetStringRef(), &symbol);
127
128 for (const auto &name_to_symbol : name_map) {
129 const Symbol *symbol = name_to_symbol.second;
130 s->Indent();
131 symbol->Dump(s, target, symbol - &m_symbols[0], name_preference);
132 }
133 } break;
134
135 case eSortOrderBySize: {
136 s->PutCString(" (sorted by size):\n");
138
139 std::multimap<size_t, const Symbol *, std::greater<size_t>> size_map;
140 for (const Symbol &symbol : m_symbols)
141 size_map.emplace(symbol.GetByteSize(), &symbol);
142
143 size_t idx = 0;
144 for (const auto &size_to_symbol : size_map) {
145 const Symbol *symbol = size_to_symbol.second;
146 s->Indent();
147 symbol->Dump(s, target, idx++, name_preference);
148 }
149 } break;
150
152 s->PutCString(" (sorted by address):\n");
156 const size_t num_entries = m_file_addr_to_index.GetSize();
157 for (size_t i = 0; i < num_entries; ++i) {
158 s->Indent();
159 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
160 m_symbols[symbol_idx].Dump(s, target, symbol_idx, name_preference);
161 }
162 break;
163 }
164 } else {
165 s->PutCString("\n");
166 }
167}
168
169void Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes,
170 Mangled::NamePreference name_preference) const {
171 std::lock_guard<std::recursive_mutex> guard(m_mutex);
172
173 const size_t num_symbols = GetNumSymbols();
174 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
175 s->Indent();
176 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n",
177 (uint64_t)indexes.size(), (uint64_t)m_symbols.size());
178 s->IndentMore();
179
180 if (!indexes.empty()) {
181 std::vector<uint32_t>::const_iterator pos;
182 std::vector<uint32_t>::const_iterator end = indexes.end();
184 for (pos = indexes.begin(); pos != end; ++pos) {
185 size_t idx = *pos;
186 if (idx < num_symbols) {
187 s->Indent();
188 m_symbols[idx].Dump(s, target, idx, name_preference);
189 }
190 }
191 }
192 s->IndentLess();
193}
194
196 s->Indent(" Debug symbol\n");
197 s->Indent(" |Synthetic symbol\n");
198 s->Indent(" ||Externally Visible\n");
199 s->Indent(" |||\n");
200 s->Indent("Index UserID DSX Type File Address/Value Load "
201 "Address Size Flags Name\n");
202 s->Indent("------- ------ --- --------------- ------------------ "
203 "------------------ ------------------ ---------- "
204 "----------------------------------\n");
205}
206
207static int CompareSymbolID(const void *key, const void *p) {
208 const user_id_t match_uid = *(const user_id_t *)key;
209 const user_id_t symbol_uid = ((const Symbol *)p)->GetID();
210 if (match_uid < symbol_uid)
211 return -1;
212 if (match_uid > symbol_uid)
213 return 1;
214 return 0;
215}
216
218 std::lock_guard<std::recursive_mutex> guard(m_mutex);
219
220 Symbol *symbol =
221 (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(),
222 sizeof(m_symbols[0]), CompareSymbolID);
223 return symbol;
224}
225
227 // Clients should grab the mutex from this symbol table and lock it manually
228 // when calling this function to avoid performance issues.
229 if (idx < m_symbols.size())
230 return &m_symbols[idx];
231 return nullptr;
232}
233
234const Symbol *Symtab::SymbolAtIndex(size_t idx) const {
235 // Clients should grab the mutex from this symbol table and lock it manually
236 // when calling this function to avoid performance issues.
237 if (idx < m_symbols.size())
238 return &m_symbols[idx];
239 return nullptr;
240}
241
243 // Protected function, no need to lock mutex...
245 return;
246
248 ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
250
251 // Collect all loaded language plugins.
252 std::vector<Language *> languages;
253 Language::ForEach([&languages](Language *l) {
254 languages.push_back(l);
256 });
257
258 auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
259 auto &basename_to_index =
260 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
261 auto &method_to_index =
262 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
263 auto &selector_to_index =
264 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeSelector);
265 // Create the name index vector to be able to quickly search by name
266 const size_t num_symbols = m_symbols.size();
267 name_to_index.Reserve(num_symbols);
268
269 // The "const char *" in "class_contexts" and backlog::value_type::second
270 // must come from a ConstString::GetCString()
271 std::set<const char *> class_contexts;
272 std::vector<std::pair<NameToIndexMap::Entry, const char *>> backlog;
273 backlog.reserve(num_symbols / 2);
274
275 // A single parser per mangling scheme handles all of its names, so that it
276 // can reuse its demangler. Parsers are only created for the schemes that
277 // occur, and a nullptr means that no language parses the scheme.
278 std::array<std::unique_ptr<Language::MangledNameParser>,
280 parsers;
281 std::bitset<Mangled::g_num_mangling_schemes> created_parsers;
282 auto parse_function_name =
283 [&](const Mangled &mangled) -> std::optional<Mangled::FunctionNameParts> {
285 Mangled::GetManglingScheme(mangled.GetMangledName().GetStringRef());
286 if (!created_parsers.test(scheme)) {
287 created_parsers.set(scheme);
289 parsers[scheme] = lang->CreateMangledNameParser(scheme);
290 }
291 if (!parsers[scheme])
292 return std::nullopt;
293 return parsers[scheme]->Parse(mangled);
294 };
295 for (size_t value = 0; value < num_symbols; ++value) {
296 Symbol *symbol = &m_symbols[value];
297
298 // Don't let trampolines get into the lookup by name map If we ever need
299 // the trampoline symbols to be searchable by name we can remove this and
300 // then possibly add a new bool to any of the Symtab functions that
301 // lookup symbols by name to indicate if they want trampolines. We also
302 // don't want any synthetic symbols with auto generated names in the
303 // name lookups.
304 if (symbol->IsTrampoline() || symbol->IsSyntheticWithAutoGeneratedName())
305 continue;
306
307 // If the symbol's name string matched a Mangled::ManglingScheme, it is
308 // stored in the mangled field.
309 Mangled &mangled = symbol->GetMangled();
310 if (ConstString name = mangled.GetMangledName()) {
311 name_to_index.Append(name, value);
312
313 if (symbol->ContainsLinkerAnnotations()) {
314 // If the symbol has linker annotations, also add the version without
315 // the annotations.
316 ConstString stripped = ConstString(
317 m_objfile->StripLinkerSymbolAnnotations(name.GetStringRef()));
318 name_to_index.Append(stripped, value);
319 }
320
321 const SymbolType type = symbol->GetType();
322 if (type == eSymbolTypeCode || type == eSymbolTypeResolver) {
323 if (std::optional<Mangled::FunctionNameParts> parts =
324 parse_function_name(mangled)) {
325 RegisterMangledNameEntry(value, *parts, class_contexts, backlog);
326 continue;
327 }
328 }
329 }
330
331 // Symbol name strings that didn't match a Mangled::ManglingScheme, are
332 // stored in the demangled field.
333 if (ConstString name = mangled.GetDemangledName()) {
334 name_to_index.Append(name, value);
335
336 if (symbol->ContainsLinkerAnnotations()) {
337 // If the symbol has linker annotations, also add the version without
338 // the annotations.
339 name = ConstString(
340 m_objfile->StripLinkerSymbolAnnotations(name.GetStringRef()));
341 name_to_index.Append(name, value);
342 }
343
344 // If the demangled name turns out to be an ObjC name, and is a category
345 // name, add the version without categories to the index too.
346 for (Language *lang : languages) {
347 for (auto variant : lang->GetMethodNameVariants(name)) {
348 if (variant.GetType() & lldb::eFunctionNameTypeSelector)
349 selector_to_index.Append(ConstString(variant.GetName()), value);
350 else if (variant.GetType() & lldb::eFunctionNameTypeFull)
351 name_to_index.Append(ConstString(variant.GetName()), value);
352 else if (variant.GetType() & lldb::eFunctionNameTypeMethod)
353 method_to_index.Append(ConstString(variant.GetName()), value);
354 else if (variant.GetType() & lldb::eFunctionNameTypeBase)
355 basename_to_index.Append(ConstString(variant.GetName()), value);
356 }
357 }
358 }
359 }
360
361 for (const auto &record : backlog)
362 RegisterBacklogEntry(record.first, record.second, class_contexts);
363
364 name_to_index.Sort();
365 name_to_index.SizeToFit();
366 selector_to_index.Sort();
367 selector_to_index.SizeToFit();
368 basename_to_index.Sort();
369 basename_to_index.SizeToFit();
370 method_to_index.Sort();
371 method_to_index.SizeToFit();
372}
373
375 uint32_t value, const Mangled::FunctionNameParts &parts,
376 std::set<const char *> &class_contexts,
377 std::vector<std::pair<NameToIndexMap::Entry, const char *>> &backlog) {
378 // Only register functions that have a base name.
379 if (!parts.basename)
380 return;
381
382 // The base name will be our entry's name.
383 NameToIndexMap::Entry entry(parts.basename, value);
384
385 switch (parts.kind) {
387 // This has to be a basename
388 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase).Append(entry);
389 // If there is no context (no namespaces or class scopes that come before
390 // the function name) then this also could be a fullname.
391 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone).Append(entry);
392 return;
393
395 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase).Append(entry);
396 return;
397
399 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod).Append(entry);
400 return;
401
402 // Register constructors and destructors. They are methods and create
403 // declaration contexts.
405 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod).Append(entry);
406 class_contexts.insert(parts.decl_context.GetCString());
407 return;
408
410 const char *decl_context_ccstr = parts.decl_context.GetCString();
411
412 // Register regular methods with a known declaration context.
413 if (class_contexts.count(decl_context_ccstr)) {
414 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod).Append(entry);
415 return;
416 }
417
418 // Regular methods in unknown declaration contexts are put to the backlog.
419 // We will revisit them once we processed all remaining symbols.
420 backlog.push_back(std::make_pair(entry, decl_context_ccstr));
421 return;
422 }
423 }
424 llvm_unreachable("unknown function name kind");
425}
426
428 const NameToIndexMap::Entry &entry, const char *decl_context,
429 const std::set<const char *> &class_contexts) {
430 auto &method_to_index =
431 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeMethod);
432 auto it = class_contexts.find(decl_context);
433 if (it != class_contexts.end()) {
434 method_to_index.Append(entry);
435 } else {
436 // If we got here, we have something that had a context (was inside
437 // a namespace or class) yet we don't know the entry
438 method_to_index.Append(entry);
439 auto &basename_to_index =
440 GetNameToSymbolIndexMap(lldb::eFunctionNameTypeBase);
441 basename_to_index.Append(entry);
442 }
443}
444
446 std::lock_guard<std::recursive_mutex> guard(m_mutex);
448}
449
451 bool add_demangled, bool add_mangled,
452 NameToIndexMap &name_to_index_map) const {
454 if (add_demangled || add_mangled) {
455 std::lock_guard<std::recursive_mutex> guard(m_mutex);
456
457 // Create the name index vector to be able to quickly search by name
458 const size_t num_indexes = indexes.size();
459 for (size_t i = 0; i < num_indexes; ++i) {
460 uint32_t value = indexes[i];
461 assert(i < m_symbols.size());
462 const Symbol *symbol = &m_symbols[value];
463
464 const Mangled &mangled = symbol->GetMangled();
465 if (add_demangled) {
466 if (ConstString name = mangled.GetDemangledName())
467 name_to_index_map.Append(name, value);
468 }
469
470 if (add_mangled) {
471 if (ConstString name = mangled.GetMangledName())
472 name_to_index_map.Append(name, value);
473 }
474 }
475 }
476}
477
479 std::vector<uint32_t> &indexes,
480 uint32_t start_idx,
481 uint32_t end_index) const {
482 std::lock_guard<std::recursive_mutex> guard(m_mutex);
483
484 uint32_t prev_size = indexes.size();
485
486 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
487
488 for (uint32_t i = start_idx; i < count; ++i) {
489 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
490 indexes.push_back(i);
491 }
492
493 return indexes.size() - prev_size;
494}
495
497 SymbolType symbol_type, uint32_t flags_value,
498 std::vector<uint32_t> &indexes, uint32_t start_idx,
499 uint32_t end_index) const {
500 std::lock_guard<std::recursive_mutex> guard(m_mutex);
501
502 uint32_t prev_size = indexes.size();
503
504 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
505
506 for (uint32_t i = start_idx; i < count; ++i) {
507 if ((symbol_type == eSymbolTypeAny ||
508 m_symbols[i].GetType() == symbol_type) &&
509 m_symbols[i].GetFlags() == flags_value)
510 indexes.push_back(i);
511 }
512
513 return indexes.size() - prev_size;
514}
515
517 Debug symbol_debug_type,
518 Visibility symbol_visibility,
519 std::vector<uint32_t> &indexes,
520 uint32_t start_idx,
521 uint32_t end_index) const {
522 std::lock_guard<std::recursive_mutex> guard(m_mutex);
523
524 uint32_t prev_size = indexes.size();
525
526 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index);
527
528 for (uint32_t i = start_idx; i < count; ++i) {
529 if (symbol_type == eSymbolTypeAny ||
530 m_symbols[i].GetType() == symbol_type) {
531 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
532 indexes.push_back(i);
533 }
534 }
535
536 return indexes.size() - prev_size;
537}
538
539uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const {
540 if (!m_symbols.empty()) {
541 const Symbol *first_symbol = &m_symbols[0];
542 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
543 return symbol - first_symbol;
544 }
545 return UINT32_MAX;
546}
547
551};
552
553namespace {
554struct SymbolIndexComparator {
555 const std::vector<Symbol> &symbols;
556 std::vector<lldb::addr_t> &addr_cache;
557
558 // Getting from the symbol to the Address to the File Address involves some
559 // work. Since there are potentially many symbols here, and we're using this
560 // for sorting so we're going to be computing the address many times, cache
561 // that in addr_cache. The array passed in has to be the same size as the
562 // symbols array passed into the member variable symbols, and should be
563 // initialized with LLDB_INVALID_ADDRESS.
564 // NOTE: You have to make addr_cache externally and pass it in because
565 // std::stable_sort
566 // makes copies of the comparator it is initially passed in, and you end up
567 // spending huge amounts of time copying this array...
568
569 SymbolIndexComparator(const std::vector<Symbol> &s,
570 std::vector<lldb::addr_t> &a)
571 : symbols(s), addr_cache(a) {
572 assert(symbols.size() == addr_cache.size());
573 }
574 bool operator()(uint32_t index_a, uint32_t index_b) {
575 addr_t value_a = addr_cache[index_a];
576 if (value_a == LLDB_INVALID_ADDRESS) {
577 value_a = symbols[index_a].GetAddressRef().GetFileAddress();
578 addr_cache[index_a] = value_a;
579 }
580
581 addr_t value_b = addr_cache[index_b];
582 if (value_b == LLDB_INVALID_ADDRESS) {
583 value_b = symbols[index_b].GetAddressRef().GetFileAddress();
584 addr_cache[index_b] = value_b;
585 }
586
587 if (value_a == value_b) {
588 // The if the values are equal, use the original symbol user ID
589 lldb::user_id_t uid_a = symbols[index_a].GetID();
590 lldb::user_id_t uid_b = symbols[index_b].GetID();
591 if (uid_a < uid_b)
592 return true;
593 if (uid_a > uid_b)
594 return false;
595 return false;
596 } else if (value_a < value_b)
597 return true;
598
599 return false;
600 }
601};
602}
603
604void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
605 bool remove_duplicates) const {
606 std::lock_guard<std::recursive_mutex> guard(m_mutex);
608 // No need to sort if we have zero or one items...
609 if (indexes.size() <= 1)
610 return;
611
612 // Sort the indexes in place using std::stable_sort.
613 // NOTE: The use of std::stable_sort instead of llvm::sort here is strictly
614 // for performance, not correctness. The indexes vector tends to be "close"
615 // to sorted, which the stable sort handles better.
616
617 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
618
619 SymbolIndexComparator comparator(m_symbols, addr_cache);
620 llvm::stable_sort(indexes, comparator);
621
622 // Remove any duplicates if requested
623 if (remove_duplicates) {
624 auto last = llvm::unique(indexes);
625 indexes.erase(last, indexes.end());
626 }
627}
628
630 std::vector<uint32_t> &indexes) {
631 auto &name_to_index = GetNameToSymbolIndexMap(lldb::eFunctionNameTypeNone);
632 const uint32_t count = name_to_index.GetValues(symbol_name, indexes);
633 if (count)
634 return count;
635 // Synthetic symbol names are not added to the name indexes, but they start
636 // with a prefix and end with the symbol file address. This allows users to
637 // find these symbols without having to add them to the name indexes. These
638 // queries will not happen very often since the names don't mean anything, so
639 // performance is not paramount in this case.
640 llvm::StringRef name = symbol_name.GetStringRef();
641 // String the synthetic prefix if the name starts with it.
642 if (!name.consume_front(Symbol::GetSyntheticSymbolPrefix()))
643 return 0; // Not a synthetic symbol name
644
645 // Extract the file address from the symbol name
646 unsigned long long file_address = 0;
647 if (getAsUnsignedInteger(name, /*Radix=*/16, file_address))
648 return 0; // Failed to extract the user ID as an integer
649
650 const Symbol *symbol =
651 FindSymbolAtFileAddress(static_cast<addr_t>(file_address));
652 if (symbol == nullptr)
653 return 0;
654 const uint32_t symbol_idx = GetIndexForSymbol(symbol);
655 if (symbol_idx == UINT32_MAX)
656 return 0;
657 indexes.push_back(symbol_idx);
658 return 1;
659}
660
662 std::vector<uint32_t> &indexes) {
663 std::lock_guard<std::recursive_mutex> guard(m_mutex);
664
665 if (symbol_name) {
667
668 return GetNameIndexes(symbol_name, indexes);
669 }
670 return 0;
671}
672
674 Debug symbol_debug_type,
675 Visibility symbol_visibility,
676 std::vector<uint32_t> &indexes) {
677 std::lock_guard<std::recursive_mutex> guard(m_mutex);
678
680 if (symbol_name) {
681 const size_t old_size = indexes.size();
683
684 std::vector<uint32_t> all_name_indexes;
685 const size_t name_match_count =
686 GetNameIndexes(symbol_name, all_name_indexes);
687 for (size_t i = 0; i < name_match_count; ++i) {
688 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type,
689 symbol_visibility))
690 indexes.push_back(all_name_indexes[i]);
691 }
692 return indexes.size() - old_size;
693 }
694 return 0;
695}
696
697uint32_t
699 SymbolType symbol_type,
700 std::vector<uint32_t> &indexes) {
701 std::lock_guard<std::recursive_mutex> guard(m_mutex);
702
703 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0 &&
704 symbol_type != eSymbolTypeAny) {
705 llvm::erase_if(indexes, [this, symbol_type](uint32_t index) {
706 return m_symbols[index].GetType() != symbol_type;
707 });
708 }
709 return indexes.size();
710}
711
713 ConstString symbol_name, SymbolType symbol_type,
714 Debug symbol_debug_type, Visibility symbol_visibility,
715 std::vector<uint32_t> &indexes) {
716 std::lock_guard<std::recursive_mutex> guard(m_mutex);
717
718 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type,
719 symbol_visibility, indexes) > 0 &&
720 symbol_type != eSymbolTypeAny) {
721 llvm::erase_if(indexes, [this, symbol_type](uint32_t index) {
722 return m_symbols[index].GetType() != symbol_type;
723 });
724 }
725 return indexes.size();
726}
727
729 const RegularExpression &regexp, SymbolType symbol_type,
730 std::vector<uint32_t> &indexes, Mangled::NamePreference name_preference) {
731 std::lock_guard<std::recursive_mutex> guard(m_mutex);
732
733 uint32_t prev_size = indexes.size();
734 uint32_t sym_end = m_symbols.size();
735
736 for (uint32_t i = 0; i < sym_end; i++) {
737 if (symbol_type == eSymbolTypeAny ||
738 m_symbols[i].GetType() == symbol_type) {
739 const char *name =
740 m_symbols[i].GetMangled().GetName(name_preference).AsCString(nullptr);
741 if (name) {
742 if (regexp.Execute(name))
743 indexes.push_back(i);
744 }
745 }
746 }
747 return indexes.size() - prev_size;
748}
749
751 const RegularExpression &regexp, SymbolType symbol_type,
752 Debug symbol_debug_type, Visibility symbol_visibility,
753 std::vector<uint32_t> &indexes, Mangled::NamePreference name_preference) {
754 std::lock_guard<std::recursive_mutex> guard(m_mutex);
755
756 uint32_t prev_size = indexes.size();
757 uint32_t sym_end = m_symbols.size();
758
759 for (uint32_t i = 0; i < sym_end; i++) {
760 if (symbol_type == eSymbolTypeAny ||
761 m_symbols[i].GetType() == symbol_type) {
762 if (!CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
763 continue;
764
765 const char *name =
766 m_symbols[i].GetMangled().GetName(name_preference).AsCString(nullptr);
767 if (name) {
768 if (regexp.Execute(name))
769 indexes.push_back(i);
770 }
771 }
772 }
773 return indexes.size() - prev_size;
774}
775
777 Debug symbol_debug_type,
778 Visibility symbol_visibility,
779 uint32_t &start_idx) {
780 std::lock_guard<std::recursive_mutex> guard(m_mutex);
781
782 const size_t count = m_symbols.size();
783 for (size_t idx = start_idx; idx < count; ++idx) {
784 if (symbol_type == eSymbolTypeAny ||
785 m_symbols[idx].GetType() == symbol_type) {
786 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) {
787 start_idx = idx;
788 return &m_symbols[idx];
789 }
790 }
791 }
792 return nullptr;
793}
794
795void
797 SymbolType symbol_type,
798 std::vector<uint32_t> &symbol_indexes) {
799 std::lock_guard<std::recursive_mutex> guard(m_mutex);
800
801 // Initialize all of the lookup by name indexes before converting NAME to a
802 // uniqued string NAME_STR below.
804
805 if (name) {
806 // The string table did have a string that matched, but we need to check
807 // the symbols and match the symbol_type if any was given.
808 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes);
809 }
810}
811
813 ConstString name, SymbolType symbol_type, Debug symbol_debug_type,
814 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) {
815 std::lock_guard<std::recursive_mutex> guard(m_mutex);
816
818 // Initialize all of the lookup by name indexes before converting NAME to a
819 // uniqued string NAME_STR below.
821
822 if (name) {
823 // The string table did have a string that matched, but we need to check
824 // the symbols and match the symbol_type if any was given.
825 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
826 symbol_visibility, symbol_indexes);
827 }
828}
829
831 const RegularExpression &regex, SymbolType symbol_type,
832 Debug symbol_debug_type, Visibility symbol_visibility,
833 std::vector<uint32_t> &symbol_indexes,
834 Mangled::NamePreference name_preference) {
835 std::lock_guard<std::recursive_mutex> guard(m_mutex);
836
837 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type,
838 symbol_visibility, symbol_indexes,
839 name_preference);
840}
841
843 SymbolType symbol_type,
844 Debug symbol_debug_type,
845 Visibility symbol_visibility) {
846 std::lock_guard<std::recursive_mutex> guard(m_mutex);
849
850 if (name) {
851 std::vector<uint32_t> matching_indexes;
852 // The string table did have a string that matched, but we need to check
853 // the symbols and match the symbol_type if any was given.
854 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type,
855 symbol_visibility,
856 matching_indexes)) {
857 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
858 for (pos = matching_indexes.begin(); pos != end; ++pos) {
859 Symbol *symbol = SymbolAtIndex(*pos);
860
861 if (symbol->Compare(name, symbol_type))
862 return symbol;
863 }
864 }
865 }
866 return nullptr;
867}
868
876
877// Add all the section file start address & size to the RangeVector, recusively
878// adding any children sections.
879static void AddSectionsToRangeMap(SectionList *sectlist,
880 RangeVector<addr_t, addr_t> &section_ranges) {
881 const int num_sections = sectlist->GetNumSections(0);
882 for (int i = 0; i < num_sections; i++) {
883 SectionSP sect_sp = sectlist->GetSectionAtIndex(i);
884 if (sect_sp) {
885 SectionList &child_sectlist = sect_sp->GetChildren();
886
887 // If this section has children, add the children to the RangeVector.
888 // Else add this section to the RangeVector.
889 if (child_sectlist.GetNumSections(0) > 0) {
890 AddSectionsToRangeMap(&child_sectlist, section_ranges);
891 } else {
892 // Debug info sections can share addresses with the sections that
893 // symbols are defined in.
894 size_t size = sect_sp->GetByteSize();
895 if (size > 0 && !sect_sp->ContainsOnlyDebugInfo()) {
896 addr_t base_addr = sect_sp->GetFileAddress();
898 entry.SetRangeBase(base_addr);
899 entry.SetByteSize(size);
900 section_ranges.Append(entry);
901 }
902 }
903 }
904 }
905}
906
908 // Protected function, no need to lock mutex...
909 if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
911
913 const_iterator begin = m_symbols.begin();
914 const_iterator end = m_symbols.end();
915 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
916 if (pos->ValueIsAddress()) {
917 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
918 entry.SetByteSize(pos->GetByteSize());
919 entry.data = std::distance(begin, pos);
920 m_file_addr_to_index.Append(entry);
921 }
922 }
923 const size_t num_entries = m_file_addr_to_index.GetSize();
924 if (num_entries > 0) {
926
927 // Create a RangeVector with the start & size of all the sections for
928 // this objfile. We'll need to check this for any FileRangeToIndexMap
929 // entries with an uninitialized size, which could potentially be a large
930 // number so reconstituting the weak pointer is busywork when it is
931 // invariant information.
932 SectionList *sectlist = m_objfile->GetSectionList();
933 RangeVector<addr_t, addr_t> section_ranges;
934 if (sectlist) {
935 AddSectionsToRangeMap(sectlist, section_ranges);
936 section_ranges.Sort();
937 }
938
939 // Iterate through the FileRangeToIndexMap and fill in the size for any
940 // entries that didn't already have a size from the Symbol (e.g. if we
941 // have a plain linker symbol with an address only, instead of debug info
942 // where we get an address and a size and a type, etc.)
943 for (size_t i = 0; i < num_entries; i++) {
945 m_file_addr_to_index.GetMutableEntryAtIndex(i);
946 if (entry->GetByteSize() == 0) {
947 addr_t curr_base_addr = entry->GetRangeBase();
948 const RangeVector<addr_t, addr_t>::Entry *containing_section =
949 section_ranges.FindEntryThatContains(curr_base_addr);
950
951 // Use the end of the section as the default max size of the symbol
952 addr_t sym_size = 0;
953 if (containing_section) {
954 sym_size =
955 containing_section->GetByteSize() -
956 (entry->GetRangeBase() - containing_section->GetRangeBase());
957 }
958
959 for (size_t j = i; j < num_entries; j++) {
960 FileRangeToIndexMap::Entry *next_entry =
961 m_file_addr_to_index.GetMutableEntryAtIndex(j);
962 addr_t next_base_addr = next_entry->GetRangeBase();
963 if (next_base_addr > curr_base_addr) {
964 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
965
966 // Take the difference between this symbol and the next one as
967 // its size, if it is less than the size of the section.
968 if (sym_size == 0 || size_to_next_symbol < sym_size) {
969 sym_size = size_to_next_symbol;
970 }
971 break;
972 }
973 }
974
975 if (sym_size > 0) {
976 entry->SetByteSize(sym_size);
977 Symbol &symbol = m_symbols[entry->data];
978 symbol.SetByteSize(sym_size);
979 symbol.SetSizeIsSynthesized(true);
980 }
981 }
982 }
983
984 // Sort again in case the range size changes the ordering
986 }
987 }
988}
989
991 std::lock_guard<std::recursive_mutex> guard(m_mutex);
992 // Calculate the size of symbols inside InitAddressIndexes.
994 // Shrink to fit the symbols so we don't waste memory
995 m_symbols.shrink_to_fit();
996 SaveToCache();
997}
998
1000 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1003
1004 const FileRangeToIndexMap::Entry *entry =
1005 m_file_addr_to_index.FindEntryStartsAt(file_addr);
1006 if (entry) {
1007 Symbol *symbol = SymbolAtIndex(entry->data);
1008 if (symbol->GetFileAddress() == file_addr)
1009 return symbol;
1010 }
1011 return nullptr;
1012}
1013
1015 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1016
1019
1020 const FileRangeToIndexMap::Entry *entry =
1021 m_file_addr_to_index.FindEntryThatContains(file_addr);
1022 if (entry) {
1023 Symbol *symbol = SymbolAtIndex(entry->data);
1024 if (symbol->ContainsFileAddress(file_addr))
1025 return symbol;
1026 }
1027 return nullptr;
1028}
1029
1031 addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1032 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1033
1036
1037 std::vector<uint32_t> all_addr_indexes;
1038
1039 // Get all symbols with file_addr
1040 const size_t addr_match_count =
1041 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1042 all_addr_indexes);
1043
1044 for (size_t i = 0; i < addr_match_count; ++i) {
1045 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1046 if (symbol->ContainsFileAddress(file_addr)) {
1047 if (!callback(symbol))
1048 break;
1049 }
1050 }
1051}
1052
1054 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1055 // No need to protect this call using m_mutex all other method calls are
1056 // already thread safe.
1057
1058 const bool merge_symbol_into_function = true;
1059 size_t num_indices = symbol_indexes.size();
1060 if (num_indices > 0) {
1061 SymbolContext sc;
1062 sc.module_sp = m_objfile->GetModule();
1063 for (size_t i = 0; i < num_indices; i++) {
1064 sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1065 if (sc.symbol)
1066 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1067 }
1068 }
1069}
1070
1071void Symtab::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1072 SymbolContextList &sc_list) {
1073 std::vector<uint32_t> symbol_indexes;
1074
1075 // eFunctionNameTypeAuto should be pre-resolved by a call to
1076 // Module::LookupInfo::LookupInfo()
1077 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1078
1079 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1080 std::vector<uint32_t> temp_symbol_indexes;
1081 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1082
1083 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1084 if (temp_symbol_indexes_size > 0) {
1085 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1086 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1087 SymbolContext sym_ctx;
1088 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1089 if (sym_ctx.symbol) {
1090 switch (sym_ctx.symbol->GetType()) {
1091 case eSymbolTypeCode:
1095 symbol_indexes.push_back(temp_symbol_indexes[i]);
1096 break;
1097 default:
1098 break;
1099 }
1100 }
1101 }
1102 }
1103 }
1104
1105 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1107
1108 for (lldb::FunctionNameType type :
1109 {lldb::eFunctionNameTypeBase, lldb::eFunctionNameTypeMethod,
1110 lldb::eFunctionNameTypeSelector}) {
1111 if (name_type_mask & type) {
1112 auto map = GetNameToSymbolIndexMap(type);
1113
1115 for (match = map.FindFirstValueForName(name); match != nullptr;
1116 match = map.FindNextValueForName(match)) {
1117 symbol_indexes.push_back(match->value);
1118 }
1119 }
1120 }
1121
1122 if (!symbol_indexes.empty()) {
1123 llvm::sort(symbol_indexes);
1124 symbol_indexes.erase(llvm::unique(symbol_indexes), symbol_indexes.end());
1125 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1126 }
1127}
1128
1129const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1130 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1131 if (child_idx != UINT32_MAX && child_idx > 0) {
1132 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1133 const Symbol *symbol = SymbolAtIndex(idx);
1134 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1135 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)
1136 return symbol;
1137 }
1138 }
1139 return nullptr;
1140}
1141
1142std::string Symtab::GetCacheKey() {
1143 std::string key;
1144 llvm::raw_string_ostream strm(key);
1145 // Symbol table can come from different object files for the same module. A
1146 // module can have one object file as the main executable and might have
1147 // another object file in a separate symbol file.
1148 strm << m_objfile->GetModule()->GetCacheKey() << "-symtab-"
1149 << llvm::format_hex(m_objfile->GetCacheHash(), 10);
1150 return key;
1151}
1152
1155 if (m_objfile->IsInMemory()) {
1156 if (m_objfile->GetDisableMemorySymtabCaching())
1157 return;
1158 else
1160 }
1161
1162 DataFileCache *cache = Module::GetIndexCache(type);
1163 if (!cache)
1164 return; // Caching is not enabled.
1165
1166 // Init the name indexes so we can cache them as well.
1168 const auto byte_order = endian::InlHostByteOrder();
1169 DataEncoder file(byte_order, /*addr_size=*/8);
1170 // Encode will return false if the symbol table's object file doesn't have
1171 // anything to make a signature from.
1172 if (Encode(file))
1173 if (cache->SetCachedData(GetCacheKey(), file.GetData()))
1175}
1176
1177constexpr llvm::StringLiteral kIdentifierCStrMap("CMAP");
1178
1179static void EncodeCStrMap(DataEncoder &encoder, ConstStringTable &strtab,
1180 const UniqueCStringMap<uint32_t> &cstr_map) {
1182 encoder.AppendU32(cstr_map.GetSize());
1183 for (const auto &entry: cstr_map) {
1184 // Make sure there are no empty strings.
1185 assert((bool)entry.cstring);
1186 encoder.AppendU32(strtab.Add(entry.cstring));
1187 encoder.AppendU32(entry.value);
1188 }
1189}
1190
1191bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr,
1192 const StringTableReader &strtab,
1193 UniqueCStringMap<uint32_t> &cstr_map) {
1194 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1195 if (identifier != kIdentifierCStrMap)
1196 return false;
1197 const uint32_t count = data.GetU32(offset_ptr);
1198 cstr_map.Reserve(count);
1199 for (uint32_t i=0; i<count; ++i)
1200 {
1201 llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
1202 uint32_t value = data.GetU32(offset_ptr);
1203 // No empty strings in the name indexes in Symtab
1204 if (str.empty())
1205 return false;
1206 cstr_map.Append(ConstString(str), value);
1207 }
1208 // We must sort the UniqueCStringMap after decoding it since it is a vector
1209 // of UniqueCStringMap::Entry objects which contain a ConstString and type T.
1210 // ConstString objects are sorted by "const char *" and then type T and
1211 // the "const char *" are point values that will depend on the order in which
1212 // ConstString objects are created and in which of the 256 string pools they
1213 // are created in. So after we decode all of the entries, we must sort the
1214 // name map to ensure name lookups succeed. If we encode and decode within
1215 // the same process we wouldn't need to sort, so unit testing didn't catch
1216 // this issue when first checked in.
1217 cstr_map.Sort();
1218 return true;
1219}
1220
1221constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB");
1222constexpr uint32_t CURRENT_CACHE_VERSION = 4;
1223
1224// If the size of the Symbol object changes, the serialized
1225// format likely also needs to change so the
1226// CURRENT_CACHE_VERSION number will need to be incremented.
1227#if __SIZEOF_POINTER__ == 8
1228static_assert(
1229 sizeof(lldb_private::Symbol) == 72,
1230 "Symbol size has changed, DataFileCache version likely needs updating");
1231#endif
1232
1233/// The encoding format for the symbol table is as follows:
1234///
1235/// Signature signature;
1236/// ConstStringTable strtab;
1237/// Identifier four character code: 'SYMB'
1238/// uint32_t version;
1239/// uint32_t num_symbols;
1240/// Symbol symbols[num_symbols];
1241/// uint8_t num_cstr_maps;
1242/// UniqueCStringMap<uint32_t> cstr_maps[num_cstr_maps]
1243bool Symtab::Encode(DataEncoder &encoder) const {
1244 // Name indexes must be computed before calling this function.
1246
1247 // Encode the object file's signature
1248 CacheSignature signature(m_objfile);
1249 if (!signature.Encode(encoder))
1250 return false;
1251 ConstStringTable strtab;
1252
1253 // Encode the symbol table into a separate encoder first. This allows us
1254 // gather all of the strings we will need in "strtab" as we will need to
1255 // write the string table out before the symbol table.
1256 DataEncoder symtab_encoder(encoder.GetByteOrder(),
1257 encoder.GetAddressByteSize());
1258 symtab_encoder.AppendData(kIdentifierSymbolTable);
1259 // Encode the symtab data version.
1260 symtab_encoder.AppendU32(CURRENT_CACHE_VERSION);
1261 // Encode the number of symbols.
1262 symtab_encoder.AppendU32(m_symbols.size());
1263 // Encode the symbol data for all symbols.
1264 for (const auto &symbol: m_symbols)
1265 symbol.Encode(symtab_encoder, strtab);
1266
1267 // Emit a byte for how many C string maps we emit. We will fix this up after
1268 // we emit the C string maps since we skip emitting C string maps if they are
1269 // empty.
1270 size_t num_cmaps_offset = symtab_encoder.GetByteSize();
1271 uint8_t num_cmaps = 0;
1272 symtab_encoder.AppendU8(0);
1273 for (const auto &pair: m_name_to_symbol_indices) {
1274 if (pair.second.IsEmpty())
1275 continue;
1276 ++num_cmaps;
1277 symtab_encoder.AppendU8(pair.first);
1278 EncodeCStrMap(symtab_encoder, strtab, pair.second);
1279 }
1280 if (num_cmaps > 0)
1281 symtab_encoder.PutU8(num_cmaps_offset, num_cmaps);
1282
1283 // Now that all strings have been gathered, we will emit the string table.
1284 strtab.Encode(encoder);
1285 // Followed by the symbol table data.
1286 encoder.AppendData(symtab_encoder.GetData());
1287 return true;
1288}
1289
1290bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
1291 bool &signature_mismatch) {
1292 signature_mismatch = false;
1293 CacheSignature signature;
1294 StringTableReader strtab;
1295 { // Scope for "elapsed" object below so it can measure the time parse.
1296 ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabParseTime());
1297 if (!signature.Decode(data, offset_ptr))
1298 return false;
1299 if (CacheSignature(m_objfile) != signature) {
1300 signature_mismatch = true;
1301 return false;
1302 }
1303 // We now decode the string table for all strings in the data cache file.
1304 if (!strtab.Decode(data, offset_ptr))
1305 return false;
1306
1307 // And now we can decode the symbol table with string table we just decoded.
1308 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1309 if (identifier != kIdentifierSymbolTable)
1310 return false;
1311 const uint32_t version = data.GetU32(offset_ptr);
1312 if (version != CURRENT_CACHE_VERSION)
1313 return false;
1314 const uint32_t num_symbols = data.GetU32(offset_ptr);
1315 if (num_symbols == 0)
1316 return true;
1317 m_symbols.resize(num_symbols);
1318 SectionList *sections = m_objfile->GetModule()->GetSectionList();
1319 for (uint32_t i=0; i<num_symbols; ++i) {
1320 if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab)) {
1321 // Don't leave partially decoded symbols behind for the caller to
1322 // parse the symbol table on top of.
1323 m_symbols.clear();
1324 return false;
1325 }
1326 }
1327 }
1328
1329 { // Scope for "elapsed" object below so it can measure the time to index.
1330 ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
1331 const uint8_t num_cstr_maps = data.GetU8(offset_ptr);
1332 for (uint8_t i=0; i<num_cstr_maps; ++i) {
1333 uint8_t type = data.GetU8(offset_ptr);
1334 auto cstr_map =
1335 m_name_to_symbol_indices.find((lldb::FunctionNameType)type);
1336 if (cstr_map == m_name_to_symbol_indices.end() ||
1337 !DecodeCStrMap(data, offset_ptr, strtab, cstr_map->second)) {
1338 // Don't leave decoded symbols or name indexes behind for the caller
1339 // to parse the symbol table on top of.
1340 m_symbols.clear();
1341 for (auto &pair : m_name_to_symbol_indices)
1342 pair.second.Clear();
1343 return false;
1344 }
1345 }
1347 }
1348 return true;
1349}
1350
1353 if (m_objfile->IsInMemory()) {
1354 if (m_objfile->GetDisableMemorySymtabCaching())
1355 return false;
1356 else
1358 }
1359 DataFileCache *cache = Module::GetIndexCache(type);
1360 if (!cache)
1361 return false;
1362
1363 std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
1364 cache->GetCachedData(GetCacheKey());
1365 if (!mem_buffer_up)
1366 return false;
1367 DataExtractor data(mem_buffer_up->getBufferStart(),
1368 mem_buffer_up->getBufferSize(),
1369 m_objfile->GetByteOrder(),
1370 m_objfile->GetAddressByteSize());
1371 bool signature_mismatch = false;
1372 lldb::offset_t offset = 0;
1373 const bool result = Decode(data, &offset, signature_mismatch);
1374 if (signature_mismatch)
1375 cache->RemoveCacheFile(GetCacheKey());
1376 if (result)
1378 return result;
1379}
static constexpr uint32_t CURRENT_CACHE_VERSION
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB")
bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr, const StringTableReader &strtab, UniqueCStringMap< uint32_t > &cstr_map)
Definition Symtab.cpp:1191
static int CompareSymbolID(const void *key, const void *p)
Definition Symtab.cpp:207
static void EncodeCStrMap(DataEncoder &encoder, ConstStringTable &strtab, const UniqueCStringMap< uint32_t > &cstr_map)
Definition Symtab.cpp:1179
static void AddSectionsToRangeMap(SectionList *sectlist, RangeVector< addr_t, addr_t > &section_ranges)
Definition Symtab.cpp:879
constexpr llvm::StringLiteral kIdentifierCStrMap("CMAP")
#define LLDB_SCOPED_TIMER()
Definition Timer.h:88
Many cache files require string tables to store data efficiently.
bool Encode(DataEncoder &encoder)
uint32_t Add(ConstString s)
Add a string into the string table.
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
const char * GetCString() const
Get the string value as a C string.
An binary data encoding class.
Definition DataEncoder.h:42
lldb::ByteOrder GetByteOrder() const
uint32_t PutU8(uint32_t offset, uint8_t value)
Encode an unsigned integer at offset offset.
llvm::ArrayRef< uint8_t > GetData() const
Get a access to the bytes that this references.
void AppendU32(uint32_t value)
size_t GetByteSize() const
Get the number of bytes contained in this object.
void AppendU8(uint8_t value)
Append a unsigned integer to the end of the owned data.
void AppendData(llvm::StringRef data)
Append bytes to the end of the owned data.
uint8_t GetAddressByteSize() const
The address size to use when encoding pointers or addresses.
An data extractor class.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
This class enables data to be cached into a directory using the llvm caching code.
std::unique_ptr< llvm::MemoryBuffer > GetCachedData(llvm::StringRef key)
Get cached data from the cache directory for the specified key.
bool SetCachedData(llvm::StringRef key, llvm::ArrayRef< uint8_t > data)
Set cached data for the specified key.
Status RemoveCacheFile(llvm::StringRef key)
Remove the cache file associated with the key.
A class that measures elapsed time in an exception safe way.
Definition Statistics.h:76
A file utility class.
Definition FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
static void ForEach(llvm::function_ref< IterationAction(Language *)> callback)
Definition Language.cpp:154
static Language * FindPluginForManglingScheme(Mangled::ManglingScheme scheme)
Returns the Language that registered scheme with Mangled::RegisterManglingScheme(),...
Definition Language.cpp:133
A class that handles mangled names.
Definition Mangled.h:36
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:169
ConstString GetDemangledName() const
Demangled name get accessor.
Definition Mangled.cpp:238
static constexpr size_t g_num_mangling_schemes
The number of possible mangling schemes, which sizes the tables that are indexed by ManglingScheme.
Definition Mangled.h:51
static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name)
Try to identify the mangling scheme used.
Definition Mangled.cpp:182
@ eLLDBIndexTypeMemoryModuleSymtab
Definition Module.h:1072
static DataFileCache * GetIndexCache(LLDBIndexType type)
Get the global index file cache.
Definition Module.cpp:1697
A plug-in interface definition class for object file parsers.
Definition ObjectFile.h:46
const Entry * FindEntryThatContains(B addr) const
Definition RangeMap.h:338
void Append(const Entry &entry)
Definition RangeMap.h:179
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
size_t GetNumSections(uint32_t depth) const
Definition Section.cpp:544
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:555
A stream class that can stream formatted output to a file.
Definition Stream.h:28
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t Indent(llvm::StringRef s="")
Indent the current line in the stream.
Definition Stream.cpp:157
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition Stream.cpp:63
void IndentLess(unsigned amount=2)
Decrement the current indentation level.
Definition Stream.cpp:204
void IndentMore(unsigned amount=2)
Increment the current indentation level.
Definition Stream.cpp:201
Many cache files require string tables to store data efficiently.
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
llvm::StringRef Get(uint32_t offset) const
Defines a list of symbol context objects.
bool AppendIfUnique(const SymbolContext &sc, bool merge_symbol_into_function)
Defines a symbol context baton that can be handed other debug core functions.
lldb::ModuleSP module_sp
The Module for a given query.
Symbol * symbol
The Symbol for a given query.
uint32_t GetSiblingIndex() const
Definition Symbol.cpp:242
void SetSizeIsSynthesized(bool b)
Definition Symbol.h:219
bool ContainsLinkerAnnotations() const
Definition Symbol.h:267
lldb::addr_t GetFileAddress() const
Definition Symbol.cpp:601
bool ContainsFileAddress(lldb::addr_t file_addr) const
Definition Symbol.cpp:687
Mangled & GetMangled()
Definition Symbol.h:162
bool IsTrampoline() const
Definition Symbol.cpp:248
bool IsSyntheticWithAutoGeneratedName() const
Definition Symbol.cpp:692
bool Compare(ConstString name, lldb::SymbolType type) const
Definition Symbol.cpp:425
static llvm::StringRef GetSyntheticSymbolPrefix()
Definition Symbol.h:296
lldb::SymbolType GetType() const
Definition Symbol.h:197
void SetByteSize(lldb::addr_t size)
Definition Symbol.h:241
void Dump(Stream *s, Target *target, uint32_t index, Mangled::NamePreference name_preference=Mangled::ePreferDemangled) const
Definition Symbol.cpp:300
Symbol * FindSymbolByID(lldb::user_id_t uid) const
Definition Symtab.cpp:217
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:374
Symbol * SymbolAtIndex(size_t idx)
Definition Symtab.cpp:226
Symbol * FindFirstSymbolWithNameAndType(ConstString name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
Definition Symtab.cpp:842
bool m_name_indexes_computed
Definition Symtab.h:289
void ForEachSymbolContainingFileAddress(lldb::addr_t file_addr, std::function< bool(Symbol *)> const &callback)
Definition Symtab.cpp:1030
void AppendSymbolNamesToMap(const IndexCollection &indexes, bool add_demangled, bool add_mangled, NameToIndexMap &name_to_index_map) const
Definition Symtab.cpp:450
Symbol * Resize(size_t count)
Definition Symtab.cpp:55
Symbol * FindSymbolAtFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:999
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1014
FileRangeToIndexMap m_file_addr_to_index
Definition Symtab.h:283
std::recursive_mutex m_mutex
Provide thread safety for this symbol table.
Definition Symtab.h:279
Symbol * FindSymbolWithType(lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t &start_idx)
Definition Symtab.cpp:776
void SortSymbolIndexesByValue(std::vector< uint32_t > &indexes, bool remove_duplicates) const
Definition Symtab.cpp:604
uint32_t AddSymbol(const Symbol &symbol)
Definition Symtab.cpp:62
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:830
uint32_t GetNameIndexes(ConstString symbol_name, std::vector< uint32_t > &indexes)
A helper function that looks up full function names.
Definition Symtab.cpp:629
void Dump(Stream *s, Target *target, SortOrder sort_type, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition Symtab.cpp:85
uint32_t AppendSymbolIndexesWithName(ConstString symbol_name, std::vector< uint32_t > &matches)
Definition Symtab.cpp:661
void RegisterBacklogEntry(const NameToIndexMap::Entry &entry, const char *decl_context, const std::set< const char * > &class_contexts)
Definition Symtab.cpp:427
static void DumpSymbolHeader(Stream *s)
Definition Symtab.cpp:195
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:478
void SaveToCache()
Save the symbol table data out into a cache.
Definition Symtab.cpp:1153
void SetWasLoadedFromCache()
Accessors for the bool that indicates if the debug info index was loaded from, or saved to the module...
Definition Symtab.h:228
std::vector< uint32_t > IndexCollection
Definition Symtab.h:24
size_t GetNumSymbols() const
Definition Symtab.cpp:75
ObjectFile * m_objfile
Definition Symtab.h:281
uint32_t GetIndexForSymbol(const Symbol *symbol) const
Definition Symtab.cpp:539
void SectionFileAddressesChanged()
Definition Symtab.cpp:80
bool m_file_addr_to_index_computed
Definition Symtab.h:288
bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
Definition Symtab.h:304
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:287
void FindAllSymbolsWithNameAndType(ConstString name, lldb::SymbolType symbol_type, std::vector< uint32_t > &symbol_indexes)
Definition Symtab.cpp:796
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:496
void InitAddressIndexes()
Definition Symtab.cpp:907
std::string GetCacheKey()
Get the cache key string for this symbol table.
Definition Symtab.cpp:1142
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:1129
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr, bool &uuid_mismatch)
Decode a serialized version of this object from data.
Definition Symtab.cpp:1290
collection m_symbols
Definition Symtab.h:282
void SetWasSavedToCache()
Definition Symtab.h:231
bool Encode(DataEncoder &encoder) const
Encode this object into a data encoder object.
Definition Symtab.cpp:1243
collection::const_iterator const_iterator
Definition Symtab.h:250
uint32_t AppendSymbolIndexesWithNameAndType(ConstString symbol_name, lldb::SymbolType symbol_type, std::vector< uint32_t > &matches)
Definition Symtab.cpp:698
void SymbolIndicesToSymbolContextList(std::vector< uint32_t > &symbol_indexes, SymbolContextList &sc_list)
Definition Symtab.cpp:1053
bool LoadFromCache()
Load the symbol table from the index cache.
Definition Symtab.cpp:1351
void FindFunctionSymbols(ConstString name, uint32_t name_type_mask, SymbolContextList &sc_list)
Definition Symtab.cpp:1071
UniqueCStringMap< uint32_t > NameToIndexMap
Definition Symtab.h:25
UniqueCStringMap< uint32_t > & GetNameToSymbolIndexMap(lldb::FunctionNameType type)
Definition Symtab.h:299
uint32_t AppendSymbolIndexesMatchingRegExAndType(const RegularExpression &regex, lldb::SymbolType symbol_type, std::vector< uint32_t > &indexes, Mangled::NamePreference name_preference=Mangled::ePreferDemangled)
Definition Symtab.cpp:728
void Append(ConstString unique_cstr, const T &value)
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
lldb::ByteOrder InlHostByteOrder()
Definition Endian.h:25
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:86
SymbolType
Symbol types.
@ eSymbolTypeReExported
@ eSymbolTypeResolver
@ eSymbolTypeAbsolute
uint64_t user_id_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::Section > SectionSP
uint64_t addr_t
Definition lldb-types.h:80
const Symtab * symtab
Definition Symtab.cpp:870
addr_t match_offset
Definition Symtab.cpp:874
Symbol * match_symbol
Definition Symtab.cpp:872
const uint32_t * match_index_ptr
Definition Symtab.cpp:873
const addr_t file_addr
Definition Symtab.cpp:871
const bool sort_by_load_addr
Definition Symtab.cpp:549
const Symbol * symbols
Definition Symtab.cpp:550
A signature for a given file on disk.
bool Decode(const DataExtractor &data, lldb::offset_t *offset_ptr)
Decode a serialized version of this object from data.
bool Encode(DataEncoder &encoder) const
Encode this object into a data encoder object.
The parts of a mangled function name used in the symbol table's indices.
Definition Mangled.h:63
ConstString basename
If empty, the symbol is only indexed by its mangled name.
Definition Mangled.h:65
ConstString decl_context
The enclosing context.
Definition Mangled.h:70
BaseType GetRangeBase() const
Definition RangeMap.h:45
SizeType GetByteSize() const
Definition RangeMap.h:87
void SetRangeBase(BaseType b)
Set the start value for the range, and keep the same size.
Definition RangeMap.h:48
void SetByteSize(SizeType s)
Definition RangeMap.h:89