The DynamicLoader plug-in instance now lives up in lldb_private::Process where
it should live and the lldb_private::Process takes care of managing the 
auto pointer to the dynamic loader instance.

Also, now that the ArchSpec contains the target triple, we are able to 
correctly set the Target architecture in DidLaunch/DidAttach in the subclasses,
and then the lldb_private::Process will find the dynamic loader plug-in 
by letting the dynamic loader plug-ins inspect the arch/triple in the target.

So now the ProcessGDBRemote plug-in is another step closer to be purely 
process/platform agnostic.

I updated the ProcessMacOSX and the ProcessLinux plug-ins accordingly.

llvm-svn: 125650
diff --git a/lldb/source/Core/DynamicLoader.cpp b/lldb/source/Core/DynamicLoader.cpp
index a207e9f..fa00e77 100644
--- a/lldb/source/Core/DynamicLoader.cpp
+++ b/lldb/source/Core/DynamicLoader.cpp
@@ -23,7 +23,7 @@
         create_callback  = PluginManager::GetDynamicLoaderCreateCallbackForPluginName (plugin_name);
         if (create_callback)
         {
-            std::auto_ptr<DynamicLoader> instance_ap(create_callback(process));
+            std::auto_ptr<DynamicLoader> instance_ap(create_callback(process, true));
             if (instance_ap.get())
                 return instance_ap.release();
         }
@@ -32,7 +32,7 @@
     {
         for (uint32_t idx = 0; (create_callback = PluginManager::GetDynamicLoaderCreateCallbackAtIndex(idx)) != NULL; ++idx)
         {
-            std::auto_ptr<DynamicLoader> instance_ap(create_callback(process));
+            std::auto_ptr<DynamicLoader> instance_ap(create_callback(process, false));
             if (instance_ap.get())
                 return instance_ap.release();
         }
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 64d1235..20709de 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -261,20 +261,20 @@
             
             if (is_64_bit_capable)
             {
+#if defined (__i386__) || defined (__x86_64__)
+                if (cpusubtype == CPU_SUBTYPE_486)
+                    cpusubtype = CPU_SUBTYPE_I386_ALL;
+#endif
                 if (cputype & CPU_ARCH_ABI64)
                 {
                     // We have a 64 bit kernel on a 64 bit system
-                    g_host_arch_32.SetMachOArch (CPU_TYPE_I386, CPU_SUBTYPE_386);
+                    g_host_arch_32.SetMachOArch (~(CPU_ARCH_MASK) & cputype, cpusubtype);
                     g_host_arch_64.SetMachOArch (cputype, cpusubtype);
                 }
                 else
                 {
                     // We have a 32 bit kernel on a 64 bit system
                     g_host_arch_32.SetMachOArch (cputype, cpusubtype);
-#if defined (__i386__) || defined (__x86_64__)
-                    if (cpusubtype == CPU_SUBTYPE_486)
-                        cpusubtype = CPU_SUBTYPE_I386_ALL;
-#endif
                     cputype |= CPU_ARCH_ABI64;
                     g_host_arch_64.SetMachOArch (cputype, cpusubtype);
                 }
diff --git a/lldb/source/Plugins/DynamicLoader/Linux-DYLD/DynamicLoaderLinuxDYLD.cpp b/lldb/source/Plugins/DynamicLoader/Linux-DYLD/DynamicLoaderLinuxDYLD.cpp
index 0802ff6..78ebe4d 100644
--- a/lldb/source/Plugins/DynamicLoader/Linux-DYLD/DynamicLoaderLinuxDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/Linux-DYLD/DynamicLoaderLinuxDYLD.cpp
@@ -73,9 +73,19 @@
 }
 
 DynamicLoader *
-DynamicLoaderLinuxDYLD::CreateInstance(Process *process)
+DynamicLoaderLinuxDYLD::CreateInstance(Process *process, bool force)
 {
-    return new DynamicLoaderLinuxDYLD(process);
+    bool create = force;
+    if (!create)
+    {
+        const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
+        if (triple_ref.getOS() == llvm::Triple::Linux)
+            create = true;
+    }
+    
+    if (create)
+        return new DynamicLoaderLinuxDYLD (process);
+    return NULL;
 }
 
 DynamicLoaderLinuxDYLD::DynamicLoaderLinuxDYLD(Process *process)
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
index 003ed9a..67958c9 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.cpp
@@ -63,9 +63,19 @@
 // allows the lldb to instantiate an instance of this class.
 //----------------------------------------------------------------------
 DynamicLoader *
-DynamicLoaderMacOSXDYLD::CreateInstance (Process* process)
+DynamicLoaderMacOSXDYLD::CreateInstance (Process* process, bool force)
 {
-    return new DynamicLoaderMacOSXDYLD (process);
+    bool create = force;
+    if (!create)
+    {
+        const llvm::Triple &triple_ref = process->GetTarget().GetArchitecture().GetTriple();
+        if (triple_ref.getOS() == llvm::Triple::Darwin && triple_ref.getVendor() == llvm::Triple::Apple)
+            create = true;
+    }
+    
+    if (create)
+        return new DynamicLoaderMacOSXDYLD (process);
+    return NULL;
 }
 
 //----------------------------------------------------------------------
diff --git a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
index eee6bc1..d557510 100644
--- a/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
+++ b/lldb/source/Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h
@@ -44,7 +44,7 @@
     GetPluginDescriptionStatic();
 
     static lldb_private::DynamicLoader *
-    CreateInstance (lldb_private::Process *process);
+    CreateInstance (lldb_private::Process *process, bool force);
 
     DynamicLoaderMacOSXDYLD (lldb_private::Process *process);
 
diff --git a/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp
index fa5b5b7..9939fe7 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp
+++ b/lldb/source/Plugins/Process/Linux/ProcessLinux.cpp
@@ -106,12 +106,6 @@
 ProcessLinux::WillLaunch(Module* module)
 {
     Error error;
-
-    m_dyld_ap.reset(DynamicLoader::FindPlugin(this, "dynamic-loader.linux-dyld"));
-    if (m_dyld_ap.get() == NULL)
-        error.SetErrorString("unable to find the dynamic loader named "
-                             "'dynamic-loader.linux-dyld'");
-
     return error;
 }
 
@@ -146,8 +140,6 @@
 void
 ProcessLinux::DidLaunch()
 {
-    if (m_dyld_ap.get() != NULL)
-        m_dyld_ap->DidLaunch();
 }
 
 Error
@@ -405,12 +397,6 @@
     return m_byte_order;
 }
 
-DynamicLoader *
-ProcessLinux::GetDynamicLoader()
-{
-    return m_dyld_ap.get();
-}
-
 //------------------------------------------------------------------------------
 // ProcessInterface protocol.
 
diff --git a/lldb/source/Plugins/Process/Linux/ProcessLinux.h b/lldb/source/Plugins/Process/Linux/ProcessLinux.h
index 6fe0552..18e5aa7 100644
--- a/lldb/source/Plugins/Process/Linux/ProcessLinux.h
+++ b/lldb/source/Plugins/Process/Linux/ProcessLinux.h
@@ -138,9 +138,6 @@
     virtual lldb::addr_t
     GetImageInfoAddress();
 
-    virtual lldb_private::DynamicLoader *
-    GetDynamicLoader();
-
     //------------------------------------------------------------------
     // PluginInterface protocol
     //------------------------------------------------------------------
@@ -186,9 +183,6 @@
     lldb_private::Mutex m_message_mutex;
     std::queue<ProcessMessage> m_message_queue;
 
-    /// Dynamic loader plugin associated with this process.
-    std::auto_ptr<lldb_private::DynamicLoader> m_dyld_ap;
-
     /// Updates the loaded sections provided by the executable.
     ///
     /// FIXME:  It would probably be better to delegate this task to the
diff --git a/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.cpp b/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.cpp
index 5318014..e4be273 100644
--- a/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.cpp
+++ b/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.cpp
@@ -234,8 +234,7 @@
     m_stdout_data (),
     m_exception_messages (),
     m_exception_messages_mutex (Mutex::eMutexTypeRecursive),
-    m_arch_spec (),
-    m_dynamic_loader_ap ()
+    m_arch_spec ()
 {
 }
 
@@ -414,14 +413,7 @@
 Error
 ProcessMacOSX::WillLaunchOrAttach ()
 {
-    Error error;
-    // TODO: this is hardcoded for macosx right now. We need this to be more dynamic
-    m_dynamic_loader_ap.reset(DynamicLoader::FindPlugin(this, "dynamic-loader.macosx-dyld"));
-
-    if (m_dynamic_loader_ap.get() == NULL)
-        error.SetErrorString("unable to find the dynamic loader named 'dynamic-loader.macosx-dyld'");
-    
-    return error;
+    return Error();
 }
 
 
@@ -434,11 +426,7 @@
 void
 ProcessMacOSX::DidLaunchOrAttach ()
 {
-    if (GetID() == LLDB_INVALID_PROCESS_ID)
-    {
-        m_dynamic_loader_ap.reset();
-    }
-    else
+    if (GetID() != LLDB_INVALID_PROCESS_ID)
     {
         Module * exe_module = GetTarget().GetExecutableModule ().get();
         assert (exe_module);
@@ -455,16 +443,12 @@
 {
     ProcessMacOSXLog::LogIf (PD_LOG_PROCESS, "ProcessMacOSX::DidLaunch()");
     DidLaunchOrAttach ();
-    if (m_dynamic_loader_ap.get())
-        m_dynamic_loader_ap->DidLaunch();
 }
 
 void
 ProcessMacOSX::DidAttach ()
 {
     DidLaunchOrAttach ();
-    if (m_dynamic_loader_ap.get())
-        m_dynamic_loader_ap->DidAttach();
 }
 
 Error
@@ -870,12 +854,6 @@
     return Task().GetDYLDAllImageInfosAddress();
 }
 
-DynamicLoader *
-ProcessMacOSX::GetDynamicLoader()
-{
-    return m_dynamic_loader_ap.get();
-}
-
 //------------------------------------------------------------------
 // Process Memory
 //------------------------------------------------------------------
diff --git a/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.h b/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.h
index c0a80c5..f2ab6cf 100644
--- a/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.h
+++ b/lldb/source/Plugins/Process/MacOSX-User/source/ProcessMacOSX.h
@@ -222,9 +222,6 @@
     virtual lldb_private::Error
     DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
 
-    virtual lldb_private::DynamicLoader *
-    GetDynamicLoader ();
-
     static void
     AddArchCreateCallback(const lldb_private::ArchSpec& arch_spec,
                           ProcessMacOSX::CreateArchCalback callback);
@@ -244,7 +241,6 @@
     MachException::Message::collection m_exception_messages;       // A collection of exception messages caught when listening to the exception port
     lldb_private::Mutex m_exception_messages_mutex; // Multithreaded protection for m_exception_messages
     lldb_private::ArchSpec m_arch_spec;
-    std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
 
     //----------------------------------------------------------------------
     // Child process control
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index cba0ca1..df3e09a 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -101,7 +101,6 @@
 //----------------------------------------------------------------------
 ProcessGDBRemote::ProcessGDBRemote(Target& target, Listener &listener) :
     Process (target, listener),
-    m_dynamic_loader_ap (),
     m_flags (0),
     m_stdio_mutex (Mutex::eMutexTypeRecursive),
     m_gdb_comm(),
@@ -132,8 +131,6 @@
 //----------------------------------------------------------------------
 ProcessGDBRemote::~ProcessGDBRemote()
 {
-    m_dynamic_loader_ap.reset();
-
     if (IS_VALID_LLDB_HOST_THREAD(m_debugserver_thread))
     {
         Host::ThreadCancel (m_debugserver_thread, NULL);
@@ -410,13 +407,7 @@
 ProcessGDBRemote::WillLaunchOrAttach ()
 {
     Error error;
-    // TODO: this is hardcoded for macosx right now. We need this to be more dynamic
-    m_dynamic_loader_ap.reset(DynamicLoader::FindPlugin(this, "dynamic-loader.macosx-dyld"));
-
-    if (m_dynamic_loader_ap.get() == NULL)
-        error.SetErrorString("unable to find the dynamic loader named 'dynamic-loader.macosx-dyld'");
     m_stdio_communication.Clear ();
-    
     return error;
 }
 
@@ -628,11 +619,7 @@
     LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
     if (log)
         log->Printf ("ProcessGDBRemote::DidLaunch()");
-    if (GetID() == LLDB_INVALID_PROCESS_ID)
-    {
-        m_dynamic_loader_ap.reset();
-    }
-    else
+    if (GetID() != LLDB_INVALID_PROCESS_ID)
     {
         m_dispatch_queue_offsets_addr = LLDB_INVALID_ADDRESS;
 
@@ -673,8 +660,6 @@
 ProcessGDBRemote::DidLaunch ()
 {
     DidLaunchOrAttach ();
-    if (m_dynamic_loader_ap.get())
-        m_dynamic_loader_ap->DidLaunch();
 }
 
 Error
@@ -816,8 +801,6 @@
 ProcessGDBRemote::DidAttach ()
 {
     DidLaunchOrAttach ();
-    if (m_dynamic_loader_ap.get())
-        m_dynamic_loader_ap->DidAttach();
 }
 
 Error
@@ -1540,12 +1523,6 @@
     return LLDB_INVALID_ADDRESS;
 }
 
-DynamicLoader *
-ProcessGDBRemote::GetDynamicLoader()
-{
-    return m_dynamic_loader_ap.get();
-}
-
 //------------------------------------------------------------------
 // Process Memory
 //------------------------------------------------------------------
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
index 3e61ac6..7d5a361 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
@@ -219,9 +219,6 @@
     virtual lldb_private::Error
     DisableWatchpoint (lldb_private::WatchpointLocation *wp_loc);
 
-    virtual lldb_private::DynamicLoader *
-    GetDynamicLoader ();
-    
     virtual bool
     StartNoticingNewThreads();    
 
@@ -328,8 +325,6 @@
         eBroadcastBitAsyncThreadShouldExit          = (1 << 1)
     };
 
-
-    std::auto_ptr<lldb_private::DynamicLoader> m_dynamic_loader_ap;
     lldb_private::Flags m_flags;            // Process specific flags (see eFlags enums)
     lldb_private::Mutex m_stdio_mutex;      // Multithreaded protection for stdio
     GDBRemoteCommunication m_gdb_comm;
diff --git a/lldb/source/Target/Process.cpp b/lldb/source/Target/Process.cpp
index cf019e6..fb6614d 100644
--- a/lldb/source/Target/Process.cpp
+++ b/lldb/source/Target/Process.cpp
@@ -845,12 +845,6 @@
     return error;
 }
 
-DynamicLoader *
-Process::GetDynamicLoader()
-{
-    return NULL;
-}
-
 const ABI *
 Process::GetABI()
 {
@@ -1503,6 +1497,7 @@
 {
     Error error;
     m_abi_sp.reset();
+    m_dyld_ap.reset();
     m_process_input_reader.reset();
 
     Module *exe_module = m_target.GetExecutableModule().get();
@@ -1569,8 +1564,13 @@
 
                     if (state == eStateStopped || state == eStateCrashed)
                     {
+
                         DidLaunch ();
 
+                        m_dyld_ap.reset (DynamicLoader::FindPlugin(this, false));
+                        if (m_dyld_ap.get())
+                            m_dyld_ap->DidLaunch();
+
                         // This delays passing the stopped event to listeners till DidLaunch gets
                         // a chance to complete...
                         HandlePrivateEvent (event_sp);
@@ -1609,25 +1609,7 @@
             // lldb_private::Process subclasses must set the process must set
             // the new process ID.
             assert (m_process->GetID() != LLDB_INVALID_PROCESS_ID);
-            m_process->DidAttach ();
-            // Figure out which one is the executable, and set that in our target:
-            ModuleList &modules = m_process->GetTarget().GetImages();
-            
-            size_t num_modules = modules.GetSize();
-            for (int i = 0; i < num_modules; i++)
-            {
-                ModuleSP module_sp = modules.GetModuleAtIndex(i);
-                if (module_sp->IsExecutable())
-                {
-                    ModuleSP exec_module = m_process->GetTarget().GetExecutableModule();
-                    if (!exec_module || exec_module != module_sp)
-                    {
-                        
-                        m_process->GetTarget().SetExecutableModule (module_sp, false);
-                    }
-                    break;
-                }
-            }
+            m_process->CompleteAttach ();
             return eEventActionSuccess;
         }
             
@@ -1671,6 +1653,8 @@
         GetTarget().SetArchitecture(attach_spec);
     }
 
+    m_dyld_ap.reset();
+
     Error error (WillAttachToProcessWithID(attach_pid));
     if (error.Success())
     {
@@ -1716,6 +1700,8 @@
             GetTarget().SetArchitecture(attach_spec);
         }
     }
+
+    m_dyld_ap.reset();
     
     Error error (WillAttachToProcessWithName(process_name, wait_for_launch));
     if (error.Success())
@@ -1743,6 +1729,36 @@
     return error;
 }
 
+void
+Process::CompleteAttach ()
+{
+    // Let the process subclass figure out at much as it can about the process
+    // before we go looking for a dynamic loader plug-in.
+    DidAttach();
+
+    // We have complete the attach, now it is time to find the dynamic loader
+    // plug-in
+    m_dyld_ap.reset (DynamicLoader::FindPlugin(this, false));
+    if (m_dyld_ap.get())
+        m_dyld_ap->DidAttach();
+
+    // Figure out which one is the executable, and set that in our target:
+    ModuleList &modules = m_target.GetImages();
+    
+    size_t num_modules = modules.GetSize();
+    for (int i = 0; i < num_modules; i++)
+    {
+        ModuleSP module_sp (modules.GetModuleAtIndex(i));
+        if (module_sp->IsExecutable())
+        {
+            ModuleSP target_exe_module_sp (m_target.GetExecutableModule());
+            if (target_exe_module_sp != module_sp)
+                m_target.SetExecutableModule (module_sp, false);
+            break;
+        }
+    }
+}
+
 Error
 Process::ConnectRemote (const char *remote_url)
 {