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