[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / lldb / source / Plugins / ScriptInterpreter / Python / Interfaces / ScriptedPythonInterface.cpp
blobcf11c06e8a95d4e4819f431547e68ce2b82112e7
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 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/Config.h"
10 #include "lldb/Utility/Log.h"
11 #include "lldb/lldb-enumerations.h"
13 #if LLDB_ENABLE_PYTHON
15 // LLDB Python header must be included first
16 #include "../lldb-python.h"
18 #include "../ScriptInterpreterPythonImpl.h"
19 #include "ScriptedPythonInterface.h"
20 #include <optional>
22 using namespace lldb;
23 using namespace lldb_private;
25 ScriptedPythonInterface::ScriptedPythonInterface(
26 ScriptInterpreterPythonImpl &interpreter)
27 : ScriptedInterface(), m_interpreter(interpreter) {}
29 template <>
30 StructuredData::ArraySP
31 ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
32 python::PythonObject &p, Status &error) {
33 python::PythonList result_list(python::PyRefType::Borrowed, p.get());
34 return result_list.CreateStructuredArray();
37 template <>
38 StructuredData::DictionarySP
39 ScriptedPythonInterface::ExtractValueFromPythonObject<
40 StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
41 python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
42 return result_dict.CreateStructuredDictionary();
45 template <>
46 Status 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.");
54 return {};
57 template <>
58 Event *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.");
66 return nullptr;
69 template <>
70 lldb::StreamSP
71 ScriptedPythonInterface::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.");
79 return nullptr;
82 template <>
83 lldb::DataExtractorSP
84 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
85 python::PythonObject &p, Status &error) {
86 lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
87 python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
89 if (!sb_data) {
90 error = Status::FromErrorStringWithFormat(
91 "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
92 return nullptr;
95 return m_interpreter.GetDataExtractorFromSBData(*sb_data);
98 template <>
99 lldb::BreakpointSP
100 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
101 python::PythonObject &p, Status &error) {
102 lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
103 python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
105 if (!sb_breakpoint) {
106 error = Status::FromErrorStringWithFormat(
107 "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
108 return nullptr;
111 return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
114 template <>
115 lldb::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()));
120 if (!sb_attach_info) {
121 error = Status::FromErrorStringWithFormat(
122 "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
123 return nullptr;
126 return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
129 template <>
130 lldb::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()));
135 if (!sb_launch_info) {
136 error = Status::FromErrorStringWithFormat(
137 "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
138 return nullptr;
141 return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
144 template <>
145 std::optional<MemoryRegionInfo>
146 ScriptedPythonInterface::ExtractValueFromPythonObject<
147 std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
149 lldb::SBMemoryRegionInfo *sb_mem_reg_info =
150 reinterpret_cast<lldb::SBMemoryRegionInfo *>(
151 python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
153 if (!sb_mem_reg_info) {
154 error = Status::FromErrorStringWithFormat(
155 "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
156 return {};
159 return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
162 template <>
163 lldb::ExecutionContextRefSP
164 ScriptedPythonInterface::ExtractValueFromPythonObject<
165 lldb::ExecutionContextRefSP>(python::PythonObject &p, Status &error) {
167 lldb::SBExecutionContext *sb_exe_ctx =
168 reinterpret_cast<lldb::SBExecutionContext *>(
169 python::LLDBSWIGPython_CastPyObjectToSBExecutionContext(p.get()));
171 if (!sb_exe_ctx) {
172 error = Status::FromErrorStringWithFormat(
173 "Couldn't cast lldb::SBExecutionContext to "
174 "lldb::ExecutionContextRefSP.");
175 return {};
178 return m_interpreter.GetOpaqueTypeFromSBExecutionContext(*sb_exe_ctx);
181 #endif