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 else
52 error.SetErrorString("Couldn't cast lldb::SBError to lldb::Status.");
53
54 return {};
55}
56
57template <>
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 <>
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
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: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
Definition: lldb-forward.h:382
std::shared_ptr< lldb_private::Breakpoint > BreakpointSP
Definition: lldb-forward.h:313
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:330
std::shared_ptr< lldb_private::ProcessLaunchInfo > ProcessLaunchInfoSP
Definition: lldb-forward.h:383