91 changes: 91 additions & 0 deletions lldb/include/lldb/Interpreter/ScriptInterpreterPython.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,97 @@ class ScriptInterpreterPython :
public IOHandlerDelegateMultiline
{
public:
typedef void (*SWIGInitCallback) (void);

typedef bool (*SWIGBreakpointCallbackFunction) (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& frame_sp,
const lldb::BreakpointLocationSP &bp_loc_sp);

typedef bool (*SWIGWatchpointCallbackFunction) (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& frame_sp,
const lldb::WatchpointSP &wp_sp);

typedef bool (*SWIGPythonTypeScriptCallbackFunction) (const char *python_function_name,
void *session_dictionary,
const lldb::ValueObjectSP& valobj_sp,
void** pyfunct_wrapper,
const lldb::TypeSummaryOptionsSP& options,
std::string& retval);

typedef void* (*SWIGPythonCreateSyntheticProvider) (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ValueObjectSP& valobj_sp);

typedef void* (*SWIGPythonCreateCommandObject) (const char *python_class_name,
const char *session_dictionary_name,
const lldb::DebuggerSP debugger_sp);

typedef void* (*SWIGPythonCreateScriptedThreadPlan) (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ThreadPlanSP& thread_plan_sp);

typedef bool (*SWIGPythonCallThreadPlan) (void *implementor, const char *method_name, Event *event_sp, bool &got_error);

typedef void* (*SWIGPythonCreateOSPlugin) (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ProcessSP& process_sp);

typedef size_t (*SWIGPythonCalculateNumChildren) (void *implementor);
typedef void* (*SWIGPythonGetChildAtIndex) (void *implementor, uint32_t idx);
typedef int (*SWIGPythonGetIndexOfChildWithName) (void *implementor, const char* child_name);
typedef void* (*SWIGPythonCastPyObjectToSBValue) (void* data);
typedef lldb::ValueObjectSP (*SWIGPythonGetValueObjectSPFromSBValue) (void* data);
typedef bool (*SWIGPythonUpdateSynthProviderInstance) (void* data);
typedef bool (*SWIGPythonMightHaveChildrenSynthProviderInstance) (void* data);
typedef void* (*SWIGPythonGetValueSynthProviderInstance) (void *implementor);

typedef bool (*SWIGPythonCallCommand) (const char *python_function_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject& cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

typedef bool (*SWIGPythonCallCommandObject) (void *implementor,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject& cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);


typedef bool (*SWIGPythonCallModuleInit) (const char *python_module_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger);

typedef bool (*SWIGPythonScriptKeyword_Process) (const char* python_function_name,
const char* session_dictionary_name,
lldb::ProcessSP& process,
std::string& output);
typedef bool (*SWIGPythonScriptKeyword_Thread) (const char* python_function_name,
const char* session_dictionary_name,
lldb::ThreadSP& thread,
std::string& output);

typedef bool (*SWIGPythonScriptKeyword_Target) (const char* python_function_name,
const char* session_dictionary_name,
lldb::TargetSP& target,
std::string& output);

typedef bool (*SWIGPythonScriptKeyword_Frame) (const char* python_function_name,
const char* session_dictionary_name,
lldb::StackFrameSP& frame,
std::string& output);

typedef bool (*SWIGPythonScriptKeyword_Value) (const char* python_function_name,
const char* session_dictionary_name,
lldb::ValueObjectSP& value,
std::string& output);

typedef void* (*SWIGPython_GetDynamicSetting) (void* module,
const char* setting,
const lldb::TargetSP& target_sp);

friend class IOHandlerPythonInterpreter;

Expand Down
30 changes: 24 additions & 6 deletions lldb/lldb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@
3F8160A61AB9F7DD001DA9DF /* Logging.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F8160A51AB9F7DD001DA9DF /* Logging.cpp */; };
3F8169191ABA2419001DA9DF /* ConvertEnum.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F8169171ABA2419001DA9DF /* ConvertEnum.cpp */; };
3F81691A1ABA2419001DA9DF /* NameMatches.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F8169181ABA2419001DA9DF /* NameMatches.cpp */; };
3F8169281ABB73D9001DA9DF /* InitializeLLDB.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F8169271ABB73D9001DA9DF /* InitializeLLDB.cpp */; };
3F81692C1ABB7A1E001DA9DF /* SystemInitializerFull.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F81692A1ABB7A16001DA9DF /* SystemInitializerFull.cpp */; };
3F8169311ABB7A6D001DA9DF /* SystemInitializer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F81692E1ABB7A6D001DA9DF /* SystemInitializer.cpp */; };
3F8169321ABB7A6D001DA9DF /* SystemInitializerCommon.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F81692F1ABB7A6D001DA9DF /* SystemInitializerCommon.cpp */; };
3F8169331ABB7A6D001DA9DF /* SystemLifetimeManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3F8169301ABB7A6D001DA9DF /* SystemLifetimeManager.cpp */; };
3FDFDDBD199C3A06009756A7 /* FileAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDBC199C3A06009756A7 /* FileAction.cpp */; };
3FDFDDBF199D345E009756A7 /* FileCache.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDBE199D345E009756A7 /* FileCache.cpp */; };
3FDFDDC6199D37ED009756A7 /* FileSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3FDFDDC5199D37ED009756A7 /* FileSystem.cpp */; };
Expand Down Expand Up @@ -2167,8 +2170,14 @@
3F8169181ABA2419001DA9DF /* NameMatches.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = NameMatches.cpp; path = source/Utility/NameMatches.cpp; sourceTree = "<group>"; };
3F81691B1ABA242B001DA9DF /* ConvertEnum.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ConvertEnum.h; path = include/lldb/Utility/ConvertEnum.h; sourceTree = "<group>"; };
3F81691C1ABA242B001DA9DF /* NameMatches.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = NameMatches.h; path = include/lldb/Utility/NameMatches.h; sourceTree = "<group>"; };
3F8169271ABB73D9001DA9DF /* InitializeLLDB.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = InitializeLLDB.cpp; path = source/Initialization/InitializeLLDB.cpp; sourceTree = "<group>"; };
3F8169291ABB73E6001DA9DF /* InitializeLLDB.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = InitializeLLDB.h; path = include/lldb/Initialization/InitializeLLDB.h; sourceTree = "<group>"; };
3F81692A1ABB7A16001DA9DF /* SystemInitializerFull.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SystemInitializerFull.cpp; path = source/API/SystemInitializerFull.cpp; sourceTree = "<group>"; };
3F81692D1ABB7A40001DA9DF /* SystemInitializerFull.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SystemInitializerFull.h; path = include/lldb/API/SystemInitializerFull.h; sourceTree = "<group>"; };
3F81692E1ABB7A6D001DA9DF /* SystemInitializer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SystemInitializer.cpp; path = source/Initialization/SystemInitializer.cpp; sourceTree = "<group>"; };
3F81692F1ABB7A6D001DA9DF /* SystemInitializerCommon.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SystemInitializerCommon.cpp; path = source/Initialization/SystemInitializerCommon.cpp; sourceTree = "<group>"; };
3F8169301ABB7A6D001DA9DF /* SystemLifetimeManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SystemLifetimeManager.cpp; path = source/Initialization/SystemLifetimeManager.cpp; sourceTree = "<group>"; };
3F8169341ABB7A80001DA9DF /* SystemInitializer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SystemInitializer.h; path = include/lldb/Initialization/SystemInitializer.h; sourceTree = "<group>"; };
3F8169351ABB7A80001DA9DF /* SystemInitializerCommon.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SystemInitializerCommon.h; path = include/lldb/Initialization/SystemInitializerCommon.h; sourceTree = "<group>"; };
3F8169361ABB7A80001DA9DF /* SystemLifetimeManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SystemLifetimeManager.h; path = include/lldb/Initialization/SystemLifetimeManager.h; sourceTree = "<group>"; };
3FDFD6C3199C396E009756A7 /* FileAction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = FileAction.h; path = include/lldb/Target/FileAction.h; sourceTree = "<group>"; };
3FDFDDBC199C3A06009756A7 /* FileAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FileAction.cpp; path = source/Target/FileAction.cpp; sourceTree = "<group>"; };
3FDFDDBE199D345E009756A7 /* FileCache.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = FileCache.cpp; path = source/Host/common/FileCache.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3326,6 +3335,8 @@
94235B9B1A8D5FF300EB2EED /* SBVariablesOptions.cpp */,
B2A58721143119810092BFBA /* SBWatchpoint.h */,
B2A58723143119D50092BFBA /* SBWatchpoint.cpp */,
3F81692D1ABB7A40001DA9DF /* SystemInitializerFull.h */,
3F81692A1ABB7A16001DA9DF /* SystemInitializerFull.cpp */,
);
name = API;
sourceTree = "<group>";
Expand Down Expand Up @@ -4783,8 +4794,12 @@
3F8169261ABB73C1001DA9DF /* Initialization */ = {
isa = PBXGroup;
children = (
3F8169291ABB73E6001DA9DF /* InitializeLLDB.h */,
3F8169271ABB73D9001DA9DF /* InitializeLLDB.cpp */,
3F8169341ABB7A80001DA9DF /* SystemInitializer.h */,
3F81692E1ABB7A6D001DA9DF /* SystemInitializer.cpp */,
3F8169351ABB7A80001DA9DF /* SystemInitializerCommon.h */,
3F81692F1ABB7A6D001DA9DF /* SystemInitializerCommon.cpp */,
3F8169361ABB7A80001DA9DF /* SystemLifetimeManager.h */,
3F8169301ABB7A6D001DA9DF /* SystemLifetimeManager.cpp */,
);
name = Initialization;
sourceTree = "<group>";
Expand Down Expand Up @@ -5810,6 +5825,7 @@
26680324116005D9008E1FE4 /* SBThread.cpp in Sources */,
26680326116005DB008E1FE4 /* SBTarget.cpp in Sources */,
26680327116005DC008E1FE4 /* SBSourceManager.cpp in Sources */,
3F81692C1ABB7A1E001DA9DF /* SystemInitializerFull.cpp in Sources */,
26680328116005DE008E1FE4 /* SBProcess.cpp in Sources */,
2668032A116005E0008E1FE4 /* SBListener.cpp in Sources */,
2668032C116005E2008E1FE4 /* SBFrame.cpp in Sources */,
Expand Down Expand Up @@ -5927,6 +5943,7 @@
2689002413353DDE00698AC0 /* CommandObjectSettings.cpp in Sources */,
2689002513353DDE00698AC0 /* CommandObjectSource.cpp in Sources */,
2689002613353DDE00698AC0 /* CommandObjectSyntax.cpp in Sources */,
3F8169331ABB7A6D001DA9DF /* SystemLifetimeManager.cpp in Sources */,
4959511F1A1BC4BC00F6F8FC /* ClangModulesDeclVendor.cpp in Sources */,
26BC179918C7F2B300D2196D /* JITLoader.cpp in Sources */,
2689002713353DDE00698AC0 /* CommandObjectTarget.cpp in Sources */,
Expand Down Expand Up @@ -5995,6 +6012,7 @@
8CCB017E19BA28A80009FD44 /* ThreadCollection.cpp in Sources */,
2689005613353E0400698AC0 /* Value.cpp in Sources */,
2689005713353E0400698AC0 /* ValueObject.cpp in Sources */,
3F8169321ABB7A6D001DA9DF /* SystemInitializerCommon.cpp in Sources */,
2689005813353E0400698AC0 /* ValueObjectChild.cpp in Sources */,
E7723D441AC4A7FB002BA082 /* RegisterContextPOSIXCore_arm64.cpp in Sources */,
233B007F1960CB280090E598 /* ProcessLaunchInfo.cpp in Sources */,
Expand Down Expand Up @@ -6086,6 +6104,7 @@
268900B013353E5000698AC0 /* RegisterContextLLDB.cpp in Sources */,
3FDFE56C19AF9C44009756A7 /* HostProcessPosix.cpp in Sources */,
268900B413353E5000698AC0 /* RegisterContextMacOSXFrameBackchain.cpp in Sources */,
3F8169311ABB7A6D001DA9DF /* SystemInitializer.cpp in Sources */,
3FDFED2D19C257A0009756A7 /* HostProcess.cpp in Sources */,
268900B513353E5000698AC0 /* StopInfoMachException.cpp in Sources */,
268900B613353E5000698AC0 /* UnwindMacOSXFrameBackchain.cpp in Sources */,
Expand Down Expand Up @@ -6377,7 +6396,6 @@
94CB257216B0A4270059775D /* TypeSynthetic.cpp in Sources */,
94CB257416B1D3880059775D /* FormatCache.cpp in Sources */,
A36FF33C17D8E94600244D40 /* OptionParser.cpp in Sources */,
3F8169281ABB73D9001DA9DF /* InitializeLLDB.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions lldb/source/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ add_lldb_library(liblldb SHARED
SBVariablesOptions.cpp
SBWatchpoint.cpp
SBUnixSignals.cpp
SystemInitializerFull.cpp
${LLDB_WRAP_PYTHON}
${LLDB_VERS_GENERATED_FILE}
)
Expand Down
177 changes: 0 additions & 177 deletions lldb/source/API/SBCommandInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,183 +588,6 @@ SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
return false;
}

#ifndef LLDB_DISABLE_PYTHON

// Defined in the SWIG source file
extern "C" void
init_lldb(void);

// these are the Pythonic implementations of the required callbacks
// these are scripting-language specific, which is why they belong here
// we still need to use function pointers to them instead of relying
// on linkage-time resolution because the SWIG stuff and this file
// get built at different times
extern "C" bool
LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& sb_frame,
const lldb::BreakpointLocationSP& sb_bp_loc);

extern "C" bool
LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& sb_frame,
const lldb::WatchpointSP& sb_wp);

extern "C" bool
LLDBSwigPythonCallTypeScript (const char *python_function_name,
void *session_dictionary,
const lldb::ValueObjectSP& valobj_sp,
void** pyfunct_wrapper,
const lldb::TypeSummaryOptionsSP& options_sp,
std::string& retval);

extern "C" void*
LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ValueObjectSP& valobj_sp);

extern "C" void*
LLDBSwigPythonCreateCommandObject (const char *python_class_name,
const char *session_dictionary_name,
const lldb::DebuggerSP debugger_sp);

extern "C" void*
LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ThreadPlanSP& thread_plan_sp);

extern "C" bool
LLDBSWIGPythonCallThreadPlan (void *implementor,
const char *method_name,
Event *event_sp,
bool &got_error);

extern "C" size_t
LLDBSwigPython_CalculateNumChildren (void *implementor);

extern "C" void *
LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);

extern "C" int
LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);

extern "C" void *
LLDBSWIGPython_CastPyObjectToSBValue (void* data);

extern lldb::ValueObjectSP
LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);

extern "C" bool
LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);

extern "C" bool
LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);

extern "C" void *
LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);

extern "C" bool
LLDBSwigPythonCallCommand (const char *python_function_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

extern "C" bool
LLDBSwigPythonCallCommandObject (void *implementor,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject& cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

extern "C" bool
LLDBSwigPythonCallModuleInit (const char *python_module_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger);

extern "C" void*
LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ProcessSP& process_sp);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
const char* session_dictionary_name,
lldb::ProcessSP& process,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
const char* session_dictionary_name,
lldb::ThreadSP& thread,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
const char* session_dictionary_name,
lldb::TargetSP& target,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
const char* session_dictionary_name,
lldb::StackFrameSP& frame,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name,
const char* session_dictionary_name,
lldb::ValueObjectSP& value,
std::string& output);

extern "C" void*
LLDBSWIGPython_GetDynamicSetting (void* module,
const char* setting,
const lldb::TargetSP& target_sp);


#endif

void
SBCommandInterpreter::InitializeSWIG ()
{
static bool g_initialized = false;
if (!g_initialized)
{
g_initialized = true;
#ifndef LLDB_DISABLE_PYTHON
ScriptInterpreter::InitializeInterpreter (init_lldb,
LLDBSwigPythonBreakpointCallbackFunction,
LLDBSwigPythonWatchpointCallbackFunction,
LLDBSwigPythonCallTypeScript,
LLDBSwigPythonCreateSyntheticProvider,
LLDBSwigPythonCreateCommandObject,
LLDBSwigPython_CalculateNumChildren,
LLDBSwigPython_GetChildAtIndex,
LLDBSwigPython_GetIndexOfChildWithName,
LLDBSWIGPython_CastPyObjectToSBValue,
LLDBSWIGPython_GetValueObjectSPFromSBValue,
LLDBSwigPython_UpdateSynthProviderInstance,
LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
LLDBSwigPython_GetValueSynthProviderInstance,
LLDBSwigPythonCallCommand,
LLDBSwigPythonCallCommandObject,
LLDBSwigPythonCallModuleInit,
LLDBSWIGPythonCreateOSPlugin,
LLDBSWIGPythonRunScriptKeywordProcess,
LLDBSWIGPythonRunScriptKeywordThread,
LLDBSWIGPythonRunScriptKeywordTarget,
LLDBSWIGPythonRunScriptKeywordFrame,
LLDBSWIGPythonRunScriptKeywordValue,
LLDBSWIGPython_GetDynamicSetting,
LLDBSwigPythonCreateScriptedThreadPlan,
LLDBSWIGPythonCallThreadPlan);
#endif
}
}

lldb::SBCommand
SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help)
{
Expand Down
61 changes: 33 additions & 28 deletions lldb/source/API/SBDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "lldb/lldb-private.h"

#include "lldb/API/SystemInitializerFull.h"
#include "lldb/API/SBListener.h"
#include "lldb/API/SBBroadcaster.h"
#include "lldb/API/SBCommandInterpreter.h"
Expand All @@ -37,42 +38,20 @@
#include "lldb/Core/State.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/DataFormatters/DataVisualization.h"
#include "lldb/Initialization/InitializeLLDB.h"
#include "lldb/Initialization/SystemLifetimeManager.h"
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/OptionGroupPlatform.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/TargetList.h"

#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/DynamicLibrary.h"

using namespace lldb;
using namespace lldb_private;


SBInputReader::SBInputReader()
{
}
SBInputReader::~SBInputReader()
{
}

SBError
SBInputReader::Initialize(lldb::SBDebugger& sb_debugger, unsigned long (*)(void*, lldb::SBInputReader*, lldb::InputReaderAction, char const*, unsigned long), void*, lldb::InputReaderGranularity, char const*, char const*, bool)
{
return SBError();
}

void
SBInputReader::SetIsDone(bool)
{
}
bool
SBInputReader::IsActive() const
{
return false;
}

static llvm::sys::DynamicLibrary
LoadPlugin (const lldb::DebuggerSP &debugger_sp, const FileSpec& spec, Error& error)
{
Expand Down Expand Up @@ -107,6 +86,34 @@ LoadPlugin (const lldb::DebuggerSP &debugger_sp, const FileSpec& spec, Error& er
return llvm::sys::DynamicLibrary();
}

static llvm::ManagedStatic<SystemLifetimeManager> g_debugger_lifetime;

SBInputReader::SBInputReader()
{
}
SBInputReader::~SBInputReader()
{
}

SBError
SBInputReader::Initialize(lldb::SBDebugger &sb_debugger,
unsigned long (*)(void *, lldb::SBInputReader *, lldb::InputReaderAction, char const *,
unsigned long),
void *, lldb::InputReaderGranularity, char const *, char const *, bool)
{
return SBError();
}

void
SBInputReader::SetIsDone(bool)
{
}
bool
SBInputReader::IsActive() const
{
return false;
}

void
SBDebugger::Initialize ()
{
Expand All @@ -115,15 +122,13 @@ SBDebugger::Initialize ()
if (log)
log->Printf ("SBDebugger::Initialize ()");

SBCommandInterpreter::InitializeSWIG ();

lldb_private::Initialize(LoadPlugin);
g_debugger_lifetime->Initialize(llvm::make_unique<SystemInitializerFull>(), LoadPlugin);
}

void
SBDebugger::Terminate ()
{
lldb_private::Terminate();
g_debugger_lifetime->Terminate();
}

void
Expand Down
394 changes: 394 additions & 0 deletions lldb/source/API/SystemInitializerFull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,394 @@
//===-- SystemInitializerFull.cpp -------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/API/SystemInitializerFull.h"

#include "lldb/Core/Debugger.h"
#include "lldb/Core/Timer.h"
#include "lldb/Host/Host.h"
#include "lldb/Initialization/SystemInitializerCommon.h"

#include "Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h"
#include "Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h"
#include "Plugins/ABI/MacOSX-arm64/ABIMacOSX_arm64.h"
#include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
#include "Plugins/ABI/SysV-ppc/ABISysV_ppc.h"
#include "Plugins/ABI/SysV-ppc64/ABISysV_ppc64.h"
#include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
#include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
#include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
#include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
#include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
#include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h"
#include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
#include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
#include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
#include "Plugins/MemoryHistory/asan/MemoryHistoryASan.h"
#include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
#include "Plugins/Process/elf-core/ProcessElfCore.h"
#include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
#include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
#include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
#include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
#include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"

#if defined(__APPLE__)
#include "Plugins/Process/mach-core/ProcessMachCore.h"
#include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
#include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
#endif

#if defined(__FreeBSD__)
#include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
#endif

#if defined(__linux__)
#include "Plugins/Process/Linux/ProcessLinux.h"
#endif

#if defined(_MSC_VER)
#include "lldb/Host/windows/windows.h"
#include "Plugins/Process/Windows/DynamicLoaderWindows.h"
#include "Plugins/Process/Windows/ProcessWindows.h"
#endif

#if !defined(LLDB_DISABLE_PYTHON)
#include "lldb/Interpreter/ScriptInterpreterPython.h"
#endif

#include "llvm/Support/TargetSelect.h"

#include <string>

using namespace lldb_private;

#ifndef LLDB_DISABLE_PYTHON

// Defined in the SWIG source file
extern "C" void
init_lldb(void);

// these are the Pythonic implementations of the required callbacks
// these are scripting-language specific, which is why they belong here
// we still need to use function pointers to them instead of relying
// on linkage-time resolution because the SWIG stuff and this file
// get built at different times
extern "C" bool
LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& sb_frame,
const lldb::BreakpointLocationSP& sb_bp_loc);

extern "C" bool
LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
const char *session_dictionary_name,
const lldb::StackFrameSP& sb_frame,
const lldb::WatchpointSP& sb_wp);

extern "C" bool
LLDBSwigPythonCallTypeScript (const char *python_function_name,
void *session_dictionary,
const lldb::ValueObjectSP& valobj_sp,
void** pyfunct_wrapper,
const lldb::TypeSummaryOptionsSP& options_sp,
std::string& retval);

extern "C" void*
LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ValueObjectSP& valobj_sp);

extern "C" void*
LLDBSwigPythonCreateCommandObject (const char *python_class_name,
const char *session_dictionary_name,
const lldb::DebuggerSP debugger_sp);

extern "C" void*
LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ThreadPlanSP& thread_plan_sp);

extern "C" bool
LLDBSWIGPythonCallThreadPlan (void *implementor,
const char *method_name,
Event *event_sp,
bool &got_error);

extern "C" size_t
LLDBSwigPython_CalculateNumChildren (void *implementor);

extern "C" void *
LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);

extern "C" int
LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);

extern "C" void *
LLDBSWIGPython_CastPyObjectToSBValue (void* data);

extern lldb::ValueObjectSP
LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);

extern "C" bool
LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);

extern "C" bool
LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);

extern "C" void *
LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);

extern "C" bool
LLDBSwigPythonCallCommand (const char *python_function_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject &cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

extern "C" bool
LLDBSwigPythonCallCommandObject (void *implementor,
lldb::DebuggerSP& debugger,
const char* args,
lldb_private::CommandReturnObject& cmd_retobj,
lldb::ExecutionContextRefSP exe_ctx_ref_sp);

extern "C" bool
LLDBSwigPythonCallModuleInit (const char *python_module_name,
const char *session_dictionary_name,
lldb::DebuggerSP& debugger);

extern "C" void*
LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
const char *session_dictionary_name,
const lldb::ProcessSP& process_sp);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
const char* session_dictionary_name,
lldb::ProcessSP& process,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
const char* session_dictionary_name,
lldb::ThreadSP& thread,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
const char* session_dictionary_name,
lldb::TargetSP& target,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
const char* session_dictionary_name,
lldb::StackFrameSP& frame,
std::string& output);

extern "C" bool
LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name,
const char* session_dictionary_name,
lldb::ValueObjectSP& value,
std::string& output);

extern "C" void*
LLDBSWIGPython_GetDynamicSetting (void* module,
const char* setting,
const lldb::TargetSP& target_sp);


#endif

SystemInitializerFull::SystemInitializerFull()
{
}

SystemInitializerFull::~SystemInitializerFull()
{
}

void
SystemInitializerFull::Initialize()
{
InitializeSWIG();

SystemInitializerCommon::Initialize();

// 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();

SymbolVendorELF::Initialize();
SymbolFileDWARF::Initialize();
SymbolFileSymtab::Initialize();
UnwindAssemblyInstEmulation::Initialize();
UnwindAssembly_x86::Initialize();
EmulateInstructionARM::Initialize();
EmulateInstructionARM64::Initialize();
EmulateInstructionMIPS64::Initialize();
SymbolFileDWARFDebugMap::Initialize();
ItaniumABILanguageRuntime::Initialize();
AppleObjCRuntimeV2::Initialize();
AppleObjCRuntimeV1::Initialize();
SystemRuntimeMacOSX::Initialize();

#if defined(__linux__)
//----------------------------------------------------------------------
// Linux hosted plugins
//----------------------------------------------------------------------
ProcessLinux::Initialize();
#endif
#if defined(_MSC_VER)
DynamicLoaderWindows::Initialize();
ProcessWindows::Initialize();
#endif
#if defined(__FreeBSD__)
ProcessFreeBSD::Initialize();
#endif
#if defined(__APPLE__)
SymbolVendorMacOSX::Initialize();
ProcessKDP::Initialize();
ProcessMachCore::Initialize();
#endif
//----------------------------------------------------------------------
// Platform agnostic plugins
//----------------------------------------------------------------------
platform_gdb_server::PlatformRemoteGDBServer::Initialize();

process_gdb_remote::ProcessGDBRemote::Initialize();
DynamicLoaderStatic::Initialize();

// Scan for any system or user LLDB plug-ins
PluginManager::Initialize();

// The process settings need to know about installed plug-ins, so the Settings must be initialized
// AFTER PluginManager::Initialize is called.

Debugger::SettingsInitialize();
}

void SystemInitializerFull::InitializeSWIG()
{
#if !defined(LLDB_DISABLE_PYTHON)
ScriptInterpreterPython::InitializeInterpreter(
init_lldb,
LLDBSwigPythonBreakpointCallbackFunction,
LLDBSwigPythonWatchpointCallbackFunction,
LLDBSwigPythonCallTypeScript,
LLDBSwigPythonCreateSyntheticProvider,
LLDBSwigPythonCreateCommandObject,
LLDBSwigPython_CalculateNumChildren,
LLDBSwigPython_GetChildAtIndex,
LLDBSwigPython_GetIndexOfChildWithName,
LLDBSWIGPython_CastPyObjectToSBValue,
LLDBSWIGPython_GetValueObjectSPFromSBValue,
LLDBSwigPython_UpdateSynthProviderInstance,
LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
LLDBSwigPython_GetValueSynthProviderInstance,
LLDBSwigPythonCallCommand,
LLDBSwigPythonCallCommandObject,
LLDBSwigPythonCallModuleInit,
LLDBSWIGPythonCreateOSPlugin,
LLDBSWIGPythonRunScriptKeywordProcess,
LLDBSWIGPythonRunScriptKeywordThread,
LLDBSWIGPythonRunScriptKeywordTarget,
LLDBSWIGPythonRunScriptKeywordFrame,
LLDBSWIGPythonRunScriptKeywordValue,
LLDBSWIGPython_GetDynamicSetting,
LLDBSwigPythonCreateScriptedThreadPlan,
LLDBSWIGPythonCallThreadPlan);
#endif
}

void
SystemInitializerFull::Terminate()
{
Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);

Debugger::SettingsTerminate();

// 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();
SymbolVendorELF::Terminate();
SymbolFileDWARF::Terminate();
SymbolFileSymtab::Terminate();
UnwindAssembly_x86::Terminate();
UnwindAssemblyInstEmulation::Terminate();
EmulateInstructionARM::Terminate();
EmulateInstructionARM64::Terminate();
EmulateInstructionMIPS64::Terminate();
SymbolFileDWARFDebugMap::Terminate();
ItaniumABILanguageRuntime::Terminate();
AppleObjCRuntimeV2::Terminate();
AppleObjCRuntimeV1::Terminate();
SystemRuntimeMacOSX::Terminate();

#if defined(__APPLE__)
ProcessMachCore::Terminate();
ProcessKDP::Terminate();
SymbolVendorMacOSX::Terminate();
#endif
#if defined(_MSC_VER)
DynamicLoaderWindows::Terminate();
#endif

#if defined(__linux__)
ProcessLinux::Terminate();
#endif

#if defined(__FreeBSD__)
ProcessFreeBSD::Terminate();
#endif
Debugger::SettingsTerminate();

platform_gdb_server::PlatformRemoteGDBServer::Terminate();
process_gdb_remote::ProcessGDBRemote::Terminate();
DynamicLoaderStatic::Terminate();

// Now shutdown the common parts, in reverse order.
SystemInitializerCommon::Terminate();
}

void SystemInitializerFull::TerminateSWIG()
{

}
47 changes: 17 additions & 30 deletions lldb/source/Core/Debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include "lldb/Host/HostInfo.h"
#include "lldb/Host/Terminal.h"
#include "lldb/Host/ThreadLauncher.h"
#include "lldb/Initialization/InitializeLLDB.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Interpreter/OptionValueSInt64.h"
Expand All @@ -65,7 +64,6 @@ using namespace lldb;
using namespace lldb_private;


static uint32_t g_shared_debugger_refcount = 0;
static lldb::user_id_t g_unique_id = 1;
static size_t g_debugger_event_thread_stack_bytes = 8 * 1024 * 1024;

Expand Down Expand Up @@ -407,35 +405,24 @@ Debugger::GetEscapeNonPrintables () const
//}
//

int
Debugger::TestDebuggerRefCount ()
{
return g_shared_debugger_refcount;
}

static bool lldb_initialized = true;
static bool lldb_initialized = false;
void
Debugger::Initialize(LoadPluginCallbackType load_plugin_callback)
{
assert(!lldb_initialized && "Debugger::Initialize called more than once!");

lldb_initialized = true;
g_shared_debugger_refcount++;
g_load_plugin_callback = load_plugin_callback;
}

int
void
Debugger::Terminate ()
{
if (g_shared_debugger_refcount > 0)
{
g_shared_debugger_refcount--;
if (g_shared_debugger_refcount == 0)
{
// Clear our master list of debugger objects
Mutex::Locker locker (GetDebuggerListMutex ());
GetDebuggerList().clear();
}
}
return g_shared_debugger_refcount;
assert(lldb_initialized && "Debugger::Terminate called without a matching Debugger::Initialize!");

// Clear our master list of debugger objects
Mutex::Locker locker (GetDebuggerListMutex ());
GetDebuggerList().clear();
}

void
Expand Down Expand Up @@ -568,7 +555,7 @@ DebuggerSP
Debugger::CreateInstance (lldb::LogOutputCallback log_callback, void *baton)
{
DebuggerSP debugger_sp (new Debugger(log_callback, baton));
if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
GetDebuggerList().push_back(debugger_sp);
Expand All @@ -585,7 +572,7 @@ Debugger::Destroy (DebuggerSP &debugger_sp)

debugger_sp->Clear();

if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList ();
Expand All @@ -605,7 +592,7 @@ DebuggerSP
Debugger::FindDebuggerWithInstanceName (const ConstString &instance_name)
{
DebuggerSP debugger_sp;
if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList();
Expand All @@ -627,7 +614,7 @@ TargetSP
Debugger::FindTargetWithProcessID (lldb::pid_t pid)
{
TargetSP target_sp;
if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList();
Expand All @@ -646,7 +633,7 @@ TargetSP
Debugger::FindTargetWithProcess (Process *process)
{
TargetSP target_sp;
if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList();
Expand Down Expand Up @@ -1129,7 +1116,7 @@ Debugger::GetAsyncErrorStream ()
size_t
Debugger::GetNumDebuggers()
{
if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
return GetDebuggerList().size();
Expand All @@ -1142,7 +1129,7 @@ Debugger::GetDebuggerAtIndex (size_t index)
{
DebuggerSP debugger_sp;

if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList();
Expand All @@ -1159,7 +1146,7 @@ Debugger::FindDebuggerWithID (lldb::user_id_t id)
{
DebuggerSP debugger_sp;

if (g_shared_debugger_refcount > 0)
if (lldb_initialized)
{
Mutex::Locker locker (GetDebuggerListMutex ());
DebuggerList &debugger_list = GetDebuggerList();
Expand Down
4 changes: 3 additions & 1 deletion lldb/source/Initialization/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
add_lldb_library(lldbInitialization
InitializeLLDB.cpp
SystemInitializerCommon.cpp
SystemInitializer.cpp
SystemLifetimeManager.cpp
)
400 changes: 0 additions & 400 deletions lldb/source/Initialization/InitializeLLDB.cpp

This file was deleted.

20 changes: 20 additions & 0 deletions lldb/source/Initialization/SystemInitializer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- SystemInitializer.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Initialization/SystemInitializer.h"

using namespace lldb_private;

SystemInitializer::SystemInitializer()
{
}

SystemInitializer::~SystemInitializer()
{
}
167 changes: 167 additions & 0 deletions lldb/source/Initialization/SystemInitializerCommon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
//===-- SystemInitializerCommon.cpp -----------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Initialization/SystemInitializerCommon.h"

#include "lldb/Host/Host.h"
#include "lldb/Host/HostInfo.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Timer.h"
#include "lldb/Interpreter/ScriptInterpreterPython.h"

#include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
#include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
#include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
#include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
#include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
#include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
#include "Plugins/Platform/Android/PlatformAndroid.h"
#include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
#include "Plugins/Platform/Kalimba/PlatformKalimba.h"
#include "Plugins/Platform/Linux/PlatformLinux.h"
#include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
#include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
#include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
#include "Plugins/Platform/Windows/PlatformWindows.h"
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"

#if defined(__APPLE__)
#include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
#include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
#endif

#if defined(__linux__)
#include "Plugins/Process/Linux/ProcessLinux.h"
#include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
#endif

#if defined(_MSC_VER)
#include "lldb/Host/windows/windows.h"
#endif

#include "llvm/Support/TargetSelect.h"

#include <string>

using namespace lldb_private;

static void
fatal_error_handler(void *user_data, const std::string &reason, bool gen_crash_diag)
{
Host::SetCrashDescription(reason.c_str());
::abort();
}

SystemInitializerCommon::SystemInitializerCommon()
{
}

SystemInitializerCommon::~SystemInitializerCommon()
{
}

void
SystemInitializerCommon::Initialize()
{
#if defined(_MSC_VER)
const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
if (disable_crash_dialog_var && llvm::StringRef(disable_crash_dialog_var).equals_lower("true"))
{
// This will prevent Windows from displaying a dialog box requiring user interaction when
// LLDB crashes. This is mostly useful when automating LLDB, for example via the test
// suite, so that a crash in LLDB does not prevent completion of the test suite.
::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);

_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
}
#endif

Log::Initialize();
HostInfo::Initialize();
Timer::Initialize();
Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);

llvm::install_fatal_error_handler(fatal_error_handler, 0);

process_gdb_remote::ProcessGDBRemoteLog::Initialize();

// Initialize plug-ins
ObjectContainerBSDArchive::Initialize();
ObjectFileELF::Initialize();
ObjectFilePECOFF::Initialize();
DynamicLoaderPOSIXDYLD::Initialize();
PlatformFreeBSD::Initialize();
platform_linux::PlatformLinux::Initialize();
PlatformWindows::Initialize();
PlatformKalimba::Initialize();
platform_android::PlatformAndroid::Initialize();

//----------------------------------------------------------------------
// Apple/Darwin hosted plugins
//----------------------------------------------------------------------
DynamicLoaderMacOSXDYLD::Initialize();
ObjectContainerUniversalMachO::Initialize();

PlatformRemoteiOS::Initialize();
PlatformMacOSX::Initialize();
PlatformiOSSimulator::Initialize();

#if defined(__APPLE__)
DynamicLoaderDarwinKernel::Initialize();
PlatformDarwinKernel::Initialize();
ObjectFileMachO::Initialize();
#endif
#if defined(__linux__)
static ConstString g_linux_log_name("linux");
ProcessPOSIXLog::Initialize(g_linux_log_name);
#endif
#ifndef LLDB_DISABLE_PYTHON
ScriptInterpreterPython::InitializePrivate();
OperatingSystemPython::Initialize();
#endif
}

void
SystemInitializerCommon::Terminate()
{
Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
ObjectContainerBSDArchive::Terminate();
ObjectFileELF::Terminate();
ObjectFilePECOFF::Terminate();
DynamicLoaderPOSIXDYLD::Terminate();
PlatformFreeBSD::Terminate();
platform_linux::PlatformLinux::Terminate();
PlatformWindows::Terminate();
PlatformKalimba::Terminate();
platform_android::PlatformAndroid::Terminate();
DynamicLoaderMacOSXDYLD::Terminate();
ObjectContainerUniversalMachO::Terminate();
PlatformMacOSX::Terminate();
PlatformRemoteiOS::Terminate();
PlatformiOSSimulator::Terminate();

#if defined(__APPLE__)
DynamicLoaderDarwinKernel::Terminate();
ObjectFileMachO::Terminate();
PlatformDarwinKernel::Terminate();
#endif

#ifndef LLDB_DISABLE_PYTHON
OperatingSystemPython::Terminate();
#endif

Log::Terminate();
}
61 changes: 61 additions & 0 deletions lldb/source/Initialization/SystemLifetimeManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===-- SystemLifetimeManager.cpp ------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Initialization/SystemLifetimeManager.h"

#include "lldb/Core/Debugger.h"
#include "lldb/Host/Mutex.h"
#include "lldb/Initialization/SystemInitializer.h"

#include <utility>

using namespace lldb_private;

SystemLifetimeManager::SystemLifetimeManager()
: m_mutex(Mutex::eMutexTypeRecursive)
, m_initialized(false)
{
}

SystemLifetimeManager::~SystemLifetimeManager()
{
assert(!m_initialized && "SystemLifetimeManager destroyed without calling Terminate!");
}

void
SystemLifetimeManager::Initialize(std::unique_ptr<SystemInitializer> initializer,
LoadPluginCallbackType plugin_callback)
{
Mutex::Locker locker(m_mutex);
if (!m_initialized)
{
assert(!m_initializer &&
"Attempting to call SystemLifetimeManager::Initialize() when it is already initialized");
m_initialized = true;
m_initializer = std::move(initializer);

m_initializer->Initialize();
Debugger::Initialize(plugin_callback);
}
}

void
SystemLifetimeManager::Terminate()
{
Mutex::Locker locker(m_mutex);

if (m_initialized)
{
Debugger::Terminate();
m_initializer->Terminate();

m_initializer.reset();
m_initialized = false;
}
}
58 changes: 0 additions & 58 deletions lldb/source/Interpreter/ScriptInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,61 +110,3 @@ ScriptInterpreter::AcquireInterpreterLock ()
{
return std::unique_ptr<ScriptInterpreterLocker>(new ScriptInterpreterLocker());
}

void
ScriptInterpreter::InitializeInterpreter (SWIGInitCallback python_swig_init_callback,
SWIGBreakpointCallbackFunction swig_breakpoint_callback,
SWIGWatchpointCallbackFunction swig_watchpoint_callback,
SWIGPythonTypeScriptCallbackFunction swig_typescript_callback,
SWIGPythonCreateSyntheticProvider swig_synthetic_script,
SWIGPythonCreateCommandObject swig_create_cmd,
SWIGPythonCalculateNumChildren swig_calc_children,
SWIGPythonGetChildAtIndex swig_get_child_index,
SWIGPythonGetIndexOfChildWithName swig_get_index_child,
SWIGPythonCastPyObjectToSBValue swig_cast_to_sbvalue ,
SWIGPythonGetValueObjectSPFromSBValue swig_get_valobj_sp_from_sbvalue,
SWIGPythonUpdateSynthProviderInstance swig_update_provider,
SWIGPythonMightHaveChildrenSynthProviderInstance swig_mighthavechildren_provider,
SWIGPythonGetValueSynthProviderInstance swig_getvalue_provider,
SWIGPythonCallCommand swig_call_command,
SWIGPythonCallCommandObject swig_call_command_object,
SWIGPythonCallModuleInit swig_call_module_init,
SWIGPythonCreateOSPlugin swig_create_os_plugin,
SWIGPythonScriptKeyword_Process swig_run_script_keyword_process,
SWIGPythonScriptKeyword_Thread swig_run_script_keyword_thread,
SWIGPythonScriptKeyword_Target swig_run_script_keyword_target,
SWIGPythonScriptKeyword_Frame swig_run_script_keyword_frame,
SWIGPythonScriptKeyword_Value swig_run_script_keyword_value,
SWIGPython_GetDynamicSetting swig_plugin_get,
SWIGPythonCreateScriptedThreadPlan swig_thread_plan_script,
SWIGPythonCallThreadPlan swig_call_thread_plan)
{
#ifndef LLDB_DISABLE_PYTHON
ScriptInterpreterPython::InitializeInterpreter (python_swig_init_callback,
swig_breakpoint_callback,
swig_watchpoint_callback,
swig_typescript_callback,
swig_synthetic_script,
swig_create_cmd,
swig_calc_children,
swig_get_child_index,
swig_get_index_child,
swig_cast_to_sbvalue ,
swig_get_valobj_sp_from_sbvalue,
swig_update_provider,
swig_mighthavechildren_provider,
swig_getvalue_provider,
swig_call_command,
swig_call_command_object,
swig_call_module_init,
swig_create_os_plugin,
swig_run_script_keyword_process,
swig_run_script_keyword_thread,
swig_run_script_keyword_target,
swig_run_script_keyword_frame,
swig_run_script_keyword_value,
swig_plugin_get,
swig_thread_plan_script,
swig_call_thread_plan);
#endif // #ifndef LLDB_DISABLE_PYTHON
}
92 changes: 30 additions & 62 deletions lldb/source/Interpreter/ScriptInterpreterPython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,34 @@
using namespace lldb;
using namespace lldb_private;


static ScriptInterpreter::SWIGInitCallback g_swig_init_callback = nullptr;
static ScriptInterpreter::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = nullptr;
static ScriptInterpreter::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = nullptr;
static ScriptInterpreter::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = nullptr;
static ScriptInterpreter::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = nullptr;
static ScriptInterpreter::SWIGPythonCreateCommandObject g_swig_create_cmd = nullptr;
static ScriptInterpreter::SWIGPythonCalculateNumChildren g_swig_calc_children = nullptr;
static ScriptInterpreter::SWIGPythonGetChildAtIndex g_swig_get_child_index = nullptr;
static ScriptInterpreter::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = nullptr;
static ScriptInterpreter::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = nullptr;
static ScriptInterpreter::SWIGPythonGetValueObjectSPFromSBValue g_swig_get_valobj_sp_from_sbvalue = nullptr;
static ScriptInterpreter::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = nullptr;
static ScriptInterpreter::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = nullptr;
static ScriptInterpreter::SWIGPythonGetValueSynthProviderInstance g_swig_getvalue_provider = nullptr;
static ScriptInterpreter::SWIGPythonCallCommand g_swig_call_command = nullptr;
static ScriptInterpreter::SWIGPythonCallCommandObject g_swig_call_command_object = nullptr;
static ScriptInterpreter::SWIGPythonCallModuleInit g_swig_call_module_init = nullptr;
static ScriptInterpreter::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = nullptr;
static ScriptInterpreter::SWIGPythonScriptKeyword_Process g_swig_run_script_keyword_process = nullptr;
static ScriptInterpreter::SWIGPythonScriptKeyword_Thread g_swig_run_script_keyword_thread = nullptr;
static ScriptInterpreter::SWIGPythonScriptKeyword_Target g_swig_run_script_keyword_target = nullptr;
static ScriptInterpreter::SWIGPythonScriptKeyword_Frame g_swig_run_script_keyword_frame = nullptr;
static ScriptInterpreter::SWIGPythonScriptKeyword_Value g_swig_run_script_keyword_value = nullptr;
static ScriptInterpreter::SWIGPython_GetDynamicSetting g_swig_plugin_get = nullptr;
static ScriptInterpreter::SWIGPythonCreateScriptedThreadPlan g_swig_thread_plan_script = nullptr;
static ScriptInterpreter::SWIGPythonCallThreadPlan g_swig_call_thread_plan = nullptr;
static ScriptInterpreterPython::SWIGInitCallback g_swig_init_callback = nullptr;
static ScriptInterpreterPython::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = nullptr;
static ScriptInterpreterPython::SWIGWatchpointCallbackFunction g_swig_watchpoint_callback = nullptr;
static ScriptInterpreterPython::SWIGPythonTypeScriptCallbackFunction g_swig_typescript_callback = nullptr;
static ScriptInterpreterPython::SWIGPythonCreateSyntheticProvider g_swig_synthetic_script = nullptr;
static ScriptInterpreterPython::SWIGPythonCreateCommandObject g_swig_create_cmd = nullptr;
static ScriptInterpreterPython::SWIGPythonCalculateNumChildren g_swig_calc_children = nullptr;
static ScriptInterpreterPython::SWIGPythonGetChildAtIndex g_swig_get_child_index = nullptr;
static ScriptInterpreterPython::SWIGPythonGetIndexOfChildWithName g_swig_get_index_child = nullptr;
static ScriptInterpreterPython::SWIGPythonCastPyObjectToSBValue g_swig_cast_to_sbvalue = nullptr;
static ScriptInterpreterPython::SWIGPythonGetValueObjectSPFromSBValue g_swig_get_valobj_sp_from_sbvalue = nullptr;
static ScriptInterpreterPython::SWIGPythonUpdateSynthProviderInstance g_swig_update_provider = nullptr;
static ScriptInterpreterPython::SWIGPythonMightHaveChildrenSynthProviderInstance g_swig_mighthavechildren_provider = nullptr;
static ScriptInterpreterPython::SWIGPythonGetValueSynthProviderInstance g_swig_getvalue_provider = nullptr;
static ScriptInterpreterPython::SWIGPythonCallCommand g_swig_call_command = nullptr;
static ScriptInterpreterPython::SWIGPythonCallCommandObject g_swig_call_command_object = nullptr;
static ScriptInterpreterPython::SWIGPythonCallModuleInit g_swig_call_module_init = nullptr;
static ScriptInterpreterPython::SWIGPythonCreateOSPlugin g_swig_create_os_plugin = nullptr;
static ScriptInterpreterPython::SWIGPythonScriptKeyword_Process g_swig_run_script_keyword_process = nullptr;
static ScriptInterpreterPython::SWIGPythonScriptKeyword_Thread g_swig_run_script_keyword_thread = nullptr;
static ScriptInterpreterPython::SWIGPythonScriptKeyword_Target g_swig_run_script_keyword_target = nullptr;
static ScriptInterpreterPython::SWIGPythonScriptKeyword_Frame g_swig_run_script_keyword_frame = nullptr;
static ScriptInterpreterPython::SWIGPythonScriptKeyword_Value g_swig_run_script_keyword_value = nullptr;
static ScriptInterpreterPython::SWIGPython_GetDynamicSetting g_swig_plugin_get = nullptr;
static ScriptInterpreterPython::SWIGPythonCreateScriptedThreadPlan g_swig_thread_plan_script = nullptr;
static ScriptInterpreterPython::SWIGPythonCallThreadPlan g_swig_call_thread_plan = nullptr;

static bool g_initialized = false;

static std::string
ReadPythonBacktrace (PyObject* py_backtrace);
Expand Down Expand Up @@ -175,8 +176,7 @@ ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interprete
m_lock_count (0),
m_command_thread_state (nullptr)
{

ScriptInterpreterPython::InitializePrivate ();
assert(g_initialized && "ScriptInterpreterPython created but initialize has not been called!");

m_dictionary_name.append("_dict");
StreamString run_string;
Expand All @@ -189,16 +189,6 @@ ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interprete

run_string.Clear();

// 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.
//
// Bonus question: Why doesn't the ref-count always increase? Because sometimes lldb has already been imported, in
// which case the code inside it, including the call to SBDebugger::Initialize(), does not get executed.

int old_count = Debugger::TestDebuggerRefCount();

run_string.Printf ("run_one_line (%s, 'import copy, keyword, os, re, sys, uuid, lldb')", m_dictionary_name.c_str());
PyRun_SimpleString (run_string.GetData());

Expand All @@ -209,11 +199,6 @@ ScriptInterpreterPython::ScriptInterpreterPython (CommandInterpreter &interprete
PyRun_SimpleString (run_string.GetData());
run_string.Clear();

int new_count = Debugger::TestDebuggerRefCount();

if (new_count > old_count)
Debugger::Terminate();

run_string.Printf ("run_one_line (%s, 'import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line')", m_dictionary_name.c_str());
PyRun_SimpleString (run_string.GetData());
run_string.Clear();
Expand Down Expand Up @@ -3029,11 +3014,7 @@ ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback swig_init_callb
void
ScriptInterpreterPython::InitializePrivate ()
{
static int g_initialized = false;

if (g_initialized)
return;

assert(!g_initialized && "ScriptInterpreterPython::InitializePrivate() called more than once!");
g_initialized = true;

Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
Expand All @@ -3057,7 +3038,6 @@ ScriptInterpreterPython::InitializePrivate ()
}
Py_InitializeEx (0);

// Initialize SWIG after setting up python
if (g_swig_init_callback)
g_swig_init_callback ();

Expand Down Expand Up @@ -3095,20 +3075,8 @@ ScriptInterpreterPython::InitializePrivate ()
}
}

// 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" : "");
Expand Down
12 changes: 9 additions & 3 deletions lldb/tools/lldb-server/lldb-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
//===----------------------------------------------------------------------===//

#include "lldb/Core/Debugger.h"
#include "lldb/Initialization/InitializeLLDB.h"
#include "lldb/Initialization/SystemLifetimeManager.h"
#include "lldb/Initialization/SystemInitializerCommon.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/ManagedStatic.h"

#include <stdio.h>
#include <stdlib.h>

static llvm::ManagedStatic<lldb_private::SystemLifetimeManager> g_debugger_lifetime;

static void
display_usage (const char *progname)
{
Expand All @@ -30,13 +36,13 @@ int main_platform (int argc, char *argv[]);
static void
initialize ()
{
lldb_private::InitializeForLLGS(nullptr);
g_debugger_lifetime->Initialize(llvm::make_unique<lldb_private::SystemInitializerCommon>(), nullptr);
}

static void
terminate ()
{
lldb_private::TerminateLLGS();
g_debugger_lifetime->Terminate();
}

//----------------------------------------------------------------------
Expand Down