xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp (revision 0d4f9484bcd51d1ed07723c7b762e0f33d9f448f)
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 #if LLDB_ENABLE_PYTHON
10 
11 // clang-format off
12 // LLDB Python header must be included first
13 #include "../../lldb-python.h"
14 //clang-format on
15 #endif
16 
17 #include "lldb/Host/Config.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/lldb-enumerations.h"
20 
21 #if LLDB_ENABLE_PYTHON
22 
23 #include "../ScriptInterpreterPythonImpl.h"
24 #include "ScriptedPythonInterface.h"
25 #include <optional>
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 ScriptedPythonInterface::ScriptedPythonInterface(
31     ScriptInterpreterPythonImpl &interpreter)
32     : ScriptedInterface(), m_interpreter(interpreter) {}
33 
34 template <>
35 StructuredData::ArraySP
36 ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
37     python::PythonObject &p, Status &error) {
38   python::PythonList result_list(python::PyRefType::Borrowed, p.get());
39   return result_list.CreateStructuredArray();
40 }
41 
42 template <>
43 StructuredData::DictionarySP
44 ScriptedPythonInterface::ExtractValueFromPythonObject<
45     StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
46   python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
47   return result_dict.CreateStructuredDictionary();
48 }
49 
50 template <>
51 Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
52     python::PythonObject &p, Status &error) {
53   if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
54           python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
55     return m_interpreter.GetStatusFromSBError(*sb_error);
56   error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
57 
58   return {};
59 }
60 
61 template <>
62 Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
63     python::PythonObject &p, Status &error) {
64   if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
65           python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
66     return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
67   error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event.");
68 
69   return nullptr;
70 }
71 
72 template <>
73 lldb::StreamSP
74 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
75     python::PythonObject &p, Status &error) {
76   if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
77           python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
78     return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
79   error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream.");
80 
81   return nullptr;
82 }
83 
84 template <>
85 lldb::DataExtractorSP
86 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
87     python::PythonObject &p, Status &error) {
88   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
89       python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
90 
91   if (!sb_data) {
92     error.SetErrorString(
93         "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
94     return nullptr;
95   }
96 
97   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
98 }
99 
100 template <>
101 lldb::BreakpointSP
102 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
103     python::PythonObject &p, Status &error) {
104   lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
105       python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
106 
107   if (!sb_breakpoint) {
108     error.SetErrorString(
109         "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
110     return nullptr;
111   }
112 
113   return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
114 }
115 
116 template <>
117 lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
118     lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
119   lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
120       python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
121 
122   if (!sb_attach_info) {
123     error.SetErrorString(
124         "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
125     return nullptr;
126   }
127 
128   return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
129 }
130 
131 template <>
132 lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
133     lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
134   lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
135       python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
136 
137   if (!sb_launch_info) {
138     error.SetErrorString(
139         "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
140     return nullptr;
141   }
142 
143   return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
144 }
145 
146 template <>
147 std::optional<MemoryRegionInfo>
148 ScriptedPythonInterface::ExtractValueFromPythonObject<
149     std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
150 
151   lldb::SBMemoryRegionInfo *sb_mem_reg_info =
152       reinterpret_cast<lldb::SBMemoryRegionInfo *>(
153           python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
154 
155   if (!sb_mem_reg_info) {
156     error.SetErrorString(
157         "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
158     return {};
159   }
160 
161   return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
162 }
163 
164 #endif
165