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 
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace lldb_private::python;
30 using Locker = ScriptInterpreterPythonImpl::Locker;
31 
32 ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(
33     ScriptInterpreterPythonImpl &interpreter)
34     : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}
35 
36 llvm::Expected<StructuredData::GenericSP>
37 ScriptedPlatformPythonInterface::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 
47 StructuredData::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 
64 StructuredData::DictionarySP
65 ScriptedPlatformPythonInterface::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 
82 Status ScriptedPlatformPythonInterface::AttachToProcess(
83     ProcessAttachInfoSP attach_info) {
84   // FIXME: Pass `attach_info` to method call
85   return GetStatusFromMethod("attach_to_process");
86 }
87 
88 Status ScriptedPlatformPythonInterface::LaunchProcess(
89     ProcessLaunchInfoSP launch_info) {
90   // FIXME: Pass `launch_info` to method call
91   return GetStatusFromMethod("launch_process");
92 }
93 
94 Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
95   return GetStatusFromMethod("kill_process", pid);
96 }
97 
98 void ScriptedPlatformPythonInterface::Initialize() {
99   PluginManager::RegisterPlugin(
100       GetPluginNameStatic(), "Mock platform and interact with its processes.",
101       CreateInstance, eScriptLanguagePython, {});
102 }
103 
104 void ScriptedPlatformPythonInterface::Terminate() {
105   PluginManager::UnregisterPlugin(CreateInstance);
106 }
107 
108 #endif // LLDB_ENABLE_PYTHON
109