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