From 9a14adeae00015798843ff5cad987e5fdbdddb34 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Mon, 22 Nov 2021 16:32:44 +0100 Subject: [lldb] Remove 'extern "C"' from the lldb-swig-python interface The LLDBSWIGPython functions had (at least) two problems: - There wasn't a single source of truth (a header file) for the prototypes of these functions. This meant that subtle differences in copies of function declarations could go by undetected. And not-so-subtle differences would result in strange runtime failures. - All of the declarations had to have an extern "C" interface, because the function definitions were being placed inside and extert "C" block generated by swig. This patch fixes both problems by moving the function definitions to the %header block of the swig files. This block is not surrounded by extern "C", and seems more appropriate anyway, as swig docs say it is meant for "user-defined support code" (whereas the previous %wrapper code was for automatically-generated wrappers). It also puts the declarations into the SWIGPythonBridge header file (which seems to have been created for this purpose), and ensures it is included by all code wishing to define or use these functions. This means that any differences in the declaration become a compiler error instead of a runtime failure. Differential Revision: https://reviews.llvm.org/D114369 --- .../ScriptInterpreter/Python/SWIGPythonBridge.h | 152 +++++++++++++++++++-- 1 file changed, 142 insertions(+), 10 deletions(-) (limited to 'lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h') diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h index 798d947..c7af135 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h +++ b/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h @@ -15,8 +15,12 @@ #if LLDB_ENABLE_PYTHON +// LLDB Python header must be included first +#include "lldb-python.h" + #include "lldb/lldb-forward.h" #include "lldb/lldb-types.h" +#include "llvm/Support/Error.h" namespace lldb_private { @@ -41,20 +45,148 @@ template <> const char *GetPythonValueFormatString(unsigned long long); template <> const char *GetPythonValueFormatString(float t); template <> const char *GetPythonValueFormatString(double t); -extern "C" void *LLDBSwigPythonCreateScriptedProcess( +void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data); +void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data); + +// These prototypes are the Pythonic implementations of the required callbacks. +// Although these are scripting-language specific, their definition depends on +// the public API. + +void *LLDBSwigPythonCreateScriptedProcess(const char *python_class_name, + const char *session_dictionary_name, + const lldb::TargetSP &target_sp, + StructuredDataImpl *args_impl, + std::string &error_string); + +void *LLDBSwigPythonCreateScriptedThread(const char *python_class_name, + const char *session_dictionary_name, + const lldb::ProcessSP &process_sp, + StructuredDataImpl *args_impl, + std::string &error_string); + +llvm::Expected LLDBSwigPythonBreakpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, + const lldb::BreakpointLocationSP &sb_bp_loc, + lldb_private::StructuredDataImpl *args_impl); + +bool LLDBSwigPythonWatchpointCallbackFunction( + const char *python_function_name, const char *session_dictionary_name, + const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp); + +bool LLDBSwigPythonCallTypeScript(const char *python_function_name, + const void *session_dictionary, + const lldb::ValueObjectSP &valobj_sp, + void **pyfunct_wrapper, + const lldb::TypeSummaryOptionsSP &options_sp, + std::string &retval); + +void * +LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name, + const char *session_dictionary_name, + const lldb::ValueObjectSP &valobj_sp); + +void *LLDBSwigPythonCreateCommandObject(const char *python_class_name, + const char *session_dictionary_name, + const lldb::DebuggerSP debugger_sp); + +void *LLDBSwigPythonCreateScriptedThreadPlan( const char *python_class_name, const char *session_dictionary_name, - const lldb::TargetSP &target_sp, StructuredDataImpl *args_impl, - std::string &error_string); + lldb_private::StructuredDataImpl *args_data, std::string &error_string, + const lldb::ThreadPlanSP &thread_plan_sp); -extern "C" void *LLDBSwigPythonCreateScriptedThread( +bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name, + lldb_private::Event *event_sp, + bool &got_error); + +void *LLDBSwigPythonCreateScriptedBreakpointResolver( const char *python_class_name, const char *session_dictionary_name, - const lldb::ProcessSP &process_sp, StructuredDataImpl *args_impl, - std::string &error_string); + lldb_private::StructuredDataImpl *args, const lldb::BreakpointSP &bkpt_sp); + +unsigned int +LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name, + lldb_private::SymbolContext *sym_ctx); + +void *LLDBSwigPythonCreateScriptedStopHook( + lldb::TargetSP target_sp, const char *python_class_name, + const char *session_dictionary_name, lldb_private::StructuredDataImpl *args, + lldb_private::Status &error); + +bool LLDBSwigPythonStopHookCallHandleStop(void *implementor, + lldb::ExecutionContextRefSP exc_ctx, + lldb::StreamSP stream); + +size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, uint32_t max); + +PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, uint32_t idx); + +int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor, + const char *child_name); + +lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data); + +bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor); + +bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance( + PyObject *implementor); + +PyObject *LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor); + +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); + +bool LLDBSwigPythonCallCommandObject( + PyObject *implementor, lldb::DebuggerSP &debugger, const char *args, + lldb_private::CommandReturnObject &cmd_retobj, + lldb::ExecutionContextRefSP exe_ctx_ref_sp); + +bool LLDBSwigPythonCallModuleInit(const char *python_module_name, + const char *session_dictionary_name, + lldb::DebuggerSP &debugger); + +void *LLDBSWIGPythonCreateOSPlugin(const char *python_class_name, + const char *session_dictionary_name, + const lldb::ProcessSP &process_sp); + +void *LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name, + const char *session_dictionary_name); + +PyObject * +LLDBSwigPython_GetRecognizedArguments(PyObject *implementor, + const lldb::StackFrameSP &frame_sp); + +bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name, + const char *session_dictionary_name, + const lldb::ProcessSP &process, + std::string &output); + +bool LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name, + const char *session_dictionary_name, + lldb::ThreadSP &thread, + std::string &output); + +bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name, + const char *session_dictionary_name, + const lldb::TargetSP &target, + std::string &output); + +bool LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name, + const char *session_dictionary_name, + lldb::StackFrameSP &frame, + std::string &output); + +bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name, + const char *session_dictionary_name, + const lldb::ValueObjectSP &value, + std::string &output); -extern "C" void *LLDBSWIGPython_CastPyObjectToSBData(void *data); -extern "C" void *LLDBSWIGPython_CastPyObjectToSBError(void *data); -extern "C" void *LLDBSWIGPython_CastPyObjectToSBValue(void *data); -extern "C" void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(void *data); +void *LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting, + const lldb::TargetSP &target_sp); } // namespace lldb_private -- cgit v1.1