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 (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
49     return LLDB_INVALID_THREAD_ID;
50 
51   return obj->GetUnsignedIntegerValue(LLDB_INVALID_THREAD_ID);
52 }
53 
54 std::optional<std::string> ScriptedThreadPythonInterface::GetName() {
55   Status error;
56   StructuredData::ObjectSP obj = Dispatch("get_name", error);
57 
58   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
59     return {};
60 
61   return obj->GetStringValue().str();
62 }
63 
64 lldb::StateType ScriptedThreadPythonInterface::GetState() {
65   Status error;
66   StructuredData::ObjectSP obj = Dispatch("get_state", error);
67 
68   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
69     return eStateInvalid;
70 
71   return static_cast<StateType>(obj->GetUnsignedIntegerValue(eStateInvalid));
72 }
73 
74 std::optional<std::string> ScriptedThreadPythonInterface::GetQueue() {
75   Status error;
76   StructuredData::ObjectSP obj = Dispatch("get_queue", error);
77 
78   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
79     return {};
80 
81   return obj->GetStringValue().str();
82 }
83 
84 StructuredData::DictionarySP ScriptedThreadPythonInterface::GetStopReason() {
85   Status error;
86   StructuredData::DictionarySP dict =
87       Dispatch<StructuredData::DictionarySP>("get_stop_reason", error);
88 
89   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, error))
90     return {};
91 
92   return dict;
93 }
94 
95 StructuredData::ArraySP ScriptedThreadPythonInterface::GetStackFrames() {
96   Status error;
97   StructuredData::ArraySP arr =
98       Dispatch<StructuredData::ArraySP>("get_stackframes", error);
99 
100   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr, error))
101     return {};
102 
103   return arr;
104 }
105 
106 StructuredData::DictionarySP ScriptedThreadPythonInterface::GetRegisterInfo() {
107   Status error;
108   StructuredData::DictionarySP dict =
109       Dispatch<StructuredData::DictionarySP>("get_register_info", error);
110 
111   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict, error))
112     return {};
113 
114   return dict;
115 }
116 
117 std::optional<std::string> ScriptedThreadPythonInterface::GetRegisterContext() {
118   Status error;
119   StructuredData::ObjectSP obj = Dispatch("get_register_context", error);
120 
121   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj, error))
122     return {};
123 
124   return obj->GetAsString()->GetValue().str();
125 }
126 
127 StructuredData::ArraySP ScriptedThreadPythonInterface::GetExtendedInfo() {
128   Status error;
129   StructuredData::ArraySP arr =
130       Dispatch<StructuredData::ArraySP>("get_extended_info", error);
131 
132   if (!CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr, error))
133     return {};
134 
135   return arr;
136 }
137 
138 #endif
139