1c0981da4SDimitry Andric //===-- ScriptedPythonInterface.cpp ---------------------------------------===//
2c0981da4SDimitry Andric //
3c0981da4SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c0981da4SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5c0981da4SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c0981da4SDimitry Andric //
7c0981da4SDimitry Andric //===----------------------------------------------------------------------===//
8c0981da4SDimitry Andric
9c0981da4SDimitry Andric #include "lldb/Host/Config.h"
10c0981da4SDimitry Andric #include "lldb/Utility/Log.h"
11c0981da4SDimitry Andric #include "lldb/lldb-enumerations.h"
12c0981da4SDimitry Andric
13c0981da4SDimitry Andric #if LLDB_ENABLE_PYTHON
14c0981da4SDimitry Andric
15c0981da4SDimitry Andric // LLDB Python header must be included first
16b1c73532SDimitry Andric #include "../lldb-python.h"
17c0981da4SDimitry Andric
18b1c73532SDimitry Andric #include "../ScriptInterpreterPythonImpl.h"
19c0981da4SDimitry Andric #include "ScriptedPythonInterface.h"
20e3b55780SDimitry Andric #include <optional>
21c0981da4SDimitry Andric
22c0981da4SDimitry Andric using namespace lldb;
23c0981da4SDimitry Andric using namespace lldb_private;
24c0981da4SDimitry Andric
ScriptedPythonInterface(ScriptInterpreterPythonImpl & interpreter)25c0981da4SDimitry Andric ScriptedPythonInterface::ScriptedPythonInterface(
26c0981da4SDimitry Andric ScriptInterpreterPythonImpl &interpreter)
27c0981da4SDimitry Andric : ScriptedInterface(), m_interpreter(interpreter) {}
28c0981da4SDimitry Andric
29c0981da4SDimitry Andric template <>
30145449b1SDimitry Andric StructuredData::ArraySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)31145449b1SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<StructuredData::ArraySP>(
32145449b1SDimitry Andric python::PythonObject &p, Status &error) {
33145449b1SDimitry Andric python::PythonList result_list(python::PyRefType::Borrowed, p.get());
34145449b1SDimitry Andric return result_list.CreateStructuredArray();
35145449b1SDimitry Andric }
36145449b1SDimitry Andric
37145449b1SDimitry Andric template <>
38c0981da4SDimitry Andric StructuredData::DictionarySP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)39c0981da4SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<
40c0981da4SDimitry Andric StructuredData::DictionarySP>(python::PythonObject &p, Status &error) {
41c0981da4SDimitry Andric python::PythonDictionary result_dict(python::PyRefType::Borrowed, p.get());
42c0981da4SDimitry Andric return result_dict.CreateStructuredDictionary();
43c0981da4SDimitry Andric }
44c0981da4SDimitry Andric
45c0981da4SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)46c0981da4SDimitry Andric Status ScriptedPythonInterface::ExtractValueFromPythonObject<Status>(
47c0981da4SDimitry Andric python::PythonObject &p, Status &error) {
48c0981da4SDimitry Andric if (lldb::SBError *sb_error = reinterpret_cast<lldb::SBError *>(
497fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBError(p.get())))
50e3b55780SDimitry Andric return m_interpreter.GetStatusFromSBError(*sb_error);
51c0981da4SDimitry Andric error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
52c0981da4SDimitry Andric
53e3b55780SDimitry Andric return {};
54c0981da4SDimitry Andric }
55c0981da4SDimitry Andric
56c0981da4SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)57ac9a064cSDimitry Andric Event *ScriptedPythonInterface::ExtractValueFromPythonObject<Event *>(
58ac9a064cSDimitry Andric python::PythonObject &p, Status &error) {
59ac9a064cSDimitry Andric if (lldb::SBEvent *sb_event = reinterpret_cast<lldb::SBEvent *>(
60ac9a064cSDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBEvent(p.get())))
61ac9a064cSDimitry Andric return m_interpreter.GetOpaqueTypeFromSBEvent(*sb_event);
62ac9a064cSDimitry Andric error.SetErrorString("Couldn't cast lldb::SBEvent to lldb_private::Event.");
63ac9a064cSDimitry Andric
64ac9a064cSDimitry Andric return nullptr;
65ac9a064cSDimitry Andric }
66ac9a064cSDimitry Andric
67ac9a064cSDimitry Andric template <>
68ac9a064cSDimitry Andric lldb::StreamSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)69ac9a064cSDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::StreamSP>(
70ac9a064cSDimitry Andric python::PythonObject &p, Status &error) {
71ac9a064cSDimitry Andric if (lldb::SBStream *sb_stream = reinterpret_cast<lldb::SBStream *>(
72ac9a064cSDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBStream(p.get())))
73ac9a064cSDimitry Andric return m_interpreter.GetOpaqueTypeFromSBStream(*sb_stream);
74ac9a064cSDimitry Andric error.SetErrorString("Couldn't cast lldb::SBStream to lldb_private::Stream.");
75ac9a064cSDimitry Andric
76ac9a064cSDimitry Andric return nullptr;
77ac9a064cSDimitry Andric }
78ac9a064cSDimitry Andric
79ac9a064cSDimitry Andric template <>
80c0981da4SDimitry Andric lldb::DataExtractorSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)81c0981da4SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
82c0981da4SDimitry Andric python::PythonObject &p, Status &error) {
83c0981da4SDimitry Andric lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
847fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
85c0981da4SDimitry Andric
86c0981da4SDimitry Andric if (!sb_data) {
87c0981da4SDimitry Andric error.SetErrorString(
88c0981da4SDimitry Andric "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
89c0981da4SDimitry Andric return nullptr;
90c0981da4SDimitry Andric }
91c0981da4SDimitry Andric
92c0981da4SDimitry Andric return m_interpreter.GetDataExtractorFromSBData(*sb_data);
93c0981da4SDimitry Andric }
94c0981da4SDimitry Andric
95c0981da4SDimitry Andric template <>
967fa27ce4SDimitry Andric lldb::BreakpointSP
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)977fa27ce4SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
987fa27ce4SDimitry Andric python::PythonObject &p, Status &error) {
997fa27ce4SDimitry Andric lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
1007fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
1017fa27ce4SDimitry Andric
1027fa27ce4SDimitry Andric if (!sb_breakpoint) {
1037fa27ce4SDimitry Andric error.SetErrorString(
1047fa27ce4SDimitry Andric "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
1057fa27ce4SDimitry Andric return nullptr;
1067fa27ce4SDimitry Andric }
1077fa27ce4SDimitry Andric
1087fa27ce4SDimitry Andric return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
1097fa27ce4SDimitry Andric }
1107fa27ce4SDimitry Andric
1117fa27ce4SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)1127fa27ce4SDimitry Andric lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
1137fa27ce4SDimitry Andric lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
1147fa27ce4SDimitry Andric lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
1157fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
1167fa27ce4SDimitry Andric
1177fa27ce4SDimitry Andric if (!sb_attach_info) {
1187fa27ce4SDimitry Andric error.SetErrorString(
1197fa27ce4SDimitry Andric "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
1207fa27ce4SDimitry Andric return nullptr;
1217fa27ce4SDimitry Andric }
1227fa27ce4SDimitry Andric
1237fa27ce4SDimitry Andric return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
1247fa27ce4SDimitry Andric }
1257fa27ce4SDimitry Andric
1267fa27ce4SDimitry Andric template <>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)1277fa27ce4SDimitry Andric lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
1287fa27ce4SDimitry Andric lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
1297fa27ce4SDimitry Andric lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
1307fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
1317fa27ce4SDimitry Andric
1327fa27ce4SDimitry Andric if (!sb_launch_info) {
1337fa27ce4SDimitry Andric error.SetErrorString(
1347fa27ce4SDimitry Andric "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
1357fa27ce4SDimitry Andric return nullptr;
1367fa27ce4SDimitry Andric }
1377fa27ce4SDimitry Andric
1387fa27ce4SDimitry Andric return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
1397fa27ce4SDimitry Andric }
1407fa27ce4SDimitry Andric
1417fa27ce4SDimitry Andric template <>
142e3b55780SDimitry Andric std::optional<MemoryRegionInfo>
ExtractValueFromPythonObject(python::PythonObject & p,Status & error)143c0981da4SDimitry Andric ScriptedPythonInterface::ExtractValueFromPythonObject<
144e3b55780SDimitry Andric std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
145c0981da4SDimitry Andric
146c0981da4SDimitry Andric lldb::SBMemoryRegionInfo *sb_mem_reg_info =
147c0981da4SDimitry Andric reinterpret_cast<lldb::SBMemoryRegionInfo *>(
1487fa27ce4SDimitry Andric python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
149c0981da4SDimitry Andric
150c0981da4SDimitry Andric if (!sb_mem_reg_info) {
151c0981da4SDimitry Andric error.SetErrorString(
152c0981da4SDimitry Andric "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
153c0981da4SDimitry Andric return {};
154c0981da4SDimitry Andric }
155c0981da4SDimitry Andric
156c0981da4SDimitry Andric return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
157c0981da4SDimitry Andric }
158c0981da4SDimitry Andric
159c0981da4SDimitry Andric #endif
160