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