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