[Go to site: main page, start]

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);
83 bool use_synthetic);
99 static PythonObject ToSWIGWrapper(const TypeSummaryOptions &summary_options);
101 static PythonObject ToSWIGWrapper(const Stream *stream);
102 static PythonObject ToSWIGWrapper(std::shared_ptr<lldb::SBStream> stream_sb);
104
109
110 static PythonObject
111 ToSWIGWrapper(std::unique_ptr<lldb::SBStructuredData> data_sb);
112 static PythonObject
113 ToSWIGWrapper(std::unique_ptr<lldb::SBFileSpec> file_spec_sb);
114 static PythonObject
115 ToSWIGWrapper(std::unique_ptr<lldb::SBModuleSpec> module_spec_sb);
116
119 // These prototypes are the Pythonic implementations of the required
120 // callbacks. Although these are scripting-language specific, their definition
121 // depends on the public API.
122
123 static llvm::Expected<bool> LLDBSwigPythonBreakpointCallbackFunction(
124 const char *python_function_name, const char *session_dictionary_name,
125 const lldb::StackFrameSP &sb_frame,
126 const lldb::BreakpointLocationSP &sb_bp_loc,
127 const lldb_private::StructuredDataImpl &args_impl);
128
130 const char *python_function_name, const char *session_dictionary_name,
131 const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp);
132
133 static bool
134 LLDBSwigPythonFormatterCallbackFunction(const char *python_function_name,
135 const char *session_dictionary_name,
136 lldb::TypeImplSP type_impl_sp);
137
139 const char *python_function_name, const void *session_dictionary,
140 const lldb::ValueObjectSP &valobj_sp, void **pyfunct_wrapper,
141 const lldb::TypeSummaryOptionsSP &options_sp, std::string &retval);
142
144 LLDBSwigPythonCreateCommandObject(const char *python_class_name,
145 const char *session_dictionary_name,
146 lldb::DebuggerSP debugger_sp);
147
150
151 static bool
152 LLDBSwigPythonCallCommand(const char *python_function_name,
153 const char *session_dictionary_name,
154 lldb::DebuggerSP debugger, const char *args,
156 lldb::ExecutionContextRefSP exe_ctx_ref_sp);
157
158 static bool LLDBSwigPythonCallModuleInit(const char *python_module_name,
159 const char *session_dictionary_name,
160 lldb::DebuggerSP debugger);
161
162 static bool
163 LLDBSwigPythonCallModuleNewTarget(const char *python_module_name,
164 const char *session_dictionary_name,
165 lldb::TargetSP target);
166
168 LLDBSWIGPythonCreateOSPlugin(const char *python_class_name,
169 const char *session_dictionary_name,
170 const lldb::ProcessSP &process_sp);
171
173 const char *python_function_name, const char *session_dictionary_name,
174 const lldb::ProcessSP &process, std::string &output);
175
176 static std::optional<std::string>
177 LLDBSWIGPythonRunScriptKeywordThread(const char *python_function_name,
178 const char *session_dictionary_name,
179 lldb::ThreadSP thread);
180
182 const char *python_function_name, const char *session_dictionary_name,
183 const lldb::TargetSP &target, std::string &output);
184
185 static std::optional<std::string>
186 LLDBSWIGPythonRunScriptKeywordFrame(const char *python_function_name,
187 const char *session_dictionary_name,
188 lldb::StackFrameSP frame);
189
191 const char *python_function_name, const char *session_dictionary_name,
192 const lldb::ValueObjectSP &value, std::string &output);
193
194 static void *
195 LLDBSWIGPython_GetDynamicSetting(void *module, const char *setting,
196 const lldb::TargetSP &target_sp);
197};
198
218} // namespace python
219
220} // namespace lldb_private
221
222#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(lldb::ValueObjectSP value_sp, bool use_synthetic)
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 PythonObject ToSWIGWrapper(lldb::ProcessLaunchInfoSP launch_info_sp)
static PythonObject ToSWIGWrapper(lldb::DebuggerSP debugger_sp)
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 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 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 bool LLDBSwigPythonWatchpointCallbackFunction(const char *python_function_name, const char *session_dictionary_name, const lldb::StackFrameSP &sb_frame, const lldb::WatchpointSP &sb_wp)
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 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 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