ModuleList.cpp revision 263363
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// Other libraries and framework includes 15// Project includes 16#include "lldb/Core/Log.h" 17#include "lldb/Core/Module.h" 18#include "lldb/Core/ModuleSpec.h" 19#include "lldb/Host/Host.h" 20#include "lldb/Host/Symbols.h" 21#include "lldb/Symbol/ClangNamespaceDecl.h" 22#include "lldb/Symbol/ObjectFile.h" 23#include "lldb/Symbol/VariableList.h" 24 25using namespace lldb; 26using namespace lldb_private; 27 28//---------------------------------------------------------------------- 29// ModuleList constructor 30//---------------------------------------------------------------------- 31ModuleList::ModuleList() : 32 m_modules(), 33 m_modules_mutex (Mutex::eMutexTypeRecursive), 34 m_notifier(NULL) 35{ 36} 37 38//---------------------------------------------------------------------- 39// Copy constructor 40//---------------------------------------------------------------------- 41ModuleList::ModuleList(const ModuleList& rhs) : 42 m_modules(), 43 m_modules_mutex (Mutex::eMutexTypeRecursive) 44{ 45 Mutex::Locker lhs_locker(m_modules_mutex); 46 Mutex::Locker rhs_locker(rhs.m_modules_mutex); 47 m_modules = rhs.m_modules; 48} 49 50ModuleList::ModuleList (ModuleList::Notifier* notifier) : 51 m_modules(), 52 m_modules_mutex (Mutex::eMutexTypeRecursive), 53 m_notifier(notifier) 54{ 55} 56 57//---------------------------------------------------------------------- 58// Assignment operator 59//---------------------------------------------------------------------- 60const ModuleList& 61ModuleList::operator= (const ModuleList& rhs) 62{ 63 if (this != &rhs) 64 { 65 Mutex::Locker lhs_locker(m_modules_mutex); 66 Mutex::Locker rhs_locker(rhs.m_modules_mutex); 67 m_modules = rhs.m_modules; 68 } 69 return *this; 70} 71 72//---------------------------------------------------------------------- 73// Destructor 74//---------------------------------------------------------------------- 75ModuleList::~ModuleList() 76{ 77} 78 79void 80ModuleList::AppendImpl (const ModuleSP &module_sp, bool use_notifier) 81{ 82 if (module_sp) 83 { 84 Mutex::Locker locker(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 Mutex::Locker locker(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 Mutex::Locker locker(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 Mutex::Locker locker(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 Mutex::Locker locker(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 Mutex::Locker locker; 237 238 if (mandatory) 239 { 240 locker.Lock (m_modules_mutex); 241 } 242 else 243 { 244 // Not mandatory, remove orphans if we can get the mutex 245 if (!locker.TryLock(m_modules_mutex)) 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 Mutex::Locker locker(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 Mutex::Locker locker(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 Mutex::Locker locker(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 NULL; 314} 315 316ModuleSP 317ModuleList::GetModuleAtIndex(size_t idx) const 318{ 319 Mutex::Locker locker(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 ConstString lookup_name; 348 uint32_t lookup_name_type_mask = 0; 349 bool match_name_after_lookup = false; 350 Module::PrepareForFunctionNameLookup (name, name_type_mask, 351 lookup_name, 352 lookup_name_type_mask, 353 match_name_after_lookup); 354 355 Mutex::Locker locker(m_modules_mutex); 356 collection::const_iterator pos, end = m_modules.end(); 357 for (pos = m_modules.begin(); pos != end; ++pos) 358 { 359 (*pos)->FindFunctions (lookup_name, 360 NULL, 361 lookup_name_type_mask, 362 include_symbols, 363 include_inlines, 364 true, 365 sc_list); 366 } 367 368 if (match_name_after_lookup) 369 { 370 SymbolContext sc; 371 size_t i = old_size; 372 while (i<sc_list.GetSize()) 373 { 374 if (sc_list.GetContextAtIndex(i, sc)) 375 { 376 const char *func_name = sc.GetFunctionName().GetCString(); 377 if (func_name && strstr (func_name, name.GetCString()) == NULL) 378 { 379 // Remove the current context 380 sc_list.RemoveContextAtIndex(i); 381 // Don't increment i and continue in the loop 382 continue; 383 } 384 } 385 ++i; 386 } 387 } 388 389 } 390 else 391 { 392 Mutex::Locker locker(m_modules_mutex); 393 collection::const_iterator pos, end = m_modules.end(); 394 for (pos = m_modules.begin(); pos != end; ++pos) 395 { 396 (*pos)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list); 397 } 398 } 399 return sc_list.GetSize() - old_size; 400} 401 402size_t 403ModuleList::FindFunctionSymbols (const ConstString &name, 404 uint32_t name_type_mask, 405 SymbolContextList& sc_list) 406{ 407 const size_t old_size = sc_list.GetSize(); 408 409 if (name_type_mask & eFunctionNameTypeAuto) 410 { 411 ConstString lookup_name; 412 uint32_t lookup_name_type_mask = 0; 413 bool match_name_after_lookup = false; 414 Module::PrepareForFunctionNameLookup (name, name_type_mask, 415 lookup_name, 416 lookup_name_type_mask, 417 match_name_after_lookup); 418 419 Mutex::Locker locker(m_modules_mutex); 420 collection::const_iterator pos, end = m_modules.end(); 421 for (pos = m_modules.begin(); pos != end; ++pos) 422 { 423 (*pos)->FindFunctionSymbols (lookup_name, 424 lookup_name_type_mask, 425 sc_list); 426 } 427 428 if (match_name_after_lookup) 429 { 430 SymbolContext sc; 431 size_t i = old_size; 432 while (i<sc_list.GetSize()) 433 { 434 if (sc_list.GetContextAtIndex(i, sc)) 435 { 436 const char *func_name = sc.GetFunctionName().GetCString(); 437 if (func_name && strstr (func_name, name.GetCString()) == NULL) 438 { 439 // Remove the current context 440 sc_list.RemoveContextAtIndex(i); 441 // Don't increment i and continue in the loop 442 continue; 443 } 444 } 445 ++i; 446 } 447 } 448 449 } 450 else 451 { 452 Mutex::Locker locker(m_modules_mutex); 453 collection::const_iterator pos, end = m_modules.end(); 454 for (pos = m_modules.begin(); pos != end; ++pos) 455 { 456 (*pos)->FindFunctionSymbols (name, name_type_mask, sc_list); 457 } 458 } 459 460 return sc_list.GetSize() - old_size; 461} 462 463size_t 464ModuleList::FindCompileUnits (const FileSpec &path, 465 bool append, 466 SymbolContextList &sc_list) const 467{ 468 if (!append) 469 sc_list.Clear(); 470 471 Mutex::Locker locker(m_modules_mutex); 472 collection::const_iterator pos, end = m_modules.end(); 473 for (pos = m_modules.begin(); pos != end; ++pos) 474 { 475 (*pos)->FindCompileUnits (path, true, sc_list); 476 } 477 478 return sc_list.GetSize(); 479} 480 481size_t 482ModuleList::FindGlobalVariables (const ConstString &name, 483 bool append, 484 size_t max_matches, 485 VariableList& variable_list) const 486{ 487 size_t initial_size = variable_list.GetSize(); 488 Mutex::Locker locker(m_modules_mutex); 489 collection::const_iterator pos, end = m_modules.end(); 490 for (pos = m_modules.begin(); pos != end; ++pos) 491 { 492 (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list); 493 } 494 return variable_list.GetSize() - initial_size; 495} 496 497 498size_t 499ModuleList::FindGlobalVariables (const RegularExpression& regex, 500 bool append, 501 size_t max_matches, 502 VariableList& variable_list) const 503{ 504 size_t initial_size = variable_list.GetSize(); 505 Mutex::Locker locker(m_modules_mutex); 506 collection::const_iterator pos, end = m_modules.end(); 507 for (pos = m_modules.begin(); pos != end; ++pos) 508 { 509 (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list); 510 } 511 return variable_list.GetSize() - initial_size; 512} 513 514 515size_t 516ModuleList::FindSymbolsWithNameAndType (const ConstString &name, 517 SymbolType symbol_type, 518 SymbolContextList &sc_list, 519 bool append) const 520{ 521 Mutex::Locker locker(m_modules_mutex); 522 if (!append) 523 sc_list.Clear(); 524 size_t initial_size = sc_list.GetSize(); 525 526 collection::const_iterator pos, end = m_modules.end(); 527 for (pos = m_modules.begin(); pos != end; ++pos) 528 (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list); 529 return sc_list.GetSize() - initial_size; 530} 531 532size_t 533ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, 534 lldb::SymbolType symbol_type, 535 SymbolContextList &sc_list, 536 bool append) const 537{ 538 Mutex::Locker locker(m_modules_mutex); 539 if (!append) 540 sc_list.Clear(); 541 size_t initial_size = sc_list.GetSize(); 542 543 collection::const_iterator pos, end = m_modules.end(); 544 for (pos = m_modules.begin(); pos != end; ++pos) 545 (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list); 546 return sc_list.GetSize() - initial_size; 547} 548 549size_t 550ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const 551{ 552 size_t existing_matches = matching_module_list.GetSize(); 553 554 Mutex::Locker locker(m_modules_mutex); 555 collection::const_iterator pos, end = m_modules.end(); 556 for (pos = m_modules.begin(); pos != end; ++pos) 557 { 558 ModuleSP module_sp(*pos); 559 if (module_sp->MatchesModuleSpec (module_spec)) 560 matching_module_list.Append(module_sp); 561 } 562 return matching_module_list.GetSize() - existing_matches; 563} 564 565ModuleSP 566ModuleList::FindModule (const Module *module_ptr) const 567{ 568 ModuleSP module_sp; 569 570 // Scope for "locker" 571 { 572 Mutex::Locker locker(m_modules_mutex); 573 collection::const_iterator pos, end = m_modules.end(); 574 575 for (pos = m_modules.begin(); pos != end; ++pos) 576 { 577 if ((*pos).get() == module_ptr) 578 { 579 module_sp = (*pos); 580 break; 581 } 582 } 583 } 584 return module_sp; 585 586} 587 588ModuleSP 589ModuleList::FindModule (const UUID &uuid) const 590{ 591 ModuleSP module_sp; 592 593 if (uuid.IsValid()) 594 { 595 Mutex::Locker locker(m_modules_mutex); 596 collection::const_iterator pos, end = m_modules.end(); 597 598 for (pos = m_modules.begin(); pos != end; ++pos) 599 { 600 if ((*pos)->GetUUID() == uuid) 601 { 602 module_sp = (*pos); 603 break; 604 } 605 } 606 } 607 return module_sp; 608} 609 610 611size_t 612ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, size_t max_matches, TypeList& types) const 613{ 614 Mutex::Locker locker(m_modules_mutex); 615 616 size_t total_matches = 0; 617 collection::const_iterator pos, end = m_modules.end(); 618 if (sc.module_sp) 619 { 620 // The symbol context "sc" contains a module so we want to search that 621 // one first if it is in our list... 622 for (pos = m_modules.begin(); pos != end; ++pos) 623 { 624 if (sc.module_sp.get() == (*pos).get()) 625 { 626 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types); 627 628 if (total_matches >= max_matches) 629 break; 630 } 631 } 632 } 633 634 if (total_matches < max_matches) 635 { 636 SymbolContext world_sc; 637 for (pos = m_modules.begin(); pos != end; ++pos) 638 { 639 // Search the module if the module is not equal to the one in the symbol 640 // context "sc". If "sc" contains a empty module shared pointer, then 641 // the comparisong will always be true (valid_module_ptr != NULL). 642 if (sc.module_sp.get() != (*pos).get()) 643 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, types); 644 645 if (total_matches >= max_matches) 646 break; 647 } 648 } 649 650 return total_matches; 651} 652 653bool 654ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 655{ 656 Mutex::Locker locker(m_modules_mutex); 657 collection::const_iterator pos, end = m_modules.end(); 658 for (pos = m_modules.begin(); pos != end; ++pos) 659 { 660 if ((*pos)->FindSourceFile (orig_spec, new_spec)) 661 return true; 662 } 663 return false; 664} 665 666void 667ModuleList::FindAddressesForLine (const lldb::TargetSP target_sp, 668 const FileSpec &file, uint32_t line, 669 Function *function, 670 std::vector<Address> &output_local, std::vector<Address> &output_extern) 671{ 672 Mutex::Locker locker(m_modules_mutex); 673 collection::const_iterator pos, end = m_modules.end(); 674 for (pos = m_modules.begin(); pos != end; ++pos) 675 { 676 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local, output_extern); 677 } 678} 679 680ModuleSP 681ModuleList::FindFirstModule (const ModuleSpec &module_spec) const 682{ 683 ModuleSP module_sp; 684 Mutex::Locker locker(m_modules_mutex); 685 collection::const_iterator pos, end = m_modules.end(); 686 for (pos = m_modules.begin(); pos != end; ++pos) 687 { 688 ModuleSP module_sp(*pos); 689 if (module_sp->MatchesModuleSpec (module_spec)) 690 return module_sp; 691 } 692 return module_sp; 693 694} 695 696size_t 697ModuleList::GetSize() const 698{ 699 size_t size = 0; 700 { 701 Mutex::Locker locker(m_modules_mutex); 702 size = m_modules.size(); 703 } 704 return size; 705} 706 707 708void 709ModuleList::Dump(Stream *s) const 710{ 711// s.Printf("%.*p: ", (int)sizeof(void*) * 2, this); 712// s.Indent(); 713// s << "ModuleList\n"; 714 715 Mutex::Locker locker(m_modules_mutex); 716 collection::const_iterator pos, end = m_modules.end(); 717 for (pos = m_modules.begin(); pos != end; ++pos) 718 { 719 (*pos)->Dump(s); 720 } 721} 722 723void 724ModuleList::LogUUIDAndPaths (Log *log, const char *prefix_cstr) 725{ 726 if (log) 727 { 728 Mutex::Locker locker(m_modules_mutex); 729 collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end(); 730 for (pos = begin; pos != end; ++pos) 731 { 732 Module *module = pos->get(); 733 const FileSpec &module_file_spec = module->GetFileSpec(); 734 log->Printf ("%s[%u] %s (%s) \"%s\"", 735 prefix_cstr ? prefix_cstr : "", 736 (uint32_t)std::distance (begin, pos), 737 module->GetUUID().GetAsString().c_str(), 738 module->GetArchitecture().GetArchitectureName(), 739 module_file_spec.GetPath().c_str()); 740 } 741 } 742} 743 744bool 745ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const 746{ 747 Mutex::Locker locker(m_modules_mutex); 748 collection::const_iterator pos, end = m_modules.end(); 749 for (pos = m_modules.begin(); pos != end; ++pos) 750 { 751 if ((*pos)->ResolveFileAddress (vm_addr, so_addr)) 752 return true; 753 } 754 755 return false; 756} 757 758uint32_t 759ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const 760{ 761 // The address is already section offset so it has a module 762 uint32_t resolved_flags = 0; 763 ModuleSP module_sp (so_addr.GetModule()); 764 if (module_sp) 765 { 766 resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr, 767 resolve_scope, 768 sc); 769 } 770 else 771 { 772 Mutex::Locker locker(m_modules_mutex); 773 collection::const_iterator pos, end = m_modules.end(); 774 for (pos = m_modules.begin(); pos != end; ++pos) 775 { 776 resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr, 777 resolve_scope, 778 sc); 779 if (resolved_flags != 0) 780 break; 781 } 782 } 783 784 return resolved_flags; 785} 786 787uint32_t 788ModuleList::ResolveSymbolContextForFilePath 789( 790 const char *file_path, 791 uint32_t line, 792 bool check_inlines, 793 uint32_t resolve_scope, 794 SymbolContextList& sc_list 795) const 796{ 797 FileSpec file_spec(file_path, false); 798 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 799} 800 801uint32_t 802ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const 803{ 804 Mutex::Locker locker(m_modules_mutex); 805 collection::const_iterator pos, end = m_modules.end(); 806 for (pos = m_modules.begin(); pos != end; ++pos) 807 { 808 (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 809 } 810 811 return sc_list.GetSize(); 812} 813 814size_t 815ModuleList::GetIndexForModule (const Module *module) const 816{ 817 if (module) 818 { 819 Mutex::Locker locker(m_modules_mutex); 820 collection::const_iterator pos; 821 collection::const_iterator begin = m_modules.begin(); 822 collection::const_iterator end = m_modules.end(); 823 for (pos = begin; pos != end; ++pos) 824 { 825 if ((*pos).get() == module) 826 return std::distance (begin, pos); 827 } 828 } 829 return LLDB_INVALID_INDEX32; 830} 831 832static ModuleList & 833GetSharedModuleList () 834{ 835 // NOTE: Intentionally leak the module list so a program doesn't have to 836 // cleanup all modules and object files as it exits. This just wastes time 837 // doing a bunch of cleanup that isn't required. 838 static ModuleList *g_shared_module_list = NULL; 839 if (g_shared_module_list == NULL) 840 g_shared_module_list = new ModuleList(); // <--- Intentional leak!!! 841 842 return *g_shared_module_list; 843} 844 845bool 846ModuleList::ModuleIsInCache (const Module *module_ptr) 847{ 848 if (module_ptr) 849 { 850 ModuleList &shared_module_list = GetSharedModuleList (); 851 return shared_module_list.FindModule (module_ptr).get() != NULL; 852 } 853 return false; 854} 855 856size_t 857ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list) 858{ 859 return GetSharedModuleList ().FindModules (module_spec, matching_module_list); 860} 861 862size_t 863ModuleList::RemoveOrphanSharedModules (bool mandatory) 864{ 865 return GetSharedModuleList ().RemoveOrphans(mandatory); 866} 867 868Error 869ModuleList::GetSharedModule 870( 871 const ModuleSpec &module_spec, 872 ModuleSP &module_sp, 873 const FileSpecList *module_search_paths_ptr, 874 ModuleSP *old_module_sp_ptr, 875 bool *did_create_ptr, 876 bool always_create 877) 878{ 879 ModuleList &shared_module_list = GetSharedModuleList (); 880 Mutex::Locker locker(shared_module_list.m_modules_mutex); 881 char path[PATH_MAX]; 882 883 Error error; 884 885 module_sp.reset(); 886 887 if (did_create_ptr) 888 *did_create_ptr = false; 889 if (old_module_sp_ptr) 890 old_module_sp_ptr->reset(); 891 892 const UUID *uuid_ptr = module_spec.GetUUIDPtr(); 893 const FileSpec &module_file_spec = module_spec.GetFileSpec(); 894 const ArchSpec &arch = module_spec.GetArchitecture(); 895 896 // Make sure no one else can try and get or create a module while this 897 // function is actively working on it by doing an extra lock on the 898 // global mutex list. 899 if (always_create == false) 900 { 901 ModuleList matching_module_list; 902 const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list); 903 if (num_matching_modules > 0) 904 { 905 for (size_t module_idx = 0; module_idx < num_matching_modules; ++module_idx) 906 { 907 module_sp = matching_module_list.GetModuleAtIndex(module_idx); 908 909 // Make sure the file for the module hasn't been modified 910 if (module_sp->FileHasChanged()) 911 { 912 if (old_module_sp_ptr && !old_module_sp_ptr->get()) 913 *old_module_sp_ptr = module_sp; 914 915 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_MODULES)); 916 if (log) 917 log->Printf("module changed: %p, removing from global module list", module_sp.get()); 918 919 shared_module_list.Remove (module_sp); 920 module_sp.reset(); 921 } 922 else 923 { 924 // The module matches and the module was not modified from 925 // when it was last loaded. 926 return error; 927 } 928 } 929 } 930 } 931 932 if (module_sp) 933 return error; 934 else 935 { 936 module_sp.reset (new Module (module_spec)); 937 // Make sure there are a module and an object file since we can specify 938 // a valid file path with an architecture that might not be in that file. 939 // By getting the object file we can guarantee that the architecture matches 940 if (module_sp) 941 { 942 if (module_sp->GetObjectFile()) 943 { 944 // If we get in here we got the correct arch, now we just need 945 // to verify the UUID if one was given 946 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) 947 module_sp.reset(); 948 else 949 { 950 if (did_create_ptr) 951 *did_create_ptr = true; 952 953 shared_module_list.ReplaceEquivalent(module_sp); 954 return error; 955 } 956 } 957 else 958 module_sp.reset(); 959 } 960 } 961 962 // Either the file didn't exist where at the path, or no path was given, so 963 // we now have to use more extreme measures to try and find the appropriate 964 // module. 965 966 // Fixup the incoming path in case the path points to a valid file, yet 967 // the arch or UUID (if one was passed in) don't match. 968 FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec); 969 970 // Don't look for the file if it appears to be the same one we already 971 // checked for above... 972 if (file_spec != module_file_spec) 973 { 974 if (!file_spec.Exists()) 975 { 976 file_spec.GetPath(path, sizeof(path)); 977 if (path[0] == '\0') 978 module_file_spec.GetPath(path, sizeof(path)); 979 if (file_spec.Exists()) 980 { 981 std::string uuid_str; 982 if (uuid_ptr && uuid_ptr->IsValid()) 983 uuid_str = uuid_ptr->GetAsString(); 984 985 if (arch.IsValid()) 986 { 987 if (!uuid_str.empty()) 988 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_str.c_str()); 989 else 990 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName()); 991 } 992 } 993 else 994 { 995 error.SetErrorStringWithFormat("'%s' does not exist", path); 996 } 997 if (error.Fail()) 998 module_sp.reset(); 999 return error; 1000 } 1001 1002 1003 // Make sure no one else can try and get or create a module while this 1004 // function is actively working on it by doing an extra lock on the 1005 // global mutex list. 1006 ModuleSpec platform_module_spec(module_spec); 1007 platform_module_spec.GetFileSpec() = file_spec; 1008 platform_module_spec.GetPlatformFileSpec() = file_spec; 1009 ModuleList matching_module_list; 1010 if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0) 1011 { 1012 module_sp = matching_module_list.GetModuleAtIndex(0); 1013 1014 // If we didn't have a UUID in mind when looking for the object file, 1015 // then we should make sure the modification time hasn't changed! 1016 if (platform_module_spec.GetUUIDPtr() == NULL) 1017 { 1018 TimeValue file_spec_mod_time(file_spec.GetModificationTime()); 1019 if (file_spec_mod_time.IsValid()) 1020 { 1021 if (file_spec_mod_time != module_sp->GetModificationTime()) 1022 { 1023 if (old_module_sp_ptr) 1024 *old_module_sp_ptr = module_sp; 1025 shared_module_list.Remove (module_sp); 1026 module_sp.reset(); 1027 } 1028 } 1029 } 1030 } 1031 1032 if (module_sp.get() == NULL) 1033 { 1034 module_sp.reset (new Module (platform_module_spec)); 1035 // Make sure there are a module and an object file since we can specify 1036 // a valid file path with an architecture that might not be in that file. 1037 // By getting the object file we can guarantee that the architecture matches 1038 if (module_sp && module_sp->GetObjectFile()) 1039 { 1040 if (did_create_ptr) 1041 *did_create_ptr = true; 1042 1043 shared_module_list.ReplaceEquivalent(module_sp); 1044 } 1045 else 1046 { 1047 file_spec.GetPath(path, sizeof(path)); 1048 1049 if (file_spec) 1050 { 1051 if (arch.IsValid()) 1052 error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path); 1053 else 1054 error.SetErrorStringWithFormat("unable to open '%s'", path); 1055 } 1056 else 1057 { 1058 std::string uuid_str; 1059 if (uuid_ptr && uuid_ptr->IsValid()) 1060 uuid_str = uuid_ptr->GetAsString(); 1061 1062 if (!uuid_str.empty()) 1063 error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_str.c_str()); 1064 else 1065 error.SetErrorStringWithFormat("cannot locate a module"); 1066 } 1067 } 1068 } 1069 } 1070 1071 return error; 1072} 1073 1074bool 1075ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp) 1076{ 1077 return GetSharedModuleList ().Remove (module_sp); 1078} 1079 1080bool 1081ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr) 1082{ 1083 return GetSharedModuleList ().RemoveIfOrphaned (module_ptr); 1084} 1085 1086bool 1087ModuleList::LoadScriptingResourcesInTarget (Target *target, 1088 std::list<Error>& errors, 1089 Stream *feedback_stream, 1090 bool continue_on_error) 1091{ 1092 if (!target) 1093 return false; 1094 Mutex::Locker locker(m_modules_mutex); 1095 for (auto module : m_modules) 1096 { 1097 Error error; 1098 if (module) 1099 { 1100 if (!module->LoadScriptingResourceInTarget(target, error, feedback_stream)) 1101 { 1102 if (error.Fail() && error.AsCString()) 1103 { 1104 error.SetErrorStringWithFormat("unable to load scripting data for module %s - error reported was %s", 1105 module->GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1106 error.AsCString()); 1107 errors.push_back(error); 1108 } 1109 if (!continue_on_error) 1110 return false; 1111 } 1112 } 1113 } 1114 return errors.size() == 0; 1115} 1116