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