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