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