1 //===-- ScriptedThreadPythonInterface.cpp ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/Config.h"
10 #include "lldb/Target/ExecutionContext.h"
11 #include "lldb/Utility/Log.h"
12 #include "lldb/lldb-enumerations.h"
14 #if LLDB_ENABLE_PYTHON
16 // LLDB Python header must be included first
17 #include "../lldb-python.h"
19 #include "../SWIGPythonBridge.h"
20 #include "../ScriptInterpreterPythonImpl.h"
21 #include "ScriptedThreadPythonInterface.h"
25 using namespace lldb_private
;
26 using namespace lldb_private::python
;
27 using Locker
= ScriptInterpreterPythonImpl::Locker
;
29 ScriptedThreadPythonInterface::ScriptedThreadPythonInterface(
30 ScriptInterpreterPythonImpl
&interpreter
)
31 : ScriptedThreadInterface(), ScriptedPythonInterface(interpreter
) {}
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
);
44 lldb::tid_t
ScriptedThreadPythonInterface::GetThreadID() {
46 StructuredData::ObjectSP obj
= Dispatch("get_thread_id", error
);
48 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, obj
,
50 return LLDB_INVALID_THREAD_ID
;
52 return obj
->GetUnsignedIntegerValue(LLDB_INVALID_THREAD_ID
);
55 std::optional
<std::string
> ScriptedThreadPythonInterface::GetName() {
57 StructuredData::ObjectSP obj
= Dispatch("get_name", error
);
59 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, obj
,
63 return obj
->GetStringValue().str();
66 lldb::StateType
ScriptedThreadPythonInterface::GetState() {
68 StructuredData::ObjectSP obj
= Dispatch("get_state", error
);
70 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, obj
,
74 return static_cast<StateType
>(obj
->GetUnsignedIntegerValue(eStateInvalid
));
77 std::optional
<std::string
> ScriptedThreadPythonInterface::GetQueue() {
79 StructuredData::ObjectSP obj
= Dispatch("get_queue", error
);
81 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, obj
,
85 return obj
->GetStringValue().str();
88 StructuredData::DictionarySP
ScriptedThreadPythonInterface::GetStopReason() {
90 StructuredData::DictionarySP dict
=
91 Dispatch
<StructuredData::DictionarySP
>("get_stop_reason", error
);
93 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, dict
,
100 StructuredData::ArraySP
ScriptedThreadPythonInterface::GetStackFrames() {
102 StructuredData::ArraySP arr
=
103 Dispatch
<StructuredData::ArraySP
>("get_stackframes", error
);
105 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, arr
,
112 StructuredData::DictionarySP
ScriptedThreadPythonInterface::GetRegisterInfo() {
114 StructuredData::DictionarySP dict
=
115 Dispatch
<StructuredData::DictionarySP
>("get_register_info", error
);
117 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, dict
,
124 std::optional
<std::string
> ScriptedThreadPythonInterface::GetRegisterContext() {
126 StructuredData::ObjectSP obj
= Dispatch("get_register_context", error
);
128 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, obj
,
132 return obj
->GetAsString()->GetValue().str();
135 StructuredData::ArraySP
ScriptedThreadPythonInterface::GetExtendedInfo() {
137 StructuredData::ArraySP arr
=
138 Dispatch
<StructuredData::ArraySP
>("get_extended_info", error
);
140 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION
, arr
,