LLDB mainline
ScriptedPythonInterface.cpp
Go to the documentation of this file.
1//===-- ScriptedPythonInterface.cpp ---------------------------------------===//
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#include "lldb/Host/Config.h"
10#include "lldb/Utility/Log.h"
12
13#if LLDB_ENABLE_PYTHON
14
15// LLDB Python header must be included first
16#include "../lldb-python.h"
17
21#include <optional>
22
23using namespace lldb;
24using namespace lldb_private;
25
26ScriptedPythonInterface::ScriptedPythonInterface(
27 ScriptInterpreterPythonImpl &interpreter)
28 : ScriptedInterface(), m_interpreter(interpreter) {}
29
30template <>
32ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
33 python::PythonObject &p, Status &error) {
34 python::PythonList result_list(python::PyRefType::Borrowed, p.get());
35 return result_list.CreateStructuredArray();
36}
37
38template <>
40ScriptedPythonInterface::ExtractValueFromPythonObject<
41 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
42 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
43 return result_dict.CreateStructuredDictionary();
44}
45
46template <>
47Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
48 python::PythonObject &p, Status &error) {
49 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
50 python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
51 return m_interpreter.GetStatusFromSBError(*sb_error);
52 error =
53 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");
54
55 return {};
56}
57
58template <>
59Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
60 python::PythonObject &p, Status &error) {
61 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
62 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
63 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
65 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
66
67 return nullptr;
68}
69
70template <>
72ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
73 python::PythonObject &p, Status &error) {
74 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
75 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
76 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
78 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
79
80 return nullptr;
81}
82
83template <>
85ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameSP>(
86 python::PythonObject &p, Status &error) {
87 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(
88 python::LLDBSWIGPython_CastPyObjectToSBFrame(p.get())))
89 return m_interpreter.GetOpaqueTypeFromSBFrame(*sb_frame);
91 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");
92
93 return nullptr;
94}
95
96template <>
98ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ThreadSP>(
99 python::PythonObject &p, Status &error) {
100 if (lldb::SBThread *sb_thread = reinterpret_cast<lldb::SBThread *>(
101 python::LLDBSWIGPython_CastPyObjectToSBThread(p.get())))
102 return m_interpreter.GetOpaqueTypeFromSBThread(*sb_thread);
104 "Couldn't cast lldb::SBThread to lldb_private::Thread.");
105
106 return nullptr;
107}
108
109template <>
111ScriptedPythonInterface::ExtractValueFromPythonObject<SymbolContext>(
112 python::PythonObject &p, Status &error) {
113 if (lldb::SBSymbolContext *sb_symbol_context =
114 reinterpret_cast<lldb::SBSymbolContext *>(
115 python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(p.get())))
116 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
118 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
119
120 return {};
121}
122
123template <>
125ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
126 python::PythonObject &p, Status &error) {
127 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
128 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
129
130 if (!sb_data) {
132 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
133 return nullptr;
134 }
135
136 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
137}
138
139template <>
141ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
142 python::PythonObject &p, Status &error) {
143 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
144 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
145
146 if (!sb_breakpoint) {
148 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
149 return nullptr;
150 }
151
152 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
153}
154
155template <>
157ScriptedPythonInterface::ExtractValueFromPythonObject<
158 lldb::BreakpointLocationSP>(python::PythonObject &p, Status &error) {
159 lldb::SBBreakpointLocation *sb_break_loc =
160 reinterpret_cast<lldb::SBBreakpointLocation *>(
161 python::LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(p.get()));
162
163 if (!sb_break_loc) {
165 "Couldn't cast lldb::SBBreakpointLocation to "
166 "lldb::BreakpointLocationSP.");
167 return nullptr;
168 }
169
170 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);
171}
172
173template <>
174lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
175 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
176 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
177 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
178
179 if (!sb_attach_info) {
181 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
182 return nullptr;
183 }
184
185 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
186}
187
188template <>
189lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
190 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
191 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
192 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
193
194 if (!sb_launch_info) {
196 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
197 return nullptr;
198 }
199
200 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
201}
202
203template <>
204std::optional<MemoryRegionInfo>
205ScriptedPythonInterface::ExtractValueFromPythonObject<
206 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
207
208 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
209 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
210 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
211
212 if (!sb_mem_reg_info) {
214 "Couldn't cast lldb::SBMemoryRegionInfo to "
215 "lldb_private::MemoryRegionInfo.");
216 return {};
217 }
218
219 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
220}
221
222template <>
224ScriptedPythonInterface::ExtractValueFromPythonObject<
225 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
226
227 lldb::SBExecutionContext *sb_exe_ctx =
228 reinterpret_cast<lldb::SBExecutionContext *>(
229 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
230
231 if (!sb_exe_ctx) {
233 "Couldn't cast lldb::SBExecutionContext to "
234 "lldb::ExecutionContextRefSP.");
235 return {};
236 }
237
238 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
239}
240
241template <>
243ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DescriptionLevel>(
244 python::PythonObject &p, Status &error) {
246 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
247 if (!unsigned_or_err) {
248 error = (Status::FromError(unsigned_or_err.takeError()));
249 return ret_val;
250 }
251 unsigned long long unsigned_val = *unsigned_or_err;
253 error = Status("value too large for lldb::DescriptionLevel.");
254 return ret_val;
255 }
256 return static_cast<lldb::DescriptionLevel>(unsigned_val);
257}
258
259template <>
261ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameListSP>(
262 python::PythonObject &p, Status &error) {
263
264 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
265 python::LLDBSWIGPython_CastPyObjectToSBFrameList(p.get()));
266
267 if (!sb_frame_list) {
269 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
270 return {};
271 }
272
273 return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);
274}
275
276#endif
static llvm::raw_ostream & error(Stream &strm)
Represents a list of SBFrame objects.
Definition SBFrameList.h:31
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status FromError(llvm::Error error)
Avoid using this in new code. Migrate APIs to llvm::Expected instead.
Definition Status.cpp:137
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Array > ArraySP
Defines a symbol context baton that can be handed other debug core functions.
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::BreakpointLocation > BreakpointLocationSP
DescriptionLevel
Description levels for "void GetDescription(Stream *, DescriptionLevel)" calls.
@ eDescriptionLevelBrief
@ kNumDescriptionLevels
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
std::shared_ptr< lldb_private::Stream > StreamSP
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP