LLDB mainline
ScriptedProcessPythonInterface.cpp
Go to the documentation of this file.
1//===-- ScriptedProcessPythonInterface.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
10#include "lldb/Host/Config.h"
11#include "lldb/Utility/Log.h"
12#include "lldb/Utility/Status.h"
14
15#if LLDB_ENABLE_PYTHON
16
17// clang-format off
18// LLDB Python header must be included first
19#include "../lldb-python.h"
20
21#include "../SWIGPythonBridge.h"
22#include "../ScriptInterpreterPythonImpl.h"
25
26// Included in this position to prevent redefinition of pid_t on Windows.
27#include "lldb/Target/Process.h"
28//clang-format off
29
30#include <optional>
31
32using namespace lldb;
33using namespace lldb_private;
34using namespace lldb_private::python;
35using Locker = ScriptInterpreterPythonImpl::Locker;
36
37ScriptedProcessPythonInterface::ScriptedProcessPythonInterface(
38 ScriptInterpreterPythonImpl &interpreter)
39 : ScriptedProcessInterface(), ScriptedPythonInterface(interpreter) {}
40
41llvm::Expected<StructuredData::GenericSP>
42ScriptedProcessPythonInterface::CreatePluginObject(
43 llvm::StringRef class_name, ExecutionContext &exe_ctx,
45 ExecutionContextRefSP exe_ctx_ref_sp =
46 std::make_shared<ExecutionContextRef>(exe_ctx);
47 StructuredDataImpl sd_impl(args_sp);
48 return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
49 exe_ctx_ref_sp, sd_impl);
50}
51
52StructuredData::DictionarySP ScriptedProcessPythonInterface::GetCapabilities() {
55 Dispatch<StructuredData::DictionarySP>("get_capabilities", error);
56
57 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
58 error))
59 return {};
60
61 return dict;
62}
63
65ScriptedProcessPythonInterface::Attach(const ProcessAttachInfo &attach_info) {
66 lldb::ProcessAttachInfoSP attach_info_sp =
67 std::make_shared<ProcessAttachInfo>(attach_info);
68 return GetStatusFromMethod("attach", attach_info_sp);
69}
70
71Status ScriptedProcessPythonInterface::Launch() {
72 return GetStatusFromMethod("launch");
73}
74
75Status ScriptedProcessPythonInterface::Resume() {
76 // When calling ScriptedProcess.Resume from lldb we should always stop.
77 return GetStatusFromMethod("resume", /*should_stop=*/true);
78}
79
80std::optional<MemoryRegionInfo>
81ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress(
82 lldb::addr_t address, Status &error) {
83 auto mem_region = Dispatch<std::optional<MemoryRegionInfo>>(
84 "get_memory_region_containing_address", error, address);
85
86 if (error.Fail()) {
87 return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION,
88 error.AsCString(), error);
89 }
90
91 return mem_region;
92}
93
94StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() {
97 Dispatch<StructuredData::DictionarySP>("get_threads_info", error);
98
99 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
100 error))
101 return {};
102
103 return dict;
104}
105
106bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr,
107 Status &error) {
108 Status py_error;
110 Dispatch("create_breakpoint", py_error, addr, error);
111
112 // If there was an error on the python call, surface it to the user.
113 if (py_error.Fail())
114 error = py_error;
115
116 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
117 error))
118 return {};
119
120 return obj->GetBooleanValue();
121}
122
123lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(
124 lldb::addr_t address, size_t size, Status &error) {
125 Status py_error;
126 lldb::DataExtractorSP data_sp = Dispatch<lldb::DataExtractorSP>(
127 "read_memory_at_address", py_error, address, size, error);
128
129 // If there was an error on the python call, surface it to the user.
130 if (py_error.Fail())
131 error = py_error;
132
133 return data_sp;
134}
135
136lldb::offset_t ScriptedProcessPythonInterface::WriteMemoryAtAddress(
138 Status py_error;
140 Dispatch("write_memory_at_address", py_error, addr, data_sp, error);
141
142 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
143 error))
144 return LLDB_INVALID_OFFSET;
145
146 // If there was an error on the python call, surface it to the user.
147 if (py_error.Fail())
148 error = py_error;
149
150 return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET);
151}
152
153StructuredData::ArraySP ScriptedProcessPythonInterface::GetLoadedImages() {
156 Dispatch<StructuredData::ArraySP>("get_loaded_images", error);
157
158 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array,
159 error))
160 return {};
161
162 return array;
163}
164
165lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() {
167 StructuredData::ObjectSP obj = Dispatch("get_process_id", error);
168
169 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
170 error))
172
173 return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID);
174}
175
176bool ScriptedProcessPythonInterface::IsAlive() {
178 StructuredData::ObjectSP obj = Dispatch("is_alive", error);
179
180 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
181 error))
182 return {};
183
184 return obj->GetBooleanValue();
185}
186
187std::optional<std::string>
188ScriptedProcessPythonInterface::GetScriptedThreadPluginName() {
190 StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error);
191
192 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
193 error))
194 return {};
195
196 return obj->GetStringValue().str();
197}
198
200ScriptedProcessPythonInterface::CreateScriptedThreadInterface() {
201 return m_interpreter.CreateScriptedThreadInterface();
202}
203
204StructuredData::DictionarySP ScriptedProcessPythonInterface::GetMetadata() {
207 Dispatch<StructuredData::DictionarySP>("get_process_metadata", error);
208
209 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
210 error))
211 return {};
212
213 return dict;
214}
215
216void ScriptedProcessPythonInterface::Initialize() {
217 const std::vector<llvm::StringRef> ci_usages = {
218 "process attach -C <script-name> [-k key -v value ...]",
219 "process launch -C <script-name> [-k key -v value ...]"};
220 const std::vector<llvm::StringRef> api_usages = {
221 "SBAttachInfo.SetScriptedProcessClassName",
222 "SBAttachInfo.SetScriptedProcessDictionary",
223 "SBTarget.Attach",
224 "SBLaunchInfo.SetScriptedProcessClassName",
225 "SBLaunchInfo.SetScriptedProcessDictionary",
226 "SBTarget.Launch"};
227 PluginManager::RegisterPlugin(
228 GetPluginNameStatic(), llvm::StringRef("Mock process state"),
229 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
230}
231
232void ScriptedProcessPythonInterface::Terminate() {
233 PluginManager::UnregisterPlugin(CreateInstance);
234}
235
236#endif
static llvm::raw_ostream & error(Stream &strm)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
An error handling class.
Definition: Status.h:44
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
#define LLDB_INVALID_OFFSET
Definition: lldb-defines.h:93
#define LLDB_INVALID_PROCESS_ID
Definition: lldb-defines.h:89
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
Definition: lldb-forward.h:388
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
Definition: lldb-forward.h:411
uint64_t pid_t
Definition: lldb-types.h:83
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP
Definition: lldb-forward.h:348