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" |
11 | #include "lldb/lldb-enumerations.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" |
19 | #include "ScriptedPythonInterface.h" |
20 | #include <optional> |
21 | |
22 | using namespace lldb; |
23 | using namespace lldb_private; |
24 | |
25 | ScriptedPythonInterface::ScriptedPythonInterface( |
26 | ScriptInterpreterPythonImpl &interpreter) |
27 | : ScriptedInterface(), m_interpreter(interpreter) {} |
28 | |
29 | template <> |
30 | StructuredData::ArraySP |
31 | ScriptedPythonInterface::<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 | |
37 | template <> |
38 | StructuredData::DictionarySP |
39 | ScriptedPythonInterface::< |
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 | |
45 | template <> |
46 | Status ScriptedPythonInterface::<Status>( |
47 | python::PythonObject &p, Status &error) { |
48 | if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>( |
49 | python::LLDBSWIGPython_CastPyObjectToSBError(data: p.get()))) |
50 | return m_interpreter.GetStatusFromSBError(error: *sb_error); |
51 | error = |
52 | Status::FromErrorString(str: "Couldn't cast lldb::SBError to lldb::Status." ); |
53 | |
54 | return {}; |
55 | } |
56 | |
57 | template <> |
58 | Event *ScriptedPythonInterface::<Event *>( |
59 | python::PythonObject &p, Status &error) { |
60 | if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>( |
61 | python::LLDBSWIGPython_CastPyObjectToSBEvent(data: p.get()))) |
62 | return m_interpreter.GetOpaqueTypeFromSBEvent(event: *sb_event); |
63 | error = Status::FromErrorString( |
64 | str: "Couldn't cast lldb::SBEvent to lldb_private::Event." ); |
65 | |
66 | return nullptr; |
67 | } |
68 | |
69 | template <> |
70 | lldb::StreamSP |
71 | ScriptedPythonInterface::<lldb::StreamSP>( |
72 | python::PythonObject &p, Status &error) { |
73 | if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>( |
74 | python::LLDBSWIGPython_CastPyObjectToSBStream(data: p.get()))) |
75 | return m_interpreter.GetOpaqueTypeFromSBStream(stream: *sb_stream); |
76 | error = Status::FromErrorString( |
77 | str: "Couldn't cast lldb::SBStream to lldb_private::Stream." ); |
78 | |
79 | return nullptr; |
80 | } |
81 | |
82 | template <> |
83 | lldb::DataExtractorSP |
84 | ScriptedPythonInterface::<lldb::DataExtractorSP>( |
85 | python::PythonObject &p, Status &error) { |
86 | lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>( |
87 | python::LLDBSWIGPython_CastPyObjectToSBData(data: p.get())); |
88 | |
89 | if (!sb_data) { |
90 | error = Status::FromErrorStringWithFormat( |
91 | format: "Couldn't cast lldb::SBData to lldb::DataExtractorSP." ); |
92 | return nullptr; |
93 | } |
94 | |
95 | return m_interpreter.GetDataExtractorFromSBData(data: *sb_data); |
96 | } |
97 | |
98 | template <> |
99 | lldb::BreakpointSP |
100 | ScriptedPythonInterface::<lldb::BreakpointSP>( |
101 | python::PythonObject &p, Status &error) { |
102 | lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>( |
103 | python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(data: p.get())); |
104 | |
105 | if (!sb_breakpoint) { |
106 | error = Status::FromErrorStringWithFormat( |
107 | format: "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP." ); |
108 | return nullptr; |
109 | } |
110 | |
111 | return m_interpreter.GetOpaqueTypeFromSBBreakpoint(breakpoint: *sb_breakpoint); |
112 | } |
113 | |
114 | template <> |
115 | lldb::ProcessAttachInfoSP ScriptedPythonInterface::< |
116 | lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) { |
117 | lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>( |
118 | python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(data: p.get())); |
119 | |
120 | if (!sb_attach_info) { |
121 | error = Status::FromErrorStringWithFormat( |
122 | format: "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP." ); |
123 | return nullptr; |
124 | } |
125 | |
126 | return m_interpreter.GetOpaqueTypeFromSBAttachInfo(attach_info: *sb_attach_info); |
127 | } |
128 | |
129 | template <> |
130 | lldb::ProcessLaunchInfoSP ScriptedPythonInterface::< |
131 | lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) { |
132 | lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>( |
133 | python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(data: p.get())); |
134 | |
135 | if (!sb_launch_info) { |
136 | error = Status::FromErrorStringWithFormat( |
137 | format: "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP." ); |
138 | return nullptr; |
139 | } |
140 | |
141 | return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(launch_info: *sb_launch_info); |
142 | } |
143 | |
144 | template <> |
145 | std::optional<MemoryRegionInfo> |
146 | ScriptedPythonInterface::< |
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(data: p.get())); |
152 | |
153 | if (!sb_mem_reg_info) { |
154 | error = Status::FromErrorStringWithFormat( |
155 | format: "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP." ); |
156 | return {}; |
157 | } |
158 | |
159 | return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(mem_region: *sb_mem_reg_info); |
160 | } |
161 | |
162 | template <> |
163 | lldb::ExecutionContextRefSP |
164 | ScriptedPythonInterface::< |
165 | lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) { |
166 | |
167 | lldb::SBExecutionContext *sb_exe_ctx = |
168 | reinterpret_cast<lldb::SBExecutionContext *>( |
169 | python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(data: p.get())); |
170 | |
171 | if (!sb_exe_ctx) { |
172 | error = Status::FromErrorStringWithFormat( |
173 | format: "Couldn't cast lldb::SBExecutionContext to " |
174 | "lldb::ExecutionContextRefSP." ); |
175 | return {}; |
176 | } |
177 | |
178 | return m_interpreter.GetOpaqueTypeFromSBExecutionContext(exe_ctx: *sb_exe_ctx); |
179 | } |
180 | |
181 | #endif |
182 | |