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