xref: /llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPythonInterface.cpp (revision 1130e923e2d7fe046101bf639bc5ebcde194c005)
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::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 
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();
43 }
44 
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.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
52 
53   return {};
54 }
55 
56 template <>
57 Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
58     python::PythonObject &p, Status &error) {
59   if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
60           python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
61     return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
62   error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event.");
63 
64   return nullptr;
65 }
66 
67 template <>
68 lldb::StreamSP
69 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
70     python::PythonObject &p, Status &error) {
71   if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
72           python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
73     return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
74   error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream.");
75 
76   return nullptr;
77 }
78 
79 template <>
80 lldb::DataExtractorSP
81 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
82     python::PythonObject &p, Status &error) {
83   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
84       python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
85 
86   if (!sb_data) {
87     error.SetErrorString(
88         "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
89     return nullptr;
90   }
91 
92   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
93 }
94 
95 template <>
96 lldb::BreakpointSP
97 ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
98     python::PythonObject &p, Status &error) {
99   lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
100       python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
101 
102   if (!sb_breakpoint) {
103     error.SetErrorString(
104         "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
105     return nullptr;
106   }
107 
108   return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
109 }
110 
111 template <>
112 lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
113     lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
114   lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
115       python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
116 
117   if (!sb_attach_info) {
118     error.SetErrorString(
119         "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
120     return nullptr;
121   }
122 
123   return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
124 }
125 
126 template <>
127 lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
128     lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
129   lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
130       python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
131 
132   if (!sb_launch_info) {
133     error.SetErrorString(
134         "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
135     return nullptr;
136   }
137 
138   return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
139 }
140 
141 template <>
142 std::optional<MemoryRegionInfo>
143 ScriptedPythonInterface::ExtractValueFromPythonObject<
144     std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
145 
146   lldb::SBMemoryRegionInfo *sb_mem_reg_info =
147       reinterpret_cast<lldb::SBMemoryRegionInfo *>(
148           python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
149 
150   if (!sb_mem_reg_info) {
151     error.SetErrorString(
152         "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
153     return {};
154   }
155 
156   return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
157 }
158 
159 #endif
160