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
9#include "../lldb-python.h"
10
12#include "lldb/Target/Thread.h"
13#include "lldb/Utility/Log.h"
15
16#include "../SWIGPythonBridge.h"
19#include <optional>
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::python;
25
29
31 llvm::StringRef class_name, lldb::ThreadSP thread_sp) {
32 // If there is any issue with this method, we will just assume it also applies
33 // to this thread which is the default behavior.
34 constexpr bool fail_value = true;
36 CallStaticMethod(class_name, "applies_to_thread", thread_sp),
37 LLVM_PRETTY_FUNCTION);
38 if (!obj)
39 return fail_value;
40
41 return obj->GetBooleanValue(fail_value);
42}
43
44llvm::Expected<StructuredData::GenericSP>
46 const ScriptedMetadata &scripted_metadata,
47 lldb::StackFrameListSP input_frames) {
48 if (!input_frames)
49 return llvm::createStringError("invalid frame list");
50
52 scripted_metadata, nullptr, input_frames, scripted_metadata.GetArgsSP());
53}
54
56 llvm::StringRef class_name) {
58 CallStaticMethod(class_name, "get_description"), LLVM_PRETTY_FUNCTION);
59 if (!obj)
60 return {};
61
62 return obj->GetStringValue().str();
63}
64
65std::optional<uint32_t>
68 CallStaticMethod(class_name, "get_priority"), LLVM_PRETTY_FUNCTION);
69 if (!obj)
70 return std::nullopt;
71
72 // Try to extract as unsigned integer. Return nullopt if Python returned None
73 // or if extraction fails.
74 if (StructuredData::UnsignedInteger *int_obj = obj->GetAsUnsignedInteger())
75 return static_cast<uint32_t>(int_obj->GetValue());
76
77 return std::nullopt;
78}
79
83 Dispatch("get_frame_at_index", index), LLVM_PRETTY_FUNCTION);
84 if (!obj)
85 return {};
86
87 return obj;
88}
89
92 if (language != eScriptLanguagePython)
93 return false;
94
95 return true;
96}
97
99 const std::vector<llvm::StringRef> ci_usages = {
100 "target frame-provider register -C <script-name> [-k key -v value ...]",
101 "target frame-provider list",
102 "target frame-provider remove <provider-name>",
103 "target frame-provider clear"};
104 const std::vector<llvm::StringRef> api_usages = {
105 "SBTarget.RegisterScriptedFrameProvider",
106 "SBTarget.RemoveScriptedFrameProvider",
107 "SBTarget.ClearScriptedFrameProvider"};
110 llvm::StringRef("Provide scripted stack frames for threads"),
112 eScriptLanguagePython, {ci_usages, api_usages});
113}
114
ScriptInterpreterPythonImpl::Locker Locker
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, lldb::StackFrameListSP input_frames) override
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.
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.
StructuredData::DictionarySP GetArgsSP() const
static T LogAndDefault(llvm::Expected< T > value_or_err, llvm::StringRef caller)
Log the failure in value_or_err and fall back to a default-constructed T.
ScriptedPythonInterface(ScriptInterpreterPythonImpl &interpreter)
llvm::Expected< T > CallStaticMethod(llvm::StringRef class_name, llvm::StringRef method_name, Args &&...args)
Call a static method on a Python class without creating an instance.
llvm::Expected< T > Dispatch(llvm::StringRef method_name, Args &&...args)
Call method_name on the scripted object.
llvm::Expected< StructuredData::GenericSP > CreatePluginObject(const ScriptedMetadata &scripted_metadata, StructuredData::Generic *script_obj, Args... args)
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
@ eScriptedExtensionScriptedFrameProvider
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP