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