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 //===----------------------------------------------------------------------===//
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/RegularExpression.h"
14 #include "lldb/Core/Section.h"
15 #include "lldb/Core/Timer.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 #include "lldb/Symbol/Symtab.h"
20 #include "lldb/Target/CPPLanguageRuntime.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
24 using namespace lldb_private;
28 Symtab::Symtab(ObjectFile *objfile) :
31 m_file_addr_to_index (),
33 m_mutex (Mutex::eMutexTypeRecursive),
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);
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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 CPPLanguageRuntime::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 ObjCLanguageRuntime::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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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 Mutex::Locker locker (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;
896 SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
898 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
899 if (symbol == nullptr)
902 const addr_t info_file_addr = info->file_addr;
903 if (symbol->ValueIsAddress())
905 const addr_t curr_file_addr = symbol->GetAddressRef().GetFileAddress();
906 if (info_file_addr < curr_file_addr)
909 // Since we are finding the closest symbol that is greater than or equal
910 // to 'info->file_addr' we set the symbol here. This will get set
911 // multiple times, but after the search is done it will contain the best
913 info->match_symbol = const_cast<Symbol *>(symbol);
914 info->match_index_ptr = index_ptr;
915 info->match_offset = info_file_addr - curr_file_addr;
917 if (info_file_addr > curr_file_addr)
925 Symtab::InitAddressIndexes()
927 // Protected function, no need to lock mutex...
928 if (!m_file_addr_to_index_computed && !m_symbols.empty())
930 m_file_addr_to_index_computed = true;
932 FileRangeToIndexMap::Entry entry;
933 const_iterator begin = m_symbols.begin();
934 const_iterator end = m_symbols.end();
935 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
937 if (pos->ValueIsAddress())
939 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress());
940 entry.SetByteSize(pos->GetByteSize());
941 entry.data = std::distance(begin, pos);
942 m_file_addr_to_index.Append(entry);
945 const size_t num_entries = m_file_addr_to_index.GetSize();
948 m_file_addr_to_index.Sort();
949 m_file_addr_to_index.CalculateSizesOfZeroByteSizeRanges();
951 // Now our last symbols might not have had sizes because there
952 // was no subsequent symbol to calculate the size from. If this is
953 // the case, then calculate the size by capping it at the end of the
954 // section in which the symbol resides
955 for (int i = num_entries - 1; i >= 0; --i)
957 const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i);
958 // As we iterate backwards, as soon as we find a symbol with a valid
959 // byte size, we are done
960 if (entry.GetByteSize() > 0)
963 // Cap the size to the end of the section in which the symbol resides
964 SectionSP section_sp (m_objfile->GetSectionList()->FindSectionContainingFileAddress (entry.GetRangeBase()));
967 const lldb::addr_t end_section_file_addr = section_sp->GetFileAddress() + section_sp->GetByteSize();
968 const lldb::addr_t symbol_file_addr = entry.GetRangeBase();
969 if (end_section_file_addr > symbol_file_addr)
971 Symbol &symbol = m_symbols[entry.data];
973 symbol.SetByteSize(end_section_file_addr - symbol_file_addr);
974 symbol.SetSizeIsSynthesized(true);
978 // Sort again in case the range size changes the ordering
979 m_file_addr_to_index.Sort();
985 Symtab::CalculateSymbolSizes ()
987 Mutex::Locker locker (m_mutex);
989 if (!m_symbols.empty())
991 if (!m_file_addr_to_index_computed)
992 InitAddressIndexes();
994 const size_t num_entries = m_file_addr_to_index.GetSize();
996 for (size_t i = 0; i < num_entries; ++i)
998 // The entries in the m_file_addr_to_index have calculated the sizes already
999 // so we will use this size if we need to.
1000 const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i);
1002 Symbol &symbol = m_symbols[entry.data];
1004 // If the symbol size is already valid, no need to do anything
1005 if (symbol.GetByteSizeIsValid())
1008 const addr_t range_size = entry.GetByteSize();
1011 symbol.SetByteSize(range_size);
1012 symbol.SetSizeIsSynthesized(true);
1019 Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
1021 Mutex::Locker locker (m_mutex);
1024 SymbolSearchInfo info = { this, file_addr, nullptr, nullptr, 0 };
1030 (ComparisonFunction)SymbolWithClosestFileAddress);
1032 if (info.match_symbol)
1034 if (info.match_offset == 0)
1036 // We found an exact match!
1037 return info.match_symbol;
1040 const size_t symbol_byte_size = info.match_symbol->GetByteSize();
1042 if (symbol_byte_size == 0)
1044 // We weren't able to find the size of the symbol so lets just go
1045 // with that match we found in our search...
1046 return info.match_symbol;
1049 // We were able to figure out a symbol size so lets make sure our
1050 // offset puts "file_addr" in the symbol's address range.
1051 if (info.match_offset < symbol_byte_size)
1052 return info.match_symbol;
1058 Symtab::FindSymbolContainingFileAddress (addr_t file_addr)
1060 Mutex::Locker locker (m_mutex);
1062 if (!m_file_addr_to_index_computed)
1063 InitAddressIndexes();
1065 const FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.FindEntryThatContains(file_addr);
1067 return SymbolAtIndex(entry->data);
1072 Symtab::SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1074 // No need to protect this call using m_mutex all other method calls are
1075 // already thread safe.
1077 const bool merge_symbol_into_function = true;
1078 size_t num_indices = symbol_indexes.size();
1079 if (num_indices > 0)
1082 sc.module_sp = m_objfile->GetModule();
1083 for (size_t i = 0; i < num_indices; i++)
1085 sc.symbol = SymbolAtIndex (symbol_indexes[i]);
1087 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1094 Symtab::FindFunctionSymbols (const ConstString &name,
1095 uint32_t name_type_mask,
1096 SymbolContextList& sc_list)
1099 std::vector<uint32_t> symbol_indexes;
1101 const char *name_cstr = name.GetCString();
1103 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
1104 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
1106 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull))
1108 std::vector<uint32_t> temp_symbol_indexes;
1109 FindAllSymbolsWithNameAndType (name, eSymbolTypeAny, temp_symbol_indexes);
1111 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1112 if (temp_symbol_indexes_size > 0)
1114 Mutex::Locker locker (m_mutex);
1115 for (unsigned i = 0; i < temp_symbol_indexes_size; i++)
1117 SymbolContext sym_ctx;
1118 sym_ctx.symbol = SymbolAtIndex (temp_symbol_indexes[i]);
1121 switch (sym_ctx.symbol->GetType())
1123 case eSymbolTypeCode:
1124 case eSymbolTypeResolver:
1125 case eSymbolTypeReExported:
1126 symbol_indexes.push_back(temp_symbol_indexes[i]);
1136 if (name_type_mask & eFunctionNameTypeBase)
1138 // From mangled names we can't tell what is a basename and what
1139 // is a method name, so we just treat them the same
1140 if (!m_name_indexes_computed)
1143 if (!m_basename_to_index.IsEmpty())
1145 const UniqueCStringMap<uint32_t>::Entry *match;
1146 for (match = m_basename_to_index.FindFirstValueForName(name_cstr);
1148 match = m_basename_to_index.FindNextValueForName(match))
1150 symbol_indexes.push_back(match->value);
1155 if (name_type_mask & eFunctionNameTypeMethod)
1157 if (!m_name_indexes_computed)
1160 if (!m_method_to_index.IsEmpty())
1162 const UniqueCStringMap<uint32_t>::Entry *match;
1163 for (match = m_method_to_index.FindFirstValueForName(name_cstr);
1165 match = m_method_to_index.FindNextValueForName(match))
1167 symbol_indexes.push_back(match->value);
1172 if (name_type_mask & eFunctionNameTypeSelector)
1174 if (!m_name_indexes_computed)
1177 if (!m_selector_to_index.IsEmpty())
1179 const UniqueCStringMap<uint32_t>::Entry *match;
1180 for (match = m_selector_to_index.FindFirstValueForName(name_cstr);
1182 match = m_selector_to_index.FindNextValueForName(match))
1184 symbol_indexes.push_back(match->value);
1189 if (!symbol_indexes.empty())
1191 std::sort(symbol_indexes.begin(), symbol_indexes.end());
1192 symbol_indexes.erase(std::unique(symbol_indexes.begin(), symbol_indexes.end()), symbol_indexes.end());
1193 count = symbol_indexes.size();
1194 SymbolIndicesToSymbolContextList (symbol_indexes, sc_list);
1202 Symtab::GetParent (Symbol *child_symbol) const
1204 uint32_t child_idx = GetIndexForSymbol(child_symbol);
1205 if (child_idx != UINT32_MAX && child_idx > 0)
1207 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx)
1209 const Symbol *symbol = SymbolAtIndex (idx);
1210 const uint32_t sibling_idx = symbol->GetSiblingIndex();
1211 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx)