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
18#include "../ScriptInterpreterPythonImpl.h"
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24
25ScriptedPythonInterface::ScriptedPythonInterface(
26 ScriptInterpreterPythonImpl &interpreter)
27 : ScriptedInterface(), m_interpreter(interpreter) {}
28
29template <>
31ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
32 python::PythonObject &p, Status &error) {
33 python::PythonList result_list(python::PyRefType::Borrowed, p.get());
34 return result_list.CreateStructuredArray();
35}
36
37template <>
39ScriptedPythonInterface::ExtractValueFromPythonObject<
40 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
41 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
42 return result_dict.CreateStructuredDictionary();
43}
44
45template <>
46Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
47 python::PythonObject &p, Status &error) {
48 if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
49 python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
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 <>
58Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
59 python::PythonObject &p, Status &error) {
60 if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
61 python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
62 return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
63 error = Status::FromErrorString(
64 "Couldn't cast lldb::SBEvent to lldb_private::Event.");
65
66 return nullptr;
67}
68
69template <>
71ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
72 python::PythonObject &p, Status &error) {
73 if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
74 python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
75 return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
76 error = Status::FromErrorString(
77 "Couldn't cast lldb::SBStream to lldb_private::Stream.");
78
79 return nullptr;
80}
81
82template <>
84ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
85 python::PythonObject &p, Status &error) {
86 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
87 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
88
89 if (!sb_data) {
90 error = Status::FromErrorStringWithFormat(
91 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
92 return nullptr;
93 }
94
95 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
96}
97
98template <>
100ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
101 python::PythonObject &p, Status &error) {
102 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
103 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
104
105 if (!sb_breakpoint) {
106 error = Status::FromErrorStringWithFormat(
107 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
108 return nullptr;
109 }
110
111 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
112}
113
114template <>
115lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
116 lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
117 lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
118 python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
119
120 if (!sb_attach_info) {
121 error = Status::FromErrorStringWithFormat(
122 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
123 return nullptr;
124 }
125
126 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
127}
128
129template <>
130lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
131 lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
132 lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
133 python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
134
135 if (!sb_launch_info) {
136 error = Status::FromErrorStringWithFormat(
137 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
138 return nullptr;
139 }
140
141 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
142}
143
144template <>
145std::optional<MemoryRegionInfo>
146ScriptedPythonInterface::ExtractValueFromPythonObject<
147 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
148
149 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
150 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
151 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
152
153 if (!sb_mem_reg_info) {
154 error = Status::FromErrorStringWithFormat(
155 "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
156 return {};
157 }
158
159 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
160}
161
162template <>
164ScriptedPythonInterface::ExtractValueFromPythonObject<
165 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
166
167 lldb::SBExecutionContext *sb_exe_ctx =
168 reinterpret_cast<lldb::SBExecutionContext *>(
169 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
170
171 if (!sb_exe_ctx) {
172 error = Status::FromErrorStringWithFormat(
173 "Couldn't cast lldb::SBExecutionContext to "
174 "lldb::ExecutionContextRefSP.");
175 return {};
176 }
177
178 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
179}
180
181#endif
static llvm::raw_ostream & error(Stream &strm)
An error handling class.
Definition: Status.h:118
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Array > ArraySP
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
Definition: lldb-forward.h:390
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:432
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:321
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:338
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
Definition: lldb-forward.h:391
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP
Definition: lldb-forward.h:350