LLDB mainline
SyntheticFrameProvider.h
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#ifndef LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H
10#define LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H
11
16#include "lldb/Utility/Status.h"
17#include "lldb/lldb-forward.h"
18#include "llvm/Support/Error.h"
19
20#include <optional>
21#include <vector>
22
23namespace lldb_private {
24
25/// This struct contains the metadata needed to instantiate a frame provider
26/// and optional filters to control which threads it applies to.
28 /// Metadata for instantiating the provider (e.g. script class name and args).
30
31 /// Interface for calling static methods on the provider class.
33
34 /// Optional list of thread specifications to which this provider applies.
35 /// If empty, the provider applies to all threads. A thread matches if it
36 /// satisfies ANY of the specs in this vector (OR logic).
37 std::vector<ThreadSpec> thread_specs;
38
40
43
45 const std::vector<ThreadSpec> &specs)
46 : scripted_metadata_sp(metadata_sp), thread_specs(specs) {}
47
48 /// Get the name of this descriptor (the scripted class name).
49 llvm::StringRef GetName() const {
50 return scripted_metadata_sp ? scripted_metadata_sp->GetClassName() : "";
51 }
52
53 /// Get the description of this frame provider.
54 ///
55 /// \return A string describing what this frame provider does, or an
56 /// empty string if no description is available.
57 std::string GetDescription() const;
58
59 /// Check if this descriptor applies to the given thread.
60 bool AppliesToThread(Thread &thread) const {
61 // If no thread specs specified, applies to all threads.
62 if (thread_specs.empty())
63 return true;
64
65 // Check if the thread matches any of the specs (OR logic).
66 for (const auto &spec : thread_specs) {
67 if (spec.ThreadPassesBasicTests(thread))
68 return true;
69 }
70 return false;
71 }
72
73 /// Check if this descriptor has valid metadata for script-based providers.
74 bool IsValid() const { return scripted_metadata_sp != nullptr; }
75
76 /// Get a unique identifier for this descriptor based on its contents.
77 /// The ID is computed from the class name and arguments dictionary,
78 /// not from the pointer address, so two descriptors with the same
79 /// contents will have the same ID.
80 uint32_t GetID() const;
81
82 /// Dump a description of this descriptor to the given stream.
83 void Dump(Stream *s) const;
84};
85
86/// Base class for all synthetic frame providers.
87///
88/// Synthetic frame providers allow modifying or replacing the stack frames
89/// shown for a thread. This is useful for:
90/// - Providing frames for custom calling conventions or languages.
91/// - Reconstructing missing frames from crash dumps or core files.
92/// - Adding diagnostic or synthetic frames for debugging.
93/// - Visualizing state machines or async execution contexts.
95public:
96 /// Try to create a SyntheticFrameProvider instance for the given input
97 /// frames and descriptor.
98 ///
99 /// This method iterates through all registered SyntheticFrameProvider
100 /// plugins and returns the first one that can handle the given descriptor.
101 ///
102 /// \param[in] input_frames
103 /// The input stack frame list that this provider will transform.
104 /// This could be real unwound frames or output from another provider.
105 ///
106 /// \param[in] descriptor
107 /// The descriptor containing metadata for the provider.
108 ///
109 /// \return
110 /// A shared pointer to a SyntheticFrameProvider if one could be created,
111 /// otherwise an \a llvm::Error.
112 static llvm::Expected<lldb::SyntheticFrameProviderSP>
114 const ScriptedFrameProviderDescriptor &descriptor);
115
116 /// Try to create a SyntheticFrameProvider instance for the given input
117 /// frames using a specific C++ plugin.
118 ///
119 /// This method directly invokes a specific SyntheticFrameProvider plugin
120 /// by name, bypassing the descriptor-based plugin iteration. This is useful
121 /// for C++ plugins that don't require scripted metadata.
122 ///
123 /// \param[in] input_frames
124 /// The input stack frame list that this provider will transform.
125 /// This could be real unwound frames or output from another provider.
126 ///
127 /// \param[in] plugin_name
128 /// The name of the plugin to use for creating the provider.
129 ///
130 /// \param[in] thread_specs
131 /// Optional list of thread specifications to which this provider applies.
132 /// If empty, the provider applies to all threads.
133 ///
134 /// \return
135 /// A shared pointer to a SyntheticFrameProvider if one could be created,
136 /// otherwise an \a llvm::Error.
137 static llvm::Expected<lldb::SyntheticFrameProviderSP>
139 llvm::StringRef plugin_name,
140 const std::vector<ThreadSpec> &thread_specs = {});
141
143
144 virtual std::string GetDescription() const = 0;
145
146 /// Get a single stack frame at the specified index.
147 ///
148 /// This method is called lazily - frames are only created when requested.
149 /// The provider can access its input frames via GetInputFrames() if needed.
150 ///
151 /// \param[in] idx
152 /// The index of the frame to create.
153 ///
154 /// \return
155 /// An Expected containing the StackFrameSP if successful. Returns an
156 /// error when the index is beyond the last frame to signal the end of
157 /// the frame list.
158 virtual llvm::Expected<lldb::StackFrameSP> GetFrameAtIndex(uint32_t idx) = 0;
159
160 /// Get the thread associated with this provider.
161 Thread &GetThread() { return m_input_frames->GetThread(); }
162
163 /// Get the input frames that this provider transforms.
165
166protected:
168
170};
171
172} // namespace lldb_private
173
174#endif // LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H
A stream class that can stream formatted output to a file.
Definition Stream.h:28
Thread & GetThread()
Get the thread associated with this provider.
lldb::StackFrameListSP GetInputFrames() const
Get the input frames that this provider transforms.
virtual std::string GetDescription() const =0
static llvm::Expected< lldb::SyntheticFrameProviderSP > CreateInstance(lldb::StackFrameListSP input_frames, const ScriptedFrameProviderDescriptor &descriptor)
Try to create a SyntheticFrameProvider instance for the given input frames and descriptor.
virtual llvm::Expected< lldb::StackFrameSP > GetFrameAtIndex(uint32_t idx)=0
Get a single stack frame at the specified index.
SyntheticFrameProvider(lldb::StackFrameListSP input_frames)
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::ScriptedMetadata > ScriptedMetadataSP
std::shared_ptr< lldb_private::ScriptedFrameProviderInterface > ScriptedFrameProviderInterfaceSP
std::shared_ptr< lldb_private::StackFrameList > StackFrameListSP
This struct contains the metadata needed to instantiate a frame provider and optional filters to cont...
ScriptedFrameProviderDescriptor(lldb::ScriptedMetadataSP metadata_sp)
std::vector< ThreadSpec > thread_specs
Optional list of thread specifications to which this provider applies.
uint32_t GetID() const
Get a unique identifier for this descriptor based on its contents.
lldb::ScriptedMetadataSP scripted_metadata_sp
Metadata for instantiating the provider (e.g. script class name and args).
void Dump(Stream *s) const
Dump a description of this descriptor to the given stream.
llvm::StringRef GetName() const
Get the name of this descriptor (the scripted class name).
lldb::ScriptedFrameProviderInterfaceSP interface_sp
Interface for calling static methods on the provider class.
ScriptedFrameProviderDescriptor(lldb::ScriptedMetadataSP metadata_sp, const std::vector< ThreadSpec > &specs)
std::string GetDescription() const
Get the description of this frame provider.
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.