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#include "lldb-python.h"
16
18#include "lldb/lldb-forward.h"
19#include "lldb/lldb-types.h"
20#include "llvm/Support/Error.h"
21
22namespace lldb {
23class SBEvent;
25class SBValue;
26class SBStream;
28class SBFileSpec;
29class SBModuleSpec;
30class SBStringList;
31} // namespace lldb
32
33namespace lldb_private {
34namespace python {
35
37
39
40/// A class that automatically clears an SB object when it goes out of scope.
41/// Use for cases where the SB object points to a temporary/unowned entity.
42template <typename T> class ScopedPythonObject : PythonObject {
43public:
45 : PythonObject(ToSWIGHelper(sb, info)), m_sb(sb) {}
47 if (m_sb)
48 *m_sb = T();
49 }
51 : PythonObject(std::move(rhs)), m_sb(std::exchange(rhs.m_sb, nullptr)) {}
55
56 const PythonObject &obj() const { return *this; }
57
58private:
59 T *m_sb;
60};
61
62// TODO: We may want to support other languages in the future w/ SWIG (we
63// already support Lua right now, for example). We could create a generic
64// SWIGBridge class and have this one specialize it, something like this:
65//
66// <typename T>
67// class SWIGBridge {
68// static T ToSWIGWrapper(...);
69// };
70//
71// class SWIGPythonBridge : public SWIGBridge<PythonObject> {
72// template<> static PythonObject ToSWIGWrapper(...);
73// };
74//
75// And we should be able to more easily support things like Lua
77public:
78 static PythonObject ToSWIGWrapper(std::unique_ptr<lldb::SBValue> value_sb);
79 static PythonObject
80 ToSWIGWrapper(std::unique_ptr<lldb::SBCommandReturnObject> result_up);
97 static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
99 static PythonObject ToSWIGWrapper(const Stream *stream);
100 static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb);
102
107
108 static PythonObject
109 ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
110 static PythonObject
111 ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb);
112 static PythonObject
113 ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb);
114
117 // These prototypes are the Pythonic implementations of the required
118 // callbacks. Although these are scripting-language specific, their definition
119 // depends on the public API.
120
121 static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
122 const char *python_function_name, const char *session_dictionary_name,
123 const lldb::StackFrameSP &sb_frame,
124 const lldb::BreakpointLocationSP &sb_bp_loc,
125 const lldb_private::StructuredDataImpl &args_impl);
126
128 const char *python_function_name, const char *session_dictionary_name,
129 const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
130
131 static bool
132 LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name,
133 const char *session_dictionary_name,
134 lldb::TypeImplSP type_impl_sp);
135
137 const char *python_function_name, const void *session_dictionary,
138 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
139 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);
140
142 LLDBSwigPythonCreateSyntheticProvider(const char *python_class_name,
143 const char *session_dictionary_name,
144 const lldb::ValueObjectSP &valobj_sp);
145
147 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
148 const char *session_dictionary_name,
149 lldb::DebuggerSP debugger_sp);
150
151 static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,
152 uint32_t max);
153
154 static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor,
155 uint32_t idx);
156
157 static uint32_t
159 const char *child_name);
160
163
164 static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
165
166 static bool
168
169 static PyObject *
171
172 static bool
173 LLDBSwigPythonCallCommand(const char *python_function_name,
174 const char *session_dictionary_name,
175 lldb::DebuggerSP debugger, const char *args,
177 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
178
179 static bool
180 LLDBSwigPythonCallCommandObject(PyObject *implementor,
181 lldb::DebuggerSP debugger, const char *args,
183 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
185 PyObject *implementor, lldb::DebuggerSP debugger,
186 StructuredDataImpl &args_impl,
188 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
189
190 static std::optional<std::string>
192 std::string &command);
193
196 PyObject *implementor, std::vector<llvm::StringRef> &args_impl,
197 size_t args_pos, size_t pos_in_arg);
198
201 PyObject *implementor, llvm::StringRef &long_option, size_t pos_in_arg);
202
203 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
204 const char *session_dictionary_name,
205 lldb::DebuggerSP debugger);
206
207 static bool
208 LLDBSwigPythonCallModuleNewTarget(const char *python_module_name,
209 const char *session_dictionary_name,
210 lldb::TargetSP target);
211
213 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
214 const char *session_dictionary_name,
215 const lldb::ProcessSP &process_sp);
216
218 LLDBSWIGPython_CreateFrameRecognizer(const char *python_class_name,
219 const char *session_dictionary_name);
220
221 static PyObject *
223 const lldb::StackFrameSP &frame_sp);
224
225 static bool LLDBSwigPython_ShouldHide(PyObject *implementor,
226 const lldb::StackFrameSP &frame_sp);
227
229 const char *python_function_name, const char *session_dictionary_name,
230 const lldb::ProcessSP &process, std::string &output);
231
232 static std::optional<std::string>
233 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
234 const char *session_dictionary_name,
235 lldb::ThreadSP thread);
236
238 const char *python_function_name, const char *session_dictionary_name,
239 const lldb::TargetSP &target, std::string &output);
240
241 static std::optional<std::string>
242 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
243 const char *session_dictionary_name,
244 lldb::StackFrameSP frame);
245
247 const char *python_function_name, const char *session_dictionary_name,
248 const lldb::ValueObjectSP &value, std::string &output);
249
250 static void *
251 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
252 const lldb::TargetSP &target_sp);
253};
254
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_CastPyObjectToSBTarget(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