LLDB  mainline
Event.h
Go to the documentation of this file.
1 //===-- Event.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 
9 #ifndef LLDB_UTILITY_EVENT_H
10 #define LLDB_UTILITY_EVENT_H
11 
14 #include "lldb/Utility/Predicate.h"
16 #include "lldb/lldb-defines.h"
17 #include "lldb/lldb-forward.h"
18 
19 #include "llvm/ADT/StringRef.h"
20 
21 #include <chrono>
22 #include <memory>
23 #include <string>
24 
25 #include <stddef.h>
26 #include <stdint.h>
27 
28 namespace lldb_private {
29 class Event;
30 }
31 namespace lldb_private {
32 class Stream;
33 }
34 
35 namespace lldb_private {
36 
37 // lldb::EventData
38 class EventData {
39  friend class Event;
40 
41 public:
42  EventData();
43 
44  virtual ~EventData();
45 
46  virtual ConstString GetFlavor() const = 0;
47 
48  virtual void Dump(Stream *s) const;
49 
50 private:
51  virtual void DoOnRemoval(Event *event_ptr) {}
52 
53  DISALLOW_COPY_AND_ASSIGN(EventData);
54 };
55 
56 // lldb::EventDataBytes
57 class EventDataBytes : public EventData {
58 public:
59  // Constructors
61 
62  EventDataBytes(const char *cstr);
63 
64  EventDataBytes(llvm::StringRef str);
65 
66  EventDataBytes(const void *src, size_t src_len);
67 
68  ~EventDataBytes() override;
69 
70  // Member functions
71  ConstString GetFlavor() const override;
72 
73  void Dump(Stream *s) const override;
74 
75  const void *GetBytes() const;
76 
77  size_t GetByteSize() const;
78 
79  void SetBytes(const void *src, size_t src_len);
80 
81  void SwapBytes(std::string &new_bytes);
82 
83  void SetBytesFromCString(const char *cstr);
84 
85  // Static functions
86  static const EventDataBytes *GetEventDataFromEvent(const Event *event_ptr);
87 
88  static const void *GetBytesFromEvent(const Event *event_ptr);
89 
90  static size_t GetByteSizeFromEvent(const Event *event_ptr);
91 
92  static ConstString GetFlavorString();
93 
94 private:
95  std::string m_bytes;
96 
97  DISALLOW_COPY_AND_ASSIGN(EventDataBytes);
98 };
99 
100 class EventDataReceipt : public EventData {
101 public:
102  EventDataReceipt() : EventData(), m_predicate(false) {}
103 
104  ~EventDataReceipt() override {}
105 
107  static ConstString g_flavor("Process::ProcessEventData");
108  return g_flavor;
109  }
110 
111  ConstString GetFlavor() const override { return GetFlavorString(); }
112 
114  return m_predicate.WaitForValueEqualTo(true, timeout);
115  }
116 
117 private:
118  Predicate<bool> m_predicate;
119 
120  void DoOnRemoval(Event *event_ptr) override {
121  m_predicate.SetValue(true, eBroadcastAlways);
122  }
123 };
124 
125 /// This class handles one or more StructuredData::Dictionary entries
126 /// that are raised for structured data events.
127 
129 public:
130  // Constructors
132 
133  EventDataStructuredData(const lldb::ProcessSP &process_sp,
134  const StructuredData::ObjectSP &object_sp,
135  const lldb::StructuredDataPluginSP &plugin_sp);
136 
137  ~EventDataStructuredData() override;
138 
139  // Member functions
140  ConstString GetFlavor() const override;
141 
142  void Dump(Stream *s) const override;
143 
144  const lldb::ProcessSP &GetProcess() const;
145 
146  const StructuredData::ObjectSP &GetObject() const;
147 
148  const lldb::StructuredDataPluginSP &GetStructuredDataPlugin() const;
149 
150  void SetProcess(const lldb::ProcessSP &process_sp);
151 
152  void SetObject(const StructuredData::ObjectSP &object_sp);
153 
154  void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp);
155 
156  // Static functions
157  static const EventDataStructuredData *
158  GetEventDataFromEvent(const Event *event_ptr);
159 
160  static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr);
161 
162  static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr);
163 
164  static lldb::StructuredDataPluginSP
165  GetPluginFromEvent(const Event *event_ptr);
166 
167  static ConstString GetFlavorString();
168 
169 private:
170  lldb::ProcessSP m_process_sp;
171  StructuredData::ObjectSP m_object_sp;
172  lldb::StructuredDataPluginSP m_plugin_sp;
173 
174  DISALLOW_COPY_AND_ASSIGN(EventDataStructuredData);
175 };
176 
177 // lldb::Event
178 class Event {
179  friend class Listener;
180  friend class EventData;
182 
183 public:
184  Event(Broadcaster *broadcaster, uint32_t event_type,
185  EventData *data = nullptr);
186 
187  Event(Broadcaster *broadcaster, uint32_t event_type,
188  const lldb::EventDataSP &event_data_sp);
189 
190  Event(uint32_t event_type, EventData *data = nullptr);
191 
192  Event(uint32_t event_type, const lldb::EventDataSP &event_data_sp);
193 
194  ~Event();
195 
196  void Dump(Stream *s) const;
197 
198  EventData *GetData() { return m_data_sp.get(); }
199 
200  const EventData *GetData() const { return m_data_sp.get(); }
201 
202  void SetData(EventData *new_data) { m_data_sp.reset(new_data); }
203 
204  uint32_t GetType() const { return m_type; }
205 
206  void SetType(uint32_t new_type) { m_type = new_type; }
207 
209  Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
210  m_broadcaster_wp.lock();
211  if (broadcaster_impl_sp)
212  return broadcaster_impl_sp->GetBroadcaster();
213  else
214  return nullptr;
215  }
216 
217  bool BroadcasterIs(Broadcaster *broadcaster) {
218  Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
219  m_broadcaster_wp.lock();
220  if (broadcaster_impl_sp)
221  return broadcaster_impl_sp->GetBroadcaster() == broadcaster;
222  else
223  return false;
224  }
225 
226  void Clear() { m_data_sp.reset(); }
227 
228 private:
229  // This is only called by Listener when it pops an event off the queue for
230  // the listener. It calls the Event Data's DoOnRemoval() method, which is
231  // virtual and can be overridden by the specific data classes.
232 
233  void DoOnRemoval();
234 
235  // Called by Broadcaster::BroadcastEvent prior to letting all the listeners
236  // know about it update the contained broadcaster so that events can be
237  // popped off one queue and re-broadcast to others.
238  void SetBroadcaster(Broadcaster *broadcaster) {
239  m_broadcaster_wp = broadcaster->GetBroadcasterImpl();
240  }
241 
243  m_broadcaster_wp; // The broadcaster that sent this event
244  uint32_t m_type; // The bit describing this event
245  lldb::EventDataSP m_data_sp; // User specific data for this event
246 
247  DISALLOW_COPY_AND_ASSIGN(Event);
248  Event(); // Disallow default constructor
249 };
250 
251 } // namespace lldb_private
252 
253 #endif // LLDB_UTILITY_EVENT_H
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static ConstString GetFlavorString()
Definition: Event.h:106
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void SetData(EventData *new_data)
Definition: Event.h:202
friend class Event
Definition: Event.h:39
Broadcaster * GetBroadcaster() const
Definition: Event.h:208
An event broadcasting class.
Definition: Broadcaster.h:250
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:128
ConstString GetFlavor() const override
Definition: Event.h:111
bool BroadcasterIs(Broadcaster *broadcaster)
Definition: Event.h:217
bool WaitForEventReceived(const Timeout< std::micro > &timeout=llvm::None)
Definition: Event.h:113
~EventDataReceipt() override
Definition: Event.h:104
uint32_t GetType() const
Definition: Event.h:204
BroadcasterImplSP GetBroadcasterImpl()
Definition: Broadcaster.h:544
Always send a broadcast when the value is modified.
Definition: Predicate.h:28
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:541
virtual void Dump(Stream *s) const
Definition: Event.cpp:94
void SetValue(T value, PredicateBroadcastType broadcast_type)
Value set accessor.
Definition: Predicate.h:90
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:542
const EventData * GetData() const
Definition: Event.h:200
A uniqued constant string class.
Definition: ConstString.h:38
EventData * GetData()
Definition: Event.h:198
std::shared_ptr< Object > ObjectSP
void SetType(uint32_t new_type)
Definition: Event.h:206
virtual ConstString GetFlavor() const =0