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"
15#include "lldb/Target/Process.h"
17#include "lldb/Target/Thread.h"
19#include "lldb/Utility/Status.h"
20#include "llvm/Support/Error.h"
21#include <cstdint>
22
23using namespace lldb;
24using namespace lldb_private;
25
28 "Provides synthetic frames via scripting",
30}
31
35
36llvm::Expected<lldb::SyntheticFrameProviderSP>
38 lldb::StackFrameListSP input_frames,
39 const ScriptedFrameProviderDescriptor &descriptor) {
40 if (!input_frames)
41 return llvm::createStringError(
42 "failed to create scripted frame provider: invalid input frames");
43
44 Thread &thread = input_frames->GetThread();
45 ProcessSP process_sp = thread.GetProcess();
46 if (!process_sp)
47 return nullptr;
48
49 if (!descriptor.IsValid())
50 return llvm::createStringError(
51 "failed to create scripted frame provider: invalid scripted metadata");
52
53 if (!descriptor.AppliesToThread(thread))
54 return nullptr;
55
56 ScriptInterpreter *script_interp =
57 process_sp->GetTarget().GetDebugger().GetScriptInterpreter();
58 if (!script_interp)
59 return llvm::createStringError("cannot create scripted frame provider: No "
60 "script interpreter installed");
61
64 if (!interface_sp)
65 return llvm::createStringError(
66 "cannot create scripted frame provider: script interpreter couldn't "
67 "create Scripted Frame Provider Interface");
68
69 const ScriptedMetadataSP scripted_metadata = descriptor.scripted_metadata_sp;
70
71 // If we shouldn't attach a frame provider to this thread, just exit early.
72 if (!interface_sp->AppliesToThread(scripted_metadata->GetClassName(),
73 thread.shared_from_this()))
74 return nullptr;
75
76 auto obj_or_err = interface_sp->CreatePluginObject(
77 scripted_metadata->GetClassName(), input_frames,
78 scripted_metadata->GetArgsSP());
79 if (!obj_or_err)
80 return obj_or_err.takeError();
81
82 StructuredData::ObjectSP object_sp = *obj_or_err;
83 if (!object_sp || !object_sp->IsValid())
84 return llvm::createStringError(
85 "cannot create scripted frame provider: failed to create valid scripted"
86 "frame provider object");
87
88 return std::make_shared<ScriptedFrameProvider>(input_frames, interface_sp,
89 descriptor);
90}
91
98
100
102 if (!m_interface_sp)
103 return {};
104
105 return m_interface_sp->GetDescription(m_descriptor.GetName());
106}
107
108llvm::Expected<StackFrameSP>
110 if (!m_interface_sp)
111 return llvm::createStringError(
112 "cannot get stack frame: scripted frame provider not initialized");
113
114 auto create_frame_from_dict =
115 [this](StructuredData::Dictionary *dict,
116 uint32_t index) -> llvm::Expected<StackFrameSP> {
118 if (!dict->GetValueForKeyAsInteger("pc", pc))
119 return llvm::createStringError(
120 "missing 'pc' key from scripted frame dictionary");
121
122 Address symbol_addr;
123 symbol_addr.SetLoadAddress(pc, &GetThread().GetProcess()->GetTarget());
124
126 const bool cfa_is_valid = false;
127 const bool artificial = false;
128 const bool behaves_like_zeroth_frame = false;
129 SymbolContext sc;
130 symbol_addr.CalculateSymbolContext(&sc);
131
132 ThreadSP thread_sp = GetThread().shared_from_this();
133 return std::make_shared<StackFrame>(thread_sp, index, index, cfa,
134 cfa_is_valid, pc,
135 StackFrame::Kind::Synthetic, artificial,
136 behaves_like_zeroth_frame, &sc);
137 };
138
139 auto create_frame_from_script_object =
140 [this](
141 StructuredData::ObjectSP object_sp) -> llvm::Expected<StackFrameSP> {
143 if (!object_sp || !object_sp->GetAsGeneric())
144 return llvm::createStringError("invalid script object");
145
146 ThreadSP thread_sp = GetThread().shared_from_this();
147 auto frame_or_error = ScriptedFrame::Create(thread_sp, nullptr, nullptr,
148 object_sp->GetAsGeneric());
149
150 if (!frame_or_error) {
152 LLVM_PRETTY_FUNCTION, toString(frame_or_error.takeError()), error);
153 return error.ToError();
154 }
155
156 return *frame_or_error;
157 };
158
159 StructuredData::ObjectSP obj_sp = m_interface_sp->GetFrameAtIndex(idx);
160
161 // None/null means no more frames or error.
162 if (!obj_sp || !obj_sp->IsValid())
163 return llvm::createStringError("invalid script object returned for frame " +
164 llvm::Twine(idx));
165
166 StackFrameSP synth_frame_sp = nullptr;
168 obj_sp->GetAsUnsignedInteger()) {
169 uint32_t real_frame_index = int_obj->GetValue();
170 if (real_frame_index < m_input_frames->GetNumFrames()) {
171 synth_frame_sp = m_input_frames->GetFrameAtIndex(real_frame_index);
172 }
173 } else if (StructuredData::Dictionary *dict = obj_sp->GetAsDictionary()) {
174 // Check if it's a dictionary describing a frame.
175 auto frame_from_dict_or_err = create_frame_from_dict(dict, idx);
176 if (!frame_from_dict_or_err) {
177 return llvm::createStringError(llvm::Twine(
178 "couldn't create frame from dictionary at index " + llvm::Twine(idx) +
179 ": " + toString(frame_from_dict_or_err.takeError())));
180 }
181 synth_frame_sp = *frame_from_dict_or_err;
182 } else if (obj_sp->GetAsGeneric()) {
183 // It's a ScriptedFrame object.
184 auto frame_from_script_obj_or_err = create_frame_from_script_object(obj_sp);
185 if (!frame_from_script_obj_or_err) {
186 return llvm::createStringError(
187 llvm::Twine("couldn't create frame from script object at index " +
188 llvm::Twine(idx) + ": " +
189 toString(frame_from_script_obj_or_err.takeError())));
190 }
191 synth_frame_sp = *frame_from_script_obj_or_err;
192 } else {
193 return llvm::createStringError(
194 llvm::Twine("invalid return type from get_frame_at_index at index " +
195 llvm::Twine(idx)));
196 }
197
198 if (!synth_frame_sp)
199 return llvm::createStringError(
200 llvm::Twine("failed to create frame at index " + llvm::Twine(idx)));
201
202 synth_frame_sp->SetFrameIndex(idx);
203
204 return synth_frame_sp;
205}
206
207namespace lldb_private {
211
215} // 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::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:65
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.