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/Target/Thread.h"
11#include "lldb/Utility/Log.h"
13
14// LLDB Python header must be included first
15#include "../lldb-python.h"
16
17#include "../SWIGPythonBridge.h"
20#include <optional>
21
22using namespace lldb;
23using namespace lldb_private;
24using namespace lldb_private::python;
26
30
32 llvm::StringRef class_name, lldb::ThreadSP thread_sp) {
33 // If there is any issue with this method, we will just assume it also applies
34 // to this thread which is the default behavior.
35 constexpr bool fail_value = true;
38 CallStaticMethod(class_name, "applies_to_thread", error, thread_sp);
39 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
40 error))
41 return fail_value;
42
43 return obj->GetBooleanValue(fail_value);
44}
45
46llvm::Expected<StructuredData::GenericSP>
48 const llvm::StringRef class_name, lldb::StackFrameListSP input_frames,
50 if (!input_frames)
51 return llvm::createStringError("invalid frame list");
52
53 StructuredDataImpl sd_impl(args_sp);
54 return ScriptedPythonInterface::CreatePluginObject(class_name, nullptr,
55 input_frames, sd_impl);
56}
57
59 llvm::StringRef class_name) {
62 CallStaticMethod(class_name, "get_description", error);
63 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
64 error))
65 return {};
66
67 return obj->GetStringValue().str();
68}
69
70std::optional<uint32_t>
74 CallStaticMethod(class_name, "get_priority", error);
75
76 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
77 error))
78 return std::nullopt;
79
80 // Try to extract as unsigned integer. Return nullopt if Python returned None
81 // or if extraction fails.
82 if (StructuredData::UnsignedInteger *int_obj = obj->GetAsUnsignedInteger())
83 return static_cast<uint32_t>(int_obj->GetValue());
84
85 return std::nullopt;
86}
87
91 StructuredData::ObjectSP obj = Dispatch("get_frame_at_index", error, index);
92
93 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
94 error))
95 return {};
96
97 return obj;
98}
99
102 if (language != eScriptLanguagePython)
103 return false;
104
105 return true;
106}
107
109 const std::vector<llvm::StringRef> ci_usages = {
110 "target frame-provider register -C <script-name> [-k key -v value ...]",
111 "target frame-provider list",
112 "target frame-provider remove <provider-name>",
113 "target frame-provider clear"};
114 const std::vector<llvm::StringRef> api_usages = {
115 "SBTarget.RegisterScriptedFrameProvider",
116 "SBTarget.RemoveScriptedFrameProvider",
117 "SBTarget.ClearScriptedFrameProvider"};
120 llvm::StringRef("Provide scripted stack frames for threads"),
121 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
122}
123
static llvm::raw_ostream & error(Stream &strm)
ScriptInterpreterPythonImpl::Locker Locker
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
StructuredData::ObjectSP GetFrameAtIndex(uint32_t index) override
ScriptedFrameProviderPythonInterface(ScriptInterpreterPythonImpl &interpreter)
std::optional< uint32_t > GetPriority(llvm::StringRef class_name) override
Get the priority of this frame provider.
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(llvm::StringRef class_name, lldb::StackFrameListSP input_frames, StructuredData::DictionarySP args_sp) override
bool AppliesToThread(llvm::StringRef class_name, lldb::ThreadSP thread_sp) override
std::string GetDescription(llvm::StringRef class_name) override
Get a description string for the frame provider.
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(llvm::StringRef class_name, StructuredData::Generic *script_obj, Args... args)
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
T CallStaticMethod(llvm::StringRef class_name, llvm::StringRef method_name, Status &error, Args &&...args)
Call a static method on a Python class without creating an instance.
T Dispatch(llvm::StringRef method_name, Status &error, Args &&...args)
An error handling class.
Definition Status.h:118
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