We were accessing the ModuleList in the target without locking it for tasks like
setting breakpoints.  That's dangerous, since while we are setting a breakpoint,
the target might hit the dyld load notification, and start removing modules from
the list.  This change adds a GetMutex accessor to the ModuleList class, and
uses it whenever we are accessing the target's ModuleList (as returned by GetImages().)

<rdar://problem/11552372>

llvm-svn: 157668
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
index c8e9cec..c42f469 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
@@ -1057,12 +1057,14 @@
         // 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();
-        ModuleList &modules = target.GetImages();
+        ModuleList &target_modules = target.GetImages();
         ModuleList not_loaded_modules;
-        size_t num_modules = modules.GetSize();
+        Mutex::Locker modules_locker(target_modules.GetMutex());
+        
+        size_t num_modules = target_modules.GetSize();
         for (size_t i = 0; i < num_modules; i++)
         {
-            ModuleSP module_sp = modules.GetModuleAtIndex(i);
+            ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked (i);
             if (!module_sp->IsLoadedInTarget (&target))
             {
                 if (log)
diff --git a/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp b/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp
index a1d6442..abfbf1c 100644
--- a/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp
+++ b/lldb/source/Plugins/DynamicLoader/Static/DynamicLoaderStatic.cpp
@@ -97,10 +97,12 @@
     
     ModuleList loaded_module_list;
 
+    Mutex::Locker mutex_locker(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.GetModuleAtIndex (idx));
+        ModuleSP module_sp (module_list.GetModuleAtIndexUnlocked (idx));
         if (module_sp)
         {
             bool changed = false;
diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
index 1cb7ab8..a2bc888 100644
--- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
@@ -263,11 +263,13 @@
         return eObjC_VersionUnknown;
         
     Target &target = process->GetTarget();
-    ModuleList &images = target.GetImages();
-    size_t num_images = images.GetSize();
+    ModuleList &target_modules = target.GetImages();
+    Mutex::Locker modules_locker(target_modules.GetMutex());
+    
+    size_t num_images = target_modules.GetSize();
     for (size_t i = 0; i < num_images; i++)
     {
-        ModuleSP module_sp = images.GetModuleAtIndex(i);
+        ModuleSP module_sp = target_modules.GetModuleAtIndexUnlocked(i);
         // 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 library sitting around in the list, and we don't want to look at that.
diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCSymbolVendor.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCSymbolVendor.cpp
index ff13580..00c90c6 100644
--- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCSymbolVendor.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCSymbolVendor.cpp
@@ -43,13 +43,14 @@
     
     uint32_t ret = 0;
     
-    ModuleList &images = m_process->GetTarget().GetImages();
+    ModuleList &target_modules = m_process->GetTarget().GetImages();
+    Mutex::Locker modules_locker(target_modules.GetMutex());
     
-    for (size_t image_index = 0, end_index = images.GetSize();
+    for (size_t image_index = 0, end_index = target_modules.GetSize();
          image_index < end_index;
          ++image_index)
     {
-        Module *image = images.GetModulePointerAtIndex(image_index);
+        Module *image = target_modules.GetModulePointerAtIndexUnlocked(image_index);
         
         if (!image)
             continue;
diff --git a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp
index f8cab06..54eda8b 100644
--- a/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCTrampolineHandler.cpp
@@ -334,15 +334,16 @@
         return true;
     Target &target = m_process_sp->GetTarget();
     
-    ModuleList &modules = target.GetImages();
-    size_t num_modules = modules.GetSize();
+    ModuleList &target_modules = target.GetImages();
+    Mutex::Locker modules_locker(target_modules.GetMutex());
+    size_t num_modules = target_modules.GetSize();
     if (!m_objc_module_sp)
     {
         for (size_t i = 0; i < num_modules; i++)
         {
-            if (m_process_sp->GetObjCLanguageRuntime()->IsModuleObjCLibrary (modules.GetModuleAtIndex(i)))
+            if (m_process_sp->GetObjCLanguageRuntime()->IsModuleObjCLibrary (target_modules.GetModuleAtIndexUnlocked(i)))
             {
-                m_objc_module_sp = modules.GetModuleAtIndex(i);
+                m_objc_module_sp = target_modules.GetModuleAtIndexUnlocked(i);
                 break;
             }
         }