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