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