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
22#include <optional>
23
24using namespace lldb;
25using namespace lldb_private;
26
27ScriptedPythonInterface::ScriptedPythonInterface(
28 ScriptInterpreterPythonImpl &interpreter)
29 : ScriptedInterface(), m_interpreter(interpreter) {}
30
31template <>
33ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
34 python::PythonObject &p, Status &error) {
35 python::PythonList result_list(python::PyRefType::Borrowed, p.get());
36 return result_list.CreateStructuredArray();
37}
38
39template <>
41ScriptedPythonInterface::ExtractValueFromPythonObject<
42 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
43 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
44 return result_dict.CreateStructuredDictionary();
45}
46
47template <>
48Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
49 python::PythonObject &p, Status &error) {
50 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
51 python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
52 return m_interpreter.GetStatusFromSBError(*sb_error);
53 error =
54 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");
55
56 return {};
57}
58
59template <>
60Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
61 python::PythonObject &p, Status &error) {
62 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
63 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
64 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
66 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
67
68 return nullptr;
69}
70
71template <>
73ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
74 python::PythonObject &p, Status &error) {
75 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
76 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
77 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
79 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
80
81 return nullptr;
82}
83
84template <>
86ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameSP>(
87 python::PythonObject &p, Status &error) {
88 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(
89 python::LLDBSWIGPython_CastPyObjectToSBFrame(p.get())))
90 return m_interpreter.GetOpaqueTypeFromSBFrame(*sb_frame);
92 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");
93
94 return nullptr;
95}
96
97template <>
99ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ThreadSP>(
100 python::PythonObject &p, Status &error) {
101 if (lldb::SBThread *sb_thread = reinterpret_cast<lldb::SBThread *>(
102 python::LLDBSWIGPython_CastPyObjectToSBThread(p.get())))
103 return m_interpreter.GetOpaqueTypeFromSBThread(*sb_thread);
105 "Couldn't cast lldb::SBThread to lldb_private::Thread.");
106
107 return nullptr;
108}
109
110template <>
112ScriptedPythonInterface::ExtractValueFromPythonObject<SymbolContext>(
113 python::PythonObject &p, Status &error) {
114 if (lldb::SBSymbolContext *sb_symbol_context =
115 reinterpret_cast<lldb::SBSymbolContext *>(
116 python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(p.get())))
117 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
119 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
120
121 return {};
122}
123
124template <>
126ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
127 python::PythonObject &p, Status &error) {
128 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
129 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
130
131 if (!sb_data) {
133 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
134 return nullptr;
135 }
136
137 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
138}
139
140template <>
142ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
143 python::PythonObject &p, Status &error) {
144 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
145 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
146
147 if (!sb_breakpoint) {
149 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
150 return nullptr;
151 }
152
153 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
154}
155
156template <>
158ScriptedPythonInterface::ExtractValueFromPythonObject<
159 lldb::BreakpointLocationSP>(python::PythonObject &p, Status &error) {
160 lldb::SBBreakpointLocation *sb_break_loc =
161 reinterpret_cast<lldb::SBBreakpointLocation *>(
162 python::LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(p.get()));
163
164 if (!sb_break_loc) {
166 "Couldn't cast lldb::SBBreakpointLocation to "
167 "lldb::BreakpointLocationSP.");
168 return nullptr;
169 }
170
171 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);
172}
173
174template <>
175lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
176 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
177 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
178 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
179
180 if (!sb_attach_info) {
182 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
183 return nullptr;
184 }
185
186 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
187}
188
189template <>
190lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
191 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
192 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
193 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
194
195 if (!sb_launch_info) {
197 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
198 return nullptr;
199 }
200
201 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
202}
203
204template <>
205std::optional<MemoryRegionInfo>
206ScriptedPythonInterface::ExtractValueFromPythonObject<
207 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
208
209 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
210 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
211 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
212
213 if (!sb_mem_reg_info) {
215 "Couldn't cast lldb::SBMemoryRegionInfo to "
216 "lldb_private::MemoryRegionInfo.");
217 return {};
218 }
219
220 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
221}
222
223template <>
225ScriptedPythonInterface::ExtractValueFromPythonObject<
226 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
227
228 lldb::SBExecutionContext *sb_exe_ctx =
229 reinterpret_cast<lldb::SBExecutionContext *>(
230 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
231
232 if (!sb_exe_ctx) {
234 "Couldn't cast lldb::SBExecutionContext to "
235 "lldb::ExecutionContextRefSP.");
236 return {};
237 }
238
239 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
240}
241
242template <>
244ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DescriptionLevel>(
245 python::PythonObject &p, Status &error) {
247 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
248 if (!unsigned_or_err) {
249 error = (Status::FromError(unsigned_or_err.takeError()));
250 return ret_val;
251 }
252 unsigned long long unsigned_val = *unsigned_or_err;
254 error = Status("value too large for lldb::DescriptionLevel.");
255 return ret_val;
256 }
257 return static_cast<lldb::DescriptionLevel>(unsigned_val);
258}
259
260template <>
262ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StackFrameListSP>(
263 python::PythonObject &p, Status &error) {
264
265 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
266 python::LLDBSWIGPython_CastPyObjectToSBFrameList(p.get()));
267
268 if (!sb_frame_list) {
270 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
271 return {};
272 }
273
274 return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);
275}
276
277template <>
279ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectSP>(
280 python::PythonObject &p, Status &error) {
281 lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
282 python::LLDBSWIGPython_CastPyObjectToSBValue(p.get()));
283 if (!sb_value) {
285 "couldn't cast lldb::SBValue to lldb::ValueObjectSP");
286 return {};
287 }
288
289 return m_interpreter.GetOpaqueTypeFromSBValue(*sb_value);
290}
291
292template <>
294ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::ValueObjectListSP>(
295 python::PythonObject &p, Status &error) {
296 lldb::SBValueList *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
297 python::LLDBSWIGPython_CastPyObjectToSBValueList(p.get()));
298
299 if (!sb_value_list) {
301 "couldn't cast lldb::SBValueList to lldb::ValueObjectListSP");
302 return {};
303 }
304
305 lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
306 for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
307 SBValue value = sb_value_list->GetValueAtIndex(i);
308 out->Append(m_interpreter.GetOpaqueTypeFromSBValue(value));
309 }
310
311 return out;
312}
313
314#endif
static llvm::raw_ostream & error(Stream &strm)
uint32_t GetSize()
lldb::SBValue GetValueAtIndex(uint32_t index)
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::ValueObject > ValueObjectSP
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::ValueObjectList > ValueObjectListSP
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