[Lldb-commits] [lldb] r228944 - Add Initialize/Terminate method to Platform base plugin

Tamas Berghammer tberghammer at google.com
Thu Feb 12 10:18:27 PST 2015


Author: tberghammer
Date: Thu Feb 12 12:18:27 2015
New Revision: 228944

URL: http://llvm.org/viewvc/llvm-project?rev=228944&view=rev
Log:
Add Initialize/Terminate method to Platform base plugin

Platform holds a smart pointer to each platform object created in a
static variable what cause the platform destructors called only on
program exit when other static variables are not availables. With this
change the destructors are called on lldb_private::Terminate()

+ Fix DebuggerRefCount handling in ScriptInterpreterPython

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

Modified:
    lldb/trunk/include/lldb/Target/Platform.h
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
    lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
    lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
    lldb/trunk/source/Plugins/Platform/Kalimba/PlatformKalimba.cpp
    lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
    lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
    lldb/trunk/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
    lldb/trunk/source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp
    lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
    lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
    lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
    lldb/trunk/source/Target/Platform.cpp
    lldb/trunk/source/lldb.cpp

Modified: lldb/trunk/include/lldb/Target/Platform.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Platform.h?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Platform.h (original)
+++ lldb/trunk/include/lldb/Target/Platform.h Thu Feb 12 12:18:27 2015
@@ -50,8 +50,14 @@ namespace lldb_private {
     {
     public:
 
+        static void
+        Initialize ();
+
+        static void
+        Terminate ();
+
         //------------------------------------------------------------------
-        /// Get the native host platform plug-in. 
+        /// Get the native host platform plug-in.
         ///
         /// There should only be one of these for each host that LLDB runs
         /// upon that should be statically compiled in and registered using

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Thu Feb 12 12:18:27 2015
@@ -2825,8 +2825,20 @@ ScriptInterpreterPython::InitializePriva
         }
     }
 
+    // Importing 'lldb' module calls SBDebugger::Initialize, which calls Debugger::Initialize, which increments a
+    // global debugger ref-count; therefore we need to check the ref-count before and after importing lldb, and if the
+    // ref-count increased we need to call Debugger::Terminate here to decrement the ref-count so that when the final 
+    // call to Debugger::Terminate is made, the ref-count has the correct value. 
+    
+    int old_count = Debugger::TestDebuggerRefCount ();
+
     PyRun_SimpleString ("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line");
 
+    int new_count = Debugger::TestDebuggerRefCount ();
+    
+    if (new_count > old_count)
+        Debugger::Terminate ();
+
     if (threads_already_initialized) {
         if (log)
             log->Printf("Releasing PyGILState. Returning to state = %slocked\n", gstate == PyGILState_UNLOCKED ? "un" : "");

Modified: lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Android/PlatformAndroid.cpp Thu Feb 12 12:18:27 2015
@@ -25,6 +25,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformAndroid::Initialize ()
 {
+    PlatformLinux::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
         PluginManager::RegisterPlugin (PlatformAndroid::GetPluginNameStatic(),
@@ -43,6 +45,8 @@ PlatformAndroid::Terminate ()
             PluginManager::UnregisterPlugin (PlatformAndroid::CreateInstance);
         }
     }
+
+    PlatformLinux::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp Thu Feb 12 12:18:27 2015
@@ -118,6 +118,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformFreeBSD::Initialize ()
 {
+    Platform::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
 #if defined (__FreeBSD__)
@@ -137,6 +139,8 @@ PlatformFreeBSD::Terminate ()
 {
     if (g_initialize_count > 0 && --g_initialize_count == 0)
     	PluginManager::UnregisterPlugin (PlatformFreeBSD::CreateInstance);
+
+    Platform::Terminate ();
 }
 
 //------------------------------------------------------------------

Modified: lldb/trunk/source/Plugins/Platform/Kalimba/PlatformKalimba.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Kalimba/PlatformKalimba.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Kalimba/PlatformKalimba.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Kalimba/PlatformKalimba.cpp Thu Feb 12 12:18:27 2015
@@ -76,6 +76,8 @@ PlatformKalimba::GetPluginName()
 void
 PlatformKalimba::Initialize ()
 {
+    Platform::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
         PluginManager::RegisterPlugin(PlatformKalimba::GetPluginNameStatic(false),
@@ -94,6 +96,8 @@ PlatformKalimba::Terminate ()
             PluginManager::UnregisterPlugin (PlatformKalimba::CreateInstance);
         }
     }
+
+    Platform::Terminate ();
 }
 
 Error

Modified: lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp Thu Feb 12 12:18:27 2015
@@ -242,6 +242,8 @@ PlatformLinux::GetPluginName()
 void
 PlatformLinux::Initialize ()
 {
+    PlatformPOSIX::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
 #if defined(__linux__)
@@ -266,6 +268,8 @@ PlatformLinux::Terminate ()
             PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance);
         }
     }
+
+    PlatformPOSIX::Terminate ();
 }
 
 Error

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp Thu Feb 12 12:18:27 2015
@@ -51,6 +51,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformDarwinKernel::Initialize ()
 {
+    PlatformDarwin::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
         PluginManager::RegisterPlugin (PlatformDarwinKernel::GetPluginNameStatic(),
@@ -70,6 +72,8 @@ PlatformDarwinKernel::Terminate ()
             PluginManager::UnregisterPlugin (PlatformDarwinKernel::CreateInstance);
         }
     }
+
+    PlatformDarwin::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp Thu Feb 12 12:18:27 2015
@@ -40,6 +40,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformMacOSX::Initialize ()
 {
+    PlatformDarwin::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
 #if defined (__APPLE__)
@@ -64,6 +66,8 @@ PlatformMacOSX::Terminate ()
             PluginManager::UnregisterPlugin (PlatformMacOSX::CreateInstance);
         }
     }
+
+    PlatformDarwin::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp Thu Feb 12 12:18:27 2015
@@ -63,6 +63,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformRemoteiOS::Initialize ()
 {
+    PlatformDarwin::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
         PluginManager::RegisterPlugin (PlatformRemoteiOS::GetPluginNameStatic(),
@@ -81,6 +83,8 @@ PlatformRemoteiOS::Terminate ()
             PluginManager::UnregisterPlugin (PlatformRemoteiOS::CreateInstance);
         }
     }
+
+    PlatformDarwin::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp Thu Feb 12 12:18:27 2015
@@ -41,6 +41,8 @@ static uint32_t g_initialize_count = 0;
 void
 PlatformiOSSimulator::Initialize ()
 {
+    PlatformDarwin::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
         PluginManager::RegisterPlugin (PlatformiOSSimulator::GetPluginNameStatic(),
@@ -59,6 +61,8 @@ PlatformiOSSimulator::Terminate ()
             PluginManager::UnregisterPlugin (PlatformiOSSimulator::CreateInstance);
         }
     }
+
+    PlatformDarwin::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp Thu Feb 12 12:18:27 2015
@@ -146,6 +146,8 @@ PlatformWindows::GetPluginName(void)
 void
 PlatformWindows::Initialize(void)
 {
+    Platform::Initialize ();
+
     if (g_initialize_count++ == 0)
     {
 #if defined (_WIN32)
@@ -175,6 +177,8 @@ PlatformWindows::Terminate( void )
             PluginManager::UnregisterPlugin (PlatformWindows::CreateInstance);
         }
     }
+
+    Platform::Terminate ();
 }
 
 //------------------------------------------------------------------

Modified: lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp Thu Feb 12 12:18:27 2015
@@ -40,6 +40,8 @@ static bool g_initialized = false;
 void
 PlatformRemoteGDBServer::Initialize ()
 {
+    Platform::Initialize ();
+
     if (g_initialized == false)
     {
         g_initialized = true;
@@ -57,6 +59,8 @@ PlatformRemoteGDBServer::Terminate ()
         g_initialized = false;
         PluginManager::UnregisterPlugin (PlatformRemoteGDBServer::CreateInstance);
     }
+
+    Platform::Terminate ();
 }
 
 PlatformSP

Modified: lldb/trunk/source/Target/Platform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Platform.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/Target/Platform.cpp (original)
+++ lldb/trunk/source/Target/Platform.cpp Thu Feb 12 12:18:27 2015
@@ -29,7 +29,9 @@
 
 using namespace lldb;
 using namespace lldb_private;
-    
+
+static uint32_t g_initialize_count = 0;
+
 // Use a singleton function for g_local_platform_sp to avoid init
 // constructors since LLDB is often part of a shared library
 static PlatformSP&
@@ -76,6 +78,25 @@ GetPlatformListMutex ()
 }
 
 void
+Platform::Initialize ()
+{
+    g_initialize_count++;
+}
+
+void
+Platform::Terminate ()
+{
+    if (g_initialize_count > 0)
+    {
+        if (--g_initialize_count == 0)
+        {
+            Mutex::Locker locker(GetPlatformListMutex ());
+            GetPlatformList().clear();
+        }
+    }
+}
+
+void
 Platform::SetHostPlatform (const lldb::PlatformSP &platform_sp)
 {
     // The native platform should use its static void Platform::Initialize()

Modified: lldb/trunk/source/lldb.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/lldb.cpp?rev=228944&r1=228943&r2=228944&view=diff
==============================================================================
--- lldb/trunk/source/lldb.cpp (original)
+++ lldb/trunk/source/lldb.cpp Thu Feb 12 12:18:27 2015
@@ -313,6 +313,7 @@ lldb_private::Terminate ()
     ProcessFreeBSD::Terminate();
 #endif
 
+    PlatformRemoteGDBServer::Terminate();
     ProcessGDBRemote::Terminate();
     DynamicLoaderStatic::Terminate();
 





More information about the lldb-commits mailing list