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