15f757f3fSDimitry Andric //===-- ScriptedThreadPythonInterface.cpp ---------------------------------===//
25f757f3fSDimitry Andric //
35f757f3fSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f757f3fSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f757f3fSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f757f3fSDimitry Andric //
75f757f3fSDimitry Andric //===----------------------------------------------------------------------===//
85f757f3fSDimitry Andric
95f757f3fSDimitry Andric #include "lldb/Host/Config.h"
10*0fca6ea1SDimitry Andric #include "lldb/Target/ExecutionContext.h"
115f757f3fSDimitry Andric #include "lldb/Utility/Log.h"
125f757f3fSDimitry Andric #include "lldb/lldb-enumerations.h"
135f757f3fSDimitry Andric
145f757f3fSDimitry Andric #if LLDB_ENABLE_PYTHON
155f757f3fSDimitry Andric
165f757f3fSDimitry Andric // LLDB Python header must be included first
175f757f3fSDimitry Andric #include "../lldb-python.h"
185f757f3fSDimitry Andric
195f757f3fSDimitry Andric #include "../SWIGPythonBridge.h"
205f757f3fSDimitry Andric #include "../ScriptInterpreterPythonImpl.h"
215f757f3fSDimitry Andric #include "ScriptedThreadPythonInterface.h"
225f757f3fSDimitry Andric #include <optional>
235f757f3fSDimitry Andric
245f757f3fSDimitry Andric using namespace lldb;
255f757f3fSDimitry Andric using namespace lldb_private;
265f757f3fSDimitry Andric using namespace lldb_private::python;
275f757f3fSDimitry Andric using Locker = ScriptInterpreterPythonImpl::Locker;
285f757f3fSDimitry Andric
ScriptedThreadPythonInterface(ScriptInterpreterPythonImpl & interpreter)295f757f3fSDimitry Andric ScriptedThreadPythonInterface::ScriptedThreadPythonInterface(
305f757f3fSDimitry Andric ScriptInterpreterPythonImpl &interpreter)
315f757f3fSDimitry Andric : ScriptedThreadInterface(), ScriptedPythonInterface(interpreter) {}
325f757f3fSDimitry Andric
335f757f3fSDimitry Andric llvm::Expected<StructuredData::GenericSP>
CreatePluginObject(const llvm::StringRef class_name,ExecutionContext & exe_ctx,StructuredData::DictionarySP args_sp,StructuredData::Generic * script_obj)345f757f3fSDimitry Andric ScriptedThreadPythonInterface::CreatePluginObject(
355f757f3fSDimitry Andric const llvm::StringRef class_name, ExecutionContext &exe_ctx,
365f757f3fSDimitry Andric StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
375f757f3fSDimitry Andric ExecutionContextRefSP exe_ctx_ref_sp =
385f757f3fSDimitry Andric std::make_shared<ExecutionContextRef>(exe_ctx);
395f757f3fSDimitry Andric StructuredDataImpl sd_impl(args_sp);
405f757f3fSDimitry Andric return ScriptedPythonInterface::CreatePluginObject(class_name, script_obj,
415f757f3fSDimitry Andric exe_ctx_ref_sp, sd_impl);
425f757f3fSDimitry Andric }
435f757f3fSDimitry Andric
GetThreadID()445f757f3fSDimitry Andric lldb::tid_t ScriptedThreadPythonInterface::GetThreadID() {
455f757f3fSDimitry Andric Status error;
465f757f3fSDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_thread_id", error);
475f757f3fSDimitry Andric
48*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
49*0fca6ea1SDimitry Andric error))
505f757f3fSDimitry Andric return LLDB_INVALID_THREAD_ID;
515f757f3fSDimitry Andric
525f757f3fSDimitry Andric return obj->GetUnsignedIntegerValue(LLDB_INVALID_THREAD_ID);
535f757f3fSDimitry Andric }
545f757f3fSDimitry Andric
GetName()555f757f3fSDimitry Andric std::optional<std::string> ScriptedThreadPythonInterface::GetName() {
565f757f3fSDimitry Andric Status error;
575f757f3fSDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_name", error);
585f757f3fSDimitry Andric
59*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
60*0fca6ea1SDimitry Andric error))
615f757f3fSDimitry Andric return {};
625f757f3fSDimitry Andric
635f757f3fSDimitry Andric return obj->GetStringValue().str();
645f757f3fSDimitry Andric }
655f757f3fSDimitry Andric
GetState()665f757f3fSDimitry Andric lldb::StateType ScriptedThreadPythonInterface::GetState() {
675f757f3fSDimitry Andric Status error;
685f757f3fSDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_state", error);
695f757f3fSDimitry Andric
70*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
71*0fca6ea1SDimitry Andric error))
725f757f3fSDimitry Andric return eStateInvalid;
735f757f3fSDimitry Andric
745f757f3fSDimitry Andric return static_cast<StateType>(obj->GetUnsignedIntegerValue(eStateInvalid));
755f757f3fSDimitry Andric }
765f757f3fSDimitry Andric
GetQueue()775f757f3fSDimitry Andric std::optional<std::string> ScriptedThreadPythonInterface::GetQueue() {
785f757f3fSDimitry Andric Status error;
795f757f3fSDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_queue", error);
805f757f3fSDimitry Andric
81*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
82*0fca6ea1SDimitry Andric error))
835f757f3fSDimitry Andric return {};
845f757f3fSDimitry Andric
855f757f3fSDimitry Andric return obj->GetStringValue().str();
865f757f3fSDimitry Andric }
875f757f3fSDimitry Andric
GetStopReason()885f757f3fSDimitry Andric StructuredData::DictionarySP ScriptedThreadPythonInterface::GetStopReason() {
895f757f3fSDimitry Andric Status error;
905f757f3fSDimitry Andric StructuredData::DictionarySP dict =
915f757f3fSDimitry Andric Dispatch<StructuredData::DictionarySP>("get_stop_reason", error);
925f757f3fSDimitry Andric
93*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
94*0fca6ea1SDimitry Andric error))
955f757f3fSDimitry Andric return {};
965f757f3fSDimitry Andric
975f757f3fSDimitry Andric return dict;
985f757f3fSDimitry Andric }
995f757f3fSDimitry Andric
GetStackFrames()1005f757f3fSDimitry Andric StructuredData::ArraySP ScriptedThreadPythonInterface::GetStackFrames() {
1015f757f3fSDimitry Andric Status error;
1025f757f3fSDimitry Andric StructuredData::ArraySP arr =
1035f757f3fSDimitry Andric Dispatch<StructuredData::ArraySP>("get_stackframes", error);
1045f757f3fSDimitry Andric
105*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr,
106*0fca6ea1SDimitry Andric error))
1075f757f3fSDimitry Andric return {};
1085f757f3fSDimitry Andric
1095f757f3fSDimitry Andric return arr;
1105f757f3fSDimitry Andric }
1115f757f3fSDimitry Andric
GetRegisterInfo()1125f757f3fSDimitry Andric StructuredData::DictionarySP ScriptedThreadPythonInterface::GetRegisterInfo() {
1135f757f3fSDimitry Andric Status error;
1145f757f3fSDimitry Andric StructuredData::DictionarySP dict =
1155f757f3fSDimitry Andric Dispatch<StructuredData::DictionarySP>("get_register_info", error);
1165f757f3fSDimitry Andric
117*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
118*0fca6ea1SDimitry Andric error))
1195f757f3fSDimitry Andric return {};
1205f757f3fSDimitry Andric
1215f757f3fSDimitry Andric return dict;
1225f757f3fSDimitry Andric }
1235f757f3fSDimitry Andric
GetRegisterContext()1245f757f3fSDimitry Andric std::optional<std::string> ScriptedThreadPythonInterface::GetRegisterContext() {
1255f757f3fSDimitry Andric Status error;
1265f757f3fSDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_register_context", error);
1275f757f3fSDimitry Andric
128*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
129*0fca6ea1SDimitry Andric error))
1305f757f3fSDimitry Andric return {};
1315f757f3fSDimitry Andric
1325f757f3fSDimitry Andric return obj->GetAsString()->GetValue().str();
1335f757f3fSDimitry Andric }
1345f757f3fSDimitry Andric
GetExtendedInfo()1355f757f3fSDimitry Andric StructuredData::ArraySP ScriptedThreadPythonInterface::GetExtendedInfo() {
1365f757f3fSDimitry Andric Status error;
1375f757f3fSDimitry Andric StructuredData::ArraySP arr =
1385f757f3fSDimitry Andric Dispatch<StructuredData::ArraySP>("get_extended_info", error);
1395f757f3fSDimitry Andric
140*0fca6ea1SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr,
141*0fca6ea1SDimitry Andric error))
1425f757f3fSDimitry Andric return {};
1435f757f3fSDimitry Andric
1445f757f3fSDimitry Andric return arr;
1455f757f3fSDimitry Andric }
1465f757f3fSDimitry Andric
1475f757f3fSDimitry Andric #endif
148