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