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<SymbolContext>(
86 python::PythonObject &p, Status &error) {
87 if (lldb::SBSymbolContext *sb_symbol_context =
88 reinterpret_cast<lldb::SBSymbolContext *>(
89 python::LLDBSWIGPython_CastPyObjectToSBSymbolContext(p.get())))
90 return m_interpreter.GetOpaqueTypeFromSBSymbolContext(*sb_symbol_context);
92 "Couldn't cast lldb::SBSymbolContext to lldb_private::SymbolContext.");
93
94 return {};
95}
96
97template <>
99ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
100 python::PythonObject &p, Status &error) {
101 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
102 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
103
104 if (!sb_data) {
106 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
107 return nullptr;
108 }
109
110 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
111}
112
113template <>
115ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
116 python::PythonObject &p, Status &error) {
117 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
118 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
119
120 if (!sb_breakpoint) {
122 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
123 return nullptr;
124 }
125
126 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
127}
128
129template <>
130lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
131 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
132 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
133 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
134
135 if (!sb_attach_info) {
137 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
138 return nullptr;
139 }
140
141 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
142}
143
144template <>
145lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
146 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
147 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
148 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
149
150 if (!sb_launch_info) {
152 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
153 return nullptr;
154 }
155
156 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
157}
158
159template <>
160std::optional<MemoryRegionInfo>
161ScriptedPythonInterface::ExtractValueFromPythonObject<
162 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
163
164 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
165 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
166 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
167
168 if (!sb_mem_reg_info) {
170 "Couldn't cast lldb::SBMemoryRegionInfo to "
171 "lldb_private::MemoryRegionInfo.");
172 return {};
173 }
174
175 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
176}
177
178template <>
180ScriptedPythonInterface::ExtractValueFromPythonObject<
181 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
182
183 lldb::SBExecutionContext *sb_exe_ctx =
184 reinterpret_cast<lldb::SBExecutionContext *>(
185 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
186
187 if (!sb_exe_ctx) {
189 "Couldn't cast lldb::SBExecutionContext to "
190 "lldb::ExecutionContextRefSP.");
191 return {};
192 }
193
194 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
195}
196
197#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
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::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