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