109 lines · cpp
1//===-- ScriptedPlatformPythonInterface.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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Core/PluginManager.h"10#include "lldb/Host/Config.h"11#include "lldb/Target/ExecutionContext.h"12#include "lldb/Utility/Log.h"13#include "lldb/Utility/Status.h"14#include "lldb/lldb-enumerations.h"15 16#if LLDB_ENABLE_PYTHON17 18// clang-format off19// LLDB Python header must be included first20#include "../lldb-python.h"21//clang-format on22 23#include "../SWIGPythonBridge.h"24#include "../ScriptInterpreterPythonImpl.h"25#include "ScriptedPlatformPythonInterface.h"26 27using namespace lldb;28using namespace lldb_private;29using namespace lldb_private::python;30using Locker = ScriptInterpreterPythonImpl::Locker;31 32ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(33 ScriptInterpreterPythonImpl &interpreter)34 : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}35 36llvm::Expected<StructuredData::GenericSP>37ScriptedPlatformPythonInterface::CreatePluginObject(38 llvm::StringRef class_name, ExecutionContext &exe_ctx,39 StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {40 ExecutionContextRefSP exe_ctx_ref_sp =41 std::make_shared<ExecutionContextRef>(exe_ctx);42 StructuredDataImpl sd_impl(args_sp);43 return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,44 exe_ctx_ref_sp, sd_impl);45}46 47StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {48 Status error;49 StructuredData::DictionarySP dict_sp =50 Dispatch<StructuredData::DictionarySP>("list_processes", error);51 52 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {53 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(54 LLVM_PRETTY_FUNCTION,55 llvm::Twine("Null or invalid object (" +56 llvm::Twine(error.AsCString()) + llvm::Twine(")."))57 .str(),58 error);59 }60 61 return dict_sp;62}63 64StructuredData::DictionarySP65ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) {66 Status error;67 StructuredData::DictionarySP dict_sp =68 Dispatch<StructuredData::DictionarySP>("get_process_info", error, pid);69 70 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {71 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(72 LLVM_PRETTY_FUNCTION,73 llvm::Twine("Null or invalid object (" +74 llvm::Twine(error.AsCString()) + llvm::Twine(")."))75 .str(),76 error);77 }78 79 return dict_sp;80}81 82Status ScriptedPlatformPythonInterface::AttachToProcess(83 ProcessAttachInfoSP attach_info) {84 // FIXME: Pass `attach_info` to method call85 return GetStatusFromMethod("attach_to_process");86}87 88Status ScriptedPlatformPythonInterface::LaunchProcess(89 ProcessLaunchInfoSP launch_info) {90 // FIXME: Pass `launch_info` to method call91 return GetStatusFromMethod("launch_process");92}93 94Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {95 return GetStatusFromMethod("kill_process", pid);96}97 98void ScriptedPlatformPythonInterface::Initialize() {99 PluginManager::RegisterPlugin(100 GetPluginNameStatic(), "Mock platform and interact with its processes.",101 CreateInstance, eScriptLanguagePython, {});102}103 104void ScriptedPlatformPythonInterface::Terminate() {105 PluginManager::UnregisterPlugin(CreateInstance);106}107 108#endif // LLDB_ENABLE_PYTHON109