[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / lldb / source / Plugins / ScriptInterpreter / Python / Interfaces / ScriptedPlatformPythonInterface.cpp
blob0c078f019b8f3bbd4f93f0e0d30c1d07bad8b969
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 //===----------------------------------------------------------------------===//
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"
16 #if LLDB_ENABLE_PYTHON
18 // clang-format off
19 // LLDB Python header must be included first
20 #include "../lldb-python.h"
21 //clang-format on
23 #include "../SWIGPythonBridge.h"
24 #include "../ScriptInterpreterPythonImpl.h"
25 #include "ScriptedPlatformPythonInterface.h"
27 using namespace lldb;
28 using namespace lldb_private;
29 using namespace lldb_private::python;
30 using Locker = ScriptInterpreterPythonImpl::Locker;
32 ScriptedPlatformPythonInterface::ScriptedPlatformPythonInterface(
33 ScriptInterpreterPythonImpl &interpreter)
34 : ScriptedPlatformInterface(), ScriptedPythonInterface(interpreter) {}
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);
47 StructuredData::DictionarySP ScriptedPlatformPythonInterface::ListProcesses() {
48 Status error;
49 StructuredData::DictionarySP dict_sp =
50 Dispatch<StructuredData::DictionarySP>("list_processes", error);
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);
61 return dict_sp;
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);
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);
79 return dict_sp;
82 Status ScriptedPlatformPythonInterface::AttachToProcess(
83 ProcessAttachInfoSP attach_info) {
84 // FIXME: Pass `attach_info` to method call
85 return GetStatusFromMethod("attach_to_process");
88 Status ScriptedPlatformPythonInterface::LaunchProcess(
89 ProcessLaunchInfoSP launch_info) {
90 // FIXME: Pass `launch_info` to method call
91 return GetStatusFromMethod("launch_process");
94 Status ScriptedPlatformPythonInterface::KillProcess(lldb::pid_t pid) {
95 return GetStatusFromMethod("kill_process", pid);
98 void ScriptedPlatformPythonInterface::Initialize() {
99 PluginManager::RegisterPlugin(
100 GetPluginNameStatic(), "Mock platform and interact with its processes.",
101 CreateInstance, eScriptLanguagePython, {});
104 void ScriptedPlatformPythonInterface::Terminate() {
105 PluginManager::UnregisterPlugin(CreateInstance);
108 #endif // LLDB_ENABLE_PYTHON