Index: lldb/include/lldb/Core/ModuleList.h =================================================================== --- lldb/include/lldb/Core/ModuleList.h +++ lldb/include/lldb/Core/ModuleList.h @@ -237,20 +237,6 @@ /// \see ModuleList::GetSize() Module *GetModulePointerAtIndex(size_t idx) const; - /// Get the module pointer for the module at index \a idx without acquiring - /// the ModuleList mutex. This MUST already have been acquired with - /// ModuleList::GetMutex and locked for this call to be safe. - /// - /// \param[in] idx - /// An index into this module collection. - /// - /// \return - /// A pointer to a Module which can by nullptr if \a idx is out - /// of range. - /// - /// \see ModuleList::GetSize() - Module *GetModulePointerAtIndexUnlocked(size_t idx) const; - /// Find compile units by partial or full path. /// /// Finds all compile units that match \a path in all of the modules and @@ -491,11 +477,13 @@ typedef LockingAdaptedIterable ModuleIterable; - ModuleIterable Modules() { return ModuleIterable(m_modules, GetMutex()); } + ModuleIterable Modules() const { + return ModuleIterable(m_modules, GetMutex()); + } typedef AdaptedIterable ModuleIterableNoLocking; - ModuleIterableNoLocking ModulesNoLocking() { + ModuleIterableNoLocking ModulesNoLocking() const { return ModuleIterableNoLocking(m_modules); } }; Index: lldb/include/lldb/Utility/Iterable.h =================================================================== --- lldb/include/lldb/Utility/Iterable.h +++ lldb/include/lldb/Utility/Iterable.h @@ -170,7 +170,7 @@ typename MutexType> class LockingAdaptedIterable : public AdaptedIterable { public: - LockingAdaptedIterable(C &container, MutexType &mutex) + LockingAdaptedIterable(const C &container, MutexType &mutex) : AdaptedIterable(container), m_mutex(&mutex) { m_mutex->lock(); } Index: lldb/source/Breakpoint/Breakpoint.cpp =================================================================== --- lldb/source/Breakpoint/Breakpoint.cpp +++ lldb/source/Breakpoint/Breakpoint.cpp @@ -508,7 +508,6 @@ "delete_locations: %i\n", module_list.GetSize(), load, delete_locations); - std::lock_guard guard(module_list.GetMutex()); if (load) { // The logic for handling new modules is: // 1) If the filter rejects this module, then skip it. 2) Run through the @@ -525,7 +524,7 @@ // them after the locations pass. Have to do it this way because resolving // breakpoints will add new locations potentially. - for (ModuleSP module_sp : module_list.ModulesNoLocking()) { + for (ModuleSP module_sp : module_list.Modules()) { bool seen = false; if (!m_filter_sp->ModulePasses(module_sp)) continue; @@ -589,9 +588,7 @@ else removed_locations_event = nullptr; - size_t num_modules = module_list.GetSize(); - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(i)); + for (ModuleSP module_sp : module_list.Modules()) { if (m_filter_sp->ModulePasses(module_sp)) { size_t loc_idx = 0; size_t num_locations = m_locations.GetSize(); Index: lldb/source/Commands/CommandObjectTarget.cpp =================================================================== --- lldb/source/Commands/CommandObjectTarget.cpp +++ lldb/source/Commands/CommandObjectTarget.cpp @@ -1406,19 +1406,18 @@ strm.Printf("Dumping headers for %" PRIu64 " module(s).\n", static_cast(num_modules)); strm.IndentMore(); - for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { - Module *module = module_list.GetModulePointerAtIndexUnlocked(image_idx); - if (module) { + for (ModuleSP module_sp : module_list.ModulesNoLocking()) { + if (module_sp) { if (num_dumped++ > 0) { strm.EOL(); strm.EOL(); } - ObjectFile *objfile = module->GetObjectFile(); + ObjectFile *objfile = module_sp->GetObjectFile(); if (objfile) objfile->Dump(&strm); else { strm.Format("No object file for module: {0:F}\n", - module->GetFileSpec()); + module_sp->GetFileSpec()); } } } @@ -2025,14 +2024,14 @@ if (command.GetArgumentCount() == 0) { // Dump all sections for all modules images - std::lock_guard guard( - target->GetImages().GetMutex()); - const size_t num_modules = target->GetImages().GetSize(); + const ModuleList &module_list = target->GetImages(); + std::lock_guard guard(module_list.GetMutex()); + const size_t num_modules = module_list.GetSize(); if (num_modules > 0) { result.GetOutputStream().Printf("Dumping symbol table for %" PRIu64 " modules.\n", (uint64_t)num_modules); - for (size_t image_idx = 0; image_idx < num_modules; ++image_idx) { + for (ModuleSP module_sp : module_list.ModulesNoLocking()) { if (num_dumped > 0) { result.GetOutputStream().EOL(); result.GetOutputStream().EOL(); @@ -2040,10 +2039,9 @@ if (m_interpreter.WasInterrupted()) break; num_dumped++; - DumpModuleSymtab( - m_interpreter, result.GetOutputStream(), - target->GetImages().GetModulePointerAtIndexUnlocked(image_idx), - m_options.m_sort_order, name_preference); + DumpModuleSymtab(m_interpreter, result.GetOutputStream(), + module_sp.get(), m_options.m_sort_order, + name_preference); } } else { result.AppendError("the target has no associated executable images"); @@ -2060,9 +2058,8 @@ const size_t num_matches = FindModulesByName(target, arg_cstr, module_list, true); if (num_matches > 0) { - for (size_t i = 0; i < num_matches; ++i) { - Module *module = module_list.GetModulePointerAtIndex(i); - if (module) { + for (ModuleSP module_sp : module_list.Modules()) { + if (module_sp) { if (num_dumped > 0) { result.GetOutputStream().EOL(); result.GetOutputStream().EOL(); @@ -2070,8 +2067,9 @@ if (m_interpreter.WasInterrupted()) break; num_dumped++; - DumpModuleSymtab(m_interpreter, result.GetOutputStream(), module, - m_options.m_sort_order, name_preference); + DumpModuleSymtab(m_interpreter, result.GetOutputStream(), + module_sp.get(), m_options.m_sort_order, + name_preference); } } } else @@ -2283,12 +2281,10 @@ result.GetOutputStream().Printf("Dumping debug symbols for %" PRIu64 " modules.\n", (uint64_t)num_modules); - for (uint32_t image_idx = 0; image_idx < num_modules; ++image_idx) { + for (ModuleSP module_sp : target_modules.ModulesNoLocking()) { if (m_interpreter.WasInterrupted()) break; - if (DumpModuleSymbolFile( - result.GetOutputStream(), - target_modules.GetModulePointerAtIndexUnlocked(image_idx))) + if (DumpModuleSymbolFile(result.GetOutputStream(), module_sp.get())) num_dumped++; } } else { @@ -2376,12 +2372,11 @@ const size_t num_modules = target_modules.GetSize(); if (num_modules > 0) { uint32_t num_dumped = 0; - for (uint32_t i = 0; i < num_modules; ++i) { + for (ModuleSP module_sp : target_modules.ModulesNoLocking()) { if (m_interpreter.WasInterrupted()) break; if (DumpCompileUnitLineTable( - m_interpreter, result.GetOutputStream(), - target_modules.GetModulePointerAtIndexUnlocked(i), + m_interpreter, result.GetOutputStream(), module_sp.get(), file_spec, m_options.m_verbose ? eDescriptionLevelFull : eDescriptionLevelBrief)) @@ -3905,14 +3900,10 @@ std::lock_guard guard(target_modules.GetMutex()); const size_t num_modules = target_modules.GetSize(); if (num_modules > 0) { - for (i = 0; i < num_modules && !syntax_error; ++i) { - Module *module_pointer = - target_modules.GetModulePointerAtIndexUnlocked(i); - - if (module_pointer != current_module.get() && - LookupInModule(m_interpreter, - target_modules.GetModulePointerAtIndexUnlocked(i), - result, syntax_error)) { + for (ModuleSP module_sp : target_modules.ModulesNoLocking()) { + if (module_sp != current_module && + LookupInModule(m_interpreter, module_sp.get(), result, + syntax_error)) { result.GetOutputStream().EOL(); num_successful_lookups++; } Index: lldb/source/Core/ModuleList.cpp =================================================================== --- lldb/source/Core/ModuleList.cpp +++ lldb/source/Core/ModuleList.cpp @@ -346,10 +346,6 @@ Module *ModuleList::GetModulePointerAtIndex(size_t idx) const { std::lock_guard guard(m_modules_mutex); - return GetModulePointerAtIndexUnlocked(idx); -} - -Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const { if (idx < m_modules.size()) return m_modules[idx].get(); return nullptr; Index: lldb/source/Core/SearchFilter.cpp =================================================================== --- lldb/source/Core/SearchFilter.cpp +++ lldb/source/Core/SearchFilter.cpp @@ -228,11 +228,7 @@ return; } - std::lock_guard guard(modules.GetMutex()); - const size_t numModules = modules.GetSize(); - - for (size_t i = 0; i < numModules; i++) { - ModuleSP module_sp(modules.GetModuleAtIndexUnlocked(i)); + for (ModuleSP module_sp : modules.Modules()) { if (!ModulePasses(module_sp)) continue; if (DoModuleIteration(module_sp, searcher) == Searcher::eCallbackReturnStop) @@ -262,14 +258,9 @@ return Searcher::eCallbackReturnContinue; } - const ModuleList &target_images = m_target_sp->GetImages(); - std::lock_guard guard(target_images.GetMutex()); - - size_t n_modules = target_images.GetSize(); - for (size_t i = 0; i < n_modules; i++) { + for (ModuleSP module_sp : m_target_sp->GetImages().Modules()) { // If this is the last level supplied, then call the callback directly, // otherwise descend. - ModuleSP module_sp(target_images.GetModuleAtIndexUnlocked(i)); if (!ModulePasses(module_sp)) continue; @@ -434,11 +425,9 @@ const ModuleList &target_modules = m_target_sp->GetImages(); std::lock_guard guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); - for (size_t i = 0; i < num_modules; i++) { - Module *module = target_modules.GetModulePointerAtIndexUnlocked(i); - if (FileSpec::Match(m_module_spec, module->GetFileSpec())) { - SymbolContext matchingContext(m_target_sp, module->shared_from_this()); + for (ModuleSP module_sp : m_target_sp->GetImages().Modules()) { + if (FileSpec::Match(m_module_spec, module_sp->GetFileSpec())) { + SymbolContext matchingContext(m_target_sp, module_sp); Searcher::CallbackReturn shouldContinue; shouldContinue = DoModuleIteration(matchingContext, searcher); @@ -550,17 +539,11 @@ // If the module file spec is a full path, then we can just find the one // filespec that passes. Otherwise, we need to go through all modules and // find the ones that match the file name. - - const ModuleList &target_modules = m_target_sp->GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - - const size_t num_modules = target_modules.GetSize(); - for (size_t i = 0; i < num_modules; i++) { - Module *module = target_modules.GetModulePointerAtIndexUnlocked(i); - if (m_module_spec_list.FindFileIndex(0, module->GetFileSpec(), false) == + for (ModuleSP module_sp : m_target_sp->GetImages().Modules()) { + if (m_module_spec_list.FindFileIndex(0, module_sp->GetFileSpec(), false) == UINT32_MAX) continue; - SymbolContext matchingContext(m_target_sp, module->shared_from_this()); + SymbolContext matchingContext(m_target_sp, module_sp); Searcher::CallbackReturn shouldContinue; shouldContinue = DoModuleIteration(matchingContext, searcher); @@ -752,13 +735,9 @@ // find the ones that match the file name. ModuleList matching_modules; - const ModuleList &target_images = m_target_sp->GetImages(); - std::lock_guard guard(target_images.GetMutex()); - const size_t num_modules = target_images.GetSize(); bool no_modules_in_filter = m_module_spec_list.GetSize() == 0; - for (size_t i = 0; i < num_modules; i++) { - lldb::ModuleSP module_sp = target_images.GetModuleAtIndexUnlocked(i); + for (ModuleSP module_sp : m_target_sp->GetImages().Modules()) { if (!no_modules_in_filter && m_module_spec_list.FindFileIndex(0, module_sp->GetFileSpec(), false) == UINT32_MAX) Index: lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp =================================================================== --- lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp +++ lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderDarwin.cpp @@ -216,15 +216,11 @@ const ModuleList &target_modules = target.GetImages(); std::lock_guard guard(target_modules.GetMutex()); - size_t num_modules = target_modules.GetSize(); ModuleSP dyld_sp(GetDYLDModule()); - - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); - + for (ModuleSP module_sp : target_modules.Modules()) { // Don't remove dyld - else we'll lose our breakpoint notifying us about // libraries being re-loaded... - if (module_sp.get() != nullptr && module_sp.get() != dyld_sp.get()) { + if (module_sp && module_sp != dyld_sp) { UnloadSections(module_sp); unloaded_modules_list.Append(module_sp); } Index: lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp =================================================================== --- lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp +++ lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOS.cpp @@ -401,18 +401,15 @@ Status DynamicLoaderMacOS::CanLoadImage() { Status error; addr_t symbol_address = LLDB_INVALID_ADDRESS; - Target &target = m_process->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); ConstString g_libdyld_name("libdyld.dylib"); + Target &target = m_process->GetTarget(); + const size_t num_modules = target.GetImages().GetSize(); // Find any modules named "libdyld.dylib" and look for the symbol there first - for (size_t i = 0; i < num_modules; i++) { - Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); - if (module_pointer) { - if (module_pointer->GetFileSpec().GetFilename() == g_libdyld_name) { - symbol_address = GetDyldLockVariableAddressFromModule(module_pointer); + for (ModuleSP module_sp : target.GetImages().Modules()) { + if (module_sp) { + if (module_sp->GetFileSpec().GetFilename() == g_libdyld_name) { + symbol_address = GetDyldLockVariableAddressFromModule(module_sp.get()); if (symbol_address != LLDB_INVALID_ADDRESS) break; } @@ -421,12 +418,10 @@ // Search through all modules looking for the symbol in them if (symbol_address == LLDB_INVALID_ADDRESS) { - for (size_t i = 0; i < num_modules; i++) { - Module *module_pointer = - target_modules.GetModulePointerAtIndexUnlocked(i); - if (module_pointer) { + for (ModuleSP module_sp : target.GetImages().Modules()) { + if (module_sp) { addr_t symbol_address = - GetDyldLockVariableAddressFromModule(module_pointer); + GetDyldLockVariableAddressFromModule(module_sp.get()); if (symbol_address != LLDB_INVALID_ADDRESS) break; } Index: lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp =================================================================== --- lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp +++ lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp @@ -728,13 +728,8 @@ // DYLD_*_PATH pointed to an equivalent version. We don't want it to stay // in the target's module list or it will confuse us, so unload it here. Target &target = m_process->GetTarget(); - const ModuleList &target_modules = target.GetImages(); ModuleList not_loaded_modules; - std::lock_guard guard(target_modules.GetMutex()); - - size_t num_modules = target_modules.GetSize(); - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); + for (ModuleSP module_sp : target.GetImages().Modules()) { if (!module_sp->IsLoadedInTarget(&target)) { if (log) { StreamString s; Index: lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp =================================================================== --- lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp +++ lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp @@ -83,11 +83,7 @@ // Disable JIT for static dynamic loader targets m_process->SetCanJIT(false); - std::lock_guard guard(module_list.GetMutex()); - - const size_t num_modules = module_list.GetSize(); - for (uint32_t idx = 0; idx < num_modules; ++idx) { - ModuleSP module_sp(module_list.GetModuleAtIndexUnlocked(idx)); + for (ModuleSP module_sp : module_list.Modules()) { if (module_sp) { bool changed = false; ObjectFile *image_object_file = module_sp->GetObjectFile(); Index: lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp =================================================================== --- lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp +++ lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp @@ -690,12 +690,7 @@ return; } - const ModuleList &target_images = m_target->GetImages(); - std::lock_guard guard(target_images.GetMutex()); - - for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { - lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); - + for (lldb::ModuleSP image : m_target->GetImages().Modules()) { if (!image) continue; @@ -1667,14 +1662,8 @@ module_sp->GetFileSpec().GetFilename()); } } else { - const ModuleList &target_images = m_target->GetImages(); - std::lock_guard guard(target_images.GetMutex()); - CompilerDeclContext null_namespace_decl; - - for (size_t i = 0, e = target_images.GetSize(); i < e; ++i) { - lldb::ModuleSP image = target_images.GetModuleAtIndexUnlocked(i); - + for (lldb::ModuleSP image : m_target->GetImages().Modules()) { if (!image) continue; Index: lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp +++ lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp @@ -377,12 +377,7 @@ llvm::Triple::VendorType::Apple) return ObjCRuntimeVersions::eObjC_VersionUnknown; - const ModuleList &target_modules = target.GetImages(); - std::lock_guard gaurd(target_modules.GetMutex()); - - size_t num_images = target_modules.GetSize(); - for (size_t i = 0; i < num_images; i++) { - ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i); + for (ModuleSP module_sp : target.GetImages().Modules()) { // One tricky bit here is that we might get called as part of the initial // module loading, but before all the pre-run libraries get winnowed from // the module list. So there might actually be an old and incorrect ObjC Index: lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp +++ lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp @@ -1590,10 +1590,7 @@ ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process); if (objc_runtime) { - const ModuleList &images = process->GetTarget().GetImages(); - std::lock_guard guard(images.GetMutex()); - for (size_t i = 0; i < images.GetSize(); ++i) { - lldb::ModuleSP mod_sp = images.GetModuleAtIndexUnlocked(i); + for (lldb::ModuleSP mod_sp : process->GetTarget().GetImages().Modules()) { if (objc_runtime->IsModuleObjCLibrary(mod_sp)) { const Symbol *symbol = mod_sp->FindFirstSymbolWithNameAndType(g_class_getNameRaw_symbol_name, Index: lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp =================================================================== --- lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp +++ lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp @@ -452,15 +452,11 @@ if (process_sp) { Target &target = process_sp->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - size_t num_modules = target_modules.GetSize(); if (!m_objc_module_sp) { - for (size_t i = 0; i < num_modules; i++) { + for (ModuleSP module_sp : target.GetImages().Modules()) { if (ObjCLanguageRuntime::Get(*process_sp) - ->IsModuleObjCLibrary( - target_modules.GetModuleAtIndexUnlocked(i))) { - m_objc_module_sp = target_modules.GetModuleAtIndexUnlocked(i); + ->IsModuleObjCLibrary(module_sp)) { + m_objc_module_sp = module_sp; break; } } Index: lldb/source/Plugins/MemoryHistory/asan/MemoryHistoryASan.cpp =================================================================== --- lldb/source/Plugins/MemoryHistory/asan/MemoryHistoryASan.cpp +++ lldb/source/Plugins/MemoryHistory/asan/MemoryHistoryASan.cpp @@ -36,13 +36,8 @@ Target &target = process_sp->GetTarget(); - const ModuleList &target_modules = target.GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - const size_t num_modules = target_modules.GetSize(); - for (size_t i = 0; i < num_modules; ++i) { - Module *module_pointer = target_modules.GetModulePointerAtIndexUnlocked(i); - - const Symbol *symbol = module_pointer->FindFirstSymbolWithNameAndType( + for (ModuleSP module_sp : target.GetImages().Modules()) { + const Symbol *symbol = module_sp->FindFirstSymbolWithNameAndType( ConstString("__asan_get_alloc_stack"), lldb::eSymbolTypeAny); if (symbol != nullptr) Index: lldb/source/Target/Process.cpp =================================================================== --- lldb/source/Target/Process.cpp +++ lldb/source/Target/Process.cpp @@ -3076,13 +3076,8 @@ } } // Figure out which one is the executable, and set that in our target: - const ModuleList &target_modules = GetTarget().GetImages(); - std::lock_guard guard(target_modules.GetMutex()); - size_t num_modules = target_modules.GetSize(); ModuleSP new_executable_module_sp; - - for (size_t i = 0; i < num_modules; i++) { - ModuleSP module_sp(target_modules.GetModuleAtIndexUnlocked(i)); + for (ModuleSP module_sp : GetTarget().GetImages().Modules()) { if (module_sp && module_sp->IsExecutable()) { if (GetTarget().GetExecutableModulePointer() != module_sp.get()) new_executable_module_sp = module_sp;