Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:55

0001 //===-- StructuredDataPlugin.h ----------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_TARGET_STRUCTUREDDATAPLUGIN_H
0010 #define LLDB_TARGET_STRUCTUREDDATAPLUGIN_H
0011 
0012 #include "lldb/Core/PluginInterface.h"
0013 #include "lldb/Utility/StructuredData.h"
0014 
0015 namespace lldb_private {
0016 
0017 class CommandObjectMultiword;
0018 
0019 /// Plugin that supports process-related structured data sent asynchronously
0020 /// from the debug monitor (e.g. debugserver, lldb-server, etc.)
0021 ///
0022 /// This plugin type is activated by a Process-derived instance when that
0023 /// instance detects that a given structured data feature is available.
0024 ///
0025 /// StructuredDataPlugin instances are inherently tied to a process.  The
0026 /// main functionality they support is the ability to consume asynchronously-
0027 /// delivered structured data from the process monitor, and do something
0028 /// reasonable with it.  Something reasonable can include broadcasting a
0029 /// StructuredData event, which other parts of the system can then do with
0030 /// as they please.  An IDE could use this facility to retrieve CPU usage,
0031 /// memory usage, and other run-time aspects of the process.  That data
0032 /// can then be displayed meaningfully to the user through the IDE.
0033 
0034 /// For command-line LLDB, the Debugger instance listens for the structured
0035 /// data events raised by the plugin, and give the plugin both the output
0036 /// and error streams such that the plugin can display something about the
0037 /// event, at a time when the debugger ensures it is safe to write to the
0038 /// output or error streams.
0039 
0040 class StructuredDataPlugin
0041     : public PluginInterface,
0042       public std::enable_shared_from_this<StructuredDataPlugin> {
0043 public:
0044   ~StructuredDataPlugin() override;
0045 
0046   lldb::ProcessSP GetProcess() const;
0047 
0048   // Public instance API
0049 
0050   /// Return whether this plugin supports the given StructuredData feature.
0051   ///
0052   /// When Process is informed of a list of process-monitor-supported
0053   /// structured data features, Process will go through the list of plugins,
0054   /// one at a time, and have the first plugin that supports a given feature
0055   /// be the plugin instantiated to handle that feature.  There is a 1-1
0056   /// correspondence between a Process instance and a StructuredDataPlugin
0057   /// mapped to that process.  A plugin can support handling multiple
0058   /// features, and if that happens, there is a single plugin instance
0059   /// created covering all of the mapped features for a given process.
0060   ///
0061   /// \param[in] type_name
0062   ///     The name of the feature tag supported by a process.
0063   ///     e.g. "darwin-log".
0064   ///
0065   /// \return
0066   ///     true if the plugin supports the feature; otherwise, false.
0067   virtual bool SupportsStructuredDataType(llvm::StringRef type_name) = 0;
0068 
0069   /// Handle the arrival of asynchronous structured data from the process.
0070   ///
0071   /// When asynchronous structured data arrives from the process monitor,
0072   /// it is immediately delivered to the plugin mapped for that feature
0073   /// if one exists.  The structured data that arrives from a process
0074   /// monitor must be a dictionary, and it must have a string field named
0075   /// "type" that must contain the StructuredData feature name set as the
0076   /// value.  This is the manner in which the data is routed to the proper
0077   /// plugin instance.
0078   ///
0079   /// \param[in] process
0080   ///     The process instance that just received the structured data.
0081   ///     This will always be the same process for a given instance of
0082   ///     a plugin.
0083   ///
0084   /// \param[in] type_name
0085   ///     The name of the feature tag for the asynchronous structured data.
0086   ///     Note this data will also be present in the \b object_sp dictionary
0087   ///     under the string value with key "type".
0088   ///
0089   /// \param[in] object_sp
0090   ///     A shared pointer to the structured data that arrived.  This must
0091   ///     be a dictionary.  The only key required is the aforementioned
0092   ///     key named "type" that must be a string value containing the
0093   ///     structured data type name.
0094   virtual void
0095   HandleArrivalOfStructuredData(Process &process, llvm::StringRef type_name,
0096                                 const StructuredData::ObjectSP &object_sp) = 0;
0097 
0098   /// Get a human-readable description of the contents of the data.
0099   ///
0100   /// In command-line LLDB, this method will be called by the Debugger
0101   /// instance for each structured data event generated, and the output
0102   /// will be printed to the LLDB console.  If nothing is added to the stream,
0103   /// nothing will be printed; otherwise, a newline will be added to the end
0104   /// when displayed.
0105   ///
0106   /// \param[in] object_sp
0107   ///     A shared pointer to the structured data to format.
0108   ///
0109   /// \param[in] stream
0110   ///     The stream where the structured data should be pretty printed.
0111   ///
0112   /// \return
0113   ///     The error if formatting the object contents failed; otherwise,
0114   ///     success.
0115   virtual Status GetDescription(const StructuredData::ObjectSP &object_sp,
0116                                 lldb_private::Stream &stream) = 0;
0117 
0118   /// Returns whether the plugin's features are enabled.
0119   ///
0120   /// This is a convenience method for plugins that can enable or disable
0121   /// their functionality.  It allows retrieval of this state without
0122   /// requiring a cast.
0123   ///
0124   /// \param[in] type_name
0125   ///     The name of the feature tag for the asynchronous structured data.
0126   ///     This is needed for plugins that support more than one feature.
0127   virtual bool GetEnabled(llvm::StringRef type_name) const;
0128 
0129   /// Allow the plugin to do work related to modules that loaded in the
0130   /// the corresponding process.
0131   ///
0132   /// This method defaults to doing nothing.  Plugins can override it
0133   /// if they have any behavior they want to enable/modify based on loaded
0134   /// modules.
0135   ///
0136   /// \param[in] process
0137   ///     The process that just was notified of modules having been loaded.
0138   ///     This will always be the same process for a given instance of
0139   ///     a plugin.
0140   ///
0141   /// \param[in] module_list
0142   ///     The list of modules that the process registered as having just
0143   ///     loaded.  See \b Process::ModulesDidLoad(...).
0144   virtual void ModulesDidLoad(Process &process, ModuleList &module_list);
0145 
0146 protected:
0147   // Derived-class API
0148   StructuredDataPlugin(const lldb::ProcessWP &process_wp);
0149 
0150   /// Derived classes must call this before attempting to hook up commands
0151   /// to the 'plugin structured-data' tree.
0152   ///
0153   /// This ensures the relevant command and options hook points for all
0154   /// StructuredDataPlugin derived classes are available for this debugger.
0155   /// If this has already happened, this call is a no-op.
0156   ///
0157   /// \param[in] debugger
0158   ///     The Debugger instance for which we're creating the required shared
0159   ///     components for the StructuredDataPlugin derived classes.
0160   static void InitializeBasePluginForDebugger(Debugger &debugger);
0161 
0162 private:
0163   lldb::ProcessWP m_process_wp;
0164 
0165   StructuredDataPlugin(const StructuredDataPlugin &) = delete;
0166   const StructuredDataPlugin &operator=(const StructuredDataPlugin &) = delete;
0167 };
0168 }
0169 
0170 #endif