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// LLDB Python header must be included first
14#include "../lldb-python.h"
15
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24
28
29template <>
36
37template <>
44
45template <>
48 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
50 return m_interpreter.GetStatusFromSBError(*sb_error);
51 error =
52 Status::FromErrorString("Couldn't cast lldb::SBError to lldb::Status.");
53
54 return {};
55}
56
57template <>
60 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
62 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
64 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
65
66 return nullptr;
67}
68
69template <>
73 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
75 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
77 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
78
79 return nullptr;
80}
81
82template <>
86 if (lldb::SBFrame *sb_frame = reinterpret_cast<lldb::SBFrame *>(
88 return m_interpreter.GetOpaqueTypeFromSBFrame(*sb_frame);
90 "Couldn't cast lldb::SBFrame to lldb_private::StackFrame.");
91
92 return nullptr;
93}
94
95template <>
99 if (lldb::SBThread *sb_thread = reinterpret_cast<lldb::SBThread *>(
101 return m_interpreter.GetOpaqueTypeFromSBThread(*sb_thread);
103 "Couldn't cast lldb::SBThread to lldb_private::Thread.");
104
105 return nullptr;
106}
107
108template <>
112 if (lldb::SBSymbolContext *sb_symbol_context =
113 reinterpret_cast<lldb::SBSymbolContext *>(
115 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
117 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
118
119 return {};
120}
121
122template <>
126 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
128
129 if (!sb_data) {
131 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
132 return nullptr;
133 }
134
135 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
136}
137
138template <>
142 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
144
145 if (!sb_breakpoint) {
147 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
148 return nullptr;
149 }
150
151 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
152}
153
154template <>
158 lldb::SBBreakpointLocation *sb_break_loc =
159 reinterpret_cast<lldb::SBBreakpointLocation *>(
161
162 if (!sb_break_loc) {
164 "Couldn't cast lldb::SBBreakpointLocation to "
165 "lldb::BreakpointLocationSP.");
166 return nullptr;
167 }
168
169 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);
170}
171
172template <>
175 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
177
178 if (!sb_attach_info) {
180 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
181 return nullptr;
182 }
183
184 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
185}
186
187template <>
190 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
192
193 if (!sb_launch_info) {
195 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
196 return nullptr;
197 }
198
199 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
200}
201
202template <>
203std::optional<MemoryRegionInfo>
205 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
206
207 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
208 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
210
211 if (!sb_mem_reg_info) {
213 "Couldn't cast lldb::SBMemoryRegionInfo to "
214 "lldb_private::MemoryRegionInfo.");
215 return {};
216 }
217
218 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
219}
220
221template <>
225
226 lldb::SBExecutionContext *sb_exe_ctx =
227 reinterpret_cast<lldb::SBExecutionContext *>(
229
230 if (!sb_exe_ctx) {
232 "Couldn't cast lldb::SBExecutionContext to "
233 "lldb::ExecutionContextRefSP.");
234 return {};
235 }
236
237 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
238}
239
240template <>
245 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
246 if (!unsigned_or_err) {
247 error = (Status::FromError(unsigned_or_err.takeError()));
248 return ret_val;
249 }
250 unsigned long long unsigned_val = *unsigned_or_err;
252 error = Status("value too large for lldb::DescriptionLevel.");
253 return ret_val;
254 }
255 return static_cast<lldb::DescriptionLevel>(unsigned_val);
256}
257
258template <>
262
263 lldb::SBFrameList *sb_frame_list = reinterpret_cast<lldb::SBFrameList *>(
265
266 if (!sb_frame_list) {
268 "couldn't cast lldb::SBFrameList to lldb::StackFrameListSP.");
269 return {};
270 }
271
272 return m_interpreter.GetOpaqueTypeFromSBFrameList(*sb_frame_list);
273}
274
275template <>
279 lldb::SBValue *sb_value = reinterpret_cast<lldb::SBValue *>(
281 if (!sb_value) {
283 "couldn't cast lldb::SBValue to lldb::ValueObjectSP");
284 return {};
285 }
286
287 return m_interpreter.GetOpaqueTypeFromSBValue(*sb_value);
288}
289
290template <>
294 lldb::SBValueList *sb_value_list = reinterpret_cast<lldb::SBValueList *>(
296
297 if (!sb_value_list) {
299 "couldn't cast lldb::SBValueList to lldb::ValueObjectListSP");
300 return {};
301 }
302
303 lldb::ValueObjectListSP out = std::make_shared<ValueObjectList>();
304 for (uint32_t i = 0, e = sb_value_list->GetSize(); i < e; ++i) {
305 SBValue value = sb_value_list->GetValueAtIndex(i);
306 out->Append(m_interpreter.GetOpaqueTypeFromSBValue(value));
307 }
308
309 return out;
310}
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
ScriptInterpreterPythonImpl & m_interpreter
T ExtractValueFromPythonObject(python::PythonObject &p, Status &error)
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
An error handling class.
Definition Status.h:118
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.
StructuredData::DictionarySP CreateStructuredDictionary() const
StructuredData::ArraySP CreateStructuredArray() const
llvm::Expected< unsigned long long > AsUnsignedLongLong() const
void * LLDBSWIGPython_CastPyObjectToSBExecutionContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBFrameList(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBThread(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBData(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBSymbolContext(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBStream(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(PyObject *data)
void * LLDBSWIGPython_CastPyObjectToSBError(PyObject *data)
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::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
class LLDB_API SBMemoryRegionInfo
Definition SBDefines.h:88
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