-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[lldb/Target] Add SyntheticFrameProvider class #166664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+342
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H | ||
| #define LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H | ||
|
|
||
| #include "lldb/Core/PluginInterface.h" | ||
| #include "lldb/Target/StackFrameList.h" | ||
| #include "lldb/Target/ThreadSpec.h" | ||
| #include "lldb/Utility/ScriptedMetadata.h" | ||
| #include "lldb/Utility/Status.h" | ||
| #include "lldb/lldb-forward.h" | ||
| #include "llvm/Support/Error.h" | ||
|
|
||
| #include <optional> | ||
| #include <vector> | ||
|
|
||
| namespace lldb_private { | ||
|
|
||
| /// This struct contains the metadata needed to instantiate a frame provider | ||
| /// and optional filters to control which threads it applies to. | ||
| struct SyntheticFrameProviderDescriptor { | ||
medismailben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// Metadata for instantiating the provider (e.g. script class name and args). | ||
| lldb::ScriptedMetadataSP scripted_metadata_sp; | ||
|
|
||
| /// Optional list of thread specifications to which this provider applies. | ||
| /// If empty, the provider applies to all threads. A thread matches if it | ||
| /// satisfies ANY of the specs in this vector (OR logic). | ||
| std::vector<ThreadSpec> thread_specs; | ||
|
|
||
| SyntheticFrameProviderDescriptor() = default; | ||
|
|
||
| SyntheticFrameProviderDescriptor(lldb::ScriptedMetadataSP metadata_sp) | ||
| : scripted_metadata_sp(metadata_sp) {} | ||
|
|
||
| SyntheticFrameProviderDescriptor(lldb::ScriptedMetadataSP metadata_sp, | ||
| const std::vector<ThreadSpec> &specs) | ||
| : scripted_metadata_sp(metadata_sp), thread_specs(specs) {} | ||
|
|
||
| /// Get the name of this descriptor (the scripted class name). | ||
| llvm::StringRef GetName() const { | ||
| return scripted_metadata_sp ? scripted_metadata_sp->GetClassName() : ""; | ||
| } | ||
|
|
||
| /// Check if this descriptor applies to the given thread. | ||
| bool AppliesToThread(Thread &thread) const { | ||
| // If no thread specs specified, applies to all threads. | ||
| if (thread_specs.empty()) | ||
| return true; | ||
|
|
||
| // Check if the thread matches any of the specs (OR logic). | ||
| for (const auto &spec : thread_specs) { | ||
| if (spec.ThreadPassesBasicTests(thread)) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /// Check if this descriptor has valid metadata for script-based providers. | ||
| bool IsValid() const { return scripted_metadata_sp != nullptr; } | ||
|
|
||
| void Dump(Stream *s) const; | ||
| }; | ||
|
|
||
| /// Base class for all synthetic frame providers. | ||
| /// | ||
| /// Synthetic frame providers allow modifying or replacing the stack frames | ||
| /// shown for a thread. This is useful for: | ||
| /// - Providing frames for custom calling conventions or languages. | ||
| /// - Reconstructing missing frames from crash dumps or core files. | ||
| /// - Adding diagnostic or synthetic frames for debugging. | ||
| /// - Visualizing state machines or async execution contexts. | ||
| class SyntheticFrameProvider : public PluginInterface { | ||
| public: | ||
| /// Try to create a SyntheticFrameProvider instance for the given input | ||
| /// frames and descriptor. | ||
| /// | ||
| /// This method iterates through all registered SyntheticFrameProvider | ||
| /// plugins and returns the first one that can handle the given descriptor. | ||
| /// | ||
| /// \param[in] input_frames | ||
| /// The input stack frame list that this provider will transform. | ||
| /// This could be real unwound frames or output from another provider. | ||
| /// | ||
| /// \param[in] descriptor | ||
| /// The descriptor containing metadata for the provider. | ||
| /// | ||
| /// \return | ||
| /// A shared pointer to a SyntheticFrameProvider if one could be created, | ||
| /// otherwise an \a llvm::Error. | ||
| static llvm::Expected<lldb::SyntheticFrameProviderSP> | ||
| CreateInstance(lldb::StackFrameListSP input_frames, | ||
| const SyntheticFrameProviderDescriptor &descriptor); | ||
medismailben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Try to create a SyntheticFrameProvider instance for the given input | ||
| /// frames using a specific C++ plugin. | ||
| /// | ||
| /// This method directly invokes a specific SyntheticFrameProvider plugin | ||
| /// by name, bypassing the descriptor-based plugin iteration. This is useful | ||
| /// for C++ plugins that don't require scripted metadata. | ||
| /// | ||
| /// \param[in] input_frames | ||
| /// The input stack frame list that this provider will transform. | ||
| /// This could be real unwound frames or output from another provider. | ||
| /// | ||
| /// \param[in] plugin_name | ||
| /// The name of the plugin to use for creating the provider. | ||
| /// | ||
| /// \param[in] thread_specs | ||
| /// Optional list of thread specifications to which this provider applies. | ||
| /// If empty, the provider applies to all threads. | ||
| /// | ||
| /// \return | ||
| /// A shared pointer to a SyntheticFrameProvider if one could be created, | ||
| /// otherwise an \a llvm::Error. | ||
| static llvm::Expected<lldb::SyntheticFrameProviderSP> | ||
| CreateInstance(lldb::StackFrameListSP input_frames, | ||
| llvm::StringRef plugin_name, | ||
| const std::vector<ThreadSpec> &thread_specs = {}); | ||
|
|
||
| ~SyntheticFrameProvider() override; | ||
|
|
||
| /// Get a single stack frame at the specified index. | ||
| /// | ||
| /// This method is called lazily - frames are only created when requested. | ||
| /// The provider can access its input frames via GetInputFrames() if needed. | ||
| /// | ||
| /// \param[in] idx | ||
| /// The index of the frame to create. | ||
| /// | ||
| /// \return | ||
| /// An Expected containing the StackFrameSP if successful. Returns an | ||
| /// error when the index is beyond the last frame to signal the end of | ||
| /// the frame list. | ||
| virtual llvm::Expected<lldb::StackFrameSP> GetFrameAtIndex(uint32_t idx) = 0; | ||
|
|
||
| /// Get the thread associated with this provider. | ||
| Thread &GetThread() { return m_input_frames->GetThread(); } | ||
|
|
||
| /// Get the input frames that this provider transforms. | ||
| lldb::StackFrameListSP GetInputFrames() const { return m_input_frames; } | ||
|
|
||
| protected: | ||
| SyntheticFrameProvider(lldb::StackFrameListSP input_frames); | ||
|
|
||
| lldb::StackFrameListSP m_input_frames; | ||
medismailben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace lldb_private | ||
|
|
||
| #endif // LLDB_TARGET_SYNTHETICFRAMEPROVIDER_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "lldb/Target/SyntheticFrameProvider.h" | ||
| #include "lldb/Core/PluginManager.h" | ||
| #include "lldb/Target/Thread.h" | ||
| #include "lldb/Utility/LLDBLog.h" | ||
| #include "lldb/Utility/Log.h" | ||
| #include "lldb/Utility/Status.h" | ||
|
|
||
| using namespace lldb; | ||
| using namespace lldb_private; | ||
|
|
||
| SyntheticFrameProvider::SyntheticFrameProvider(StackFrameListSP input_frames) | ||
| : m_input_frames(std::move(input_frames)) {} | ||
|
|
||
| SyntheticFrameProvider::~SyntheticFrameProvider() = default; | ||
|
|
||
| void SyntheticFrameProviderDescriptor::Dump(Stream *s) const { | ||
| if (!s) | ||
| return; | ||
|
|
||
| s->Printf(" Name: %s\n", GetName().str().c_str()); | ||
|
|
||
| // Show thread filter information. | ||
| if (thread_specs.empty()) { | ||
| s->PutCString(" Thread Filter: (applies to all threads)\n"); | ||
| } else { | ||
| s->Printf(" Thread Filter: %zu specification(s)\n", thread_specs.size()); | ||
| for (size_t i = 0; i < thread_specs.size(); ++i) { | ||
| const ThreadSpec &spec = thread_specs[i]; | ||
| s->Printf(" [%zu] ", i); | ||
| spec.GetDescription(s, lldb::eDescriptionLevelVerbose); | ||
| s->PutChar('\n'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| llvm::Expected<SyntheticFrameProviderSP> SyntheticFrameProvider::CreateInstance( | ||
| StackFrameListSP input_frames, | ||
| const SyntheticFrameProviderDescriptor &descriptor) { | ||
| if (!input_frames) | ||
| return llvm::createStringError( | ||
| "cannot create synthetic frame provider: invalid input frames"); | ||
|
|
||
| // Iterate through all registered ScriptedFrameProvider plugins. | ||
| ScriptedFrameProviderCreateInstance create_callback = nullptr; | ||
| for (uint32_t idx = 0; | ||
| (create_callback = | ||
| PluginManager::GetScriptedFrameProviderCreateCallbackAtIndex( | ||
| idx)) != nullptr; | ||
| ++idx) { | ||
| auto provider_or_err = create_callback(input_frames, descriptor); | ||
| if (!provider_or_err) { | ||
| LLDB_LOG_ERROR(GetLog(LLDBLog::Target), provider_or_err.takeError(), | ||
| "Failed to create synthetic frame provider: {0}"); | ||
| continue; | ||
| } | ||
|
|
||
| if (auto frame_provider_up = std::move(*provider_or_err)) | ||
| return std::move(frame_provider_up); | ||
| } | ||
|
|
||
| return llvm::createStringError( | ||
| "cannot create synthetic frame provider: no suitable plugin found"); | ||
| } | ||
|
|
||
| llvm::Expected<SyntheticFrameProviderSP> SyntheticFrameProvider::CreateInstance( | ||
| StackFrameListSP input_frames, llvm::StringRef plugin_name, | ||
| const std::vector<ThreadSpec> &thread_specs) { | ||
| if (!input_frames) | ||
| return llvm::createStringError( | ||
| "cannot create synthetic frame provider: invalid input frames"); | ||
|
|
||
| // Look up the specific C++ plugin by name. | ||
| SyntheticFrameProviderCreateInstance create_callback = | ||
| PluginManager::GetSyntheticFrameProviderCreateCallbackForPluginName( | ||
| plugin_name); | ||
|
|
||
| if (!create_callback) | ||
| return llvm::createStringError( | ||
| "cannot create synthetic frame provider: C++ plugin '%s' not found", | ||
| plugin_name.str().c_str()); | ||
|
|
||
| auto provider_or_err = create_callback(input_frames, thread_specs); | ||
| if (!provider_or_err) | ||
| return provider_or_err.takeError(); | ||
|
|
||
| if (auto frame_provider_sp = std::move(*provider_or_err)) | ||
| return std::move(frame_provider_sp); | ||
|
|
||
| return llvm::createStringError( | ||
| "cannot create synthetic frame provider: C++ plugin '%s' returned null", | ||
| plugin_name.str().c_str()); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.