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