xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h (revision c2be702104284cb3facf31124494b9a400296179)
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 } // namespace lldb
34 
35 namespace lldb_private {
36 namespace python {
37 
38 typedef struct swig_type_info swig_type_info;
39 
40 python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info);
41 
42 /// A class that automatically clears an SB object when it goes out of scope.
43 /// Use for cases where the SB object points to a temporary/unowned entity.
44 template <typename T> class ScopedPythonObject : PythonObject {
45 public:
46   ScopedPythonObject(T *sb, swig_type_info *info)
47       : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
48   ~ScopedPythonObject() {
49     if (m_sb)
50       *m_sb = T();
51   }
52   ScopedPythonObject(ScopedPythonObject &&rhs)
53       : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
54   ScopedPythonObject(const ScopedPythonObject &) = delete;
55   ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
56   ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
57 
58   const PythonObject &obj() const { return *this; }
59 
60 private:
61   T *m_sb;
62 };
63 
64 PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp);
65 PythonObject ToSWIGWrapper(lldb::TargetSP target_sp);
66 PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp);
67 PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp);
68 PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp);
69 PythonObject ToSWIGWrapper(const Status &status);
70 PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl);
71 PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp);
72 PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp);
73 PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp);
74 PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp);
75 PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp);
76 PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp);
77 PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
78 PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx);
79 
80 PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp);
81 PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp);
82 PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp);
83 
84 PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
85 PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStream> stream_sb);
86 PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
87 
88 python::ScopedPythonObject<lldb::SBCommandReturnObject>
89 ToSWIGWrapper(CommandReturnObject &cmd_retobj);
90 python::ScopedPythonObject<lldb::SBEvent> ToSWIGWrapper(Event *event);
91 
92 } // namespace python
93 
94 void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);
95 void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data);
96 void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data);
97 void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data);
98 void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);
99 void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
100 void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
101 
102 // These prototypes are the Pythonic implementations of the required callbacks.
103 // Although these are scripting-language specific, their definition depends on
104 // the public API.
105 
106 python::PythonObject LLDBSwigPythonCreateScriptedObject(
107     const char *python_class_name, const char *session_dictionary_name,
108     lldb::ExecutionContextRefSP exe_ctx_sp,
109     const lldb_private::StructuredDataImpl &args_impl,
110     std::string &error_string);
111 
112 llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
113     const char *python_function_name, const char *session_dictionary_name,
114     const lldb::StackFrameSP &sb_frame,
115     const lldb::BreakpointLocationSP &sb_bp_loc,
116     const lldb_private::StructuredDataImpl &args_impl);
117 
118 bool LLDBSwigPythonWatchpointCallbackFunction(
119     const char *python_function_name, const char *session_dictionary_name,
120     const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
121 
122 bool LLDBSwigPythonFormatterCallbackFunction(
123     const char *python_function_name, const char *session_dictionary_name,
124     lldb::TypeImplSP type_impl_sp);
125 
126 bool LLDBSwigPythonCallTypeScript(const char *python_function_name,
127                                   const void *session_dictionary,
128                                   const lldb::ValueObjectSP &valobj_sp,
129                                   void **pyfunct_wrapper,
130                                   const lldb::TypeSummaryOptionsSP &options_sp,
131                                   std::string &retval);
132 
133 python::PythonObject
134 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
135                                       const char *session_dictionary_name,
136                                       const lldb::ValueObjectSP &valobj_sp);
137 
138 python::PythonObject
139 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
140                                   const char *session_dictionary_name,
141                                   lldb::DebuggerSP debugger_sp);
142 
143 python::PythonObject LLDBSwigPythonCreateScriptedThreadPlan(
144     const char *python_class_name, const char *session_dictionary_name,
145     const StructuredDataImpl &args_data, std::string &error_string,
146     const lldb::ThreadPlanSP &thread_plan_sp);
147 
148 bool LLDBSWIGPythonCallThreadPlan(void *implementor, const char *method_name,
149                                   lldb_private::Event *event_sp,
150                                   bool &got_error);
151 
152 bool LLDBSWIGPythonCallThreadPlan(void *implementor,
153                                   const char *method_name,
154                                   lldb_private::Stream *stream,
155                                   bool &got_error);
156 
157 python::PythonObject LLDBSwigPythonCreateScriptedBreakpointResolver(
158     const char *python_class_name, const char *session_dictionary_name,
159     const StructuredDataImpl &args, const lldb::BreakpointSP &bkpt_sp);
160 
161 unsigned int
162 LLDBSwigPythonCallBreakpointResolver(void *implementor, const char *method_name,
163                                      lldb_private::SymbolContext *sym_ctx);
164 
165 python::PythonObject LLDBSwigPythonCreateScriptedStopHook(
166     lldb::TargetSP target_sp, const char *python_class_name,
167     const char *session_dictionary_name, const StructuredDataImpl &args,
168     lldb_private::Status &error);
169 
170 bool LLDBSwigPythonStopHookCallHandleStop(void *implementor,
171                                           lldb::ExecutionContextRefSP exc_ctx,
172                                           lldb::StreamSP stream);
173 
174 size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, uint32_t max);
175 
176 PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor, uint32_t idx);
177 
178 int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor,
179                                            const char *child_name);
180 
181 lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
182 
183 bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
184 
185 bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance(
186     PyObject *implementor);
187 
188 PyObject *LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);
189 
190 bool 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 bool LLDBSwigPythonCallCommandObject(
197     PyObject *implementor, lldb::DebuggerSP debugger, const char *args,
198     lldb_private::CommandReturnObject &cmd_retobj,
199     lldb::ExecutionContextRefSP exe_ctx_ref_sp);
200 
201 bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
202                                   const char *session_dictionary_name,
203                                   lldb::DebuggerSP debugger);
204 
205 python::PythonObject
206 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
207                              const char *session_dictionary_name,
208                              const lldb::ProcessSP &process_sp);
209 
210 python::PythonObject
211 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
212                                      const char *session_dictionary_name);
213 
214 PyObject *
215 LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,
216                                       const lldb::StackFrameSP &frame_sp);
217 
218 bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name,
219                                            const char *session_dictionary_name,
220                                            const lldb::ProcessSP &process,
221                                            std::string &output);
222 
223 std::optional<std::string>
224 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
225                                      const char *session_dictionary_name,
226                                      lldb::ThreadSP thread);
227 
228 bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name,
229                                           const char *session_dictionary_name,
230                                           const lldb::TargetSP &target,
231                                           std::string &output);
232 
233 std::optional<std::string>
234 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
235                                     const char *session_dictionary_name,
236                                     lldb::StackFrameSP frame);
237 
238 bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name,
239                                          const char *session_dictionary_name,
240                                          const lldb::ValueObjectSP &value,
241                                          std::string &output);
242 
243 void *LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
244                                        const lldb::TargetSP &target_sp);
245 
246 } // namespace lldb_private
247 
248 #endif // LLDB_ENABLE_PYTHON
249 #endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
250