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