LLDB mainline
ScriptedFrameProvider.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
11#include "lldb/Core/Debugger.h"
16#include "lldb/Target/Process.h"
18#include "lldb/Target/Thread.h"
20#include "lldb/Utility/Status.h"
21#include "llvm/Support/Error.h"
22#include <cstdint>
23
24using namespace lldb;
25using namespace lldb_private;
26
29 "Provides synthetic frames via scripting",
31}
32
36
37llvm::Expected<lldb::SyntheticFrameProviderSP>
39 lldb::StackFrameListSP input_frames,
40 const ScriptedFrameProviderDescriptor &descriptor) {
41 if (!input_frames)
42 return llvm::createStringError(
43 "failed to create scripted frame provider: invalid input frames");
44
45 Thread &thread = input_frames->GetThread();
46 ProcessSP process_sp = thread.GetProcess();
47 if (!process_sp)
48 return nullptr;
49
50 if (!descriptor.IsValid())
51 return llvm::createStringError(
52 "failed to create scripted frame provider: invalid scripted metadata");
53
54 if (!descriptor.AppliesToThread(thread))
55 return nullptr;
56
57 ScriptInterpreter *script_interp =
58 process_sp->GetTarget().GetDebugger().GetScriptInterpreter();
59 if (!script_interp)
60 return llvm::createStringError("cannot create scripted frame provider: No "
61 "script interpreter installed");
62
65 if (!interface_sp)
66 return llvm::createStringError(
67 "cannot create scripted frame provider: script interpreter couldn't "
68 "create Scripted Frame Provider Interface");
69
70 const ScriptedMetadataSP scripted_metadata = descriptor.scripted_metadata_sp;
71
72 // If we shouldn't attach a frame provider to this thread, just exit early.
73 if (!interface_sp->AppliesToThread(scripted_metadata->GetClassName(),
74 thread.shared_from_this()))
75 return nullptr;
76
77 auto obj_or_err = interface_sp->CreatePluginObject(
78 scripted_metadata->GetClassName(), input_frames,
79 scripted_metadata->GetArgsSP());
80 if (!obj_or_err)
81 return obj_or_err.takeError();
82
83 StructuredData::ObjectSP object_sp = *obj_or_err;
84 if (!object_sp || !object_sp->IsValid())
85 return llvm::createStringError(
86 "cannot create scripted frame provider: failed to create valid scripted"
87 "frame provider object");
88
89 return std::make_shared<ScriptedFrameProvider>(input_frames, interface_sp,
90 descriptor);
91}
92
99
101
103 if (!m_interface_sp)
104 return {};
105
106 return m_interface_sp->GetDescription(m_descriptor.GetName());
107}
108
109std::optional<uint32_t> ScriptedFrameProvider::GetPriority() const {
110 if (!m_interface_sp)
111 return std::nullopt;
112
113 return m_interface_sp->GetPriority(m_descriptor.GetName());
114}
115
116llvm::Expected<StackFrameSP>
118 if (!m_interface_sp)
119 return llvm::createStringError(
120 "cannot get stack frame: scripted frame provider not initialized");
121
122 auto create_frame_from_dict =
123 [this](StructuredData::Dictionary *dict,
124 uint32_t index) -> llvm::Expected<StackFrameSP> {
126 if (!dict->GetValueForKeyAsInteger("pc", pc))
127 return llvm::createStringError(
128 "missing 'pc' key from scripted frame dictionary");
129
130 Address symbol_addr;
131 symbol_addr.SetLoadAddress(pc, &GetThread().GetProcess()->GetTarget());
132
134 const bool cfa_is_valid = false;
135 const bool artificial = false;
136 const bool behaves_like_zeroth_frame = false;
137 SymbolContext sc;
138 symbol_addr.CalculateSymbolContext(&sc);
139
140 ThreadSP thread_sp = GetThread().shared_from_this();
141 return std::make_shared<StackFrame>(thread_sp, index, index, cfa,
142 cfa_is_valid, pc,
143 StackFrame::Kind::Synthetic, artificial,
144 behaves_like_zeroth_frame, &sc);
145 };
146
147 auto create_frame_from_script_object =
148 [this](
149 StructuredData::ObjectSP object_sp) -> llvm::Expected<StackFrameSP> {
151 if (!object_sp || !object_sp->GetAsGeneric())
152 return llvm::createStringError("invalid script object");
153
154 ThreadSP thread_sp = GetThread().shared_from_this();
155 auto frame_or_error = ScriptedFrame::Create(thread_sp, nullptr, nullptr,
156 object_sp->GetAsGeneric());
157
158 if (!frame_or_error) {
160 LLVM_PRETTY_FUNCTION, toString(frame_or_error.takeError()), error);
161 return error.ToError();
162 }
163
164 return *frame_or_error;
165 };
166
167 StructuredData::ObjectSP obj_sp = m_interface_sp->GetFrameAtIndex(idx);
168
169 // None/null means no more frames or error.
170 if (!obj_sp || !obj_sp->IsValid())
171 return llvm::createStringError("invalid script object returned for frame " +
172 llvm::Twine(idx));
173
174 StackFrameSP synth_frame_sp = nullptr;
176 obj_sp->GetAsUnsignedInteger()) {
177 uint32_t real_frame_index = int_obj->GetValue();
178 if (real_frame_index < m_input_frames->GetNumFrames()) {
179 StackFrameSP real_frame_sp =
180 m_input_frames->GetFrameAtIndex(real_frame_index);
181 synth_frame_sp =
182 (real_frame_index == idx)
183 ? real_frame_sp
184 : std::make_shared<BorrowedStackFrame>(real_frame_sp, idx);
185 }
186 } else if (StructuredData::Dictionary *dict = obj_sp->GetAsDictionary()) {
187 // Check if it's a dictionary describing a frame.
188 auto frame_from_dict_or_err = create_frame_from_dict(dict, idx);
189 if (!frame_from_dict_or_err) {
190 return llvm::createStringError(llvm::Twine(
191 "couldn't create frame from dictionary at index " + llvm::Twine(idx) +
192 ": " + toString(frame_from_dict_or_err.takeError())));
193 }
194 synth_frame_sp = *frame_from_dict_or_err;
195 } else if (obj_sp->GetAsGeneric()) {
196 // It's a ScriptedFrame object.
197 auto frame_from_script_obj_or_err = create_frame_from_script_object(obj_sp);
198 if (!frame_from_script_obj_or_err) {
199 return llvm::createStringError(
200 llvm::Twine("couldn't create frame from script object at index " +
201 llvm::Twine(idx) + ": " +
202 toString(frame_from_script_obj_or_err.takeError())));
203 }
204 synth_frame_sp = *frame_from_script_obj_or_err;
205 } else {
206 return llvm::createStringError(
207 llvm::Twine("invalid return type from get_frame_at_index at index " +
208 llvm::Twine(idx)));
209 }
210
211 if (!synth_frame_sp)
212 return llvm::createStringError(
213 llvm::Twine("failed to create frame at index " + llvm::Twine(idx)));
214
215 synth_frame_sp->SetFrameIndex(idx);
216
217 return synth_frame_sp;
218}
219
220namespace lldb_private {
224
228} // namespace lldb_private
static llvm::raw_ostream & error(Stream &strm)
A section + offset based address class.
Definition Address.h:62
bool SetLoadAddress(lldb::addr_t load_addr, Target *target, bool allow_section_end=false)
Set the address to represent load_addr.
Definition Address.cpp:1035
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition Address.cpp:820
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
virtual lldb::ScriptedFrameProviderInterfaceSP CreateScriptedFrameProviderInterface()
lldb::ScriptedFrameProviderInterfaceSP m_interface_sp
static llvm::StringRef GetPluginNameStatic()
llvm::Expected< lldb::StackFrameSP > GetFrameAtIndex(uint32_t idx) override
Get a single stack frame at the specified index.
static llvm::Expected< lldb::SyntheticFrameProviderSP > CreateInstance(lldb::StackFrameListSP input_frames, const ScriptedFrameProviderDescriptor &descriptor)
ScriptedFrameProvider(lldb::StackFrameListSP input_frames, lldb::ScriptedFrameProviderInterfaceSP interface_sp, const ScriptedFrameProviderDescriptor &descriptor)
const ScriptedFrameProviderDescriptor & m_descriptor
std::optional< uint32_t > GetPriority() const override
Get the priority of this frame provider.
std::string GetDescription() const override
static llvm::Expected< std::shared_ptr< ScriptedFrame > > Create(lldb::ThreadSP thread_sp, lldb::ScriptedThreadInterfaceSP scripted_thread_interface_sp, StructuredData::DictionarySP args_sp, StructuredData::Generic *script_object=nullptr)
Create a ScriptedFrame from a object instanciated in the script interpreter.
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef error_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
@ Synthetic
An synthetic stack frame (e.g.
Definition StackFrame.h:72
An error handling class.
Definition Status.h:118
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const
std::shared_ptr< Object > ObjectSP
Integer< uint64_t > UnsignedInteger
Defines a symbol context baton that can be handed other debug core functions.
Thread & GetThread()
Get the thread associated with this provider.
SyntheticFrameProvider(lldb::StackFrameListSP input_frames)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
void lldb_terminate_ScriptedFrameProvider()
void lldb_initialize_ScriptedFrameProvider()
const char * toString(AppleArm64ExceptionClass EC)
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ScriptedMetadata > ScriptedMetadataSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::ScriptedFrameProviderInterface > ScriptedFrameProviderInterfaceSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
This struct contains the metadata needed to instantiate a frame provider and optional filters to cont...
lldb::ScriptedMetadataSP scripted_metadata_sp
Metadata for instantiating the provider (e.g. script class name and args).
bool AppliesToThread(Thread &thread) const
Check if this descriptor applies to the given thread.
bool IsValid() const
Check if this descriptor has valid metadata for script-based providers.