Added auto completion for architecture names and for platforms.

Modified the OptionGroupOptions to be able to specify only some of the options
that should be appended by using the usage_mask in the group defintions and
also provided a way to remap them to a new usage mask after the copy. This 
allows options to be re-used and also targetted for specific option groups.

Modfied the CommandArgumentType to have a new eArgTypePlatform enumeration.
Taught the option parser to be able to automatically use the appropriate
auto completion for a given options if nothing is explicitly specified
in the option definition. So you don't have to specify it in the option
definition tables.

Renamed the default host platform name to "host", and the default platform
hostname to be "localhost".

Modified the "file" and "platform select" commands to make sure all options
and args are good prior to creating a new platform. Also defer the computation
of the architecture in the file command until all options are parsed and the
platform has either not been specified or reset to a new value to avoid
computing the arch more than once.

Switch the PluginManager code over to using llvm::StringRef for string
comparisons and got rid of all the AccessorXXX functions in lieu of the newer
mutex + collection singleton accessors.




git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@129483 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Commands/CommandCompletions.cpp b/source/Commands/CommandCompletions.cpp
index fcfc29b..e57a8b4 100644
--- a/source/Commands/CommandCompletions.cpp
+++ b/source/Commands/CommandCompletions.cpp
@@ -20,6 +20,7 @@
 // Project includes
 #include "lldb/Host/FileSpec.h"
 #include "lldb/Core/FileSpecList.h"
+#include "lldb/Core/PluginManager.h"
 #include "lldb/Interpreter/Args.h"
 #include "lldb/Interpreter/CommandCompletions.h"
 #include "lldb/Interpreter/CommandInterpreter.h"
@@ -38,6 +39,8 @@
     {eSymbolCompletion,          CommandCompletions::Symbols},
     {eModuleCompletion,          CommandCompletions::Modules},
     {eSettingsNameCompletion,    CommandCompletions::SettingsNames},
+    {ePlatformPluginCompletion,  CommandCompletions::PlatformPluginNames},
+    {eArchitectureCompletion,    CommandCompletions::ArchitectureNames},
     {eNoCompletion,              NULL}      // This one has to be last in the list.
 };
 
@@ -413,6 +416,36 @@
     //return matches.GetSize();
 }
 
+
+int
+CommandCompletions::PlatformPluginNames (CommandInterpreter &interpreter,
+                                         const char *partial_name,
+                                         int match_start_point,
+                                         int max_return_elements,
+                                         SearchFilter *searcher,
+                                         bool &word_complete,
+                                         lldb_private::StringList &matches)
+{
+    const uint32_t num_matches = PluginManager::AutoCompletePlatformName(partial_name, matches);
+    word_complete = num_matches == 1;
+    return num_matches;
+}
+
+int
+CommandCompletions::ArchitectureNames (CommandInterpreter &interpreter,
+                                       const char *partial_name,
+                                       int match_start_point,
+                                       int max_return_elements,
+                                       SearchFilter *searcher,
+                                       bool &word_complete,
+                                       lldb_private::StringList &matches)
+{
+    const uint32_t num_matches = ArchSpec::AutoComplete (partial_name, matches);
+    word_complete = num_matches == 1;
+    return num_matches;
+}
+
+
 CommandCompletions::Completer::Completer 
 (
     CommandInterpreter &interpreter,
diff --git a/source/Commands/CommandObjectFile.cpp b/source/Commands/CommandObjectFile.cpp
index 372baf9..563cbe2 100644
--- a/source/Commands/CommandObjectFile.cpp
+++ b/source/Commands/CommandObjectFile.cpp
@@ -26,7 +26,6 @@
 using namespace lldb_private;
 
 FileOptionGroup::FileOptionGroup() :
-    m_arch (),
     m_arch_str ()
 {
 }
@@ -53,6 +52,17 @@
     return g_file_option_table;
 }
 
+bool
+FileOptionGroup::GetArchitecture (Platform *platform, ArchSpec &arch)
+{
+    if (m_arch_str.empty())
+        arch.Clear();
+    else
+        arch.SetTriple(m_arch_str.c_str(), platform);
+    return arch.IsValid();
+}
+
+
 Error
 FileOptionGroup::SetOptionValue (CommandInterpreter &interpreter,
                                  uint32_t option_idx,
@@ -64,17 +74,7 @@
     switch (short_option)
     {
         case 'a':
-            {
-                // Save the arch value in case we specify a platform after specifying the arch
-                m_arch_str.assign (option_arg);
-                // Check to see if we already have a platform?
-                m_arch_platform_sp = interpreter.GetPlatform (false);
-                ArchSpec option_arch (option_arg, m_arch_platform_sp.get());
-                if (option_arch.IsValid())
-                    m_arch = option_arch;
-                else
-                    error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", option_arg);
-            }
+            m_arch_str.assign (option_arg);
             break;
 
         default:
@@ -88,26 +88,7 @@
 void
 FileOptionGroup::OptionParsingStarting (CommandInterpreter &interpreter)
 {
-    m_arch.Clear();
-}
-
-Error
-FileOptionGroup::OptionParsingFinished (CommandInterpreter &interpreter)
-{
-    Error error;
-    if (m_arch.IsValid())
-    {
-        PlatformSP curr_platform_sp (interpreter.GetPlatform (false));
-        if (curr_platform_sp.get() != m_arch_platform_sp.get())
-        {
-            ArchSpec option_arch (m_arch_str.c_str(), curr_platform_sp.get());
-            if (option_arch.IsValid())
-                m_arch = option_arch;
-            else
-                error.SetErrorStringWithFormat ("invalid arch '%s' for platform '%s'", m_arch_str.c_str(), curr_platform_sp->GetName());
-        }
-    }
-    return error;
+    m_arch_str.clear();
 }
 
 //-------------------------------------------------------------------------
@@ -136,8 +117,8 @@
     // Push the data for the first argument into the m_arguments vector.
     m_arguments.push_back (arg);
     
-    m_option_group.Append (&m_file_options);
-    m_option_group.Append (&m_platform_options);
+    m_option_group.Append (&m_file_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
+    m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
     m_option_group.Finalize();
 }
 
@@ -164,18 +145,46 @@
     if (argc == 1)
     {
         FileSpec file_spec (file_path, true);
+        
+        bool select = true;
+        PlatformSP platform_sp;
+        
+        Error error;
+        
+        if (!m_platform_options.platform_name.empty())
+        {
+            platform_sp = m_platform_options.CreatePlatformWithOptions(m_interpreter, select, error);
+            if (!platform_sp)
+            {
+                result.AppendError(error.AsCString());
+                result.SetStatus (eReturnStatusFailed);
+                return false;
+            }
+        }
+        ArchSpec file_arch;
+        
+        if (!m_file_options.m_arch_str.empty())
+        {        
+            if (!platform_sp)
+                platform_sp = m_interpreter.GetDebugger().GetPlatformList().GetSelectedPlatform();
+            if (!m_file_options.GetArchitecture(platform_sp.get(), file_arch))
+            {
+                result.AppendErrorWithFormat("invalid architecture '%s'", m_file_options.m_arch_str.c_str());
+                result.SetStatus (eReturnStatusFailed);
+                return false;
+            }
+        }
 
         if (! file_spec.Exists() && !file_spec.ResolveExecutableLocation())
         {
             result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
             result.SetStatus (eReturnStatusFailed);
-            return result.Succeeded();
+            return false;
         }
 
         TargetSP target_sp;
-
         Debugger &debugger = m_interpreter.GetDebugger();
-        Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, m_file_options.m_arch, true, target_sp);
+        error = debugger.GetTargetList().CreateTarget (debugger, file_spec, file_arch, true, target_sp);
 
         if (target_sp)
         {
diff --git a/source/Commands/CommandObjectFile.h b/source/Commands/CommandObjectFile.h
index 1af8896..f3ef2d9 100644
--- a/source/Commands/CommandObjectFile.h
+++ b/source/Commands/CommandObjectFile.h
@@ -26,37 +26,35 @@
 // CommandObjectFile
 //-------------------------------------------------------------------------
 
-    class FileOptionGroup : public OptionGroup
-    {
-    public:
-        
-        FileOptionGroup ();
-        
-        virtual
-        ~FileOptionGroup ();
+class FileOptionGroup : public OptionGroup
+{
+public:
+    
+    FileOptionGroup ();
+    
+    virtual
+    ~FileOptionGroup ();
 
-        
-        virtual uint32_t
-        GetNumDefinitions ();
-        
-        virtual const OptionDefinition*
-        GetDefinitions ();
-        
-        virtual Error
-        SetOptionValue (CommandInterpreter &interpreter,
-                        uint32_t option_idx,
-                        const char *option_value);
-        
-        virtual void
-        OptionParsingStarting (CommandInterpreter &interpreter);
-        
-        virtual Error
-        OptionParsingFinished (CommandInterpreter &interpreter);
-        
-        ArchSpec m_arch;
-        lldb::PlatformSP m_arch_platform_sp; // The platform that was used to resolve m_arch
-        std::string m_arch_str; // Save the arch triple in case a platform is specified after the architecture
-    };
+    
+    virtual uint32_t
+    GetNumDefinitions ();
+    
+    virtual const OptionDefinition*
+    GetDefinitions ();
+    
+    virtual Error
+    SetOptionValue (CommandInterpreter &interpreter,
+                    uint32_t option_idx,
+                    const char *option_value);
+    
+    virtual void
+    OptionParsingStarting (CommandInterpreter &interpreter);
+    
+    bool
+    GetArchitecture (Platform *platform, ArchSpec &arch);
+
+    std::string m_arch_str; // Save the arch triple in case a platform is specified after the architecture
+};
 
 class CommandObjectFile : public CommandObject
 {
diff --git a/source/Commands/CommandObjectLog.cpp b/source/Commands/CommandObjectLog.cpp
index e38b618..7dcfabc 100644
--- a/source/Commands/CommandObjectLog.cpp
+++ b/source/Commands/CommandObjectLog.cpp
@@ -42,16 +42,6 @@
 using namespace lldb_private;
 
 
-static LogChannelSP
-GetLogChannelPluginForChannel (const char *channel)
-{
-    std::string log_channel_plugin_name(channel);
-    log_channel_plugin_name += LogChannel::GetPluginSuffix();
-    LogChannelSP log_channel_sp (LogChannel::FindPlugin (log_channel_plugin_name.c_str()));
-    return log_channel_sp;
-}
-
-
 class CommandObjectLogEnable : public CommandObject
 {
 public:
@@ -99,6 +89,27 @@
         return &m_options;
     }
 
+//    int
+//    HandleArgumentCompletion (Args &input,
+//                              int &cursor_index,
+//                              int &cursor_char_position,
+//                              OptionElementVector &opt_element_vector,
+//                              int match_start_point,
+//                              int max_return_elements,
+//                              bool &word_complete,
+//                              StringList &matches)
+//    {
+//        std::string completion_str (input.GetArgumentAtIndex(cursor_index));
+//        completion_str.erase (cursor_char_position);
+//        
+//        if (cursor_index == 1)
+//        {
+//            //
+//            Log::AutoCompleteChannelName (completion_str.c_str(), matches);
+//        }
+//        return matches.GetSize();
+//    }
+//
     virtual bool
     Execute (Args& args,
              CommandReturnObject &result)
@@ -141,7 +152,7 @@
             }
             else
             {
-                LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
+                LogChannelSP log_channel_sp (LogChannel::FindPlugin (channel.c_str()));
                 if (log_channel_sp)
                 {
                     if (log_channel_sp->Enable (log_stream_sp, log_options, &result.GetErrorStream(), args))
@@ -314,7 +325,7 @@
             }
             else
             {
-                LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
+                LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
                 if (log_channel_sp)
                 {
                     log_channel_sp->Disable(args, &result.GetErrorStream());
@@ -388,7 +399,7 @@
                 }
                 else
                 {
-                    LogChannelSP log_channel_sp (GetLogChannelPluginForChannel(channel.c_str()));
+                    LogChannelSP log_channel_sp (LogChannel::FindPlugin(channel.c_str()));
                     if (log_channel_sp)
                     {
                         log_channel_sp->ListCategories(&result.GetOutputStream());
diff --git a/source/Commands/CommandObjectPlatform.cpp b/source/Commands/CommandObjectPlatform.cpp
index 46a53c7..2528651 100644
--- a/source/Commands/CommandObjectPlatform.cpp
+++ b/source/Commands/CommandObjectPlatform.cpp
@@ -29,44 +29,31 @@
 
 
 PlatformSP 
-PlatformOptionGroup::CreatePlatformWithOptions (CommandInterpreter &interpreter,
-                                                const char *platform_name, 
-                                                bool select, 
-                                                Error& error)
+PlatformOptionGroup::CreatePlatformWithOptions (CommandInterpreter &interpreter, bool select, Error& error)
 {
-    if (platform_name && platform_name[0])
+    PlatformSP platform_sp;
+    if (!platform_name.empty())
     {
-        if (platform_sp)
-        {
-            error.SetErrorString ("platform can't be set more than once in a command");
-            return PlatformSP();
-        }
-
-        platform_sp = Platform::Create (platform_name, error);
+        platform_sp = Platform::Create (platform_name.c_str(), error);
 
         if (platform_sp)
         {
-                interpreter.GetDebugger().GetPlatformList().Append (platform_sp, select);
-                if (os_version_major != UINT32_MAX)
-                {
-                    platform_sp->SetOSVersion (os_version_major,
-                                               os_version_minor,
-                                               os_version_update);
-                }
+            interpreter.GetDebugger().GetPlatformList().Append (platform_sp, select);
+            if (os_version_major != UINT32_MAX)
+            {
+                platform_sp->SetOSVersion (os_version_major,
+                                           os_version_minor,
+                                           os_version_update);
+            }
         }
     }
-    else
-    {
-        error.SetErrorString ("invalid platform name");
-        platform_sp.reset();
-    }
     return platform_sp;
 }
 
 void
 PlatformOptionGroup::OptionParsingStarting (CommandInterpreter &interpreter)
 {
-    platform_sp.reset();
+    platform_name.clear();
     os_version_major = UINT32_MAX;
     os_version_minor = UINT32_MAX;
     os_version_update = UINT32_MAX;
@@ -75,7 +62,7 @@
 static OptionDefinition
 g_option_table[] =
 {
-    { LLDB_OPT_SET_ALL, false, "platform"   , 'p', required_argument, NULL, 0, eArgTypeNone, "Specify name of the platform to use for this target, creating the platform if necessary."},
+    { LLDB_OPT_SET_ALL, false, "platform"   , 'p', required_argument, NULL, 0, eArgTypePlatform, "Specify name of the platform to use for this target, creating the platform if necessary."},
     { LLDB_OPT_SET_ALL, false, "sdk-version", 'v', required_argument, NULL, 0, eArgTypeNone, "Specify the initial SDK version to use prior to connecting." }
 };
 
@@ -112,19 +99,12 @@
     switch (short_option)
     {
     case 'p':
-        CreatePlatformWithOptions (interpreter, option_arg, true, error);
+        platform_name.assign (option_arg);
         break;
 
     case 'v':
         if (Args::StringToVersion (option_arg, os_version_major, os_version_minor, os_version_update) == option_arg)
-        {
             error.SetErrorStringWithFormat ("invalid version string '%s'", option_arg);
-        }
-        else
-        {
-            if (platform_sp)
-                platform_sp->SetOSVersion (os_version_major, os_version_minor, os_version_update);
-        }
         break;
         
     default:
@@ -149,7 +129,7 @@
         m_option_group (interpreter),
         m_platform_options (false) // Don't include the "--platform" option by passing false
     {
-        m_option_group.Append (&m_platform_options);
+        m_option_group.Append (&m_platform_options, LLDB_OPT_SET_ALL, 1);
         m_option_group.Finalize();
     }
 
@@ -161,16 +141,31 @@
     virtual bool
     Execute (Args& args, CommandReturnObject &result)
     {
-        Error error;
         if (args.GetArgumentCount() == 1)
         {
-            const bool select = true;
-            PlatformSP platform_sp (m_platform_options.CreatePlatformWithOptions (m_interpreter,
-                                                                                  args.GetArgumentAtIndex (0), 
-                                                                                  select, 
-                                                                                  error));
-            if (platform_sp)
-                platform_sp->GetStatus (result.GetOutputStream());
+            const char *platform_name = args.GetArgumentAtIndex (0);
+            if (platform_name && platform_name[0])
+            {
+                const bool select = true;
+                m_platform_options.platform_name.assign (platform_name);
+                Error error;
+                PlatformSP platform_sp (m_platform_options.CreatePlatformWithOptions (m_interpreter, select, error));
+                if (platform_sp)
+                {
+                    platform_sp->GetStatus (result.GetOutputStream());
+                    result.SetStatus (eReturnStatusSuccessFinishResult);
+                }
+                else
+                {
+                    result.AppendError(error.AsCString());
+                    result.SetStatus (eReturnStatusFailed);
+                }
+            }
+            else
+            {
+                result.AppendError ("invalid platform name");
+                result.SetStatus (eReturnStatusFailed);
+            }
         }
         else
         {
diff --git a/source/Commands/CommandObjectPlatform.h b/source/Commands/CommandObjectPlatform.h
index 255fc3b..0019915 100644
--- a/source/Commands/CommandObjectPlatform.h
+++ b/source/Commands/CommandObjectPlatform.h
@@ -49,11 +49,11 @@
 public:
     
     PlatformOptionGroup (bool include_platform_option) :
-        m_include_platform_option (include_platform_option),
-        platform_sp (),
+        platform_name (),
         os_version_major (UINT32_MAX),
         os_version_minor (UINT32_MAX),
-        os_version_update (UINT32_MAX)
+        os_version_update (UINT32_MAX),
+        m_include_platform_option (include_platform_option)
     {
     }
     
@@ -74,17 +74,16 @@
                     const char *option_value);
     
     lldb::PlatformSP 
-    CreatePlatformWithOptions (CommandInterpreter &interpreter,
-                               const char *platform_name, 
-                               bool select, 
-                               Error& error);
+    CreatePlatformWithOptions (CommandInterpreter &interpreter, 
+                               bool select,
+                               Error &error);
 
     virtual void
     OptionParsingStarting (CommandInterpreter &interpreter);
         
     // Instance variables to hold the values for command options.
     
-    lldb::PlatformSP platform_sp;
+    std::string platform_name;
     uint32_t os_version_major;
     uint32_t os_version_minor;
     uint32_t os_version_update;
diff --git a/source/Core/ArchSpec.cpp b/source/Core/ArchSpec.cpp
index e573b99..4556732 100644
--- a/source/Core/ArchSpec.cpp
+++ b/source/Core/ArchSpec.cpp
@@ -79,11 +79,11 @@
     { eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc  , ArchSpec::eCore_sparc_generic   , "sparc"     },
     { eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9, ArchSpec::eCore_sparc9_generic  , "sparcv9"   },
 
-    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i386     , "i386"      },
-    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i486     , "i486"      },
-    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i486sx   , "i486sx"    },
+    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i386    , "i386"      },
+    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i486    , "i486"      },
+    { eByteOrderLittle, 4, 1, 15, llvm::Triple::x86    , ArchSpec::eCore_x86_32_i486sx  , "i486sx"    },
 
-    { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64   , "x86_64"    }
+    { eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64 , ArchSpec::eCore_x86_64_x86_64  , "x86_64"    }
 };
 
 struct ArchDefinitionEntry
@@ -104,6 +104,28 @@
 };
 
 
+uint32_t
+ArchSpec::AutoComplete (const char *name, StringList &matches)
+{
+    uint32_t i;
+    if (name && name[0])
+    {
+        for (i = 0; i < ArchSpec::kNumCores; ++i)
+        {
+            if (NameMatches(g_core_definitions[i].name, eNameMatchStartsWith, name))
+                matches.AppendString (g_core_definitions[i].name);
+        }
+    }
+    else
+    {
+        for (i = 0; i < ArchSpec::kNumCores; ++i)
+            matches.AppendString (g_core_definitions[i].name);
+    }
+    return matches.GetSize();
+}
+
+
+
 #define CPU_ANY (UINT32_MAX)
 
 //===----------------------------------------------------------------------===//
diff --git a/source/Core/Log.cpp b/source/Core/Log.cpp
index 1b5c74a..f537f1e 100644
--- a/source/Core/Log.cpp
+++ b/source/Core/Log.cpp
@@ -382,6 +382,27 @@
 }
 
 void
+Log::AutoCompleteChannelName (const char *channel_name, StringList &matches)
+{
+    LogChannelMap &map = GetChannelMap ();
+    LogChannelMapIter pos, end = map.end();
+    for (pos = map.begin(); pos != end; ++pos)
+    {
+        const char *pos_channel_name = pos->first.GetCString();
+        if (channel_name && channel_name[0])
+        {
+            if (NameMatches (channel_name, eNameMatchStartsWith, pos_channel_name))
+            {
+                matches.AppendString(pos_channel_name);
+            }
+        }
+        else
+            matches.AppendString(pos_channel_name);
+
+    }
+}
+
+void
 Log::DisableAllLogChannels (Stream *feedback_strm)
 {
     CallbackMap &callback_map = GetCallbackMap ();
@@ -499,10 +520,4 @@
 {
 }
 
-const char *
-LogChannel::GetPluginSuffix ()
-{
-    return ".log-channel";
-}
-
 
diff --git a/source/Core/PluginManager.cpp b/source/Core/PluginManager.cpp
index ae89c72..aff9b2b 100644
--- a/source/Core/PluginManager.cpp
+++ b/source/Core/PluginManager.cpp
@@ -19,6 +19,8 @@
 #include "lldb/Host/Host.h"
 #include "lldb/Host/Mutex.h"
 
+#include "llvm/ADT/StringRef.h"
+
 using namespace lldb;
 using namespace lldb_private;
 
@@ -256,7 +258,7 @@
     {
         ABIInstance instance;
         assert (name && name[0]);
-        instance.name = name;
+        instance.name.assign (name);
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
@@ -293,7 +295,6 @@
 {
     Mutex::Locker locker (GetABIInstancesMutex ());
     ABIInstances &instances = GetABIInstances ();
-
     if (idx < instances.size())
         return instances[idx].create_callback;
     return NULL;
@@ -305,13 +306,13 @@
     if (name && name[0])
     {
         Mutex::Locker locker (GetABIInstancesMutex ());
-        std::string ss_name(name);
+        llvm::StringRef name_sref(name);
         ABIInstances &instances = GetABIInstances ();
 
         ABIInstances::iterator pos, end = instances.end();
         for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (pos->name == ss_name)
+            if (name_sref.equals (pos->name))
                 return pos->create_callback;
         }
     }
@@ -338,50 +339,19 @@
 
 typedef std::vector<DisassemblerInstance> DisassemblerInstances;
 
-static bool
-AccessDisassemblerInstances (PluginAction action, DisassemblerInstance &instance, uint32_t index)
+static Mutex &
+GetDisassemblerMutex ()
 {
-    static DisassemblerInstances g_plugin_instances;
-
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            DisassemblerInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
 }
 
+static DisassemblerInstances &
+GetDisassemblerInstances ()
+{
+    static DisassemblerInstances g_instances;
+    return g_instances;
+}
 
 bool
 PluginManager::RegisterPlugin
@@ -399,7 +369,9 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessDisassemblerInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetDisassemblerMutex ());
+        GetDisassemblerInstances ().push_back (instance);
+        return true;
     }
     return false;
 }
@@ -409,9 +381,18 @@
 {
     if (create_callback)
     {
-        DisassemblerInstance instance;
-        instance.create_callback = create_callback;
-        return AccessDisassemblerInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetDisassemblerMutex ());
+        DisassemblerInstances &instances = GetDisassemblerInstances ();
+        
+        DisassemblerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -419,9 +400,10 @@
 DisassemblerCreateInstance
 PluginManager::GetDisassemblerCreateCallbackAtIndex (uint32_t idx)
 {
-    DisassemblerInstance instance;
-    if (AccessDisassemblerInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetDisassemblerMutex ());
+    DisassemblerInstances &instances = GetDisassemblerInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -430,12 +412,15 @@
 {
     if (name && name[0])
     {
-        DisassemblerInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessDisassemblerInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetDisassemblerMutex ());
+        DisassemblerInstances &instances = GetDisassemblerInstances ();
+        
+        DisassemblerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -462,48 +447,19 @@
 
 typedef std::vector<DynamicLoaderInstance> DynamicLoaderInstances;
 
-static bool
-AccessDynamicLoaderInstances (PluginAction action, DynamicLoaderInstance &instance, uint32_t index)
+
+static Mutex &
+GetDynamicLoaderMutex ()
 {
-    static DynamicLoaderInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            DynamicLoaderInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static DynamicLoaderInstances &
+GetDynamicLoaderInstances ()
+{
+    static DynamicLoaderInstances g_instances;
+    return g_instances;
 }
 
 
@@ -523,7 +479,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessDynamicLoaderInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetDynamicLoaderMutex ());
+        GetDynamicLoaderInstances ().push_back (instance);
     }
     return false;
 }
@@ -533,9 +490,18 @@
 {
     if (create_callback)
     {
-        DynamicLoaderInstance instance;
-        instance.create_callback = create_callback;
-        return AccessDynamicLoaderInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetDynamicLoaderMutex ());
+        DynamicLoaderInstances &instances = GetDynamicLoaderInstances ();
+        
+        DynamicLoaderInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -543,9 +509,10 @@
 DynamicLoaderCreateInstance
 PluginManager::GetDynamicLoaderCreateCallbackAtIndex (uint32_t idx)
 {
-    DynamicLoaderInstance instance;
-    if (AccessDynamicLoaderInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetDynamicLoaderMutex ());
+    DynamicLoaderInstances &instances = GetDynamicLoaderInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -554,12 +521,15 @@
 {
     if (name && name[0])
     {
-        DynamicLoaderInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessDynamicLoaderInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetDynamicLoaderMutex ());
+        DynamicLoaderInstances &instances = GetDynamicLoaderInstances ();
+        
+        DynamicLoaderInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -584,48 +554,18 @@
 
 typedef std::vector<EmulateInstructionInstance> EmulateInstructionInstances;
 
-static bool
-AccessEmulateInstructionInstances (PluginAction action, EmulateInstructionInstance &instance, uint32_t index)
+static Mutex &
+GetEmulateInstructionMutex ()
 {
-    static EmulateInstructionInstances g_plugin_instances;
-    
-    switch (action)
-    {
-        case ePluginRegisterInstance:
-            if (instance.create_callback)
-            {
-                g_plugin_instances.push_back (instance);
-                return true;
-            }
-            break;
-            
-        case ePluginUnregisterInstance:
-            if (instance.create_callback)
-            {
-                EmulateInstructionInstances::iterator pos, end = g_plugin_instances.end();
-                for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-                {
-                    if (pos->create_callback == instance.create_callback)
-                    {
-                        g_plugin_instances.erase(pos);
-                        return true;
-                    }
-                }
-            }
-            break;
-            
-        case ePluginGetInstanceAtIndex:
-            if (index < g_plugin_instances.size())
-            {
-                instance = g_plugin_instances[index];
-                return true;
-            }
-            break;
-            
-        default:
-            break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
+
+static EmulateInstructionInstances &
+GetEmulateInstructionInstances ()
+{
+    static EmulateInstructionInstances g_instances;
+    return g_instances;
 }
 
 
@@ -645,7 +585,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessEmulateInstructionInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetEmulateInstructionMutex ());
+        GetEmulateInstructionInstances ().push_back (instance);
     }
     return false;
 }
@@ -655,9 +596,18 @@
 {
     if (create_callback)
     {
-        EmulateInstructionInstance instance;
-        instance.create_callback = create_callback;
-        return AccessEmulateInstructionInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetEmulateInstructionMutex ());
+        EmulateInstructionInstances &instances = GetEmulateInstructionInstances ();
+        
+        EmulateInstructionInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -665,9 +615,10 @@
 EmulateInstructionCreateInstance
 PluginManager::GetEmulateInstructionCreateCallbackAtIndex (uint32_t idx)
 {
-    EmulateInstructionInstance instance;
-    if (AccessEmulateInstructionInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetEmulateInstructionMutex ());
+    EmulateInstructionInstances &instances = GetEmulateInstructionInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -676,12 +627,15 @@
 {
     if (name && name[0])
     {
-        EmulateInstructionInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessEmulateInstructionInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetEmulateInstructionMutex ());
+        EmulateInstructionInstances &instances = GetEmulateInstructionInstances ();
+        
+        EmulateInstructionInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -707,50 +661,19 @@
 
 typedef std::vector<LanguageRuntimeInstance> LanguageRuntimeInstances;
 
-static bool
-AccessLanguageRuntimeInstances (PluginAction action, LanguageRuntimeInstance &instance, uint32_t index)
+static Mutex &
+GetLanguageRuntimeMutex ()
 {
-    static LanguageRuntimeInstances g_plugin_instances;
-
-    switch (action)
-    {
-        case ePluginRegisterInstance:
-            if (instance.create_callback)
-            {
-                g_plugin_instances.push_back (instance);
-                return true;
-            }
-            break;
-
-        case ePluginUnregisterInstance:
-            if (instance.create_callback)
-            {
-                LanguageRuntimeInstances::iterator pos, end = g_plugin_instances.end();
-                for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-                {
-                    if (pos->create_callback == instance.create_callback)
-                    {
-                        g_plugin_instances.erase(pos);
-                        return true;
-                    }
-                }
-            }
-            break;
-
-        case ePluginGetInstanceAtIndex:
-            if (index < g_plugin_instances.size())
-            {
-                instance = g_plugin_instances[index];
-                return true;
-            }
-            break;
-
-        default:
-            break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
 }
 
+static LanguageRuntimeInstances &
+GetLanguageRuntimeInstances ()
+{
+    static LanguageRuntimeInstances g_instances;
+    return g_instances;
+}
 
 bool
 PluginManager::RegisterPlugin
@@ -768,7 +691,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessLanguageRuntimeInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetLanguageRuntimeMutex ());
+        GetLanguageRuntimeInstances ().push_back (instance);
     }
     return false;
 }
@@ -778,9 +702,18 @@
 {
     if (create_callback)
     {
-        LanguageRuntimeInstance instance;
-        instance.create_callback = create_callback;
-        return AccessLanguageRuntimeInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetLanguageRuntimeMutex ());
+        LanguageRuntimeInstances &instances = GetLanguageRuntimeInstances ();
+        
+        LanguageRuntimeInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -788,9 +721,10 @@
 LanguageRuntimeCreateInstance
 PluginManager::GetLanguageRuntimeCreateCallbackAtIndex (uint32_t idx)
 {
-    LanguageRuntimeInstance instance;
-    if (AccessLanguageRuntimeInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetLanguageRuntimeMutex ());
+    LanguageRuntimeInstances &instances = GetLanguageRuntimeInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -799,12 +733,15 @@
 {
     if (name && name[0])
     {
-        LanguageRuntimeInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessLanguageRuntimeInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetLanguageRuntimeMutex ());
+        LanguageRuntimeInstances &instances = GetLanguageRuntimeInstances ();
+        
+        LanguageRuntimeInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -828,48 +765,18 @@
 
 typedef std::vector<ObjectFileInstance> ObjectFileInstances;
 
-static bool
-AccessObjectFileInstances (PluginAction action, ObjectFileInstance &instance, uint32_t index)
+static Mutex &
+GetObjectFileMutex ()
 {
-    static ObjectFileInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            ObjectFileInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static ObjectFileInstances &
+GetObjectFileInstances ()
+{
+    static ObjectFileInstances g_instances;
+    return g_instances;
 }
 
 
@@ -889,7 +796,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessObjectFileInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetObjectFileMutex ());
+        GetObjectFileInstances ().push_back (instance);
     }
     return false;
 }
@@ -899,9 +807,18 @@
 {
     if (create_callback)
     {
-        ObjectFileInstance instance;
-        instance.create_callback = create_callback;
-        return AccessObjectFileInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetObjectFileMutex ());
+        ObjectFileInstances &instances = GetObjectFileInstances ();
+        
+        ObjectFileInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -909,22 +826,27 @@
 ObjectFileCreateInstance
 PluginManager::GetObjectFileCreateCallbackAtIndex (uint32_t idx)
 {
-    ObjectFileInstance instance;
-    if (AccessObjectFileInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetObjectFileMutex ());
+    ObjectFileInstances &instances = GetObjectFileInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
+
 ObjectFileCreateInstance
 PluginManager::GetObjectFileCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        ObjectFileInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessObjectFileInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetObjectFileMutex ());
+        ObjectFileInstances &instances = GetObjectFileInstances ();
+        
+        ObjectFileInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -950,50 +872,19 @@
 
 typedef std::vector<ObjectContainerInstance> ObjectContainerInstances;
 
-static bool
-AccessObjectContainerInstances (PluginAction action, ObjectContainerInstance &instance, uint32_t index)
+static Mutex &
+GetObjectContainerMutex ()
 {
-    static ObjectContainerInstances g_plugin_instances;
-
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            ObjectContainerInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
 }
 
+static ObjectContainerInstances &
+GetObjectContainerInstances ()
+{
+    static ObjectContainerInstances g_instances;
+    return g_instances;
+}
 
 bool
 PluginManager::RegisterPlugin
@@ -1011,7 +902,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessObjectContainerInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetObjectContainerMutex ());
+        GetObjectContainerInstances ().push_back (instance);
     }
     return false;
 }
@@ -1021,9 +913,18 @@
 {
     if (create_callback)
     {
-        ObjectContainerInstance instance;
-        instance.create_callback = create_callback;
-        return AccessObjectContainerInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetObjectContainerMutex ());
+        ObjectContainerInstances &instances = GetObjectContainerInstances ();
+        
+        ObjectContainerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1031,22 +932,27 @@
 ObjectContainerCreateInstance
 PluginManager::GetObjectContainerCreateCallbackAtIndex (uint32_t idx)
 {
-    ObjectContainerInstance instance;
-    if (AccessObjectContainerInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetObjectContainerMutex ());
+    ObjectContainerInstances &instances = GetObjectContainerInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
+
 ObjectContainerCreateInstance
 PluginManager::GetObjectContainerCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        ObjectContainerInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessObjectContainerInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetObjectContainerMutex ());
+        ObjectContainerInstances &instances = GetObjectContainerInstances ();
+        
+        ObjectContainerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1054,9 +960,9 @@
 
 #pragma mark LogChannel
 
-struct LogChannelInstance
+struct LogInstance
 {
-    LogChannelInstance() :
+    LogInstance() :
         name(),
         description(),
         create_callback(NULL)
@@ -1068,52 +974,23 @@
     LogChannelCreateInstance create_callback;
 };
 
-typedef std::vector<LogChannelInstance> LogChannelInstances;
+typedef std::vector<LogInstance> LogInstances;
 
-static bool
-AccessLogChannelInstances (PluginAction action, LogChannelInstance &instance, uint32_t index)
+static Mutex &
+GetLogMutex ()
 {
-    static LogChannelInstances g_plugin_instances;
-
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            LogChannelInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
 }
 
+static LogInstances &
+GetLogInstances ()
+{
+    static LogInstances g_instances;
+    return g_instances;
+}
+
+
 
 bool
 PluginManager::RegisterPlugin
@@ -1125,13 +1002,14 @@
 {
     if (create_callback)
     {
-        LogChannelInstance instance;
+        LogInstance instance;
         assert (name && name[0]);
         instance.name = name;
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessLogChannelInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetLogMutex ());
+        GetLogInstances ().push_back (instance);
     }
     return false;
 }
@@ -1141,9 +1019,18 @@
 {
     if (create_callback)
     {
-        LogChannelInstance instance;
-        instance.create_callback = create_callback;
-        return AccessLogChannelInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetLogMutex ());
+        LogInstances &instances = GetLogInstances ();
+        
+        LogInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1151,9 +1038,10 @@
 const char *
 PluginManager::GetLogChannelCreateNameAtIndex (uint32_t idx)
 {
-    LogChannelInstance instance;
-    if (AccessLogChannelInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.name.c_str();
+    Mutex::Locker locker (GetLogMutex ());
+    LogInstances &instances = GetLogInstances ();
+    if (idx < instances.size())
+        return instances[idx].name.c_str();
     return NULL;
 }
 
@@ -1161,9 +1049,10 @@
 LogChannelCreateInstance
 PluginManager::GetLogChannelCreateCallbackAtIndex (uint32_t idx)
 {
-    LogChannelInstance instance;
-    if (AccessLogChannelInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetLogMutex ());
+    LogInstances &instances = GetLogInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -1172,12 +1061,15 @@
 {
     if (name && name[0])
     {
-        LogChannelInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessLogChannelInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetLogMutex ());
+        LogInstances &instances = GetLogInstances ();
+        
+        LogInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1215,6 +1107,7 @@
     return g_platform_instances;
 }
 
+
 bool
 PluginManager::RegisterPlugin (const char *name,
                                const char *description,
@@ -1240,9 +1133,9 @@
 PluginManager::GetPlatformPluginNameAtIndex (uint32_t idx)
 {
     Mutex::Locker locker (GetPlatformInstancesMutex ());
-    PlatformInstances &platform_instances = GetPlatformInstances ();
-    if (idx < platform_instances.size())
-        return platform_instances[idx].name.c_str();
+    PlatformInstances &instances = GetPlatformInstances ();
+    if (idx < instances.size())
+        return instances[idx].name.c_str();
     return NULL;
 }
 
@@ -1250,9 +1143,9 @@
 PluginManager::GetPlatformPluginDescriptionAtIndex (uint32_t idx)
 {
     Mutex::Locker locker (GetPlatformInstancesMutex ());
-    PlatformInstances &platform_instances = GetPlatformInstances ();
-    if (idx < platform_instances.size())
-        return platform_instances[idx].description.c_str();
+    PlatformInstances &instances = GetPlatformInstances ();
+    if (idx < instances.size())
+        return instances[idx].description.c_str();
     return NULL;
 }
 
@@ -1262,14 +1155,14 @@
     if (create_callback)
     {
         Mutex::Locker locker (GetPlatformInstancesMutex ());
-        PlatformInstances &platform_instances = GetPlatformInstances ();
+        PlatformInstances &instances = GetPlatformInstances ();
 
-        PlatformInstances::iterator pos, end = platform_instances.end();
-        for (pos = platform_instances.begin(); pos != end; ++ pos)
+        PlatformInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
             if (pos->create_callback == create_callback)
             {
-                platform_instances.erase(pos);
+                instances.erase(pos);
                 return true;
             }
         }
@@ -1281,9 +1174,9 @@
 PluginManager::GetPlatformCreateCallbackAtIndex (uint32_t idx)
 {
     Mutex::Locker locker (GetPlatformInstancesMutex ());
-    PlatformInstances &platform_instances = GetPlatformInstances ();
-    if (idx < platform_instances.size())
-        return platform_instances[idx].create_callback;
+    PlatformInstances &instances = GetPlatformInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -1293,19 +1186,39 @@
     if (name && name[0])
     {
         Mutex::Locker locker (GetPlatformInstancesMutex ());
-        std::string ss_name(name);
-        PlatformInstances &platform_instances = GetPlatformInstances ();
+        PlatformInstances &instances = GetPlatformInstances ();
+        llvm::StringRef name_sref(name);
 
-        PlatformInstances::iterator pos, end = platform_instances.end();
-        for (pos = platform_instances.begin(); pos != end; ++ pos)
+        PlatformInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (pos->name == ss_name)
+            if (name_sref.equals (name))
                 return pos->create_callback;
         }
     }
     return NULL;
 }
 
+uint32_t
+PluginManager::AutoCompletePlatformName (const char *name, StringList &matches)
+{
+    if (name && name[0])
+    {
+        Mutex::Locker locker (GetPlatformInstancesMutex ());
+        PlatformInstances &instances = GetPlatformInstances ();
+        llvm::StringRef name_sref(name);
+
+        PlatformInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            const char *plugin_name = pos->name.c_str();
+            if (name_sref.startswith(plugin_name))
+                matches.AppendString (plugin_name);
+        }
+    }
+    return matches.GetSize();
+}
+
 #pragma mark Process
 
 struct ProcessInstance
@@ -1324,48 +1237,18 @@
 
 typedef std::vector<ProcessInstance> ProcessInstances;
 
-static bool
-AccessProcessInstances (PluginAction action, ProcessInstance &instance, uint32_t index)
+static Mutex &
+GetProcessMutex ()
 {
-    static ProcessInstances g_plugin_instances;
-    
-    switch (action)
-    {
-        case ePluginRegisterInstance:
-            if (instance.create_callback)
-            {
-                g_plugin_instances.push_back (instance);
-                return true;
-            }
-            break;
-            
-        case ePluginUnregisterInstance:
-            if (instance.create_callback)
-            {
-                ProcessInstances::iterator pos, end = g_plugin_instances.end();
-                for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-                {
-                    if (pos->create_callback == instance.create_callback)
-                    {
-                        g_plugin_instances.erase(pos);
-                        return true;
-                    }
-                }
-            }
-            break;
-            
-        case ePluginGetInstanceAtIndex:
-            if (index < g_plugin_instances.size())
-            {
-                instance = g_plugin_instances[index];
-                return true;
-            }
-            break;
-            
-        default:
-            break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
+
+static ProcessInstances &
+GetProcessInstances ()
+{
+    static ProcessInstances g_instances;
+    return g_instances;
 }
 
 
@@ -1385,7 +1268,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessProcessInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetProcessMutex ());
+        GetProcessInstances ().push_back (instance);
     }
     return false;
 }
@@ -1393,18 +1277,20 @@
 const char *
 PluginManager::GetProcessPluginNameAtIndex (uint32_t idx)
 {
-    ProcessInstance instance;
-    if (AccessProcessInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.name.c_str();
+    Mutex::Locker locker (GetProcessMutex ());
+    ProcessInstances &instances = GetProcessInstances ();
+    if (idx < instances.size())
+        return instances[idx].name.c_str();
     return NULL;
 }
 
 const char *
 PluginManager::GetProcessPluginDescriptionAtIndex (uint32_t idx)
 {
-    ProcessInstance instance;
-    if (AccessProcessInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.description.c_str();
+    Mutex::Locker locker (GetProcessMutex ());
+    ProcessInstances &instances = GetProcessInstances ();
+    if (idx < instances.size())
+        return instances[idx].description.c_str();
     return NULL;
 }
 
@@ -1413,9 +1299,18 @@
 {
     if (create_callback)
     {
-        ProcessInstance instance;
-        instance.create_callback = create_callback;
-        return AccessProcessInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetProcessMutex ());
+        ProcessInstances &instances = GetProcessInstances ();
+        
+        ProcessInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1423,23 +1318,28 @@
 ProcessCreateInstance
 PluginManager::GetProcessCreateCallbackAtIndex (uint32_t idx)
 {
-    ProcessInstance instance;
-    if (AccessProcessInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetProcessMutex ());
+    ProcessInstances &instances = GetProcessInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
+
 ProcessCreateInstance
 PluginManager::GetProcessCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        ProcessInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessProcessInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetProcessMutex ());
+        ProcessInstances &instances = GetProcessInstances ();
+        
+        ProcessInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1463,48 +1363,18 @@
 
 typedef std::vector<SymbolFileInstance> SymbolFileInstances;
 
-static bool
-AccessSymbolFileInstances (PluginAction action, SymbolFileInstance &instance, uint32_t index)
+static Mutex &
+GetSymbolFileMutex ()
 {
-    static SymbolFileInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            SymbolFileInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static SymbolFileInstances &
+GetSymbolFileInstances ()
+{
+    static SymbolFileInstances g_instances;
+    return g_instances;
 }
 
 
@@ -1524,7 +1394,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessSymbolFileInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetSymbolFileMutex ());
+        GetSymbolFileInstances ().push_back (instance);
     }
     return false;
 }
@@ -1534,9 +1405,18 @@
 {
     if (create_callback)
     {
-        SymbolFileInstance instance;
-        instance.create_callback = create_callback;
-        return AccessSymbolFileInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetSymbolFileMutex ());
+        SymbolFileInstances &instances = GetSymbolFileInstances ();
+        
+        SymbolFileInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1544,22 +1424,27 @@
 SymbolFileCreateInstance
 PluginManager::GetSymbolFileCreateCallbackAtIndex (uint32_t idx)
 {
-    SymbolFileInstance instance;
-    if (AccessSymbolFileInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetSymbolFileMutex ());
+    SymbolFileInstances &instances = GetSymbolFileInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
+
 SymbolFileCreateInstance
 PluginManager::GetSymbolFileCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        SymbolFileInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessSymbolFileInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetSymbolFileMutex ());
+        SymbolFileInstances &instances = GetSymbolFileInstances ();
+        
+        SymbolFileInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1585,48 +1470,18 @@
 
 typedef std::vector<SymbolVendorInstance> SymbolVendorInstances;
 
-static bool
-AccessSymbolVendorInstances (PluginAction action, SymbolVendorInstance &instance, uint32_t index)
+static Mutex &
+GetSymbolVendorMutex ()
 {
-    static SymbolVendorInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            SymbolVendorInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static SymbolVendorInstances &
+GetSymbolVendorInstances ()
+{
+    static SymbolVendorInstances g_instances;
+    return g_instances;
 }
 
 bool
@@ -1645,7 +1500,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessSymbolVendorInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetSymbolVendorMutex ());
+        GetSymbolVendorInstances ().push_back (instance);
     }
     return false;
 }
@@ -1655,9 +1511,18 @@
 {
     if (create_callback)
     {
-        SymbolVendorInstance instance;
-        instance.create_callback = create_callback;
-        return AccessSymbolVendorInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetSymbolVendorMutex ());
+        SymbolVendorInstances &instances = GetSymbolVendorInstances ();
+        
+        SymbolVendorInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1665,23 +1530,28 @@
 SymbolVendorCreateInstance
 PluginManager::GetSymbolVendorCreateCallbackAtIndex (uint32_t idx)
 {
-    SymbolVendorInstance instance;
-    if (AccessSymbolVendorInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetSymbolVendorMutex ());
+    SymbolVendorInstances &instances = GetSymbolVendorInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
+
 SymbolVendorCreateInstance
 PluginManager::GetSymbolVendorCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        SymbolVendorInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessSymbolVendorInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetSymbolVendorMutex ());
+        SymbolVendorInstances &instances = GetSymbolVendorInstances ();
+        
+        SymbolVendorInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1706,48 +1576,18 @@
 
 typedef std::vector<UnwindAssemblyProfilerInstance> UnwindAssemblyProfilerInstances;
 
-static bool
-AccessUnwindAssemblyProfilerInstances (PluginAction action, UnwindAssemblyProfilerInstance &instance, uint32_t index)
+static Mutex &
+GetUnwindAssemblyProfilerMutex ()
 {
-    static UnwindAssemblyProfilerInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            UnwindAssemblyProfilerInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static UnwindAssemblyProfilerInstances &
+GetUnwindAssemblyProfilerInstances ()
+{
+    static UnwindAssemblyProfilerInstances g_instances;
+    return g_instances;
 }
 
 bool
@@ -1766,7 +1606,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessUnwindAssemblyProfilerInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetUnwindAssemblyProfilerMutex ());
+        GetUnwindAssemblyProfilerInstances ().push_back (instance);
     }
     return false;
 }
@@ -1776,9 +1617,18 @@
 {
     if (create_callback)
     {
-        UnwindAssemblyProfilerInstance instance;
-        instance.create_callback = create_callback;
-        return AccessUnwindAssemblyProfilerInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetUnwindAssemblyProfilerMutex ());
+        UnwindAssemblyProfilerInstances &instances = GetUnwindAssemblyProfilerInstances ();
+        
+        UnwindAssemblyProfilerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1786,23 +1636,28 @@
 UnwindAssemblyProfilerCreateInstance
 PluginManager::GetUnwindAssemblyProfilerCreateCallbackAtIndex (uint32_t idx)
 {
-    UnwindAssemblyProfilerInstance instance;
-    if (AccessUnwindAssemblyProfilerInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetUnwindAssemblyProfilerMutex ());
+    UnwindAssemblyProfilerInstances &instances = GetUnwindAssemblyProfilerInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
+
 UnwindAssemblyProfilerCreateInstance
 PluginManager::GetUnwindAssemblyProfilerCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        UnwindAssemblyProfilerInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessUnwindAssemblyProfilerInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetUnwindAssemblyProfilerMutex ());
+        UnwindAssemblyProfilerInstances &instances = GetUnwindAssemblyProfilerInstances ();
+        
+        UnwindAssemblyProfilerInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1826,50 +1681,21 @@
 
 typedef std::vector<ArchDefaultUnwindPlanInstance> ArchDefaultUnwindPlanInstances;
 
-static bool
-AccessArchDefaultUnwindPlanInstances (PluginAction action, ArchDefaultUnwindPlanInstance &instance, uint32_t index)
+static Mutex &
+GetArchDefaultUnwindPlanMutex ()
 {
-    static ArchDefaultUnwindPlanInstances g_plugin_instances;
-
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            ArchDefaultUnwindPlanInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
 }
 
+static ArchDefaultUnwindPlanInstances &
+GetArchDefaultUnwindPlanInstances ()
+{
+    static ArchDefaultUnwindPlanInstances g_instances;
+    return g_instances;
+}
+
+
 bool
 PluginManager::RegisterPlugin
 (
@@ -1886,7 +1712,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessArchDefaultUnwindPlanInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetArchDefaultUnwindPlanMutex ());
+        GetArchDefaultUnwindPlanInstances ().push_back (instance);
     }
     return false;
 }
@@ -1896,9 +1723,18 @@
 {
     if (create_callback)
     {
-        ArchDefaultUnwindPlanInstance instance;
-        instance.create_callback = create_callback;
-        return AccessArchDefaultUnwindPlanInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetArchDefaultUnwindPlanMutex ());
+        ArchDefaultUnwindPlanInstances &instances = GetArchDefaultUnwindPlanInstances ();
+        
+        ArchDefaultUnwindPlanInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -1906,23 +1742,28 @@
 ArchDefaultUnwindPlanCreateInstance
 PluginManager::GetArchDefaultUnwindPlanCreateCallbackAtIndex (uint32_t idx)
 {
-    ArchDefaultUnwindPlanInstance instance;
-    if (AccessArchDefaultUnwindPlanInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetArchDefaultUnwindPlanMutex ());
+    ArchDefaultUnwindPlanInstances &instances = GetArchDefaultUnwindPlanInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
+
 ArchDefaultUnwindPlanCreateInstance
 PluginManager::GetArchDefaultUnwindPlanCreateCallbackForPluginName (const char *name)
 {
     if (name && name[0])
     {
-        ArchDefaultUnwindPlanInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessArchDefaultUnwindPlanInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetArchDefaultUnwindPlanMutex ());
+        ArchDefaultUnwindPlanInstances &instances = GetArchDefaultUnwindPlanInstances ();
+        
+        ArchDefaultUnwindPlanInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
@@ -1946,48 +1787,18 @@
 
 typedef std::vector<ArchVolatileRegsInstance> ArchVolatileRegsInstances;
 
-static bool
-AccessArchVolatileRegsInstances (PluginAction action, ArchVolatileRegsInstance &instance, uint32_t index)
+static Mutex &
+GetArchVolatileRegsMutex ()
 {
-    static ArchVolatileRegsInstances g_plugin_instances;
+    static Mutex g_instances_mutex (Mutex::eMutexTypeRecursive);
+    return g_instances_mutex;
+}
 
-    switch (action)
-    {
-    case ePluginRegisterInstance:
-        if (instance.create_callback)
-        {
-            g_plugin_instances.push_back (instance);
-            return true;
-        }
-        break;
-
-    case ePluginUnregisterInstance:
-        if (instance.create_callback)
-        {
-            ArchVolatileRegsInstances::iterator pos, end = g_plugin_instances.end();
-            for (pos = g_plugin_instances.begin(); pos != end; ++ pos)
-            {
-                if (pos->create_callback == instance.create_callback)
-                {
-                    g_plugin_instances.erase(pos);
-                    return true;
-                }
-            }
-        }
-        break;
-
-    case ePluginGetInstanceAtIndex:
-        if (index < g_plugin_instances.size())
-        {
-            instance = g_plugin_instances[index];
-            return true;
-        }
-        break;
-
-    default:
-        break;
-    }
-    return false;
+static ArchVolatileRegsInstances &
+GetArchVolatileRegsInstances ()
+{
+    static ArchVolatileRegsInstances g_instances;
+    return g_instances;
 }
 
 bool
@@ -2006,7 +1817,8 @@
         if (description && description[0])
             instance.description = description;
         instance.create_callback = create_callback;
-        return AccessArchVolatileRegsInstances (ePluginRegisterInstance, instance, 0);
+        Mutex::Locker locker (GetArchVolatileRegsMutex ());
+        GetArchVolatileRegsInstances ().push_back (instance);
     }
     return false;
 }
@@ -2016,9 +1828,18 @@
 {
     if (create_callback)
     {
-        ArchVolatileRegsInstance instance;
-        instance.create_callback = create_callback;
-        return AccessArchVolatileRegsInstances (ePluginUnregisterInstance, instance, 0);
+        Mutex::Locker locker (GetArchVolatileRegsMutex ());
+        ArchVolatileRegsInstances &instances = GetArchVolatileRegsInstances ();
+        
+        ArchVolatileRegsInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
+        {
+            if (pos->create_callback == create_callback)
+            {
+                instances.erase(pos);
+                return true;
+            }
+        }
     }
     return false;
 }
@@ -2026,9 +1847,10 @@
 ArchVolatileRegsCreateInstance
 PluginManager::GetArchVolatileRegsCreateCallbackAtIndex (uint32_t idx)
 {
-    ArchVolatileRegsInstance instance;
-    if (AccessArchVolatileRegsInstances (ePluginGetInstanceAtIndex, instance, idx))
-        return instance.create_callback;
+    Mutex::Locker locker (GetArchVolatileRegsMutex ());
+    ArchVolatileRegsInstances &instances = GetArchVolatileRegsInstances ();
+    if (idx < instances.size())
+        return instances[idx].create_callback;
     return NULL;
 }
 
@@ -2037,12 +1859,15 @@
 {
     if (name && name[0])
     {
-        ArchVolatileRegsInstance instance;
-        std::string ss_name(name);
-        for (uint32_t idx = 0; AccessArchVolatileRegsInstances (ePluginGetInstanceAtIndex, instance, idx); ++idx)
+        llvm::StringRef name_sref(name);
+        Mutex::Locker locker (GetArchVolatileRegsMutex ());
+        ArchVolatileRegsInstances &instances = GetArchVolatileRegsInstances ();
+        
+        ArchVolatileRegsInstances::iterator pos, end = instances.end();
+        for (pos = instances.begin(); pos != end; ++ pos)
         {
-            if (instance.name == ss_name)
-                return instance.create_callback;
+            if (name_sref.equals (pos->name))
+                return pos->create_callback;
         }
     }
     return NULL;
diff --git a/source/Interpreter/CommandObject.cpp b/source/Interpreter/CommandObject.cpp
index baf4fcb..76cee5a 100644
--- a/source/Interpreter/CommandObject.cpp
+++ b/source/Interpreter/CommandObject.cpp
@@ -650,7 +650,7 @@
     { eArgTypeAddress, "address", CommandCompletions::eNoCompletion, NULL, "A valid address in the target program's execution space." },
     { eArgTypeAliasName, "alias-name", CommandCompletions::eNoCompletion, NULL, "The name of an abbreviation (alias) for a debugger command." },
     { eArgTypeAliasOptions, "options-for-aliased-command", CommandCompletions::eNoCompletion, NULL, "Command options to be used as part of an alias (abbreviation) definition.  (See 'help commands alias' for more information.)" },
-    { eArgTypeArchitecture, "arch", CommandCompletions::eNoCompletion, NULL, "The architecture name, e.g. i386 or x86_64." },
+    { eArgTypeArchitecture, "arch", CommandCompletions::eArchitectureCompletion, NULL, "The architecture name, e.g. i386 or x86_64." },
     { eArgTypeBoolean, "boolean", CommandCompletions::eNoCompletion, NULL, "A Boolean value: 'true' or 'false'" },
     { eArgTypeBreakpointID, "breakpt-id", CommandCompletions::eNoCompletion, BreakpointIDHelpTextCallback, NULL },
     { eArgTypeBreakpointIDRange, "breakpt-id-list", CommandCompletions::eNoCompletion, BreakpointIDRangeHelpTextCallback, NULL },
@@ -707,6 +707,7 @@
     { eArgTypeValue, "value", CommandCompletions::eNoCompletion, NULL, "A value could be anything, depending on where and how it is used." },
     { eArgTypeWidth, "width", CommandCompletions::eNoCompletion, NULL, "Help text goes here." },
     { eArgTypeNone, "none", CommandCompletions::eNoCompletion, NULL, "No help available for this." },
+    { eArgTypePlatform, "platform-name", CommandCompletions::ePlatformPluginCompletion, NULL, "The name of an installed platform plug-in . Type 'platform list' to see a complete list of installed platforms." }
 };
 
 const CommandObject::ArgumentTableEntry*
diff --git a/source/Interpreter/Options.cpp b/source/Interpreter/Options.cpp
index 8f1e10a..d6b30ed 100644
--- a/source/Interpreter/Options.cpp
+++ b/source/Interpreter/Options.cpp
@@ -11,8 +11,9 @@
 
 // C Includes
 // C++ Includes
-#include <bitset>
 #include <algorithm>
+#include <bitset>
+#include <set>
 
 // Other libraries and framework includes
 // Project includes
@@ -853,7 +854,19 @@
     // for that shared library.
     // FIXME: Do we want to also have an "OptionType" so we don't have to match string names?
 
-    uint32_t completion_mask = opt_defs[opt_defs_index].completionType;
+    uint32_t completion_mask = opt_defs[opt_defs_index].completion_type;
+    
+    if (completion_mask == 0)
+    {
+        lldb::CommandArgumentType option_arg_type = opt_defs[opt_defs_index].argument_type;
+        if (option_arg_type != eArgTypeNone)
+        {
+            CommandObject::ArgumentTableEntry *arg_entry = CommandObject::FindArgumentDataByType (opt_defs[opt_defs_index].argument_type);
+            if (arg_entry)
+                completion_mask = arg_entry->completion_type;
+        }
+    }
+
     if (completion_mask & CommandCompletions::eSourceFileCompletion
         || completion_mask & CommandCompletions::eSymbolCompletion)
     {
@@ -893,28 +906,21 @@
 }
 
 
-
-
 void
-OptionGroupOptions::Append (OptionGroup* group)
+OptionGroupOptions::Append (OptionGroup* group, 
+                            uint32_t src_mask, 
+                            uint32_t dst_mask)
 {
-    m_option_groups.push_back (group);
-    const OptionDefinition* group_option_defs = group->GetDefinitions ();
-    const uint32_t group_option_count = group->GetNumDefinitions();
-    for (uint32_t i=0; i<group_option_count; ++i)
-        m_option_defs.push_back (group_option_defs[i]);
-}
-
-void
-OptionGroupOptions::Append (OptionGroup* group, uint32_t usage_mask)
-{
-    m_option_groups.push_back (group);
     const OptionDefinition* group_option_defs = group->GetDefinitions ();
     const uint32_t group_option_count = group->GetNumDefinitions();
     for (uint32_t i=0; i<group_option_count; ++i)
     {
-        m_option_defs.push_back (group_option_defs[i]);
-        m_option_defs.back().usage_mask = usage_mask;
+        if (group_option_defs[i].usage_mask & src_mask)
+        {
+            m_option_infos.push_back (OptionInfo (group, i));
+            m_option_defs.push_back (group_option_defs[i]);
+            m_option_defs.back().usage_mask = dst_mask;
+        }
     }
 }
 
@@ -933,38 +939,53 @@
     // After calling OptionGroupOptions::Append(...), you must finalize the groups
     // by calling OptionGroupOptions::Finlize()
     assert (m_did_finalize);
-
-    uint32_t curr_idx = 0;
-    OptionGroupsType::iterator pos, end = m_option_groups.end();
-    for (pos = m_option_groups.begin(); pos != end; ++pos)
-    {
-        const uint32_t num_group_definitions = (*pos)->GetNumDefinitions();
-        if (option_idx < curr_idx + num_group_definitions)
-            return (*pos)->SetOptionValue (m_interpreter, option_idx - curr_idx, option_value);
-        curr_idx += num_group_definitions;
-    }
+    assert (m_option_infos.size() + 1 == m_option_defs.size());
     Error error;
-    error.SetErrorString ("invalid option index"); // Shouldn't happen...
+    if (option_idx < m_option_infos.size())
+    {
+        error = m_option_infos[option_idx].option_group->SetOptionValue (m_interpreter, 
+                                                                         m_option_infos[option_idx].option_index,
+                                                                         option_value);
+        
+    }
+    else
+    {
+        error.SetErrorString ("invalid option index"); // Shouldn't happen...
+    }
     return error;
 }
 
 void
 OptionGroupOptions::OptionParsingStarting ()
 {
-    OptionGroupsType::iterator pos, end = m_option_groups.end();
-    for (pos = m_option_groups.begin(); pos != end; ++pos)
-        (*pos)->OptionParsingStarting (m_interpreter);
+    std::set<OptionGroup*> group_set;
+    OptionInfos::iterator pos, end = m_option_infos.end();
+    for (pos = m_option_infos.begin(); pos != end; ++pos)
+    {
+        OptionGroup* group = pos->option_group;
+        if (group_set.find(group) == group_set.end())
+        {
+            group->OptionParsingStarting (m_interpreter);
+            group_set.insert(group);
+        }
+    }
 }
 Error
 OptionGroupOptions::OptionParsingFinished ()
 {
+    std::set<OptionGroup*> group_set;
     Error error;
-    OptionGroupsType::iterator pos, end = m_option_groups.end();
-    for (pos = m_option_groups.begin(); pos != end; ++pos)
+    OptionInfos::iterator pos, end = m_option_infos.end();
+    for (pos = m_option_infos.begin(); pos != end; ++pos)
     {
-        error = (*pos)->OptionParsingFinished (m_interpreter);
-        if (error.Fail())
-            return error;
+        OptionGroup* group = pos->option_group;
+        if (group_set.find(group) == group_set.end())
+        {
+            error = group->OptionParsingFinished (m_interpreter);
+            group_set.insert(group);
+            if (error.Fail())
+                return error;
+        }
     }
     return error;
 }
diff --git a/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp b/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
index d86ac4e..2251593 100644
--- a/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
+++ b/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
@@ -80,7 +80,7 @@
 PlatformMacOSX::GetShortPluginNameStatic (bool is_host)
 {
     if (is_host)
-        return "local-macosx";
+        return Platform::GetHostPlatformName ();
     else
         return "remote-macosx";
 }
diff --git a/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp b/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
index f8b67a3..66534c3 100644
--- a/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
+++ b/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
@@ -312,7 +312,7 @@
                 break;
         }
     }
-    const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (3000); // TODO: lower this to 5 seconds prior to checkin!!!
+    const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (5);
     int arg_packet_err = m_gdb_client.SendArgumentsPacket (argv);
     m_gdb_client.SetPacketTimeout (old_packet_timeout);
     if (arg_packet_err == 0)
diff --git a/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp b/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
index 9e9f644..aa43d4f 100644
--- a/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
+++ b/source/Plugins/SymbolFile/DWARF/LogChannelDWARF.cpp
@@ -54,16 +54,9 @@
 const char *
 LogChannelDWARF::GetPluginNameStatic()
 {
-    static std::string g_plugin_name;
-    if (g_plugin_name.empty())
-    {
-        g_plugin_name = SymbolFileDWARF::GetPluginNameStatic();
-        g_plugin_name += LogChannel::GetPluginSuffix ();
-    }
-    return g_plugin_name.c_str();
+    return SymbolFileDWARF::GetPluginNameStatic();
 }
 
-
 const char *
 LogChannelDWARF::GetPluginDescriptionStatic()
 {
diff --git a/source/Target/Platform.cpp b/source/Target/Platform.cpp
index 34c7337..5c38ab3 100644
--- a/source/Target/Platform.cpp
+++ b/source/Target/Platform.cpp
@@ -46,6 +46,13 @@
     return g_remote_connected_platforms;
 }
 
+
+const char *
+Platform::GetHostPlatformName ()
+{
+    return "host";
+}
+
 //------------------------------------------------------------------
 /// Get the native host platform plug-in. 
 ///
@@ -319,11 +326,8 @@
 const char *
 Platform::GetHostname ()
 {
-    if (IsHost() && m_name.empty())
-    {
-        if (!Host::GetHostname(m_name))
-            return "localhost";
-    }
+    if (IsHost())
+        return "localhost";
 
     if (m_name.empty())        
         return NULL;