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/Host/Config.h"
10 #include "lldb/Utility/Log.h"
11 #include "lldb/Utility/Status.h"
12 #include "lldb/lldb-enumerations.h"
13 
14 #if LLDB_ENABLE_PYTHON
15 
16 // LLDB Python header must be included first
17 #include "../lldb-python.h"
18 
19 #include "../SWIGPythonBridge.h"
20 #include "../ScriptInterpreterPythonImpl.h"
21 #include "ScriptedPlatformPythonInterface.h"
22 
23 #include "lldb/Target/ExecutionContext.h"
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 using namespace lldb_private::python;
28 using Locker = ScriptInterpreterPythonImpl::Locker;
29 
30 ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(
31     ScriptInterpreterPythonImpl &interpreter)
32     : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}
33 
34 llvm::Expected<StructuredData::GenericSP>
35 ScriptedPlatformPythonInterface::CreatePluginObject(
36     llvm::StringRef class_name, ExecutionContext &exe_ctx,
37     StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
38   ExecutionContextRefSP exe_ctx_ref_sp =
39       std::make_shared<ExecutionContextRef>(exe_ctx);
40   StructuredDataImpl sd_impl(args_sp);
41   return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
42                                                      exe_ctx_ref_sp, sd_impl);
43 }
44 
45 StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {
46   Status error;
47   StructuredData::DictionarySP dict_sp =
48       Dispatch<StructuredData::DictionarySP>("list_processes", error);
49 
50   if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
51     return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
52         LLVM_PRETTY_FUNCTION,
53         llvm::Twine("Null or invalid object (" +
54                     llvm::Twine(error.AsCString()) + llvm::Twine(")."))
55             .str(),
56         error);
57   }
58 
59   return dict_sp;
60 }
61 
62 StructuredData::DictionarySP
63 ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) {
64   Status error;
65   StructuredData::DictionarySP dict_sp =
66       Dispatch<StructuredData::DictionarySP>("get_process_info", error, pid);
67 
68   if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
69     return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
70         LLVM_PRETTY_FUNCTION,
71         llvm::Twine("Null or invalid object (" +
72                     llvm::Twine(error.AsCString()) + llvm::Twine(")."))
73             .str(),
74         error);
75   }
76 
77   return dict_sp;
78 }
79 
80 Status ScriptedPlatformPythonInterface::AttachToProcess(
81     ProcessAttachInfoSP attach_info) {
82   // FIXME: Pass `attach_info` to method call
83   return GetStatusFromMethod("attach_to_process");
84 }
85 
86 Status ScriptedPlatformPythonInterface::LaunchProcess(
87     ProcessLaunchInfoSP launch_info) {
88   // FIXME: Pass `launch_info` to method call
89   return GetStatusFromMethod("launch_process");
90 }
91 
92 Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
93   return GetStatusFromMethod("kill_process", pid);
94 }
95 
96 #endif // LLDB_ENABLE_PYTHON
97