1 //===-- ScriptInterpreterPython.h -------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H 10 #define LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H 11 12 #include <optional> 13 #include <string> 14 15 #include "lldb/Host/Config.h" 16 17 #if LLDB_ENABLE_PYTHON 18 19 // LLDB Python header must be included first 20 #include "lldb-python.h" 21 22 #include "Plugins/ScriptInterpreter/Python/PythonDataObjects.h" 23 #include "lldb/lldb-forward.h" 24 #include "lldb/lldb-types.h" 25 #include "llvm/Support/Error.h" 26 27 namespace lldb { 28 class SBEvent; 29 class SBCommandReturnObject; 30 class SBValue; 31 class SBStream; 32 class SBStructuredData; 33 class SBFileSpec; 34 class SBModuleSpec; 35 } // namespace lldb 36 37 namespace lldb_private { 38 namespace python { 39 40 typedef struct swig_type_info swig_type_info; 41 42 python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info); 43 44 /// A class that automatically clears an SB object when it goes out of scope. 45 /// Use for cases where the SB object points to a temporary/unowned entity. 46 template <typename T> class ScopedPythonObject : PythonObject { 47 public: 48 ScopedPythonObject(T *sb, swig_type_info *info) 49 : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {} 50 ~ScopedPythonObject() { 51 if (m_sb) 52 *m_sb = T(); 53 } 54 ScopedPythonObject(ScopedPythonObject &&rhs) 55 : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {} 56 ScopedPythonObject(const ScopedPythonObject &) = delete; 57 ScopedPythonObject &operator=(const ScopedPythonObject &) = delete; 58 ScopedPythonObject &operator=(ScopedPythonObject &&) = delete; 59 60 const PythonObject &obj() const { return *this; } 61 62 private: 63 T *m_sb; 64 }; 65 66 // TODO: We may want to support other languages in the future w/ SWIG (we 67 // already support Lua right now, for example). We could create a generic 68 // SWIGBridge class and have this one specialize it, something like this: 69 // 70 // <typename T> 71 // class SWIGBridge { 72 // static T ToSWIGWrapper(...); 73 // }; 74 // 75 // class SWIGPythonBridge : public SWIGBridge<PythonObject> { 76 // template<> static PythonObject ToSWIGWrapper(...); 77 // }; 78 // 79 // And we should be able to more easily support things like Lua 80 class SWIGBridge { 81 public: 82 static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb); 83 static PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp); 84 static PythonObject ToSWIGWrapper(lldb::TargetSP target_sp); 85 static PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp); 86 static PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp); 87 static PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp); 88 static PythonObject ToSWIGWrapper(const Status &status); 89 static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl); 90 static PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp); 91 static PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp); 92 static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp); 93 static PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp); 94 static PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp); 95 static PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp); 96 static PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp); 97 static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options); 98 static PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx); 99 static PythonObject ToSWIGWrapper(const Stream *stream); 100 static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb); 101 static PythonObject ToSWIGWrapper(Event *event); 102 103 static PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp); 104 static PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp); 105 static PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp); 106 107 static PythonObject 108 ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb); 109 static PythonObject 110 ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb); 111 static PythonObject 112 ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb); 113 114 static python::ScopedPythonObject<lldb::SBCommandReturnObject> 115 ToSWIGWrapper(CommandReturnObject &cmd_retobj); 116 // These prototypes are the Pythonic implementations of the required 117 // callbacks. Although these are scripting-language specific, their definition 118 // depends on the public API. 119 120 static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction( 121 const char *python_function_name, const char *session_dictionary_name, 122 const lldb::StackFrameSP &sb_frame, 123 const lldb::BreakpointLocationSP &sb_bp_loc, 124 const lldb_private::StructuredDataImpl &args_impl); 125 126 static bool LLDBSwigPythonWatchpointCallbackFunction( 127 const char *python_function_name, const char *session_dictionary_name, 128 const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp); 129 130 static bool 131 LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name, 132 const char *session_dictionary_name, 133 lldb::TypeImplSP type_impl_sp); 134 135 static bool LLDBSwigPythonCallTypeScript( 136 const char *python_function_name, const void *session_dictionary, 137 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper, 138 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval); 139 140 static python::PythonObject 141 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name, 142 const char *session_dictionary_name, 143 const lldb::ValueObjectSP &valobj_sp); 144 145 static python::PythonObject 146 LLDBSwigPythonCreateCommandObject(const char *python_class_name, 147 const char *session_dictionary_name, 148 lldb::DebuggerSP debugger_sp); 149 150 static python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver( 151 const char *python_class_name, const char *session_dictionary_name, 152 const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp); 153 154 static unsigned int 155 LLDBSwigPythonCallBreakpointResolver(void *implementor, 156 const char *method_name, 157 lldb_private::SymbolContext *sym_ctx); 158 159 static python::PythonObject LLDBSwigPythonCreateScriptedStopHook( 160 lldb::TargetSP target_sp, const char *python_class_name, 161 const char *session_dictionary_name, const StructuredDataImpl &args, 162 lldb_private::Status &error); 163 164 static bool 165 LLDBSwigPythonStopHookCallHandleStop(void *implementor, 166 lldb::ExecutionContextRefSP exc_ctx, 167 lldb::StreamSP stream); 168 169 static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, 170 uint32_t max); 171 172 static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, 173 uint32_t idx); 174 175 static int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor, 176 const char *child_name); 177 178 static lldb::ValueObjectSP 179 LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data); 180 181 static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor); 182 183 static bool 184 LLDBSwigPython_MightHaveChildrenSynthProviderInstance(PyObject *implementor); 185 186 static PyObject * 187 LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor); 188 189 static bool 190 LLDBSwigPythonCallCommand(const char *python_function_name, 191 const char *session_dictionary_name, 192 lldb::DebuggerSP debugger, const char *args, 193 lldb_private::CommandReturnObject &cmd_retobj, 194 lldb::ExecutionContextRefSP exe_ctx_ref_sp); 195 196 static bool 197 LLDBSwigPythonCallCommandObject(PyObject *implementor, 198 lldb::DebuggerSP debugger, const char *args, 199 lldb_private::CommandReturnObject &cmd_retobj, 200 lldb::ExecutionContextRefSP exe_ctx_ref_sp); 201 202 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name, 203 const char *session_dictionary_name, 204 lldb::DebuggerSP debugger); 205 206 static python::PythonObject 207 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name, 208 const char *session_dictionary_name, 209 const lldb::ProcessSP &process_sp); 210 211 static python::PythonObject 212 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name, 213 const char *session_dictionary_name); 214 215 static PyObject * 216 LLDBSwigPython_GetRecognizedArguments(PyObject *implementor, 217 const lldb::StackFrameSP &frame_sp); 218 219 static bool LLDBSWIGPythonRunScriptKeywordProcess( 220 const char *python_function_name, const char *session_dictionary_name, 221 const lldb::ProcessSP &process, std::string &output); 222 223 static std::optional<std::string> 224 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name, 225 const char *session_dictionary_name, 226 lldb::ThreadSP thread); 227 228 static bool LLDBSWIGPythonRunScriptKeywordTarget( 229 const char *python_function_name, const char *session_dictionary_name, 230 const lldb::TargetSP &target, std::string &output); 231 232 static std::optional<std::string> 233 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name, 234 const char *session_dictionary_name, 235 lldb::StackFrameSP frame); 236 237 static bool LLDBSWIGPythonRunScriptKeywordValue( 238 const char *python_function_name, const char *session_dictionary_name, 239 const lldb::ValueObjectSP &value, std::string &output); 240 241 static void * 242 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting, 243 const lldb::TargetSP &target_sp); 244 }; 245 246 void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data); 247 void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data); 248 void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data); 249 void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data); 250 void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data); 251 void *LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data); 252 void *LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data); 253 void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data); 254 void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data); 255 } // namespace python 256 257 } // namespace lldb_private 258 259 #endif // LLDB_ENABLE_PYTHON 260 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H 261