1 //===-- Symtab.cpp ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <map> 11 #include <set> 12 13 #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" 14 #include "Plugins/Language/ObjC/ObjCLanguage.h" 15 #include "lldb/Core/Module.h" 16 #include "lldb/Core/RegularExpression.h" 17 #include "lldb/Core/Section.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Core/Timer.h" 20 #include "lldb/Symbol/ObjectFile.h" 21 #include "lldb/Symbol/Symbol.h" 22 #include "lldb/Symbol/SymbolContext.h" 23 #include "lldb/Symbol/Symtab.h" 24 25 using namespace lldb; 26 using namespace lldb_private; 27 28 Symtab::Symtab(ObjectFile *objfile) 29 : m_objfile(objfile), m_symbols(), m_file_addr_to_index(), 30 m_name_to_index(), m_mutex(), m_file_addr_to_index_computed(false), 31 m_name_indexes_computed(false) {} 32 33 Symtab::~Symtab() {} 34 35 void Symtab::Reserve(size_t count) { 36 // Clients should grab the mutex from this symbol table and lock it manually 37 // when calling this function to avoid performance issues. 38 m_symbols.reserve(count); 39 } 40 41 Symbol *Symtab::Resize(size_t count) { 42 // Clients should grab the mutex from this symbol table and lock it manually 43 // when calling this function to avoid performance issues. 44 m_symbols.resize(count); 45 return m_symbols.empty() ? nullptr : &m_symbols[0]; 46 } 47 48 uint32_t Symtab::AddSymbol(const Symbol &symbol) { 49 // Clients should grab the mutex from this symbol table and lock it manually 50 // when calling this function to avoid performance issues. 51 uint32_t symbol_idx = m_symbols.size(); 52 m_name_to_index.Clear(); 53 m_file_addr_to_index.Clear(); 54 m_symbols.push_back(symbol); 55 m_file_addr_to_index_computed = false; 56 m_name_indexes_computed = false; 57 return symbol_idx; 58 } 59 60 size_t Symtab::GetNumSymbols() const { 61 std::lock_guard<std::recursive_mutex> guard(m_mutex); 62 return m_symbols.size(); 63 } 64 65 void Symtab::SectionFileAddressesChanged() { 66 m_name_to_index.Clear(); 67 m_file_addr_to_index_computed = false; 68 } 69 70 void Symtab::Dump(Stream *s, Target *target, SortOrder sort_order) { 71 std::lock_guard<std::recursive_mutex> guard(m_mutex); 72 73 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 74 s->Indent(); 75 const FileSpec &file_spec = m_objfile->GetFileSpec(); 76 const char *object_name = nullptr; 77 if (m_objfile->GetModule()) 78 object_name = m_objfile->GetModule()->GetObjectName().GetCString(); 79 80 if (file_spec) 81 s->Printf("Symtab, file = %s%s%s%s, num_symbols = %" PRIu64, 82 file_spec.GetPath().c_str(), object_name ? "(" : "", 83 object_name ? object_name : "", object_name ? ")" : "", 84 (uint64_t)m_symbols.size()); 85 else 86 s->Printf("Symtab, num_symbols = %" PRIu64 "", (uint64_t)m_symbols.size()); 87 88 if (!m_symbols.empty()) { 89 switch (sort_order) { 90 case eSortOrderNone: { 91 s->PutCString(":\n"); 92 DumpSymbolHeader(s); 93 const_iterator begin = m_symbols.begin(); 94 const_iterator end = m_symbols.end(); 95 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) { 96 s->Indent(); 97 pos->Dump(s, target, std::distance(begin, pos)); 98 } 99 } break; 100 101 case eSortOrderByName: { 102 // Although we maintain a lookup by exact name map, the table 103 // isn't sorted by name. So we must make the ordered symbol list 104 // up ourselves. 105 s->PutCString(" (sorted by name):\n"); 106 DumpSymbolHeader(s); 107 typedef std::multimap<const char *, const Symbol *, 108 CStringCompareFunctionObject> 109 CStringToSymbol; 110 CStringToSymbol name_map; 111 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 112 pos != end; ++pos) { 113 const char *name = pos->GetName().AsCString(); 114 if (name && name[0]) 115 name_map.insert(std::make_pair(name, &(*pos))); 116 } 117 118 for (CStringToSymbol::const_iterator pos = name_map.begin(), 119 end = name_map.end(); 120 pos != end; ++pos) { 121 s->Indent(); 122 pos->second->Dump(s, target, pos->second - &m_symbols[0]); 123 } 124 } break; 125 126 case eSortOrderByAddress: 127 s->PutCString(" (sorted by address):\n"); 128 DumpSymbolHeader(s); 129 if (!m_file_addr_to_index_computed) 130 InitAddressIndexes(); 131 const size_t num_entries = m_file_addr_to_index.GetSize(); 132 for (size_t i = 0; i < num_entries; ++i) { 133 s->Indent(); 134 const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data; 135 m_symbols[symbol_idx].Dump(s, target, symbol_idx); 136 } 137 break; 138 } 139 } 140 } 141 142 void Symtab::Dump(Stream *s, Target *target, 143 std::vector<uint32_t> &indexes) const { 144 std::lock_guard<std::recursive_mutex> guard(m_mutex); 145 146 const size_t num_symbols = GetNumSymbols(); 147 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 148 s->Indent(); 149 s->Printf("Symtab %" PRIu64 " symbol indexes (%" PRIu64 " symbols total):\n", 150 (uint64_t)indexes.size(), (uint64_t)m_symbols.size()); 151 s->IndentMore(); 152 153 if (!indexes.empty()) { 154 std::vector<uint32_t>::const_iterator pos; 155 std::vector<uint32_t>::const_iterator end = indexes.end(); 156 DumpSymbolHeader(s); 157 for (pos = indexes.begin(); pos != end; ++pos) { 158 size_t idx = *pos; 159 if (idx < num_symbols) { 160 s->Indent(); 161 m_symbols[idx].Dump(s, target, idx); 162 } 163 } 164 } 165 s->IndentLess(); 166 } 167 168 void Symtab::DumpSymbolHeader(Stream *s) { 169 s->Indent(" Debug symbol\n"); 170 s->Indent(" |Synthetic symbol\n"); 171 s->Indent(" ||Externally Visible\n"); 172 s->Indent(" |||\n"); 173 s->Indent("Index UserID DSX Type File Address/Value Load " 174 "Address Size Flags Name\n"); 175 s->Indent("------- ------ --- --------------- ------------------ " 176 "------------------ ------------------ ---------- " 177 "----------------------------------\n"); 178 } 179 180 static int CompareSymbolID(const void *key, const void *p) { 181 const user_id_t match_uid = *(const user_id_t *)key; 182 const user_id_t symbol_uid = ((const Symbol *)p)->GetID(); 183 if (match_uid < symbol_uid) 184 return -1; 185 if (match_uid > symbol_uid) 186 return 1; 187 return 0; 188 } 189 190 Symbol *Symtab::FindSymbolByID(lldb::user_id_t symbol_uid) const { 191 std::lock_guard<std::recursive_mutex> guard(m_mutex); 192 193 Symbol *symbol = 194 (Symbol *)::bsearch(&symbol_uid, &m_symbols[0], m_symbols.size(), 195 sizeof(m_symbols[0]), CompareSymbolID); 196 return symbol; 197 } 198 199 Symbol *Symtab::SymbolAtIndex(size_t idx) { 200 // Clients should grab the mutex from this symbol table and lock it manually 201 // when calling this function to avoid performance issues. 202 if (idx < m_symbols.size()) 203 return &m_symbols[idx]; 204 return nullptr; 205 } 206 207 const Symbol *Symtab::SymbolAtIndex(size_t idx) const { 208 // Clients should grab the mutex from this symbol table and lock it manually 209 // when calling this function to avoid performance issues. 210 if (idx < m_symbols.size()) 211 return &m_symbols[idx]; 212 return nullptr; 213 } 214 215 //---------------------------------------------------------------------- 216 // InitNameIndexes 217 //---------------------------------------------------------------------- 218 void Symtab::InitNameIndexes() { 219 // Protected function, no need to lock mutex... 220 if (!m_name_indexes_computed) { 221 m_name_indexes_computed = true; 222 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 223 // Create the name index vector to be able to quickly search by name 224 const size_t num_symbols = m_symbols.size(); 225 #if 1 226 m_name_to_index.Reserve(num_symbols); 227 #else 228 // TODO: benchmark this to see if we save any memory. Otherwise we 229 // will always keep the memory reserved in the vector unless we pull 230 // some STL swap magic and then recopy... 231 uint32_t actual_count = 0; 232 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 233 pos != end; ++pos) { 234 const Mangled &mangled = pos->GetMangled(); 235 if (mangled.GetMangledName()) 236 ++actual_count; 237 238 if (mangled.GetDemangledName()) 239 ++actual_count; 240 } 241 242 m_name_to_index.Reserve(actual_count); 243 #endif 244 245 NameToIndexMap::Entry entry; 246 247 // The "const char *" in "class_contexts" must come from a 248 // ConstString::GetCString() 249 std::set<const char *> class_contexts; 250 UniqueCStringMap<uint32_t> mangled_name_to_index; 251 std::vector<const char *> symbol_contexts(num_symbols, nullptr); 252 253 for (entry.value = 0; entry.value < num_symbols; ++entry.value) { 254 const Symbol *symbol = &m_symbols[entry.value]; 255 256 // Don't let trampolines get into the lookup by name map 257 // If we ever need the trampoline symbols to be searchable by name 258 // we can remove this and then possibly add a new bool to any of the 259 // Symtab functions that lookup symbols by name to indicate if they 260 // want trampolines. 261 if (symbol->IsTrampoline()) 262 continue; 263 264 const Mangled &mangled = symbol->GetMangled(); 265 entry.cstring = mangled.GetMangledName().GetCString(); 266 if (entry.cstring && entry.cstring[0]) { 267 m_name_to_index.Append(entry); 268 269 if (symbol->ContainsLinkerAnnotations()) { 270 // If the symbol has linker annotations, also add the version without 271 // the 272 // annotations. 273 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations( 274 entry.cstring)) 275 .GetCString(); 276 m_name_to_index.Append(entry); 277 } 278 279 const SymbolType symbol_type = symbol->GetType(); 280 if (symbol_type == eSymbolTypeCode || 281 symbol_type == eSymbolTypeResolver) { 282 if (entry.cstring[0] == '_' && entry.cstring[1] == 'Z' && 283 (entry.cstring[2] != 'T' && // avoid virtual table, VTT structure, 284 // typeinfo structure, and typeinfo 285 // name 286 entry.cstring[2] != 'G' && // avoid guard variables 287 entry.cstring[2] != 'Z')) // named local entities (if we 288 // eventually handle eSymbolTypeData, 289 // we will want this back) 290 { 291 CPlusPlusLanguage::MethodName cxx_method( 292 mangled.GetDemangledName(lldb::eLanguageTypeC_plus_plus)); 293 entry.cstring = ConstString(cxx_method.GetBasename()).GetCString(); 294 if (entry.cstring && entry.cstring[0]) { 295 // ConstString objects permanently store the string in the pool so 296 // calling 297 // GetCString() on the value gets us a const char * that will 298 // never go away 299 const char *const_context = 300 ConstString(cxx_method.GetContext()).GetCString(); 301 302 if (entry.cstring[0] == '~' || 303 !cxx_method.GetQualifiers().empty()) { 304 // The first character of the demangled basename is '~' which 305 // means we have a class destructor. We can use this information 306 // to help us know what is a class and what isn't. 307 if (class_contexts.find(const_context) == class_contexts.end()) 308 class_contexts.insert(const_context); 309 m_method_to_index.Append(entry); 310 } else { 311 if (const_context && const_context[0]) { 312 if (class_contexts.find(const_context) != 313 class_contexts.end()) { 314 // The current decl context is in our "class_contexts" which 315 // means 316 // this is a method on a class 317 m_method_to_index.Append(entry); 318 } else { 319 // We don't know if this is a function basename or a method, 320 // so put it into a temporary collection so once we are done 321 // we can look in class_contexts to see if each entry is a 322 // class 323 // or just a function and will put any remaining items into 324 // m_method_to_index or m_basename_to_index as needed 325 mangled_name_to_index.Append(entry); 326 symbol_contexts[entry.value] = const_context; 327 } 328 } else { 329 // No context for this function so this has to be a basename 330 m_basename_to_index.Append(entry); 331 } 332 } 333 } 334 } 335 } 336 } 337 338 entry.cstring = 339 mangled.GetDemangledName(symbol->GetLanguage()).GetCString(); 340 if (entry.cstring && entry.cstring[0]) { 341 m_name_to_index.Append(entry); 342 343 if (symbol->ContainsLinkerAnnotations()) { 344 // If the symbol has linker annotations, also add the version without 345 // the 346 // annotations. 347 entry.cstring = ConstString(m_objfile->StripLinkerSymbolAnnotations( 348 entry.cstring)) 349 .GetCString(); 350 m_name_to_index.Append(entry); 351 } 352 } 353 354 // If the demangled name turns out to be an ObjC name, and 355 // is a category name, add the version without categories to the index 356 // too. 357 ObjCLanguage::MethodName objc_method(entry.cstring, true); 358 if (objc_method.IsValid(true)) { 359 entry.cstring = objc_method.GetSelector().GetCString(); 360 m_selector_to_index.Append(entry); 361 362 ConstString objc_method_no_category( 363 objc_method.GetFullNameWithoutCategory(true)); 364 if (objc_method_no_category) { 365 entry.cstring = objc_method_no_category.GetCString(); 366 m_name_to_index.Append(entry); 367 } 368 } 369 } 370 371 size_t count; 372 if (!mangled_name_to_index.IsEmpty()) { 373 count = mangled_name_to_index.GetSize(); 374 for (size_t i = 0; i < count; ++i) { 375 if (mangled_name_to_index.GetValueAtIndex(i, entry.value)) { 376 entry.cstring = mangled_name_to_index.GetCStringAtIndex(i); 377 if (symbol_contexts[entry.value] && 378 class_contexts.find(symbol_contexts[entry.value]) != 379 class_contexts.end()) { 380 m_method_to_index.Append(entry); 381 } else { 382 // If we got here, we have something that had a context (was inside 383 // a namespace or class) 384 // yet we don't know if the entry 385 m_method_to_index.Append(entry); 386 m_basename_to_index.Append(entry); 387 } 388 } 389 } 390 } 391 m_name_to_index.Sort(); 392 m_name_to_index.SizeToFit(); 393 m_selector_to_index.Sort(); 394 m_selector_to_index.SizeToFit(); 395 m_basename_to_index.Sort(); 396 m_basename_to_index.SizeToFit(); 397 m_method_to_index.Sort(); 398 m_method_to_index.SizeToFit(); 399 400 // static StreamFile a ("/tmp/a.txt"); 401 // 402 // count = m_basename_to_index.GetSize(); 403 // if (count) 404 // { 405 // for (size_t i=0; i<count; ++i) 406 // { 407 // if (m_basename_to_index.GetValueAtIndex(i, entry.value)) 408 // a.Printf ("%s BASENAME\n", 409 // m_symbols[entry.value].GetMangled().GetName().GetCString()); 410 // } 411 // } 412 // count = m_method_to_index.GetSize(); 413 // if (count) 414 // { 415 // for (size_t i=0; i<count; ++i) 416 // { 417 // if (m_method_to_index.GetValueAtIndex(i, entry.value)) 418 // a.Printf ("%s METHOD\n", 419 // m_symbols[entry.value].GetMangled().GetName().GetCString()); 420 // } 421 // } 422 } 423 } 424 425 void Symtab::AppendSymbolNamesToMap(const IndexCollection &indexes, 426 bool add_demangled, bool add_mangled, 427 NameToIndexMap &name_to_index_map) const { 428 if (add_demangled || add_mangled) { 429 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 430 std::lock_guard<std::recursive_mutex> guard(m_mutex); 431 432 // Create the name index vector to be able to quickly search by name 433 NameToIndexMap::Entry entry; 434 const size_t num_indexes = indexes.size(); 435 for (size_t i = 0; i < num_indexes; ++i) { 436 entry.value = indexes[i]; 437 assert(i < m_symbols.size()); 438 const Symbol *symbol = &m_symbols[entry.value]; 439 440 const Mangled &mangled = symbol->GetMangled(); 441 if (add_demangled) { 442 entry.cstring = 443 mangled.GetDemangledName(symbol->GetLanguage()).GetCString(); 444 if (entry.cstring && entry.cstring[0]) 445 name_to_index_map.Append(entry); 446 } 447 448 if (add_mangled) { 449 entry.cstring = mangled.GetMangledName().GetCString(); 450 if (entry.cstring && entry.cstring[0]) 451 name_to_index_map.Append(entry); 452 } 453 } 454 } 455 } 456 457 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type, 458 std::vector<uint32_t> &indexes, 459 uint32_t start_idx, 460 uint32_t end_index) const { 461 std::lock_guard<std::recursive_mutex> guard(m_mutex); 462 463 uint32_t prev_size = indexes.size(); 464 465 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 466 467 for (uint32_t i = start_idx; i < count; ++i) { 468 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 469 indexes.push_back(i); 470 } 471 472 return indexes.size() - prev_size; 473 } 474 475 uint32_t Symtab::AppendSymbolIndexesWithTypeAndFlagsValue( 476 SymbolType symbol_type, uint32_t flags_value, 477 std::vector<uint32_t> &indexes, uint32_t start_idx, 478 uint32_t end_index) const { 479 std::lock_guard<std::recursive_mutex> guard(m_mutex); 480 481 uint32_t prev_size = indexes.size(); 482 483 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 484 485 for (uint32_t i = start_idx; i < count; ++i) { 486 if ((symbol_type == eSymbolTypeAny || 487 m_symbols[i].GetType() == symbol_type) && 488 m_symbols[i].GetFlags() == flags_value) 489 indexes.push_back(i); 490 } 491 492 return indexes.size() - prev_size; 493 } 494 495 uint32_t Symtab::AppendSymbolIndexesWithType(SymbolType symbol_type, 496 Debug symbol_debug_type, 497 Visibility symbol_visibility, 498 std::vector<uint32_t> &indexes, 499 uint32_t start_idx, 500 uint32_t end_index) const { 501 std::lock_guard<std::recursive_mutex> guard(m_mutex); 502 503 uint32_t prev_size = indexes.size(); 504 505 const uint32_t count = std::min<uint32_t>(m_symbols.size(), end_index); 506 507 for (uint32_t i = start_idx; i < count; ++i) { 508 if (symbol_type == eSymbolTypeAny || 509 m_symbols[i].GetType() == symbol_type) { 510 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility)) 511 indexes.push_back(i); 512 } 513 } 514 515 return indexes.size() - prev_size; 516 } 517 518 uint32_t Symtab::GetIndexForSymbol(const Symbol *symbol) const { 519 if (!m_symbols.empty()) { 520 const Symbol *first_symbol = &m_symbols[0]; 521 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size()) 522 return symbol - first_symbol; 523 } 524 return UINT32_MAX; 525 } 526 527 struct SymbolSortInfo { 528 const bool sort_by_load_addr; 529 const Symbol *symbols; 530 }; 531 532 namespace { 533 struct SymbolIndexComparator { 534 const std::vector<Symbol> &symbols; 535 std::vector<lldb::addr_t> &addr_cache; 536 537 // Getting from the symbol to the Address to the File Address involves some 538 // work. 539 // Since there are potentially many symbols here, and we're using this for 540 // sorting so 541 // we're going to be computing the address many times, cache that in 542 // addr_cache. 543 // The array passed in has to be the same size as the symbols array passed 544 // into the 545 // member variable symbols, and should be initialized with 546 // LLDB_INVALID_ADDRESS. 547 // NOTE: You have to make addr_cache externally and pass it in because 548 // std::stable_sort 549 // makes copies of the comparator it is initially passed in, and you end up 550 // spending 551 // huge amounts of time copying this array... 552 553 SymbolIndexComparator(const std::vector<Symbol> &s, 554 std::vector<lldb::addr_t> &a) 555 : symbols(s), addr_cache(a) { 556 assert(symbols.size() == addr_cache.size()); 557 } 558 bool operator()(uint32_t index_a, uint32_t index_b) { 559 addr_t value_a = addr_cache[index_a]; 560 if (value_a == LLDB_INVALID_ADDRESS) { 561 value_a = symbols[index_a].GetAddressRef().GetFileAddress(); 562 addr_cache[index_a] = value_a; 563 } 564 565 addr_t value_b = addr_cache[index_b]; 566 if (value_b == LLDB_INVALID_ADDRESS) { 567 value_b = symbols[index_b].GetAddressRef().GetFileAddress(); 568 addr_cache[index_b] = value_b; 569 } 570 571 if (value_a == value_b) { 572 // The if the values are equal, use the original symbol user ID 573 lldb::user_id_t uid_a = symbols[index_a].GetID(); 574 lldb::user_id_t uid_b = symbols[index_b].GetID(); 575 if (uid_a < uid_b) 576 return true; 577 if (uid_a > uid_b) 578 return false; 579 return false; 580 } else if (value_a < value_b) 581 return true; 582 583 return false; 584 } 585 }; 586 } 587 588 void Symtab::SortSymbolIndexesByValue(std::vector<uint32_t> &indexes, 589 bool remove_duplicates) const { 590 std::lock_guard<std::recursive_mutex> guard(m_mutex); 591 592 Timer scoped_timer(LLVM_PRETTY_FUNCTION, LLVM_PRETTY_FUNCTION); 593 // No need to sort if we have zero or one items... 594 if (indexes.size() <= 1) 595 return; 596 597 // Sort the indexes in place using std::stable_sort. 598 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for 599 // performance, 600 // not correctness. The indexes vector tends to be "close" to sorted, which 601 // the 602 // stable sort handles better. 603 604 std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS); 605 606 SymbolIndexComparator comparator(m_symbols, addr_cache); 607 std::stable_sort(indexes.begin(), indexes.end(), comparator); 608 609 // Remove any duplicates if requested 610 if (remove_duplicates) 611 std::unique(indexes.begin(), indexes.end()); 612 } 613 614 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name, 615 std::vector<uint32_t> &indexes) { 616 std::lock_guard<std::recursive_mutex> guard(m_mutex); 617 618 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 619 if (symbol_name) { 620 const char *symbol_cstr = symbol_name.GetCString(); 621 if (!m_name_indexes_computed) 622 InitNameIndexes(); 623 624 return m_name_to_index.GetValues(symbol_cstr, indexes); 625 } 626 return 0; 627 } 628 629 uint32_t Symtab::AppendSymbolIndexesWithName(const ConstString &symbol_name, 630 Debug symbol_debug_type, 631 Visibility symbol_visibility, 632 std::vector<uint32_t> &indexes) { 633 std::lock_guard<std::recursive_mutex> guard(m_mutex); 634 635 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 636 if (symbol_name) { 637 const size_t old_size = indexes.size(); 638 if (!m_name_indexes_computed) 639 InitNameIndexes(); 640 641 const char *symbol_cstr = symbol_name.GetCString(); 642 643 std::vector<uint32_t> all_name_indexes; 644 const size_t name_match_count = 645 m_name_to_index.GetValues(symbol_cstr, all_name_indexes); 646 for (size_t i = 0; i < name_match_count; ++i) { 647 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, 648 symbol_visibility)) 649 indexes.push_back(all_name_indexes[i]); 650 } 651 return indexes.size() - old_size; 652 } 653 return 0; 654 } 655 656 uint32_t 657 Symtab::AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name, 658 SymbolType symbol_type, 659 std::vector<uint32_t> &indexes) { 660 std::lock_guard<std::recursive_mutex> guard(m_mutex); 661 662 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) { 663 std::vector<uint32_t>::iterator pos = indexes.begin(); 664 while (pos != indexes.end()) { 665 if (symbol_type == eSymbolTypeAny || 666 m_symbols[*pos].GetType() == symbol_type) 667 ++pos; 668 else 669 pos = indexes.erase(pos); 670 } 671 } 672 return indexes.size(); 673 } 674 675 uint32_t Symtab::AppendSymbolIndexesWithNameAndType( 676 const ConstString &symbol_name, SymbolType symbol_type, 677 Debug symbol_debug_type, Visibility symbol_visibility, 678 std::vector<uint32_t> &indexes) { 679 std::lock_guard<std::recursive_mutex> guard(m_mutex); 680 681 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, 682 symbol_visibility, indexes) > 0) { 683 std::vector<uint32_t>::iterator pos = indexes.begin(); 684 while (pos != indexes.end()) { 685 if (symbol_type == eSymbolTypeAny || 686 m_symbols[*pos].GetType() == symbol_type) 687 ++pos; 688 else 689 pos = indexes.erase(pos); 690 } 691 } 692 return indexes.size(); 693 } 694 695 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType( 696 const RegularExpression ®exp, SymbolType symbol_type, 697 std::vector<uint32_t> &indexes) { 698 std::lock_guard<std::recursive_mutex> guard(m_mutex); 699 700 uint32_t prev_size = indexes.size(); 701 uint32_t sym_end = m_symbols.size(); 702 703 for (uint32_t i = 0; i < sym_end; i++) { 704 if (symbol_type == eSymbolTypeAny || 705 m_symbols[i].GetType() == symbol_type) { 706 const char *name = m_symbols[i].GetName().AsCString(); 707 if (name) { 708 if (regexp.Execute(name)) 709 indexes.push_back(i); 710 } 711 } 712 } 713 return indexes.size() - prev_size; 714 } 715 716 uint32_t Symtab::AppendSymbolIndexesMatchingRegExAndType( 717 const RegularExpression ®exp, SymbolType symbol_type, 718 Debug symbol_debug_type, Visibility symbol_visibility, 719 std::vector<uint32_t> &indexes) { 720 std::lock_guard<std::recursive_mutex> guard(m_mutex); 721 722 uint32_t prev_size = indexes.size(); 723 uint32_t sym_end = m_symbols.size(); 724 725 for (uint32_t i = 0; i < sym_end; i++) { 726 if (symbol_type == eSymbolTypeAny || 727 m_symbols[i].GetType() == symbol_type) { 728 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false) 729 continue; 730 731 const char *name = m_symbols[i].GetName().AsCString(); 732 if (name) { 733 if (regexp.Execute(name)) 734 indexes.push_back(i); 735 } 736 } 737 } 738 return indexes.size() - prev_size; 739 } 740 741 Symbol *Symtab::FindSymbolWithType(SymbolType symbol_type, 742 Debug symbol_debug_type, 743 Visibility symbol_visibility, 744 uint32_t &start_idx) { 745 std::lock_guard<std::recursive_mutex> guard(m_mutex); 746 747 const size_t count = m_symbols.size(); 748 for (size_t idx = start_idx; idx < count; ++idx) { 749 if (symbol_type == eSymbolTypeAny || 750 m_symbols[idx].GetType() == symbol_type) { 751 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) { 752 start_idx = idx; 753 return &m_symbols[idx]; 754 } 755 } 756 } 757 return nullptr; 758 } 759 760 size_t 761 Symtab::FindAllSymbolsWithNameAndType(const ConstString &name, 762 SymbolType symbol_type, 763 std::vector<uint32_t> &symbol_indexes) { 764 std::lock_guard<std::recursive_mutex> guard(m_mutex); 765 766 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 767 // Initialize all of the lookup by name indexes before converting NAME 768 // to a uniqued string NAME_STR below. 769 if (!m_name_indexes_computed) 770 InitNameIndexes(); 771 772 if (name) { 773 // The string table did have a string that matched, but we need 774 // to check the symbols and match the symbol_type if any was given. 775 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_indexes); 776 } 777 return symbol_indexes.size(); 778 } 779 780 size_t Symtab::FindAllSymbolsWithNameAndType( 781 const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, 782 Visibility symbol_visibility, std::vector<uint32_t> &symbol_indexes) { 783 std::lock_guard<std::recursive_mutex> guard(m_mutex); 784 785 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 786 // Initialize all of the lookup by name indexes before converting NAME 787 // to a uniqued string NAME_STR below. 788 if (!m_name_indexes_computed) 789 InitNameIndexes(); 790 791 if (name) { 792 // The string table did have a string that matched, but we need 793 // to check the symbols and match the symbol_type if any was given. 794 AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type, 795 symbol_visibility, symbol_indexes); 796 } 797 return symbol_indexes.size(); 798 } 799 800 size_t Symtab::FindAllSymbolsMatchingRexExAndType( 801 const RegularExpression ®ex, SymbolType symbol_type, 802 Debug symbol_debug_type, Visibility symbol_visibility, 803 std::vector<uint32_t> &symbol_indexes) { 804 std::lock_guard<std::recursive_mutex> guard(m_mutex); 805 806 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, 807 symbol_visibility, symbol_indexes); 808 return symbol_indexes.size(); 809 } 810 811 Symbol *Symtab::FindFirstSymbolWithNameAndType(const ConstString &name, 812 SymbolType symbol_type, 813 Debug symbol_debug_type, 814 Visibility symbol_visibility) { 815 std::lock_guard<std::recursive_mutex> guard(m_mutex); 816 817 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "%s", LLVM_PRETTY_FUNCTION); 818 if (!m_name_indexes_computed) 819 InitNameIndexes(); 820 821 if (name) { 822 std::vector<uint32_t> matching_indexes; 823 // The string table did have a string that matched, but we need 824 // to check the symbols and match the symbol_type if any was given. 825 if (AppendSymbolIndexesWithNameAndType(name, symbol_type, symbol_debug_type, 826 symbol_visibility, 827 matching_indexes)) { 828 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end(); 829 for (pos = matching_indexes.begin(); pos != end; ++pos) { 830 Symbol *symbol = SymbolAtIndex(*pos); 831 832 if (symbol->Compare(name, symbol_type)) 833 return symbol; 834 } 835 } 836 } 837 return nullptr; 838 } 839 840 typedef struct { 841 const Symtab *symtab; 842 const addr_t file_addr; 843 Symbol *match_symbol; 844 const uint32_t *match_index_ptr; 845 addr_t match_offset; 846 } SymbolSearchInfo; 847 848 // Add all the section file start address & size to the RangeVector, 849 // recusively adding any children sections. 850 static void AddSectionsToRangeMap(SectionList *sectlist, 851 RangeVector<addr_t, addr_t> §ion_ranges) { 852 const int num_sections = sectlist->GetNumSections(0); 853 for (int i = 0; i < num_sections; i++) { 854 SectionSP sect_sp = sectlist->GetSectionAtIndex(i); 855 if (sect_sp) { 856 SectionList &child_sectlist = sect_sp->GetChildren(); 857 858 // If this section has children, add the children to the RangeVector. 859 // Else add this section to the RangeVector. 860 if (child_sectlist.GetNumSections(0) > 0) { 861 AddSectionsToRangeMap(&child_sectlist, section_ranges); 862 } else { 863 size_t size = sect_sp->GetByteSize(); 864 if (size > 0) { 865 addr_t base_addr = sect_sp->GetFileAddress(); 866 RangeVector<addr_t, addr_t>::Entry entry; 867 entry.SetRangeBase(base_addr); 868 entry.SetByteSize(size); 869 section_ranges.Append(entry); 870 } 871 } 872 } 873 } 874 } 875 876 void Symtab::InitAddressIndexes() { 877 // Protected function, no need to lock mutex... 878 if (!m_file_addr_to_index_computed && !m_symbols.empty()) { 879 m_file_addr_to_index_computed = true; 880 881 FileRangeToIndexMap::Entry entry; 882 const_iterator begin = m_symbols.begin(); 883 const_iterator end = m_symbols.end(); 884 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) { 885 if (pos->ValueIsAddress()) { 886 entry.SetRangeBase(pos->GetAddressRef().GetFileAddress()); 887 entry.SetByteSize(pos->GetByteSize()); 888 entry.data = std::distance(begin, pos); 889 m_file_addr_to_index.Append(entry); 890 } 891 } 892 const size_t num_entries = m_file_addr_to_index.GetSize(); 893 if (num_entries > 0) { 894 m_file_addr_to_index.Sort(); 895 896 // Create a RangeVector with the start & size of all the sections for 897 // this objfile. We'll need to check this for any FileRangeToIndexMap 898 // entries with an uninitialized size, which could potentially be a 899 // large number so reconstituting the weak pointer is busywork when it 900 // is invariant information. 901 SectionList *sectlist = m_objfile->GetSectionList(); 902 RangeVector<addr_t, addr_t> section_ranges; 903 if (sectlist) { 904 AddSectionsToRangeMap(sectlist, section_ranges); 905 section_ranges.Sort(); 906 } 907 908 // Iterate through the FileRangeToIndexMap and fill in the size for any 909 // entries that didn't already have a size from the Symbol (e.g. if we 910 // have a plain linker symbol with an address only, instead of debug info 911 // where we get an address and a size and a type, etc.) 912 for (size_t i = 0; i < num_entries; i++) { 913 FileRangeToIndexMap::Entry *entry = 914 m_file_addr_to_index.GetMutableEntryAtIndex(i); 915 if (entry->GetByteSize() == 0) { 916 addr_t curr_base_addr = entry->GetRangeBase(); 917 const RangeVector<addr_t, addr_t>::Entry *containing_section = 918 section_ranges.FindEntryThatContains(curr_base_addr); 919 920 // Use the end of the section as the default max size of the symbol 921 addr_t sym_size = 0; 922 if (containing_section) { 923 sym_size = 924 containing_section->GetByteSize() - 925 (entry->GetRangeBase() - containing_section->GetRangeBase()); 926 } 927 928 for (size_t j = i; j < num_entries; j++) { 929 FileRangeToIndexMap::Entry *next_entry = 930 m_file_addr_to_index.GetMutableEntryAtIndex(j); 931 addr_t next_base_addr = next_entry->GetRangeBase(); 932 if (next_base_addr > curr_base_addr) { 933 addr_t size_to_next_symbol = next_base_addr - curr_base_addr; 934 935 // Take the difference between this symbol and the next one as its 936 // size, 937 // if it is less than the size of the section. 938 if (sym_size == 0 || size_to_next_symbol < sym_size) { 939 sym_size = size_to_next_symbol; 940 } 941 break; 942 } 943 } 944 945 if (sym_size > 0) { 946 entry->SetByteSize(sym_size); 947 Symbol &symbol = m_symbols[entry->data]; 948 symbol.SetByteSize(sym_size); 949 symbol.SetSizeIsSynthesized(true); 950 } 951 } 952 } 953 954 // Sort again in case the range size changes the ordering 955 m_file_addr_to_index.Sort(); 956 } 957 } 958 } 959 960 void Symtab::CalculateSymbolSizes() { 961 std::lock_guard<std::recursive_mutex> guard(m_mutex); 962 963 if (!m_symbols.empty()) { 964 if (!m_file_addr_to_index_computed) 965 InitAddressIndexes(); 966 967 const size_t num_entries = m_file_addr_to_index.GetSize(); 968 969 for (size_t i = 0; i < num_entries; ++i) { 970 // The entries in the m_file_addr_to_index have calculated the sizes 971 // already 972 // so we will use this size if we need to. 973 const FileRangeToIndexMap::Entry &entry = 974 m_file_addr_to_index.GetEntryRef(i); 975 976 Symbol &symbol = m_symbols[entry.data]; 977 978 // If the symbol size is already valid, no need to do anything 979 if (symbol.GetByteSizeIsValid()) 980 continue; 981 982 const addr_t range_size = entry.GetByteSize(); 983 if (range_size > 0) { 984 symbol.SetByteSize(range_size); 985 symbol.SetSizeIsSynthesized(true); 986 } 987 } 988 } 989 } 990 991 Symbol *Symtab::FindSymbolAtFileAddress(addr_t file_addr) { 992 std::lock_guard<std::recursive_mutex> guard(m_mutex); 993 if (!m_file_addr_to_index_computed) 994 InitAddressIndexes(); 995 996 const FileRangeToIndexMap::Entry *entry = 997 m_file_addr_to_index.FindEntryStartsAt(file_addr); 998 if (entry) { 999 Symbol *symbol = SymbolAtIndex(entry->data); 1000 if (symbol->GetFileAddress() == file_addr) 1001 return symbol; 1002 } 1003 return nullptr; 1004 } 1005 1006 Symbol *Symtab::FindSymbolContainingFileAddress(addr_t file_addr) { 1007 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1008 1009 if (!m_file_addr_to_index_computed) 1010 InitAddressIndexes(); 1011 1012 const FileRangeToIndexMap::Entry *entry = 1013 m_file_addr_to_index.FindEntryThatContains(file_addr); 1014 if (entry) { 1015 Symbol *symbol = SymbolAtIndex(entry->data); 1016 if (symbol->ContainsFileAddress(file_addr)) 1017 return symbol; 1018 } 1019 return nullptr; 1020 } 1021 1022 void Symtab::ForEachSymbolContainingFileAddress( 1023 addr_t file_addr, std::function<bool(Symbol *)> const &callback) { 1024 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1025 1026 if (!m_file_addr_to_index_computed) 1027 InitAddressIndexes(); 1028 1029 std::vector<uint32_t> all_addr_indexes; 1030 1031 // Get all symbols with file_addr 1032 const size_t addr_match_count = 1033 m_file_addr_to_index.FindEntryIndexesThatContain(file_addr, 1034 all_addr_indexes); 1035 1036 for (size_t i = 0; i < addr_match_count; ++i) { 1037 Symbol *symbol = SymbolAtIndex(all_addr_indexes[i]); 1038 if (symbol->ContainsFileAddress(file_addr)) { 1039 if (!callback(symbol)) 1040 break; 1041 } 1042 } 1043 } 1044 1045 void Symtab::SymbolIndicesToSymbolContextList( 1046 std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) { 1047 // No need to protect this call using m_mutex all other method calls are 1048 // already thread safe. 1049 1050 const bool merge_symbol_into_function = true; 1051 size_t num_indices = symbol_indexes.size(); 1052 if (num_indices > 0) { 1053 SymbolContext sc; 1054 sc.module_sp = m_objfile->GetModule(); 1055 for (size_t i = 0; i < num_indices; i++) { 1056 sc.symbol = SymbolAtIndex(symbol_indexes[i]); 1057 if (sc.symbol) 1058 sc_list.AppendIfUnique(sc, merge_symbol_into_function); 1059 } 1060 } 1061 } 1062 1063 size_t Symtab::FindFunctionSymbols(const ConstString &name, 1064 uint32_t name_type_mask, 1065 SymbolContextList &sc_list) { 1066 size_t count = 0; 1067 std::vector<uint32_t> symbol_indexes; 1068 1069 const char *name_cstr = name.GetCString(); 1070 1071 // eFunctionNameTypeAuto should be pre-resolved by a call to 1072 // Module::PrepareForFunctionNameLookup() 1073 assert((name_type_mask & eFunctionNameTypeAuto) == 0); 1074 1075 if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) { 1076 std::vector<uint32_t> temp_symbol_indexes; 1077 FindAllSymbolsWithNameAndType(name, eSymbolTypeAny, temp_symbol_indexes); 1078 1079 unsigned temp_symbol_indexes_size = temp_symbol_indexes.size(); 1080 if (temp_symbol_indexes_size > 0) { 1081 std::lock_guard<std::recursive_mutex> guard(m_mutex); 1082 for (unsigned i = 0; i < temp_symbol_indexes_size; i++) { 1083 SymbolContext sym_ctx; 1084 sym_ctx.symbol = SymbolAtIndex(temp_symbol_indexes[i]); 1085 if (sym_ctx.symbol) { 1086 switch (sym_ctx.symbol->GetType()) { 1087 case eSymbolTypeCode: 1088 case eSymbolTypeResolver: 1089 case eSymbolTypeReExported: 1090 symbol_indexes.push_back(temp_symbol_indexes[i]); 1091 break; 1092 default: 1093 break; 1094 } 1095 } 1096 } 1097 } 1098 } 1099 1100 if (name_type_mask & eFunctionNameTypeBase) { 1101 // From mangled names we can't tell what is a basename and what 1102 // is a method name, so we just treat them the same 1103 if (!m_name_indexes_computed) 1104 InitNameIndexes(); 1105 1106 if (!m_basename_to_index.IsEmpty()) { 1107 const UniqueCStringMap<uint32_t>::Entry *match; 1108 for (match = m_basename_to_index.FindFirstValueForName(name_cstr); 1109 match != nullptr; 1110 match = m_basename_to_index.FindNextValueForName(match)) { 1111 symbol_indexes.push_back(match->value); 1112 } 1113 } 1114 } 1115 1116 if (name_type_mask & eFunctionNameTypeMethod) { 1117 if (!m_name_indexes_computed) 1118 InitNameIndexes(); 1119 1120 if (!m_method_to_index.IsEmpty()) { 1121 const UniqueCStringMap<uint32_t>::Entry *match; 1122 for (match = m_method_to_index.FindFirstValueForName(name_cstr); 1123 match != nullptr; 1124 match = m_method_to_index.FindNextValueForName(match)) { 1125 symbol_indexes.push_back(match->value); 1126 } 1127 } 1128 } 1129 1130 if (name_type_mask & eFunctionNameTypeSelector) { 1131 if (!m_name_indexes_computed) 1132 InitNameIndexes(); 1133 1134 if (!m_selector_to_index.IsEmpty()) { 1135 const UniqueCStringMap<uint32_t>::Entry *match; 1136 for (match = m_selector_to_index.FindFirstValueForName(name_cstr); 1137 match != nullptr; 1138 match = m_selector_to_index.FindNextValueForName(match)) { 1139 symbol_indexes.push_back(match->value); 1140 } 1141 } 1142 } 1143 1144 if (!symbol_indexes.empty()) { 1145 std::sort(symbol_indexes.begin(), symbol_indexes.end()); 1146 symbol_indexes.erase( 1147 std::unique(symbol_indexes.begin(), symbol_indexes.end()), 1148 symbol_indexes.end()); 1149 count = symbol_indexes.size(); 1150 SymbolIndicesToSymbolContextList(symbol_indexes, sc_list); 1151 } 1152 1153 return count; 1154 } 1155 1156 const Symbol *Symtab::GetParent(Symbol *child_symbol) const { 1157 uint32_t child_idx = GetIndexForSymbol(child_symbol); 1158 if (child_idx != UINT32_MAX && child_idx > 0) { 1159 for (uint32_t idx = child_idx - 1; idx != UINT32_MAX; --idx) { 1160 const Symbol *symbol = SymbolAtIndex(idx); 1161 const uint32_t sibling_idx = symbol->GetSiblingIndex(); 1162 if (sibling_idx != UINT32_MAX && sibling_idx > child_idx) 1163 return symbol; 1164 } 1165 } 1166 return NULL; 1167 } 1168