1//===-- ScriptedThreadPythonInterface.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/Core/PluginManager.h"
10#include "lldb/Host/Config.h"
11#include "lldb/Target/ExecutionContext.h"
12#include "lldb/Utility/Log.h"
13#include "lldb/lldb-enumerations.h"
14
15#if LLDB_ENABLE_PYTHON
16
17// clang-format off
18// LLDB Python header must be included first
19#include "../lldb-python.h"
20//clang-format on
21
22#include "../SWIGPythonBridge.h"
23#include "../ScriptInterpreterPythonImpl.h"
24#include "OperatingSystemPythonInterface.h"
25
26using namespace lldb;
27using namespace lldb_private;
28using namespace lldb_private::python;
29using Locker = ScriptInterpreterPythonImpl::Locker;
30
31OperatingSystemPythonInterface::OperatingSystemPythonInterface(
32 ScriptInterpreterPythonImpl &interpreter)
33 : OperatingSystemInterface(), ScriptedThreadPythonInterface(interpreter) {}
34
35llvm::Expected<StructuredData::GenericSP>
36OperatingSystemPythonInterface::CreatePluginObject(
37 llvm::StringRef class_name, ExecutionContext &exe_ctx,
38 StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
39 return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj: nullptr,
40 args: exe_ctx.GetProcessSP());
41}
42
43StructuredData::DictionarySP
44OperatingSystemPythonInterface::CreateThread(lldb::tid_t tid,
45 lldb::addr_t context) {
46 Status error;
47 StructuredData::DictionarySP dict = Dispatch<StructuredData::DictionarySP>(
48 method_name: "create_thread", error, args&: tid, args&: context);
49
50 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: dict,
51 error))
52 return {};
53
54 return dict;
55}
56
57StructuredData::ArraySP OperatingSystemPythonInterface::GetThreadInfo() {
58 Status error;
59 StructuredData::ArraySP arr =
60 Dispatch<StructuredData::ArraySP>(method_name: "get_thread_info", error);
61
62 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: arr,
63 error))
64 return {};
65
66 return arr;
67}
68
69StructuredData::DictionarySP OperatingSystemPythonInterface::GetRegisterInfo() {
70 return ScriptedThreadPythonInterface::GetRegisterInfo();
71}
72
73std::optional<std::string>
74OperatingSystemPythonInterface::GetRegisterContextForTID(lldb::tid_t tid) {
75 Status error;
76 StructuredData::ObjectSP obj = Dispatch(method_name: "get_register_data", error, args&: tid);
77
78 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
79 error))
80 return {};
81
82 return obj->GetAsString()->GetValue().str();
83}
84
85std::optional<bool> OperatingSystemPythonInterface::DoesPluginReportAllThreads() {
86 Status error;
87 StructuredData::ObjectSP obj = Dispatch(method_name: "does_plugin_report_all_threads", error);
88 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
89 error))
90 return {};
91
92 return obj->GetAsBoolean()->GetValue();
93}
94
95void OperatingSystemPythonInterface::Initialize() {
96 const std::vector<llvm::StringRef> ci_usages = {
97 "settings set target.process.python-os-plugin-path <script-path>",
98 "settings set process.experimental.os-plugin-reports-all-threads [0/1]"};
99 const std::vector<llvm::StringRef> api_usages = {};
100 PluginManager::RegisterPlugin(
101 name: GetPluginNameStatic(), description: llvm::StringRef("Mock thread state"),
102 create_callback: CreateInstance, language: eScriptLanguagePython, usages: {ci_usages, api_usages});
103}
104
105void OperatingSystemPythonInterface::Terminate() {
106 PluginManager::UnregisterPlugin(create_callback: CreateInstance);
107}
108
109#endif
110

source code of lldb/source/Plugins/ScriptInterpreter/Python/Interfaces/OperatingSystemPythonInterface.cpp