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