1*c9157d92SDimitry Andric //===-- ScriptedProcessPythonInterface.cpp --------------------------------===//
2*c9157d92SDimitry Andric //
3*c9157d92SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c9157d92SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*c9157d92SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c9157d92SDimitry Andric //
7*c9157d92SDimitry Andric //===----------------------------------------------------------------------===//
8*c9157d92SDimitry Andric
9*c9157d92SDimitry Andric #include "lldb/Host/Config.h"
10*c9157d92SDimitry Andric #if LLDB_ENABLE_PYTHON
11*c9157d92SDimitry Andric // LLDB Python header must be included first
12*c9157d92SDimitry Andric #include "../lldb-python.h"
13*c9157d92SDimitry Andric #endif
14*c9157d92SDimitry Andric #include "lldb/Target/Process.h"
15*c9157d92SDimitry Andric #include "lldb/Utility/Log.h"
16*c9157d92SDimitry Andric #include "lldb/Utility/Status.h"
17*c9157d92SDimitry Andric #include "lldb/lldb-enumerations.h"
18*c9157d92SDimitry Andric
19*c9157d92SDimitry Andric #if LLDB_ENABLE_PYTHON
20*c9157d92SDimitry Andric
21*c9157d92SDimitry Andric #include "../SWIGPythonBridge.h"
22*c9157d92SDimitry Andric #include "../ScriptInterpreterPythonImpl.h"
23*c9157d92SDimitry Andric #include "ScriptedProcessPythonInterface.h"
24*c9157d92SDimitry Andric #include "ScriptedThreadPythonInterface.h"
25*c9157d92SDimitry Andric #include <optional>
26*c9157d92SDimitry Andric
27*c9157d92SDimitry Andric using namespace lldb;
28*c9157d92SDimitry Andric using namespace lldb_private;
29*c9157d92SDimitry Andric using namespace lldb_private::python;
30*c9157d92SDimitry Andric using Locker = ScriptInterpreterPythonImpl::Locker;
31*c9157d92SDimitry Andric
ScriptedProcessPythonInterface(ScriptInterpreterPythonImpl & interpreter)32*c9157d92SDimitry Andric ScriptedProcessPythonInterface::ScriptedProcessPythonInterface(
33*c9157d92SDimitry Andric ScriptInterpreterPythonImpl &interpreter)
34*c9157d92SDimitry Andric : ScriptedProcessInterface(), ScriptedPythonInterface(interpreter) {}
35*c9157d92SDimitry Andric
36*c9157d92SDimitry Andric llvm::Expected<StructuredData::GenericSP>
CreatePluginObject(llvm::StringRef class_name,ExecutionContext & exe_ctx,StructuredData::DictionarySP args_sp,StructuredData::Generic * script_obj)37*c9157d92SDimitry Andric ScriptedProcessPythonInterface::CreatePluginObject(
38*c9157d92SDimitry Andric llvm::StringRef class_name, ExecutionContext &exe_ctx,
39*c9157d92SDimitry Andric StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
40*c9157d92SDimitry Andric ExecutionContextRefSP exe_ctx_ref_sp =
41*c9157d92SDimitry Andric std::make_shared<ExecutionContextRef>(exe_ctx);
42*c9157d92SDimitry Andric StructuredDataImpl sd_impl(args_sp);
43*c9157d92SDimitry Andric return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
44*c9157d92SDimitry Andric exe_ctx_ref_sp, sd_impl);
45*c9157d92SDimitry Andric }
46*c9157d92SDimitry Andric
GetCapabilities()47*c9157d92SDimitry Andric StructuredData::DictionarySP ScriptedProcessPythonInterface::GetCapabilities() {
48*c9157d92SDimitry Andric Status error;
49*c9157d92SDimitry Andric StructuredData::DictionarySP dict =
50*c9157d92SDimitry Andric Dispatch<StructuredData::DictionarySP>("get_capabilities", error);
51*c9157d92SDimitry Andric
52*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, error))
53*c9157d92SDimitry Andric return {};
54*c9157d92SDimitry Andric
55*c9157d92SDimitry Andric return dict;
56*c9157d92SDimitry Andric }
57*c9157d92SDimitry Andric
58*c9157d92SDimitry Andric Status
Attach(const ProcessAttachInfo & attach_info)59*c9157d92SDimitry Andric ScriptedProcessPythonInterface::Attach(const ProcessAttachInfo &attach_info) {
60*c9157d92SDimitry Andric lldb::ProcessAttachInfoSP attach_info_sp =
61*c9157d92SDimitry Andric std::make_shared<ProcessAttachInfo>(attach_info);
62*c9157d92SDimitry Andric return GetStatusFromMethod("attach", attach_info_sp);
63*c9157d92SDimitry Andric }
64*c9157d92SDimitry Andric
Launch()65*c9157d92SDimitry Andric Status ScriptedProcessPythonInterface::Launch() {
66*c9157d92SDimitry Andric return GetStatusFromMethod("launch");
67*c9157d92SDimitry Andric }
68*c9157d92SDimitry Andric
Resume()69*c9157d92SDimitry Andric Status ScriptedProcessPythonInterface::Resume() {
70*c9157d92SDimitry Andric // When calling ScriptedProcess.Resume from lldb we should always stop.
71*c9157d92SDimitry Andric return GetStatusFromMethod("resume", /*should_stop=*/true);
72*c9157d92SDimitry Andric }
73*c9157d92SDimitry Andric
74*c9157d92SDimitry Andric std::optional<MemoryRegionInfo>
GetMemoryRegionContainingAddress(lldb::addr_t address,Status & error)75*c9157d92SDimitry Andric ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress(
76*c9157d92SDimitry Andric lldb::addr_t address, Status &error) {
77*c9157d92SDimitry Andric auto mem_region = Dispatch<std::optional<MemoryRegionInfo>>(
78*c9157d92SDimitry Andric "get_memory_region_containing_address", error, address);
79*c9157d92SDimitry Andric
80*c9157d92SDimitry Andric if (error.Fail()) {
81*c9157d92SDimitry Andric return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION,
82*c9157d92SDimitry Andric error.AsCString(), error);
83*c9157d92SDimitry Andric }
84*c9157d92SDimitry Andric
85*c9157d92SDimitry Andric return mem_region;
86*c9157d92SDimitry Andric }
87*c9157d92SDimitry Andric
GetThreadsInfo()88*c9157d92SDimitry Andric StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() {
89*c9157d92SDimitry Andric Status error;
90*c9157d92SDimitry Andric StructuredData::DictionarySP dict =
91*c9157d92SDimitry Andric Dispatch<StructuredData::DictionarySP>("get_threads_info", error);
92*c9157d92SDimitry Andric
93*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, error))
94*c9157d92SDimitry Andric return {};
95*c9157d92SDimitry Andric
96*c9157d92SDimitry Andric return dict;
97*c9157d92SDimitry Andric }
98*c9157d92SDimitry Andric
CreateBreakpoint(lldb::addr_t addr,Status & error)99*c9157d92SDimitry Andric bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr,
100*c9157d92SDimitry Andric Status &error) {
101*c9157d92SDimitry Andric Status py_error;
102*c9157d92SDimitry Andric StructuredData::ObjectSP obj =
103*c9157d92SDimitry Andric Dispatch("create_breakpoint", py_error, addr, error);
104*c9157d92SDimitry Andric
105*c9157d92SDimitry Andric // If there was an error on the python call, surface it to the user.
106*c9157d92SDimitry Andric if (py_error.Fail())
107*c9157d92SDimitry Andric error = py_error;
108*c9157d92SDimitry Andric
109*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
110*c9157d92SDimitry Andric return {};
111*c9157d92SDimitry Andric
112*c9157d92SDimitry Andric return obj->GetBooleanValue();
113*c9157d92SDimitry Andric }
114*c9157d92SDimitry Andric
ReadMemoryAtAddress(lldb::addr_t address,size_t size,Status & error)115*c9157d92SDimitry Andric lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(
116*c9157d92SDimitry Andric lldb::addr_t address, size_t size, Status &error) {
117*c9157d92SDimitry Andric Status py_error;
118*c9157d92SDimitry Andric lldb::DataExtractorSP data_sp = Dispatch<lldb::DataExtractorSP>(
119*c9157d92SDimitry Andric "read_memory_at_address", py_error, address, size, error);
120*c9157d92SDimitry Andric
121*c9157d92SDimitry Andric // If there was an error on the python call, surface it to the user.
122*c9157d92SDimitry Andric if (py_error.Fail())
123*c9157d92SDimitry Andric error = py_error;
124*c9157d92SDimitry Andric
125*c9157d92SDimitry Andric return data_sp;
126*c9157d92SDimitry Andric }
127*c9157d92SDimitry Andric
WriteMemoryAtAddress(lldb::addr_t addr,lldb::DataExtractorSP data_sp,Status & error)128*c9157d92SDimitry Andric lldb::offset_t ScriptedProcessPythonInterface::WriteMemoryAtAddress(
129*c9157d92SDimitry Andric lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error) {
130*c9157d92SDimitry Andric Status py_error;
131*c9157d92SDimitry Andric StructuredData::ObjectSP obj =
132*c9157d92SDimitry Andric Dispatch("write_memory_at_address", py_error, addr, data_sp, error);
133*c9157d92SDimitry Andric
134*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
135*c9157d92SDimitry Andric return LLDB_INVALID_OFFSET;
136*c9157d92SDimitry Andric
137*c9157d92SDimitry Andric // If there was an error on the python call, surface it to the user.
138*c9157d92SDimitry Andric if (py_error.Fail())
139*c9157d92SDimitry Andric error = py_error;
140*c9157d92SDimitry Andric
141*c9157d92SDimitry Andric return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET);
142*c9157d92SDimitry Andric }
143*c9157d92SDimitry Andric
GetLoadedImages()144*c9157d92SDimitry Andric StructuredData::ArraySP ScriptedProcessPythonInterface::GetLoadedImages() {
145*c9157d92SDimitry Andric Status error;
146*c9157d92SDimitry Andric StructuredData::ArraySP array =
147*c9157d92SDimitry Andric Dispatch<StructuredData::ArraySP>("get_loaded_images", error);
148*c9157d92SDimitry Andric
149*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array, error))
150*c9157d92SDimitry Andric return {};
151*c9157d92SDimitry Andric
152*c9157d92SDimitry Andric return array;
153*c9157d92SDimitry Andric }
154*c9157d92SDimitry Andric
GetProcessID()155*c9157d92SDimitry Andric lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() {
156*c9157d92SDimitry Andric Status error;
157*c9157d92SDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_process_id", error);
158*c9157d92SDimitry Andric
159*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
160*c9157d92SDimitry Andric return LLDB_INVALID_PROCESS_ID;
161*c9157d92SDimitry Andric
162*c9157d92SDimitry Andric return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID);
163*c9157d92SDimitry Andric }
164*c9157d92SDimitry Andric
IsAlive()165*c9157d92SDimitry Andric bool ScriptedProcessPythonInterface::IsAlive() {
166*c9157d92SDimitry Andric Status error;
167*c9157d92SDimitry Andric StructuredData::ObjectSP obj = Dispatch("is_alive", error);
168*c9157d92SDimitry Andric
169*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
170*c9157d92SDimitry Andric return {};
171*c9157d92SDimitry Andric
172*c9157d92SDimitry Andric return obj->GetBooleanValue();
173*c9157d92SDimitry Andric }
174*c9157d92SDimitry Andric
175*c9157d92SDimitry Andric std::optional<std::string>
GetScriptedThreadPluginName()176*c9157d92SDimitry Andric ScriptedProcessPythonInterface::GetScriptedThreadPluginName() {
177*c9157d92SDimitry Andric Status error;
178*c9157d92SDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error);
179*c9157d92SDimitry Andric
180*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
181*c9157d92SDimitry Andric return {};
182*c9157d92SDimitry Andric
183*c9157d92SDimitry Andric return obj->GetStringValue().str();
184*c9157d92SDimitry Andric }
185*c9157d92SDimitry Andric
186*c9157d92SDimitry Andric lldb::ScriptedThreadInterfaceSP
CreateScriptedThreadInterface()187*c9157d92SDimitry Andric ScriptedProcessPythonInterface::CreateScriptedThreadInterface() {
188*c9157d92SDimitry Andric return m_interpreter.CreateScriptedThreadInterface();
189*c9157d92SDimitry Andric }
190*c9157d92SDimitry Andric
GetMetadata()191*c9157d92SDimitry Andric StructuredData::DictionarySP ScriptedProcessPythonInterface::GetMetadata() {
192*c9157d92SDimitry Andric Status error;
193*c9157d92SDimitry Andric StructuredData::DictionarySP dict =
194*c9157d92SDimitry Andric Dispatch<StructuredData::DictionarySP>("get_process_metadata", error);
195*c9157d92SDimitry Andric
196*c9157d92SDimitry Andric if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, error))
197*c9157d92SDimitry Andric return {};
198*c9157d92SDimitry Andric
199*c9157d92SDimitry Andric return dict;
200*c9157d92SDimitry Andric }
201*c9157d92SDimitry Andric
202*c9157d92SDimitry Andric #endif
203