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