<rdar://problem/10103468>

I started work on being able to add symbol files after a debug session
had started with a new "target symfile add" command and quickly ran into
problems with stale Address objects in breakpoint locations that had 
lldb_private::Section pointers into modules that had been removed or 
replaced. This also let to grabbing stale modules from those sections. 
So I needed to thread harded the Address, Section and related objects.

To do this I modified the ModuleChild class to now require a ModuleSP
on initialization so that a weak reference can created. I also changed
all places that were handing out "Section *" to have them hand out SectionSP.
All ObjectFile, SymbolFile and SymbolVendors were inheriting from ModuleChild
so all of the find plug-in, static creation function and constructors now
require ModuleSP references instead of Module *. 

Address objects now have weak references to their sections which can
safely go stale when a module gets destructed. 

This checkin doesn't complete the "target symfile add" command, but it
does get us a lot clioser to being able to do such things without a high
risk of crashing or memory corruption.

llvm-svn: 151336
diff --git a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
index 8456ecd..1d9158a 100644
--- a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
+++ b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
@@ -263,7 +263,7 @@
 ObjectContainer *
 ObjectContainerBSDArchive::CreateInstance
 (
-    Module* module,
+    const lldb::ModuleSP &module_sp,
     DataBufferSP& data_sp,
     const FileSpec *file,
     addr_t offset,
@@ -275,13 +275,13 @@
     {
         Timer scoped_timer (__PRETTY_FUNCTION__,
                             "ObjectContainerBSDArchive::CreateInstance (module = %s/%s, file = %p, file_offset = 0x%z8.8x, file_size = 0x%z8.8x)",
-                            module->GetFileSpec().GetDirectory().AsCString(),
-                            module->GetFileSpec().GetFilename().AsCString(),
+                            module_sp->GetFileSpec().GetDirectory().AsCString(),
+                            module_sp->GetFileSpec().GetFilename().AsCString(),
                             file, offset, length);
 
-        Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module->GetArchitecture(), module->GetModificationTime()));
+        Archive::shared_ptr archive_sp (Archive::FindCachedArchive (*file, module_sp->GetArchitecture(), module_sp->GetModificationTime()));
 
-        std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module, data_sp, file, offset, length));
+        std::auto_ptr<ObjectContainerBSDArchive> container_ap(new ObjectContainerBSDArchive (module_sp, data_sp, file, offset, length));
 
         if (container_ap.get())
         {
@@ -316,13 +316,13 @@
 
 ObjectContainerBSDArchive::ObjectContainerBSDArchive
 (
-    Module* module,
+    const lldb::ModuleSP &module_sp,
     DataBufferSP& dataSP,
     const lldb_private::FileSpec *file,
     lldb::addr_t offset,
     lldb::addr_t size
 ) :
-    ObjectContainer (module, file, offset, size, dataSP),
+    ObjectContainer (module_sp, file, offset, size, dataSP),
     m_archive_sp ()
 {
 }
@@ -345,10 +345,14 @@
     {
         if (m_data.GetByteSize() > 0)
         {
-            m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
-                                                                 m_module->GetArchitecture(),
-                                                                 m_module->GetModificationTime(),
-                                                                 m_data);
+            ModuleSP module_sp (GetModule());
+            if (module_sp)
+            {
+                m_archive_sp = Archive::ParseAndCacheArchiveForFile (m_file,
+                                                                     module_sp->GetArchitecture(),
+                                                                     module_sp->GetModificationTime(),
+                                                                     m_data);
+            }
         }
     }
     return m_archive_sp.get() != NULL;
@@ -383,15 +387,19 @@
 ObjectFileSP
 ObjectContainerBSDArchive::GetObjectFile (const FileSpec *file)
 {
-    if (m_module->GetObjectName() && m_archive_sp)
+    ModuleSP module_sp (GetModule());
+    if (module_sp)
     {
-        Object *object = m_archive_sp->FindObject (m_module->GetObjectName());
-        if (object)
-            return ObjectFile::FindPlugin (m_module, 
-                                           file, 
-                                           object->ar_file_offset, 
-                                           object->ar_file_size, 
-                                           m_data.GetSharedDataBuffer());
+        if (module_sp->GetObjectName() && m_archive_sp)
+        {
+            Object *object = m_archive_sp->FindObject (module_sp->GetObjectName());
+            if (object)
+                return ObjectFile::FindPlugin (module_sp, 
+                                               file, 
+                                               object->ar_file_offset, 
+                                               object->ar_file_size, 
+                                               m_data.GetSharedDataBuffer());
+        }
     }
     return ObjectFileSP();
 }
diff --git a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
index 6501c5a..a072119 100644
--- a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
+++ b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h
@@ -39,7 +39,7 @@
     GetPluginDescriptionStatic();
 
     static lldb_private::ObjectContainer *
-    CreateInstance (lldb_private::Module* module,
+    CreateInstance (const lldb::ModuleSP &module_sp,
                     lldb::DataBufferSP& dataSP,
                     const lldb_private::FileSpec *file,
                     lldb::addr_t offset,
@@ -51,7 +51,7 @@
     //------------------------------------------------------------------
     // Member Functions
     //------------------------------------------------------------------
-    ObjectContainerBSDArchive (lldb_private::Module* module,
+    ObjectContainerBSDArchive (const lldb::ModuleSP &module_sp,
                                lldb::DataBufferSP& dataSP,
                                const lldb_private::FileSpec *file,
                                lldb::addr_t offset,
diff --git a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp
index bd257dc..1abb029 100644
--- a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp
+++ b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.cpp
@@ -50,7 +50,7 @@
 ObjectContainer *
 ObjectContainerUniversalMachO::CreateInstance
 (
-    Module* module,
+    const lldb::ModuleSP &module_sp,
     DataBufferSP& data_sp,
     const FileSpec *file,
     addr_t offset,
@@ -61,7 +61,7 @@
     data.SetData (data_sp, offset, length);
     if (ObjectContainerUniversalMachO::MagicBytesMatch(data))
     {
-        std::auto_ptr<ObjectContainerUniversalMachO> container_ap(new ObjectContainerUniversalMachO (module, data_sp, file, offset, length));
+        std::auto_ptr<ObjectContainerUniversalMachO> container_ap(new ObjectContainerUniversalMachO (module_sp, data_sp, file, offset, length));
         if (container_ap->ParseHeader())
         {
             return container_ap.release();
@@ -82,13 +82,13 @@
 
 ObjectContainerUniversalMachO::ObjectContainerUniversalMachO
 (
-    Module* module,
+    const lldb::ModuleSP &module_sp,
     DataBufferSP& dataSP,
     const FileSpec *file,
     addr_t offset,
     addr_t length
 ) :
-    ObjectContainer (module, file, offset, length, dataSP),
+    ObjectContainer (module_sp, file, offset, length, dataSP),
     m_header(),
     m_fat_archs()
 {
@@ -190,27 +190,31 @@
     ArchSpec arch;
     // If the module hasn't specified an architecture yet, set it to the default 
     // architecture:
-    if (!m_module->GetArchitecture().IsValid())
+    ModuleSP module_sp (GetModule());
+    if (module_sp)
     {
-        arch = Target::GetDefaultArchitecture ();
-        if (!arch.IsValid())
-            arch.SetTriple (LLDB_ARCH_DEFAULT, NULL);
-    }
-    else
-        arch = m_module->GetArchitecture();
-        
-    ArchSpec curr_arch;
-    for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx)
-    {
-        if (GetArchitectureAtIndex (arch_idx, curr_arch))
+        if (!module_sp->GetArchitecture().IsValid())
         {
-            if (arch == curr_arch)
+            arch = Target::GetDefaultArchitecture ();
+            if (!arch.IsValid())
+                arch.SetTriple (LLDB_ARCH_DEFAULT, NULL);
+        }
+        else
+            arch = module_sp->GetArchitecture();
+            
+        ArchSpec curr_arch;
+        for (arch_idx = 0; arch_idx < m_header.nfat_arch; ++arch_idx)
+        {
+            if (GetArchitectureAtIndex (arch_idx, curr_arch))
             {
-                return ObjectFile::FindPlugin (m_module, 
-                                               file, 
-                                               m_offset + m_fat_archs[arch_idx].offset, 
-                                               m_fat_archs[arch_idx].size,
-                                               m_data.GetSharedDataBuffer());
+                if (arch == curr_arch)
+                {
+                    return ObjectFile::FindPlugin (module_sp, 
+                                                   file, 
+                                                   m_offset + m_fat_archs[arch_idx].offset, 
+                                                   m_fat_archs[arch_idx].size,
+                                                   m_data.GetSharedDataBuffer());
+                }
             }
         }
     }
diff --git a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h
index d63440a..088f587 100644
--- a/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h
+++ b/lldb/source/Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h
@@ -35,7 +35,7 @@
     GetPluginDescriptionStatic();
 
     static lldb_private::ObjectContainer *
-    CreateInstance (lldb_private::Module* module,
+    CreateInstance (const lldb::ModuleSP &module_sp,
                     lldb::DataBufferSP& dataSP,
                     const lldb_private::FileSpec *file,
                     lldb::addr_t offset,
@@ -47,7 +47,7 @@
     //------------------------------------------------------------------
     // Member Functions
     //------------------------------------------------------------------
-    ObjectContainerUniversalMachO (lldb_private::Module* module,
+    ObjectContainerUniversalMachO (const lldb::ModuleSP &module_sp,
                                    lldb::DataBufferSP& dataSP,
                                    const lldb_private::FileSpec *file,
                                    lldb::addr_t offset,