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<SymbolContext>(
99 python::PythonObject &p, Status &error) {
100 if (lldb::SBSymbolContext *sb_symbol_context =
101 reinterpret_cast<lldb::SBSymbolContext *>(
102 python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(p.get())))
103 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
105 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
106
107 return {};
108}
109
110template <>
112ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
113 python::PythonObject &p, Status &error) {
114 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
115 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
116
117 if (!sb_data) {
119 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
120 return nullptr;
121 }
122
123 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
124}
125
126template <>
128ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
129 python::PythonObject &p, Status &error) {
130 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
131 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
132
133 if (!sb_breakpoint) {
135 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
136 return nullptr;
137 }
138
139 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
140}
141
142template <>
144ScriptedPythonInterface::ExtractValueFromPythonObject<
145 lldb::BreakpointLocationSP>(python::PythonObject &p, Status &error) {
146 lldb::SBBreakpointLocation *sb_break_loc =
147 reinterpret_cast<lldb::SBBreakpointLocation *>(
148 python::LLDBSWIGPython_CastPyObjectToSBBreakpointLocation(p.get()));
149
150 if (!sb_break_loc) {
152 "Couldn't cast lldb::SBBreakpointLocation to "
153 "lldb::BreakpointLocationSP.");
154 return nullptr;
155 }
156
157 return m_interpreter.GetOpaqueTypeFromSBBreakpointLocation(*sb_break_loc);
158}
159
160template <>
161lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
162 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
163 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
164 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
165
166 if (!sb_attach_info) {
168 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
169 return nullptr;
170 }
171
172 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
173}
174
175template <>
176lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
177 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
178 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
179 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
180
181 if (!sb_launch_info) {
183 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
184 return nullptr;
185 }
186
187 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
188}
189
190template <>
191std::optional<MemoryRegionInfo>
192ScriptedPythonInterface::ExtractValueFromPythonObject<
193 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
194
195 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
196 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
197 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
198
199 if (!sb_mem_reg_info) {
201 "Couldn't cast lldb::SBMemoryRegionInfo to "
202 "lldb_private::MemoryRegionInfo.");
203 return {};
204 }
205
206 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
207}
208
209template <>
211ScriptedPythonInterface::ExtractValueFromPythonObject<
212 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
213
214 lldb::SBExecutionContext *sb_exe_ctx =
215 reinterpret_cast<lldb::SBExecutionContext *>(
216 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
217
218 if (!sb_exe_ctx) {
220 "Couldn't cast lldb::SBExecutionContext to "
221 "lldb::ExecutionContextRefSP.");
222 return {};
223 }
224
225 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
226}
227
228template <>
230ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DescriptionLevel>(
231 python::PythonObject &p, Status &error) {
233 llvm::Expected<unsigned long long> unsigned_or_err = p.AsUnsignedLongLong();
234 if (!unsigned_or_err) {
235 error = (Status::FromError(unsigned_or_err.takeError()));
236 return ret_val;
237 }
238 unsigned long long unsigned_val = *unsigned_or_err;
240 error = Status("value too large for lldb::DescriptionLevel.");
241 return ret_val;
242 }
243 return static_cast<lldb::DescriptionLevel>(unsigned_val);
244}
245
246#endif
static llvm::raw_ostream & error(Stream &strm)
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::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::ExecutionContextRef > ExecutionContextRefSP