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
15#include "lldb/lldb-defines.h"
16#include "lldb/lldb-forward.h"
17
18#include "llvm/ADT/StringRef.h"
19
20#include <chrono>
21#include <memory>
22#include <string>
23
24#include <cstddef>
25#include <cstdint>
26
27namespace lldb_private {
28class Event;
29class Stream;
30}
31
32namespace lldb_private {
33
34// lldb::EventData
35class EventData {
36 friend class Event;
37
38public:
40
41 virtual ~EventData();
42
43 virtual llvm::StringRef GetFlavor() const = 0;
44
45 virtual Log *GetLogChannel() { return nullptr; }
46
47 virtual void Dump(Stream *s) const;
48
49private:
50 /// This will be queried for a Broadcaster with a primary and some secondary
51 /// listeners after the primary listener pulled the event from the event queue
52 /// and ran its DoOnRemoval, right before the event is delivered.
53 /// If it returns true, the event will also be forwarded to the secondary
54 /// listeners, and if false, event propagation stops at the primary listener.
55 /// Some broadcasters (particularly the Process broadcaster) fetch events on
56 /// a private Listener, and then forward the event to the Public Listeners
57 /// after some processing. The Process broadcaster does not want to forward
58 /// to the secondary listeners at the private processing stage.
59 virtual bool ForwardEventToPendingListeners(Event *event_ptr) { return true; }
60
61 virtual void DoOnRemoval(Event *event_ptr) {}
62
63 EventData(const EventData &) = delete;
64 const EventData &operator=(const EventData &) = delete;
65};
66
67// lldb::EventDataBytes
68class EventDataBytes : public EventData {
69public:
70 // Constructors
72
73 EventDataBytes(llvm::StringRef str);
74
75 ~EventDataBytes() override;
76
77 // Member functions
78 llvm::StringRef GetFlavor() const override;
79
80 void Dump(Stream *s) const override;
81
82 const void *GetBytes() const;
83
84 size_t GetByteSize() const;
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
93 static llvm::StringRef GetFlavorString();
94
95private:
96 std::string m_bytes;
97
98 EventDataBytes(const EventDataBytes &) = delete;
99 const EventDataBytes &operator=(const EventDataBytes &) = delete;
100};
101
103public:
105
106 ~EventDataReceipt() override = default;
107
108 static llvm::StringRef GetFlavorString();
109
110 llvm::StringRef GetFlavor() const override { return GetFlavorString(); }
111
112 bool WaitForEventReceived(const Timeout<std::micro> &timeout = std::nullopt) {
113 return m_predicate.WaitForValueEqualTo(true, timeout);
114 }
115
116private:
118
119 void DoOnRemoval(Event *event_ptr) override {
120 m_predicate.SetValue(true, eBroadcastAlways);
121 }
122};
123
124/// This class handles one or more StructuredData::Dictionary entries
125/// that are raised for structured data events.
126
128public:
129 // Constructors
131
133 const StructuredData::ObjectSP &object_sp,
134 const lldb::StructuredDataPluginSP &plugin_sp);
135
137
138 // Member functions
139 llvm::StringRef GetFlavor() const override;
140
141 void Dump(Stream *s) const override;
142
143 const lldb::ProcessSP &GetProcess() const;
144
145 const StructuredData::ObjectSP &GetObject() const;
146
148
149 void SetProcess(const lldb::ProcessSP &process_sp);
150
151 void SetObject(const StructuredData::ObjectSP &object_sp);
152
154
155 // Static functions
156 static const EventDataStructuredData *
157 GetEventDataFromEvent(const Event *event_ptr);
158
159 static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr);
160
161 static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr);
162
164 GetPluginFromEvent(const Event *event_ptr);
165
166 static llvm::StringRef GetFlavorString();
167
168private:
172
176};
177
178// lldb::Event
179class Event : public std::enable_shared_from_this<Event> {
180 friend class Listener;
181 friend class EventData;
183
184public:
185 Event(Broadcaster *broadcaster, uint32_t event_type,
186 EventData *data = nullptr);
187
188 Event(Broadcaster *broadcaster, uint32_t event_type,
189 const lldb::EventDataSP &event_data_sp);
190
191 Event(uint32_t event_type, EventData *data = nullptr);
192
193 Event(uint32_t event_type, const lldb::EventDataSP &event_data_sp);
194
196
197 void Dump(Stream *s) const;
198
199 EventData *GetData() { return m_data_sp.get(); }
200
201 const EventData *GetData() const { return m_data_sp.get(); }
202
203 void SetData(EventData *new_data) { m_data_sp.reset(new_data); }
204
205 uint32_t GetType() const { return m_type; }
206
207 void SetType(uint32_t new_type) { m_type = new_type; }
208
210 Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
211 m_broadcaster_wp.lock();
212 if (broadcaster_impl_sp)
213 return broadcaster_impl_sp->GetBroadcaster();
214 else
215 return nullptr;
216 }
217
218 bool BroadcasterIs(Broadcaster *broadcaster) {
219 Broadcaster::BroadcasterImplSP broadcaster_impl_sp =
220 m_broadcaster_wp.lock();
221 if (broadcaster_impl_sp)
222 return broadcaster_impl_sp->GetBroadcaster() == broadcaster;
223 else
224 return false;
225 }
226
227 void Clear() { m_data_sp.reset(); }
228
229 /// This is used by Broadcasters with Primary Listeners to store the other
230 /// Listeners till after the Event's DoOnRemoval has completed.
231 void AddPendingListener(lldb::ListenerSP pending_listener_sp) {
232 m_pending_listeners.push_back(pending_listener_sp);
233 };
234
235private:
236 // This is only called by Listener when it pops an event off the queue for
237 // the listener. It calls the Event Data's DoOnRemoval() method, which is
238 // virtual and can be overridden by the specific data classes.
239
240 void DoOnRemoval();
241
242 // Called by Broadcaster::BroadcastEvent prior to letting all the listeners
243 // know about it update the contained broadcaster so that events can be
244 // popped off one queue and re-broadcast to others.
245 void SetBroadcaster(Broadcaster *broadcaster) {
246 m_broadcaster_wp = broadcaster->GetBroadcasterImpl();
247 }
248
250 m_broadcaster_wp; // The broadcaster that sent this event
251 uint32_t m_type; // The bit describing this event
252 lldb::EventDataSP m_data_sp; // User specific data for this event
253 std::vector<lldb::ListenerSP> m_pending_listeners;
255
256 Event(const Event &) = delete;
257 const Event &operator=(const Event &) = delete;
258 Event() = delete;
259};
260
261} // namespace lldb_private
262
263#endif // LLDB_UTILITY_EVENT_H
BroadcasterImpl contains the actual Broadcaster implementation.
An event broadcasting class.
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
BroadcasterImplSP GetBroadcasterImpl()
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
const EventDataBytes & operator=(const EventDataBytes &)=delete
const void * GetBytes() const
Definition Event.cpp:140
static llvm::StringRef GetFlavorString()
Definition Event.cpp:124
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition Event.cpp:146
static size_t GetByteSizeFromEvent(const Event *event_ptr)
Definition Event.cpp:153
EventDataBytes(const EventDataBytes &)=delete
llvm::StringRef GetFlavor() const override
Definition Event.cpp:126
void Dump(Stream *s) const override
Definition Event.cpp:130
static const EventDataBytes * GetEventDataFromEvent(const Event *event_ptr)
Definition Event.cpp:161
size_t GetByteSize() const
Definition Event.cpp:144
static llvm::StringRef GetFlavorString()
Definition Event.cpp:171
llvm::StringRef GetFlavor() const override
Definition Event.h:110
Predicate< bool > m_predicate
Definition Event.h:117
void DoOnRemoval(Event *event_ptr) override
Definition Event.h:119
bool WaitForEventReceived(const Timeout< std::micro > &timeout=std::nullopt)
Definition Event.h:112
~EventDataReceipt() override=default
llvm::StringRef GetFlavor() const override
Definition Event.cpp:193
EventDataStructuredData(const EventDataStructuredData &)=delete
const StructuredData::ObjectSP & GetObject() const
Definition Event.cpp:209
const lldb::ProcessSP & GetProcess() const
Definition Event.cpp:205
void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp)
Definition Event.cpp:227
lldb::StructuredDataPluginSP m_plugin_sp
Definition Event.h:171
const lldb::StructuredDataPluginSP & GetStructuredDataPlugin() const
Definition Event.cpp:214
void SetObject(const StructuredData::ObjectSP &object_sp)
Definition Event.cpp:222
StructuredData::ObjectSP m_object_sp
Definition Event.h:170
static llvm::StringRef GetFlavorString()
Definition Event.cpp:273
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition Event.cpp:247
void SetProcess(const lldb::ProcessSP &process_sp)
Definition Event.cpp:218
const EventDataStructuredData & operator=(const EventDataStructuredData &)=delete
static lldb::StructuredDataPluginSP GetPluginFromEvent(const Event *event_ptr)
Definition Event.cpp:265
static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr)
Definition Event.cpp:256
static const EventDataStructuredData * GetEventDataFromEvent(const Event *event_ptr)
Definition Event.cpp:235
void Dump(Stream *s) const override
Definition Event.cpp:197
const EventData & operator=(const EventData &)=delete
virtual bool ForwardEventToPendingListeners(Event *event_ptr)
This will be queried for a Broadcaster with a primary and some secondary listeners after the primary ...
Definition Event.h:59
friend class Event
Definition Event.h:36
virtual void DoOnRemoval(Event *event_ptr)
Definition Event.h:61
virtual Log * GetLogChannel()
Definition Event.h:45
virtual void Dump(Stream *s) const
Definition Event.cpp:111
EventData(const EventData &)=delete
virtual llvm::StringRef GetFlavor() const =0
EventData * GetData()
Definition Event.h:199
Broadcaster::BroadcasterImplWP m_broadcaster_wp
Definition Event.h:250
bool BroadcasterIs(Broadcaster *broadcaster)
Definition Event.h:218
Event(const Event &)=delete
void SetType(uint32_t new_type)
Definition Event.h:207
uint32_t GetType() const
Definition Event.h:205
friend class Listener
Definition Event.h:180
lldb::EventDataSP m_data_sp
Definition Event.h:252
friend class EventData
Definition Event.h:181
void AddPendingListener(lldb::ListenerSP pending_listener_sp)
This is used by Broadcasters with Primary Listeners to store the other Listeners till after the Event...
Definition Event.h:231
uint32_t m_type
Definition Event.h:251
const EventData * GetData() const
Definition Event.h:201
std::mutex m_listeners_mutex
Definition Event.h:254
void SetData(EventData *new_data)
Definition Event.h:203
const Event & operator=(const Event &)=delete
void Dump(Stream *s) const
Definition Event.cpp:50
std::vector< lldb::ListenerSP > m_pending_listeners
Definition Event.h:253
Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data=nullptr)
Definition Event.cpp:33
void SetBroadcaster(Broadcaster *broadcaster)
Definition Event.h:245
Broadcaster * GetBroadcaster() const
Definition Event.h:209
A C++ wrapper class for providing threaded access to a value of type T.
Definition Predicate.h:42
A stream class that can stream formatted output to a file.
Definition Stream.h:28
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
@ eBroadcastAlways
Always send a broadcast when the value is modified.
Definition Predicate.h:29
std::shared_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginSP
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::Listener > ListenerSP
std::shared_ptr< lldb_private::EventData > EventDataSP