ModuleList.cpp revision 309124
1//===-- ModuleList.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 "lldb/Core/ModuleList.h" 11 12// C Includes 13// C++ Includes 14#include <cstdint> 15#include <mutex> 16 17// Other libraries and framework includes 18// Project includes 19#include "lldb/Core/Log.h" 20#include "lldb/Core/Module.h" 21#include "lldb/Core/ModuleSpec.h" 22#include "lldb/Host/Host.h" 23#include "lldb/Host/Symbols.h" 24#include "lldb/Symbol/ObjectFile.h" 25#include "lldb/Symbol/SymbolFile.h" 26#include "lldb/Symbol/VariableList.h" 27 28using namespace lldb; 29using namespace lldb_private; 30 31ModuleList::ModuleList() : m_modules(), m_modules_mutex(), m_notifier(nullptr) 32{ 33} 34 35ModuleList::ModuleList(const ModuleList &rhs) : m_modules(), m_modules_mutex(), m_notifier(nullptr) 36{ 37 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex); 38 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex); 39 m_modules = rhs.m_modules; 40} 41 42ModuleList::ModuleList(ModuleList::Notifier *notifier) : m_modules(), m_modules_mutex(), m_notifier(notifier) 43{ 44} 45 46const ModuleList& 47ModuleList::operator= (const ModuleList& rhs) 48{ 49 if (this != &rhs) 50 { 51 // That's probably me nit-picking, but in theoretical situation: 52 // 53 // * that two threads A B and 54 // * two ModuleList's x y do opposite assignments ie.: 55 // 56 // in thread A: | in thread B: 57 // x = y; | y = x; 58 // 59 // This establishes correct(same) lock taking order and thus 60 // avoids priority inversion. 61 if (uintptr_t(this) > uintptr_t(&rhs)) 62 { 63 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex); 64 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex); 65 m_modules = rhs.m_modules; 66 } 67 else 68 { 69 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex); 70 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex); 71 m_modules = rhs.m_modules; 72 } 73 } 74 return *this; 75} 76 77ModuleList::~ModuleList() = default; 78 79void 80ModuleList::AppendImpl (const ModuleSP &module_sp, bool use_notifier) 81{ 82 if (module_sp) 83 { 84 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 85 m_modules.push_back(module_sp); 86 if (use_notifier && m_notifier) 87 m_notifier->ModuleAdded(*this, module_sp); 88 } 89} 90 91void 92ModuleList::Append (const ModuleSP &module_sp) 93{ 94 AppendImpl (module_sp); 95} 96 97void 98ModuleList::ReplaceEquivalent (const ModuleSP &module_sp) 99{ 100 if (module_sp) 101 { 102 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 103 104 // First remove any equivalent modules. Equivalent modules are modules 105 // whose path, platform path and architecture match. 106 ModuleSpec equivalent_module_spec (module_sp->GetFileSpec(), module_sp->GetArchitecture()); 107 equivalent_module_spec.GetPlatformFileSpec() = module_sp->GetPlatformFileSpec(); 108 109 size_t idx = 0; 110 while (idx < m_modules.size()) 111 { 112 ModuleSP module_sp (m_modules[idx]); 113 if (module_sp->MatchesModuleSpec (equivalent_module_spec)) 114 RemoveImpl(m_modules.begin() + idx); 115 else 116 ++idx; 117 } 118 // Now add the new module to the list 119 Append(module_sp); 120 } 121} 122 123bool 124ModuleList::AppendIfNeeded (const ModuleSP &module_sp) 125{ 126 if (module_sp) 127 { 128 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 129 collection::iterator pos, end = m_modules.end(); 130 for (pos = m_modules.begin(); pos != end; ++pos) 131 { 132 if (pos->get() == module_sp.get()) 133 return false; // Already in the list 134 } 135 // Only push module_sp on the list if it wasn't already in there. 136 Append(module_sp); 137 return true; 138 } 139 return false; 140} 141 142void 143ModuleList::Append (const ModuleList& module_list) 144{ 145 for (auto pos : module_list.m_modules) 146 Append(pos); 147} 148 149bool 150ModuleList::AppendIfNeeded (const ModuleList& module_list) 151{ 152 bool any_in = false; 153 for (auto pos : module_list.m_modules) 154 { 155 if (AppendIfNeeded(pos)) 156 any_in = true; 157 } 158 return any_in; 159} 160 161bool 162ModuleList::RemoveImpl (const ModuleSP &module_sp, bool use_notifier) 163{ 164 if (module_sp) 165 { 166 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 167 collection::iterator pos, end = m_modules.end(); 168 for (pos = m_modules.begin(); pos != end; ++pos) 169 { 170 if (pos->get() == module_sp.get()) 171 { 172 m_modules.erase (pos); 173 if (use_notifier && m_notifier) 174 m_notifier->ModuleRemoved(*this, module_sp); 175 return true; 176 } 177 } 178 } 179 return false; 180} 181 182ModuleList::collection::iterator 183ModuleList::RemoveImpl (ModuleList::collection::iterator pos, bool use_notifier) 184{ 185 ModuleSP module_sp(*pos); 186 collection::iterator retval = m_modules.erase(pos); 187 if (use_notifier && m_notifier) 188 m_notifier->ModuleRemoved(*this, module_sp); 189 return retval; 190} 191 192bool 193ModuleList::Remove (const ModuleSP &module_sp) 194{ 195 return RemoveImpl (module_sp); 196} 197 198bool 199ModuleList::ReplaceModule (const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp) 200{ 201 if (!RemoveImpl(old_module_sp, false)) 202 return false; 203 AppendImpl (new_module_sp, false); 204 if (m_notifier) 205 m_notifier->ModuleUpdated(*this, old_module_sp,new_module_sp); 206 return true; 207} 208 209bool 210ModuleList::RemoveIfOrphaned (const Module *module_ptr) 211{ 212 if (module_ptr) 213 { 214 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 215 collection::iterator pos, end = m_modules.end(); 216 for (pos = m_modules.begin(); pos != end; ++pos) 217 { 218 if (pos->get() == module_ptr) 219 { 220 if (pos->unique()) 221 { 222 pos = RemoveImpl(pos); 223 return true; 224 } 225 else 226 return false; 227 } 228 } 229 } 230 return false; 231} 232 233size_t 234ModuleList::RemoveOrphans (bool mandatory) 235{ 236 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock); 237 238 if (mandatory) 239 { 240 lock.lock(); 241 } 242 else 243 { 244 // Not mandatory, remove orphans if we can get the mutex 245 if (!lock.try_lock()) 246 return 0; 247 } 248 collection::iterator pos = m_modules.begin(); 249 size_t remove_count = 0; 250 while (pos != m_modules.end()) 251 { 252 if (pos->unique()) 253 { 254 pos = RemoveImpl(pos); 255 ++remove_count; 256 } 257 else 258 { 259 ++pos; 260 } 261 } 262 return remove_count; 263} 264 265size_t 266ModuleList::Remove (ModuleList &module_list) 267{ 268 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 269 size_t num_removed = 0; 270 collection::iterator pos, end = module_list.m_modules.end(); 271 for (pos = module_list.m_modules.begin(); pos != end; ++pos) 272 { 273 if (Remove (*pos)) 274 ++num_removed; 275 } 276 return num_removed; 277} 278 279 280void 281ModuleList::Clear() 282{ 283 ClearImpl(); 284} 285 286void 287ModuleList::Destroy() 288{ 289 ClearImpl(); 290} 291 292void 293ModuleList::ClearImpl (bool use_notifier) 294{ 295 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 296 if (use_notifier && m_notifier) 297 m_notifier->WillClearList(*this); 298 m_modules.clear(); 299} 300 301Module* 302ModuleList::GetModulePointerAtIndex (size_t idx) const 303{ 304 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 305 return GetModulePointerAtIndexUnlocked(idx); 306} 307 308Module* 309ModuleList::GetModulePointerAtIndexUnlocked (size_t idx) const 310{ 311 if (idx < m_modules.size()) 312 return m_modules[idx].get(); 313 return nullptr; 314} 315 316ModuleSP 317ModuleList::GetModuleAtIndex(size_t idx) const 318{ 319 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 320 return GetModuleAtIndexUnlocked(idx); 321} 322 323ModuleSP 324ModuleList::GetModuleAtIndexUnlocked(size_t idx) const 325{ 326 ModuleSP module_sp; 327 if (idx < m_modules.size()) 328 module_sp = m_modules[idx]; 329 return module_sp; 330} 331 332size_t 333ModuleList::FindFunctions (const ConstString &name, 334 uint32_t name_type_mask, 335 bool include_symbols, 336 bool include_inlines, 337 bool append, 338 SymbolContextList &sc_list) const 339{ 340 if (!append) 341 sc_list.Clear(); 342 343 const size_t old_size = sc_list.GetSize(); 344 345 if (name_type_mask & eFunctionNameTypeAuto) 346 { 347 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 348 349 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 350 collection::const_iterator pos, end = m_modules.end(); 351 for (pos = m_modules.begin(); pos != end; ++pos) 352 { 353 (*pos)->FindFunctions(lookup_info.GetLookupName(), 354 nullptr, 355 lookup_info.GetNameTypeMask(), 356 include_symbols, 357 include_inlines, 358 true, 359 sc_list); 360 } 361 362 const size_t new_size = sc_list.GetSize(); 363 364 if (old_size < new_size) 365 lookup_info.Prune (sc_list, old_size); 366 } 367 else 368 { 369 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 370 collection::const_iterator pos, end = m_modules.end(); 371 for (pos = m_modules.begin(); pos != end; ++pos) 372 { 373 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols, include_inlines, true, sc_list); 374 } 375 } 376 return sc_list.GetSize() - old_size; 377} 378 379size_t 380ModuleList::FindFunctionSymbols (const ConstString &name, 381 uint32_t name_type_mask, 382 SymbolContextList& sc_list) 383{ 384 const size_t old_size = sc_list.GetSize(); 385 386 if (name_type_mask & eFunctionNameTypeAuto) 387 { 388 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 389 390 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 391 collection::const_iterator pos, end = m_modules.end(); 392 for (pos = m_modules.begin(); pos != end; ++pos) 393 { 394 (*pos)->FindFunctionSymbols (lookup_info.GetLookupName(), 395 lookup_info.GetNameTypeMask(), 396 sc_list); 397 } 398 399 400 const size_t new_size = sc_list.GetSize(); 401 402 if (old_size < new_size) 403 lookup_info.Prune (sc_list, old_size); 404 } 405 else 406 { 407 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 408 collection::const_iterator pos, end = m_modules.end(); 409 for (pos = m_modules.begin(); pos != end; ++pos) 410 { 411 (*pos)->FindFunctionSymbols (name, name_type_mask, sc_list); 412 } 413 } 414 415 return sc_list.GetSize() - old_size; 416} 417 418size_t 419ModuleList::FindFunctions(const RegularExpression &name, 420 bool include_symbols, 421 bool include_inlines, 422 bool append, 423 SymbolContextList& sc_list) 424{ 425 const size_t old_size = sc_list.GetSize(); 426 427 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 428 collection::const_iterator pos, end = m_modules.end(); 429 for (pos = m_modules.begin(); pos != end; ++pos) 430 { 431 (*pos)->FindFunctions (name, include_symbols, include_inlines, append, sc_list); 432 } 433 434 return sc_list.GetSize() - old_size; 435} 436 437size_t 438ModuleList::FindCompileUnits (const FileSpec &path, 439 bool append, 440 SymbolContextList &sc_list) const 441{ 442 if (!append) 443 sc_list.Clear(); 444 445 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 446 collection::const_iterator pos, end = m_modules.end(); 447 for (pos = m_modules.begin(); pos != end; ++pos) 448 { 449 (*pos)->FindCompileUnits (path, true, sc_list); 450 } 451 452 return sc_list.GetSize(); 453} 454 455size_t 456ModuleList::FindGlobalVariables (const ConstString &name, 457 bool append, 458 size_t max_matches, 459 VariableList& variable_list) const 460{ 461 size_t initial_size = variable_list.GetSize(); 462 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 463 collection::const_iterator pos, end = m_modules.end(); 464 for (pos = m_modules.begin(); pos != end; ++pos) 465 { 466 (*pos)->FindGlobalVariables(name, nullptr, append, max_matches, variable_list); 467 } 468 return variable_list.GetSize() - initial_size; 469} 470 471size_t 472ModuleList::FindGlobalVariables (const RegularExpression& regex, 473 bool append, 474 size_t max_matches, 475 VariableList& variable_list) const 476{ 477 size_t initial_size = variable_list.GetSize(); 478 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 479 collection::const_iterator pos, end = m_modules.end(); 480 for (pos = m_modules.begin(); pos != end; ++pos) 481 { 482 (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list); 483 } 484 return variable_list.GetSize() - initial_size; 485} 486 487size_t 488ModuleList::FindSymbolsWithNameAndType (const ConstString &name, 489 SymbolType symbol_type, 490 SymbolContextList &sc_list, 491 bool append) const 492{ 493 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 494 if (!append) 495 sc_list.Clear(); 496 size_t initial_size = sc_list.GetSize(); 497 498 collection::const_iterator pos, end = m_modules.end(); 499 for (pos = m_modules.begin(); pos != end; ++pos) 500 (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list); 501 return sc_list.GetSize() - initial_size; 502} 503 504size_t 505ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, 506 lldb::SymbolType symbol_type, 507 SymbolContextList &sc_list, 508 bool append) const 509{ 510 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 511 if (!append) 512 sc_list.Clear(); 513 size_t initial_size = sc_list.GetSize(); 514 515 collection::const_iterator pos, end = m_modules.end(); 516 for (pos = m_modules.begin(); pos != end; ++pos) 517 (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list); 518 return sc_list.GetSize() - initial_size; 519} 520 521size_t 522ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const 523{ 524 size_t existing_matches = matching_module_list.GetSize(); 525 526 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 527 collection::const_iterator pos, end = m_modules.end(); 528 for (pos = m_modules.begin(); pos != end; ++pos) 529 { 530 ModuleSP module_sp(*pos); 531 if (module_sp->MatchesModuleSpec (module_spec)) 532 matching_module_list.Append(module_sp); 533 } 534 return matching_module_list.GetSize() - existing_matches; 535} 536 537ModuleSP 538ModuleList::FindModule (const Module *module_ptr) const 539{ 540 ModuleSP module_sp; 541 542 // Scope for "locker" 543 { 544 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 545 collection::const_iterator pos, end = m_modules.end(); 546 547 for (pos = m_modules.begin(); pos != end; ++pos) 548 { 549 if ((*pos).get() == module_ptr) 550 { 551 module_sp = (*pos); 552 break; 553 } 554 } 555 } 556 return module_sp; 557} 558 559ModuleSP 560ModuleList::FindModule (const UUID &uuid) const 561{ 562 ModuleSP module_sp; 563 564 if (uuid.IsValid()) 565 { 566 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 567 collection::const_iterator pos, end = m_modules.end(); 568 569 for (pos = m_modules.begin(); pos != end; ++pos) 570 { 571 if ((*pos)->GetUUID() == uuid) 572 { 573 module_sp = (*pos); 574 break; 575 } 576 } 577 } 578 return module_sp; 579} 580 581size_t 582ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, size_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeList& types) const 583{ 584 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 585 586 size_t total_matches = 0; 587 collection::const_iterator pos, end = m_modules.end(); 588 if (sc.module_sp) 589 { 590 // The symbol context "sc" contains a module so we want to search that 591 // one first if it is in our list... 592 for (pos = m_modules.begin(); pos != end; ++pos) 593 { 594 if (sc.module_sp.get() == (*pos).get()) 595 { 596 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, searched_symbol_files, types); 597 598 if (total_matches >= max_matches) 599 break; 600 } 601 } 602 } 603 604 if (total_matches < max_matches) 605 { 606 SymbolContext world_sc; 607 for (pos = m_modules.begin(); pos != end; ++pos) 608 { 609 // Search the module if the module is not equal to the one in the symbol 610 // context "sc". If "sc" contains a empty module shared pointer, then 611 // the comparison will always be true (valid_module_ptr != nullptr). 612 if (sc.module_sp.get() != (*pos).get()) 613 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, searched_symbol_files, types); 614 615 if (total_matches >= max_matches) 616 break; 617 } 618 } 619 620 return total_matches; 621} 622 623bool 624ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 625{ 626 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 627 collection::const_iterator pos, end = m_modules.end(); 628 for (pos = m_modules.begin(); pos != end; ++pos) 629 { 630 if ((*pos)->FindSourceFile (orig_spec, new_spec)) 631 return true; 632 } 633 return false; 634} 635 636void 637ModuleList::FindAddressesForLine (const lldb::TargetSP target_sp, 638 const FileSpec &file, uint32_t line, 639 Function *function, 640 std::vector<Address> &output_local, std::vector<Address> &output_extern) 641{ 642 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 643 collection::const_iterator pos, end = m_modules.end(); 644 for (pos = m_modules.begin(); pos != end; ++pos) 645 { 646 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local, output_extern); 647 } 648} 649 650ModuleSP 651ModuleList::FindFirstModule (const ModuleSpec &module_spec) const 652{ 653 ModuleSP module_sp; 654 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 655 collection::const_iterator pos, end = m_modules.end(); 656 for (pos = m_modules.begin(); pos != end; ++pos) 657 { 658 ModuleSP module_sp(*pos); 659 if (module_sp->MatchesModuleSpec (module_spec)) 660 return module_sp; 661 } 662 return module_sp; 663 664} 665 666size_t 667ModuleList::GetSize() const 668{ 669 size_t size = 0; 670 { 671 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 672 size = m_modules.size(); 673 } 674 return size; 675} 676 677void 678ModuleList::Dump(Stream *s) const 679{ 680 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this); 681 // s.Indent(); 682 // s << "ModuleList\n"; 683 684 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 685 collection::const_iterator pos, end = m_modules.end(); 686 for (pos = m_modules.begin(); pos != end; ++pos) 687 { 688 (*pos)->Dump(s); 689 } 690} 691 692void 693ModuleList::LogUUIDAndPaths (Log *log, const char *prefix_cstr) 694{ 695 if (log != nullptr) 696 { 697 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 698 collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end(); 699 for (pos = begin; pos != end; ++pos) 700 { 701 Module *module = pos->get(); 702 const FileSpec &module_file_spec = module->GetFileSpec(); 703 log->Printf ("%s[%u] %s (%s) \"%s\"", 704 prefix_cstr ? prefix_cstr : "", 705 (uint32_t)std::distance (begin, pos), 706 module->GetUUID().GetAsString().c_str(), 707 module->GetArchitecture().GetArchitectureName(), 708 module_file_spec.GetPath().c_str()); 709 } 710 } 711} 712 713bool 714ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const 715{ 716 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 717 collection::const_iterator pos, end = m_modules.end(); 718 for (pos = m_modules.begin(); pos != end; ++pos) 719 { 720 if ((*pos)->ResolveFileAddress (vm_addr, so_addr)) 721 return true; 722 } 723 724 return false; 725} 726 727uint32_t 728ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const 729{ 730 // The address is already section offset so it has a module 731 uint32_t resolved_flags = 0; 732 ModuleSP module_sp (so_addr.GetModule()); 733 if (module_sp) 734 { 735 resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr, 736 resolve_scope, 737 sc); 738 } 739 else 740 { 741 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 742 collection::const_iterator pos, end = m_modules.end(); 743 for (pos = m_modules.begin(); pos != end; ++pos) 744 { 745 resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr, 746 resolve_scope, 747 sc); 748 if (resolved_flags != 0) 749 break; 750 } 751 } 752 753 return resolved_flags; 754} 755 756uint32_t 757ModuleList::ResolveSymbolContextForFilePath(const char *file_path, 758 uint32_t line, 759 bool check_inlines, 760 uint32_t resolve_scope, 761 SymbolContextList& sc_list) const 762{ 763 FileSpec file_spec(file_path, false); 764 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 765} 766 767uint32_t 768ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const 769{ 770 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 771 collection::const_iterator pos, end = m_modules.end(); 772 for (pos = m_modules.begin(); pos != end; ++pos) 773 { 774 (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 775 } 776 777 return sc_list.GetSize(); 778} 779 780size_t 781ModuleList::GetIndexForModule (const Module *module) const 782{ 783 if (module) 784 { 785 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 786 collection::const_iterator pos; 787 collection::const_iterator begin = m_modules.begin(); 788 collection::const_iterator end = m_modules.end(); 789 for (pos = begin; pos != end; ++pos) 790 { 791 if ((*pos).get() == module) 792 return std::distance (begin, pos); 793 } 794 } 795 return LLDB_INVALID_INDEX32; 796} 797 798static ModuleList & 799GetSharedModuleList () 800{ 801 static ModuleList *g_shared_module_list = nullptr; 802 static std::once_flag g_once_flag; 803 std::call_once(g_once_flag, [](){ 804 // NOTE: Intentionally leak the module list so a program doesn't have to 805 // cleanup all modules and object files as it exits. This just wastes time 806 // doing a bunch of cleanup that isn't required. 807 if (g_shared_module_list == nullptr) 808 g_shared_module_list = new ModuleList(); // <--- Intentional leak!!! 809 }); 810 return *g_shared_module_list; 811} 812 813bool 814ModuleList::ModuleIsInCache (const Module *module_ptr) 815{ 816 if (module_ptr) 817 { 818 ModuleList &shared_module_list = GetSharedModuleList (); 819 return shared_module_list.FindModule(module_ptr).get() != nullptr; 820 } 821 return false; 822} 823 824size_t 825ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list) 826{ 827 return GetSharedModuleList ().FindModules (module_spec, matching_module_list); 828} 829 830size_t 831ModuleList::RemoveOrphanSharedModules (bool mandatory) 832{ 833 return GetSharedModuleList ().RemoveOrphans(mandatory); 834} 835 836Error 837ModuleList::GetSharedModule(const ModuleSpec &module_spec, 838 ModuleSP &module_sp, 839 const FileSpecList *module_search_paths_ptr, 840 ModuleSP *old_module_sp_ptr, 841 bool *did_create_ptr, 842 bool always_create) 843{ 844 ModuleList &shared_module_list = GetSharedModuleList (); 845 std::lock_guard<std::recursive_mutex> guard(shared_module_list.m_modules_mutex); 846 char path[PATH_MAX]; 847 848 Error error; 849 850 module_sp.reset(); 851 852 if (did_create_ptr) 853 *did_create_ptr = false; 854 if (old_module_sp_ptr) 855 old_module_sp_ptr->reset(); 856 857 const UUID *uuid_ptr = module_spec.GetUUIDPtr(); 858 const FileSpec &module_file_spec = module_spec.GetFileSpec(); 859 const ArchSpec &arch = module_spec.GetArchitecture(); 860 861 // Make sure no one else can try and get or create a module while this 862 // function is actively working on it by doing an extra lock on the 863 // global mutex list. 864 if (!always_create) 865 { 866 ModuleList matching_module_list; 867 const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list); 868 if (num_matching_modules > 0) 869 { 870 for (size_t module_idx = 0; module_idx < num_matching_modules; ++module_idx) 871 { 872 module_sp = matching_module_list.GetModuleAtIndex(module_idx); 873 874 // Make sure the file for the module hasn't been modified 875 if (module_sp->FileHasChanged()) 876 { 877 if (old_module_sp_ptr && !*old_module_sp_ptr) 878 *old_module_sp_ptr = module_sp; 879 880 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_MODULES)); 881 if (log != nullptr) 882 log->Printf("module changed: %p, removing from global module list", 883 static_cast<void*>(module_sp.get())); 884 885 shared_module_list.Remove (module_sp); 886 module_sp.reset(); 887 } 888 else 889 { 890 // The module matches and the module was not modified from 891 // when it was last loaded. 892 return error; 893 } 894 } 895 } 896 } 897 898 if (module_sp) 899 return error; 900 901 module_sp.reset (new Module (module_spec)); 902 // Make sure there are a module and an object file since we can specify 903 // a valid file path with an architecture that might not be in that file. 904 // By getting the object file we can guarantee that the architecture matches 905 if (module_sp->GetObjectFile()) 906 { 907 // If we get in here we got the correct arch, now we just need 908 // to verify the UUID if one was given 909 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) 910 { 911 module_sp.reset(); 912 } 913 else 914 { 915 if (module_sp->GetObjectFile() && module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeStubLibrary) 916 { 917 module_sp.reset(); 918 } 919 else 920 { 921 if (did_create_ptr) 922 { 923 *did_create_ptr = true; 924 } 925 926 shared_module_list.ReplaceEquivalent(module_sp); 927 return error; 928 } 929 } 930 } 931 else 932 { 933 module_sp.reset(); 934 } 935 936 if (module_search_paths_ptr) 937 { 938 const auto num_directories = module_search_paths_ptr->GetSize(); 939 for (size_t idx = 0; idx < num_directories; ++idx) 940 { 941 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx); 942 if (!search_path_spec.ResolvePath()) 943 continue; 944 if (!search_path_spec.Exists() || !search_path_spec.IsDirectory()) 945 continue; 946 search_path_spec.AppendPathComponent(module_spec.GetFileSpec().GetFilename().AsCString()); 947 if (!search_path_spec.Exists()) 948 continue; 949 950 auto resolved_module_spec(module_spec); 951 resolved_module_spec.GetFileSpec() = search_path_spec; 952 module_sp.reset (new Module (resolved_module_spec)); 953 if (module_sp->GetObjectFile()) 954 { 955 // If we get in here we got the correct arch, now we just need 956 // to verify the UUID if one was given 957 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) 958 { 959 module_sp.reset(); 960 } 961 else 962 { 963 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeStubLibrary) 964 { 965 module_sp.reset(); 966 } 967 else 968 { 969 if (did_create_ptr) 970 *did_create_ptr = true; 971 972 shared_module_list.ReplaceEquivalent(module_sp); 973 return Error(); 974 } 975 } 976 } 977 else 978 { 979 module_sp.reset(); 980 } 981 } 982 } 983 984 // Either the file didn't exist where at the path, or no path was given, so 985 // we now have to use more extreme measures to try and find the appropriate 986 // module. 987 988 // Fixup the incoming path in case the path points to a valid file, yet 989 // the arch or UUID (if one was passed in) don't match. 990 ModuleSpec located_binary_modulespec = Symbols::LocateExecutableObjectFile (module_spec); 991 992 // Don't look for the file if it appears to be the same one we already 993 // checked for above... 994 if (located_binary_modulespec.GetFileSpec() != module_file_spec) 995 { 996 if (!located_binary_modulespec.GetFileSpec().Exists()) 997 { 998 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path)); 999 if (path[0] == '\0') 1000 module_file_spec.GetPath(path, sizeof(path)); 1001 // How can this check ever be true? This branch it is false, and we haven't modified file_spec. 1002 if (located_binary_modulespec.GetFileSpec().Exists()) 1003 { 1004 std::string uuid_str; 1005 if (uuid_ptr && uuid_ptr->IsValid()) 1006 uuid_str = uuid_ptr->GetAsString(); 1007 1008 if (arch.IsValid()) 1009 { 1010 if (!uuid_str.empty()) 1011 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_str.c_str()); 1012 else 1013 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName()); 1014 } 1015 } 1016 else 1017 { 1018 error.SetErrorStringWithFormat("'%s' does not exist", path); 1019 } 1020 if (error.Fail()) 1021 module_sp.reset(); 1022 return error; 1023 } 1024 1025 // Make sure no one else can try and get or create a module while this 1026 // function is actively working on it by doing an extra lock on the 1027 // global mutex list. 1028 ModuleSpec platform_module_spec(module_spec); 1029 platform_module_spec.GetFileSpec() = located_binary_modulespec.GetFileSpec(); 1030 platform_module_spec.GetPlatformFileSpec() = located_binary_modulespec.GetFileSpec(); 1031 platform_module_spec.GetSymbolFileSpec() = located_binary_modulespec.GetSymbolFileSpec(); 1032 ModuleList matching_module_list; 1033 if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0) 1034 { 1035 module_sp = matching_module_list.GetModuleAtIndex(0); 1036 1037 // If we didn't have a UUID in mind when looking for the object file, 1038 // then we should make sure the modification time hasn't changed! 1039 if (platform_module_spec.GetUUIDPtr() == nullptr) 1040 { 1041 TimeValue file_spec_mod_time(located_binary_modulespec.GetFileSpec().GetModificationTime()); 1042 if (file_spec_mod_time.IsValid()) 1043 { 1044 if (file_spec_mod_time != module_sp->GetModificationTime()) 1045 { 1046 if (old_module_sp_ptr) 1047 *old_module_sp_ptr = module_sp; 1048 shared_module_list.Remove (module_sp); 1049 module_sp.reset(); 1050 } 1051 } 1052 } 1053 } 1054 1055 if (!module_sp) 1056 { 1057 module_sp.reset (new Module (platform_module_spec)); 1058 // Make sure there are a module and an object file since we can specify 1059 // a valid file path with an architecture that might not be in that file. 1060 // By getting the object file we can guarantee that the architecture matches 1061 if (module_sp && module_sp->GetObjectFile()) 1062 { 1063 if (module_sp->GetObjectFile()->GetType() == ObjectFile::eTypeStubLibrary) 1064 { 1065 module_sp.reset(); 1066 } 1067 else 1068 { 1069 if (did_create_ptr) 1070 *did_create_ptr = true; 1071 1072 shared_module_list.ReplaceEquivalent(module_sp); 1073 } 1074 } 1075 else 1076 { 1077 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path)); 1078 1079 if (located_binary_modulespec.GetFileSpec()) 1080 { 1081 if (arch.IsValid()) 1082 error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path); 1083 else 1084 error.SetErrorStringWithFormat("unable to open '%s'", path); 1085 } 1086 else 1087 { 1088 std::string uuid_str; 1089 if (uuid_ptr && uuid_ptr->IsValid()) 1090 uuid_str = uuid_ptr->GetAsString(); 1091 1092 if (!uuid_str.empty()) 1093 error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_str.c_str()); 1094 else 1095 error.SetErrorStringWithFormat("cannot locate a module"); 1096 } 1097 } 1098 } 1099 } 1100 1101 return error; 1102} 1103 1104bool 1105ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp) 1106{ 1107 return GetSharedModuleList ().Remove (module_sp); 1108} 1109 1110bool 1111ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr) 1112{ 1113 return GetSharedModuleList ().RemoveIfOrphaned (module_ptr); 1114} 1115 1116bool 1117ModuleList::LoadScriptingResourcesInTarget (Target *target, 1118 std::list<Error>& errors, 1119 Stream *feedback_stream, 1120 bool continue_on_error) 1121{ 1122 if (!target) 1123 return false; 1124 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 1125 for (auto module : m_modules) 1126 { 1127 Error error; 1128 if (module) 1129 { 1130 if (!module->LoadScriptingResourceInTarget(target, error, feedback_stream)) 1131 { 1132 if (error.Fail() && error.AsCString()) 1133 { 1134 error.SetErrorStringWithFormat("unable to load scripting data for module %s - error reported was %s", 1135 module->GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1136 error.AsCString()); 1137 errors.push_back(error); 1138 1139 if (!continue_on_error) 1140 return false; 1141 } 1142 } 1143 } 1144 } 1145 return errors.empty(); 1146} 1147 1148void 1149ModuleList::ForEach (std::function <bool (const ModuleSP &module_sp)> const &callback) const 1150{ 1151 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 1152 for (const auto &module : m_modules) 1153 { 1154 // If the callback returns false, then stop iterating and break out 1155 if (!callback (module)) 1156 break; 1157 } 1158} 1159