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/Host/Config.h"
10#include "lldb/Utility/Log.h"
11#include "lldb/lldb-enumerations.h"
12
13#if LLDB_ENABLE_PYTHON
14
15// LLDB Python header must be included first
16#include "../lldb-python.h"
17
18#include "../SWIGPythonBridge.h"
19#include "../ScriptInterpreterPythonImpl.h"
20#include "ScriptedThreadPythonInterface.h"
21#include <optional>
22
23using namespace lldb;
24using namespace lldb_private;
25using namespace lldb_private::python;
26using Locker = ScriptInterpreterPythonImpl::Locker;
27
28ScriptedThreadPythonInterface::ScriptedThreadPythonInterface(
29 ScriptInterpreterPythonImpl &interpreter)
30 : ScriptedThreadInterface(), ScriptedPythonInterface(interpreter) {}
31
32llvm::Expected<StructuredData::GenericSP>
33ScriptedThreadPythonInterface::CreatePluginObject(
34 const llvm::StringRef class_name, ExecutionContext &exe_ctx,
35 StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
36 ExecutionContextRefSP exe_ctx_ref_sp =
37 std::make_shared<ExecutionContextRef>(args&: exe_ctx);
38 StructuredDataImpl sd_impl(args_sp);
39 return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
40 args: exe_ctx_ref_sp, args: sd_impl);
41}
42
43lldb::tid_t ScriptedThreadPythonInterface::GetThreadID() {
44 Status error;
45 StructuredData::ObjectSP obj = Dispatch(method_name: "get_thread_id", error);
46
47 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
48 return LLDB_INVALID_THREAD_ID;
49
50 return obj->GetUnsignedIntegerValue(LLDB_INVALID_THREAD_ID);
51}
52
53std::optional<std::string> ScriptedThreadPythonInterface::GetName() {
54 Status error;
55 StructuredData::ObjectSP obj = Dispatch(method_name: "get_name", error);
56
57 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
58 return {};
59
60 return obj->GetStringValue().str();
61}
62
63lldb::StateType ScriptedThreadPythonInterface::GetState() {
64 Status error;
65 StructuredData::ObjectSP obj = Dispatch(method_name: "get_state", error);
66
67 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
68 return eStateInvalid;
69
70 return static_cast<StateType>(obj->GetUnsignedIntegerValue(fail_value: eStateInvalid));
71}
72
73std::optional<std::string> ScriptedThreadPythonInterface::GetQueue() {
74 Status error;
75 StructuredData::ObjectSP obj = Dispatch(method_name: "get_queue", error);
76
77 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
78 return {};
79
80 return obj->GetStringValue().str();
81}
82
83StructuredData::DictionarySP ScriptedThreadPythonInterface::GetStopReason() {
84 Status error;
85 StructuredData::DictionarySP dict =
86 Dispatch<StructuredData::DictionarySP>(method_name: "get_stop_reason", error);
87
88 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: dict, error))
89 return {};
90
91 return dict;
92}
93
94StructuredData::ArraySP ScriptedThreadPythonInterface::GetStackFrames() {
95 Status error;
96 StructuredData::ArraySP arr =
97 Dispatch<StructuredData::ArraySP>(method_name: "get_stackframes", error);
98
99 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: arr, error))
100 return {};
101
102 return arr;
103}
104
105StructuredData::DictionarySP ScriptedThreadPythonInterface::GetRegisterInfo() {
106 Status error;
107 StructuredData::DictionarySP dict =
108 Dispatch<StructuredData::DictionarySP>(method_name: "get_register_info", error);
109
110 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: dict, error))
111 return {};
112
113 return dict;
114}
115
116std::optional<std::string> ScriptedThreadPythonInterface::GetRegisterContext() {
117 Status error;
118 StructuredData::ObjectSP obj = Dispatch(method_name: "get_register_context", error);
119
120 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
121 return {};
122
123 return obj->GetAsString()->GetValue().str();
124}
125
126StructuredData::ArraySP ScriptedThreadPythonInterface::GetExtendedInfo() {
127 Status error;
128 StructuredData::ArraySP arr =
129 Dispatch<StructuredData::ArraySP>(method_name: "get_extended_info", error);
130
131 if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj: arr, error))
132 return {};
133
134 return arr;
135}
136
137#endif
138

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