1 //===-- Symtab.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/RegularExpression.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/Timer.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/Symbol.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/Symtab.h"
22 #include "Plugins/Language/ObjC/ObjCLanguage.h"
23 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
26 using namespace lldb_private;
28 Symtab::Symtab(ObjectFile *objfile)
31 m_file_addr_to_index(),
34 m_file_addr_to_index_computed(false),
35 m_name_indexes_computed(false)
44 Symtab::Reserve(size_t count)
46 // Clients should grab the mutex from this symbol table and lock it manually
47 // when calling this function to avoid performance issues.
48 m_symbols.reserve (count);
52 Symtab::Resize(size_t count)
54 // Clients should grab the mutex from this symbol table and lock it manually
55 // when calling this function to avoid performance issues.
56 m_symbols.resize (count);
57 return m_symbols.empty() ? nullptr : &m_symbols[0];
61 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 m_name_to_index.Clear();
67 m_file_addr_to_index.Clear();
68 m_symbols.push_back(symbol);
69 m_file_addr_to_index_computed = false;
70 m_name_indexes_computed = false;
75 Symtab::GetNumSymbols() const
77 std::lock_guard<std::recursive_mutex> guard(m_mutex);
78 return m_symbols.size();
82 Symtab::SectionFileAddressesChanged ()
84 m_name_to_index.Clear();
85 m_file_addr_to_index_computed = false;
89 Symtab::Dump (Stream *s, Target *target, SortOrder sort_order)
91 std::lock_guard<std::recursive_mutex> guard(m_mutex);
93 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
95 const FileSpec &file_spec = m_objfile->GetFileSpec();
96 const char * object_name = nullptr;
97 if (m_objfile->GetModule())
98 object_name = m_objfile->GetModule()->GetObjectName().GetCString();
101 s->Printf("Symtab, file = %s%s%s%s, num_symbols = %" PRIu64,
102 file_spec.GetPath().c_str(),
103 object_name ? "(" : "",
104 object_name ? object_name : "",
105 object_name ? ")" : "",
106 (uint64_t)m_symbols.size());
108 s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size());
110 if (!m_symbols.empty())
116 s->PutCString (":\n");
117 DumpSymbolHeader (s);
118 const_iterator begin = m_symbols.begin();
119 const_iterator end = m_symbols.end();
120 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
123 pos->Dump(s, target, std::distance(begin, pos));
128 case eSortOrderByName:
130 // Although we maintain a lookup by exact name map, the table
131 // isn't sorted by name. So we must make the ordered symbol list
133 s->PutCString (" (sorted by name):\n");
134 DumpSymbolHeader (s);
135 typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol;
136 CStringToSymbol name_map;
137 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos)
139 const char *name = pos->GetName().AsCString();
141 name_map.insert (std::make_pair(name, &(*pos)));
144 for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos)
147 pos->second->Dump (s, target, pos->second - &m_symbols[0]);
152 case eSortOrderByAddress:
153 s->PutCString (" (sorted by address):\n");
154 DumpSymbolHeader (s);
155 if (!m_file_addr_to_index_computed)
156 InitAddressIndexes();
157 const size_t num_entries = m_file_addr_to_index.GetSize();
158 for (size_t i=0; i<num_entries; ++i)
161 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
162 m_symbols[symbol_idx].Dump(s, target, symbol_idx);
170 Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const
172 std::lock_guard<std::recursive_mutex> guard(m_mutex);
174 const size_t num_symbols = GetNumSymbols();
175 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
177 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n", (uint64_t)indexes.size(), (uint64_t)m_symbols.size());
180 if (!indexes.empty())
182 std::vector<uint32_t>::const_iterator pos;
183 std::vector<uint32_t>::const_iterator end = indexes.end();
184 DumpSymbolHeader (s);
185 for (pos = indexes.begin(); pos != end; ++pos)
188 if (idx < num_symbols)
191 m_symbols[idx].Dump(s, target, idx);
199 Symtab::DumpSymbolHeader (Stream *s)
201 s->Indent(" Debug symbol\n");
202 s->Indent(" |Synthetic symbol\n");
203 s->Indent(" ||Externally Visible\n");
205 s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n");
206 s->Indent("------- ------ --- --------------- ------------------ ------------------ ------------------ ---------- ----------------------------------\n");
211 CompareSymbolID (const void *key, const void *p)
213 const user_id_t match_uid = *(const user_id_t*) key;
214 const user_id_t symbol_uid = ((const Symbol *)p)->GetID();
215 if (match_uid < symbol_uid)
217 if (match_uid > symbol_uid)
223 Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const
225 std::lock_guard<std::recursive_mutex> guard(m_mutex);
227 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid,
230 sizeof(m_symbols[0]),
237 Symtab::SymbolAtIndex(size_t idx)
239 // Clients should grab the mutex from this symbol table and lock it manually
240 // when calling this function to avoid performance issues.
241 if (idx < m_symbols.size())
242 return &m_symbols[idx];
248 Symtab::SymbolAtIndex(size_t idx) const
250 // Clients should grab the mutex from this symbol table and lock it manually
251 // when calling this function to avoid performance issues.
252 if (idx < m_symbols.size())
253 return &m_symbols[idx];
257 //----------------------------------------------------------------------
259 //----------------------------------------------------------------------
261 Symtab::InitNameIndexes()
263 // Protected function, no need to lock mutex...
264 if (!m_name_indexes_computed)
266 m_name_indexes_computed = true;
267 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
268 // Create the name index vector to be able to quickly search by name
269 const size_t num_symbols = m_symbols.size();
271 m_name_to_index.Reserve (num_symbols);
273 // TODO: benchmark this to see if we save any memory. Otherwise we
274 // will always keep the memory reserved in the vector unless we pull
275 // some STL swap magic and then recopy...
276 uint32_t actual_count = 0;
277 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
281 const Mangled &mangled = pos->GetMangled();
282 if (mangled.GetMangledName())
285 if (mangled.GetDemangledName())
289 m_name_to_index.Reserve (actual_count);
292 NameToIndexMap::Entry entry;
294 // The "const char *" in "class_contexts" must come from a ConstString::GetCString()
295 std::set<const char *> class_contexts;
296 UniqueCStringMap<uint32_t> mangled_name_to_index;
297 std::vector<const char *> symbol_contexts(num_symbols, nullptr);
299 for (entry.value = 0; entry.value<num_symbols; ++entry.value)
301 const Symbol *symbol = &m_symbols[entry.value];
303 // Don't let trampolines get into the lookup by name map
304 // If we ever need the trampoline symbols to be searchable by name
305 // we can remove this and then possibly add a new bool to any of the
306 // Symtab functions that lookup symbols by name to indicate if they
308 if (symbol->IsTrampoline())
311 const Mangled &mangled = symbol->GetMangled();
312 entry.cstring = mangled.GetMangledName().GetCString();
313 if (entry.cstring && entry.cstring[0])
315 m_name_to_index.Append (entry);
317 if (symbol->ContainsLinkerAnnotations()) {
318 // If the symbol has linker annotations, also add the version without the
320 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(entry.cstring)).GetCString();
321 m_name_to_index.Append (entry);
324 const SymbolType symbol_type = symbol->GetType();
325 if (symbol_type == eSymbolTypeCode || symbol_type == eSymbolTypeResolver)
327 if (entry.cstring[0] == '_' && entry.cstring[1] == 'Z' &&
328 (entry.cstring[2] != 'T' && // avoid virtual table, VTT structure, typeinfo structure, and typeinfo name
329 entry.cstring[2] != 'G' && // avoid guard variables
330 entry.cstring[2] != 'Z')) // named local entities (if we eventually handle eSymbolTypeData, we will want this back)
332 CPlusPlusLanguage::MethodName cxx_method (mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus));
333 entry.cstring = ConstString(cxx_method.GetBasename()).GetCString();
334 if (entry.cstring && entry.cstring[0])
336 // ConstString objects permanently store the string in the pool so calling
337 // GetCString() on the value gets us a const char * that will never go away
338 const char *const_context = ConstString(cxx_method.GetContext()).GetCString();
340 if (entry.cstring[0] == '~' || !cxx_method.GetQualifiers().empty())
342 // The first character of the demangled basename is '~' which
343 // means we have a class destructor. We can use this information
344 // to help us know what is a class and what isn't.
345 if (class_contexts.find(const_context) == class_contexts.end())
346 class_contexts.insert(const_context);
347 m_method_to_index.Append (entry);
351 if (const_context && const_context[0])
353 if (class_contexts.find(const_context) != class_contexts.end())
355 // The current decl context is in our "class_contexts" which means
356 // this is a method on a class
357 m_method_to_index.Append (entry);
361 // We don't know if this is a function basename or a method,
362 // so put it into a temporary collection so once we are done
363 // we can look in class_contexts to see if each entry is a class
364 // or just a function and will put any remaining items into
365 // m_method_to_index or m_basename_to_index as needed
366 mangled_name_to_index.Append (entry);
367 symbol_contexts[entry.value] = const_context;
372 // No context for this function so this has to be a basename
373 m_basename_to_index.Append(entry);
381 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage()).GetCString();
382 if (entry.cstring && entry.cstring[0]) {
383 m_name_to_index.Append (entry);
385 if (symbol->ContainsLinkerAnnotations()) {
386 // If the symbol has linker annotations, also add the version without the
388 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations(entry.cstring)).GetCString();
389 m_name_to_index.Append (entry);
393 // If the demangled name turns out to be an ObjC name, and
394 // is a category name, add the version without categories to the index too.
395 ObjCLanguage::MethodName objc_method (entry.cstring, true);
396 if (objc_method.IsValid(true))
398 entry.cstring = objc_method.GetSelector().GetCString();
399 m_selector_to_index.Append (entry);
401 ConstString objc_method_no_category (objc_method.GetFullNameWithoutCategory(true));
402 if (objc_method_no_category)
404 entry.cstring = objc_method_no_category.GetCString();
405 m_name_to_index.Append (entry);
412 if (!mangled_name_to_index.IsEmpty())
414 count = mangled_name_to_index.GetSize();
415 for (size_t i=0; i<count; ++i)
417 if (mangled_name_to_index.GetValueAtIndex(i, entry.value))
419 entry.cstring = mangled_name_to_index.GetCStringAtIndex(i);
420 if (symbol_contexts[entry.value] && class_contexts.find(symbol_contexts[entry.value]) != class_contexts.end())
422 m_method_to_index.Append (entry);
426 // If we got here, we have something that had a context (was inside a namespace or class)
427 // yet we don't know if the entry
428 m_method_to_index.Append (entry);
429 m_basename_to_index.Append (entry);
434 m_name_to_index.Sort();
435 m_name_to_index.SizeToFit();
436 m_selector_to_index.Sort();
437 m_selector_to_index.SizeToFit();
438 m_basename_to_index.Sort();
439 m_basename_to_index.SizeToFit();
440 m_method_to_index.Sort();
441 m_method_to_index.SizeToFit();
443 // static StreamFile a ("/tmp/a.txt");
445 // count = m_basename_to_index.GetSize();
448 // for (size_t i=0; i<count; ++i)
450 // if (m_basename_to_index.GetValueAtIndex(i, entry.value))
451 // a.Printf ("%s BASENAME\n", m_symbols[entry.value].GetMangled().GetName().GetCString());
454 // count = m_method_to_index.GetSize();
457 // for (size_t i=0; i<count; ++i)
459 // if (m_method_to_index.GetValueAtIndex(i, entry.value))
460 // a.Printf ("%s METHOD\n", m_symbols[entry.value].GetMangled().GetName().GetCString());
467 Symtab::AppendSymbolNamesToMap (const IndexCollection &indexes,
470 NameToIndexMap &name_to_index_map) const
472 if (add_demangled || add_mangled)
474 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
475 std::lock_guard<std::recursive_mutex> guard(m_mutex);
477 // Create the name index vector to be able to quickly search by name
478 NameToIndexMap::Entry entry;
479 const size_t num_indexes = indexes.size();
480 for (size_t i=0; i<num_indexes; ++i)
482 entry.value = indexes[i];
483 assert (i < m_symbols.size());
484 const Symbol *symbol = &m_symbols[entry.value];
486 const Mangled &mangled = symbol->GetMangled();
489 entry.cstring = mangled.GetDemangledName(symbol->GetLanguage()).GetCString();
490 if (entry.cstring && entry.cstring[0])
491 name_to_index_map.Append (entry);
496 entry.cstring = mangled.GetMangledName().GetCString();
497 if (entry.cstring && entry.cstring[0])
498 name_to_index_map.Append (entry);
505 Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
507 std::lock_guard<std::recursive_mutex> guard(m_mutex);
509 uint32_t prev_size = indexes.size();
511 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
513 for (uint32_t i = start_idx; i < count; ++i)
515 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
516 indexes.push_back(i);
519 return indexes.size() - prev_size;
523 Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
525 std::lock_guard<std::recursive_mutex> guard(m_mutex);
527 uint32_t prev_size = indexes.size();
529 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
531 for (uint32_t i = start_idx; i < count; ++i)
533 if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value)
534 indexes.push_back(i);
537 return indexes.size() - prev_size;
541 Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
543 std::lock_guard<std::recursive_mutex> guard(m_mutex);
545 uint32_t prev_size = indexes.size();
547 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
549 for (uint32_t i = start_idx; i < count; ++i)
551 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
553 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
554 indexes.push_back(i);
558 return indexes.size() - prev_size;
563 Symtab::GetIndexForSymbol (const Symbol *symbol) const
565 if (!m_symbols.empty())
567 const Symbol *first_symbol = &m_symbols[0];
568 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
569 return symbol - first_symbol;
574 struct SymbolSortInfo
576 const bool sort_by_load_addr;
577 const Symbol *symbols;
581 struct SymbolIndexComparator {
582 const std::vector<Symbol>& symbols;
583 std::vector<lldb::addr_t> &addr_cache;
585 // Getting from the symbol to the Address to the File Address involves some work.
586 // Since there are potentially many symbols here, and we're using this for sorting so
587 // we're going to be computing the address many times, cache that in addr_cache.
588 // The array passed in has to be the same size as the symbols array passed into the
589 // member variable symbols, and should be initialized with LLDB_INVALID_ADDRESS.
590 // NOTE: You have to make addr_cache externally and pass it in because std::stable_sort
591 // makes copies of the comparator it is initially passed in, and you end up spending
592 // huge amounts of time copying this array...
594 SymbolIndexComparator(const std::vector<Symbol>& s, std::vector<lldb::addr_t> &a) : symbols(s), addr_cache(a) {
595 assert (symbols.size() == addr_cache.size());
597 bool operator()(uint32_t index_a, uint32_t index_b) {
598 addr_t value_a = addr_cache[index_a];
599 if (value_a == LLDB_INVALID_ADDRESS)
601 value_a = symbols[index_a].GetAddressRef().GetFileAddress();
602 addr_cache[index_a] = value_a;
605 addr_t value_b = addr_cache[index_b];
606 if (value_b == LLDB_INVALID_ADDRESS)
608 value_b = symbols[index_b].GetAddressRef().GetFileAddress();
609 addr_cache[index_b] = value_b;
613 if (value_a == value_b) {
614 // The if the values are equal, use the original symbol user ID
615 lldb::user_id_t uid_a = symbols[index_a].GetID();
616 lldb::user_id_t uid_b = symbols[index_b].GetID();
622 } else if (value_a < value_b)
631 Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const
633 std::lock_guard<std::recursive_mutex> guard(m_mutex);
635 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__);
636 // No need to sort if we have zero or one items...
637 if (indexes.size() <= 1)
640 // Sort the indexes in place using std::stable_sort.
641 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance,
642 // not correctness. The indexes vector tends to be "close" to sorted, which the
643 // stable sort handles better.
645 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
647 SymbolIndexComparator comparator(m_symbols, addr_cache);
648 std::stable_sort(indexes.begin(), indexes.end(), comparator);
650 // Remove any duplicates if requested
651 if (remove_duplicates)
652 std::unique(indexes.begin(), indexes.end());
656 Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes)
658 std::lock_guard<std::recursive_mutex> guard(m_mutex);
660 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
663 const char *symbol_cstr = symbol_name.GetCString();
664 if (!m_name_indexes_computed)
667 return m_name_to_index.GetValues (symbol_cstr, indexes);
673 Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
675 std::lock_guard<std::recursive_mutex> guard(m_mutex);
677 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
680 const size_t old_size = indexes.size();
681 if (!m_name_indexes_computed)
684 const char *symbol_cstr = symbol_name.GetCString();
686 std::vector<uint32_t> all_name_indexes;
687 const size_t name_match_count = m_name_to_index.GetValues (symbol_cstr, all_name_indexes);
688 for (size_t i=0; i<name_match_count; ++i)
690 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, symbol_visibility))
691 indexes.push_back (all_name_indexes[i]);
693 return indexes.size() - old_size;
699 Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes)
701 std::lock_guard<std::recursive_mutex> guard(m_mutex);
703 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0)
705 std::vector<uint32_t>::iterator pos = indexes.begin();
706 while (pos != indexes.end())
708 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
711 pos = indexes.erase(pos);
714 return indexes.size();
718 Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
720 std::lock_guard<std::recursive_mutex> guard(m_mutex);
722 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0)
724 std::vector<uint32_t>::iterator pos = indexes.begin();
725 while (pos != indexes.end())
727 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
730 pos = indexes.erase(pos);
733 return indexes.size();
738 Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, std::vector<uint32_t>& indexes)
740 std::lock_guard<std::recursive_mutex> guard(m_mutex);
742 uint32_t prev_size = indexes.size();
743 uint32_t sym_end = m_symbols.size();
745 for (uint32_t i = 0; i < sym_end; i++)
747 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
749 const char *name = m_symbols[i].GetName().AsCString();
752 if (regexp.Execute (name))
753 indexes.push_back(i);
757 return indexes.size() - prev_size;
762 Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
764 std::lock_guard<std::recursive_mutex> guard(m_mutex);
766 uint32_t prev_size = indexes.size();
767 uint32_t sym_end = m_symbols.size();
769 for (uint32_t i = 0; i < sym_end; i++)
771 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
773 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false)
776 const char *name = m_symbols[i].GetName().AsCString();
779 if (regexp.Execute (name))
780 indexes.push_back(i);
784 return indexes.size() - prev_size;
789 Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx)
791 std::lock_guard<std::recursive_mutex> guard(m_mutex);
793 const size_t count = m_symbols.size();
794 for (size_t idx = start_idx; idx < count; ++idx)
796 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type)
798 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility))
801 return &m_symbols[idx];
809 Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes)
811 std::lock_guard<std::recursive_mutex> guard(m_mutex);
813 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
814 // Initialize all of the lookup by name indexes before converting NAME
815 // to a uniqued string NAME_STR below.
816 if (!m_name_indexes_computed)
821 // The string table did have a string that matched, but we need
822 // to check the symbols and match the symbol_type if any was given.
823 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes);
825 return symbol_indexes.size();
829 Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
831 std::lock_guard<std::recursive_mutex> guard(m_mutex);
833 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
834 // Initialize all of the lookup by name indexes before converting NAME
835 // to a uniqued string NAME_STR below.
836 if (!m_name_indexes_computed)
841 // The string table did have a string that matched, but we need
842 // to check the symbols and match the symbol_type if any was given.
843 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
845 return symbol_indexes.size();
849 Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression ®ex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
851 std::lock_guard<std::recursive_mutex> guard(m_mutex);
853 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
854 return symbol_indexes.size();
858 Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
860 std::lock_guard<std::recursive_mutex> guard(m_mutex);
862 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
863 if (!m_name_indexes_computed)
868 std::vector<uint32_t> matching_indexes;
869 // The string table did have a string that matched, but we need
870 // to check the symbols and match the symbol_type if any was given.
871 if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes))
873 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
874 for (pos = matching_indexes.begin(); pos != end; ++pos)
876 Symbol *symbol = SymbolAtIndex(*pos);
878 if (symbol->Compare(name, symbol_type))
888 const Symtab *symtab;
889 const addr_t file_addr;
890 Symbol *match_symbol;
891 const uint32_t *match_index_ptr;
895 // Add all the section file start address & size to the RangeVector,
896 // recusively adding any children sections.
898 AddSectionsToRangeMap (SectionList *sectlist, RangeVector<addr_t, addr_t> §ion_ranges)
900 const int num_sections = sectlist->GetNumSections (0);
901 for (int i = 0; i < num_sections; i++)
903 SectionSP sect_sp = sectlist->GetSectionAtIndex (i);
906 SectionList &child_sectlist = sect_sp->GetChildren();
908 // If this section has children, add the children to the RangeVector.
909 // Else add this section to the RangeVector.
910 if (child_sectlist.GetNumSections (0) > 0)
912 AddSectionsToRangeMap (&child_sectlist, section_ranges);
916 size_t size = sect_sp->GetByteSize();
919 addr_t base_addr = sect_sp->GetFileAddress();
920 RangeVector<addr_t, addr_t>::Entry entry;
921 entry.SetRangeBase (base_addr);
922 entry.SetByteSize (size);
923 section_ranges.Append (entry);
931 Symtab::InitAddressIndexes()
933 // Protected function, no need to lock mutex...
934 if (!m_file_addr_to_index_computed && !m_symbols.empty())
936 m_file_addr_to_index_computed = true;
938 FileRangeToIndexMap::Entry entry;
939 const_iterator begin = m_symbols.begin();
940 const_iterator end = m_symbols.end();
941 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
943 if (pos->ValueIsAddress())
945 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
946 entry.SetByteSize(pos->GetByteSize());
947 entry.data = std::distance(begin, pos);
948 m_file_addr_to_index.Append(entry);
951 const size_t num_entries = m_file_addr_to_index.GetSize();
954 m_file_addr_to_index.Sort();
956 // Create a RangeVector with the start & size of all the sections for
957 // this objfile. We'll need to check this for any FileRangeToIndexMap
958 // entries with an uninitialized size, which could potentially be a
959 // large number so reconstituting the weak pointer is busywork when it
960 // is invariant information.
961 SectionList *sectlist = m_objfile->GetSectionList();
962 RangeVector<addr_t, addr_t> section_ranges;
965 AddSectionsToRangeMap (sectlist, section_ranges);
966 section_ranges.Sort();
969 // Iterate through the FileRangeToIndexMap and fill in the size for any
970 // entries that didn't already have a size from the Symbol (e.g. if we
971 // have a plain linker symbol with an address only, instead of debug info
972 // where we get an address and a size and a type, etc.)
973 for (size_t i = 0; i < num_entries; i++)
975 FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.GetMutableEntryAtIndex (i);
976 if (entry->GetByteSize() == 0)
978 addr_t curr_base_addr = entry->GetRangeBase();
979 const RangeVector<addr_t, addr_t>::Entry *containing_section =
980 section_ranges.FindEntryThatContains (curr_base_addr);
982 // Use the end of the section as the default max size of the symbol
984 if (containing_section)
986 sym_size = containing_section->GetByteSize() -
987 (entry->GetRangeBase() - containing_section->GetRangeBase());
990 for (size_t j = i; j < num_entries; j++)
992 FileRangeToIndexMap::Entry *next_entry = m_file_addr_to_index.GetMutableEntryAtIndex (j);
993 addr_t next_base_addr = next_entry->GetRangeBase();
994 if (next_base_addr > curr_base_addr)
996 addr_t size_to_next_symbol = next_base_addr - curr_base_addr;
998 // Take the difference between this symbol and the next one as its size,
999 // if it is less than the size of the section.
1000 if (sym_size == 0 || size_to_next_symbol < sym_size)
1002 sym_size = size_to_next_symbol;
1010 entry->SetByteSize (sym_size);
1011 Symbol &symbol = m_symbols[entry->data];
1012 symbol.SetByteSize (sym_size);
1013 symbol.SetSizeIsSynthesized (true);
1018 // Sort again in case the range size changes the ordering
1019 m_file_addr_to_index.Sort();
1025 Symtab::CalculateSymbolSizes ()
1027 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1029 if (!m_symbols.empty())
1031 if (!m_file_addr_to_index_computed)
1032 InitAddressIndexes();
1034 const size_t num_entries = m_file_addr_to_index.GetSize();
1036 for (size_t i = 0; i < num_entries; ++i)
1038 // The entries in the m_file_addr_to_index have calculated the sizes already
1039 // so we will use this size if we need to.
1040 const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i);
1042 Symbol &symbol = m_symbols[entry.data];
1044 // If the symbol size is already valid, no need to do anything
1045 if (symbol.GetByteSizeIsValid())
1048 const addr_t range_size = entry.GetByteSize();
1051 symbol.SetByteSize(range_size);
1052 symbol.SetSizeIsSynthesized(true);
1059 Symtab::FindSymbolAtFileAddress (addr_t file_addr)
1061 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1062 if (!m_file_addr_to_index_computed)
1063 InitAddressIndexes();
1065 const FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.FindEntryStartsAt(file_addr);
1068 Symbol* symbol = SymbolAtIndex(entry->data);
1069 if (symbol->GetFileAddress() == file_addr)
1076 Symtab::FindSymbolContainingFileAddress (addr_t file_addr)
1078 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1080 if (!m_file_addr_to_index_computed)
1081 InitAddressIndexes();
1083 const FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.FindEntryThatContains(file_addr);
1086 Symbol* symbol = SymbolAtIndex(entry->data);
1087 if (symbol->ContainsFileAddress(file_addr))
1094 Symtab::ForEachSymbolContainingFileAddress(addr_t file_addr, std::function<bool(Symbol *)> const &callback)
1096 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1098 if (!m_file_addr_to_index_computed)
1099 InitAddressIndexes();
1101 std::vector<uint32_t> all_addr_indexes;
1103 // Get all symbols with file_addr
1104 const size_t addr_match_count = m_file_addr_to_index.FindEntryIndexesThatContain(file_addr, all_addr_indexes);
1106 for (size_t i = 0; i < addr_match_count; ++i)
1108 Symbol* symbol = SymbolAtIndex(all_addr_indexes[i]);
1109 if (symbol->ContainsFileAddress(file_addr))
1111 if (!callback(symbol))
1118 Symtab::SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1120 // No need to protect this call using m_mutex all other method calls are
1121 // already thread safe.
1123 const bool merge_symbol_into_function = true;
1124 size_t num_indices = symbol_indexes.size();
1125 if (num_indices > 0)
1128 sc.module_sp = m_objfile->GetModule();
1129 for (size_t i = 0; i < num_indices; i++)
1131 sc.symbol = SymbolAtIndex (symbol_indexes[i]);
1133 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1140 Symtab::FindFunctionSymbols (const ConstString &name,
1141 uint32_t name_type_mask,
1142 SymbolContextList& sc_list)
1145 std::vector<uint32_t> symbol_indexes;
1147 const char *name_cstr = name.GetCString();
1149 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
1150 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
1152 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull))
1154 std::vector<uint32_t> temp_symbol_indexes;
1155 FindAllSymbolsWithNameAndType (name, eSymbolTypeAny, temp_symbol_indexes);
1157 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1158 if (temp_symbol_indexes_size > 0)
1160 std::lock_guard<std::recursive_mutex> guard(m_mutex);
1161 for (unsigned i = 0; i < temp_symbol_indexes_size; i++)
1163 SymbolContext sym_ctx;
1164 sym_ctx.symbol = SymbolAtIndex (temp_symbol_indexes[i]);
1167 switch (sym_ctx.symbol->GetType())
1169 case eSymbolTypeCode:
1170 case eSymbolTypeResolver:
1171 case eSymbolTypeReExported:
1172 symbol_indexes.push_back(temp_symbol_indexes[i]);
1182 if (name_type_mask & eFunctionNameTypeBase)
1184 // From mangled names we can't tell what is a basename and what
1185 // is a method name, so we just treat them the same
1186 if (!m_name_indexes_computed)
1189 if (!m_basename_to_index.IsEmpty())
1191 const UniqueCStringMap<uint32_t>::Entry *match;
1192 for (match = m_basename_to_index.FindFirstValueForName(name_cstr);
1194 match = m_basename_to_index.FindNextValueForName(match))
1196 symbol_indexes.push_back(match->value);
1201 if (name_type_mask & eFunctionNameTypeMethod)
1203 if (!m_name_indexes_computed)
1206 if (!m_method_to_index.IsEmpty())
1208 const UniqueCStringMap<uint32_t>::Entry *match;
1209 for (match = m_method_to_index.FindFirstValueForName(name_cstr);
1211 match = m_method_to_index.FindNextValueForName(match))
1213 symbol_indexes.push_back(match->value);
1218 if (name_type_mask & eFunctionNameTypeSelector)
1220 if (!m_name_indexes_computed)
1223 if (!m_selector_to_index.IsEmpty())
1225 const UniqueCStringMap<uint32_t>::Entry *match;
1226 for (match = m_selector_to_index.FindFirstValueForName(name_cstr);
1228 match = m_selector_to_index.FindNextValueForName(match))
1230 symbol_indexes.push_back(match->value);
1235 if (!symbol_indexes.empty())
1237 std::sort(symbol_indexes.begin(), symbol_indexes.end());
1238 symbol_indexes.erase(std::unique(symbol_indexes.begin(), symbol_indexes.end()), symbol_indexes.end());
1239 count = symbol_indexes.size();
1240 SymbolIndicesToSymbolContextList (symbol_indexes, sc_list);
1248 Symtab::GetParent (Symbol *child_symbol) const
1250 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1251 if (child_idx != UINT32_MAX && child_idx > 0)
1253 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx)
1255 const Symbol *symbol = SymbolAtIndex (idx);
1256 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1257 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)