1b1c73532SDimitry Andric //===-- ScriptedThreadPythonInterface.cpp ---------------------------------===//
2b1c73532SDimitry Andric //
3b1c73532SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b1c73532SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5b1c73532SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b1c73532SDimitry Andric //
7b1c73532SDimitry Andric //===----------------------------------------------------------------------===//
8b1c73532SDimitry Andric
9b1c73532SDimitry Andric #include "lldb/Host/Config.h"
10b1c73532SDimitry Andric #include "lldb/Target/ExecutionContext.h"
11b1c73532SDimitry Andric #include "lldb/Utility/Log.h"
12b1c73532SDimitry Andric #include "lldb/lldb-enumerations.h"
13b1c73532SDimitry Andric
14b1c73532SDimitry Andric #if LLDB_ENABLE_PYTHON
15b1c73532SDimitry Andric
16b1c73532SDimitry Andric // LLDB Python header must be included first
17b1c73532SDimitry Andric #include "../lldb-python.h"
18b1c73532SDimitry Andric
19b1c73532SDimitry Andric #include "../SWIGPythonBridge.h"
20b1c73532SDimitry Andric #include "../ScriptInterpreterPythonImpl.h"
21b1c73532SDimitry Andric #include "OperatingSystemPythonInterface.h"
22b1c73532SDimitry Andric
23b1c73532SDimitry Andric using namespace lldb;
24b1c73532SDimitry Andric using namespace lldb_private;
25b1c73532SDimitry Andric using namespace lldb_private::python;
26b1c73532SDimitry Andric using Locker = ScriptInterpreterPythonImpl::Locker;
27b1c73532SDimitry Andric
OperatingSystemPythonInterface(ScriptInterpreterPythonImpl & interpreter)28b1c73532SDimitry Andric OperatingSystemPythonInterface::OperatingSystemPythonInterface(
29b1c73532SDimitry Andric ScriptInterpreterPythonImpl &interpreter)
30b1c73532SDimitry Andric : OperatingSystemInterface(), ScriptedThreadPythonInterface(interpreter) {}
31b1c73532SDimitry Andric
32b1c73532SDimitry Andric llvm::Expected<StructuredData::GenericSP>
CreatePluginObject(llvm::StringRef class_name,ExecutionContext & exe_ctx,StructuredData::DictionarySP args_sp,StructuredData::Generic * script_obj)33b1c73532SDimitry Andric OperatingSystemPythonInterface::CreatePluginObject(
34b1c73532SDimitry Andric llvm::StringRef class_name, ExecutionContext &exe_ctx,
35b1c73532SDimitry Andric StructuredData::DictionarySP args_sp, StructuredData::Generic *script_obj) {
36b1c73532SDimitry Andric return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
37b1c73532SDimitry Andric exe_ctx.GetProcessSP());
38b1c73532SDimitry Andric }
39b1c73532SDimitry Andric
40b1c73532SDimitry Andric StructuredData::DictionarySP
CreateThread(lldb::tid_t tid,lldb::addr_t context)41b1c73532SDimitry Andric OperatingSystemPythonInterface::CreateThread(lldb::tid_t tid,
42b1c73532SDimitry Andric lldb::addr_t context) {
43b1c73532SDimitry Andric Status error;
44b1c73532SDimitry Andric StructuredData::DictionarySP dict = Dispatch<StructuredData::DictionarySP>(
45b1c73532SDimitry Andric "create_thread", error, tid, context);
46b1c73532SDimitry Andric
47b1c73532SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, dict,
48b1c73532SDimitry Andric error))
49b1c73532SDimitry Andric return {};
50b1c73532SDimitry Andric
51b1c73532SDimitry Andric return dict;
52b1c73532SDimitry Andric }
53b1c73532SDimitry Andric
GetThreadInfo()54b1c73532SDimitry Andric StructuredData::ArraySP OperatingSystemPythonInterface::GetThreadInfo() {
55b1c73532SDimitry Andric Status error;
56b1c73532SDimitry Andric StructuredData::ArraySP arr =
57b1c73532SDimitry Andric Dispatch<StructuredData::ArraySP>("get_thread_info", error);
58b1c73532SDimitry Andric
59b1c73532SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, arr,
60b1c73532SDimitry Andric error))
61b1c73532SDimitry Andric return {};
62b1c73532SDimitry Andric
63b1c73532SDimitry Andric return arr;
64b1c73532SDimitry Andric }
65b1c73532SDimitry Andric
GetRegisterInfo()66b1c73532SDimitry Andric StructuredData::DictionarySP OperatingSystemPythonInterface::GetRegisterInfo() {
67b1c73532SDimitry Andric return ScriptedThreadPythonInterface::GetRegisterInfo();
68b1c73532SDimitry Andric }
69b1c73532SDimitry Andric
70b1c73532SDimitry Andric std::optional<std::string>
GetRegisterContextForTID(lldb::tid_t tid)71b1c73532SDimitry Andric OperatingSystemPythonInterface::GetRegisterContextForTID(lldb::tid_t tid) {
72b1c73532SDimitry Andric Status error;
73b1c73532SDimitry Andric StructuredData::ObjectSP obj = Dispatch("get_register_data", error, tid);
74b1c73532SDimitry Andric
75b1c73532SDimitry Andric if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
76b1c73532SDimitry Andric error))
77b1c73532SDimitry Andric return {};
78b1c73532SDimitry Andric
79b1c73532SDimitry Andric return obj->GetAsString()->GetValue().str();
80b1c73532SDimitry Andric }
81b1c73532SDimitry Andric
82b1c73532SDimitry Andric #endif
83