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
74ScriptedFrameProviderPythonInterface::GetFrameAtIndex(uint32_t index) {
76 StructuredData::ObjectSP obj = Dispatch("get_frame_at_index", error, index);
77
78 if (!ScriptedInterface::CheckStructuredDataObject(LLVM_PRETTY_FUNCTION, obj,
79 error))
80 return {};
81
82 return obj;
83}
84
85bool ScriptedFrameProviderPythonInterface::CreateInstance(
87 if (language != eScriptLanguagePython)
88 return false;
89
90 return true;
91}
92
93void ScriptedFrameProviderPythonInterface::Initialize() {
94 const std::vector<llvm::StringRef> ci_usages = {
95 "target frame-provider register -C <script-name> [-k key -v value ...]",
96 "target frame-provider list",
97 "target frame-provider remove <provider-name>",
98 "target frame-provider clear"};
99 const std::vector<llvm::StringRef> api_usages = {
100 "SBTarget.RegisterScriptedFrameProvider",
101 "SBTarget.RemoveScriptedFrameProvider",
102 "SBTarget.ClearScriptedFrameProvider"};
104 GetPluginNameStatic(),
105 llvm::StringRef("Provide scripted stack frames for threads"),
106 CreateInstance, eScriptLanguagePython, {ci_usages, api_usages});
107}
108
109void ScriptedFrameProviderPythonInterface::Terminate() {
110 PluginManager::UnregisterPlugin(CreateInstance);
111}
112
113#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
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