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