LLDB mainline
ScriptedFrame.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 "ScriptedFrame.h"
11
12#include "lldb/Core/Address.h"
13#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Module.h"
27#include "lldb/Target/Thread.h"
31#include "lldb/Utility/Log.h"
37#include "lldb/lldb-forward.h"
38#include "llvm/Support/ErrorHandling.h"
39
40#include <memory>
41
42using namespace lldb;
43using namespace lldb_private;
44
46
48 lldbassert(m_script_object_sp && "Invalid Script Object.");
49 lldbassert(GetInterface() && "Invalid Scripted Frame Interface.");
50}
51
52llvm::Expected<std::shared_ptr<ScriptedFrame>>
54 ScriptedThreadInterfaceSP scripted_thread_interface_sp,
56 StructuredData::Generic *script_object) {
57 if (!thread_sp || !thread_sp->IsValid())
58 return llvm::createStringError("invalid thread");
59
60 ProcessSP process_sp = thread_sp->GetProcess();
61 if (!process_sp || !process_sp->IsValid())
62 return llvm::createStringError("invalid process");
63
64 ScriptInterpreter *script_interp =
65 process_sp->GetTarget().GetDebugger().GetScriptInterpreter();
66 if (!script_interp)
67 return llvm::createStringError("no script interpreter");
68
69 auto scripted_frame_interface = script_interp->CreateScriptedFrameInterface();
70 if (!scripted_frame_interface)
71 return llvm::createStringError("failed to create scripted frame interface");
72
73 llvm::StringRef frame_class_name;
74 if (!script_object) {
75 // If no script object is provided and we have a scripted thread interface,
76 // try to get the frame class name from it.
77 if (scripted_thread_interface_sp) {
78 std::optional<std::string> class_name =
79 scripted_thread_interface_sp->GetScriptedFramePluginName();
80 if (!class_name || class_name->empty())
81 return llvm::createStringError(
82 "failed to get scripted frame class name");
83 frame_class_name = *class_name;
84 } else {
85 return llvm::createStringError(
86 "no script object provided and no scripted thread interface");
87 }
88 }
89
90 ExecutionContext exe_ctx(thread_sp);
91 auto obj_or_err = scripted_frame_interface->CreatePluginObject(
92 frame_class_name, exe_ctx, args_sp, script_object);
93
94 if (!obj_or_err)
95 return llvm::createStringError(
96 "failed to create script object: %s",
97 llvm::toString(obj_or_err.takeError()).c_str());
98
99 StructuredData::GenericSP owned_script_object_sp = *obj_or_err;
100
101 if (!owned_script_object_sp->IsValid())
102 return llvm::createStringError("created script object is invalid");
103
104 lldb::user_id_t frame_id = scripted_frame_interface->GetID();
105
106 lldb::addr_t pc = scripted_frame_interface->GetPC();
107 SymbolContext sc;
108 Address symbol_addr;
109 if (pc != LLDB_INVALID_ADDRESS) {
110 symbol_addr.SetLoadAddress(pc, &process_sp->GetTarget());
111 symbol_addr.CalculateSymbolContext(&sc);
112 }
113
114 std::optional<SymbolContext> maybe_sym_ctx =
115 scripted_frame_interface->GetSymbolContext();
116 if (maybe_sym_ctx)
117 sc = *maybe_sym_ctx;
118
119 return std::make_shared<ScriptedFrame>(thread_sp, scripted_frame_interface,
120 frame_id, pc, sc,
121 owned_script_object_sp);
122}
123
125 ScriptedFrameInterfaceSP interface_sp,
127 SymbolContext &sym_ctx,
128 StructuredData::GenericSP script_object_sp)
129 : StackFrame(thread_sp, /*frame_idx=*/id,
130 /*concrete_frame_idx=*/id, /*reg_context_sp=*/nullptr,
131 /*cfa=*/0, /*pc=*/pc,
132 /*behaves_like_zeroth_frame=*/!id, /*symbol_ctx=*/&sym_ctx),
133 m_scripted_frame_interface_sp(interface_sp),
134 m_script_object_sp(script_object_sp) {
135 // FIXME: This should be part of the base class constructor.
137
138 llvm::Expected<lldb::RegisterContextSP> reg_ctx_or_err =
140 if (!reg_ctx_or_err) {
141 std::optional<lldb::user_id_t> debugger_id;
142 if (ProcessSP process_sp = thread_sp->GetProcess())
143 debugger_id = process_sp->GetTarget().GetDebugger().GetID();
144 Debugger::ReportError("failed to create scripted frame register context: " +
145 llvm::toString(reg_ctx_or_err.takeError()),
146 debugger_id);
147 return;
148 }
149
150 m_reg_context_sp = *reg_ctx_or_err;
151}
152
154
157 std::optional<std::string> function_name = GetInterface()->GetFunctionName();
158 if (!function_name)
160 return ConstString(*function_name).AsCString(nullptr);
161}
162
165 std::optional<std::string> function_name =
166 GetInterface()->GetDisplayFunctionName();
167 if (!function_name)
169 return ConstString(*function_name).AsCString(nullptr);
170}
171
172bool ScriptedFrame::IsInlined() { return GetInterface()->IsInlined(); }
173
175 return GetInterface()->IsArtificial();
176}
177
178bool ScriptedFrame::IsHidden() { return GetInterface()->IsHidden(); }
179
183
184llvm::Expected<DynamicRegisterInfoSP> ScriptedFrame::GetDynamicRegisterInfo() {
186
187 StructuredData::DictionarySP reg_info = GetInterface()->GetRegisterInfo();
188 if (!reg_info)
189 return llvm::createStringError(
190 "failed to get scripted frame registers info");
191
192 ThreadSP thread_sp = m_thread_wp.lock();
193 if (!thread_sp || !thread_sp->IsValid())
194 return llvm::createStringError("invalid thread");
195
196 ProcessSP process_sp = thread_sp->GetProcess();
197 if (!process_sp || !process_sp->IsValid())
198 return llvm::createStringError("invalid process");
199
201 *reg_info, process_sp->GetTarget().GetArchitecture());
202 if (!register_info_sp)
203 return llvm::createStringError(
204 "failed to create scripted frame registers info");
205
206 return register_info_sp;
207}
208
209llvm::Expected<lldb::RegisterContextSP> ScriptedFrame::CreateRegisterContext() {
211 return llvm::createStringError("invalid scripted frame interface");
212
213 ThreadSP thread_sp = GetThread();
214 if (!thread_sp)
215 return llvm::createStringError("invalid thread");
216
217 // A frame that reports no register data has no register context. That is a
218 // valid state, not a failure: only frames that expose registers implement it.
219 std::optional<std::string> reg_data =
220 m_scripted_frame_interface_sp->GetRegisterContext();
221 if (!reg_data)
223
224 DataBufferSP data_sp(
225 std::make_shared<DataBufferHeap>(reg_data->c_str(), reg_data->size()));
226
227 if (!data_sp->GetByteSize())
228 return llvm::createStringError("failed to copy raw registers data");
229
230 llvm::Expected<DynamicRegisterInfoSP> register_info_or_err =
232 if (!register_info_or_err)
233 return register_info_or_err.takeError();
234
235 std::shared_ptr<RegisterContextMemory> reg_ctx_memory =
236 std::make_shared<RegisterContextMemory>(*thread_sp, GetFrameIndex(),
237 std::move(*register_info_or_err),
239
240 reg_ctx_memory->SetAllRegisterData(data_sp);
241
242 return reg_ctx_memory;
243}
244
248
250 bool include_synthetic_vars,
251 Status *error_ptr) {
252 PopulateVariableListFromInterface(include_synthetic_vars);
253 return m_variable_list_sp.get();
254}
255
258 bool include_synthetic_vars,
259 bool must_have_valid_location) {
260 PopulateVariableListFromInterface(include_synthetic_vars);
261 return m_variable_list_sp;
262}
263
265 bool include_synthetic_vars) {
266 // Fetch values from the interface.
267 ValueObjectListSP value_list_sp = GetInterface()->GetVariables();
268 if (!value_list_sp)
269 return;
270
271 // Convert what we can into a variable.
272 m_variable_list_sp = std::make_shared<VariableList>();
273
274 for (uint32_t i = 0, e = value_list_sp->GetSize(); i < e; ++i) {
275 ValueObjectSP v = value_list_sp->GetValueObjectAtIndex(i);
276 if (!v)
277 continue;
278
279 // Ask the interface about the value type of this variable. If it doesn't
280 // specify any, use the original value type.
281 lldb::ValueType vt = GetInterface()->GetValueTypeForVariable(v).value_or(
282 GetSyntheticValueType(v->GetValueType()));
283
284 if (IsSyntheticValueType(vt) && !include_synthetic_vars)
285 continue;
286
287 // Just make up a variable - the frame variable dumper just passes it
288 // back in to GetValueObjectForFrameVariable, so we really just need to
289 // make sure the name and type are correct. We create IDs based on
290 // value_list_sp in order to make sure they're unique.
291 m_variable_list_sp->AddVariable(std::make_shared<lldb_private::Variable>(
292 (lldb::user_id_t)value_list_sp->GetSize() + i,
293 v->GetName().GetCString(), v->GetName().GetCString(), nullptr, vt,
294 /*owner_scope=*/nullptr,
295 /*scope_range=*/Variable::RangeList{},
296 /*decl=*/nullptr, DWARFExpressionList{}, /*external=*/false,
297 /*artificial=*/true, /*location_is_constant_data=*/false));
298 }
299}
300
302 const lldb::VariableSP &variable_sp, lldb::DynamicValueType use_dynamic) {
303 // Fetch values from the interface.
304 ValueObjectListSP values = m_scripted_frame_interface_sp->GetVariables();
305 if (!values)
306 return {};
307
308 return values->FindValueObjectByValueName(
309 variable_sp->GetName().AsCString(nullptr));
310}
311
313 // Fetch values from the interface.
314 ValueObjectListSP values = m_scripted_frame_interface_sp->GetVariables();
315 if (!values)
316 return {};
317
318 return values->FindValueObjectByValueName(name.AsCString(nullptr));
319}
320
322 llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic,
323 uint32_t options, lldb::VariableSP &var_sp, Status &error,
324 lldb::DILMode mode) {
325 // Unless the frame implementation knows how to create variables (which it
326 // doesn't), we can't construct anything for the variable. This may seem
327 // somewhat out of place, but it's basically because of how this API is used -
328 // the print command uses this API to fill in var_sp; and this implementation
329 // can't do that!
330 // FIXME: We should make it possible for the frame implementation to create
331 // Variable objects.
332 (void)var_sp;
333 // Otherwise, delegate to the scripted frame interface pointer.
334 return m_scripted_frame_interface_sp->GetValueObjectForVariableExpression(
335 var_expr, options, error);
336}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
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:1028
uint32_t CalculateSymbolContext(SymbolContext *sc, lldb::SymbolContextItem resolve_scope=lldb::eSymbolContextEverything) const
Reconstruct a symbol context from an address.
Definition Address.cpp:819
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
static void ReportError(std::string message, std::optional< lldb::user_id_t > debugger_id=std::nullopt, std::once_flag *once=nullptr)
Report error events.
static std::unique_ptr< DynamicRegisterInfo > Create(const StructuredData::Dictionary &dict, const ArchSpec &arch)
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
virtual lldb::ScriptedFrameInterfaceSP CreateScriptedFrameInterface()
lldb::VariableListSP m_variable_list_sp
llvm::Expected< lldb::DynamicRegisterInfoSP > GetDynamicRegisterInfo()
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.
lldb::ValueObjectSP FindVariable(ConstString name) override
Attempt to reconstruct the ValueObject for a variable with a given name from within the current Stack...
bool IsArtificial() const override
Query whether this frame is artificial (e.g a synthesized result of inferring missing tail call frame...
lldb::ValueObjectSP GetValueForVariableExpressionPath(llvm::StringRef var_expr, lldb::DynamicValueType use_dynamic, uint32_t options, lldb::VariableSP &var_sp, Status &error, lldb::DILMode mode=lldb::eDILModeFull) override
Create a ValueObject for a variable name / pathname, possibly including simple dereference/child sele...
const char * GetFunctionName() override
Get the frame's demangled name.
lldb::ScriptedFrameInterfaceSP GetInterface() const
void PopulateVariableListFromInterface(bool include_synthetic_vars=true)
void CheckInterpreterAndScriptObject() const
lldb::ScriptedFrameInterfaceSP m_scripted_frame_interface_sp
lldb_private::StructuredData::GenericSP m_script_object_sp
lldb::RegisterContextSP GetRegisterContext() override
Get the RegisterContext for this frame, if possible.
bool IsHidden() override
Query whether this frame should be hidden from backtraces.
lldb::VariableListSP GetInScopeVariableList(bool get_file_globals, bool include_synthetic_vars, bool must_have_valid_location=false) override
Retrieve the list of variables that are in scope at this StackFrame's pc.
VariableList * GetVariableList(bool get_file_globals, bool include_synthetic_vars, lldb_private::Status *error_ptr) override
Retrieve the list of variables whose scope either:
bool IsInlined() override
Query whether this frame is a concrete frame on the call stack, or if it is an inlined frame derived ...
const char * GetDisplayFunctionName() override
Get the frame's demangled display name.
lldb::ValueObjectSP GetValueObjectForFrameVariable(const lldb::VariableSP &variable_sp, lldb::DynamicValueType use_dynamic) override
Create a ValueObject for a given Variable in this StackFrame.
llvm::Expected< lldb::RegisterContextSP > CreateRegisterContext()
ScriptedFrame(lldb::ThreadSP thread_sp, lldb::ScriptedFrameInterfaceSP interface_sp, lldb::user_id_t frame_idx, lldb::addr_t pc, SymbolContext &sym_ctx, StructuredData::GenericSP script_object_sp=nullptr)
lldb::ThreadSP GetThread() const
Definition StackFrame.h:135
virtual const char * GetFunctionName()
Get the frame's demangled name.
lldb::ThreadWP m_thread_wp
For StackFrame and derived classes only.
Definition StackFrame.h:594
lldb::RegisterContextSP m_reg_context_sp
Definition StackFrame.h:597
@ Synthetic
An synthetic stack frame (e.g.
Definition StackFrame.h:72
virtual const char * GetDisplayFunctionName()
Get the frame's demangled display name.
virtual uint32_t GetFrameIndex() const
Query this frame to find what frame it is in this Thread's StackFrameList.
StackFrame(const lldb::ThreadSP &thread_sp, lldb::user_id_t frame_idx, lldb::user_id_t concrete_frame_idx, lldb::addr_t cfa, bool cfa_is_valid, lldb::addr_t pc, Kind frame_kind, bool artificial, bool behaves_like_zeroth_frame, const SymbolContext *sc_ptr)
Construct a StackFrame object without supplying a RegisterContextSP.
An error handling class.
Definition Status.h:118
std::shared_ptr< Generic > GenericSP
std::shared_ptr< Dictionary > DictionarySP
Defines a symbol context baton that can be handed other debug core functions.
RangeVector< lldb::addr_t, lldb::addr_t > RangeList
Definition Variable.h:27
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
constexpr bool IsSyntheticValueType(lldb::ValueType vt)
Return true if vt represents a synthetic value, false if not.
Definition ValueType.h:27
constexpr lldb::ValueType GetSyntheticValueType(lldb::ValueType base)
Given a base value type, return a version that carries the synthetic bit.
Definition ValueType.h:22
std::shared_ptr< lldb_private::DynamicRegisterInfo > DynamicRegisterInfoSP
std::shared_ptr< lldb_private::Thread > ThreadSP
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::ScriptedThreadInterface > ScriptedThreadInterfaceSP
std::shared_ptr< lldb_private::ValueObjectList > ValueObjectListSP
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP
uint64_t user_id_t
Definition lldb-types.h:83
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
DILMode
Data Inspection Language (DIL) evaluation modes.
std::shared_ptr< lldb_private::ScriptedFrameInterface > ScriptedFrameInterfaceSP