Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedProcessPythonInterface.cpp
39688 views
//===-- ScriptedProcessPythonInterface.cpp --------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Host/Config.h"9#if LLDB_ENABLE_PYTHON10// LLDB Python header must be included first11#include "../lldb-python.h"12#endif13#include "lldb/Target/Process.h"14#include "lldb/Utility/Log.h"15#include "lldb/Utility/Status.h"16#include "lldb/lldb-enumerations.h"1718#if LLDB_ENABLE_PYTHON1920#include "../SWIGPythonBridge.h"21#include "../ScriptInterpreterPythonImpl.h"22#include "ScriptedProcessPythonInterface.h"23#include "ScriptedThreadPythonInterface.h"24#include <optional>2526using namespace lldb;27using namespace lldb_private;28using namespace lldb_private::python;29using Locker = ScriptInterpreterPythonImpl::Locker;3031ScriptedProcessPythonInterface::ScriptedProcessPythonInterface(32ScriptInterpreterPythonImpl &interpreter)33: ScriptedProcessInterface(), ScriptedPythonInterface(interpreter) {}3435llvm::Expected<StructuredData::GenericSP>36ScriptedProcessPythonInterface::CreatePluginObject(37llvm::StringRef class_name, ExecutionContext &exe_ctx,38StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {39ExecutionContextRefSP exe_ctx_ref_sp =40std::make_shared<ExecutionContextRef>(exe_ctx);41StructuredDataImpl sd_impl(args_sp);42return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,43exe_ctx_ref_sp, sd_impl);44}4546StructuredData::DictionarySP ScriptedProcessPythonInterface::GetCapabilities() {47Status error;48StructuredData::DictionarySP dict =49Dispatch<StructuredData::DictionarySP>("get_capabilities", error);5051if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,52error))53return {};5455return dict;56}5758Status59ScriptedProcessPythonInterface::Attach(const ProcessAttachInfo &attach_info) {60lldb::ProcessAttachInfoSP attach_info_sp =61std::make_shared<ProcessAttachInfo>(attach_info);62return GetStatusFromMethod("attach", attach_info_sp);63}6465Status ScriptedProcessPythonInterface::Launch() {66return GetStatusFromMethod("launch");67}6869Status ScriptedProcessPythonInterface::Resume() {70// When calling ScriptedProcess.Resume from lldb we should always stop.71return GetStatusFromMethod("resume", /*should_stop=*/true);72}7374std::optional<MemoryRegionInfo>75ScriptedProcessPythonInterface::GetMemoryRegionContainingAddress(76lldb::addr_t address, Status &error) {77auto mem_region = Dispatch<std::optional<MemoryRegionInfo>>(78"get_memory_region_containing_address", error, address);7980if (error.Fail()) {81return ErrorWithMessage<MemoryRegionInfo>(LLVM_PRETTY_FUNCTION,82error.AsCString(), error);83}8485return mem_region;86}8788StructuredData::DictionarySP ScriptedProcessPythonInterface::GetThreadsInfo() {89Status error;90StructuredData::DictionarySP dict =91Dispatch<StructuredData::DictionarySP>("get_threads_info", error);9293if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,94error))95return {};9697return dict;98}99100bool ScriptedProcessPythonInterface::CreateBreakpoint(lldb::addr_t addr,101Status &error) {102Status py_error;103StructuredData::ObjectSP obj =104Dispatch("create_breakpoint", py_error, addr, error);105106// If there was an error on the python call, surface it to the user.107if (py_error.Fail())108error = py_error;109110if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,111error))112return {};113114return obj->GetBooleanValue();115}116117lldb::DataExtractorSP ScriptedProcessPythonInterface::ReadMemoryAtAddress(118lldb::addr_t address, size_t size, Status &error) {119Status py_error;120lldb::DataExtractorSP data_sp = Dispatch<lldb::DataExtractorSP>(121"read_memory_at_address", py_error, address, size, error);122123// If there was an error on the python call, surface it to the user.124if (py_error.Fail())125error = py_error;126127return data_sp;128}129130lldb::offset_t ScriptedProcessPythonInterface::WriteMemoryAtAddress(131lldb::addr_t addr, lldb::DataExtractorSP data_sp, Status &error) {132Status py_error;133StructuredData::ObjectSP obj =134Dispatch("write_memory_at_address", py_error, addr, data_sp, error);135136if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,137error))138return LLDB_INVALID_OFFSET;139140// If there was an error on the python call, surface it to the user.141if (py_error.Fail())142error = py_error;143144return obj->GetUnsignedIntegerValue(LLDB_INVALID_OFFSET);145}146147StructuredData::ArraySP ScriptedProcessPythonInterface::GetLoadedImages() {148Status error;149StructuredData::ArraySP array =150Dispatch<StructuredData::ArraySP>("get_loaded_images", error);151152if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, array,153error))154return {};155156return array;157}158159lldb::pid_t ScriptedProcessPythonInterface::GetProcessID() {160Status error;161StructuredData::ObjectSP obj = Dispatch("get_process_id", error);162163if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,164error))165return LLDB_INVALID_PROCESS_ID;166167return obj->GetUnsignedIntegerValue(LLDB_INVALID_PROCESS_ID);168}169170bool ScriptedProcessPythonInterface::IsAlive() {171Status error;172StructuredData::ObjectSP obj = Dispatch("is_alive", error);173174if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,175error))176return {};177178return obj->GetBooleanValue();179}180181std::optional<std::string>182ScriptedProcessPythonInterface::GetScriptedThreadPluginName() {183Status error;184StructuredData::ObjectSP obj = Dispatch("get_scripted_thread_plugin", error);185186if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,187error))188return {};189190return obj->GetStringValue().str();191}192193lldb::ScriptedThreadInterfaceSP194ScriptedProcessPythonInterface::CreateScriptedThreadInterface() {195return m_interpreter.CreateScriptedThreadInterface();196}197198StructuredData::DictionarySP ScriptedProcessPythonInterface::GetMetadata() {199Status error;200StructuredData::DictionarySP dict =201Dispatch<StructuredData::DictionarySP>("get_process_metadata", error);202203if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,204error))205return {};206207return dict;208}209210#endif211212213