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
9#include "../lldb-python.h"
10
12#include "lldb/Target/Process.h"
13#include "lldb/Utility/Log.h"
14#include "lldb/Utility/Status.h"
16
17#include "../SWIGPythonBridge.h"
21
22#include <optional>
23
24using namespace lldb;
25using namespace lldb_private;
26using namespace lldb_private::python;
28
32
33llvm::Expected<StructuredData::GenericSP>
35 const ScriptedMetadata &scripted_metadata, ExecutionContext &exe_ctx,
36 StructuredData::Generic *script_obj) {
37 ExecutionContextRefSP exe_ctx_ref_sp =
38 std::make_shared<ExecutionContextRef>(exe_ctx);
40 scripted_metadata, script_obj, exe_ctx_ref_sp,
41 scripted_metadata.GetArgsSP());
42}
43
55
58 lldb::ProcessAttachInfoSP attach_info_sp =
59 std::make_shared<ProcessAttachInfo>(attach_info);
60 return GetStatusFromMethod("attach", attach_info_sp);
61}
62
66
68 // When calling ScriptedProcess.Resume from lldb we should always stop.
69 return GetStatusFromMethod("resume", /*should_stop=*/true);
70}
71
72std::optional<MemoryRegionInfo>
74 lldb::addr_t address, Status &error) {
76 "get_memory_region_containing_address", error, address);
77
78 if (error.Fail()) {
79 return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION,
80 error.AsCString(), error);
81 }
82
83 return mem_region;
84}
85
97
99 Status &error) {
100 Status py_error;
102 Dispatch("create_breakpoint", py_error, addr, error);
103
104 // If there was an error on the python call, surface it to the user.
105 if (py_error.Fail())
106 error = std::move(py_error);
107
108 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
109 error))
110 return {};
111
112 return obj->GetBooleanValue();
113}
114
116 lldb::addr_t address, size_t size, Status &error) {
117 Status py_error;
119 "read_memory_at_address", py_error, address, size, error);
120
121 // If there was an error on the python call, surface it to the user.
122 if (py_error.Fail())
123 error = std::move(py_error);
124
125 return data_sp;
126}
127
130 Status py_error;
132 Dispatch("write_memory_at_address", py_error, addr, data_sp, error);
133
134 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
135 error))
136 return LLDB_INVALID_OFFSET;
137
138 // If there was an error on the python call, surface it to the user.
139 if (py_error.Fail())
140 error = std::move(py_error);
141
142 return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET);
143}
144
148 Dispatch<StructuredData::ArraySP>("get_loaded_images", error);
149
150 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array,
151 error))
152 return {};
153
154 return array;
155}
156
159 StructuredData::ObjectSP obj = Dispatch("get_process_id", error);
160
161 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
162 error))
164
165 return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID);
166}
167
170 StructuredData::ObjectSP obj = Dispatch("is_alive", error);
171
172 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
173 error))
174 return {};
175
176 return obj->GetBooleanValue();
177}
178
179std::optional<std::string>
182 StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error);
183
184 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
185 error))
186 return {};
187
188 return obj->GetStringValue().str();
189}
190
193 return m_interpreter.CreateScriptedThreadInterface();
194}
195
199 Dispatch<StructuredData::DictionarySP>("get_process_metadata", error);
200
201 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
202 error))
203 return {};
204
205 return dict;
206}
207
209 const std::vector<llvm::StringRef> ci_usages = {
210 "process attach -C <script-name> [-k key -v value ...]",
211 "process launch -C <script-name> [-k key -v value ...]"};
212 const std::vector<llvm::StringRef> api_usages = {
213 "SBAttachInfo.SetScriptedProcessClassName",
214 "SBAttachInfo.SetScriptedProcessDictionary",
215 "SBTarget.Attach",
216 "SBLaunchInfo.SetScriptedProcessClassName",
217 "SBLaunchInfo.SetScriptedProcessDictionary",
218 "SBTarget.Launch"};
220 GetPluginNameStatic(), llvm::StringRef("Mock process state"),
221 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
222}
223
static llvm::raw_ostream & error(Stream &strm)
ScriptInterpreterPythonImpl::Locker Locker
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef error_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
StructuredData::DictionarySP GetArgsSP() const
StructuredData::DictionarySP GetThreadsInfo() override
lldb::ScriptedThreadInterfaceSP CreateScriptedThreadInterface() override
lldb::DataExtractorSP ReadMemoryAtAddress(lldb::addr_t address, size_t size, Status &error) override
std::optional< std::string > GetScriptedThreadPluginName() override
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, ExecutionContext &exe_ctx, StructuredData::Generic *script_obj=nullptr) override
bool CreateBreakpoint(lldb::addr_t addr, Status &error) override
Status Attach(const ProcessAttachInfo &attach_info) override
std::optional< MemoryRegionInfo > GetMemoryRegionContainingAddress(lldb::addr_t address, Status &error) override
lldb::offset_t WriteMemoryAtAddress(lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error) override
ScriptedProcessPythonInterface(ScriptInterpreterPythonImpl &interpreter)
StructuredData::DictionarySP GetCapabilities() override
Status GetStatusFromMethod(llvm::StringRef method_name, Args &&...args)
ScriptInterpreterPythonImpl & m_interpreter
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
T Dispatch(llvm::StringRef method_name, Status &error, Args &&...args)
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, StructuredData::Generic *script_obj, Args... args)
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
std::shared_ptr< Array > ArraySP
#define LLDB_INVALID_OFFSET
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
@ eScriptLanguagePython
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::ProcessAttachInfo > ProcessAttachInfoSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
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
std::shared_ptr< lldb_private::ExecutionContextRef > ExecutionContextRefSP