LLDB mainline
Broadcaster.h
Go to the documentation of this file.
1//===-- Broadcaster.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_BROADCASTER_H
10#define LLDB_UTILITY_BROADCASTER_H
11
13#include "lldb/lldb-defines.h"
14#include "lldb/lldb-forward.h"
15
16#include "llvm/ADT/SmallVector.h"
17
18#include <cstdint>
19#include <map>
20#include <memory>
21#include <mutex>
22#include <set>
23#include <string>
24#include <utility>
25#include <vector>
26
27namespace lldb_private {
28class Broadcaster;
29class EventData;
30class Listener;
31class Stream;
32} // namespace lldb_private
33
34namespace lldb_private {
35
36/// lldb::BroadcastEventSpec
37///
38/// This class is used to specify a kind of event to register for. The
39/// Debugger maintains a list of BroadcastEventSpec's and when it is made
41public:
42 BroadcastEventSpec(const ConstString &broadcaster_class, uint32_t event_bits)
43 : m_broadcaster_class(broadcaster_class), m_event_bits(event_bits) {}
44
46
48
50
51 /// Tell whether this BroadcastEventSpec is contained in in_spec. That is:
52 /// (a) the two spec's share the same broadcaster class (b) the event bits of
53 /// this spec are wholly contained in those of in_spec.
54 bool IsContainedIn(const BroadcastEventSpec &in_spec) const {
56 return false;
57 uint32_t in_bits = in_spec.GetEventBits();
58 if (in_bits == m_event_bits)
59 return true;
60
61 if ((m_event_bits & in_bits) != 0 && (m_event_bits & ~in_bits) == 0)
62 return true;
63
64 return false;
65 }
66
67 bool operator<(const BroadcastEventSpec &rhs) const;
68
69private:
72};
73
75 : public std::enable_shared_from_this<BroadcasterManager> {
76public:
77 friend class Listener;
78
79protected:
81
82public:
83 /// Listeners hold onto weak pointers to their broadcaster managers. So they
84 /// must be made into shared pointers, which you do with
85 /// MakeBroadcasterManager.
86 static lldb::BroadcasterManagerSP MakeBroadcasterManager();
87
89
90 uint32_t RegisterListenerForEvents(const lldb::ListenerSP &listener_sp,
91 const BroadcastEventSpec &event_spec);
92
93 bool UnregisterListenerForEvents(const lldb::ListenerSP &listener_sp,
94 const BroadcastEventSpec &event_spec);
95
96 lldb::ListenerSP
97 GetListenerForEventSpec(const BroadcastEventSpec &event_spec) const;
98
100
101 void RemoveListener(const lldb::ListenerSP &listener_sp);
102
103 void RemoveListener(Listener *listener);
104
105 void Clear();
106
107private:
108 typedef std::pair<BroadcastEventSpec, lldb::ListenerSP> event_listener_key;
109 typedef std::map<BroadcastEventSpec, lldb::ListenerSP> collection;
110 typedef std::set<lldb::ListenerSP> listener_collection;
113
114 mutable std::recursive_mutex m_manager_mutex;
115};
116
117/// \class Broadcaster Broadcaster.h "lldb/Utility/Broadcaster.h" An event
118/// broadcasting class.
119///
120/// The Broadcaster class is designed to be subclassed by objects that wish to
121/// vend events in a multi-threaded environment. Broadcaster objects can each
122/// vend 32 events. Each event is represented by a bit in a 32 bit value and
123/// these bits can be set:
124/// \see Broadcaster::SetEventBits(uint32_t)
125/// or cleared:
126/// \see Broadcaster::ResetEventBits(uint32_t)
127/// When an event gets set the Broadcaster object will notify the Listener
128/// object that is listening for the event (if there is one).
129///
130/// Subclasses should provide broadcast bit definitions for any events they
131/// vend, typically using an enumeration:
132/// \code
133/// class Foo : public Broadcaster
134/// {
135/// public:
136/// // Broadcaster event bits definitions.
137/// enum
138/// {
139/// eBroadcastBitOne = (1 << 0),
140/// eBroadcastBitTwo = (1 << 1),
141/// eBroadcastBitThree = (1 << 2),
142/// ...
143/// };
144/// \endcode
146 friend class Listener;
147 friend class Event;
148
149public:
150 /// Construct with a broadcaster with a name.
151 ///
152 /// \param[in] name
153 /// A NULL terminated C string that contains the name of the
154 /// broadcaster object.
155 Broadcaster(lldb::BroadcasterManagerSP manager_sp, const char *name);
156
157 /// Destructor.
158 ///
159 /// The destructor is virtual since this class gets subclassed.
160 virtual ~Broadcaster();
161
162 void CheckInWithManager();
163
164 /// Broadcast an event which has no associated data.
165 void BroadcastEvent(lldb::EventSP &event_sp) {
166 m_broadcaster_sp->BroadcastEvent(event_sp);
167 }
168
169 void BroadcastEventIfUnique(lldb::EventSP &event_sp) {
170 m_broadcaster_sp->BroadcastEventIfUnique(event_sp);
171 }
172
173 void BroadcastEvent(uint32_t event_type,
174 const lldb::EventDataSP &event_data_sp) {
175 m_broadcaster_sp->BroadcastEvent(event_type, event_data_sp);
176 }
177
178 void BroadcastEvent(uint32_t event_type, EventData *event_data = nullptr) {
179 m_broadcaster_sp->BroadcastEvent(event_type, event_data);
180 }
181
183 EventData *event_data = nullptr) {
184 m_broadcaster_sp->BroadcastEventIfUnique(event_type, event_data);
185 }
186
187 void Clear() { m_broadcaster_sp->Clear(); }
188
189 virtual void AddInitialEventsToListener(const lldb::ListenerSP &listener_sp,
190 uint32_t requested_events);
191
192 /// Listen for any events specified by \a event_mask.
193 ///
194 /// Only one listener can listen to each event bit in a given Broadcaster.
195 /// Once a listener has acquired an event bit, no other broadcaster will
196 /// have access to it until it is relinquished by the first listener that
197 /// gets it. The actual event bits that get acquired by \a listener may be
198 /// different from what is requested in \a event_mask, and to track this the
199 /// actual event bits that are acquired get returned.
200 ///
201 /// \param[in] listener_sp
202 /// The Listener object that wants to monitor the events that
203 /// get broadcast by this object.
204 ///
205 /// \param[in] event_mask
206 /// A bit mask that indicates which events the listener is
207 /// asking to monitor.
208 ///
209 /// \return
210 /// The actual event bits that were acquired by \a listener.
211 uint32_t AddListener(const lldb::ListenerSP &listener_sp,
212 uint32_t event_mask) {
213 return m_broadcaster_sp->AddListener(listener_sp, event_mask);
214 }
215
216 /// Get the NULL terminated C string name of this Broadcaster object.
217 ///
218 /// \return
219 /// The NULL terminated C string name of this Broadcaster.
221
222 /// Get the event name(s) for one or more event bits.
223 ///
224 /// \param[in] event_mask
225 /// A bit mask that indicates which events to get names for.
226 ///
227 /// \return
228 /// The NULL terminated C string name of this Broadcaster.
229 bool GetEventNames(Stream &s, const uint32_t event_mask,
230 bool prefix_with_broadcaster_name) const {
231 return m_broadcaster_sp->GetEventNames(s, event_mask,
232 prefix_with_broadcaster_name);
233 }
234
235 /// Set the name for an event bit.
236 ///
237 /// \param[in] event_mask
238 /// A bit mask that indicates which events the listener is
239 /// asking to monitor.
240 void SetEventName(uint32_t event_mask, const char *name) {
241 m_broadcaster_sp->SetEventName(event_mask, name);
242 }
243
244 const char *GetEventName(uint32_t event_mask) const {
245 return m_broadcaster_sp->GetEventName(event_mask);
246 }
247
249 return m_broadcaster_sp->EventTypeHasListeners(event_type);
250 }
251
252 /// Removes a Listener from this broadcasters list and frees the event bits
253 /// specified by \a event_mask that were previously acquired by \a listener
254 /// (assuming \a listener was listening to this object) for other listener
255 /// objects to use.
256 ///
257 /// \param[in] listener_sp
258 /// A Listener object that previously called AddListener.
259 ///
260 /// \param[in] event_mask
261 /// The event bits \a listener wishes to relinquish.
262 ///
263 /// \return
264 /// \b True if the listener was listening to this broadcaster
265 /// and was removed, \b false otherwise.
266 ///
267 /// \see uint32_t Broadcaster::AddListener (Listener*, uint32_t)
268 bool RemoveListener(const lldb::ListenerSP &listener_sp,
269 uint32_t event_mask = UINT32_MAX) {
270 return m_broadcaster_sp->RemoveListener(listener_sp, event_mask);
271 }
272
273 /// Provides a simple mechanism to temporarily redirect events from
274 /// broadcaster. When you call this function passing in a listener and
275 /// event type mask, all events from the broadcaster matching the mask will
276 /// now go to the hijacking listener. Only one hijack can occur at a time.
277 /// If we need more than this we will have to implement a Listener stack.
278 ///
279 /// \param[in] listener_sp
280 /// A Listener object. You do not need to call StartListeningForEvents
281 /// for this broadcaster (that would fail anyway since the event bits
282 /// would most likely be taken by the listener(s) you are usurping.
283 ///
284 /// \param[in] event_mask
285 /// The event bits \a listener wishes to hijack.
286 ///
287 /// \return
288 /// \b True if the event mask could be hijacked, \b false otherwise.
289 ///
290 /// \see uint32_t Broadcaster::AddListener (Listener*, uint32_t)
291 bool HijackBroadcaster(const lldb::ListenerSP &listener_sp,
292 uint32_t event_mask = UINT32_MAX) {
293 return m_broadcaster_sp->HijackBroadcaster(listener_sp, event_mask);
294 }
295
296 bool IsHijackedForEvent(uint32_t event_mask) {
297 return m_broadcaster_sp->IsHijackedForEvent(event_mask);
298 }
299
300 /// Restore the state of the Broadcaster from a previous hijack attempt.
301 void RestoreBroadcaster() { m_broadcaster_sp->RestoreBroadcaster(); }
302
303 /// This needs to be filled in if you are going to register the broadcaster
304 /// with the broadcaster manager and do broadcaster class matching.
305 /// FIXME: Probably should make a ManagedBroadcaster subclass with all the
306 /// bits needed to work with the BroadcasterManager, so that it is clearer
307 /// how to add one.
308 virtual ConstString &GetBroadcasterClass() const;
309
310 lldb::BroadcasterManagerSP GetManager();
311
312 virtual void SetShadowListener(lldb::ListenerSP listener_sp) {
313 m_broadcaster_sp->m_shadow_listener = listener_sp;
314 }
315
316protected:
317 /// BroadcasterImpl contains the actual Broadcaster implementation. The
318 /// Broadcaster makes a BroadcasterImpl which lives as long as it does. The
319 /// Listeners & the Events hold a weak pointer to the BroadcasterImpl, so
320 /// that they can survive if a Broadcaster they were listening to is
321 /// destroyed w/o their being able to unregister from it (which can happen if
322 /// the Broadcasters & Listeners are being destroyed on separate threads
323 /// simultaneously. The Broadcaster itself can't be shared out as a weak
324 /// pointer, because some things that are broadcasters (e.g. the Target and
325 /// the Process) are shared in their own right.
326 ///
327 /// For the most part, the Broadcaster functions dispatch to the
328 /// BroadcasterImpl, and are documented in the public Broadcaster API above.
330 friend class Listener;
331 friend class Broadcaster;
332
333 public:
334 BroadcasterImpl(Broadcaster &broadcaster);
335
336 ~BroadcasterImpl() = default;
337
338 void BroadcastEvent(lldb::EventSP &event_sp);
339
340 void BroadcastEventIfUnique(lldb::EventSP &event_sp);
341
342 void BroadcastEvent(uint32_t event_type, EventData *event_data = nullptr);
343
344 void BroadcastEvent(uint32_t event_type,
345 const lldb::EventDataSP &event_data_sp);
346
347 void BroadcastEventIfUnique(uint32_t event_type,
348 EventData *event_data = nullptr);
349
350 void Clear();
351
352 uint32_t AddListener(const lldb::ListenerSP &listener_sp,
353 uint32_t event_mask);
354
355 const char *GetBroadcasterName() const {
357 }
358
360
361 bool GetEventNames(Stream &s, const uint32_t event_mask,
362 bool prefix_with_broadcaster_name) const;
363
364 void SetEventName(uint32_t event_mask, const char *name) {
365 m_event_names[event_mask] = name;
366 }
367
368 const char *GetEventName(uint32_t event_mask) const {
369 const auto pos = m_event_names.find(event_mask);
370 if (pos != m_event_names.end())
371 return pos->second.c_str();
372 return nullptr;
373 }
374
375 bool EventTypeHasListeners(uint32_t event_type);
376
378 uint32_t event_mask = UINT32_MAX);
379
380 bool RemoveListener(const lldb::ListenerSP &listener_sp,
381 uint32_t event_mask = UINT32_MAX);
382
383 bool HijackBroadcaster(const lldb::ListenerSP &listener_sp,
384 uint32_t event_mask = UINT32_MAX);
385
386 bool IsHijackedForEvent(uint32_t event_mask);
387
388 void RestoreBroadcaster();
389
390 protected:
391 void PrivateBroadcastEvent(lldb::EventSP &event_sp, bool unique);
392
393 const char *GetHijackingListenerName();
394
395 typedef llvm::SmallVector<std::pair<lldb::ListenerWP, uint32_t>, 4>
397 typedef std::map<uint32_t, std::string> event_names_map;
398
399 llvm::SmallVector<std::pair<lldb::ListenerSP, uint32_t &>, 4>
400 GetListeners();
401
402 /// The broadcaster that this implements.
404
405 /// Optionally define event names for readability and logging for each
406 /// event bit.
408
409 /// A list of Listener / event_mask pairs that are listening to this
410 /// broadcaster.
412
413 /// A mutex that protects \a m_listeners.
414 std::recursive_mutex m_listeners_mutex;
415
416 /// A simple mechanism to intercept events from a broadcaster
417 std::vector<lldb::ListenerSP> m_hijacking_listeners;
418
419 /// At some point we may want to have a stack or Listener collections, but
420 /// for now this is just for private hijacking.
421 std::vector<uint32_t> m_hijacking_masks;
422
423 /// A optional listener that all private events get also broadcasted to,
424 /// on top the hijacked / default listeners.
425 lldb::ListenerSP m_shadow_listener = nullptr;
426
427 private:
429 const BroadcasterImpl &operator=(const BroadcasterImpl &) = delete;
430 };
431
432 typedef std::shared_ptr<BroadcasterImpl> BroadcasterImplSP;
433 typedef std::weak_ptr<BroadcasterImpl> BroadcasterImplWP;
434
436
438 return m_broadcaster_sp->GetHijackingListenerName();
439 }
440
441private:
443 lldb::BroadcasterManagerSP m_manager_sp;
444
445 /// The name of this broadcaster object.
447
448 Broadcaster(const Broadcaster &) = delete;
449 const Broadcaster &operator=(const Broadcaster &) = delete;
450};
451
452} // namespace lldb_private
453
454#endif // LLDB_UTILITY_BROADCASTER_H
lldb::BroadcastEventSpec
Definition: Broadcaster.h:40
BroadcastEventSpec(const ConstString &broadcaster_class, uint32_t event_bits)
Definition: Broadcaster.h:42
bool operator<(const BroadcastEventSpec &rhs) const
ConstString GetBroadcasterClass() const
Definition: Broadcaster.h:47
bool IsContainedIn(const BroadcastEventSpec &in_spec) const
Tell whether this BroadcastEventSpec is contained in in_spec.
Definition: Broadcaster.h:54
listener_collection m_listeners
Definition: Broadcaster.h:112
static lldb::BroadcasterManagerSP MakeBroadcasterManager()
Listeners hold onto weak pointers to their broadcaster managers.
uint32_t RegisterListenerForEvents(const lldb::ListenerSP &listener_sp, const BroadcastEventSpec &event_spec)
std::recursive_mutex m_manager_mutex
Definition: Broadcaster.h:114
void RemoveListener(const lldb::ListenerSP &listener_sp)
void SignUpListenersForBroadcaster(Broadcaster &broadcaster)
bool UnregisterListenerForEvents(const lldb::ListenerSP &listener_sp, const BroadcastEventSpec &event_spec)
std::set< lldb::ListenerSP > listener_collection
Definition: Broadcaster.h:110
std::pair< BroadcastEventSpec, lldb::ListenerSP > event_listener_key
Definition: Broadcaster.h:108
lldb::ListenerSP GetListenerForEventSpec(const BroadcastEventSpec &event_spec) const
std::map< BroadcastEventSpec, lldb::ListenerSP > collection
Definition: Broadcaster.h:109
BroadcasterImpl contains the actual Broadcaster implementation.
Definition: Broadcaster.h:329
bool IsHijackedForEvent(uint32_t event_mask)
void BroadcastEvent(lldb::EventSP &event_sp)
BroadcasterImpl(const BroadcasterImpl &)=delete
void PrivateBroadcastEvent(lldb::EventSP &event_sp, bool unique)
uint32_t AddListener(const lldb::ListenerSP &listener_sp, uint32_t event_mask)
Broadcaster & m_broadcaster
The broadcaster that this implements.
Definition: Broadcaster.h:403
bool EventTypeHasListeners(uint32_t event_type)
lldb::ListenerSP m_shadow_listener
A optional listener that all private events get also broadcasted to, on top the hijacked / default li...
Definition: Broadcaster.h:425
const char * GetEventName(uint32_t event_mask) const
Definition: Broadcaster.h:368
std::vector< uint32_t > m_hijacking_masks
At some point we may want to have a stack or Listener collections, but for now this is just for priva...
Definition: Broadcaster.h:421
std::recursive_mutex m_listeners_mutex
A mutex that protects m_listeners.
Definition: Broadcaster.h:414
bool RemoveListener(lldb_private::Listener *listener, uint32_t event_mask=UINT32_MAX)
bool GetEventNames(Stream &s, const uint32_t event_mask, bool prefix_with_broadcaster_name) const
Definition: Broadcaster.cpp:84
std::vector< lldb::ListenerSP > m_hijacking_listeners
A simple mechanism to intercept events from a broadcaster.
Definition: Broadcaster.h:417
collection m_listeners
A list of Listener / event_mask pairs that are listening to this broadcaster.
Definition: Broadcaster.h:411
void BroadcastEventIfUnique(lldb::EventSP &event_sp)
llvm::SmallVector< std::pair< lldb::ListenerSP, uint32_t & >, 4 > GetListeners()
Definition: Broadcaster.cpp:53
bool HijackBroadcaster(const lldb::ListenerSP &listener_sp, uint32_t event_mask=UINT32_MAX)
llvm::SmallVector< std::pair< lldb::ListenerWP, uint32_t >, 4 > collection
Definition: Broadcaster.h:396
void SetEventName(uint32_t event_mask, const char *name)
Definition: Broadcaster.h:364
event_names_map m_event_names
Optionally define event names for readability and logging for each event bit.
Definition: Broadcaster.h:407
std::map< uint32_t, std::string > event_names_map
Definition: Broadcaster.h:397
const BroadcasterImpl & operator=(const BroadcasterImpl &)=delete
An event broadcasting class.
Definition: Broadcaster.h:145
lldb::BroadcasterManagerSP GetManager()
uint32_t AddListener(const lldb::ListenerSP &listener_sp, uint32_t event_mask)
Listen for any events specified by event_mask.
Definition: Broadcaster.h:211
bool EventTypeHasListeners(uint32_t event_type)
Definition: Broadcaster.h:248
ConstString GetBroadcasterName()
Get the NULL terminated C string name of this Broadcaster object.
Definition: Broadcaster.h:220
bool RemoveListener(const lldb::ListenerSP &listener_sp, uint32_t event_mask=UINT32_MAX)
Removes a Listener from this broadcasters list and frees the event bits specified by event_mask that ...
Definition: Broadcaster.h:268
void BroadcastEventIfUnique(uint32_t event_type, EventData *event_data=nullptr)
Definition: Broadcaster.h:182
void RestoreBroadcaster()
Restore the state of the Broadcaster from a previous hijack attempt.
Definition: Broadcaster.h:301
virtual void SetShadowListener(lldb::ListenerSP listener_sp)
Definition: Broadcaster.h:312
void SetEventName(uint32_t event_mask, const char *name)
Set the name for an event bit.
Definition: Broadcaster.h:240
virtual void AddInitialEventsToListener(const lldb::ListenerSP &listener_sp, uint32_t requested_events)
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:433
void BroadcastEvent(uint32_t event_type, const lldb::EventDataSP &event_data_sp)
Definition: Broadcaster.h:173
bool HijackBroadcaster(const lldb::ListenerSP &listener_sp, uint32_t event_mask=UINT32_MAX)
Provides a simple mechanism to temporarily redirect events from broadcaster.
Definition: Broadcaster.h:291
virtual ConstString & GetBroadcasterClass() const
This needs to be filled in if you are going to register the broadcaster with the broadcaster manager ...
BroadcasterImplSP GetBroadcasterImpl()
Definition: Broadcaster.h:435
lldb::BroadcasterManagerSP m_manager_sp
Definition: Broadcaster.h:443
const ConstString m_broadcaster_name
The name of this broadcaster object.
Definition: Broadcaster.h:446
void BroadcastEvent(uint32_t event_type, EventData *event_data=nullptr)
Definition: Broadcaster.h:178
void BroadcastEventIfUnique(lldb::EventSP &event_sp)
Definition: Broadcaster.h:169
const Broadcaster & operator=(const Broadcaster &)=delete
Broadcaster(lldb::BroadcasterManagerSP manager_sp, const char *name)
Construct with a broadcaster with a name.
const char * GetHijackingListenerName()
Definition: Broadcaster.h:437
void BroadcastEvent(lldb::EventSP &event_sp)
Broadcast an event which has no associated data.
Definition: Broadcaster.h:165
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:432
BroadcasterImplSP m_broadcaster_sp
Definition: Broadcaster.h:442
bool IsHijackedForEvent(uint32_t event_mask)
Definition: Broadcaster.h:296
Broadcaster(const Broadcaster &)=delete
const char * GetEventName(uint32_t event_mask) const
Definition: Broadcaster.h:244
bool GetEventNames(Stream &s, const uint32_t event_mask, bool prefix_with_broadcaster_name) const
Get the event name(s) for one or more event bits.
Definition: Broadcaster.h:229
virtual ~Broadcaster()
Destructor.
Definition: Broadcaster.cpp:38
A uniqued constant string class.
Definition: ConstString.h:39
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:195
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14