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