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/SymbolContext.h"
18 #include "lldb/Symbol/Symtab.h"
19 #include "lldb/Target/CPPLanguageRuntime.h"
20 #include "lldb/Target/ObjCLanguageRuntime.h"
23 using namespace lldb_private;
27 Symtab::Symtab(ObjectFile *objfile) :
30 m_file_addr_to_index (),
32 m_mutex (Mutex::eMutexTypeRecursive),
33 m_file_addr_to_index_computed (false),
34 m_name_indexes_computed (false)
43 Symtab::Reserve(size_t count)
45 // Clients should grab the mutex from this symbol table and lock it manually
46 // when calling this function to avoid performance issues.
47 m_symbols.reserve (count);
51 Symtab::Resize(size_t count)
53 // Clients should grab the mutex from this symbol table and lock it manually
54 // when calling this function to avoid performance issues.
55 m_symbols.resize (count);
60 Symtab::AddSymbol(const Symbol& symbol)
62 // Clients should grab the mutex from this symbol table and lock it manually
63 // when calling this function to avoid performance issues.
64 uint32_t symbol_idx = m_symbols.size();
65 m_name_to_index.Clear();
66 m_file_addr_to_index.Clear();
67 m_symbols.push_back(symbol);
68 m_file_addr_to_index_computed = false;
69 m_name_indexes_computed = false;
74 Symtab::GetNumSymbols() const
76 Mutex::Locker locker (m_mutex);
77 return m_symbols.size();
81 Symtab::Dump (Stream *s, Target *target, SortOrder sort_order)
83 Mutex::Locker locker (m_mutex);
85 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
87 const FileSpec &file_spec = m_objfile->GetFileSpec();
88 const char * object_name = NULL;
89 if (m_objfile->GetModule())
90 object_name = m_objfile->GetModule()->GetObjectName().GetCString();
93 s->Printf("Symtab, file = %s%s%s%s, num_symbols = %lu",
94 file_spec.GetPath().c_str(),
95 object_name ? "(" : "",
96 object_name ? object_name : "",
97 object_name ? ")" : "",
100 s->Printf("Symtab, num_symbols = %lu", m_symbols.size());
102 if (!m_symbols.empty())
108 s->PutCString (":\n");
109 DumpSymbolHeader (s);
110 const_iterator begin = m_symbols.begin();
111 const_iterator end = m_symbols.end();
112 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
115 pos->Dump(s, target, std::distance(begin, pos));
120 case eSortOrderByName:
122 // Although we maintain a lookup by exact name map, the table
123 // isn't sorted by name. So we must make the ordered symbol list
125 s->PutCString (" (sorted by name):\n");
126 DumpSymbolHeader (s);
127 typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol;
128 CStringToSymbol name_map;
129 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos)
131 const char *name = pos->GetMangled().GetName(Mangled::ePreferDemangled).AsCString();
133 name_map.insert (std::make_pair(name, &(*pos)));
136 for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos)
139 pos->second->Dump (s, target, pos->second - &m_symbols[0]);
144 case eSortOrderByAddress:
145 s->PutCString (" (sorted by address):\n");
146 DumpSymbolHeader (s);
147 if (!m_file_addr_to_index_computed)
148 InitAddressIndexes();
149 const size_t num_entries = m_file_addr_to_index.GetSize();
150 for (size_t i=0; i<num_entries; ++i)
153 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data;
154 m_symbols[symbol_idx].Dump(s, target, symbol_idx);
162 Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const
164 Mutex::Locker locker (m_mutex);
166 const size_t num_symbols = GetNumSymbols();
167 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
169 s->Printf("Symtab %lu symbol indexes (%lu symbols total):\n", indexes.size(), m_symbols.size());
172 if (!indexes.empty())
174 std::vector<uint32_t>::const_iterator pos;
175 std::vector<uint32_t>::const_iterator end = indexes.end();
176 DumpSymbolHeader (s);
177 for (pos = indexes.begin(); pos != end; ++pos)
180 if (idx < num_symbols)
183 m_symbols[idx].Dump(s, target, idx);
191 Symtab::DumpSymbolHeader (Stream *s)
193 s->Indent(" Debug symbol\n");
194 s->Indent(" |Synthetic symbol\n");
195 s->Indent(" ||Externally Visible\n");
197 s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n");
198 s->Indent("------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------\n");
203 CompareSymbolID (const void *key, const void *p)
205 const user_id_t match_uid = *(user_id_t*) key;
206 const user_id_t symbol_uid = ((Symbol *)p)->GetID();
207 if (match_uid < symbol_uid)
209 if (match_uid > symbol_uid)
215 Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const
217 Mutex::Locker locker (m_mutex);
219 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid,
222 (uint8_t *)&m_symbols[1] - (uint8_t *)&m_symbols[0],
229 Symtab::SymbolAtIndex(size_t idx)
231 // Clients should grab the mutex from this symbol table and lock it manually
232 // when calling this function to avoid performance issues.
233 if (idx < m_symbols.size())
234 return &m_symbols[idx];
240 Symtab::SymbolAtIndex(size_t idx) const
242 // Clients should grab the mutex from this symbol table and lock it manually
243 // when calling this function to avoid performance issues.
244 if (idx < m_symbols.size())
245 return &m_symbols[idx];
249 //----------------------------------------------------------------------
251 //----------------------------------------------------------------------
253 Symtab::InitNameIndexes()
255 // Protected function, no need to lock mutex...
256 if (!m_name_indexes_computed)
258 m_name_indexes_computed = true;
259 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
260 // Create the name index vector to be able to quickly search by name
261 const size_t num_symbols = m_symbols.size();
263 m_name_to_index.Reserve (num_symbols);
265 // TODO: benchmark this to see if we save any memory. Otherwise we
266 // will always keep the memory reserved in the vector unless we pull
267 // some STL swap magic and then recopy...
268 uint32_t actual_count = 0;
269 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
273 const Mangled &mangled = pos->GetMangled();
274 if (mangled.GetMangledName())
277 if (mangled.GetDemangledName())
281 m_name_to_index.Reserve (actual_count);
284 NameToIndexMap::Entry entry;
286 // The "const char *" in "class_contexts" must come from a ConstString::GetCString()
287 std::set<const char *> class_contexts;
288 UniqueCStringMap<uint32_t> mangled_name_to_index;
289 std::vector<const char *> symbol_contexts(num_symbols, NULL);
291 for (entry.value = 0; entry.value<num_symbols; ++entry.value)
293 const Symbol *symbol = &m_symbols[entry.value];
295 // Don't let trampolines get into the lookup by name map
296 // If we ever need the trampoline symbols to be searchable by name
297 // we can remove this and then possibly add a new bool to any of the
298 // Symtab functions that lookup symbols by name to indicate if they
300 if (symbol->IsTrampoline())
303 const Mangled &mangled = symbol->GetMangled();
304 entry.cstring = mangled.GetMangledName().GetCString();
305 if (entry.cstring && entry.cstring[0])
307 m_name_to_index.Append (entry);
309 const SymbolType symbol_type = symbol->GetType();
310 if (symbol_type == eSymbolTypeCode || symbol_type == eSymbolTypeResolver)
312 if (entry.cstring[0] == '_' && entry.cstring[1] == 'Z' &&
313 (entry.cstring[2] != 'T' && // avoid virtual table, VTT structure, typeinfo structure, and typeinfo name
314 entry.cstring[2] != 'G' && // avoid guard variables
315 entry.cstring[2] != 'Z')) // named local entities (if we eventually handle eSymbolTypeData, we will want this back)
317 CPPLanguageRuntime::MethodName cxx_method (mangled.GetDemangledName());
318 entry.cstring = ConstString(cxx_method.GetBasename()).GetCString();
319 if (entry.cstring && entry.cstring[0])
321 // ConstString objects permanently store the string in the pool so calling
322 // GetCString() on the value gets us a const char * that will never go away
323 const char *const_context = ConstString(cxx_method.GetContext()).GetCString();
325 if (entry.cstring[0] == '~' || !cxx_method.GetQualifiers().empty())
327 // The first character of the demangled basename is '~' which
328 // means we have a class destructor. We can use this information
329 // to help us know what is a class and what isn't.
330 if (class_contexts.find(const_context) == class_contexts.end())
331 class_contexts.insert(const_context);
332 m_method_to_index.Append (entry);
336 if (const_context && const_context[0])
338 if (class_contexts.find(const_context) != class_contexts.end())
340 // The current decl context is in our "class_contexts" which means
341 // this is a method on a class
342 m_method_to_index.Append (entry);
346 // We don't know if this is a function basename or a method,
347 // so put it into a temporary collection so once we are done
348 // we can look in class_contexts to see if each entry is a class
349 // or just a function and will put any remaining items into
350 // m_method_to_index or m_basename_to_index as needed
351 mangled_name_to_index.Append (entry);
352 symbol_contexts[entry.value] = const_context;
357 // No context for this function so this has to be a basename
358 m_basename_to_index.Append(entry);
366 entry.cstring = mangled.GetDemangledName().GetCString();
367 if (entry.cstring && entry.cstring[0])
368 m_name_to_index.Append (entry);
370 // If the demangled name turns out to be an ObjC name, and
371 // is a category name, add the version without categories to the index too.
372 ObjCLanguageRuntime::MethodName objc_method (entry.cstring, true);
373 if (objc_method.IsValid(true))
375 entry.cstring = objc_method.GetSelector().GetCString();
376 m_selector_to_index.Append (entry);
378 ConstString objc_method_no_category (objc_method.GetFullNameWithoutCategory(true));
379 if (objc_method_no_category)
381 entry.cstring = objc_method_no_category.GetCString();
382 m_name_to_index.Append (entry);
389 if (!mangled_name_to_index.IsEmpty())
391 count = mangled_name_to_index.GetSize();
392 for (size_t i=0; i<count; ++i)
394 if (mangled_name_to_index.GetValueAtIndex(i, entry.value))
396 entry.cstring = mangled_name_to_index.GetCStringAtIndex(i);
397 if (symbol_contexts[entry.value] && class_contexts.find(symbol_contexts[entry.value]) != class_contexts.end())
399 m_method_to_index.Append (entry);
403 // If we got here, we have something that had a context (was inside a namespace or class)
404 // yet we don't know if the entry
405 m_method_to_index.Append (entry);
406 m_basename_to_index.Append (entry);
411 m_name_to_index.Sort();
412 m_name_to_index.SizeToFit();
413 m_selector_to_index.Sort();
414 m_selector_to_index.SizeToFit();
415 m_basename_to_index.Sort();
416 m_basename_to_index.SizeToFit();
417 m_method_to_index.Sort();
418 m_method_to_index.SizeToFit();
420 // static StreamFile a ("/tmp/a.txt");
422 // count = m_basename_to_index.GetSize();
425 // for (size_t i=0; i<count; ++i)
427 // if (m_basename_to_index.GetValueAtIndex(i, entry.value))
428 // a.Printf ("%s BASENAME\n", m_symbols[entry.value].GetMangled().GetName().GetCString());
431 // count = m_method_to_index.GetSize();
434 // for (size_t i=0; i<count; ++i)
436 // if (m_method_to_index.GetValueAtIndex(i, entry.value))
437 // a.Printf ("%s METHOD\n", m_symbols[entry.value].GetMangled().GetName().GetCString());
444 Symtab::AppendSymbolNamesToMap (const IndexCollection &indexes,
447 NameToIndexMap &name_to_index_map) const
449 if (add_demangled || add_mangled)
451 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
452 Mutex::Locker locker (m_mutex);
454 // Create the name index vector to be able to quickly search by name
455 NameToIndexMap::Entry entry;
456 const size_t num_indexes = indexes.size();
457 for (size_t i=0; i<num_indexes; ++i)
459 entry.value = indexes[i];
460 assert (i < m_symbols.size());
461 const Symbol *symbol = &m_symbols[entry.value];
463 const Mangled &mangled = symbol->GetMangled();
466 entry.cstring = mangled.GetDemangledName().GetCString();
467 if (entry.cstring && entry.cstring[0])
468 name_to_index_map.Append (entry);
473 entry.cstring = mangled.GetMangledName().GetCString();
474 if (entry.cstring && entry.cstring[0])
475 name_to_index_map.Append (entry);
482 Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
484 Mutex::Locker locker (m_mutex);
486 uint32_t prev_size = indexes.size();
488 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
490 for (uint32_t i = start_idx; i < count; ++i)
492 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
493 indexes.push_back(i);
496 return indexes.size() - prev_size;
500 Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
502 Mutex::Locker locker (m_mutex);
504 uint32_t prev_size = indexes.size();
506 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
508 for (uint32_t i = start_idx; i < count; ++i)
510 if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value)
511 indexes.push_back(i);
514 return indexes.size() - prev_size;
518 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
520 Mutex::Locker locker (m_mutex);
522 uint32_t prev_size = indexes.size();
524 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
526 for (uint32_t i = start_idx; i < count; ++i)
528 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
530 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
531 indexes.push_back(i);
535 return indexes.size() - prev_size;
540 Symtab::GetIndexForSymbol (const Symbol *symbol) const
542 const Symbol *first_symbol = &m_symbols[0];
543 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
544 return symbol - first_symbol;
548 struct SymbolSortInfo
550 const bool sort_by_load_addr;
551 const Symbol *symbols;
555 struct SymbolIndexComparator {
556 const std::vector<Symbol>& symbols;
557 std::vector<lldb::addr_t> &addr_cache;
559 // Getting from the symbol to the Address to the File Address involves some work.
560 // Since there are potentially many symbols here, and we're using this for sorting so
561 // we're going to be computing the address many times, cache that in addr_cache.
562 // The array passed in has to be the same size as the symbols array passed into the
563 // member variable symbols, and should be initialized with LLDB_INVALID_ADDRESS.
564 // NOTE: You have to make addr_cache externally and pass it in because std::stable_sort
565 // makes copies of the comparator it is initially passed in, and you end up spending
566 // huge amounts of time copying this array...
568 SymbolIndexComparator(const std::vector<Symbol>& s, std::vector<lldb::addr_t> &a) : symbols(s), addr_cache(a) {
569 assert (symbols.size() == addr_cache.size());
571 bool operator()(uint32_t index_a, uint32_t index_b) {
572 addr_t value_a = addr_cache[index_a];
573 if (value_a == LLDB_INVALID_ADDRESS)
575 value_a = symbols[index_a].GetAddress().GetFileAddress();
576 addr_cache[index_a] = value_a;
579 addr_t value_b = addr_cache[index_b];
580 if (value_b == LLDB_INVALID_ADDRESS)
582 value_b = symbols[index_b].GetAddress().GetFileAddress();
583 addr_cache[index_b] = value_b;
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();
596 } else if (value_a < value_b)
605 Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const
607 Mutex::Locker locker (m_mutex);
609 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__);
610 // No need to sort if we have zero or one items...
611 if (indexes.size() <= 1)
614 // Sort the indexes in place using std::stable_sort.
615 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance,
616 // not correctness. The indexes vector tends to be "close" to sorted, which the
617 // stable sort handles better.
619 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS);
621 SymbolIndexComparator comparator(m_symbols, addr_cache);
622 std::stable_sort(indexes.begin(), indexes.end(), comparator);
624 // Remove any duplicates if requested
625 if (remove_duplicates)
626 std::unique(indexes.begin(), indexes.end());
630 Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes)
632 Mutex::Locker locker (m_mutex);
634 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
637 const char *symbol_cstr = symbol_name.GetCString();
638 if (!m_name_indexes_computed)
641 return m_name_to_index.GetValues (symbol_cstr, indexes);
647 Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
649 Mutex::Locker locker (m_mutex);
651 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
654 const size_t old_size = indexes.size();
655 if (!m_name_indexes_computed)
658 const char *symbol_cstr = symbol_name.GetCString();
660 std::vector<uint32_t> all_name_indexes;
661 const size_t name_match_count = m_name_to_index.GetValues (symbol_cstr, all_name_indexes);
662 for (size_t i=0; i<name_match_count; ++i)
664 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, symbol_visibility))
665 indexes.push_back (all_name_indexes[i]);
667 return indexes.size() - old_size;
673 Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes)
675 Mutex::Locker locker (m_mutex);
677 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0)
679 std::vector<uint32_t>::iterator pos = indexes.begin();
680 while (pos != indexes.end())
682 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
688 return indexes.size();
692 Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
694 Mutex::Locker locker (m_mutex);
696 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0)
698 std::vector<uint32_t>::iterator pos = indexes.begin();
699 while (pos != indexes.end())
701 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
707 return indexes.size();
712 Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, std::vector<uint32_t>& indexes)
714 Mutex::Locker locker (m_mutex);
716 uint32_t prev_size = indexes.size();
717 uint32_t sym_end = m_symbols.size();
719 for (uint32_t i = 0; i < sym_end; i++)
721 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
723 const char *name = m_symbols[i].GetMangled().GetName().AsCString();
726 if (regexp.Execute (name))
727 indexes.push_back(i);
731 return indexes.size() - prev_size;
736 Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
738 Mutex::Locker locker (m_mutex);
740 uint32_t prev_size = indexes.size();
741 uint32_t sym_end = m_symbols.size();
743 for (uint32_t i = 0; i < sym_end; i++)
745 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
747 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false)
750 const char *name = m_symbols[i].GetMangled().GetName().AsCString();
753 if (regexp.Execute (name))
754 indexes.push_back(i);
758 return indexes.size() - prev_size;
763 Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx)
765 Mutex::Locker locker (m_mutex);
767 const size_t count = m_symbols.size();
768 for (size_t idx = start_idx; idx < count; ++idx)
770 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type)
772 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility))
775 return &m_symbols[idx];
783 Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes)
785 Mutex::Locker locker (m_mutex);
787 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
788 // Initialize all of the lookup by name indexes before converting NAME
789 // to a uniqued string NAME_STR below.
790 if (!m_name_indexes_computed)
795 // The string table did have a string that matched, but we need
796 // to check the symbols and match the symbol_type if any was given.
797 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes);
799 return symbol_indexes.size();
803 Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
805 Mutex::Locker locker (m_mutex);
807 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
808 // Initialize all of the lookup by name indexes before converting NAME
809 // to a uniqued string NAME_STR below.
810 if (!m_name_indexes_computed)
815 // The string table did have a string that matched, but we need
816 // to check the symbols and match the symbol_type if any was given.
817 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
819 return symbol_indexes.size();
823 Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression ®ex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
825 Mutex::Locker locker (m_mutex);
827 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
828 return symbol_indexes.size();
832 Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
834 Mutex::Locker locker (m_mutex);
836 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
837 if (!m_name_indexes_computed)
842 std::vector<uint32_t> matching_indexes;
843 // The string table did have a string that matched, but we need
844 // to check the symbols and match the symbol_type if any was given.
845 if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes))
847 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
848 for (pos = matching_indexes.begin(); pos != end; ++pos)
850 Symbol *symbol = SymbolAtIndex(*pos);
852 if (symbol->Compare(name, symbol_type))
862 const Symtab *symtab;
863 const addr_t file_addr;
864 Symbol *match_symbol;
865 const uint32_t *match_index_ptr;
870 SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
872 const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
873 if (curr_symbol == NULL)
876 const addr_t info_file_addr = info->file_addr;
878 // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address
879 // range if the symbol has a section!
880 if (curr_symbol->ValueIsAddress())
882 const addr_t curr_file_addr = curr_symbol->GetAddress().GetFileAddress();
883 if (info_file_addr < curr_file_addr)
885 if (info_file_addr > curr_file_addr)
887 info->match_symbol = const_cast<Symbol *>(curr_symbol);
888 info->match_index_ptr = index_ptr;
896 SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
898 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
902 const addr_t info_file_addr = info->file_addr;
903 if (symbol->ValueIsAddress())
905 const addr_t curr_file_addr = symbol->GetAddress().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)
924 static SymbolSearchInfo
925 FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
927 SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 };
932 (ComparisonFunction)SymbolWithClosestFileAddress);
938 Symtab::InitAddressIndexes()
940 // Protected function, no need to lock mutex...
941 if (!m_file_addr_to_index_computed && !m_symbols.empty())
943 m_file_addr_to_index_computed = true;
945 FileRangeToIndexMap::Entry entry;
946 const_iterator begin = m_symbols.begin();
947 const_iterator end = m_symbols.end();
948 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
950 if (pos->ValueIsAddress())
952 entry.SetRangeBase(pos->GetAddress().GetFileAddress());
953 entry.SetByteSize(pos->GetByteSize());
954 entry.data = std::distance(begin, pos);
955 m_file_addr_to_index.Append(entry);
958 const size_t num_entries = m_file_addr_to_index.GetSize();
961 m_file_addr_to_index.Sort();
962 m_file_addr_to_index.CalculateSizesOfZeroByteSizeRanges();
964 // Now our last symbols might not have had sizes because there
965 // was no subsequent symbol to calculate the size from. If this is
966 // the case, then calculate the size by capping it at the end of the
967 // section in which the symbol resides
968 for (int i = num_entries - 1; i >= 0; --i)
970 const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i);
971 // As we iterate backwards, as soon as we find a symbol with a valid
972 // byte size, we are done
973 if (entry.GetByteSize() > 0)
976 // Cap the size to the end of the section in which the symbol resides
977 SectionSP section_sp (m_objfile->GetSectionList()->FindSectionContainingFileAddress (entry.GetRangeBase()));
980 const lldb::addr_t end_section_file_addr = section_sp->GetFileAddress() + section_sp->GetByteSize();
981 const lldb::addr_t symbol_file_addr = entry.GetRangeBase();
982 if (end_section_file_addr > symbol_file_addr)
984 Symbol &symbol = m_symbols[entry.data];
986 symbol.SetByteSize(end_section_file_addr - symbol_file_addr);
987 symbol.SetSizeIsSynthesized(true);
991 // Sort again in case the range size changes the ordering
992 m_file_addr_to_index.Sort();
998 Symtab::CalculateSymbolSizes ()
1000 Mutex::Locker locker (m_mutex);
1002 if (!m_symbols.empty())
1004 if (!m_file_addr_to_index_computed)
1005 InitAddressIndexes();
1007 const size_t num_entries = m_file_addr_to_index.GetSize();
1009 for (size_t i = 0; i < num_entries; ++i)
1011 // The entries in the m_file_addr_to_index have calculated the sizes already
1012 // so we will use this size if we need to.
1013 const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i);
1015 Symbol &symbol = m_symbols[entry.data];
1017 // If the symbol size is already valid, no need to do anything
1018 if (symbol.GetByteSizeIsValid())
1021 const addr_t range_size = entry.GetByteSize();
1024 symbol.SetByteSize(range_size);
1025 symbol.SetSizeIsSynthesized(true);
1032 Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
1034 Mutex::Locker locker (m_mutex);
1037 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 };
1043 (ComparisonFunction)SymbolWithClosestFileAddress);
1045 if (info.match_symbol)
1047 if (info.match_offset == 0)
1049 // We found an exact match!
1050 return info.match_symbol;
1053 const size_t symbol_byte_size = info.match_symbol->GetByteSize();
1055 if (symbol_byte_size == 0)
1057 // We weren't able to find the size of the symbol so lets just go
1058 // with that match we found in our search...
1059 return info.match_symbol;
1062 // We were able to figure out a symbol size so lets make sure our
1063 // offset puts "file_addr" in the symbol's address range.
1064 if (info.match_offset < symbol_byte_size)
1065 return info.match_symbol;
1071 Symtab::FindSymbolContainingFileAddress (addr_t file_addr)
1073 Mutex::Locker locker (m_mutex);
1075 if (!m_file_addr_to_index_computed)
1076 InitAddressIndexes();
1078 const FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.FindEntryThatContains(file_addr);
1080 return SymbolAtIndex(entry->data);
1085 Symtab::SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list)
1087 // No need to protect this call using m_mutex all other method calls are
1088 // already thread safe.
1090 const bool merge_symbol_into_function = true;
1091 size_t num_indices = symbol_indexes.size();
1092 if (num_indices > 0)
1095 sc.module_sp = m_objfile->GetModule();
1096 for (size_t i = 0; i < num_indices; i++)
1098 sc.symbol = SymbolAtIndex (symbol_indexes[i]);
1100 sc_list.AppendIfUnique(sc, merge_symbol_into_function);
1107 Symtab::FindFunctionSymbols (const ConstString &name,
1108 uint32_t name_type_mask,
1109 SymbolContextList& sc_list)
1112 std::vector<uint32_t> symbol_indexes;
1114 const char *name_cstr = name.GetCString();
1116 // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup()
1117 assert ((name_type_mask & eFunctionNameTypeAuto) == 0);
1119 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull))
1121 std::vector<uint32_t> temp_symbol_indexes;
1122 FindAllSymbolsWithNameAndType (name, eSymbolTypeAny, temp_symbol_indexes);
1124 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size();
1125 if (temp_symbol_indexes_size > 0)
1127 Mutex::Locker locker (m_mutex);
1128 for (unsigned i = 0; i < temp_symbol_indexes_size; i++)
1130 SymbolContext sym_ctx;
1131 sym_ctx.symbol = SymbolAtIndex (temp_symbol_indexes[i]);
1134 switch (sym_ctx.symbol->GetType())
1136 case eSymbolTypeCode:
1137 case eSymbolTypeResolver:
1138 symbol_indexes.push_back(temp_symbol_indexes[i]);
1148 if (name_type_mask & eFunctionNameTypeBase)
1150 // From mangled names we can't tell what is a basename and what
1151 // is a method name, so we just treat them the same
1152 if (!m_name_indexes_computed)
1155 if (!m_basename_to_index.IsEmpty())
1157 const UniqueCStringMap<uint32_t>::Entry *match;
1158 for (match = m_basename_to_index.FindFirstValueForName(name_cstr);
1160 match = m_basename_to_index.FindNextValueForName(match))
1162 symbol_indexes.push_back(match->value);
1167 if (name_type_mask & eFunctionNameTypeMethod)
1169 if (!m_name_indexes_computed)
1172 if (!m_method_to_index.IsEmpty())
1174 const UniqueCStringMap<uint32_t>::Entry *match;
1175 for (match = m_method_to_index.FindFirstValueForName(name_cstr);
1177 match = m_method_to_index.FindNextValueForName(match))
1179 symbol_indexes.push_back(match->value);
1184 if (name_type_mask & eFunctionNameTypeSelector)
1186 if (!m_name_indexes_computed)
1189 if (!m_selector_to_index.IsEmpty())
1191 const UniqueCStringMap<uint32_t>::Entry *match;
1192 for (match = m_selector_to_index.FindFirstValueForName(name_cstr);
1194 match = m_selector_to_index.FindNextValueForName(match))
1196 symbol_indexes.push_back(match->value);
1201 if (!symbol_indexes.empty())
1203 std::sort(symbol_indexes.begin(), symbol_indexes.end());
1204 symbol_indexes.erase(std::unique(symbol_indexes.begin(), symbol_indexes.end()), symbol_indexes.end());
1205 count = symbol_indexes.size();
1206 SymbolIndicesToSymbolContextList (symbol_indexes, sc_list);