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 using namespace lldb;
24 using namespace lldb_private;
25 using namespace lldb_private::python;
26 using Locker = ScriptInterpreterPythonImpl::Locker;
27
ScriptedPlatformPythonInterface(ScriptInterpreterPythonImpl & interpreter)28 ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(
29 ScriptInterpreterPythonImpl &interpreter)
30 : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}
31
CreatePluginObject(llvm::StringRef class_name,ExecutionContext & exe_ctx,StructuredData::DictionarySP args_sp,StructuredData::Generic * script_obj)32 StructuredData::GenericSP ScriptedPlatformPythonInterface::CreatePluginObject(
33 llvm::StringRef class_name, ExecutionContext &exe_ctx,
34 StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
35 if (class_name.empty())
36 return {};
37
38 StructuredDataImpl args_impl(args_sp);
39 std::string error_string;
40
41 Locker py_lock(&m_interpreter, Locker::AcquireLock | Locker::NoSTDIN,
42 Locker::FreeLock);
43
44 lldb::ExecutionContextRefSP exe_ctx_ref_sp =
45 std::make_shared<ExecutionContextRef>(exe_ctx);
46
47 PythonObject ret_val = LLDBSwigPythonCreateScriptedObject(
48 class_name.str().c_str(), m_interpreter.GetDictionaryName(),
49 exe_ctx_ref_sp, args_impl, error_string);
50
51 m_object_instance_sp =
52 StructuredData::GenericSP(new StructuredPythonObject(std::move(ret_val)));
53
54 return m_object_instance_sp;
55 }
56
ListProcesses()57 StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {
58 Status error;
59 StructuredData::DictionarySP dict_sp =
60 Dispatch<StructuredData::DictionarySP>("list_processes", error);
61
62 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
63 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
64 LLVM_PRETTY_FUNCTION,
65 llvm::Twine("Null or invalid object (" +
66 llvm::Twine(error.AsCString()) + llvm::Twine(")."))
67 .str(),
68 error);
69 }
70
71 return dict_sp;
72 }
73
74 StructuredData::DictionarySP
GetProcessInfo(lldb::pid_t pid)75 ScriptedPlatformPythonInterface::GetProcessInfo(lldb::pid_t pid) {
76 Status error;
77 StructuredData::DictionarySP dict_sp =
78 Dispatch<StructuredData::DictionarySP>("get_process_info", error, pid);
79
80 if (!dict_sp || !dict_sp->IsValid() || error.Fail()) {
81 return ScriptedInterface::ErrorWithMessage<StructuredData::DictionarySP>(
82 LLVM_PRETTY_FUNCTION,
83 llvm::Twine("Null or invalid object (" +
84 llvm::Twine(error.AsCString()) + llvm::Twine(")."))
85 .str(),
86 error);
87 }
88
89 return dict_sp;
90 }
91
AttachToProcess(ProcessAttachInfoSP attach_info)92 Status ScriptedPlatformPythonInterface::AttachToProcess(
93 ProcessAttachInfoSP attach_info) {
94 // FIXME: Pass `attach_info` to method call
95 return GetStatusFromMethod("attach_to_process");
96 }
97
LaunchProcess(ProcessLaunchInfoSP launch_info)98 Status ScriptedPlatformPythonInterface::LaunchProcess(
99 ProcessLaunchInfoSP launch_info) {
100 // FIXME: Pass `launch_info` to method call
101 return GetStatusFromMethod("launch_process");
102 }
103
KillProcess(lldb::pid_t pid)104 Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
105 return GetStatusFromMethod("kill_process", pid);
106 }
107
108 #endif // LLDB_ENABLE_PYTHON
109