StructuredDataPlugin.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //===-- StructuredDataPlugin.h ----------------------------------*- C++ -*-===//
  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. #ifndef LLDB_TARGET_STRUCTUREDDATAPLUGIN_H
  9. #define LLDB_TARGET_STRUCTUREDDATAPLUGIN_H
  10. #include "lldb/Core/PluginInterface.h"
  11. #include "lldb/Utility/StructuredData.h"
  12. namespace lldb_private {
  13. class CommandObjectMultiword;
  14. /// Plugin that supports process-related structured data sent asynchronously
  15. /// from the debug monitor (e.g. debugserver, lldb-server, etc.)
  16. ///
  17. /// This plugin type is activated by a Process-derived instance when that
  18. /// instance detects that a given structured data feature is available.
  19. ///
  20. /// StructuredDataPlugin instances are inherently tied to a process. The
  21. /// main functionality they support is the ability to consume asynchronously-
  22. /// delivered structured data from the process monitor, and do something
  23. /// reasonable with it. Something reasonable can include broadcasting a
  24. /// StructuredData event, which other parts of the system can then do with
  25. /// as they please. An IDE could use this facility to retrieve CPU usage,
  26. /// memory usage, and other run-time aspects of the process. That data
  27. /// can then be displayed meaningfully to the user through the IDE.
  28. /// For command-line LLDB, the Debugger instance listens for the structured
  29. /// data events raised by the plugin, and give the plugin both the output
  30. /// and error streams such that the plugin can display something about the
  31. /// event, at a time when the debugger ensures it is safe to write to the
  32. /// output or error streams.
  33. class StructuredDataPlugin
  34. : public PluginInterface,
  35. public std::enable_shared_from_this<StructuredDataPlugin> {
  36. public:
  37. ~StructuredDataPlugin() override;
  38. lldb::ProcessSP GetProcess() const;
  39. // Public instance API
  40. /// Return whether this plugin supports the given StructuredData feature.
  41. ///
  42. /// When Process is informed of a list of process-monitor-supported
  43. /// structured data features, Process will go through the list of plugins,
  44. /// one at a time, and have the first plugin that supports a given feature
  45. /// be the plugin instantiated to handle that feature. There is a 1-1
  46. /// correspondence between a Process instance and a StructuredDataPlugin
  47. /// mapped to that process. A plugin can support handling multiple
  48. /// features, and if that happens, there is a single plugin instance
  49. /// created covering all of the mapped features for a given process.
  50. ///
  51. /// \param[in] type_name
  52. /// The name of the feature tag supported by a process.
  53. /// e.g. "darwin-log".
  54. ///
  55. /// \return
  56. /// true if the plugin supports the feature; otherwise, false.
  57. virtual bool SupportsStructuredDataType(ConstString type_name) = 0;
  58. /// Handle the arrival of asynchronous structured data from the process.
  59. ///
  60. /// When asynchronous structured data arrives from the process monitor,
  61. /// it is immediately delivered to the plugin mapped for that feature
  62. /// if one exists. The structured data that arrives from a process
  63. /// monitor must be a dictionary, and it must have a string field named
  64. /// "type" that must contain the StructuredData feature name set as the
  65. /// value. This is the manner in which the data is routed to the proper
  66. /// plugin instance.
  67. ///
  68. /// \param[in] process
  69. /// The process instance that just received the structured data.
  70. /// This will always be the same process for a given instance of
  71. /// a plugin.
  72. ///
  73. /// \param[in] type_name
  74. /// The name of the feature tag for the asynchronous structured data.
  75. /// Note this data will also be present in the \b object_sp dictionary
  76. /// under the string value with key "type".
  77. ///
  78. /// \param[in] object_sp
  79. /// A shared pointer to the structured data that arrived. This must
  80. /// be a dictionary. The only key required is the aforementioned
  81. /// key named "type" that must be a string value containing the
  82. /// structured data type name.
  83. virtual void
  84. HandleArrivalOfStructuredData(Process &process, ConstString type_name,
  85. const StructuredData::ObjectSP &object_sp) = 0;
  86. /// Get a human-readable description of the contents of the data.
  87. ///
  88. /// In command-line LLDB, this method will be called by the Debugger
  89. /// instance for each structured data event generated, and the output
  90. /// will be printed to the LLDB console. If nothing is added to the stream,
  91. /// nothing will be printed; otherwise, a newline will be added to the end
  92. /// when displayed.
  93. ///
  94. /// \param[in] object_sp
  95. /// A shared pointer to the structured data to format.
  96. ///
  97. /// \param[in] stream
  98. /// The stream where the structured data should be pretty printed.
  99. ///
  100. /// \return
  101. /// The error if formatting the object contents failed; otherwise,
  102. /// success.
  103. virtual Status GetDescription(const StructuredData::ObjectSP &object_sp,
  104. lldb_private::Stream &stream) = 0;
  105. /// Returns whether the plugin's features are enabled.
  106. ///
  107. /// This is a convenience method for plugins that can enable or disable
  108. /// their functionality. It allows retrieval of this state without
  109. /// requiring a cast.
  110. ///
  111. /// \param[in] type_name
  112. /// The name of the feature tag for the asynchronous structured data.
  113. /// This is needed for plugins that support more than one feature.
  114. virtual bool GetEnabled(ConstString type_name) const;
  115. /// Allow the plugin to do work related to modules that loaded in the
  116. /// the corresponding process.
  117. ///
  118. /// This method defaults to doing nothing. Plugins can override it
  119. /// if they have any behavior they want to enable/modify based on loaded
  120. /// modules.
  121. ///
  122. /// \param[in] process
  123. /// The process that just was notified of modules having been loaded.
  124. /// This will always be the same process for a given instance of
  125. /// a plugin.
  126. ///
  127. /// \param[in] module_list
  128. /// The list of modules that the process registered as having just
  129. /// loaded. See \b Process::ModulesDidLoad(...).
  130. virtual void ModulesDidLoad(Process &process, ModuleList &module_list);
  131. protected:
  132. // Derived-class API
  133. StructuredDataPlugin(const lldb::ProcessWP &process_wp);
  134. /// Derived classes must call this before attempting to hook up commands
  135. /// to the 'plugin structured-data' tree.
  136. ///
  137. /// This ensures the relevant command and options hook points for all
  138. /// StructuredDataPlugin derived classes are available for this debugger.
  139. /// If this has already happened, this call is a no-op.
  140. ///
  141. /// \param[in] debugger
  142. /// The Debugger instance for which we're creating the required shared
  143. /// components for the StructuredDataPlugin derived classes.
  144. static void InitializeBasePluginForDebugger(Debugger &debugger);
  145. private:
  146. lldb::ProcessWP m_process_wp;
  147. StructuredDataPlugin(const StructuredDataPlugin &) = delete;
  148. const StructuredDataPlugin &operator=(const StructuredDataPlugin &) = delete;
  149. };
  150. }
  151. #endif