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 : public 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
146 static size_t LLDBSwigPython_CalculateNumChildren(PyObject *implementor,
147 uint32_t max);
148
149 static PyObject *LLDBSwigPython_GetChildAtIndex(PyObject *implementor,
150 uint32_t idx);
151
152 static uint32_t
154 const char *child_name);
155
158
159 static bool LLDBSwigPython_UpdateSynthProviderInstance(PyObject *implementor);
160
161 static bool
163
164 static PyObject *
166
167 static bool
168 LLDBSwigPythonCallCommand(const char *python_function_name,
169 const char *session_dictionary_name,
170 lldb::DebuggerSP debugger, const char *args,
172 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
173
174 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
175 const char *session_dictionary_name,
176 lldb::DebuggerSP debugger);
177
178 static bool
179 LLDBSwigPythonCallModuleNewTarget(const char *python_module_name,
180 const char *session_dictionary_name,
181 lldb::TargetSP target);
182
184 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
185 const char *session_dictionary_name,
186 const lldb::ProcessSP &process_sp);
187
189 const char *python_function_name, const char *session_dictionary_name,
190 const lldb::ProcessSP &process, std::string &output);
191
192 static std::optional<std::string>
193 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
194 const char *session_dictionary_name,
195 lldb::ThreadSP thread);
196
198 const char *python_function_name, const char *session_dictionary_name,
199 const lldb::TargetSP &target, std::string &output);
200
201 static std::optional<std::string>
202 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
203 const char *session_dictionary_name,
204 lldb::StackFrameSP frame);
205
207 const char *python_function_name, const char *session_dictionary_name,
208 const lldb::ValueObjectSP &value, std::string &output);
209
210 static void *
211 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
212 const lldb::TargetSP &target_sp);
213};
214
234} // namespace python
235
236} // namespace lldb_private
237
238#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
Defines a symbol context baton that can be handed other debug core functions.
static PythonObject ToSWIGWrapper(lldb::DescriptionLevel level)
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 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 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 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 > 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 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 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 PythonObject ToSWIGWrapper(lldb::ThreadSP thread_sp)
static PythonObject ToSWIGWrapper(const StructuredDataImpl &data_impl)
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_CastPyObjectToSBCommandReturnObject(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBDebugger(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