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 25 using namespace lldb; 26 using namespace lldb_private; 27 28 //---------------------------------------------------------------------- 29 // ModuleList constructor 30 //---------------------------------------------------------------------- 31 ModuleList::ModuleList() : 32 m_modules(), 33 m_modules_mutex (Mutex::eMutexTypeRecursive), 34 m_notifier(NULL) 35 { 36 } 37 38 //---------------------------------------------------------------------- 39 // Copy constructor 40 //---------------------------------------------------------------------- 41 ModuleList::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 50 ModuleList::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 //---------------------------------------------------------------------- 60 const ModuleList& 61 ModuleList::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 //---------------------------------------------------------------------- 75 ModuleList::~ModuleList() 76 { 77 } 78 79 void 80 ModuleList::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 91 void 92 ModuleList::Append (const ModuleSP &module_sp) 93 { 94 AppendImpl (module_sp); 95 } 96 97 void 98 ModuleList::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 123 bool 124 ModuleList::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 142 void 143 ModuleList::Append (const ModuleList& module_list) 144 { 145 for (auto pos : module_list.m_modules) 146 Append(pos); 147 } 148 149 bool 150 ModuleList::AppendIfNeeded (const ModuleList& module_list) 151 { 152 bool any_in = false; 153 for (auto pos : module_list.m_modules) 154 any_in = AppendIfNeeded(pos) | any_in; 155 return any_in; 156 } 157 158 bool 159 ModuleList::RemoveImpl (const ModuleSP &module_sp, bool use_notifier) 160 { 161 if (module_sp) 162 { 163 Mutex::Locker locker(m_modules_mutex); 164 collection::iterator pos, end = m_modules.end(); 165 for (pos = m_modules.begin(); pos != end; ++pos) 166 { 167 if (pos->get() == module_sp.get()) 168 { 169 m_modules.erase (pos); 170 if (use_notifier && m_notifier) 171 m_notifier->ModuleRemoved(*this, module_sp); 172 return true; 173 } 174 } 175 } 176 return false; 177 } 178 179 ModuleList::collection::iterator 180 ModuleList::RemoveImpl (ModuleList::collection::iterator pos, bool use_notifier) 181 { 182 ModuleSP module_sp(*pos); 183 collection::iterator retval = m_modules.erase(pos); 184 if (use_notifier && m_notifier) 185 m_notifier->ModuleRemoved(*this, module_sp); 186 return retval; 187 } 188 189 bool 190 ModuleList::Remove (const ModuleSP &module_sp) 191 { 192 return RemoveImpl (module_sp); 193 } 194 195 bool 196 ModuleList::ReplaceModule (const lldb::ModuleSP &old_module_sp, const lldb::ModuleSP &new_module_sp) 197 { 198 if (!RemoveImpl(old_module_sp, false)) 199 return false; 200 AppendImpl (new_module_sp, false); 201 if (m_notifier) 202 m_notifier->ModuleUpdated(*this, old_module_sp,new_module_sp); 203 return true; 204 } 205 206 bool 207 ModuleList::RemoveIfOrphaned (const Module *module_ptr) 208 { 209 if (module_ptr) 210 { 211 Mutex::Locker locker(m_modules_mutex); 212 collection::iterator pos, end = m_modules.end(); 213 for (pos = m_modules.begin(); pos != end; ++pos) 214 { 215 if (pos->get() == module_ptr) 216 { 217 if (pos->unique()) 218 { 219 pos = RemoveImpl(pos); 220 return true; 221 } 222 else 223 return false; 224 } 225 } 226 } 227 return false; 228 } 229 230 size_t 231 ModuleList::RemoveOrphans (bool mandatory) 232 { 233 Mutex::Locker locker; 234 235 if (mandatory) 236 { 237 locker.Lock (m_modules_mutex); 238 } 239 else 240 { 241 // Not mandatory, remove orphans if we can get the mutex 242 if (!locker.TryLock(m_modules_mutex)) 243 return 0; 244 } 245 collection::iterator pos = m_modules.begin(); 246 size_t remove_count = 0; 247 while (pos != m_modules.end()) 248 { 249 if (pos->unique()) 250 { 251 pos = RemoveImpl(pos); 252 ++remove_count; 253 } 254 else 255 { 256 ++pos; 257 } 258 } 259 return remove_count; 260 } 261 262 size_t 263 ModuleList::Remove (ModuleList &module_list) 264 { 265 Mutex::Locker locker(m_modules_mutex); 266 size_t num_removed = 0; 267 collection::iterator pos, end = module_list.m_modules.end(); 268 for (pos = module_list.m_modules.begin(); pos != end; ++pos) 269 { 270 if (Remove (*pos)) 271 ++num_removed; 272 } 273 return num_removed; 274 } 275 276 277 void 278 ModuleList::Clear() 279 { 280 ClearImpl(); 281 } 282 283 void 284 ModuleList::Destroy() 285 { 286 ClearImpl(); 287 } 288 289 void 290 ModuleList::ClearImpl (bool use_notifier) 291 { 292 Mutex::Locker locker(m_modules_mutex); 293 if (use_notifier && m_notifier) 294 m_notifier->WillClearList(*this); 295 m_modules.clear(); 296 } 297 298 Module* 299 ModuleList::GetModulePointerAtIndex (size_t idx) const 300 { 301 Mutex::Locker locker(m_modules_mutex); 302 return GetModulePointerAtIndexUnlocked(idx); 303 } 304 305 Module* 306 ModuleList::GetModulePointerAtIndexUnlocked (size_t idx) const 307 { 308 if (idx < m_modules.size()) 309 return m_modules[idx].get(); 310 return NULL; 311 } 312 313 ModuleSP 314 ModuleList::GetModuleAtIndex(size_t idx) const 315 { 316 Mutex::Locker locker(m_modules_mutex); 317 return GetModuleAtIndexUnlocked(idx); 318 } 319 320 ModuleSP 321 ModuleList::GetModuleAtIndexUnlocked(size_t idx) const 322 { 323 ModuleSP module_sp; 324 if (idx < m_modules.size()) 325 module_sp = m_modules[idx]; 326 return module_sp; 327 } 328 329 size_t 330 ModuleList::FindFunctions (const ConstString &name, 331 uint32_t name_type_mask, 332 bool include_symbols, 333 bool include_inlines, 334 bool append, 335 SymbolContextList &sc_list) const 336 { 337 if (!append) 338 sc_list.Clear(); 339 340 const size_t old_size = sc_list.GetSize(); 341 342 if (name_type_mask & eFunctionNameTypeAuto) 343 { 344 ConstString lookup_name; 345 uint32_t lookup_name_type_mask = 0; 346 bool match_name_after_lookup = false; 347 Module::PrepareForFunctionNameLookup (name, name_type_mask, 348 lookup_name, 349 lookup_name_type_mask, 350 match_name_after_lookup); 351 352 Mutex::Locker locker(m_modules_mutex); 353 collection::const_iterator pos, end = m_modules.end(); 354 for (pos = m_modules.begin(); pos != end; ++pos) 355 { 356 (*pos)->FindFunctions (lookup_name, 357 NULL, 358 lookup_name_type_mask, 359 include_symbols, 360 include_inlines, 361 true, 362 sc_list); 363 } 364 365 if (match_name_after_lookup) 366 { 367 SymbolContext sc; 368 size_t i = old_size; 369 while (i<sc_list.GetSize()) 370 { 371 if (sc_list.GetContextAtIndex(i, sc)) 372 { 373 const char *func_name = sc.GetFunctionName().GetCString(); 374 if (func_name && strstr (func_name, name.GetCString()) == NULL) 375 { 376 // Remove the current context 377 sc_list.RemoveContextAtIndex(i); 378 // Don't increment i and continue in the loop 379 continue; 380 } 381 } 382 ++i; 383 } 384 } 385 386 } 387 else 388 { 389 390 Mutex::Locker locker(m_modules_mutex); 391 collection::const_iterator pos, end = m_modules.end(); 392 for (pos = m_modules.begin(); pos != end; ++pos) 393 { 394 (*pos)->FindFunctions (name, NULL, name_type_mask, include_symbols, include_inlines, true, sc_list); 395 } 396 } 397 return sc_list.GetSize() - old_size; 398 } 399 400 size_t 401 ModuleList::FindCompileUnits (const FileSpec &path, 402 bool append, 403 SymbolContextList &sc_list) const 404 { 405 if (!append) 406 sc_list.Clear(); 407 408 Mutex::Locker locker(m_modules_mutex); 409 collection::const_iterator pos, end = m_modules.end(); 410 for (pos = m_modules.begin(); pos != end; ++pos) 411 { 412 (*pos)->FindCompileUnits (path, true, sc_list); 413 } 414 415 return sc_list.GetSize(); 416 } 417 418 size_t 419 ModuleList::FindGlobalVariables (const ConstString &name, 420 bool append, 421 size_t max_matches, 422 VariableList& variable_list) const 423 { 424 size_t initial_size = variable_list.GetSize(); 425 Mutex::Locker locker(m_modules_mutex); 426 collection::const_iterator pos, end = m_modules.end(); 427 for (pos = m_modules.begin(); pos != end; ++pos) 428 { 429 (*pos)->FindGlobalVariables (name, NULL, append, max_matches, variable_list); 430 } 431 return variable_list.GetSize() - initial_size; 432 } 433 434 435 size_t 436 ModuleList::FindGlobalVariables (const RegularExpression& regex, 437 bool append, 438 size_t max_matches, 439 VariableList& variable_list) const 440 { 441 size_t initial_size = variable_list.GetSize(); 442 Mutex::Locker locker(m_modules_mutex); 443 collection::const_iterator pos, end = m_modules.end(); 444 for (pos = m_modules.begin(); pos != end; ++pos) 445 { 446 (*pos)->FindGlobalVariables (regex, append, max_matches, variable_list); 447 } 448 return variable_list.GetSize() - initial_size; 449 } 450 451 452 size_t 453 ModuleList::FindSymbolsWithNameAndType (const ConstString &name, 454 SymbolType symbol_type, 455 SymbolContextList &sc_list, 456 bool append) const 457 { 458 Mutex::Locker locker(m_modules_mutex); 459 if (!append) 460 sc_list.Clear(); 461 size_t initial_size = sc_list.GetSize(); 462 463 collection::const_iterator pos, end = m_modules.end(); 464 for (pos = m_modules.begin(); pos != end; ++pos) 465 (*pos)->FindSymbolsWithNameAndType (name, symbol_type, sc_list); 466 return sc_list.GetSize() - initial_size; 467 } 468 469 size_t 470 ModuleList::FindSymbolsMatchingRegExAndType (const RegularExpression ®ex, 471 lldb::SymbolType symbol_type, 472 SymbolContextList &sc_list, 473 bool append) const 474 { 475 Mutex::Locker locker(m_modules_mutex); 476 if (!append) 477 sc_list.Clear(); 478 size_t initial_size = sc_list.GetSize(); 479 480 collection::const_iterator pos, end = m_modules.end(); 481 for (pos = m_modules.begin(); pos != end; ++pos) 482 (*pos)->FindSymbolsMatchingRegExAndType (regex, symbol_type, sc_list); 483 return sc_list.GetSize() - initial_size; 484 } 485 486 size_t 487 ModuleList::FindModules (const ModuleSpec &module_spec, ModuleList& matching_module_list) const 488 { 489 size_t existing_matches = matching_module_list.GetSize(); 490 491 Mutex::Locker locker(m_modules_mutex); 492 collection::const_iterator pos, end = m_modules.end(); 493 for (pos = m_modules.begin(); pos != end; ++pos) 494 { 495 ModuleSP module_sp(*pos); 496 if (module_sp->MatchesModuleSpec (module_spec)) 497 matching_module_list.Append(module_sp); 498 } 499 return matching_module_list.GetSize() - existing_matches; 500 } 501 502 ModuleSP 503 ModuleList::FindModule (const Module *module_ptr) const 504 { 505 ModuleSP module_sp; 506 507 // Scope for "locker" 508 { 509 Mutex::Locker locker(m_modules_mutex); 510 collection::const_iterator pos, end = m_modules.end(); 511 512 for (pos = m_modules.begin(); pos != end; ++pos) 513 { 514 if ((*pos).get() == module_ptr) 515 { 516 module_sp = (*pos); 517 break; 518 } 519 } 520 } 521 return module_sp; 522 523 } 524 525 ModuleSP 526 ModuleList::FindModule (const UUID &uuid) const 527 { 528 ModuleSP module_sp; 529 530 if (uuid.IsValid()) 531 { 532 Mutex::Locker locker(m_modules_mutex); 533 collection::const_iterator pos, end = m_modules.end(); 534 535 for (pos = m_modules.begin(); pos != end; ++pos) 536 { 537 if ((*pos)->GetUUID() == uuid) 538 { 539 module_sp = (*pos); 540 break; 541 } 542 } 543 } 544 return module_sp; 545 } 546 547 548 size_t 549 ModuleList::FindTypes (const SymbolContext& sc, const ConstString &name, bool name_is_fully_qualified, size_t max_matches, TypeList& types) const 550 { 551 Mutex::Locker locker(m_modules_mutex); 552 553 size_t total_matches = 0; 554 collection::const_iterator pos, end = m_modules.end(); 555 if (sc.module_sp) 556 { 557 // The symbol context "sc" contains a module so we want to search that 558 // one first if it is in our list... 559 for (pos = m_modules.begin(); pos != end; ++pos) 560 { 561 if (sc.module_sp.get() == (*pos).get()) 562 { 563 total_matches += (*pos)->FindTypes (sc, name, name_is_fully_qualified, max_matches, types); 564 565 if (total_matches >= max_matches) 566 break; 567 } 568 } 569 } 570 571 if (total_matches < max_matches) 572 { 573 SymbolContext world_sc; 574 for (pos = m_modules.begin(); pos != end; ++pos) 575 { 576 // Search the module if the module is not equal to the one in the symbol 577 // context "sc". If "sc" contains a empty module shared pointer, then 578 // the comparisong will always be true (valid_module_ptr != NULL). 579 if (sc.module_sp.get() != (*pos).get()) 580 total_matches += (*pos)->FindTypes (world_sc, name, name_is_fully_qualified, max_matches, types); 581 582 if (total_matches >= max_matches) 583 break; 584 } 585 } 586 587 return total_matches; 588 } 589 590 bool 591 ModuleList::FindSourceFile (const FileSpec &orig_spec, FileSpec &new_spec) const 592 { 593 Mutex::Locker locker(m_modules_mutex); 594 collection::const_iterator pos, end = m_modules.end(); 595 for (pos = m_modules.begin(); pos != end; ++pos) 596 { 597 if ((*pos)->FindSourceFile (orig_spec, new_spec)) 598 return true; 599 } 600 return false; 601 } 602 603 604 605 ModuleSP 606 ModuleList::FindFirstModule (const ModuleSpec &module_spec) const 607 { 608 ModuleSP module_sp; 609 Mutex::Locker locker(m_modules_mutex); 610 collection::const_iterator pos, end = m_modules.end(); 611 for (pos = m_modules.begin(); pos != end; ++pos) 612 { 613 ModuleSP module_sp(*pos); 614 if (module_sp->MatchesModuleSpec (module_spec)) 615 return module_sp; 616 } 617 return module_sp; 618 619 } 620 621 size_t 622 ModuleList::GetSize() const 623 { 624 size_t size = 0; 625 { 626 Mutex::Locker locker(m_modules_mutex); 627 size = m_modules.size(); 628 } 629 return size; 630 } 631 632 633 void 634 ModuleList::Dump(Stream *s) const 635 { 636 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this); 637 // s.Indent(); 638 // s << "ModuleList\n"; 639 640 Mutex::Locker locker(m_modules_mutex); 641 collection::const_iterator pos, end = m_modules.end(); 642 for (pos = m_modules.begin(); pos != end; ++pos) 643 { 644 (*pos)->Dump(s); 645 } 646 } 647 648 void 649 ModuleList::LogUUIDAndPaths (Log *log, const char *prefix_cstr) 650 { 651 if (log) 652 { 653 Mutex::Locker locker(m_modules_mutex); 654 collection::const_iterator pos, begin = m_modules.begin(), end = m_modules.end(); 655 for (pos = begin; pos != end; ++pos) 656 { 657 Module *module = pos->get(); 658 const FileSpec &module_file_spec = module->GetFileSpec(); 659 log->Printf ("%s[%u] %s (%s) \"%s\"", 660 prefix_cstr ? prefix_cstr : "", 661 (uint32_t)std::distance (begin, pos), 662 module->GetUUID().GetAsString().c_str(), 663 module->GetArchitecture().GetArchitectureName(), 664 module_file_spec.GetPath().c_str()); 665 } 666 } 667 } 668 669 bool 670 ModuleList::ResolveFileAddress (lldb::addr_t vm_addr, Address& so_addr) const 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 if ((*pos)->ResolveFileAddress (vm_addr, so_addr)) 677 return true; 678 } 679 680 return false; 681 } 682 683 uint32_t 684 ModuleList::ResolveSymbolContextForAddress (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc) const 685 { 686 // The address is already section offset so it has a module 687 uint32_t resolved_flags = 0; 688 ModuleSP module_sp (so_addr.GetModule()); 689 if (module_sp) 690 { 691 resolved_flags = module_sp->ResolveSymbolContextForAddress (so_addr, 692 resolve_scope, 693 sc); 694 } 695 else 696 { 697 Mutex::Locker locker(m_modules_mutex); 698 collection::const_iterator pos, end = m_modules.end(); 699 for (pos = m_modules.begin(); pos != end; ++pos) 700 { 701 resolved_flags = (*pos)->ResolveSymbolContextForAddress (so_addr, 702 resolve_scope, 703 sc); 704 if (resolved_flags != 0) 705 break; 706 } 707 } 708 709 return resolved_flags; 710 } 711 712 uint32_t 713 ModuleList::ResolveSymbolContextForFilePath 714 ( 715 const char *file_path, 716 uint32_t line, 717 bool check_inlines, 718 uint32_t resolve_scope, 719 SymbolContextList& sc_list 720 ) const 721 { 722 FileSpec file_spec(file_path, false); 723 return ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 724 } 725 726 uint32_t 727 ModuleList::ResolveSymbolContextsForFileSpec (const FileSpec &file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list) const 728 { 729 Mutex::Locker locker(m_modules_mutex); 730 collection::const_iterator pos, end = m_modules.end(); 731 for (pos = m_modules.begin(); pos != end; ++pos) 732 { 733 (*pos)->ResolveSymbolContextsForFileSpec (file_spec, line, check_inlines, resolve_scope, sc_list); 734 } 735 736 return sc_list.GetSize(); 737 } 738 739 size_t 740 ModuleList::GetIndexForModule (const Module *module) const 741 { 742 if (module) 743 { 744 Mutex::Locker locker(m_modules_mutex); 745 collection::const_iterator pos; 746 collection::const_iterator begin = m_modules.begin(); 747 collection::const_iterator end = m_modules.end(); 748 for (pos = begin; pos != end; ++pos) 749 { 750 if ((*pos).get() == module) 751 return std::distance (begin, pos); 752 } 753 } 754 return LLDB_INVALID_INDEX32; 755 } 756 757 static ModuleList & 758 GetSharedModuleList () 759 { 760 // NOTE: Intentionally leak the module list so a program doesn't have to 761 // cleanup all modules and object files as it exits. This just wastes time 762 // doing a bunch of cleanup that isn't required. 763 static ModuleList *g_shared_module_list = NULL; 764 if (g_shared_module_list == NULL) 765 g_shared_module_list = new ModuleList(); // <--- Intentional leak!!! 766 767 return *g_shared_module_list; 768 } 769 770 bool 771 ModuleList::ModuleIsInCache (const Module *module_ptr) 772 { 773 if (module_ptr) 774 { 775 ModuleList &shared_module_list = GetSharedModuleList (); 776 return shared_module_list.FindModule (module_ptr).get() != NULL; 777 } 778 return false; 779 } 780 781 size_t 782 ModuleList::FindSharedModules (const ModuleSpec &module_spec, ModuleList &matching_module_list) 783 { 784 return GetSharedModuleList ().FindModules (module_spec, matching_module_list); 785 } 786 787 size_t 788 ModuleList::RemoveOrphanSharedModules (bool mandatory) 789 { 790 return GetSharedModuleList ().RemoveOrphans(mandatory); 791 } 792 793 Error 794 ModuleList::GetSharedModule 795 ( 796 const ModuleSpec &module_spec, 797 ModuleSP &module_sp, 798 const FileSpecList *module_search_paths_ptr, 799 ModuleSP *old_module_sp_ptr, 800 bool *did_create_ptr, 801 bool always_create 802 ) 803 { 804 ModuleList &shared_module_list = GetSharedModuleList (); 805 Mutex::Locker locker(shared_module_list.m_modules_mutex); 806 char path[PATH_MAX]; 807 808 Error error; 809 810 module_sp.reset(); 811 812 if (did_create_ptr) 813 *did_create_ptr = false; 814 if (old_module_sp_ptr) 815 old_module_sp_ptr->reset(); 816 817 const UUID *uuid_ptr = module_spec.GetUUIDPtr(); 818 const FileSpec &module_file_spec = module_spec.GetFileSpec(); 819 const ArchSpec &arch = module_spec.GetArchitecture(); 820 821 // Make sure no one else can try and get or create a module while this 822 // function is actively working on it by doing an extra lock on the 823 // global mutex list. 824 if (always_create == false) 825 { 826 ModuleList matching_module_list; 827 const size_t num_matching_modules = shared_module_list.FindModules (module_spec, matching_module_list); 828 if (num_matching_modules > 0) 829 { 830 for (size_t module_idx = 0; module_idx < num_matching_modules; ++module_idx) 831 { 832 module_sp = matching_module_list.GetModuleAtIndex(module_idx); 833 834 // Make sure the file for the module hasn't been modified 835 if (module_sp->FileHasChanged()) 836 { 837 if (old_module_sp_ptr && !old_module_sp_ptr->get()) 838 *old_module_sp_ptr = module_sp; 839 840 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_MODULES)); 841 if (log) 842 log->Printf("module changed: %p, removing from global module list", module_sp.get()); 843 844 shared_module_list.Remove (module_sp); 845 module_sp.reset(); 846 } 847 else 848 { 849 // The module matches and the module was not modified from 850 // when it was last loaded. 851 return error; 852 } 853 } 854 } 855 } 856 857 if (module_sp) 858 return error; 859 else 860 { 861 module_sp.reset (new Module (module_spec)); 862 // Make sure there are a module and an object file since we can specify 863 // a valid file path with an architecture that might not be in that file. 864 // By getting the object file we can guarantee that the architecture matches 865 if (module_sp) 866 { 867 if (module_sp->GetObjectFile()) 868 { 869 // If we get in here we got the correct arch, now we just need 870 // to verify the UUID if one was given 871 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) 872 module_sp.reset(); 873 else 874 { 875 if (did_create_ptr) 876 *did_create_ptr = true; 877 878 shared_module_list.ReplaceEquivalent(module_sp); 879 return error; 880 } 881 } 882 else 883 module_sp.reset(); 884 } 885 } 886 887 // Either the file didn't exist where at the path, or no path was given, so 888 // we now have to use more extreme measures to try and find the appropriate 889 // module. 890 891 // Fixup the incoming path in case the path points to a valid file, yet 892 // the arch or UUID (if one was passed in) don't match. 893 FileSpec file_spec = Symbols::LocateExecutableObjectFile (module_spec); 894 895 // Don't look for the file if it appears to be the same one we already 896 // checked for above... 897 if (file_spec != module_file_spec) 898 { 899 if (!file_spec.Exists()) 900 { 901 file_spec.GetPath(path, sizeof(path)); 902 if (path[0] == '\0') 903 module_file_spec.GetPath(path, sizeof(path)); 904 if (file_spec.Exists()) 905 { 906 std::string uuid_str; 907 if (uuid_ptr && uuid_ptr->IsValid()) 908 uuid_str = uuid_ptr->GetAsString(); 909 910 if (arch.IsValid()) 911 { 912 if (!uuid_str.empty()) 913 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture and UUID %s", path, arch.GetArchitectureName(), uuid_str.c_str()); 914 else 915 error.SetErrorStringWithFormat("'%s' does not contain the %s architecture.", path, arch.GetArchitectureName()); 916 } 917 } 918 else 919 { 920 error.SetErrorStringWithFormat("'%s' does not exist", path); 921 } 922 if (error.Fail()) 923 module_sp.reset(); 924 return error; 925 } 926 927 928 // Make sure no one else can try and get or create a module while this 929 // function is actively working on it by doing an extra lock on the 930 // global mutex list. 931 ModuleSpec platform_module_spec(module_spec); 932 platform_module_spec.GetFileSpec() = file_spec; 933 platform_module_spec.GetPlatformFileSpec() = file_spec; 934 ModuleList matching_module_list; 935 if (shared_module_list.FindModules (platform_module_spec, matching_module_list) > 0) 936 { 937 module_sp = matching_module_list.GetModuleAtIndex(0); 938 939 // If we didn't have a UUID in mind when looking for the object file, 940 // then we should make sure the modification time hasn't changed! 941 if (platform_module_spec.GetUUIDPtr() == NULL) 942 { 943 TimeValue file_spec_mod_time(file_spec.GetModificationTime()); 944 if (file_spec_mod_time.IsValid()) 945 { 946 if (file_spec_mod_time != module_sp->GetModificationTime()) 947 { 948 if (old_module_sp_ptr) 949 *old_module_sp_ptr = module_sp; 950 shared_module_list.Remove (module_sp); 951 module_sp.reset(); 952 } 953 } 954 } 955 } 956 957 if (module_sp.get() == NULL) 958 { 959 module_sp.reset (new Module (platform_module_spec)); 960 // Make sure there are a module and an object file since we can specify 961 // a valid file path with an architecture that might not be in that file. 962 // By getting the object file we can guarantee that the architecture matches 963 if (module_sp && module_sp->GetObjectFile()) 964 { 965 if (did_create_ptr) 966 *did_create_ptr = true; 967 968 shared_module_list.ReplaceEquivalent(module_sp); 969 } 970 else 971 { 972 file_spec.GetPath(path, sizeof(path)); 973 974 if (file_spec) 975 { 976 if (arch.IsValid()) 977 error.SetErrorStringWithFormat("unable to open %s architecture in '%s'", arch.GetArchitectureName(), path); 978 else 979 error.SetErrorStringWithFormat("unable to open '%s'", path); 980 } 981 else 982 { 983 std::string uuid_str; 984 if (uuid_ptr && uuid_ptr->IsValid()) 985 uuid_str = uuid_ptr->GetAsString(); 986 987 if (!uuid_str.empty()) 988 error.SetErrorStringWithFormat("cannot locate a module for UUID '%s'", uuid_str.c_str()); 989 else 990 error.SetErrorStringWithFormat("cannot locate a module"); 991 } 992 } 993 } 994 } 995 996 return error; 997 } 998 999 bool 1000 ModuleList::RemoveSharedModule (lldb::ModuleSP &module_sp) 1001 { 1002 return GetSharedModuleList ().Remove (module_sp); 1003 } 1004 1005 bool 1006 ModuleList::RemoveSharedModuleIfOrphaned (const Module *module_ptr) 1007 { 1008 return GetSharedModuleList ().RemoveIfOrphaned (module_ptr); 1009 } 1010 1011 bool 1012 ModuleList::LoadScriptingResourcesInTarget (Target *target, 1013 std::list<Error>& errors, 1014 Stream *feedback_stream, 1015 bool continue_on_error) 1016 { 1017 if (!target) 1018 return false; 1019 Mutex::Locker locker(m_modules_mutex); 1020 for (auto module : m_modules) 1021 { 1022 Error error; 1023 if (module) 1024 { 1025 if (!module->LoadScriptingResourceInTarget(target, error, feedback_stream)) 1026 { 1027 if (error.Fail() && error.AsCString()) 1028 { 1029 error.SetErrorStringWithFormat("unable to load scripting data for module %s - error reported was %s", 1030 module->GetFileSpec().GetFileNameStrippingExtension().GetCString(), 1031 error.AsCString()); 1032 errors.push_back(error); 1033 } 1034 if (!continue_on_error) 1035 return false; 1036 } 1037 } 1038 } 1039 return errors.size() == 0; 1040 } 1041