Symtab.cpp revision 16d2187c0c3992f22e9cb011f863dc0fe35e3cde
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 12#include "lldb/Core/Module.h" 13#include "lldb/Core/RegularExpression.h" 14#include "lldb/Core/Timer.h" 15#include "lldb/Symbol/ObjectFile.h" 16#include "lldb/Symbol/Symtab.h" 17#include "lldb/Target/ObjCLanguageRuntime.h" 18 19using namespace lldb; 20using namespace lldb_private; 21 22 23 24Symtab::Symtab(ObjectFile *objfile) : 25 m_objfile (objfile), 26 m_symbols (), 27 m_addr_indexes (), 28 m_name_to_index (), 29 m_mutex (Mutex::eMutexTypeRecursive), 30 m_addr_indexes_computed (false), 31 m_name_indexes_computed (false) 32{ 33} 34 35Symtab::~Symtab() 36{ 37} 38 39void 40Symtab::Reserve(uint32_t count) 41{ 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.reserve (count); 45} 46 47Symbol * 48Symtab::Resize(uint32_t count) 49{ 50 // Clients should grab the mutex from this symbol table and lock it manually 51 // when calling this function to avoid performance issues. 52 m_symbols.resize (count); 53 return &m_symbols[0]; 54} 55 56uint32_t 57Symtab::AddSymbol(const Symbol& symbol) 58{ 59 // Clients should grab the mutex from this symbol table and lock it manually 60 // when calling this function to avoid performance issues. 61 uint32_t symbol_idx = m_symbols.size(); 62 m_name_to_index.Clear(); 63 m_addr_indexes.clear(); 64 m_symbols.push_back(symbol); 65 m_addr_indexes_computed = false; 66 m_name_indexes_computed = false; 67 return symbol_idx; 68} 69 70size_t 71Symtab::GetNumSymbols() const 72{ 73 Mutex::Locker locker (m_mutex); 74 return m_symbols.size(); 75} 76 77void 78Symtab::Dump (Stream *s, Target *target, SortOrder sort_order) 79{ 80 Mutex::Locker locker (m_mutex); 81 82// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 83 s->Indent(); 84 const FileSpec &file_spec = m_objfile->GetFileSpec(); 85 const char * object_name = NULL; 86 if (m_objfile->GetModule()) 87 object_name = m_objfile->GetModule()->GetObjectName().GetCString(); 88 89 if (file_spec) 90 s->Printf("Symtab, file = %s/%s%s%s%s, num_symbols = %lu", 91 file_spec.GetDirectory().AsCString(), 92 file_spec.GetFilename().AsCString(), 93 object_name ? "(" : "", 94 object_name ? object_name : "", 95 object_name ? ")" : "", 96 m_symbols.size()); 97 else 98 s->Printf("Symtab, num_symbols = %lu", m_symbols.size()); 99 100 if (!m_symbols.empty()) 101 { 102 switch (sort_order) 103 { 104 case eSortOrderNone: 105 { 106 s->PutCString (":\n"); 107 DumpSymbolHeader (s); 108 const_iterator begin = m_symbols.begin(); 109 const_iterator end = m_symbols.end(); 110 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) 111 { 112 s->Indent(); 113 pos->Dump(s, target, std::distance(begin, pos)); 114 } 115 } 116 break; 117 118 case eSortOrderByName: 119 { 120 // Although we maintain a lookup by exact name map, the table 121 // isn't sorted by name. So we must make the ordered symbol list 122 // up ourselves. 123 s->PutCString (" (sorted by name):\n"); 124 DumpSymbolHeader (s); 125 typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol; 126 CStringToSymbol name_map; 127 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos) 128 { 129 const char *name = pos->GetMangled().GetName(Mangled::ePreferDemangled).AsCString(); 130 if (name && name[0]) 131 name_map.insert (std::make_pair(name, &(*pos))); 132 } 133 134 for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos) 135 { 136 s->Indent(); 137 pos->second->Dump (s, target, pos->second - &m_symbols[0]); 138 } 139 } 140 break; 141 142 case eSortOrderByAddress: 143 s->PutCString (" (sorted by address):\n"); 144 DumpSymbolHeader (s); 145 if (!m_addr_indexes_computed) 146 InitAddressIndexes(); 147 const size_t num_symbols = GetNumSymbols(); 148 std::vector<uint32_t>::const_iterator pos; 149 std::vector<uint32_t>::const_iterator end = m_addr_indexes.end(); 150 for (pos = m_addr_indexes.begin(); pos != end; ++pos) 151 { 152 uint32_t idx = *pos; 153 if (idx < num_symbols) 154 { 155 s->Indent(); 156 m_symbols[idx].Dump(s, target, idx); 157 } 158 } 159 break; 160 } 161 } 162} 163 164void 165Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const 166{ 167 Mutex::Locker locker (m_mutex); 168 169 const size_t num_symbols = GetNumSymbols(); 170 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 171 s->Indent(); 172 s->Printf("Symtab %lu symbol indexes (%lu symbols total):\n", indexes.size(), m_symbols.size()); 173 s->IndentMore(); 174 175 if (!indexes.empty()) 176 { 177 std::vector<uint32_t>::const_iterator pos; 178 std::vector<uint32_t>::const_iterator end = indexes.end(); 179 DumpSymbolHeader (s); 180 for (pos = indexes.begin(); pos != end; ++pos) 181 { 182 uint32_t idx = *pos; 183 if (idx < num_symbols) 184 { 185 s->Indent(); 186 m_symbols[idx].Dump(s, target, idx); 187 } 188 } 189 } 190 s->IndentLess (); 191} 192 193void 194Symtab::DumpSymbolHeader (Stream *s) 195{ 196 s->Indent(" Debug symbol\n"); 197 s->Indent(" |Synthetic symbol\n"); 198 s->Indent(" ||Externally Visible\n"); 199 s->Indent(" |||\n"); 200 s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n"); 201 s->Indent("------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------\n"); 202} 203 204 205static int 206CompareSymbolID (const void *key, const void *p) 207{ 208 const user_id_t match_uid = *(user_id_t*) key; 209 const user_id_t symbol_uid = ((Symbol *)p)->GetID(); 210 if (match_uid < symbol_uid) 211 return -1; 212 if (match_uid > symbol_uid) 213 return 1; 214 return 0; 215} 216 217Symbol * 218Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const 219{ 220 Mutex::Locker locker (m_mutex); 221 222 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid, 223 &m_symbols[0], 224 m_symbols.size(), 225 (uint8_t *)&m_symbols[1] - (uint8_t *)&m_symbols[0], 226 CompareSymbolID); 227 return symbol; 228} 229 230 231Symbol * 232Symtab::SymbolAtIndex(uint32_t idx) 233{ 234 // Clients should grab the mutex from this symbol table and lock it manually 235 // when calling this function to avoid performance issues. 236 if (idx < m_symbols.size()) 237 return &m_symbols[idx]; 238 return NULL; 239} 240 241 242const Symbol * 243Symtab::SymbolAtIndex(uint32_t idx) const 244{ 245 // Clients should grab the mutex from this symbol table and lock it manually 246 // when calling this function to avoid performance issues. 247 if (idx < m_symbols.size()) 248 return &m_symbols[idx]; 249 return NULL; 250} 251 252//---------------------------------------------------------------------- 253// InitNameIndexes 254//---------------------------------------------------------------------- 255void 256Symtab::InitNameIndexes() 257{ 258 // Protected function, no need to lock mutex... 259 if (!m_name_indexes_computed) 260 { 261 m_name_indexes_computed = true; 262 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 263 // Create the name index vector to be able to quickly search by name 264 const size_t count = m_symbols.size(); 265#if 1 266 m_name_to_index.Reserve (count); 267#else 268 // TODO: benchmark this to see if we save any memory. Otherwise we 269 // will always keep the memory reserved in the vector unless we pull 270 // some STL swap magic and then recopy... 271 uint32_t actual_count = 0; 272 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 273 pos != end; 274 ++pos) 275 { 276 const Mangled &mangled = pos->GetMangled(); 277 if (mangled.GetMangledName()) 278 ++actual_count; 279 280 if (mangled.GetDemangledName()) 281 ++actual_count; 282 } 283 284 m_name_to_index.Reserve (actual_count); 285#endif 286 287 NameToIndexMap::Entry entry; 288 289 for (entry.value = 0; entry.value < count; ++entry.value) 290 { 291 const Symbol *symbol = &m_symbols[entry.value]; 292 293 // Don't let trampolines get into the lookup by name map 294 // If we ever need the trampoline symbols to be searchable by name 295 // we can remove this and then possibly add a new bool to any of the 296 // Symtab functions that lookup symbols by name to indicate if they 297 // want trampolines. 298 if (symbol->IsTrampoline()) 299 continue; 300 301 const Mangled &mangled = symbol->GetMangled(); 302 entry.cstring = mangled.GetMangledName().GetCString(); 303 if (entry.cstring && entry.cstring[0]) 304 m_name_to_index.Append (entry); 305 306 entry.cstring = mangled.GetDemangledName().GetCString(); 307 if (entry.cstring && entry.cstring[0]) 308 m_name_to_index.Append (entry); 309 310 // If the demangled name turns out to be an ObjC name, and 311 // is a category name, add the version without categories to the index too. 312 ConstString objc_base_name; 313 if (ObjCLanguageRuntime::ParseMethodName (entry.cstring, 314 NULL, 315 NULL, 316 &objc_base_name) 317 && !objc_base_name.IsEmpty()) 318 { 319 entry.cstring = objc_base_name.GetCString(); 320 m_name_to_index.Append (entry); 321 } 322 323 } 324 m_name_to_index.Sort(); 325 } 326} 327 328void 329Symtab::AppendSymbolNamesToMap (const IndexCollection &indexes, 330 bool add_demangled, 331 bool add_mangled, 332 NameToIndexMap &name_to_index_map) const 333{ 334 if (add_demangled || add_mangled) 335 { 336 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 337 Mutex::Locker locker (m_mutex); 338 339 // Create the name index vector to be able to quickly search by name 340 NameToIndexMap::Entry entry; 341 const size_t num_indexes = indexes.size(); 342 for (size_t i=0; i<num_indexes; ++i) 343 { 344 entry.value = indexes[i]; 345 assert (i < m_symbols.size()); 346 const Symbol *symbol = &m_symbols[entry.value]; 347 348 const Mangled &mangled = symbol->GetMangled(); 349 if (add_demangled) 350 { 351 entry.cstring = mangled.GetDemangledName().GetCString(); 352 if (entry.cstring && entry.cstring[0]) 353 name_to_index_map.Append (entry); 354 } 355 356 if (add_mangled) 357 { 358 entry.cstring = mangled.GetMangledName().GetCString(); 359 if (entry.cstring && entry.cstring[0]) 360 name_to_index_map.Append (entry); 361 } 362 } 363 } 364} 365 366uint32_t 367Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const 368{ 369 Mutex::Locker locker (m_mutex); 370 371 uint32_t prev_size = indexes.size(); 372 373 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 374 375 for (uint32_t i = start_idx; i < count; ++i) 376 { 377 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 378 indexes.push_back(i); 379 } 380 381 return indexes.size() - prev_size; 382} 383 384uint32_t 385Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const 386{ 387 Mutex::Locker locker (m_mutex); 388 389 uint32_t prev_size = indexes.size(); 390 391 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 392 393 for (uint32_t i = start_idx; i < count; ++i) 394 { 395 if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value) 396 indexes.push_back(i); 397 } 398 399 return indexes.size() - prev_size; 400} 401 402uint32_t 403Symtab::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 404{ 405 Mutex::Locker locker (m_mutex); 406 407 uint32_t prev_size = indexes.size(); 408 409 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 410 411 for (uint32_t i = start_idx; i < count; ++i) 412 { 413 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 414 { 415 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility)) 416 indexes.push_back(i); 417 } 418 } 419 420 return indexes.size() - prev_size; 421} 422 423 424uint32_t 425Symtab::GetIndexForSymbol (const Symbol *symbol) const 426{ 427 const Symbol *first_symbol = &m_symbols[0]; 428 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size()) 429 return symbol - first_symbol; 430 return UINT32_MAX; 431} 432 433struct SymbolSortInfo 434{ 435 const bool sort_by_load_addr; 436 const Symbol *symbols; 437}; 438 439namespace { 440 struct SymbolIndexComparator { 441 const std::vector<Symbol>& symbols; 442 SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { } 443 bool operator()(uint32_t index_a, uint32_t index_b) { 444 addr_t value_a; 445 addr_t value_b; 446 if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection()) { 447 value_a = symbols[index_a].GetValue ().GetOffset(); 448 value_b = symbols[index_b].GetValue ().GetOffset(); 449 } else { 450 value_a = symbols[index_a].GetValue ().GetFileAddress(); 451 value_b = symbols[index_b].GetValue ().GetFileAddress(); 452 } 453 454 if (value_a == value_b) { 455 // The if the values are equal, use the original symbol user ID 456 lldb::user_id_t uid_a = symbols[index_a].GetID(); 457 lldb::user_id_t uid_b = symbols[index_b].GetID(); 458 if (uid_a < uid_b) 459 return true; 460 if (uid_a > uid_b) 461 return false; 462 return false; 463 } else if (value_a < value_b) 464 return true; 465 466 return false; 467 } 468 }; 469} 470 471void 472Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const 473{ 474 Mutex::Locker locker (m_mutex); 475 476 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__); 477 // No need to sort if we have zero or one items... 478 if (indexes.size() <= 1) 479 return; 480 481 // Sort the indexes in place using std::stable_sort. 482 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance, 483 // not correctness. The indexes vector tends to be "close" to sorted, which the 484 // stable sort handles better. 485 std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols)); 486 487 // Remove any duplicates if requested 488 if (remove_duplicates) 489 std::unique(indexes.begin(), indexes.end()); 490} 491 492uint32_t 493Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes) 494{ 495 Mutex::Locker locker (m_mutex); 496 497 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 498 if (symbol_name) 499 { 500 const char *symbol_cstr = symbol_name.GetCString(); 501 if (!m_name_indexes_computed) 502 InitNameIndexes(); 503 504 return m_name_to_index.GetValues (symbol_cstr, indexes); 505 } 506 return 0; 507} 508 509uint32_t 510Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 511{ 512 Mutex::Locker locker (m_mutex); 513 514 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 515 if (symbol_name) 516 { 517 const size_t old_size = indexes.size(); 518 if (!m_name_indexes_computed) 519 InitNameIndexes(); 520 521 const char *symbol_cstr = symbol_name.GetCString(); 522 523 std::vector<uint32_t> all_name_indexes; 524 const size_t name_match_count = m_name_to_index.GetValues (symbol_cstr, all_name_indexes); 525 for (size_t i=0; i<name_match_count; ++i) 526 { 527 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, symbol_visibility)) 528 indexes.push_back (all_name_indexes[i]); 529 } 530 return indexes.size() - old_size; 531 } 532 return 0; 533} 534 535uint32_t 536Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes) 537{ 538 Mutex::Locker locker (m_mutex); 539 540 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) 541 { 542 std::vector<uint32_t>::iterator pos = indexes.begin(); 543 while (pos != indexes.end()) 544 { 545 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) 546 ++pos; 547 else 548 indexes.erase(pos); 549 } 550 } 551 return indexes.size(); 552} 553 554uint32_t 555Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 556{ 557 Mutex::Locker locker (m_mutex); 558 559 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0) 560 { 561 std::vector<uint32_t>::iterator pos = indexes.begin(); 562 while (pos != indexes.end()) 563 { 564 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) 565 ++pos; 566 else 567 indexes.erase(pos); 568 } 569 } 570 return indexes.size(); 571} 572 573 574uint32_t 575Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, std::vector<uint32_t>& indexes) 576{ 577 Mutex::Locker locker (m_mutex); 578 579 uint32_t prev_size = indexes.size(); 580 uint32_t sym_end = m_symbols.size(); 581 582 for (int i = 0; i < sym_end; i++) 583 { 584 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 585 { 586 const char *name = m_symbols[i].GetMangled().GetName().AsCString(); 587 if (name) 588 { 589 if (regexp.Execute (name)) 590 indexes.push_back(i); 591 } 592 } 593 } 594 return indexes.size() - prev_size; 595 596} 597 598uint32_t 599Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 600{ 601 Mutex::Locker locker (m_mutex); 602 603 uint32_t prev_size = indexes.size(); 604 uint32_t sym_end = m_symbols.size(); 605 606 for (int i = 0; i < sym_end; i++) 607 { 608 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 609 { 610 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false) 611 continue; 612 613 const char *name = m_symbols[i].GetMangled().GetName().AsCString(); 614 if (name) 615 { 616 if (regexp.Execute (name)) 617 indexes.push_back(i); 618 } 619 } 620 } 621 return indexes.size() - prev_size; 622 623} 624 625Symbol * 626Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx) 627{ 628 Mutex::Locker locker (m_mutex); 629 630 const size_t count = m_symbols.size(); 631 for (uint32_t idx = start_idx; idx < count; ++idx) 632 { 633 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type) 634 { 635 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) 636 { 637 start_idx = idx; 638 return &m_symbols[idx]; 639 } 640 } 641 } 642 return NULL; 643} 644 645size_t 646Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes) 647{ 648 Mutex::Locker locker (m_mutex); 649 650 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 651 // Initialize all of the lookup by name indexes before converting NAME 652 // to a uniqued string NAME_STR below. 653 if (!m_name_indexes_computed) 654 InitNameIndexes(); 655 656 if (name) 657 { 658 // The string table did have a string that matched, but we need 659 // to check the symbols and match the symbol_type if any was given. 660 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes); 661 } 662 return symbol_indexes.size(); 663} 664 665size_t 666Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) 667{ 668 Mutex::Locker locker (m_mutex); 669 670 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 671 // Initialize all of the lookup by name indexes before converting NAME 672 // to a uniqued string NAME_STR below. 673 if (!m_name_indexes_computed) 674 InitNameIndexes(); 675 676 if (name) 677 { 678 // The string table did have a string that matched, but we need 679 // to check the symbols and match the symbol_type if any was given. 680 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); 681 } 682 return symbol_indexes.size(); 683} 684 685size_t 686Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression ®ex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) 687{ 688 Mutex::Locker locker (m_mutex); 689 690 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); 691 return symbol_indexes.size(); 692} 693 694Symbol * 695Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility) 696{ 697 Mutex::Locker locker (m_mutex); 698 699 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 700 if (!m_name_indexes_computed) 701 InitNameIndexes(); 702 703 if (name) 704 { 705 std::vector<uint32_t> matching_indexes; 706 // The string table did have a string that matched, but we need 707 // to check the symbols and match the symbol_type if any was given. 708 if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes)) 709 { 710 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end(); 711 for (pos = matching_indexes.begin(); pos != end; ++pos) 712 { 713 Symbol *symbol = SymbolAtIndex(*pos); 714 715 if (symbol->Compare(name, symbol_type)) 716 return symbol; 717 } 718 } 719 } 720 return NULL; 721} 722 723typedef struct 724{ 725 const Symtab *symtab; 726 const addr_t file_addr; 727 Symbol *match_symbol; 728 const uint32_t *match_index_ptr; 729 addr_t match_offset; 730} SymbolSearchInfo; 731 732static int 733SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) 734{ 735 const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]); 736 if (curr_symbol == NULL) 737 return -1; 738 739 const addr_t info_file_addr = info->file_addr; 740 741 // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address 742 // range if the symbol has a section! 743 const AddressRange *curr_range = curr_symbol->GetAddressRangePtr(); 744 if (curr_range) 745 { 746 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress(); 747 if (info_file_addr < curr_file_addr) 748 return -1; 749 if (info_file_addr > curr_file_addr) 750 return +1; 751 info->match_symbol = const_cast<Symbol *>(curr_symbol); 752 info->match_index_ptr = index_ptr; 753 return 0; 754 } 755 756 return -1; 757} 758 759static int 760SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) 761{ 762 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]); 763 if (symbol == NULL) 764 return -1; 765 766 const addr_t info_file_addr = info->file_addr; 767 const AddressRange *curr_range = symbol->GetAddressRangePtr(); 768 if (curr_range) 769 { 770 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress(); 771 if (info_file_addr < curr_file_addr) 772 return -1; 773 774 // Since we are finding the closest symbol that is greater than or equal 775 // to 'info->file_addr' we set the symbol here. This will get set 776 // multiple times, but after the search is done it will contain the best 777 // symbol match 778 info->match_symbol = const_cast<Symbol *>(symbol); 779 info->match_index_ptr = index_ptr; 780 info->match_offset = info_file_addr - curr_file_addr; 781 782 if (info_file_addr > curr_file_addr) 783 return +1; 784 return 0; 785 } 786 return -1; 787} 788 789static SymbolSearchInfo 790FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) 791{ 792 SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 }; 793 ::bsearch (&info, 794 indexes, 795 num_indexes, 796 sizeof(uint32_t), 797 (ComparisonFunction)SymbolWithClosestFileAddress); 798 return info; 799} 800 801 802void 803Symtab::InitAddressIndexes() 804{ 805 // Protected function, no need to lock mutex... 806 if (!m_addr_indexes_computed && !m_symbols.empty()) 807 { 808 m_addr_indexes_computed = true; 809#if 0 810 // The old was to add only code, trampoline or data symbols... 811 AppendSymbolIndexesWithType (eSymbolTypeCode, m_addr_indexes); 812 AppendSymbolIndexesWithType (eSymbolTypeTrampoline, m_addr_indexes); 813 AppendSymbolIndexesWithType (eSymbolTypeData, m_addr_indexes); 814#else 815 // The new way adds all symbols with valid addresses that are section 816 // offset. 817 const_iterator begin = m_symbols.begin(); 818 const_iterator end = m_symbols.end(); 819 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) 820 { 821 if (pos->GetAddressRangePtr()) 822 m_addr_indexes.push_back (std::distance(begin, pos)); 823 } 824#endif 825 SortSymbolIndexesByValue (m_addr_indexes, false); 826 m_addr_indexes.push_back (UINT32_MAX); // Terminator for bsearch since we might need to look at the next symbol 827 } 828} 829 830size_t 831Symtab::CalculateSymbolSize (Symbol *symbol) 832{ 833 Mutex::Locker locker (m_mutex); 834 835 if (m_symbols.empty()) 836 return 0; 837 838 // Make sure this symbol is from this symbol table... 839 if (symbol < &m_symbols.front() || symbol > &m_symbols.back()) 840 return 0; 841 842 // See if this symbol already has a byte size? 843 size_t byte_size = symbol->GetByteSize(); 844 845 if (byte_size) 846 { 847 // It does, just return it 848 return byte_size; 849 } 850 851 // Else if this is an address based symbol, figure out the delta between 852 // it and the next address based symbol 853 if (symbol->GetAddressRangePtr()) 854 { 855 if (!m_addr_indexes_computed) 856 InitAddressIndexes(); 857 const size_t num_addr_indexes = m_addr_indexes.size(); 858 SymbolSearchInfo info = FindIndexPtrForSymbolContainingAddress(this, symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(), &m_addr_indexes.front(), num_addr_indexes); 859 if (info.match_index_ptr != NULL) 860 { 861 const lldb::addr_t curr_file_addr = symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(); 862 // We can figure out the address range of all symbols except the 863 // last one by taking the delta between the current symbol and 864 // the next symbol 865 866 for (uint32_t addr_index = info.match_index_ptr - &m_addr_indexes.front() + 1; 867 addr_index < num_addr_indexes; 868 ++addr_index) 869 { 870 Symbol *next_symbol = SymbolAtIndex(m_addr_indexes[addr_index]); 871 if (next_symbol == NULL) 872 break; 873 874 assert (next_symbol->GetAddressRangePtr()); 875 const lldb::addr_t next_file_addr = next_symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(); 876 if (next_file_addr > curr_file_addr) 877 { 878 byte_size = next_file_addr - curr_file_addr; 879 symbol->GetAddressRangePtr()->SetByteSize(byte_size); 880 symbol->SetSizeIsSynthesized(true); 881 break; 882 } 883 } 884 } 885 } 886 return byte_size; 887} 888 889Symbol * 890Symtab::FindSymbolWithFileAddress (addr_t file_addr) 891{ 892 Mutex::Locker locker (m_mutex); 893 894 if (!m_addr_indexes_computed) 895 InitAddressIndexes(); 896 897 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 }; 898 899 uint32_t* match = (uint32_t*)::bsearch (&info, 900 &m_addr_indexes[0], 901 m_addr_indexes.size(), 902 sizeof(uint32_t), 903 (ComparisonFunction)SymbolWithFileAddress); 904 if (match) 905 return SymbolAtIndex (*match); 906 return NULL; 907} 908 909 910Symbol * 911Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) 912{ 913 Mutex::Locker locker (m_mutex); 914 915 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 }; 916 917 ::bsearch (&info, 918 indexes, 919 num_indexes, 920 sizeof(uint32_t), 921 (ComparisonFunction)SymbolWithClosestFileAddress); 922 923 if (info.match_symbol) 924 { 925 if (info.match_offset == 0) 926 { 927 // We found an exact match! 928 return info.match_symbol; 929 } 930 931 const size_t symbol_byte_size = CalculateSymbolSize(info.match_symbol); 932 933 if (symbol_byte_size == 0) 934 { 935 // We weren't able to find the size of the symbol so lets just go 936 // with that match we found in our search... 937 return info.match_symbol; 938 } 939 940 // We were able to figure out a symbol size so lets make sure our 941 // offset puts "file_addr" in the symbol's address range. 942 if (info.match_offset < symbol_byte_size) 943 return info.match_symbol; 944 } 945 return NULL; 946} 947 948Symbol * 949Symtab::FindSymbolContainingFileAddress (addr_t file_addr) 950{ 951 Mutex::Locker locker (m_mutex); 952 953 if (!m_addr_indexes_computed) 954 InitAddressIndexes(); 955 956 return FindSymbolContainingFileAddress (file_addr, &m_addr_indexes[0], m_addr_indexes.size()); 957} 958 959