LLDB  mainline
Broadcaster.cpp
Go to the documentation of this file.
1 //===-- Broadcaster.cpp -----------------------------------------*- 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 
10 
11 #include "lldb/Utility/Event.h"
12 #include "lldb/Utility/Listener.h"
13 #include "lldb/Utility/Log.h"
14 #include "lldb/Utility/Logging.h"
15 #include "lldb/Utility/Stream.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <type_traits>
21 
22 #include <assert.h>
23 #include <stddef.h>
24 
25 using namespace lldb;
26 using namespace lldb_private;
27 
28 Broadcaster::Broadcaster(BroadcasterManagerSP manager_sp, const char *name)
29  : m_broadcaster_sp(std::make_shared<BroadcasterImpl>(*this)),
30  m_manager_sp(manager_sp), m_broadcaster_name(name) {
32  if (log)
33  log->Printf("%p Broadcaster::Broadcaster(\"%s\")",
34  static_cast<void *>(this), GetBroadcasterName().AsCString());
35 }
36 
38  : m_broadcaster(broadcaster), m_listeners(), m_listeners_mutex(),
39  m_hijacking_listeners(), m_hijacking_masks() {}
40 
43  if (log)
44  log->Printf("%p Broadcaster::~Broadcaster(\"%s\")",
45  static_cast<void *>(this), m_broadcaster_name.AsCString());
46 
47  Clear();
48 }
49 
51  if (m_manager_sp) {
52  m_manager_sp->SignUpListenersForBroadcaster(*this);
53  }
54 }
55 
56 llvm::SmallVector<std::pair<ListenerSP, uint32_t &>, 4>
58  llvm::SmallVector<std::pair<ListenerSP, uint32_t &>, 4> listeners;
59  listeners.reserve(m_listeners.size());
60 
61  for (auto it = m_listeners.begin(); it != m_listeners.end();) {
62  lldb::ListenerSP curr_listener_sp(it->first.lock());
63  if (curr_listener_sp && it->second) {
64  listeners.emplace_back(std::move(curr_listener_sp), it->second);
65  ++it;
66  } else
67  it = m_listeners.erase(it);
68  }
69 
70  return listeners;
71 }
72 
74  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
75 
76  // Make sure the listener forgets about this broadcaster. We do this in the
77  // broadcaster in case the broadcaster object initiates the removal.
78  for (auto &pair : GetListeners())
79  pair.first->BroadcasterWillDestruct(&m_broadcaster);
80 
81  m_listeners.clear();
82 }
83 
85  return &m_broadcaster;
86 }
87 
89  Stream &s, uint32_t event_mask, bool prefix_with_broadcaster_name) const {
90  uint32_t num_names_added = 0;
91  if (event_mask && !m_event_names.empty()) {
92  event_names_map::const_iterator end = m_event_names.end();
93  for (uint32_t bit = 1u, mask = event_mask; mask != 0 && bit != 0;
94  bit <<= 1, mask >>= 1) {
95  if (mask & 1) {
96  event_names_map::const_iterator pos = m_event_names.find(bit);
97  if (pos != end) {
98  if (num_names_added > 0)
99  s.PutCString(", ");
100 
101  if (prefix_with_broadcaster_name) {
103  s.PutChar('.');
104  }
105  s.PutCString(pos->second);
106  ++num_names_added;
107  }
108  }
109  }
110  }
111  return num_names_added > 0;
112 }
113 
115  const lldb::ListenerSP &listener_sp, uint32_t requested_events) {}
116 
117 uint32_t
118 Broadcaster::BroadcasterImpl::AddListener(const lldb::ListenerSP &listener_sp,
119  uint32_t event_mask) {
120  if (!listener_sp)
121  return 0;
122 
123  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
124 
125  // See if we already have this listener, and if so, update its mask
126 
127  bool handled = false;
128 
129  for (auto &pair : GetListeners()) {
130  if (pair.first == listener_sp) {
131  handled = true;
132  pair.second |= event_mask;
133  m_broadcaster.AddInitialEventsToListener(listener_sp, event_mask);
134  break;
135  }
136  }
137 
138  if (!handled) {
139  // Grant a new listener the available event bits
140  m_listeners.push_back(
141  std::make_pair(lldb::ListenerWP(listener_sp), event_mask));
142 
143  // Individual broadcasters decide whether they have outstanding data when a
144  // listener attaches, and insert it into the listener with this method.
145  m_broadcaster.AddInitialEventsToListener(listener_sp, event_mask);
146  }
147 
148  // Return the event bits that were granted to the listener
149  return event_mask;
150 }
151 
153  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
154 
155  if (!m_hijacking_listeners.empty() && event_type & m_hijacking_masks.back())
156  return true;
157 
158  for (auto &pair : GetListeners()) {
159  if (pair.second & event_type)
160  return true;
161  }
162  return false;
163 }
164 
166  lldb_private::Listener *listener, uint32_t event_mask) {
167  if (!listener)
168  return false;
169 
170  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
171  for (auto &pair : GetListeners()) {
172  if (pair.first.get() == listener) {
173  pair.second &= ~event_mask;
174  return true;
175  }
176  }
177  return false;
178 }
179 
181  const lldb::ListenerSP &listener_sp, uint32_t event_mask) {
182  return RemoveListener(listener_sp.get(), event_mask);
183 }
184 
185 void Broadcaster::BroadcasterImpl::BroadcastEvent(EventSP &event_sp) {
186  return PrivateBroadcastEvent(event_sp, false);
187 }
188 
190  return PrivateBroadcastEvent(event_sp, true);
191 }
192 
194  bool unique) {
195  // Can't add a nullptr event...
196  if (!event_sp)
197  return;
198 
199  // Update the broadcaster on this event
200  event_sp->SetBroadcaster(&m_broadcaster);
201 
202  const uint32_t event_type = event_sp->GetType();
203 
204  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
205 
206  ListenerSP hijacking_listener_sp;
207 
208  if (!m_hijacking_listeners.empty()) {
209  assert(!m_hijacking_masks.empty());
210  hijacking_listener_sp = m_hijacking_listeners.back();
211  if ((event_type & m_hijacking_masks.back()) == 0)
212  hijacking_listener_sp.reset();
213  }
214 
216  if (log) {
217  StreamString event_description;
218  event_sp->Dump(&event_description);
219  log->Printf("%p Broadcaster(\"%s\")::BroadcastEvent (event_sp = {%s}, "
220  "unique =%i) hijack = %p",
221  static_cast<void *>(this), GetBroadcasterName(),
222  event_description.GetData(), unique,
223  static_cast<void *>(hijacking_listener_sp.get()));
224  }
225 
226  if (hijacking_listener_sp) {
227  if (unique &&
228  hijacking_listener_sp->PeekAtNextEventForBroadcasterWithType(
229  &m_broadcaster, event_type))
230  return;
231  hijacking_listener_sp->AddEvent(event_sp);
232  } else {
233  for (auto &pair : GetListeners()) {
234  if (!(pair.second & event_type))
235  continue;
236  if (unique &&
237  pair.first->PeekAtNextEventForBroadcasterWithType(&m_broadcaster,
238  event_type))
239  continue;
240 
241  pair.first->AddEvent(event_sp);
242  }
243  }
244 }
245 
247  EventData *event_data) {
248  auto event_sp = std::make_shared<Event>(event_type, event_data);
249  PrivateBroadcastEvent(event_sp, false);
250 }
251 
253  uint32_t event_type, const lldb::EventDataSP &event_data_sp) {
254  auto event_sp = std::make_shared<Event>(event_type, event_data_sp);
255  PrivateBroadcastEvent(event_sp, false);
256 }
257 
259  uint32_t event_type, EventData *event_data) {
260  auto event_sp = std::make_shared<Event>(event_type, event_data);
261  PrivateBroadcastEvent(event_sp, true);
262 }
263 
265  const lldb::ListenerSP &listener_sp, uint32_t event_mask) {
266  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
267 
269  if (log)
270  log->Printf(
271  "%p Broadcaster(\"%s\")::HijackBroadcaster (listener(\"%s\")=%p)",
272  static_cast<void *>(this), GetBroadcasterName(),
273  listener_sp->m_name.c_str(), static_cast<void *>(listener_sp.get()));
274  m_hijacking_listeners.push_back(listener_sp);
275  m_hijacking_masks.push_back(event_mask);
276  return true;
277 }
278 
280  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
281 
282  if (!m_hijacking_listeners.empty())
283  return (event_mask & m_hijacking_masks.back()) != 0;
284  return false;
285 }
286 
288  if (m_hijacking_listeners.size()) {
289  return m_hijacking_listeners.back()->GetName();
290  } else {
291  return nullptr;
292  }
293 }
294 
296  std::lock_guard<std::recursive_mutex> guard(m_listeners_mutex);
297 
298  if (!m_hijacking_listeners.empty()) {
300  if (log) {
301  ListenerSP listener_sp = m_hijacking_listeners.back();
302  log->Printf("%p Broadcaster(\"%s\")::RestoreBroadcaster (about to pop "
303  "listener(\"%s\")=%p)",
304  static_cast<void *>(this), GetBroadcasterName(),
305  listener_sp->m_name.c_str(),
306  static_cast<void *>(listener_sp.get()));
307  }
308  m_hijacking_listeners.pop_back();
309  }
310  if (!m_hijacking_masks.empty())
311  m_hijacking_masks.pop_back();
312 }
313 
315  static ConstString class_name("lldb.anonymous");
316  return class_name;
317 }
318 
320 
322  if (GetBroadcasterClass() == rhs.GetBroadcasterClass()) {
323  return GetEventBits() < rhs.GetEventBits();
324  } else {
325  return GetBroadcasterClass() < rhs.GetBroadcasterClass();
326  }
327 }
328 
330 operator=(const BroadcastEventSpec &rhs) = default;
331 
333 
334 lldb::BroadcasterManagerSP BroadcasterManager::MakeBroadcasterManager() {
335  return lldb::BroadcasterManagerSP(new BroadcasterManager());
336 }
337 
339  const lldb::ListenerSP &listener_sp, BroadcastEventSpec event_spec) {
340  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
341 
342  collection::iterator iter = m_event_map.begin(), end_iter = m_event_map.end();
343  uint32_t available_bits = event_spec.GetEventBits();
344 
345  while (iter != end_iter &&
346  (iter = find_if(iter, end_iter,
347  BroadcasterClassMatches(
348  event_spec.GetBroadcasterClass()))) != end_iter) {
349  available_bits &= ~((*iter).first.GetEventBits());
350  iter++;
351  }
352 
353  if (available_bits != 0) {
354  m_event_map.insert(event_listener_key(
355  BroadcastEventSpec(event_spec.GetBroadcasterClass(), available_bits),
356  listener_sp));
357  m_listeners.insert(listener_sp);
358  }
359 
360  return available_bits;
361 }
362 
364  const lldb::ListenerSP &listener_sp, BroadcastEventSpec event_spec) {
365  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
366  bool removed_some = false;
367 
368  if (m_listeners.erase(listener_sp) == 0)
369  return false;
370 
371  ListenerMatchesAndSharedBits predicate(event_spec, listener_sp);
372  std::vector<BroadcastEventSpec> to_be_readded;
373  uint32_t event_bits_to_remove = event_spec.GetEventBits();
374 
375  // Go through the map and delete the exact matches, and build a list of
376  // matches that weren't exact to re-add:
377  while (true) {
378  collection::iterator iter, end_iter = m_event_map.end();
379  iter = find_if(m_event_map.begin(), end_iter, predicate);
380  if (iter == end_iter) {
381  break;
382  } else {
383  uint32_t iter_event_bits = (*iter).first.GetEventBits();
384  removed_some = true;
385 
386  if (event_bits_to_remove != iter_event_bits) {
387  uint32_t new_event_bits = iter_event_bits & ~event_bits_to_remove;
388  to_be_readded.push_back(BroadcastEventSpec(
389  event_spec.GetBroadcasterClass(), new_event_bits));
390  }
391  m_event_map.erase(iter);
392  }
393  }
394 
395  // Okay now add back the bits that weren't completely removed:
396  for (size_t i = 0; i < to_be_readded.size(); i++) {
397  m_event_map.insert(event_listener_key(to_be_readded[i], listener_sp));
398  }
399 
400  return removed_some;
401 }
402 
404  BroadcastEventSpec event_spec) const {
405  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
406 
407  collection::const_iterator iter, end_iter = m_event_map.end();
408  iter = find_if(m_event_map.begin(), end_iter,
409  BroadcastEventSpecMatches(event_spec));
410  if (iter != end_iter)
411  return (*iter).second;
412  else
413  return nullptr;
414 }
415 
417  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
418  ListenerMatchesPointer predicate(listener);
419  listener_collection::iterator iter = m_listeners.begin(),
420  end_iter = m_listeners.end();
421 
422  std::find_if(iter, end_iter, predicate);
423  if (iter != end_iter)
424  m_listeners.erase(iter);
425 
426  while (true) {
427  collection::iterator iter, end_iter = m_event_map.end();
428  iter = find_if(m_event_map.begin(), end_iter, predicate);
429  if (iter == end_iter)
430  break;
431  else
432  m_event_map.erase(iter);
433  }
434 }
435 
436 void BroadcasterManager::RemoveListener(const lldb::ListenerSP &listener_sp) {
437  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
438  ListenerMatches predicate(listener_sp);
439 
440  if (m_listeners.erase(listener_sp) == 0)
441  return;
442 
443  while (true) {
444  collection::iterator iter, end_iter = m_event_map.end();
445  iter = find_if(m_event_map.begin(), end_iter, predicate);
446  if (iter == end_iter)
447  break;
448  else
449  m_event_map.erase(iter);
450  }
451 }
452 
454  Broadcaster &broadcaster) {
455  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
456 
457  collection::iterator iter = m_event_map.begin(), end_iter = m_event_map.end();
458 
459  while (iter != end_iter &&
460  (iter = find_if(iter, end_iter,
461  BroadcasterClassMatches(
462  broadcaster.GetBroadcasterClass()))) != end_iter) {
463  (*iter).second->StartListeningForEvents(&broadcaster,
464  (*iter).first.GetEventBits());
465  iter++;
466  }
467 }
468 
470  std::lock_guard<std::recursive_mutex> guard(m_manager_mutex);
471  listener_collection::iterator end_iter = m_listeners.end();
472 
473  for (listener_collection::iterator iter = m_listeners.begin();
474  iter != end_iter; iter++)
475  (*iter)->BroadcasterManagerWillDestruct(this->shared_from_this());
476  m_listeners.clear();
477  m_event_map.clear();
478 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
bool HijackBroadcaster(const lldb::ListenerSP &listener_sp, uint32_t event_mask=UINT32_MAX)
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool RemoveListener(lldb_private::Listener *listener, uint32_t event_mask=UINT32_MAX)
collection m_listeners
A list of Listener / event_mask pairs that are listening to this broadcaster.
Definition: Broadcaster.h:525
BroadcasterImpl(Broadcaster &broadcaster)
Definition: Broadcaster.cpp:37
void SignUpListenersForBroadcaster(Broadcaster &broadcaster)
uint32_t RegisterListenerForEvents(const lldb::ListenerSP &listener_sp, BroadcastEventSpec event_spec)
llvm::SmallVector< std::pair< lldb::ListenerSP, uint32_t & >, 4 > GetListeners()
Definition: Broadcaster.cpp:57
An event broadcasting class.
Definition: Broadcaster.h:250
const char * GetData() const
Definition: StreamString.h:43
std::vector< uint32_t > m_hijacking_masks
Definition: Broadcaster.h:532
BroadcastEventSpec(ConstString broadcaster_class, uint32_t event_bits)
Definition: Broadcaster.h:48
event_names_map m_event_names
Optionally define event names for readability and logging for each event bit.
Definition: Broadcaster.h:523
virtual ConstString & GetBroadcasterClass() const
std::vector< lldb::ListenerSP > m_hijacking_listeners
Definition: Broadcaster.h:529
static lldb::BroadcasterManagerSP MakeBroadcasterManager()
#define LIBLLDB_LOG_EVENTS
Definition: Logging.h:18
ConstString GetBroadcasterClass() const
Definition: Broadcaster.h:55
static uint32_t bit(const uint32_t val, const uint32_t msbit)
Definition: ARMUtils.h:269
void BroadcastEvent(lldb::EventSP &event_sp)
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
void BroadcastEventIfUnique(lldb::EventSP &event_sp)
bool EventTypeHasListeners(uint32_t event_type)
Broadcaster & m_broadcaster
The broadcaster that this implements.
Definition: Broadcaster.h:522
virtual ~Broadcaster()
Destructor.
Definition: Broadcaster.cpp:41
uint32_t AddListener(const lldb::ListenerSP &listener_sp, uint32_t event_mask)
ConstString GetBroadcasterName()
Get the NULL terminated C string name of this Broadcaster object.
Definition: Broadcaster.h:338
size_t PutChar(char ch)
Definition: Stream.cpp:103
std::recursive_mutex m_listeners_mutex
A mutex that protects m_listeners.
Definition: Broadcaster.h:528
A uniqued constant string class.
Definition: ConstString.h:38
SharingPtr< T > make_shared()
Definition: SharingPtr.h:307
BroadcastEventSpec & operator=(const BroadcastEventSpec &rhs)
Log * GetLogIfAnyCategoriesSet(uint32_t mask)
Definition: Logging.cpp:61
Definition: SBAddress.h:15
bool operator<(const BroadcastEventSpec &rhs) const
bool IsHijackedForEvent(uint32_t event_mask)
void RemoveListener(const lldb::ListenerSP &listener_sp)
bool UnregisterListenerForEvents(const lldb::ListenerSP &listener_sp, BroadcastEventSpec event_spec)
lldb::ListenerSP GetListenerForEventSpec(BroadcastEventSpec event_spec) const
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
virtual void AddInitialEventsToListener(const lldb::ListenerSP &listener_sp, uint32_t requested_events)
bool GetEventNames(Stream &s, const uint32_t event_mask, bool prefix_with_broadcaster_name) const
Definition: Broadcaster.cpp:88
void PrivateBroadcastEvent(lldb::EventSP &event_sp, bool unique)