LLDB mainline
ScriptedPythonInterface.cpp
Go to the documentation of this file.
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"
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"
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24
25ScriptedPythonInterface::ScriptedPythonInterface(
26 ScriptInterpreterPythonImpl &interpreter)
27 : ScriptedInterface(), m_interpreter(interpreter) {}
28
29template <>
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 <>
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 error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
52
53 return {};
54}
55
56template <>
57Event *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
67template <>
69ScriptedPythonInterface::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
79template <>
81ScriptedPythonInterface::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
95template <>
97ScriptedPythonInterface::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
111template <>
112lldb::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
126template <>
127lldb::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
141template <>
142std::optional<MemoryRegionInfo>
143ScriptedPythonInterface::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
static llvm::raw_ostream & error(Stream &strm)
An error handling class.
Definition: Status.h:44
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Array > ArraySP
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
Definition: lldb-forward.h:388
std::shared_ptr< lldb_private::Stream > StreamSP
Definition: lldb-forward.h:428
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:319
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
Definition: lldb-forward.h:389