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
22using namespace lldb;
23using namespace lldb_private;
24
25ScriptedPythonInterface::ScriptedPythonInterface(
26    ScriptInterpreterPythonImpl &interpreter)
27    : ScriptedInterface(), m_interpreter(interpreter) {}
28
29template <>
30StructuredData::ArraySP
31ScriptedPythonInterface::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
37template <>
38StructuredData::DictionarySP
39ScriptedPythonInterface::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
45template <>
46Status 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  else
52    error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
53
54  return {};
55}
56
57template <>
58lldb::DataExtractorSP
59ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::DataExtractorSP>(
60    python::PythonObject &p, Status &error) {
61  lldb::SBData *sb_data = reinterpret_cast<lldb::SBData *>(
62      python::LLDBSWIGPython_CastPyObjectToSBData(p.get()));
63
64  if (!sb_data) {
65    error.SetErrorString(
66        "Couldn't cast lldb::SBData to lldb::DataExtractorSP.");
67    return nullptr;
68  }
69
70  return m_interpreter.GetDataExtractorFromSBData(*sb_data);
71}
72
73template <>
74lldb::BreakpointSP
75ScriptedPythonInterface::ExtractValueFromPythonObject<lldb::BreakpointSP>(
76    python::PythonObject &p, Status &error) {
77  lldb::SBBreakpoint *sb_breakpoint = reinterpret_cast<lldb::SBBreakpoint *>(
78      python::LLDBSWIGPython_CastPyObjectToSBBreakpoint(p.get()));
79
80  if (!sb_breakpoint) {
81    error.SetErrorString(
82        "Couldn't cast lldb::SBBreakpoint to lldb::BreakpointSP.");
83    return nullptr;
84  }
85
86  return m_interpreter.GetOpaqueTypeFromSBBreakpoint(*sb_breakpoint);
87}
88
89template <>
90lldb::ProcessAttachInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
91    lldb::ProcessAttachInfoSP>(python::PythonObject &p, Status &error) {
92  lldb::SBAttachInfo *sb_attach_info = reinterpret_cast<lldb::SBAttachInfo *>(
93      python::LLDBSWIGPython_CastPyObjectToSBAttachInfo(p.get()));
94
95  if (!sb_attach_info) {
96    error.SetErrorString(
97        "Couldn't cast lldb::SBAttachInfo to lldb::ProcessAttachInfoSP.");
98    return nullptr;
99  }
100
101  return m_interpreter.GetOpaqueTypeFromSBAttachInfo(*sb_attach_info);
102}
103
104template <>
105lldb::ProcessLaunchInfoSP ScriptedPythonInterface::ExtractValueFromPythonObject<
106    lldb::ProcessLaunchInfoSP>(python::PythonObject &p, Status &error) {
107  lldb::SBLaunchInfo *sb_launch_info = reinterpret_cast<lldb::SBLaunchInfo *>(
108      python::LLDBSWIGPython_CastPyObjectToSBLaunchInfo(p.get()));
109
110  if (!sb_launch_info) {
111    error.SetErrorString(
112        "Couldn't cast lldb::SBLaunchInfo to lldb::ProcessLaunchInfoSP.");
113    return nullptr;
114  }
115
116  return m_interpreter.GetOpaqueTypeFromSBLaunchInfo(*sb_launch_info);
117}
118
119template <>
120std::optional<MemoryRegionInfo>
121ScriptedPythonInterface::ExtractValueFromPythonObject<
122    std::optional<MemoryRegionInfo>>(python::PythonObject &p, Status &error) {
123
124  lldb::SBMemoryRegionInfo *sb_mem_reg_info =
125      reinterpret_cast<lldb::SBMemoryRegionInfo *>(
126          python::LLDBSWIGPython_CastPyObjectToSBMemoryRegionInfo(p.get()));
127
128  if (!sb_mem_reg_info) {
129    error.SetErrorString(
130        "Couldn't cast lldb::SBMemoryRegionInfo to lldb::MemoryRegionInfoSP.");
131    return {};
132  }
133
134  return m_interpreter.GetOpaqueTypeFromSBMemoryRegionInfo(*sb_mem_reg_info);
135}
136
137#endif
138