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_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
10#define LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
11
12#include <optional>
13#include <string>
14
15// LLDB Python header must be included first
16#include "lldb-python.h"
17
19#include "lldb/lldb-forward.h"
20#include "lldb/lldb-types.h"
21#include "llvm/Support/Error.h"
22
23namespace lldb {
24class SBEvent;
26class SBValue;
27class SBStream;
29class SBFileSpec;
30class SBModuleSpec;
31class SBStringList;
32} // namespace lldb
33
34namespace lldb_private {
35namespace python {
36
38
40
41/// A class that automatically clears an SB object when it goes out of scope.
42/// Use for cases where the SB object points to a temporary/unowned entity.
43template <typename T> class ScopedPythonObject : PythonObject {
44public:
46 : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
48 if (m_sb)
49 *m_sb = T();
50 }
52 : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
56
57 const PythonObject &obj() const { return *this; }
58
59private:
60 T *m_sb;
61};
62
63// TODO: We may want to support other languages in the future w/ SWIG (we
64// already support Lua right now, for example). We could create a generic
65// SWIGBridge class and have this one specialize it, something like this:
66//
67// <typename T>
68// class SWIGBridge {
69// static T ToSWIGWrapper(...);
70// };
71//
72// class SWIGPythonBridge : public SWIGBridge<PythonObject> {
73// template<> static PythonObject ToSWIGWrapper(...);
74// };
75//
76// And we should be able to more easily support things like Lua
78public:
79 static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
80 static PythonObject
81 ToSWIGWrapper(std::unique_ptr<lldb::SBCommandReturnObject> result_up);
98 static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
100 static PythonObject ToSWIGWrapper(const Stream *stream);
101 static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb);
103
108
109 static PythonObject
110 ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
111 static PythonObject
112 ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb);
113 static PythonObject
114 ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb);
115
118 // These prototypes are the Pythonic implementations of the required
119 // callbacks. Although these are scripting-language specific, their definition
120 // depends on the public API.
121
122 static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
123 const char *python_function_name, const char *session_dictionary_name,
124 const lldb::StackFrameSP &sb_frame,
125 const lldb::BreakpointLocationSP &sb_bp_loc,
126 const lldb_private::StructuredDataImpl &args_impl);
127
129 const char *python_function_name, const char *session_dictionary_name,
130 const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
131
132 static bool
133 LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name,
134 const char *session_dictionary_name,
135 lldb::TypeImplSP type_impl_sp);
136
138 const char *python_function_name, const void *session_dictionary,
139 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
140 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);
141
143 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
144 const char *session_dictionary_name,
145 const lldb::ValueObjectSP &valobj_sp);
146
148 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
149 const char *session_dictionary_name,
150 lldb::DebuggerSP debugger_sp);
151
152 static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,
153 uint32_t max);
154
155 static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor,
156 uint32_t idx);
157
158 static uint32_t
160 const char *child_name);
161
164
165 static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
166
167 static bool
169
170 static PyObject *
172
173 static bool
174 LLDBSwigPythonCallCommand(const char *python_function_name,
175 const char *session_dictionary_name,
176 lldb::DebuggerSP debugger, const char *args,
178 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
179
180 static bool
181 LLDBSwigPythonCallCommandObject(PyObject *implementor,
182 lldb::DebuggerSP debugger, const char *args,
184 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
186 PyObject *implementor, lldb::DebuggerSP debugger,
187 StructuredDataImpl &args_impl,
189 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
190
191 static std::optional<std::string>
193 std::string &command);
194
197 PyObject *implementor, std::vector<llvm::StringRef> &args_impl,
198 size_t args_pos, size_t pos_in_arg);
199
202 PyObject *implementor, llvm::StringRef &long_option, size_t pos_in_arg);
203
204 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
205 const char *session_dictionary_name,
206 lldb::DebuggerSP debugger);
207
208 static bool
209 LLDBSwigPythonCallModuleNewTarget(const char *python_module_name,
210 const char *session_dictionary_name,
211 lldb::TargetSP target);
212
214 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
215 const char *session_dictionary_name,
216 const lldb::ProcessSP &process_sp);
217
219 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
220 const char *session_dictionary_name);
221
222 static PyObject *
224 const lldb::StackFrameSP &frame_sp);
225
226 static bool LLDBSwigPython_ShouldHide(PyObject *implementor,
227 const lldb::StackFrameSP &frame_sp);
228
230 const char *python_function_name, const char *session_dictionary_name,
231 const lldb::ProcessSP &process, std::string &output);
232
233 static std::optional<std::string>
234 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
235 const char *session_dictionary_name,
236 lldb::ThreadSP thread);
237
239 const char *python_function_name, const char *session_dictionary_name,
240 const lldb::TargetSP &target, std::string &output);
241
242 static std::optional<std::string>
243 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
244 const char *session_dictionary_name,
245 lldb::StackFrameSP frame);
246
248 const char *python_function_name, const char *session_dictionary_name,
249 const lldb::ValueObjectSP &value, std::string &output);
250
251 static void *
252 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
253 const lldb::TargetSP &target_sp);
254};
255
272} // namespace python
273
274} // namespace lldb_private
275
276#endif // LLDB_SOURCE_PLUGINS_SCRIPTINTERPRETER_PYTHON_SWIGPYTHONBRIDGE_H
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
std::shared_ptr< Dictionary > DictionarySP
Defines a symbol context baton that can be handed other debug core functions.
static PythonObject ToSWIGWrapper(lldb::DescriptionLevel level)
static PyObject * LLDBSwigPython_GetRecognizedArguments(PyObject *implementor, const lldb::StackFrameSP &frame_sp)
static PythonObject ToSWIGWrapper(std::unique_ptr< lldb::SBModuleSpec > module_spec_sb)
static PythonObject ToSWIGWrapper(std::unique_ptr< lldb::SBStructuredData > data_sb)
static PythonObject ToSWIGWrapper(lldb::ProcessAttachInfoSP attach_info_sp)
static PythonObject ToSWIGWrapper(lldb::DataExtractorSP data_extractor_sp)
static PythonObject ToSWIGWrapper(lldb::StackFrameSP frame_sp)
static PythonObject ToSWIGWrapper(lldb::ValueObjectSP value_sp)
static bool LLDBSWIGPythonRunScriptKeywordValue(const char *python_function_name, const char *session_dictionary_name, const lldb::ValueObjectSP &value, std::string &output)
static bool LLDBSwigPythonCallParsedCommandObject(PyObject *implementor, lldb::DebuggerSP debugger, StructuredDataImpl &args_impl, lldb_private::CommandReturnObject &cmd_retobj, lldb::ExecutionContextRefSP exe_ctx_ref_sp)
static 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)
static void * LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting, const lldb::TargetSP &target_sp)
static PythonObject ToSWIGWrapper(const SymbolContext &sym_ctx)
static python::ScopedPythonObject< lldb::SBCommandReturnObject > ToSWIGWrapper(CommandReturnObject &cmd_retobj)
static lldb::ValueObjectSP LLDBSWIGPython_GetValueObjectSPFromSBValue(void *data)
static PythonObject ToSWIGWrapper(lldb::ProcessSP process_sp)
static std::optional< std::string > LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name, const char *session_dictionary_name, lldb::ThreadSP thread)
static StructuredData::DictionarySP LLDBSwigPythonHandleArgumentCompletionForScriptedCommand(PyObject *implementor, std::vector< llvm::StringRef > &args_impl, size_t args_pos, size_t pos_in_arg)
static PythonObject ToSWIGWrapper(Status &&status)
static python::PythonObject LLDBSWIGPythonCreateOSPlugin(const char *python_class_name, const char *session_dictionary_name, const lldb::ProcessSP &process_sp)
static 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)
static PythonObject ToSWIGWrapper(lldb::TargetSP target_sp)
static PyObject * LLDBSwigPython_GetValueSynthProviderInstance(PyObject *implementor)
static PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp)
static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp)
static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor)
static PythonObject ToSWIGWrapper(lldb::ThreadPlanSP thread_plan_sp)
static StructuredData::DictionarySP LLDBSwigPythonHandleOptionArgumentCompletionForScriptedCommand(PyObject *implementor, llvm::StringRef &long_option, size_t pos_in_arg)
static PythonObject ToSWIGWrapper(lldb::ExecutionContextRefSP ctx_sp)
static bool LLDBSWIGPythonRunScriptKeywordTarget(const char *python_function_name, const char *session_dictionary_name, const lldb::TargetSP &target, std::string &output)
static uint32_t LLDBSwigPython_GetIndexOfChildWithName(PyObject *implementor, const char *child_name)
static std::optional< std::string > LLDBSwigPythonGetRepeatCommandForScriptedCommand(PyObject *implementor, std::string &command)
static std::optional< std::string > LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name, const char *session_dictionary_name, lldb::StackFrameSP frame)
static PythonObject ToSWIGWrapper(lldb::BreakpointLocationSP bp_loc_sp)
static PyObject * LLDBSwigPython_GetChildAtIndex(PyObject *implementor, uint32_t idx)
static PythonObject ToSWIGWrapper(lldb::WatchpointSP watchpoint_sp)
static PythonObject ToSWIGWrapper(lldb::ModuleSP module_sp)
static bool LLDBSWIGPythonRunScriptKeywordProcess(const char *python_function_name, const char *session_dictionary_name, const lldb::ProcessSP &process, std::string &output)
static bool LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name, const char *session_dictionary_name, lldb::TypeImplSP type_impl_sp)
static bool LLDBSwigPythonCallModuleInit(const char *python_module_name, const char *session_dictionary_name, lldb::DebuggerSP debugger)
static python::PythonObject LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name, const char *session_dictionary_name, const lldb::ValueObjectSP &valobj_sp)
static bool LLDBSwigPythonWatchpointCallbackFunction(const char *python_function_name, const char *session_dictionary_name, const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp)
static python::PythonObject LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name, const char *session_dictionary_name)
static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor, uint32_t max)
static PythonObject ToSWIGWrapper(std::unique_ptr< lldb::SBFileSpec > file_spec_sb)
static PythonObject ToSWIGWrapper(std::shared_ptr< lldb::SBStream > stream_sb)
static PythonObject ToSWIGWrapper(std::unique_ptr< lldb::SBValue > value_sb)
static PythonObject ToSWIGWrapper(lldb::BreakpointSP breakpoint_sp)
static PythonObject ToSWIGWrapper(const Stream *stream)
static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options)
static PythonObject ToSWIGWrapper(lldb::TypeImplSP type_impl_sp)
static PythonObject ToSWIGWrapper(lldb::StackFrameListSP frames_sp)
static bool LLDBSwigPythonCallCommandObject(PyObject *implementor, lldb::DebuggerSP debugger, const char *args, lldb_private::CommandReturnObject &cmd_retobj, lldb::ExecutionContextRefSP exe_ctx_ref_sp)
static bool LLDBSwigPython_MightHaveChildrenSynthProviderInstance(PyObject *implementor)
static PythonObject ToSWIGWrapper(std::unique_ptr< lldb::SBCommandReturnObject > result_up)
static PythonObject ToSWIGWrapper(Event *event)
static bool LLDBSwigPythonCallModuleNewTarget(const char *python_module_name, const char *session_dictionary_name, lldb::TargetSP target)
static python::PythonObject LLDBSwigPythonCreateCommandObject(const char *python_class_name, const char *session_dictionary_name, lldb::DebuggerSP debugger_sp)
static PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp)
static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl)
static bool LLDBSwigPython_ShouldHide(PyObject *implementor, const lldb::StackFrameSP &frame_sp)
static llvm::Expected< bool > LLDBSwigPythonBreakpointCallbackFunction(const char *python_function_name, const char *session_dictionary_name, const lldb::StackFrameSP &sb_frame, const lldb::BreakpointLocationSP &sb_bp_loc, const lldb_private::StructuredDataImpl &args_impl)
A class that automatically clears an SB object when it goes out of scope.
ScopedPythonObject(ScopedPythonObject &&rhs)
ScopedPythonObject(T *sb, swig_type_info *info)
ScopedPythonObject & operator=(const ScopedPythonObject &)=delete
ScopedPythonObject & operator=(ScopedPythonObject &&)=delete
ScopedPythonObject(const ScopedPythonObject &)=delete
void * LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBFrameList(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBThread(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBData(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data)
struct swig_type_info swig_type_info
void * LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBError(PyObject *data)
python::PythonObject ToSWIGHelper(void *obj, swig_type_info *info)
void * LLDBSWIGPython_CastPyObjectToSBAttachInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBLaunchInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBValueList(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBValue(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBEvent(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBBreakpoint(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBFrame(PyObject *data)
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::StackFrameList > StackFrameListSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP