LLDB mainline
SWIGPythonBridge.h
Go to the documentation of this file.
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
23#include "lldb/lldb-forward.h"
24#include "lldb/lldb-types.h"
25#include "llvm/Support/Error.h"
26
27namespace lldb {
28class SBEvent;
30class SBValue;
31class SBStream;
33class SBFileSpec;
34class SBModuleSpec;
35class SBStringList;
36} // namespace lldb
37
38namespace lldb_private {
39namespace python {
40
41typedef struct swig_type_info swig_type_info;
42
43python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info);
44
45/// A class that automatically clears an SB object when it goes out of scope.
46/// Use for cases where the SB object points to a temporary/unowned entity.
47template <typename T> class ScopedPythonObject : PythonObject {
48public:
49 ScopedPythonObject(T *sb, swig_type_info *info)
50 : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
51 ~ScopedPythonObject() {
52 if (m_sb)
53 *m_sb = T();
54 }
55 ScopedPythonObject(ScopedPythonObject &&rhs)
56 : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
57 ScopedPythonObject(const ScopedPythonObject &) = delete;
58 ScopedPythonObject &operator=(const ScopedPythonObject &) = delete;
59 ScopedPythonObject &operator=(ScopedPythonObject &&) = delete;
60
61 const PythonObject &obj() const { return *this; }
62
63private:
64 T *m_sb;
65};
66
67// TODO: We may want to support other languages in the future w/ SWIG (we
68// already support Lua right now, for example). We could create a generic
69// SWIGBridge class and have this one specialize it, something like this:
70//
71// <typename T>
72// class SWIGBridge {
73// static T ToSWIGWrapper(...);
74// };
75//
76// class SWIGPythonBridge : public SWIGBridge<PythonObject> {
77// template<> static PythonObject ToSWIGWrapper(...);
78// };
79//
80// And we should be able to more easily support things like Lua
81class SWIGBridge {
82public:
83 static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
84 static PythonObject
85 ToSWIGWrapper(std::unique_ptr<lldb::SBCommandReturnObject> result_up);
86 static PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp);
87 static PythonObject ToSWIGWrapper(lldb::TargetSP target_sp);
88 static PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp);
89 static PythonObject ToSWIGWrapper(lldb::ModuleSP module_sp);
90 static PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp);
91 static PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp);
92 static PythonObject ToSWIGWrapper(Status &&status);
93 static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl);
94 static PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp);
95 static PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp);
96 static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp);
97 static PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp);
98 static PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp);
99 static PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp);
100 static PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp);
101 static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
102 static PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx);
103 static PythonObject ToSWIGWrapper(const Stream *stream);
104 static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb);
105 static PythonObject ToSWIGWrapper(Event *event);
106
107 static PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp);
108 static PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp);
109 static PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp);
110 static PythonObject ToSWIGWrapper(lldb::DescriptionLevel level);
111
112 static PythonObject
113 ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
114 static PythonObject
115 ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb);
116 static PythonObject
117 ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb);
118
119 static python::ScopedPythonObject<lldb::SBCommandReturnObject>
120 ToSWIGWrapper(CommandReturnObject &cmd_retobj);
121 // These prototypes are the Pythonic implementations of the required
122 // callbacks. Although these are scripting-language specific, their definition
123 // depends on the public API.
124
125 static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
126 const char *python_function_name, const char *session_dictionary_name,
127 const lldb::StackFrameSP &sb_frame,
128 const lldb::BreakpointLocationSP &sb_bp_loc,
129 const lldb_private::StructuredDataImpl &args_impl);
130
131 static bool LLDBSwigPythonWatchpointCallbackFunction(
132 const char *python_function_name, const char *session_dictionary_name,
133 const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
134
135 static bool
136 LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name,
137 const char *session_dictionary_name,
138 lldb::TypeImplSP type_impl_sp);
139
140 static bool LLDBSwigPythonCallTypeScript(
141 const char *python_function_name, const void *session_dictionary,
142 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
143 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);
144
145 static python::PythonObject
146 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
147 const char *session_dictionary_name,
148 const lldb::ValueObjectSP &valobj_sp);
149
150 static python::PythonObject
151 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
152 const char *session_dictionary_name,
153 lldb::DebuggerSP debugger_sp);
154
155 static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,
156 uint32_t max);
157
158 static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor,
159 uint32_t idx);
160
161 static int LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor,
162 const char *child_name);
163
165 LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data);
166
167 static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
168
169 static bool
170 LLDBSwigPython_MightHaveChildrenSynthProviderInstance(PyObject *implementor);
171
172 static PyObject *
173 LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor);
174
175 static bool
176 LLDBSwigPythonCallCommand(const char *python_function_name,
177 const char *session_dictionary_name,
178 lldb::DebuggerSP debugger, const char *args,
179 lldb_private::CommandReturnObject &cmd_retobj,
180 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
181
182 static bool
183 LLDBSwigPythonCallCommandObject(PyObject *implementor,
184 lldb::DebuggerSP debugger, const char *args,
185 lldb_private::CommandReturnObject &cmd_retobj,
186 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
187 static bool LLDBSwigPythonCallParsedCommandObject(
188 PyObject *implementor, lldb::DebuggerSP debugger,
189 StructuredDataImpl &args_impl,
190 lldb_private::CommandReturnObject &cmd_retobj,
191 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
192
193 static std::optional<std::string>
194 LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor,
195 std::string &command);
196
197 static StructuredData::DictionarySP
198 LLDBSwigPythonHandleArgumentCompletionForScriptedCommand(
199 PyObject *implementor, std::vector<llvm::StringRef> &args_impl,
200 size_t args_pos, size_t pos_in_arg);
201
202 static StructuredData::DictionarySP
203 LLDBSwigPythonHandleOptionArgumentCompletionForScriptedCommand(
204 PyObject *implementor, llvm::StringRef &long_option, size_t pos_in_arg);
205
206 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
207 const char *session_dictionary_name,
208 lldb::DebuggerSP debugger);
209
210 static bool
211 LLDBSwigPythonCallModuleNewTarget(const char *python_module_name,
212 const char *session_dictionary_name,
213 lldb::TargetSP target);
214
215 static python::PythonObject
216 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
217 const char *session_dictionary_name,
218 const lldb::ProcessSP &process_sp);
219
220 static python::PythonObject
221 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
222 const char *session_dictionary_name);
223
224 static PyObject *
225 LLDBSwigPython_GetRecognizedArguments(PyObject *implementor,
226 const lldb::StackFrameSP &frame_sp);
227
228 static bool LLDBSwigPython_ShouldHide(PyObject *implementor,
229 const lldb::StackFrameSP &frame_sp);
230
231 static bool LLDBSWIGPythonRunScriptKeywordProcess(
232 const char *python_function_name, const char *session_dictionary_name,
233 const lldb::ProcessSP &process, std::string &output);
234
235 static std::optional<std::string>
236 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
237 const char *session_dictionary_name,
238 lldb::ThreadSP thread);
239
240 static bool LLDBSWIGPythonRunScriptKeywordTarget(
241 const char *python_function_name, const char *session_dictionary_name,
242 const lldb::TargetSP &target, std::string &output);
243
244 static std::optional<std::string>
245 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
246 const char *session_dictionary_name,
247 lldb::StackFrameSP frame);
248
249 static bool LLDBSWIGPythonRunScriptKeywordValue(
250 const char *python_function_name, const char *session_dictionary_name,
251 const lldb::ValueObjectSP &value, std::string &output);
252
253 static void *
254 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
255 const lldb::TargetSP &target_sp);
256};
257
258void *LLDBSWIGPython_CastPyObjectToSBData(PyObject *data);
259void *LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data);
260void *LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(PyObject *data);
261void *LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data);
262void *LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data);
263void *LLDBSWIGPython_CastPyObjectToSBError(PyObject *data);
264void *LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data);
265void *LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data);
266void *LLDBSWIGPython_CastPyObjectToSBFrame(PyObject *data);
267void *LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject *data);
268void *LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data);
269void *LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data);
270void *LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data);
271} // namespace python
272
273} // namespace lldb_private
274
275#endif // LLDB_ENABLE_PYTHON
276#endif // LLDB_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ThreadPlan > ThreadPlanSP
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
std::shared_ptr< lldb_private::TypeSummaryOptions > TypeSummaryOptionsSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::Watchpoint > WatchpointSP
std::shared_ptr< lldb_private::TypeImpl > TypeImplSP
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
std::shared_ptr< lldb_private::Module > ModuleSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP