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