Add Debugger::InitializeForLLGS to allow ref counted LLGS initialization.
authorRobert Flack <flackr@gmail.com>
Tue, 10 Mar 2015 18:07:47 +0000 (18:07 +0000)
committerRobert Flack <flackr@gmail.com>
Tue, 10 Mar 2015 18:07:47 +0000 (18:07 +0000)
After http://reviews.llvm.org/D8133 landed as r231550 process launch on remote platform stopped working.

This adds Debugger::InitializeForLLGS and tracks whether one or both of Initialize and InitializeForLLGS have been called, calling only the corresponding lldb_private::Terminate* methods as necessary. Since lldb_private::Terminate calls lldb_private::TerminateForLLGS, the latter method may be called twice if Initialize was called for both however the terminate methods ensure they are only called once after being initialized.

This still maintains the reduced binary size, though it does now technically link in lldb_private::Terminate on lldb-server even though this should never be called.

This should resolve the issue raised in http://reviews.llvm.org/D8133 where Debugger::Terminate assumed that there were 0 references to debugger and terminated early.

Differential Revision: http://reviews.llvm.org/D8183

llvm-svn: 231808

lldb/include/lldb/Core/Debugger.h
lldb/source/Core/Debugger.cpp
lldb/source/lldb.cpp
lldb/tools/lldb-server/lldb-server.cpp

index 9485170..972c636 100644 (file)
@@ -69,6 +69,9 @@ public:
     FindTargetWithProcess (Process *process);
 
     static void
+    InitializeForLLGS (LoadPluginCallbackType load_plugin_callback);
+
+    static void
     Initialize (LoadPluginCallbackType load_plugin_callback);
     
     static void 
index fb60f1f..17eb924 100644 (file)
@@ -412,12 +412,24 @@ Debugger::TestDebuggerRefCount ()
     return g_shared_debugger_refcount;
 }
 
+static bool lldb_initialized_for_llgs = false;
+void
+Debugger::InitializeForLLGS (LoadPluginCallbackType load_plugin_callback)
+{
+    lldb_initialized_for_llgs = true;
+    g_shared_debugger_refcount++;
+    g_load_plugin_callback = load_plugin_callback;
+    lldb_private::InitializeForLLGS();
+}
+
+static bool lldb_initialized = true;
 void
 Debugger::Initialize (LoadPluginCallbackType load_plugin_callback)
 {
+    lldb_initialized = true;
+    g_shared_debugger_refcount++;
     g_load_plugin_callback = load_plugin_callback;
-    if (g_shared_debugger_refcount++ == 0)
-        lldb_private::Initialize();
+    lldb_private::Initialize();
 }
 
 void
@@ -429,7 +441,14 @@ Debugger::Terminate ()
         if (g_shared_debugger_refcount == 0)
         {
             lldb_private::WillTerminate();
-            lldb_private::Terminate();
+            if (lldb_initialized_for_llgs) {
+                lldb_initialized_for_llgs = false;
+                lldb_private::TerminateLLGS();
+            }
+            if (lldb_initialized) {
+                lldb_initialized = false;
+                lldb_private::Terminate();
+            }
 
             // Clear our master list of debugger objects
             Mutex::Locker locker (GetDebuggerListMutex ());
index 69fdb10..af77902 100644 (file)
@@ -109,17 +109,17 @@ static void fatal_error_handler(void *user_data, const std::string& reason,
     ::abort();
 }
 
+static bool g_inited_for_llgs = false;
 void
 lldb_private::InitializeForLLGS ()
 {
     // Make sure we initialize only once
     static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
-    static bool g_inited = false;
 
     Mutex::Locker locker(g_inited_mutex);
-    if (!g_inited)
+    if (!g_inited_for_llgs)
     {
-        g_inited = true;
+        g_inited_for_llgs = true;
 
 #if defined(_MSC_VER)
         const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
@@ -186,15 +186,19 @@ lldb_private::InitializeForLLGS ()
         PlatformDarwinKernel::Initialize();
         ObjectFileMachO::Initialize();
 #endif
+#ifndef LLDB_DISABLE_PYTHON
+        ScriptInterpreterPython::InitializePrivate();
+        OperatingSystemPython::Initialize();
+#endif
     }
 }
 
+static bool g_inited = false;
 void
 lldb_private::Initialize ()
 {
     // Make sure we initialize only once
     static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
-    static bool g_inited = false;
 
     InitializeForLLGS();
     Mutex::Locker locker(g_inited_mutex);
@@ -202,11 +206,6 @@ lldb_private::Initialize ()
     {
         g_inited = true;
 
-#ifndef LLDB_DISABLE_PYTHON
-        ScriptInterpreterPython::InitializePrivate();
-        OperatingSystemPython::Initialize();
-#endif
-
         // Initialize LLVM and Clang
         llvm::InitializeAllTargets();
         llvm::InitializeAllAsmPrinters();
@@ -271,89 +270,99 @@ lldb_private::WillTerminate()
 void
 lldb_private::TerminateLLGS ()
 {
-    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
-    ObjectContainerBSDArchive::Terminate();
-    ObjectFileELF::Terminate();
-    SymbolVendorELF::Terminate();
-    SymbolFileDWARF::Terminate();
-    SymbolFileSymtab::Terminate();
-    UnwindAssembly_x86::Terminate();
-    UnwindAssemblyInstEmulation::Terminate();
-    EmulateInstructionARM::Terminate ();
-    EmulateInstructionARM64::Terminate ();
-    ObjectFilePECOFF::Terminate ();
-    DynamicLoaderPOSIXDYLD::Terminate ();
-    PlatformFreeBSD::Terminate();
-    PlatformLinux::Terminate();
-    PlatformWindows::Terminate();
-    PlatformKalimba::Terminate();
-    PlatformAndroid::Terminate();
-    SymbolFileDWARFDebugMap::Terminate();
-    ItaniumABILanguageRuntime::Terminate();
-    DynamicLoaderMacOSXDYLD::Terminate();
-    AppleObjCRuntimeV2::Terminate();
-    AppleObjCRuntimeV1::Terminate();
-    ObjectContainerUniversalMachO::Terminate();
-    PlatformMacOSX::Terminate();
-    PlatformRemoteiOS::Terminate();
-    PlatformiOSSimulator::Terminate();
-    SystemRuntimeMacOSX::Terminate();
+    if (g_inited_for_llgs)
+    {
+        g_inited_for_llgs = false;
+
+        Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
+        ObjectContainerBSDArchive::Terminate();
+        ObjectFileELF::Terminate();
+        SymbolVendorELF::Terminate();
+        SymbolFileDWARF::Terminate();
+        SymbolFileSymtab::Terminate();
+        UnwindAssembly_x86::Terminate();
+        UnwindAssemblyInstEmulation::Terminate();
+        EmulateInstructionARM::Terminate ();
+        EmulateInstructionARM64::Terminate ();
+        ObjectFilePECOFF::Terminate ();
+        DynamicLoaderPOSIXDYLD::Terminate ();
+        PlatformFreeBSD::Terminate();
+        PlatformLinux::Terminate();
+        PlatformWindows::Terminate();
+        PlatformKalimba::Terminate();
+        PlatformAndroid::Terminate();
+        SymbolFileDWARFDebugMap::Terminate();
+        ItaniumABILanguageRuntime::Terminate();
+        DynamicLoaderMacOSXDYLD::Terminate();
+        AppleObjCRuntimeV2::Terminate();
+        AppleObjCRuntimeV1::Terminate();
+        ObjectContainerUniversalMachO::Terminate();
+        PlatformMacOSX::Terminate();
+        PlatformRemoteiOS::Terminate();
+        PlatformiOSSimulator::Terminate();
+        SystemRuntimeMacOSX::Terminate();
 
 #if defined (__APPLE__)
-    DynamicLoaderDarwinKernel::Terminate();
-    ObjectFileMachO::Terminate();
-    PlatformDarwinKernel::Terminate();
-    SymbolVendorMacOSX::Terminate();
+        DynamicLoaderDarwinKernel::Terminate();
+        ObjectFileMachO::Terminate();
+        PlatformDarwinKernel::Terminate();
+        SymbolVendorMacOSX::Terminate();
 #endif
 
-    Log::Terminate();
+#ifndef LLDB_DISABLE_PYTHON
+        OperatingSystemPython::Terminate();
+#endif
+
+        Log::Terminate();
+    }
 }
 
 void
 lldb_private::Terminate ()
 {
-    Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
-    // Terminate and unload and loaded system or user LLDB plug-ins
-    PluginManager::Terminate();
-    ABIMacOSX_i386::Terminate();
-    ABIMacOSX_arm::Terminate();
-    ABIMacOSX_arm64::Terminate();
-    ABISysV_x86_64::Terminate();
-    ABISysV_ppc::Terminate();
-    ABISysV_ppc64::Terminate();
-    DisassemblerLLVMC::Terminate();
-
-    JITLoaderGDB::Terminate();
-    ProcessElfCore::Terminate();
-    MemoryHistoryASan::Terminate();
-    AddressSanitizerRuntime::Terminate();
+    if (g_inited)
+    {
+        g_inited = false;
+
+        Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
+        // Terminate and unload and loaded system or user LLDB plug-ins
+        PluginManager::Terminate();
+        ABIMacOSX_i386::Terminate();
+        ABIMacOSX_arm::Terminate();
+        ABIMacOSX_arm64::Terminate();
+        ABISysV_x86_64::Terminate();
+        ABISysV_ppc::Terminate();
+        ABISysV_ppc64::Terminate();
+        DisassemblerLLVMC::Terminate();
+
+        JITLoaderGDB::Terminate();
+        ProcessElfCore::Terminate();
+        MemoryHistoryASan::Terminate();
+        AddressSanitizerRuntime::Terminate();
 
 #if defined (__APPLE__)
-    ProcessMachCore::Terminate();
-    ProcessKDP::Terminate();
+        ProcessMachCore::Terminate();
+        ProcessKDP::Terminate();
 #endif
 #if defined(_MSC_VER)
-    DynamicLoaderWindows::Terminate();
+        DynamicLoaderWindows::Terminate();
 #endif
 
 #if defined (__linux__)
-    ProcessLinux::Terminate();
+        ProcessLinux::Terminate();
 #endif
 
 #if defined (__FreeBSD__)
-    ProcessFreeBSD::Terminate();
+        ProcessFreeBSD::Terminate();
 #endif
-    Debugger::SettingsTerminate ();
+        Debugger::SettingsTerminate ();
 
-    PlatformRemoteGDBServer::Terminate();
-    ProcessGDBRemote::Terminate();
-    DynamicLoaderStatic::Terminate();
+        PlatformRemoteGDBServer::Terminate();
+        ProcessGDBRemote::Terminate();
+        DynamicLoaderStatic::Terminate();
 
-#ifndef LLDB_DISABLE_PYTHON
-    OperatingSystemPython::Terminate();
-#endif
-
-    TerminateLLGS();
+        TerminateLLGS();
+    }
 }
 
 #if defined (__APPLE__)
index d439a04..83e88a8 100644 (file)
@@ -7,7 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "lldb/lldb-private.h"
+#include "lldb/Core/Debugger.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -29,14 +29,13 @@ int main_platform (int argc, char *argv[]);
 static void
 initialize ()
 {
-    lldb_private::InitializeForLLGS();
+    lldb_private::Debugger::InitializeForLLGS(NULL);
 }
 
 static void
 terminate ()
 {
-    lldb_private::WillTerminate();
-    lldb_private::TerminateLLGS();
+    lldb_private::Debugger::Terminate();
 }
 
 //----------------------------------------------------------------------