Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- Event.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_UTILITY_EVENT_H
0010 #define LLDB_UTILITY_EVENT_H
0011 
0012 #include "lldb/Utility/Broadcaster.h"
0013 #include "lldb/Utility/ConstString.h"
0014 #include "lldb/Utility/Predicate.h"
0015 #include "lldb/Utility/StructuredData.h"
0016 #include "lldb/lldb-defines.h"
0017 #include "lldb/lldb-forward.h"
0018 
0019 #include "llvm/ADT/StringRef.h"
0020 
0021 #include <chrono>
0022 #include <memory>
0023 #include <string>
0024 
0025 #include <cstddef>
0026 #include <cstdint>
0027 
0028 namespace lldb_private {
0029 class Event;
0030 class Stream;
0031 }
0032 
0033 namespace lldb_private {
0034 
0035 // lldb::EventData
0036 class EventData {
0037   friend class Event;
0038 
0039 public:
0040   EventData();
0041 
0042   virtual ~EventData();
0043 
0044   virtual llvm::StringRef GetFlavor() const = 0;
0045 
0046   virtual Log *GetLogChannel() { return nullptr; }
0047   
0048   virtual void Dump(Stream *s) const;
0049 
0050 private:
0051   /// This will be queried for a Broadcaster with a primary and some secondary
0052   /// listeners after the primary listener pulled the event from the event queue
0053   /// and ran its DoOnRemoval, right before the event is delivered.
0054   /// If it returns true, the event will also be forwarded to the secondary
0055   /// listeners, and if false, event propagation stops at the primary listener.
0056   /// Some broadcasters (particularly the Process broadcaster) fetch events on
0057   /// a private Listener, and then forward the event to the Public Listeners
0058   /// after some processing.  The Process broadcaster does not want to forward
0059   /// to the secondary listeners at the private processing stage.
0060   virtual bool ForwardEventToPendingListeners(Event *event_ptr) { return true; }
0061 
0062   virtual void DoOnRemoval(Event *event_ptr) {}
0063 
0064   EventData(const EventData &) = delete;
0065   const EventData &operator=(const EventData &) = delete;
0066 };
0067 
0068 // lldb::EventDataBytes
0069 class EventDataBytes : public EventData {
0070 public:
0071   // Constructors
0072   EventDataBytes();
0073 
0074   EventDataBytes(llvm::StringRef str);
0075 
0076   ~EventDataBytes() override;
0077 
0078   // Member functions
0079   llvm::StringRef GetFlavor() const override;
0080 
0081   void Dump(Stream *s) const override;
0082 
0083   const void *GetBytes() const;
0084 
0085   size_t GetByteSize() const;
0086 
0087   // Static functions
0088   static const EventDataBytes *GetEventDataFromEvent(const Event *event_ptr);
0089 
0090   static const void *GetBytesFromEvent(const Event *event_ptr);
0091 
0092   static size_t GetByteSizeFromEvent(const Event *event_ptr);
0093 
0094   static llvm::StringRef GetFlavorString();
0095 
0096 private:
0097   std::string m_bytes;
0098 
0099   EventDataBytes(const EventDataBytes &) = delete;
0100   const EventDataBytes &operator=(const EventDataBytes &) = delete;
0101 };
0102 
0103 class EventDataReceipt : public EventData {
0104 public:
0105   EventDataReceipt() : m_predicate(false) {}
0106 
0107   ~EventDataReceipt() override = default;
0108 
0109   static llvm::StringRef GetFlavorString();
0110 
0111   llvm::StringRef GetFlavor() const override { return GetFlavorString(); }
0112 
0113   bool WaitForEventReceived(const Timeout<std::micro> &timeout = std::nullopt) {
0114     return m_predicate.WaitForValueEqualTo(true, timeout);
0115   }
0116 
0117 private:
0118   Predicate<bool> m_predicate;
0119 
0120   void DoOnRemoval(Event *event_ptr) override {
0121     m_predicate.SetValue(true, eBroadcastAlways);
0122   }
0123 };
0124 
0125 /// This class handles one or more StructuredData::Dictionary entries
0126 /// that are raised for structured data events.
0127 
0128 class EventDataStructuredData : public EventData {
0129 public:
0130   // Constructors
0131   EventDataStructuredData();
0132 
0133   EventDataStructuredData(const lldb::ProcessSP &process_sp,
0134                           const StructuredData::ObjectSP &object_sp,
0135                           const lldb::StructuredDataPluginSP &plugin_sp);
0136 
0137   ~EventDataStructuredData() override;
0138 
0139   // Member functions
0140   llvm::StringRef GetFlavor() const override;
0141 
0142   void Dump(Stream *s) const override;
0143 
0144   const lldb::ProcessSP &GetProcess() const;
0145 
0146   const StructuredData::ObjectSP &GetObject() const;
0147 
0148   const lldb::StructuredDataPluginSP &GetStructuredDataPlugin() const;
0149 
0150   void SetProcess(const lldb::ProcessSP &process_sp);
0151 
0152   void SetObject(const StructuredData::ObjectSP &object_sp);
0153 
0154   void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp);
0155 
0156   // Static functions
0157   static const EventDataStructuredData *
0158   GetEventDataFromEvent(const Event *event_ptr);
0159 
0160   static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr);
0161 
0162   static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr);
0163 
0164   static lldb::StructuredDataPluginSP
0165   GetPluginFromEvent(const Event *event_ptr);
0166 
0167   static llvm::StringRef GetFlavorString();
0168 
0169 private:
0170   lldb::ProcessSP m_process_sp;
0171   StructuredData::ObjectSP m_object_sp;
0172   lldb::StructuredDataPluginSP m_plugin_sp;
0173 
0174   EventDataStructuredData(const EventDataStructuredData &) = delete;
0175   const EventDataStructuredData &
0176   operator=(const EventDataStructuredData &) = delete;
0177 };
0178 
0179 // lldb::Event
0180 class Event : public std::enable_shared_from_this<Event> {
0181   friend class Listener;
0182   friend class EventData;
0183   friend class Broadcaster::BroadcasterImpl;
0184 
0185 public:
0186   Event(Broadcaster *broadcaster, uint32_t event_type,
0187         EventData *data = nullptr);
0188 
0189   Event(Broadcaster *broadcaster, uint32_t event_type,
0190         const lldb::EventDataSP &event_data_sp);
0191 
0192   Event(uint32_t event_type, EventData *data = nullptr);
0193 
0194   Event(uint32_t event_type, const lldb::EventDataSP &event_data_sp);
0195 
0196   ~Event();
0197 
0198   void Dump(Stream *s) const;
0199 
0200   EventData *GetData() { return m_data_sp.get(); }
0201 
0202   const EventData *GetData() const { return m_data_sp.get(); }
0203 
0204   void SetData(EventData *new_data) { m_data_sp.reset(new_data); }
0205 
0206   uint32_t GetType() const { return m_type; }
0207 
0208   void SetType(uint32_t new_type) { m_type = new_type; }
0209 
0210   Broadcaster *GetBroadcaster() const {
0211     Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
0212         m_broadcaster_wp.lock();
0213     if (broadcaster_impl_sp)
0214       return broadcaster_impl_sp->GetBroadcaster();
0215     else
0216       return nullptr;
0217   }
0218 
0219   bool BroadcasterIs(Broadcaster *broadcaster) {
0220     Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
0221         m_broadcaster_wp.lock();
0222     if (broadcaster_impl_sp)
0223       return broadcaster_impl_sp->GetBroadcaster() == broadcaster;
0224     else
0225       return false;
0226   }
0227 
0228   void Clear() { m_data_sp.reset(); }
0229 
0230   /// This is used by Broadcasters with Primary Listeners to store the other
0231   /// Listeners till after the Event's DoOnRemoval has completed.
0232   void AddPendingListener(lldb::ListenerSP pending_listener_sp) {
0233     m_pending_listeners.push_back(pending_listener_sp);
0234   };
0235 
0236 private:
0237   // This is only called by Listener when it pops an event off the queue for
0238   // the listener.  It calls the Event Data's DoOnRemoval() method, which is
0239   // virtual and can be overridden by the specific data classes.
0240 
0241   void DoOnRemoval();
0242 
0243   // Called by Broadcaster::BroadcastEvent prior to letting all the listeners
0244   // know about it update the contained broadcaster so that events can be
0245   // popped off one queue and re-broadcast to others.
0246   void SetBroadcaster(Broadcaster *broadcaster) {
0247     m_broadcaster_wp = broadcaster->GetBroadcasterImpl();
0248   }
0249 
0250   Broadcaster::BroadcasterImplWP
0251       m_broadcaster_wp;        // The broadcaster that sent this event
0252   uint32_t m_type;             // The bit describing this event
0253   lldb::EventDataSP m_data_sp; // User specific data for this event
0254   std::vector<lldb::ListenerSP> m_pending_listeners;
0255   std::mutex m_listeners_mutex;
0256 
0257   Event(const Event &) = delete;
0258   const Event &operator=(const Event &) = delete;
0259   Event() = delete;
0260 };
0261 
0262 } // namespace lldb_private
0263 
0264 #endif // LLDB_UTILITY_EVENT_H