Index: include/lldb/Target/Platform.h =================================================================== --- include/lldb/Target/Platform.h +++ include/lldb/Target/Platform.h @@ -50,8 +50,14 @@ { 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 Index: source/Interpreter/ScriptInterpreterPython.cpp =================================================================== --- source/Interpreter/ScriptInterpreterPython.cpp +++ source/Interpreter/ScriptInterpreterPython.cpp @@ -2825,8 +2825,20 @@ } } + // 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" : ""); Index: source/Plugins/Platform/Android/PlatformAndroid.cpp =================================================================== --- source/Plugins/Platform/Android/PlatformAndroid.cpp +++ source/Plugins/Platform/Android/PlatformAndroid.cpp @@ -25,6 +25,8 @@ void PlatformAndroid::Initialize () { + PlatformLinux::Initialize (); + if (g_initialize_count++ == 0) { PluginManager::RegisterPlugin (PlatformAndroid::GetPluginNameStatic(), @@ -43,6 +45,8 @@ PluginManager::UnregisterPlugin (PlatformAndroid::CreateInstance); } } + + PlatformLinux::Terminate (); } PlatformSP Index: source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp =================================================================== --- source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp +++ source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp @@ -118,6 +118,8 @@ void PlatformFreeBSD::Initialize () { + Platform::Initialize (); + if (g_initialize_count++ == 0) { #if defined (__FreeBSD__) @@ -137,6 +139,8 @@ { if (g_initialize_count > 0 && --g_initialize_count == 0) PluginManager::UnregisterPlugin (PlatformFreeBSD::CreateInstance); + + Platform::Terminate (); } //------------------------------------------------------------------ Index: source/Plugins/Platform/Kalimba/PlatformKalimba.cpp =================================================================== --- source/Plugins/Platform/Kalimba/PlatformKalimba.cpp +++ source/Plugins/Platform/Kalimba/PlatformKalimba.cpp @@ -76,6 +76,8 @@ void PlatformKalimba::Initialize () { + Platform::Initialize (); + if (g_initialize_count++ == 0) { PluginManager::RegisterPlugin(PlatformKalimba::GetPluginNameStatic(false), @@ -94,6 +96,8 @@ PluginManager::UnregisterPlugin (PlatformKalimba::CreateInstance); } } + + Platform::Terminate (); } Error Index: source/Plugins/Platform/Linux/PlatformLinux.cpp =================================================================== --- source/Plugins/Platform/Linux/PlatformLinux.cpp +++ source/Plugins/Platform/Linux/PlatformLinux.cpp @@ -242,6 +242,8 @@ void PlatformLinux::Initialize () { + PlatformPOSIX::Initialize (); + if (g_initialize_count++ == 0) { #if defined(__linux__) @@ -266,6 +268,8 @@ PluginManager::UnregisterPlugin (PlatformLinux::CreateInstance); } } + + PlatformPOSIX::Terminate (); } Error Index: source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp +++ source/Plugins/Platform/MacOSX/PlatformDarwinKernel.cpp @@ -51,6 +51,8 @@ void PlatformDarwinKernel::Initialize () { + PlatformDarwin::Initialize (); + if (g_initialize_count++ == 0) { PluginManager::RegisterPlugin (PlatformDarwinKernel::GetPluginNameStatic(), @@ -70,6 +72,8 @@ PluginManager::UnregisterPlugin (PlatformDarwinKernel::CreateInstance); } } + + PlatformDarwin::Terminate (); } PlatformSP Index: source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp +++ source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp @@ -40,6 +40,8 @@ void PlatformMacOSX::Initialize () { + PlatformDarwin::Initialize (); + if (g_initialize_count++ == 0) { #if defined (__APPLE__) @@ -64,6 +66,8 @@ PluginManager::UnregisterPlugin (PlatformMacOSX::CreateInstance); } } + + PlatformDarwin::Terminate (); } PlatformSP Index: source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp +++ source/Plugins/Platform/MacOSX/PlatformRemoteiOS.cpp @@ -63,6 +63,8 @@ void PlatformRemoteiOS::Initialize () { + PlatformDarwin::Initialize (); + if (g_initialize_count++ == 0) { PluginManager::RegisterPlugin (PlatformRemoteiOS::GetPluginNameStatic(), @@ -81,6 +83,8 @@ PluginManager::UnregisterPlugin (PlatformRemoteiOS::CreateInstance); } } + + PlatformDarwin::Terminate (); } PlatformSP Index: source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp +++ source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp @@ -41,6 +41,8 @@ void PlatformiOSSimulator::Initialize () { + PlatformDarwin::Initialize (); + if (g_initialize_count++ == 0) { PluginManager::RegisterPlugin (PlatformiOSSimulator::GetPluginNameStatic(), @@ -59,6 +61,8 @@ PluginManager::UnregisterPlugin (PlatformiOSSimulator::CreateInstance); } } + + PlatformDarwin::Terminate (); } PlatformSP Index: source/Plugins/Platform/Windows/PlatformWindows.cpp =================================================================== --- source/Plugins/Platform/Windows/PlatformWindows.cpp +++ source/Plugins/Platform/Windows/PlatformWindows.cpp @@ -146,6 +146,8 @@ void PlatformWindows::Initialize(void) { + Platform::Initialize (); + if (g_initialize_count++ == 0) { #if defined (_WIN32) @@ -175,6 +177,8 @@ PluginManager::UnregisterPlugin (PlatformWindows::CreateInstance); } } + + Platform::Terminate (); } //------------------------------------------------------------------ Index: source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp =================================================================== --- source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp @@ -40,6 +40,8 @@ void PlatformRemoteGDBServer::Initialize () { + Platform::Initialize (); + if (g_initialized == false) { g_initialized = true; @@ -57,6 +59,8 @@ g_initialized = false; PluginManager::UnregisterPlugin (PlatformRemoteGDBServer::CreateInstance); } + + Platform::Terminate (); } PlatformSP Index: source/Target/Platform.cpp =================================================================== --- source/Target/Platform.cpp +++ source/Target/Platform.cpp @@ -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 @@ } 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() Index: source/lldb.cpp =================================================================== --- source/lldb.cpp +++ source/lldb.cpp @@ -313,6 +313,7 @@ ProcessFreeBSD::Terminate(); #endif + PlatformRemoteGDBServer::Terminate(); ProcessGDBRemote::Terminate(); DynamicLoaderStatic::Terminate();