1*c9157d92SDimitry Andric //===-- ScriptedPythonInterface.cpp ---------------------------------------===//
2*c9157d92SDimitry Andric //
3*c9157d92SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c9157d92SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*c9157d92SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c9157d92SDimitry Andric //
7*c9157d92SDimitry Andric //===----------------------------------------------------------------------===//
8*c9157d92SDimitry Andric 
9*c9157d92SDimitry Andric #include "lldb/Host/Config.h"
10*c9157d92SDimitry Andric #include "lldb/Utility/Log.h"
11*c9157d92SDimitry Andric #include "lldb/lldb-enumerations.h"
12*c9157d92SDimitry Andric 
13*c9157d92SDimitry Andric #if LLDB_ENABLE_PYTHON
14*c9157d92SDimitry Andric 
15*c9157d92SDimitry Andric // LLDB Python header must be included first
16*c9157d92SDimitry Andric #include "../lldb-python.h"
17*c9157d92SDimitry Andric 
18*c9157d92SDimitry Andric #include "../ScriptInterpreterPythonImpl.h"
19*c9157d92SDimitry Andric #include "ScriptedPythonInterface.h"
20*c9157d92SDimitry Andric #include <optional>
21*c9157d92SDimitry Andric 
22*c9157d92SDimitry Andric using namespace lldb;
23*c9157d92SDimitry Andric using namespace lldb_private;
24*c9157d92SDimitry Andric 
ScriptedPythonInterface(ScriptInterpreterPythonImpl & interpreter)25*c9157d92SDimitry Andric ScriptedPythonInterface::ScriptedPythonInterface(
26*c9157d92SDimitry Andric     ScriptInterpreterPythonImpl &interpreter)
27*c9157d92SDimitry Andric     : ScriptedInterface(), m_interpreter(interpreter) {}
28*c9157d92SDimitry Andric 
29*c9157d92SDimitry Andric template <>
30*c9157d92SDimitry Andric StructuredData::ArraySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)31*c9157d92SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
32*c9157d92SDimitry Andric     python::PythonObject &p, Status &error) {
33*c9157d92SDimitry Andric   python::PythonList result_list(python::PyRefType::Borrowed, p.get());
34*c9157d92SDimitry Andric   return result_list.CreateStructuredArray();
35*c9157d92SDimitry Andric }
36*c9157d92SDimitry Andric 
37*c9157d92SDimitry Andric template <>
38*c9157d92SDimitry Andric StructuredData::DictionarySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)39*c9157d92SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<
40*c9157d92SDimitry Andric     StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
41*c9157d92SDimitry Andric   python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
42*c9157d92SDimitry Andric   return result_dict.CreateStructuredDictionary();
43*c9157d92SDimitry Andric }
44*c9157d92SDimitry Andric 
45*c9157d92SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)46*c9157d92SDimitry Andric Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
47*c9157d92SDimitry Andric     python::PythonObject &p, Status &error) {
48*c9157d92SDimitry Andric   if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
49*c9157d92SDimitry Andric           python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
50*c9157d92SDimitry Andric     return m_interpreter.GetStatusFromSBError(*sb_error);
51*c9157d92SDimitry Andric   else
52*c9157d92SDimitry Andric     error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
53*c9157d92SDimitry Andric 
54*c9157d92SDimitry Andric   return {};
55*c9157d92SDimitry Andric }
56*c9157d92SDimitry Andric 
57*c9157d92SDimitry Andric template <>
58*c9157d92SDimitry Andric lldb::DataExtractorSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)59*c9157d92SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
60*c9157d92SDimitry Andric     python::PythonObject &p, Status &error) {
61*c9157d92SDimitry Andric   lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
62*c9157d92SDimitry Andric       python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
63*c9157d92SDimitry Andric 
64*c9157d92SDimitry Andric   if (!sb_data) {
65*c9157d92SDimitry Andric     error.SetErrorString(
66*c9157d92SDimitry Andric         "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
67*c9157d92SDimitry Andric     return nullptr;
68*c9157d92SDimitry Andric   }
69*c9157d92SDimitry Andric 
70*c9157d92SDimitry Andric   return m_interpreter.GetDataExtractorFromSBData(*sb_data);
71*c9157d92SDimitry Andric }
72*c9157d92SDimitry Andric 
73*c9157d92SDimitry Andric template <>
74*c9157d92SDimitry Andric lldb::BreakpointSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)75*c9157d92SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
76*c9157d92SDimitry Andric     python::PythonObject &p, Status &error) {
77*c9157d92SDimitry Andric   lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
78*c9157d92SDimitry Andric       python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
79*c9157d92SDimitry Andric 
80*c9157d92SDimitry Andric   if (!sb_breakpoint) {
81*c9157d92SDimitry Andric     error.SetErrorString(
82*c9157d92SDimitry Andric         "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
83*c9157d92SDimitry Andric     return nullptr;
84*c9157d92SDimitry Andric   }
85*c9157d92SDimitry Andric 
86*c9157d92SDimitry Andric   return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
87*c9157d92SDimitry Andric }
88*c9157d92SDimitry Andric 
89*c9157d92SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)90*c9157d92SDimitry Andric lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
91*c9157d92SDimitry Andric     lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
92*c9157d92SDimitry Andric   lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
93*c9157d92SDimitry Andric       python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
94*c9157d92SDimitry Andric 
95*c9157d92SDimitry Andric   if (!sb_attach_info) {
96*c9157d92SDimitry Andric     error.SetErrorString(
97*c9157d92SDimitry Andric         "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
98*c9157d92SDimitry Andric     return nullptr;
99*c9157d92SDimitry Andric   }
100*c9157d92SDimitry Andric 
101*c9157d92SDimitry Andric   return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
102*c9157d92SDimitry Andric }
103*c9157d92SDimitry Andric 
104*c9157d92SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)105*c9157d92SDimitry Andric lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
106*c9157d92SDimitry Andric     lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
107*c9157d92SDimitry Andric   lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
108*c9157d92SDimitry Andric       python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
109*c9157d92SDimitry Andric 
110*c9157d92SDimitry Andric   if (!sb_launch_info) {
111*c9157d92SDimitry Andric     error.SetErrorString(
112*c9157d92SDimitry Andric         "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
113*c9157d92SDimitry Andric     return nullptr;
114*c9157d92SDimitry Andric   }
115*c9157d92SDimitry Andric 
116*c9157d92SDimitry Andric   return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
117*c9157d92SDimitry Andric }
118*c9157d92SDimitry Andric 
119*c9157d92SDimitry Andric template <>
120*c9157d92SDimitry Andric std::optional<MemoryRegionInfo>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)121*c9157d92SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<
122*c9157d92SDimitry Andric     std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
123*c9157d92SDimitry Andric 
124*c9157d92SDimitry Andric   lldb::SBMemoryRegionInfo *sb_mem_reg_info =
125*c9157d92SDimitry Andric       reinterpret_cast<lldb::SBMemoryRegionInfo *>(
126*c9157d92SDimitry Andric           python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
127*c9157d92SDimitry Andric 
128*c9157d92SDimitry Andric   if (!sb_mem_reg_info) {
129*c9157d92SDimitry Andric     error.SetErrorString(
130*c9157d92SDimitry Andric         "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
131*c9157d92SDimitry Andric     return {};
132*c9157d92SDimitry Andric   }
133*c9157d92SDimitry Andric 
134*c9157d92SDimitry Andric   return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
135*c9157d92SDimitry Andric }
136*c9157d92SDimitry Andric 
137*c9157d92SDimitry Andric #endif
138