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-exception
6//
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_PYTHON
17
18// clang-format off
19// LLDB Python header must be included first
20#include "../lldb-python.h"
21//clang-format on
22
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>(args&: exe_ctx);
42 StructuredDataImpl sd_impl(args_sp);
43 return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
44 args: exe_ctx_ref_sp, args: sd_impl);
45}
46
47StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {
48 Status error;
49 StructuredData::DictionarySP dict_sp =
50 Dispatch<StructuredData::DictionarySP>(method_name: "list_processes", error);
51
52 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
53 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
54 LLVM_PRETTY_FUNCTION,
55 error_msg: 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::DictionarySP
65ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) {
66 Status error;
67 StructuredData::DictionarySP dict_sp =
68 Dispatch<StructuredData::DictionarySP>(method_name: "get_process_info", error, args&: pid);
69
70 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
71 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
72 LLVM_PRETTY_FUNCTION,
73 error_msg: 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 call
85 return GetStatusFromMethod(method_name: "attach_to_process");
86}
87
88Status ScriptedPlatformPythonInterface::LaunchProcess(
89 ProcessLaunchInfoSP launch_info) {
90 // FIXME: Pass `launch_info` to method call
91 return GetStatusFromMethod(method_name: "launch_process");
92}
93
94Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
95 return GetStatusFromMethod(method_name: "kill_process", args&: pid);
96}
97
98void ScriptedPlatformPythonInterface::Initialize() {
99 PluginManager::RegisterPlugin(
100 name: GetPluginNameStatic(), description: "Mock platform and interact with its processes.",
101 create_callback: CreateInstance, language: eScriptLanguagePython, usages: {});
102}
103
104void ScriptedPlatformPythonInterface::Terminate() {
105 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
106}
107
108#endif // LLDB_ENABLE_PYTHON
109

source code of lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/ScriptedPlatformPythonInterface.cpp