LLDB mainline
ScriptedFrameProviderPythonInterface.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "lldb/Host/Config.h"
11#include "lldb/Target/Thread.h"
12#include "lldb/Utility/Log.h"
14
15#if LLDB_ENABLE_PYTHON
16
17// LLDB Python header must be included first
18#include "../lldb-python.h"
19
20#include "../SWIGPythonBridge.h"
23#include <optional>
24
25using namespace lldb;
26using namespace lldb_private;
27using namespace lldb_private::python;
28using Locker = ScriptInterpreterPythonImpl::Locker;
29
30ScriptedFrameProviderPythonInterface::ScriptedFrameProviderPythonInterface(
31 ScriptInterpreterPythonImpl &interpreter)
32 : ScriptedFrameProviderInterface(), ScriptedPythonInterface(interpreter) {}
33
34bool ScriptedFrameProviderPythonInterface::AppliesToThread(
35 llvm::StringRef class_name, lldb::ThreadSP thread_sp) {
36 // If there is any issue with this method, we will just assume it also applies
37 // to this thread which is the default behavior.
38 constexpr bool fail_value = true;
41 CallStaticMethod(class_name, "applies_to_thread", error, thread_sp);
42 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
43 error))
44 return fail_value;
45
46 return obj->GetBooleanValue(fail_value);
47}
48
49llvm::Expected<StructuredData::GenericSP>
50ScriptedFrameProviderPythonInterface::CreatePluginObject(
51 const llvm::StringRef class_name, lldb::StackFrameListSP input_frames,
53 if (!input_frames)
54 return llvm::createStringError("invalid frame list");
55
56 StructuredDataImpl sd_impl(args_sp);
57 return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
58 input_frames, sd_impl);
59}
60
61std::string ScriptedFrameProviderPythonInterface::GetDescription(
62 llvm::StringRef class_name) {
65 CallStaticMethod(class_name, "get_description", error);
66 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
67 error))
68 return {};
69
70 return obj->GetStringValue().str();
71}
72
73std::optional<uint32_t>
74ScriptedFrameProviderPythonInterface::GetPriority(llvm::StringRef class_name) {
77 CallStaticMethod(class_name, "get_priority", error);
78
79 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
80 error))
81 return std::nullopt;
82
83 // Try to extract as unsigned integer. Return nullopt if Python returned None
84 // or if extraction fails.
85 if (StructuredData::UnsignedInteger *int_obj = obj->GetAsUnsignedInteger())
86 return static_cast<uint32_t>(int_obj->GetValue());
87
88 return std::nullopt;
89}
90
92ScriptedFrameProviderPythonInterface::GetFrameAtIndex(uint32_t index) {
94 StructuredData::ObjectSP obj = Dispatch("get_frame_at_index", error, index);
95
96 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
97 error))
98 return {};
99
100 return obj;
101}
102
103bool ScriptedFrameProviderPythonInterface::CreateInstance(
105 if (language != eScriptLanguagePython)
106 return false;
107
108 return true;
109}
110
111void ScriptedFrameProviderPythonInterface::Initialize() {
112 const std::vector<llvm::StringRef> ci_usages = {
113 "target frame-provider register -C <script-name> [-k key -v value ...]",
114 "target frame-provider list",
115 "target frame-provider remove <provider-name>",
116 "target frame-provider clear"};
117 const std::vector<llvm::StringRef> api_usages = {
118 "SBTarget.RegisterScriptedFrameProvider",
119 "SBTarget.RemoveScriptedFrameProvider",
120 "SBTarget.ClearScriptedFrameProvider"};
122 GetPluginNameStatic(),
123 llvm::StringRef("Provide scripted stack frames for threads"),
124 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
125}
126
127void ScriptedFrameProviderPythonInterface::Terminate() {
128 PluginManager::UnregisterPlugin(CreateInstance);
129}
130
131#endif
static llvm::raw_ostream & error(Stream &strm)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
std::shared_ptr< Dictionary > DictionarySP
std::shared_ptr< Object > ObjectSP
Integer< uint64_t > UnsignedInteger
A class that represents a running process on the host machine.
ScriptLanguage
Script interpreter types.
@ eScriptLanguagePython
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP