diff --git a/lldb/source/API/SystemInitializerFull.cpp b/lldb/source/API/SystemInitializerFull.cpp index 427186f2c420..5515548f1bbb 100644 --- a/lldb/source/API/SystemInitializerFull.cpp +++ b/lldb/source/API/SystemInitializerFull.cpp @@ -121,145 +121,6 @@ using namespace lldb_private; -#ifndef LLDB_DISABLE_PYTHON - -// Defined in the SWIG source file -#if PY_MAJOR_VERSION >= 3 -extern "C" PyObject *PyInit__lldb(void); - -#define LLDBSwigPyInit PyInit__lldb - -#else -extern "C" void init_lldb(void); - -#define LLDBSwigPyInit init_lldb -#endif - -// 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" void *LLDBSwigPythonCreateScriptedBreakpointResolver( - const char *python_class_name, - const char *session_dictionary_name, - lldb_private::StructuredDataImpl *args, - lldb::BreakpointSP &bkpt_sp); - -extern "C" unsigned int LLDBSwigPythonCallBreakpointResolver( - void *implementor, - const char *method_name, - lldb_private::SymbolContext *sym_ctx -); - -extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor, - uint32_t max); - -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" void *LLDBSWIGPython_CreateFrameRecognizer( - const char *python_class_name, - const char *session_dictionary_name); - -extern "C" void *LLDBSwigPython_GetRecognizedArguments(void *implementor, - const lldb::StackFrameSP& frame_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() {} @@ -281,11 +142,7 @@ llvm::Error SystemInitializerFull::Initialize() { #endif #if !defined(LLDB_DISABLE_PYTHON) - InitializeSWIG(); - - // ScriptInterpreterPython::Initialize() depends on things like HostInfo - // being initialized so it can compute the python directory etc, so we need - // to do this after SystemInitializerCommon::Initialize(). + ScriptInterpreterPython::InitializeSWIG(); ScriptInterpreterPython::Initialize(); #endif @@ -406,31 +263,6 @@ llvm::Error SystemInitializerFull::Initialize() { return llvm::Error::success(); } -void SystemInitializerFull::InitializeSWIG() { -#if !defined(LLDB_DISABLE_PYTHON) - ScriptInterpreterPython::InitializeInterpreter( - LLDBSwigPyInit, 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, LLDBSWIGPython_CreateFrameRecognizer, - LLDBSwigPython_GetRecognizedArguments, - LLDBSWIGPythonRunScriptKeywordProcess, - LLDBSWIGPythonRunScriptKeywordThread, - LLDBSWIGPythonRunScriptKeywordTarget, LLDBSWIGPythonRunScriptKeywordFrame, - LLDBSWIGPythonRunScriptKeywordValue, LLDBSWIGPython_GetDynamicSetting, - LLDBSwigPythonCreateScriptedThreadPlan, LLDBSWIGPythonCallThreadPlan, - LLDBSwigPythonCreateScriptedBreakpointResolver, LLDBSwigPythonCallBreakpointResolver); -#endif -} - void SystemInitializerFull::Terminate() { static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION); diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index 012c5855e63f..dc066a29b1cb 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -57,6 +57,143 @@ using namespace lldb; using namespace lldb_private; +#ifndef LLDB_DISABLE_PYTHON + +// Defined in the SWIG source file +#if PY_MAJOR_VERSION >= 3 +extern "C" PyObject *PyInit__lldb(void); + +#define LLDBSwigPyInit PyInit__lldb + +#else +extern "C" void init_lldb(void); + +#define LLDBSwigPyInit init_lldb +#endif + +// 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" void *LLDBSwigPythonCreateScriptedBreakpointResolver( + const char *python_class_name, const char *session_dictionary_name, + lldb_private::StructuredDataImpl *args, lldb::BreakpointSP &bkpt_sp); + +extern "C" unsigned int +LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name, + lldb_private::SymbolContext *sym_ctx); + +extern "C" size_t LLDBSwigPython_CalculateNumChildren(void *implementor, + uint32_t max); + +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" void * +LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name, + const char *session_dictionary_name); + +extern "C" void * +LLDBSwigPython_GetRecognizedArguments(void *implementor, + const lldb::StackFrameSP &frame_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 + static ScriptInterpreterPython::SWIGInitCallback g_swig_init_callback = nullptr; static ScriptInterpreterPython::SWIGBreakpointCallbackFunction g_swig_breakpoint_callback = nullptr; @@ -1967,7 +2104,7 @@ ScriptInterpreterPython::CreateScriptedBreakpointResolver( const char *class_name, StructuredDataImpl *args_data, lldb::BreakpointSP &bkpt_sp) { - + if (class_name == nullptr || class_name[0] == '\0') return StructuredData::GenericSP(); @@ -2002,7 +2139,7 @@ ScriptInterpreterPython::ScriptedBreakpointResolverSearchCallback( StructuredData::GenericSP implementor_sp, SymbolContext *sym_ctx) { bool should_continue = false; - + if (implementor_sp) { Locker py_lock(this, Locker::AcquireLock | Locker::InitSession | Locker::NoSTDIN); @@ -3258,6 +3395,32 @@ ScriptInterpreterPython::AcquireInterpreterLock() { return py_lock; } +void ScriptInterpreterPython::InitializeSWIG() { +#if !defined(LLDB_DISABLE_PYTHON) + InitializeInterpreter( + LLDBSwigPyInit, 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, LLDBSWIGPython_CreateFrameRecognizer, + LLDBSwigPython_GetRecognizedArguments, + LLDBSWIGPythonRunScriptKeywordProcess, + LLDBSWIGPythonRunScriptKeywordThread, + LLDBSWIGPythonRunScriptKeywordTarget, LLDBSWIGPythonRunScriptKeywordFrame, + LLDBSWIGPythonRunScriptKeywordValue, LLDBSWIGPython_GetDynamicSetting, + LLDBSwigPythonCreateScriptedThreadPlan, LLDBSWIGPythonCallThreadPlan, + LLDBSwigPythonCreateScriptedBreakpointResolver, + LLDBSwigPythonCallBreakpointResolver); +#endif +} + void ScriptInterpreterPython::InitializeInterpreter( SWIGInitCallback swig_init_callback, SWIGBreakpointCallbackFunction swig_breakpoint_callback, diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h index 8dcc6fde3f09..dc91fd1cb7ec 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h @@ -218,7 +218,7 @@ public: lldb::StateType ScriptedThreadPlanGetRunState(StructuredData::ObjectSP implementor_sp, bool &script_error) override; - + StructuredData::GenericSP CreateScriptedBreakpointResolver(const char *class_name, StructuredDataImpl *args_data, @@ -396,41 +396,6 @@ public: void ResetOutputFileHandle(FILE *new_fh) override; - static void InitializePrivate(); - - static void 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, - SWIGPythonCreateFrameRecognizer swig_create_frame_recognizer, - SWIGPythonGetRecognizedArguments swig_get_recognized_arguments, - 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, - SWIGPythonCreateScriptedBreakpointResolver swig_bkpt_resolver_script, - SWIGPythonCallBreakpointResolver swig_call_breakpoint_resolver); - const char *GetDictionaryName() { return m_dictionary_name.c_str(); } PyThreadState *GetThreadState() { return m_command_thread_state; } @@ -451,6 +416,7 @@ public: //------------------------------------------------------------------ // Static Functions //------------------------------------------------------------------ + static void InitializeSWIG(); static void Initialize(); static void Terminate(); @@ -512,6 +478,41 @@ public: }; protected: + static void InitializePrivate(); + + static void 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, + SWIGPythonCreateFrameRecognizer swig_create_frame_recognizer, + SWIGPythonGetRecognizedArguments swig_get_recognized_arguments, + 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, + SWIGPythonCreateScriptedBreakpointResolver swig_bkpt_resolver_script, + SWIGPythonCallBreakpointResolver swig_call_breakpoint_resolver); + class SynchronicityHandler { private: lldb::DebuggerSP m_debugger_sp;