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 #define LLDB_PROPERTIES_modulelist 70 #include "Properties.inc" 71 }; 72 73 enum { 74 #define LLDB_PROPERTIES_modulelist 75 #include "PropertiesEnum.inc" 76 }; 77 78 } // namespace 79 80 ModuleListProperties::ModuleListProperties() { 81 m_collection_sp = 82 std::make_shared<OptionValueProperties>(ConstString("symbols")); 83 m_collection_sp->Initialize(g_properties); 84 85 llvm::SmallString<128> path; 86 clang::driver::Driver::getDefaultModuleCachePath(path); 87 SetClangModulesCachePath(path); 88 } 89 90 bool ModuleListProperties::GetEnableExternalLookup() const { 91 const uint32_t idx = ePropertyEnableExternalLookup; 92 return m_collection_sp->GetPropertyAtIndexAsBoolean( 93 nullptr, idx, g_properties[idx].default_uint_value != 0); 94 } 95 96 bool ModuleListProperties::SetEnableExternalLookup(bool new_value) { 97 return m_collection_sp->SetPropertyAtIndexAsBoolean( 98 nullptr, ePropertyEnableExternalLookup, new_value); 99 } 100 101 FileSpec ModuleListProperties::GetClangModulesCachePath() const { 102 return m_collection_sp 103 ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, 104 ePropertyClangModulesCachePath) 105 ->GetCurrentValue(); 106 } 107 108 bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) { 109 return m_collection_sp->SetPropertyAtIndexAsString( 110 nullptr, ePropertyClangModulesCachePath, path); 111 } 112 113 ModuleList::ModuleList() 114 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {} 115 116 ModuleList::ModuleList(const ModuleList &rhs) 117 : m_modules(), m_modules_mutex(), m_notifier(nullptr) { 118 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex); 119 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex); 120 m_modules = rhs.m_modules; 121 } 122 123 ModuleList::ModuleList(ModuleList::Notifier *notifier) 124 : m_modules(), m_modules_mutex(), m_notifier(notifier) {} 125 126 const ModuleList &ModuleList::operator=(const ModuleList &rhs) { 127 if (this != &rhs) { 128 std::lock(m_modules_mutex, rhs.m_modules_mutex); 129 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex, 130 std::adopt_lock); 131 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex, 132 std::adopt_lock); 133 m_modules = rhs.m_modules; 134 } 135 return *this; 136 } 137 138 ModuleList::~ModuleList() = default; 139 140 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) { 141 if (module_sp) { 142 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 143 m_modules.push_back(module_sp); 144 if (use_notifier && m_notifier) 145 m_notifier->NotifyModuleAdded(*this, module_sp); 146 } 147 } 148 149 void ModuleList::Append(const ModuleSP &module_sp, bool notify) { 150 AppendImpl(module_sp, notify); 151 } 152 153 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) { 154 if (module_sp) { 155 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 156 157 // First remove any equivalent modules. Equivalent modules are modules 158 // whose path, platform path and architecture match. 159 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(), 160 module_sp->GetArchitecture()); 161 equivalent_module_spec.GetPlatformFileSpec() = 162 module_sp->GetPlatformFileSpec(); 163 164 size_t idx = 0; 165 while (idx < m_modules.size()) { 166 ModuleSP module_sp(m_modules[idx]); 167 if (module_sp->MatchesModuleSpec(equivalent_module_spec)) 168 RemoveImpl(m_modules.begin() + idx); 169 else 170 ++idx; 171 } 172 // Now add the new module to the list 173 Append(module_sp); 174 } 175 } 176 177 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp, bool notify) { 178 if (module_sp) { 179 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 180 collection::iterator pos, end = m_modules.end(); 181 for (pos = m_modules.begin(); pos != end; ++pos) { 182 if (pos->get() == module_sp.get()) 183 return false; // Already in the list 184 } 185 // Only push module_sp on the list if it wasn't already in there. 186 Append(module_sp, notify); 187 return true; 188 } 189 return false; 190 } 191 192 void ModuleList::Append(const ModuleList &module_list) { 193 for (auto pos : module_list.m_modules) 194 Append(pos); 195 } 196 197 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) { 198 bool any_in = false; 199 for (auto pos : module_list.m_modules) { 200 if (AppendIfNeeded(pos)) 201 any_in = true; 202 } 203 return any_in; 204 } 205 206 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) { 207 if (module_sp) { 208 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 209 collection::iterator pos, end = m_modules.end(); 210 for (pos = m_modules.begin(); pos != end; ++pos) { 211 if (pos->get() == module_sp.get()) { 212 m_modules.erase(pos); 213 if (use_notifier && m_notifier) 214 m_notifier->NotifyModuleRemoved(*this, module_sp); 215 return true; 216 } 217 } 218 } 219 return false; 220 } 221 222 ModuleList::collection::iterator 223 ModuleList::RemoveImpl(ModuleList::collection::iterator pos, 224 bool use_notifier) { 225 ModuleSP module_sp(*pos); 226 collection::iterator retval = m_modules.erase(pos); 227 if (use_notifier && m_notifier) 228 m_notifier->NotifyModuleRemoved(*this, module_sp); 229 return retval; 230 } 231 232 bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) { 233 return RemoveImpl(module_sp, notify); 234 } 235 236 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp, 237 const lldb::ModuleSP &new_module_sp) { 238 if (!RemoveImpl(old_module_sp, false)) 239 return false; 240 AppendImpl(new_module_sp, false); 241 if (m_notifier) 242 m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp); 243 return true; 244 } 245 246 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) { 247 if (module_ptr) { 248 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 249 collection::iterator pos, end = m_modules.end(); 250 for (pos = m_modules.begin(); pos != end; ++pos) { 251 if (pos->get() == module_ptr) { 252 if (pos->unique()) { 253 pos = RemoveImpl(pos); 254 return true; 255 } else 256 return false; 257 } 258 } 259 } 260 return false; 261 } 262 263 size_t ModuleList::RemoveOrphans(bool mandatory) { 264 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock); 265 266 if (mandatory) { 267 lock.lock(); 268 } else { 269 // Not mandatory, remove orphans if we can get the mutex 270 if (!lock.try_lock()) 271 return 0; 272 } 273 collection::iterator pos = m_modules.begin(); 274 size_t remove_count = 0; 275 while (pos != m_modules.end()) { 276 if (pos->unique()) { 277 pos = RemoveImpl(pos); 278 ++remove_count; 279 } else { 280 ++pos; 281 } 282 } 283 return remove_count; 284 } 285 286 size_t ModuleList::Remove(ModuleList &module_list) { 287 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 288 size_t num_removed = 0; 289 collection::iterator pos, end = module_list.m_modules.end(); 290 for (pos = module_list.m_modules.begin(); pos != end; ++pos) { 291 if (Remove(*pos, false /* notify */)) 292 ++num_removed; 293 } 294 if (m_notifier) 295 m_notifier->NotifyModulesRemoved(module_list); 296 return num_removed; 297 } 298 299 void ModuleList::Clear() { ClearImpl(); } 300 301 void ModuleList::Destroy() { ClearImpl(); } 302 303 void ModuleList::ClearImpl(bool use_notifier) { 304 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 305 if (use_notifier && m_notifier) 306 m_notifier->NotifyWillClearList(*this); 307 m_modules.clear(); 308 } 309 310 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const { 311 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 312 return GetModulePointerAtIndexUnlocked(idx); 313 } 314 315 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const { 316 if (idx < m_modules.size()) 317 return m_modules[idx].get(); 318 return nullptr; 319 } 320 321 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const { 322 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 323 return GetModuleAtIndexUnlocked(idx); 324 } 325 326 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const { 327 ModuleSP module_sp; 328 if (idx < m_modules.size()) 329 module_sp = m_modules[idx]; 330 return module_sp; 331 } 332 333 size_t ModuleList::FindFunctions(ConstString name, 334 FunctionNameType name_type_mask, 335 bool include_symbols, bool include_inlines, 336 bool append, 337 SymbolContextList &sc_list) const { 338 if (!append) 339 sc_list.Clear(); 340 341 const size_t old_size = sc_list.GetSize(); 342 343 if (name_type_mask & eFunctionNameTypeAuto) { 344 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 345 346 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 347 collection::const_iterator pos, end = m_modules.end(); 348 for (pos = m_modules.begin(); pos != end; ++pos) { 349 (*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr, 350 lookup_info.GetNameTypeMask(), include_symbols, 351 include_inlines, true, sc_list); 352 } 353 354 const size_t new_size = sc_list.GetSize(); 355 356 if (old_size < new_size) 357 lookup_info.Prune(sc_list, old_size); 358 } else { 359 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 360 collection::const_iterator pos, end = m_modules.end(); 361 for (pos = m_modules.begin(); pos != end; ++pos) { 362 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols, 363 include_inlines, true, sc_list); 364 } 365 } 366 return sc_list.GetSize() - old_size; 367 } 368 369 size_t ModuleList::FindFunctionSymbols(ConstString name, 370 lldb::FunctionNameType name_type_mask, 371 SymbolContextList &sc_list) { 372 const size_t old_size = sc_list.GetSize(); 373 374 if (name_type_mask & eFunctionNameTypeAuto) { 375 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown); 376 377 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 378 collection::const_iterator pos, end = m_modules.end(); 379 for (pos = m_modules.begin(); pos != end; ++pos) { 380 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(), 381 lookup_info.GetNameTypeMask(), sc_list); 382 } 383 384 const size_t new_size = sc_list.GetSize(); 385 386 if (old_size < new_size) 387 lookup_info.Prune(sc_list, old_size); 388 } else { 389 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 390 collection::const_iterator pos, end = m_modules.end(); 391 for (pos = m_modules.begin(); pos != end; ++pos) { 392 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list); 393 } 394 } 395 396 return sc_list.GetSize() - old_size; 397 } 398 399 size_t ModuleList::FindFunctions(const RegularExpression &name, 400 bool include_symbols, bool include_inlines, 401 bool append, SymbolContextList &sc_list) { 402 const size_t old_size = sc_list.GetSize(); 403 404 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 405 collection::const_iterator pos, end = m_modules.end(); 406 for (pos = m_modules.begin(); pos != end; ++pos) { 407 (*pos)->FindFunctions(name, include_symbols, include_inlines, append, 408 sc_list); 409 } 410 411 return sc_list.GetSize() - old_size; 412 } 413 414 size_t ModuleList::FindCompileUnits(const FileSpec &path, bool append, 415 SymbolContextList &sc_list) const { 416 if (!append) 417 sc_list.Clear(); 418 419 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 420 collection::const_iterator pos, end = m_modules.end(); 421 for (pos = m_modules.begin(); pos != end; ++pos) { 422 (*pos)->FindCompileUnits(path, true, sc_list); 423 } 424 425 return sc_list.GetSize(); 426 } 427 428 size_t ModuleList::FindGlobalVariables(ConstString name, 429 size_t max_matches, 430 VariableList &variable_list) const { 431 size_t initial_size = variable_list.GetSize(); 432 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 433 collection::const_iterator pos, end = m_modules.end(); 434 for (pos = m_modules.begin(); pos != end; ++pos) { 435 (*pos)->FindGlobalVariables(name, nullptr, max_matches, variable_list); 436 } 437 return variable_list.GetSize() - initial_size; 438 } 439 440 size_t ModuleList::FindGlobalVariables(const RegularExpression ®ex, 441 size_t max_matches, 442 VariableList &variable_list) const { 443 size_t initial_size = variable_list.GetSize(); 444 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 445 collection::const_iterator pos, end = m_modules.end(); 446 for (pos = m_modules.begin(); pos != end; ++pos) { 447 (*pos)->FindGlobalVariables(regex, max_matches, variable_list); 448 } 449 return variable_list.GetSize() - initial_size; 450 } 451 452 size_t ModuleList::FindSymbolsWithNameAndType(ConstString name, 453 SymbolType symbol_type, 454 SymbolContextList &sc_list, 455 bool append) const { 456 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 457 if (!append) 458 sc_list.Clear(); 459 size_t initial_size = sc_list.GetSize(); 460 461 collection::const_iterator pos, end = m_modules.end(); 462 for (pos = m_modules.begin(); pos != end; ++pos) 463 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list); 464 return sc_list.GetSize() - initial_size; 465 } 466 467 size_t ModuleList::FindSymbolsMatchingRegExAndType( 468 const RegularExpression ®ex, lldb::SymbolType symbol_type, 469 SymbolContextList &sc_list, bool append) const { 470 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 471 if (!append) 472 sc_list.Clear(); 473 size_t initial_size = sc_list.GetSize(); 474 475 collection::const_iterator pos, end = m_modules.end(); 476 for (pos = m_modules.begin(); pos != end; ++pos) 477 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list); 478 return sc_list.GetSize() - initial_size; 479 } 480 481 size_t ModuleList::FindModules(const ModuleSpec &module_spec, 482 ModuleList &matching_module_list) const { 483 size_t existing_matches = matching_module_list.GetSize(); 484 485 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 486 collection::const_iterator pos, end = m_modules.end(); 487 for (pos = m_modules.begin(); pos != end; ++pos) { 488 ModuleSP module_sp(*pos); 489 if (module_sp->MatchesModuleSpec(module_spec)) 490 matching_module_list.Append(module_sp); 491 } 492 return matching_module_list.GetSize() - existing_matches; 493 } 494 495 ModuleSP ModuleList::FindModule(const Module *module_ptr) const { 496 ModuleSP module_sp; 497 498 // Scope for "locker" 499 { 500 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 501 collection::const_iterator pos, end = m_modules.end(); 502 503 for (pos = m_modules.begin(); pos != end; ++pos) { 504 if ((*pos).get() == module_ptr) { 505 module_sp = (*pos); 506 break; 507 } 508 } 509 } 510 return module_sp; 511 } 512 513 ModuleSP ModuleList::FindModule(const UUID &uuid) const { 514 ModuleSP module_sp; 515 516 if (uuid.IsValid()) { 517 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 518 collection::const_iterator pos, end = m_modules.end(); 519 520 for (pos = m_modules.begin(); pos != end; ++pos) { 521 if ((*pos)->GetUUID() == uuid) { 522 module_sp = (*pos); 523 break; 524 } 525 } 526 } 527 return module_sp; 528 } 529 530 size_t 531 ModuleList::FindTypes(Module *search_first, ConstString name, 532 bool name_is_fully_qualified, size_t max_matches, 533 llvm::DenseSet<SymbolFile *> &searched_symbol_files, 534 TypeList &types) const { 535 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 536 537 size_t total_matches = 0; 538 collection::const_iterator pos, end = m_modules.end(); 539 if (search_first) { 540 for (pos = m_modules.begin(); pos != end; ++pos) { 541 if (search_first == pos->get()) { 542 total_matches += 543 search_first->FindTypes(name, name_is_fully_qualified, max_matches, 544 searched_symbol_files, types); 545 546 if (total_matches >= max_matches) 547 break; 548 } 549 } 550 } 551 552 if (total_matches < max_matches) { 553 for (pos = m_modules.begin(); pos != end; ++pos) { 554 // Search the module if the module is not equal to the one in the symbol 555 // context "sc". If "sc" contains a empty module shared pointer, then the 556 // comparison will always be true (valid_module_ptr != nullptr). 557 if (search_first != pos->get()) 558 total_matches += 559 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches, 560 searched_symbol_files, types); 561 562 if (total_matches >= max_matches) 563 break; 564 } 565 } 566 567 return total_matches; 568 } 569 570 bool ModuleList::FindSourceFile(const FileSpec &orig_spec, 571 FileSpec &new_spec) const { 572 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 573 collection::const_iterator pos, end = m_modules.end(); 574 for (pos = m_modules.begin(); pos != end; ++pos) { 575 if ((*pos)->FindSourceFile(orig_spec, new_spec)) 576 return true; 577 } 578 return false; 579 } 580 581 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp, 582 const FileSpec &file, uint32_t line, 583 Function *function, 584 std::vector<Address> &output_local, 585 std::vector<Address> &output_extern) { 586 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 587 collection::const_iterator pos, end = m_modules.end(); 588 for (pos = m_modules.begin(); pos != end; ++pos) { 589 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local, 590 output_extern); 591 } 592 } 593 594 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const { 595 ModuleSP module_sp; 596 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 597 collection::const_iterator pos, end = m_modules.end(); 598 for (pos = m_modules.begin(); pos != end; ++pos) { 599 ModuleSP module_sp(*pos); 600 if (module_sp->MatchesModuleSpec(module_spec)) 601 return module_sp; 602 } 603 return module_sp; 604 } 605 606 size_t ModuleList::GetSize() const { 607 size_t size = 0; 608 { 609 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 610 size = m_modules.size(); 611 } 612 return size; 613 } 614 615 void ModuleList::Dump(Stream *s) const { 616 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this); 617 // s.Indent(); 618 // s << "ModuleList\n"; 619 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 (*pos)->Dump(s); 624 } 625 } 626 627 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) { 628 if (log != nullptr) { 629 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 630 collection::const_iterator pos, begin = m_modules.begin(), 631 end = m_modules.end(); 632 for (pos = begin; pos != end; ++pos) { 633 Module *module = pos->get(); 634 const FileSpec &module_file_spec = module->GetFileSpec(); 635 LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "", 636 (uint32_t)std::distance(begin, pos), 637 module->GetUUID().GetAsString().c_str(), 638 module->GetArchitecture().GetArchitectureName(), 639 module_file_spec.GetPath().c_str()); 640 } 641 } 642 } 643 644 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr, 645 Address &so_addr) const { 646 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 647 collection::const_iterator pos, end = m_modules.end(); 648 for (pos = m_modules.begin(); pos != end; ++pos) { 649 if ((*pos)->ResolveFileAddress(vm_addr, so_addr)) 650 return true; 651 } 652 653 return false; 654 } 655 656 uint32_t 657 ModuleList::ResolveSymbolContextForAddress(const Address &so_addr, 658 SymbolContextItem resolve_scope, 659 SymbolContext &sc) const { 660 // The address is already section offset so it has a module 661 uint32_t resolved_flags = 0; 662 ModuleSP module_sp(so_addr.GetModule()); 663 if (module_sp) { 664 resolved_flags = 665 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc); 666 } else { 667 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 668 collection::const_iterator pos, end = m_modules.end(); 669 for (pos = m_modules.begin(); pos != end; ++pos) { 670 resolved_flags = 671 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc); 672 if (resolved_flags != 0) 673 break; 674 } 675 } 676 677 return resolved_flags; 678 } 679 680 uint32_t ModuleList::ResolveSymbolContextForFilePath( 681 const char *file_path, uint32_t line, bool check_inlines, 682 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const { 683 FileSpec file_spec(file_path); 684 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, 685 resolve_scope, sc_list); 686 } 687 688 uint32_t ModuleList::ResolveSymbolContextsForFileSpec( 689 const FileSpec &file_spec, uint32_t line, bool check_inlines, 690 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const { 691 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 692 collection::const_iterator pos, end = m_modules.end(); 693 for (pos = m_modules.begin(); pos != end; ++pos) { 694 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines, 695 resolve_scope, sc_list); 696 } 697 698 return sc_list.GetSize(); 699 } 700 701 size_t ModuleList::GetIndexForModule(const Module *module) const { 702 if (module) { 703 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex); 704 collection::const_iterator pos; 705 collection::const_iterator begin = m_modules.begin(); 706 collection::const_iterator end = m_modules.end(); 707 for (pos = begin; pos != end; ++pos) { 708 if ((*pos).get() == module) 709 return std::distance(begin, pos); 710 } 711 } 712 return LLDB_INVALID_INDEX32; 713 } 714 715 namespace { 716 struct SharedModuleListInfo { 717 ModuleList module_list; 718 ModuleListProperties module_list_properties; 719 }; 720 } 721 static SharedModuleListInfo &GetSharedModuleListInfo() 722 { 723 static SharedModuleListInfo *g_shared_module_list_info = nullptr; 724 static llvm::once_flag g_once_flag; 725 llvm::call_once(g_once_flag, []() { 726 // NOTE: Intentionally leak the module list so a program doesn't have to 727 // cleanup all modules and object files as it exits. This just wastes time 728 // doing a bunch of cleanup that isn't required. 729 if (g_shared_module_list_info == nullptr) 730 g_shared_module_list_info = new SharedModuleListInfo(); 731 }); 732 return *g_shared_module_list_info; 733 } 734 735 static ModuleList &GetSharedModuleList() { 736 return GetSharedModuleListInfo().module_list; 737 } 738 739 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() { 740 return GetSharedModuleListInfo().module_list_properties; 741 } 742 743 bool ModuleList::ModuleIsInCache(const Module *module_ptr) { 744 if (module_ptr) { 745 ModuleList &shared_module_list = GetSharedModuleList(); 746 return shared_module_list.FindModule(module_ptr).get() != nullptr; 747 } 748 return false; 749 } 750 751 size_t ModuleList::FindSharedModules(const ModuleSpec &module_spec, 752 ModuleList &matching_module_list) { 753 return GetSharedModuleList().FindModules(module_spec, matching_module_list); 754 } 755 756 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) { 757 return GetSharedModuleList().RemoveOrphans(mandatory); 758 } 759 760 Status ModuleList::GetSharedModule(const ModuleSpec &module_spec, 761 ModuleSP &module_sp, 762 const FileSpecList *module_search_paths_ptr, 763 ModuleSP *old_module_sp_ptr, 764 bool *did_create_ptr, bool always_create) { 765 ModuleList &shared_module_list = GetSharedModuleList(); 766 std::lock_guard<std::recursive_mutex> guard( 767 shared_module_list.m_modules_mutex); 768 char path[PATH_MAX]; 769 770 Status error; 771 772 module_sp.reset(); 773 774 if (did_create_ptr) 775 *did_create_ptr = false; 776 if (old_module_sp_ptr) 777 old_module_sp_ptr->reset(); 778 779 const UUID *uuid_ptr = module_spec.GetUUIDPtr(); 780 const FileSpec &module_file_spec = module_spec.GetFileSpec(); 781 const ArchSpec &arch = module_spec.GetArchitecture(); 782 783 // Make sure no one else can try and get or create a module while this 784 // function is actively working on it by doing an extra lock on the global 785 // mutex list. 786 if (!always_create) { 787 ModuleList matching_module_list; 788 const size_t num_matching_modules = 789 shared_module_list.FindModules(module_spec, matching_module_list); 790 if (num_matching_modules > 0) { 791 for (size_t module_idx = 0; module_idx < num_matching_modules; 792 ++module_idx) { 793 module_sp = matching_module_list.GetModuleAtIndex(module_idx); 794 795 // Make sure the file for the module hasn't been modified 796 if (module_sp->FileHasChanged()) { 797 if (old_module_sp_ptr && !*old_module_sp_ptr) 798 *old_module_sp_ptr = module_sp; 799 800 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES)); 801 if (log != nullptr) 802 LLDB_LOGF(log, 803 "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