Index: include/lldb/lldb-private.h =================================================================== --- include/lldb/lldb-private.h +++ include/lldb/lldb-private.h @@ -38,6 +38,15 @@ void Initialize(); +//------------------------------------------------------------------ +/// Initializes subset of lldb for LLGS. +/// +/// This function only initializes the set of components and plugins +/// necessary for lldb-platform and lldb-gdbserver, reducing the +/// impact on the statically linked binary size. +//------------------------------------------------------------------ +void +InitializeForLLGS(); //------------------------------------------------------------------ /// Notifies any classes that lldb will be terminating soon. @@ -70,6 +79,18 @@ void Terminate(); +//------------------------------------------------------------------ +/// Terminates subset of lldb initialized by InitializeForLLGS +/// +/// This function optionally can be called when clients are done +/// using lldb functionality to free up any static resources +/// that have been allocated during initialization or during +/// function calls. No lldb functions should be called after +/// calling this function without again calling DCInitialize() +/// again. +//------------------------------------------------------------------ +void +TerminateLLGS(); const char * GetVersion (); Index: source/lldb.cpp =================================================================== --- source/lldb.cpp +++ source/lldb.cpp @@ -110,9 +110,9 @@ } void -lldb_private::Initialize () +lldb_private::InitializeForLLGS () { - // Make sure we inialize only once + // Make sure we initialize only once static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive); static bool g_inited = false; @@ -141,24 +141,13 @@ Log::Initialize(); HostInfo::Initialize(); - Timer::Initialize (); - Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); + Timer::Initialize(); + Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__); - // Initialize LLVM and Clang - llvm::InitializeAllTargets(); - llvm::InitializeAllAsmPrinters(); - llvm::InitializeAllTargetMCs(); - llvm::InitializeAllDisassemblers(); llvm::install_fatal_error_handler(fatal_error_handler, 0); // Initialize plug-ins - ABIMacOSX_i386::Initialize(); - ABIMacOSX_arm::Initialize(); - ABIMacOSX_arm64::Initialize(); - ABISysV_x86_64::Initialize(); - ABISysV_ppc::Initialize(); - ABISysV_ppc64::Initialize(); - DisassemblerLLVMC::Initialize(); + ObjectContainerBSDArchive::Initialize(); ObjectFileELF::Initialize(); SymbolVendorELF::Initialize(); @@ -166,10 +155,10 @@ SymbolFileSymtab::Initialize(); UnwindAssemblyInstEmulation::Initialize(); UnwindAssembly_x86::Initialize(); - EmulateInstructionARM::Initialize (); - EmulateInstructionARM64::Initialize (); - ObjectFilePECOFF::Initialize (); - DynamicLoaderPOSIXDYLD::Initialize (); + EmulateInstructionARM::Initialize(); + EmulateInstructionARM64::Initialize(); + ObjectFilePECOFF::Initialize(); + DynamicLoaderPOSIXDYLD::Initialize(); PlatformFreeBSD::Initialize(); PlatformLinux::Initialize(); PlatformWindows::Initialize(); @@ -177,15 +166,7 @@ PlatformAndroid::Initialize(); SymbolFileDWARFDebugMap::Initialize(); ItaniumABILanguageRuntime::Initialize(); -#ifndef LLDB_DISABLE_PYTHON - ScriptInterpreterPython::InitializePrivate(); - OperatingSystemPython::Initialize(); -#endif - JITLoaderGDB::Initialize(); - ProcessElfCore::Initialize(); - MemoryHistoryASan::Initialize(); - AddressSanitizerRuntime::Initialize(); - + #if defined (__APPLE__) //---------------------------------------------------------------------- // Apple/Darwin hosted plugins @@ -196,8 +177,7 @@ AppleObjCRuntimeV1::Initialize(); ObjectContainerUniversalMachO::Initialize(); ObjectFileMachO::Initialize(); - ProcessKDP::Initialize(); - ProcessMachCore::Initialize(); + SymbolVendorMacOSX::Initialize(); PlatformDarwinKernel::Initialize(); PlatformRemoteiOS::Initialize(); @@ -205,6 +185,45 @@ PlatformiOSSimulator::Initialize(); SystemRuntimeMacOSX::Initialize(); #endif + +#ifndef LLDB_DISABLE_PYTHON + ScriptInterpreterPython::InitializePrivate(); + OperatingSystemPython::Initialize(); +#endif + } +} + +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); + if (!g_inited) + { + g_inited = true; + // Initialize LLVM and Clang + llvm::InitializeAllTargets(); + llvm::InitializeAllAsmPrinters(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllDisassemblers(); + + ABIMacOSX_i386::Initialize(); + ABIMacOSX_arm::Initialize(); + ABIMacOSX_arm64::Initialize(); + ABISysV_x86_64::Initialize(); + ABISysV_ppc::Initialize(); + ABISysV_ppc64::Initialize(); + DisassemblerLLVMC::Initialize(); + + JITLoaderGDB::Initialize(); + ProcessElfCore::Initialize(); + MemoryHistoryASan::Initialize(); + AddressSanitizerRuntime::Initialize(); + #if defined (__linux__) //---------------------------------------------------------------------- // Linux hosted plugins @@ -218,11 +237,15 @@ #if defined (__FreeBSD__) ProcessFreeBSD::Initialize(); #endif - +#if defined (__APPLE__) + ProcessKDP::Initialize(); + ProcessMachCore::Initialize(); +#endif //---------------------------------------------------------------------- // Platform agnostic plugins //---------------------------------------------------------------------- - PlatformRemoteGDBServer::Initialize (); + PlatformRemoteGDBServer::Initialize(); + ProcessGDBRemote::Initialize(); DynamicLoaderStatic::Initialize(); @@ -231,8 +254,9 @@ // The process settings need to know about installed plug-ins, so the Settings must be initialized // AFTER PluginManager::Initialize is called. - + Debugger::SettingsInitialize(); + } } @@ -243,19 +267,9 @@ } void -lldb_private::Terminate () +lldb_private::TerminateLLGS () { 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(); ObjectContainerBSDArchive::Terminate(); ObjectFileELF::Terminate(); SymbolVendorELF::Terminate(); @@ -274,14 +288,7 @@ PlatformAndroid::Terminate(); SymbolFileDWARFDebugMap::Terminate(); ItaniumABILanguageRuntime::Terminate(); -#ifndef LLDB_DISABLE_PYTHON - OperatingSystemPython::Terminate(); -#endif - JITLoaderGDB::Terminate(); - ProcessElfCore::Terminate(); - MemoryHistoryASan::Terminate(); - AddressSanitizerRuntime::Terminate(); - + #if defined (__APPLE__) DynamicLoaderMacOSXDYLD::Terminate(); DynamicLoaderDarwinKernel::Terminate(); @@ -289,8 +296,6 @@ AppleObjCRuntimeV1::Terminate(); ObjectContainerUniversalMachO::Terminate(); ObjectFileMachO::Terminate(); - ProcessMachCore::Terminate(); - ProcessKDP::Terminate(); SymbolVendorMacOSX::Terminate(); PlatformMacOSX::Terminate(); PlatformDarwinKernel::Terminate(); @@ -299,8 +304,36 @@ SystemRuntimeMacOSX::Terminate(); #endif - Debugger::SettingsTerminate (); +#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 defined (__APPLE__) + ProcessMachCore::Terminate(); + ProcessKDP::Terminate(); +#endif #if defined(_MSC_VER) DynamicLoaderWindows::Terminate(); #endif @@ -312,12 +345,12 @@ #if defined (__FreeBSD__) ProcessFreeBSD::Terminate(); #endif + Debugger::SettingsTerminate (); PlatformRemoteGDBServer::Terminate(); ProcessGDBRemote::Terminate(); DynamicLoaderStatic::Terminate(); - - Log::Terminate(); + TerminateLLGS(); } #if defined (__APPLE__) Index: tools/lldb-server/lldb-gdbserver.cpp =================================================================== --- tools/lldb-server/lldb-gdbserver.cpp +++ tools/lldb-server/lldb-gdbserver.cpp @@ -507,8 +507,6 @@ std::string named_pipe_path; bool reverse_connect = false; - Debugger::Initialize (NULL); - lldb::DebuggerSP debugger_sp = Debugger::CreateInstance (); debugger_sp->SetInputFileHandle(stdin, false); @@ -689,8 +687,6 @@ ConnectToRemote (gdb_server, reverse_connect, host_and_port, progname, subcommand, named_pipe_path.c_str ()); - Debugger::Terminate (); - fprintf(stderr, "lldb-gdbserver exiting...\n"); return 0; Index: tools/lldb-server/lldb-platform.cpp =================================================================== --- tools/lldb-server/lldb-platform.cpp +++ tools/lldb-server/lldb-platform.cpp @@ -111,7 +111,6 @@ Error error; std::string listen_host_port; int ch; - Debugger::Initialize(NULL); lldb::DebuggerSP debugger_sp = Debugger::CreateInstance (); @@ -306,8 +305,6 @@ } } while (g_stay_alive); - Debugger::Terminate(); - fprintf(stderr, "lldb-platform exiting...\n"); return 0; Index: tools/lldb-server/lldb-server.cpp =================================================================== --- tools/lldb-server/lldb-server.cpp +++ tools/lldb-server/lldb-server.cpp @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +#include "lldb/lldb-private.h" + #include #include @@ -24,6 +26,19 @@ int main_gdbserver (int argc, char *argv[]); int main_platform (int argc, char *argv[]); +static void +initialize () +{ + lldb_private::InitializeForLLGS(); +} + +static void +terminate () +{ + lldb_private::WillTerminate(); + lldb_private::TerminateLLGS(); +} + //---------------------------------------------------------------------- // main //---------------------------------------------------------------------- @@ -39,11 +54,15 @@ } else if (argv[1][0] == 'g') { + initialize(); main_gdbserver(argc, argv); + terminate(); } else if (argv[1][0] == 'p') { + initialize(); main_platform(argc, argv); + terminate(); } else { display_usage(progname);