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::kNumManglingSchemes> 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 size_t size = sect_sp->GetByteSize();
893 if (size > 0) {
894 addr_t base_addr = sect_sp->GetFileAddress();
896 entry.SetRangeBase(base_addr);
897 entry.SetByteSize(size);
898 section_ranges.Append(entry);
899 }
900 }
901 }
902 }
903}
904
906 // Protected function, no need to lock mutex...
907 if (!m_file_addr_to_index_computed && !m_symbols.empty()) {
909
911 const_iterator begin = m_symbols.begin();
912 const_iterator end = m_symbols.end();
913 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) {
914 if (pos->ValueIsAddress()) {
915 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
916 entry.SetByteSize(pos->GetByteSize());
917 entry.data = std::distance(begin, pos);
918 m_file_addr_to_index.Append(entry);
919 }
920 }
921 const size_t num_entries = m_file_addr_to_index.GetSize();
922 if (num_entries > 0) {
924
925 // Create a RangeVector with the start & size of all the sections for
926 // this objfile. We'll need to check this for any FileRangeToIndexMap
927 // entries with an uninitialized size, which could potentially be a large
928 // number so reconstituting the weak pointer is busywork when it is
929 // invariant information.
930 SectionList *sectlist = m_objfile->GetSectionList();
931 RangeVector<addr_t, addr_t> section_ranges;
932 if (sectlist) {
933 AddSectionsToRangeMap(sectlist, section_ranges);
934 section_ranges.Sort();
935 }
936
937 // Iterate through the FileRangeToIndexMap and fill in the size for any
938 // entries that didn't already have a size from the Symbol (e.g. if we
939 // have a plain linker symbol with an address only, instead of debug info
940 // where we get an address and a size and a type, etc.)
941 for (size_t i = 0; i < num_entries; i++) {
943 m_file_addr_to_index.GetMutableEntryAtIndex(i);
944 if (entry->GetByteSize() == 0) {
945 addr_t curr_base_addr = entry->GetRangeBase();
946 const RangeVector<addr_t, addr_t>::Entry *containing_section =
947 section_ranges.FindEntryThatContains(curr_base_addr);
948
949 // Use the end of the section as the default max size of the symbol
950 addr_t sym_size = 0;
951 if (containing_section) {
952 sym_size =
953 containing_section->GetByteSize() -
954 (entry->GetRangeBase() - containing_section->GetRangeBase());
955 }
956
957 for (size_t j = i; j < num_entries; j++) {
958 FileRangeToIndexMap::Entry *next_entry =
959 m_file_addr_to_index.GetMutableEntryAtIndex(j);
960 addr_t next_base_addr = next_entry->GetRangeBase();
961 if (next_base_addr > curr_base_addr) {
962 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
963
964 // Take the difference between this symbol and the next one as
965 // its size, if it is less than the size of the section.
966 if (sym_size == 0 || size_to_next_symbol < sym_size) {
967 sym_size = size_to_next_symbol;
968 }
969 break;
970 }
971 }
972
973 if (sym_size > 0) {
974 entry->SetByteSize(sym_size);
975 Symbol &symbol = m_symbols[entry->data];
976 symbol.SetByteSize(sym_size);
977 symbol.SetSizeIsSynthesized(true);
978 }
979 }
980 }
981
982 // Sort again in case the range size changes the ordering
984 }
985 }
986}
987
989 std::lock_guard<std::recursive_mutex> guard(m_mutex);
990 // Calculate the size of symbols inside InitAddressIndexes.
992 // Shrink to fit the symbols so we don't waste memory
993 m_symbols.shrink_to_fit();
994 SaveToCache();
995}
996
998 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1001
1002 const FileRangeToIndexMap::Entry *entry =
1003 m_file_addr_to_index.FindEntryStartsAt(file_addr);
1004 if (entry) {
1005 Symbol *symbol = SymbolAtIndex(entry->data);
1006 if (symbol->GetFileAddress() == file_addr)
1007 return symbol;
1008 }
1009 return nullptr;
1010}
1011
1013 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1014
1017
1018 const FileRangeToIndexMap::Entry *entry =
1019 m_file_addr_to_index.FindEntryThatContains(file_addr);
1020 if (entry) {
1021 Symbol *symbol = SymbolAtIndex(entry->data);
1022 if (symbol->ContainsFileAddress(file_addr))
1023 return symbol;
1024 }
1025 return nullptr;
1026}
1027
1029 addr_t file_addr, std::function<bool(Symbol *)> const &callback) {
1030 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1031
1034
1035 std::vector<uint32_t> all_addr_indexes;
1036
1037 // Get all symbols with file_addr
1038 const size_t addr_match_count =
1039 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr,
1040 all_addr_indexes);
1041
1042 for (size_t i = 0; i < addr_match_count; ++i) {
1043 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]);
1044 if (symbol->ContainsFileAddress(file_addr)) {
1045 if (!callback(symbol))
1046 break;
1047 }
1048 }
1049}
1050
1052 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) {
1053 // No need to protect this call using m_mutex all other method calls are
1054 // already thread safe.
1055
1056 const bool merge_symbol_into_function = true;
1057 size_t num_indices = symbol_indexes.size();
1058 if (num_indices > 0) {
1059 SymbolContext sc;
1060 sc.module_sp = m_objfile->GetModule();
1061 for (size_t i = 0; i < num_indices; i++) {
1062 sc.symbol = SymbolAtIndex(symbol_indexes[i]);
1063 if (sc.symbol)
1064 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1065 }
1066 }
1067}
1068
1069void Symtab::FindFunctionSymbols(ConstString name, uint32_t name_type_mask,
1070 SymbolContextList &sc_list) {
1071 std::vector<uint32_t> symbol_indexes;
1072
1073 // eFunctionNameTypeAuto should be pre-resolved by a call to
1074 // Module::LookupInfo::LookupInfo()
1075 assert((name_type_mask & eFunctionNameTypeAuto) == 0);
1076
1077 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) {
1078 std::vector<uint32_t> temp_symbol_indexes;
1079 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes);
1080
1081 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1082 if (temp_symbol_indexes_size > 0) {
1083 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1084 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) {
1085 SymbolContext sym_ctx;
1086 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]);
1087 if (sym_ctx.symbol) {
1088 switch (sym_ctx.symbol->GetType()) {
1089 case eSymbolTypeCode:
1093 symbol_indexes.push_back(temp_symbol_indexes[i]);
1094 break;
1095 default:
1096 break;
1097 }
1098 }
1099 }
1100 }
1101 }
1102
1103 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1105
1106 for (lldb::FunctionNameType type :
1107 {lldb::eFunctionNameTypeBase, lldb::eFunctionNameTypeMethod,
1108 lldb::eFunctionNameTypeSelector}) {
1109 if (name_type_mask & type) {
1110 auto map = GetNameToSymbolIndexMap(type);
1111
1113 for (match = map.FindFirstValueForName(name); match != nullptr;
1114 match = map.FindNextValueForName(match)) {
1115 symbol_indexes.push_back(match->value);
1116 }
1117 }
1118 }
1119
1120 if (!symbol_indexes.empty()) {
1121 llvm::sort(symbol_indexes);
1122 symbol_indexes.erase(llvm::unique(symbol_indexes), symbol_indexes.end());
1123 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list);
1124 }
1125}
1126
1127const Symbol *Symtab::GetParent(Symbol *child_symbol) const {
1128 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1129 if (child_idx != UINT32_MAX && child_idx > 0) {
1130 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) {
1131 const Symbol *symbol = SymbolAtIndex(idx);
1132 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1133 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)
1134 return symbol;
1135 }
1136 }
1137 return nullptr;
1138}
1139
1140std::string Symtab::GetCacheKey() {
1141 std::string key;
1142 llvm::raw_string_ostream strm(key);
1143 // Symbol table can come from different object files for the same module. A
1144 // module can have one object file as the main executable and might have
1145 // another object file in a separate symbol file.
1146 strm << m_objfile->GetModule()->GetCacheKey() << "-symtab-"
1147 << llvm::format_hex(m_objfile->GetCacheHash(), 10);
1148 return key;
1149}
1150
1152 DataFileCache *cache = Module::GetIndexCache(m_objfile->IsInMemory());
1153 if (!cache)
1154 return; // Caching is not enabled.
1155
1156 // Init the name indexes so we can cache them as well.
1158 const auto byte_order = endian::InlHostByteOrder();
1159 DataEncoder file(byte_order, /*addr_size=*/8);
1160 // Encode will return false if the symbol table's object file doesn't have
1161 // anything to make a signature from.
1162 if (Encode(file))
1163 if (cache->SetCachedData(GetCacheKey(), file.GetData()))
1165}
1166
1167constexpr llvm::StringLiteral kIdentifierCStrMap("CMAP");
1168
1169static void EncodeCStrMap(DataEncoder &encoder, ConstStringTable &strtab,
1170 const UniqueCStringMap<uint32_t> &cstr_map) {
1172 encoder.AppendU32(cstr_map.GetSize());
1173 for (const auto &entry: cstr_map) {
1174 // Make sure there are no empty strings.
1175 assert((bool)entry.cstring);
1176 encoder.AppendU32(strtab.Add(entry.cstring));
1177 encoder.AppendU32(entry.value);
1178 }
1179}
1180
1181bool DecodeCStrMap(const DataExtractor &data, lldb::offset_t *offset_ptr,
1182 const StringTableReader &strtab,
1183 UniqueCStringMap<uint32_t> &cstr_map) {
1184 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1185 if (identifier != kIdentifierCStrMap)
1186 return false;
1187 const uint32_t count = data.GetU32(offset_ptr);
1188 cstr_map.Reserve(count);
1189 for (uint32_t i=0; i<count; ++i)
1190 {
1191 llvm::StringRef str(strtab.Get(data.GetU32(offset_ptr)));
1192 uint32_t value = data.GetU32(offset_ptr);
1193 // No empty strings in the name indexes in Symtab
1194 if (str.empty())
1195 return false;
1196 cstr_map.Append(ConstString(str), value);
1197 }
1198 // We must sort the UniqueCStringMap after decoding it since it is a vector
1199 // of UniqueCStringMap::Entry objects which contain a ConstString and type T.
1200 // ConstString objects are sorted by "const char *" and then type T and
1201 // the "const char *" are point values that will depend on the order in which
1202 // ConstString objects are created and in which of the 256 string pools they
1203 // are created in. So after we decode all of the entries, we must sort the
1204 // name map to ensure name lookups succeed. If we encode and decode within
1205 // the same process we wouldn't need to sort, so unit testing didn't catch
1206 // this issue when first checked in.
1207 cstr_map.Sort();
1208 return true;
1209}
1210
1211constexpr llvm::StringLiteral kIdentifierSymbolTable("SYMB");
1212constexpr uint32_t CURRENT_CACHE_VERSION = 3;
1213
1214// If the size of the Symbol object changes, the serialized
1215// format likely also needs to change so the
1216// CURRENT_CACHE_VERSION number will need to be incremented.
1217#if __SIZEOF_POINTER__ == 8
1218static_assert(
1219 sizeof(lldb_private::Symbol) == 72,
1220 "Symbol size has changed, DataFileCache version likely needs updating");
1221#endif
1222
1223/// The encoding format for the symbol table is as follows:
1224///
1225/// Signature signature;
1226/// ConstStringTable strtab;
1227/// Identifier four character code: 'SYMB'
1228/// uint32_t version;
1229/// uint32_t num_symbols;
1230/// Symbol symbols[num_symbols];
1231/// uint8_t num_cstr_maps;
1232/// UniqueCStringMap<uint32_t> cstr_maps[num_cstr_maps]
1233bool Symtab::Encode(DataEncoder &encoder) const {
1234 // Name indexes must be computed before calling this function.
1236
1237 // Encode the object file's signature
1238 CacheSignature signature(m_objfile);
1239 if (!signature.Encode(encoder))
1240 return false;
1241 ConstStringTable strtab;
1242
1243 // Encode the symbol table into a separate encoder first. This allows us
1244 // gather all of the strings we will need in "strtab" as we will need to
1245 // write the string table out before the symbol table.
1246 DataEncoder symtab_encoder(encoder.GetByteOrder(),
1247 encoder.GetAddressByteSize());
1248 symtab_encoder.AppendData(kIdentifierSymbolTable);
1249 // Encode the symtab data version.
1250 symtab_encoder.AppendU32(CURRENT_CACHE_VERSION);
1251 // Encode the number of symbols.
1252 symtab_encoder.AppendU32(m_symbols.size());
1253 // Encode the symbol data for all symbols.
1254 for (const auto &symbol: m_symbols)
1255 symbol.Encode(symtab_encoder, strtab);
1256
1257 // Emit a byte for how many C string maps we emit. We will fix this up after
1258 // we emit the C string maps since we skip emitting C string maps if they are
1259 // empty.
1260 size_t num_cmaps_offset = symtab_encoder.GetByteSize();
1261 uint8_t num_cmaps = 0;
1262 symtab_encoder.AppendU8(0);
1263 for (const auto &pair: m_name_to_symbol_indices) {
1264 if (pair.second.IsEmpty())
1265 continue;
1266 ++num_cmaps;
1267 symtab_encoder.AppendU8(pair.first);
1268 EncodeCStrMap(symtab_encoder, strtab, pair.second);
1269 }
1270 if (num_cmaps > 0)
1271 symtab_encoder.PutU8(num_cmaps_offset, num_cmaps);
1272
1273 // Now that all strings have been gathered, we will emit the string table.
1274 strtab.Encode(encoder);
1275 // Followed by the symbol table data.
1276 encoder.AppendData(symtab_encoder.GetData());
1277 return true;
1278}
1279
1280bool Symtab::Decode(const DataExtractor &data, lldb::offset_t *offset_ptr,
1281 bool &signature_mismatch) {
1282 signature_mismatch = false;
1283 CacheSignature signature;
1284 StringTableReader strtab;
1285 { // Scope for "elapsed" object below so it can measure the time parse.
1286 ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabParseTime());
1287 if (!signature.Decode(data, offset_ptr))
1288 return false;
1289 if (CacheSignature(m_objfile) != signature) {
1290 signature_mismatch = true;
1291 return false;
1292 }
1293 // We now decode the string table for all strings in the data cache file.
1294 if (!strtab.Decode(data, offset_ptr))
1295 return false;
1296
1297 // And now we can decode the symbol table with string table we just decoded.
1298 llvm::StringRef identifier((const char *)data.GetData(offset_ptr, 4), 4);
1299 if (identifier != kIdentifierSymbolTable)
1300 return false;
1301 const uint32_t version = data.GetU32(offset_ptr);
1302 if (version != CURRENT_CACHE_VERSION)
1303 return false;
1304 const uint32_t num_symbols = data.GetU32(offset_ptr);
1305 if (num_symbols == 0)
1306 return true;
1307 m_symbols.resize(num_symbols);
1308 SectionList *sections = m_objfile->GetModule()->GetSectionList();
1309 for (uint32_t i=0; i<num_symbols; ++i) {
1310 if (!m_symbols[i].Decode(data, offset_ptr, sections, strtab))
1311 return false;
1312 }
1313 }
1314
1315 { // Scope for "elapsed" object below so it can measure the time to index.
1316 ElapsedTime elapsed(m_objfile->GetModule()->GetSymtabIndexTime());
1317 const uint8_t num_cstr_maps = data.GetU8(offset_ptr);
1318 for (uint8_t i=0; i<num_cstr_maps; ++i) {
1319 uint8_t type = data.GetU8(offset_ptr);
1320 UniqueCStringMap<uint32_t> &cstr_map =
1321 GetNameToSymbolIndexMap((lldb::FunctionNameType)type);
1322 if (!DecodeCStrMap(data, offset_ptr, strtab, cstr_map))
1323 return false;
1324 }
1326 }
1327 return true;
1328}
1329
1331 DataFileCache *cache = Module::GetIndexCache(m_objfile->IsInMemory());
1332 if (!cache)
1333 return false;
1334
1335 std::unique_ptr<llvm::MemoryBuffer> mem_buffer_up =
1336 cache->GetCachedData(GetCacheKey());
1337 if (!mem_buffer_up)
1338 return false;
1339 DataExtractor data(mem_buffer_up->getBufferStart(),
1340 mem_buffer_up->getBufferSize(),
1341 m_objfile->GetByteOrder(),
1342 m_objfile->GetAddressByteSize());
1343 bool signature_mismatch = false;
1344 lldb::offset_t offset = 0;
1345 const bool result = Decode(data, &offset, signature_mismatch);
1346 if (signature_mismatch)
1347 cache->RemoveCacheFile(GetCacheKey());
1348 if (result)
1350 return result;
1351}
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:1181
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:1169
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: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 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:156
static Language * FindPluginForManglingScheme(Mangled::ManglingScheme scheme)
Returns the Language that handles names mangled with scheme, or a nullptr if there is none.
Definition Language.cpp:130
A class that handles mangled names.
Definition Mangled.h:34
ConstString GetMangledName() const
Mangled name get accessor.
Definition Mangled.h:170
ConstString GetDemangledName() const
Demangled name get accessor.
Definition Mangled.cpp:171
static constexpr size_t kNumManglingSchemes
The number of mangling schemes.
Definition Mangled.h:52
static Mangled::ManglingScheme GetManglingScheme(llvm::StringRef name)
Try to identify the mangling scheme used.
Definition Mangled.cpp:43
static DataFileCache * GetIndexCache(bool memory_module)
Get the global index file cache.
Definition Module.cpp:1669
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:241
void SetSizeIsSynthesized(bool b)
Definition Symbol.h:219
bool ContainsLinkerAnnotations() const
Definition Symbol.h:267
lldb::addr_t GetFileAddress() const
Definition Symbol.cpp:598
bool ContainsFileAddress(lldb::addr_t file_addr) const
Definition Symbol.cpp:684
Mangled & GetMangled()
Definition Symbol.h:162
bool IsTrampoline() const
Definition Symbol.cpp:247
bool IsSyntheticWithAutoGeneratedName() const
Definition Symbol.cpp:689
bool Compare(ConstString name, lldb::SymbolType type) const
Definition Symbol.cpp:424
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:299
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:284
void ForEachSymbolContainingFileAddress(lldb::addr_t file_addr, std::function< bool(Symbol *)> const &callback)
Definition Symtab.cpp:1028
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:997
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1012
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: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:1151
void SetWasLoadedFromCache()
Definition Symtab.h:231
std::vector< uint32_t > IndexCollection
Definition Symtab.h:24
size_t GetNumSymbols() const
Definition Symtab.cpp:75
ObjectFile * m_objfile
Definition Symtab.h:276
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:283
bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
Definition Symtab.h:295
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:282
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:905
std::string GetCacheKey()
Get the cache key string for this symbol table.
Definition Symtab.cpp:1140
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:1127
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:1280
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:1233
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:698
void SymbolIndicesToSymbolContextList(std::vector< uint32_t > &symbol_indexes, SymbolContextList &sc_list)
Definition Symtab.cpp:1051
bool LoadFromCache()
Load the symbol table from the index cache.
Definition Symtab.cpp:1330
void FindFunctionSymbols(ConstString name, uint32_t name_type_mask, SymbolContextList &sc_list)
Definition Symtab.cpp:1069
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: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:64
ConstString basename
If empty, the symbol is only indexed by its mangled name.
Definition Mangled.h:66
ConstString decl_context
The enclosing context.
Definition Mangled.h:71
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