LLDB  mainline
Listener.cpp
Go to the documentation of this file.
1 //===-- Listener.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 
13 #include "lldb/Utility/Event.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/Logging.h"
16 
17 #include "llvm/ADT/Optional.h"
18 
19 #include <algorithm>
20 #include <memory>
21 #include <utility>
22 
23 using namespace lldb;
24 using namespace lldb_private;
25 
26 namespace {
27 class BroadcasterManagerWPMatcher {
28 public:
29  BroadcasterManagerWPMatcher(BroadcasterManagerSP manager_sp)
30  : m_manager_sp(manager_sp) {}
31  bool operator()(const BroadcasterManagerWP input_wp) const {
32  BroadcasterManagerSP input_sp = input_wp.lock();
33  return (input_sp && input_sp == m_manager_sp);
34  }
35 
36  BroadcasterManagerSP m_manager_sp;
37 };
38 } // anonymous namespace
39 
40 Listener::Listener(const char *name)
41  : m_name(name), m_broadcasters(), m_broadcasters_mutex(), m_events(),
42  m_events_mutex() {
44  if (log != nullptr)
45  log->Printf("%p Listener::Listener('%s')", static_cast<void *>(this),
46  m_name.c_str());
47 }
48 
51 
52  Clear();
53 
54  if (log)
55  log->Printf("%p Listener::%s('%s')", static_cast<void *>(this),
56  __FUNCTION__, m_name.c_str());
57 }
58 
61  std::lock_guard<std::recursive_mutex> broadcasters_guard(
62  m_broadcasters_mutex);
63  broadcaster_collection::iterator pos, end = m_broadcasters.end();
64  for (pos = m_broadcasters.begin(); pos != end; ++pos) {
65  Broadcaster::BroadcasterImplSP broadcaster_sp(pos->first.lock());
66  if (broadcaster_sp)
67  broadcaster_sp->RemoveListener(this, pos->second.event_mask);
68  }
69  m_broadcasters.clear();
70 
71  std::lock_guard<std::mutex> events_guard(m_events_mutex);
72  m_events.clear();
73  size_t num_managers = m_broadcaster_managers.size();
74 
75  for (size_t i = 0; i < num_managers; i++) {
76  BroadcasterManagerSP manager_sp(m_broadcaster_managers[i].lock());
77  if (manager_sp)
78  manager_sp->RemoveListener(this);
79  }
80 
81  if (log)
82  log->Printf("%p Listener::%s('%s')", static_cast<void *>(this),
83  __FUNCTION__, m_name.c_str());
84 }
85 
87  uint32_t event_mask) {
88  if (broadcaster) {
89  // Scope for "locker"
90  // Tell the broadcaster to add this object as a listener
91  {
92  std::lock_guard<std::recursive_mutex> broadcasters_guard(
93  m_broadcasters_mutex);
94  Broadcaster::BroadcasterImplWP impl_wp(broadcaster->GetBroadcasterImpl());
95  m_broadcasters.insert(
96  std::make_pair(impl_wp, BroadcasterInfo(event_mask)));
97  }
98 
99  uint32_t acquired_mask =
100  broadcaster->AddListener(this->shared_from_this(), event_mask);
101 
103  if (log != nullptr)
104  log->Printf("%p Listener::StartListeningForEvents (broadcaster = %p, "
105  "mask = 0x%8.8x) acquired_mask = 0x%8.8x for %s",
106  static_cast<void *>(this), static_cast<void *>(broadcaster),
107  event_mask, acquired_mask, m_name.c_str());
108 
109  return acquired_mask;
110  }
111  return 0;
112 }
113 
115  uint32_t event_mask,
116  HandleBroadcastCallback callback,
117  void *callback_user_data) {
118  if (broadcaster) {
119  // Scope for "locker"
120  // Tell the broadcaster to add this object as a listener
121  {
122  std::lock_guard<std::recursive_mutex> broadcasters_guard(
123  m_broadcasters_mutex);
124  Broadcaster::BroadcasterImplWP impl_wp(broadcaster->GetBroadcasterImpl());
125  m_broadcasters.insert(std::make_pair(
126  impl_wp, BroadcasterInfo(event_mask, callback, callback_user_data)));
127  }
128 
129  uint32_t acquired_mask =
130  broadcaster->AddListener(this->shared_from_this(), event_mask);
131 
133  if (log != nullptr) {
134  void **pointer = reinterpret_cast<void **>(&callback);
135  log->Printf("%p Listener::StartListeningForEvents (broadcaster = %p, "
136  "mask = 0x%8.8x, callback = %p, user_data = %p) "
137  "acquired_mask = 0x%8.8x for %s",
138  static_cast<void *>(this), static_cast<void *>(broadcaster),
139  event_mask, *pointer, static_cast<void *>(callback_user_data),
140  acquired_mask, m_name.c_str());
141  }
142 
143  return acquired_mask;
144  }
145  return 0;
146 }
147 
149  uint32_t event_mask) {
150  if (broadcaster) {
151  // Scope for "locker"
152  {
153  std::lock_guard<std::recursive_mutex> broadcasters_guard(
154  m_broadcasters_mutex);
155  m_broadcasters.erase(broadcaster->GetBroadcasterImpl());
156  }
157  // Remove the broadcaster from our set of broadcasters
158  return broadcaster->RemoveListener(this->shared_from_this(), event_mask);
159  }
160 
161  return false;
162 }
163 
164 // Called when a Broadcaster is in its destructor. We need to remove all
165 // knowledge of this broadcaster and any events that it may have queued up
166 void Listener::BroadcasterWillDestruct(Broadcaster *broadcaster) {
167  // Scope for "broadcasters_locker"
168  {
169  std::lock_guard<std::recursive_mutex> broadcasters_guard(
170  m_broadcasters_mutex);
171  m_broadcasters.erase(broadcaster->GetBroadcasterImpl());
172  }
173 
174  // Scope for "event_locker"
175  {
176  std::lock_guard<std::mutex> events_guard(m_events_mutex);
177  // Remove all events for this broadcaster object.
178  event_collection::iterator pos = m_events.begin();
179  while (pos != m_events.end()) {
180  if ((*pos)->GetBroadcaster() == broadcaster)
181  pos = m_events.erase(pos);
182  else
183  ++pos;
184  }
185  }
186 }
187 
188 void Listener::BroadcasterManagerWillDestruct(BroadcasterManagerSP manager_sp) {
189  // Just need to remove this broadcast manager from the list of managers:
190  broadcaster_manager_collection::iterator iter,
191  end_iter = m_broadcaster_managers.end();
192  BroadcasterManagerWP manager_wp;
193 
194  BroadcasterManagerWPMatcher matcher(manager_sp);
195  iter = std::find_if<broadcaster_manager_collection::iterator,
196  BroadcasterManagerWPMatcher>(
197  m_broadcaster_managers.begin(), end_iter, matcher);
198  if (iter != end_iter)
199  m_broadcaster_managers.erase(iter);
200 }
201 
202 void Listener::AddEvent(EventSP &event_sp) {
204  if (log != nullptr)
205  log->Printf("%p Listener('%s')::AddEvent (event_sp = {%p})",
206  static_cast<void *>(this), m_name.c_str(),
207  static_cast<void *>(event_sp.get()));
208 
209  std::lock_guard<std::mutex> guard(m_events_mutex);
210  m_events.push_back(event_sp);
211  m_events_condition.notify_all();
212 }
213 
215 public:
217  : m_broadcaster(broadcaster) {}
218 
219  bool operator()(const EventSP &event_sp) const {
220  return event_sp->BroadcasterIs(m_broadcaster);
221  }
222 
223 private:
224  Broadcaster *m_broadcaster;
225 };
226 
228 public:
229  EventMatcher(Broadcaster *broadcaster, const ConstString *broadcaster_names,
230  uint32_t num_broadcaster_names, uint32_t event_type_mask)
231  : m_broadcaster(broadcaster), m_broadcaster_names(broadcaster_names),
232  m_num_broadcaster_names(num_broadcaster_names),
233  m_event_type_mask(event_type_mask) {}
234 
235  bool operator()(const EventSP &event_sp) const {
236  if (m_broadcaster && !event_sp->BroadcasterIs(m_broadcaster))
237  return false;
238 
239  if (m_broadcaster_names) {
240  bool found_source = false;
241  ConstString event_broadcaster_name =
242  event_sp->GetBroadcaster()->GetBroadcasterName();
243  for (uint32_t i = 0; i < m_num_broadcaster_names; ++i) {
244  if (m_broadcaster_names[i] == event_broadcaster_name) {
245  found_source = true;
246  break;
247  }
248  }
249  if (!found_source)
250  return false;
251  }
252 
253  return m_event_type_mask == 0 || m_event_type_mask & event_sp->GetType();
254  }
255 
256 private:
257  Broadcaster *m_broadcaster;
258  const ConstString *m_broadcaster_names;
259  const uint32_t m_num_broadcaster_names;
260  const uint32_t m_event_type_mask;
261 };
262 
263 bool Listener::FindNextEventInternal(
264  std::unique_lock<std::mutex> &lock,
265  Broadcaster *broadcaster, // nullptr for any broadcaster
266  const ConstString *broadcaster_names, // nullptr for any event
267  uint32_t num_broadcaster_names, uint32_t event_type_mask, EventSP &event_sp,
268  bool remove) {
269  // NOTE: callers of this function must lock m_events_mutex using a
270  // Mutex::Locker
271  // and pass the locker as the first argument. m_events_mutex is no longer
272  // recursive.
274 
275  if (m_events.empty())
276  return false;
277 
278  Listener::event_collection::iterator pos = m_events.end();
279 
280  if (broadcaster == nullptr && broadcaster_names == nullptr &&
281  event_type_mask == 0) {
282  pos = m_events.begin();
283  } else {
284  pos = std::find_if(m_events.begin(), m_events.end(),
285  EventMatcher(broadcaster, broadcaster_names,
286  num_broadcaster_names, event_type_mask));
287  }
288 
289  if (pos != m_events.end()) {
290  event_sp = *pos;
291 
292  if (log != nullptr)
293  log->Printf("%p '%s' Listener::FindNextEventInternal(broadcaster=%p, "
294  "broadcaster_names=%p[%u], event_type_mask=0x%8.8x, "
295  "remove=%i) event %p",
296  static_cast<void *>(this), GetName(),
297  static_cast<void *>(broadcaster),
298  static_cast<const void *>(broadcaster_names),
299  num_broadcaster_names, event_type_mask, remove,
300  static_cast<void *>(event_sp.get()));
301 
302  if (remove) {
303  m_events.erase(pos);
304  // Unlock the event queue here. We've removed this event and are about
305  // to return it so it should be okay to get the next event off the queue
306  // here - and it might be useful to do that in the "DoOnRemoval".
307  lock.unlock();
308  event_sp->DoOnRemoval();
309  }
310  return true;
311  }
312 
313  event_sp.reset();
314  return false;
315 }
316 
318  std::unique_lock<std::mutex> guard(m_events_mutex);
319  EventSP event_sp;
320  if (FindNextEventInternal(guard, nullptr, nullptr, 0, 0, event_sp, false))
321  return event_sp.get();
322  return nullptr;
323 }
324 
326  std::unique_lock<std::mutex> guard(m_events_mutex);
327  EventSP event_sp;
328  if (FindNextEventInternal(guard, broadcaster, nullptr, 0, 0, event_sp, false))
329  return event_sp.get();
330  return nullptr;
331 }
332 
333 Event *
335  uint32_t event_type_mask) {
336  std::unique_lock<std::mutex> guard(m_events_mutex);
337  EventSP event_sp;
338  if (FindNextEventInternal(guard, broadcaster, nullptr, 0, event_type_mask,
339  event_sp, false))
340  return event_sp.get();
341  return nullptr;
342 }
343 
344 bool Listener::GetEventInternal(
345  const Timeout<std::micro> &timeout,
346  Broadcaster *broadcaster, // nullptr for any broadcaster
347  const ConstString *broadcaster_names, // nullptr for any event
348  uint32_t num_broadcaster_names, uint32_t event_type_mask,
349  EventSP &event_sp) {
351  LLDB_LOG(log, "this = {0}, timeout = {1} for {2}", this, timeout, m_name);
352 
353  std::unique_lock<std::mutex> lock(m_events_mutex);
354 
355  while (true) {
356  if (FindNextEventInternal(lock, broadcaster, broadcaster_names,
357  num_broadcaster_names, event_type_mask, event_sp,
358  true)) {
359  return true;
360  } else {
361  std::cv_status result = std::cv_status::no_timeout;
362  if (!timeout)
363  m_events_condition.wait(lock);
364  else
365  result = m_events_condition.wait_for(lock, *timeout);
366 
367  if (result == std::cv_status::timeout) {
369  if (log)
370  log->Printf("%p Listener::GetEventInternal() timed out for %s",
371  static_cast<void *>(this), m_name.c_str());
372  return false;
373  } else if (result != std::cv_status::no_timeout) {
375  if (log)
376  log->Printf("%p Listener::GetEventInternal() unknown error for %s",
377  static_cast<void *>(this), m_name.c_str());
378  return false;
379  }
380  }
381  }
382 
383  return false;
384 }
385 
387  Broadcaster *broadcaster, uint32_t event_type_mask, EventSP &event_sp,
388  const Timeout<std::micro> &timeout) {
389  return GetEventInternal(timeout, broadcaster, nullptr, 0, event_type_mask,
390  event_sp);
391 }
392 
394  EventSP &event_sp,
395  const Timeout<std::micro> &timeout) {
396  return GetEventInternal(timeout, broadcaster, nullptr, 0, 0, event_sp);
397 }
398 
399 bool Listener::GetEvent(EventSP &event_sp, const Timeout<std::micro> &timeout) {
400  return GetEventInternal(timeout, nullptr, nullptr, 0, 0, event_sp);
401 }
402 
403 size_t Listener::HandleBroadcastEvent(EventSP &event_sp) {
404  size_t num_handled = 0;
405  std::lock_guard<std::recursive_mutex> guard(m_broadcasters_mutex);
406  Broadcaster *broadcaster = event_sp->GetBroadcaster();
407  if (!broadcaster)
408  return 0;
409  broadcaster_collection::iterator pos;
410  broadcaster_collection::iterator end = m_broadcasters.end();
411  Broadcaster::BroadcasterImplSP broadcaster_impl_sp(
412  broadcaster->GetBroadcasterImpl());
413  for (pos = m_broadcasters.find(broadcaster_impl_sp);
414  pos != end && pos->first.lock() == broadcaster_impl_sp; ++pos) {
415  BroadcasterInfo info = pos->second;
416  if (event_sp->GetType() & info.event_mask) {
417  if (info.callback != nullptr) {
418  info.callback(event_sp, info.callback_user_data);
419  ++num_handled;
420  }
421  }
422  }
423  return num_handled;
424 }
425 
426 uint32_t
427 Listener::StartListeningForEventSpec(BroadcasterManagerSP manager_sp,
428  const BroadcastEventSpec &event_spec) {
429  if (!manager_sp)
430  return 0;
431 
432  // The BroadcasterManager mutex must be locked before m_broadcasters_mutex to
433  // avoid violating the lock hierarchy (manager before broadcasters).
434  std::lock_guard<std::recursive_mutex> manager_guard(
435  manager_sp->m_manager_mutex);
436  std::lock_guard<std::recursive_mutex> guard(m_broadcasters_mutex);
437 
438  uint32_t bits_acquired = manager_sp->RegisterListenerForEvents(
439  this->shared_from_this(), event_spec);
440  if (bits_acquired) {
441  broadcaster_manager_collection::iterator iter,
442  end_iter = m_broadcaster_managers.end();
443  BroadcasterManagerWP manager_wp(manager_sp);
444  BroadcasterManagerWPMatcher matcher(manager_sp);
445  iter = std::find_if<broadcaster_manager_collection::iterator,
446  BroadcasterManagerWPMatcher>(
447  m_broadcaster_managers.begin(), end_iter, matcher);
448  if (iter == end_iter)
449  m_broadcaster_managers.push_back(manager_wp);
450  }
451 
452  return bits_acquired;
453 }
454 
455 bool Listener::StopListeningForEventSpec(BroadcasterManagerSP manager_sp,
456  const BroadcastEventSpec &event_spec) {
457  if (!manager_sp)
458  return false;
459 
460  std::lock_guard<std::recursive_mutex> guard(m_broadcasters_mutex);
461  return manager_sp->UnregisterListenerForEvents(this->shared_from_this(),
462  event_spec);
463 }
464 
465 ListenerSP Listener::MakeListener(const char *name) {
466  return ListenerSP(new Listener(name));
467 }
EventBroadcasterMatches(Broadcaster *broadcaster)
Definition: Listener.cpp:216
bool StopListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:455
bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:393
#define LIBLLDB_LOG_OBJECT
Definition: Logging.h:25
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool operator()(const EventSP &event_sp) const
Definition: Listener.cpp:235
Event * PeekAtNextEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask)
Definition: Listener.cpp:334
An event broadcasting class.
Definition: Broadcaster.h:250
const char * GetName()
Definition: Listener.h:61
#define LLDB_LOG(log,...)
Definition: Log.h:209
void AddEvent(lldb::EventSP &event)
Definition: Listener.cpp:202
bool(* HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton)
Definition: Listener.h:40
#define LIBLLDB_LOG_EVENTS
Definition: Logging.h:18
uint32_t AddListener(const lldb::ListenerSP &listener_sp, uint32_t event_mask)
Listen for any events specified by event_mask.
Definition: Broadcaster.h:329
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:465
Log * GetLogIfAllCategoriesSet(uint32_t mask)
Definition: Logging.cpp:57
BroadcasterImplSP GetBroadcasterImpl()
Definition: Broadcaster.h:544
uint32_t StartListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:86
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:541
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:542
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:389
Event * PeekAtNextEvent()
Definition: Listener.cpp:317
A uniqued constant string class.
Definition: ConstString.h:38
uint32_t StartListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:427
Definition: SBAddress.h:15
size_t HandleBroadcastEvent(lldb::EventSP &event_sp)
Definition: Listener.cpp:403
bool GetEvent(lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:399
bool operator()(const EventSP &event_sp) const
Definition: Listener.cpp:219
EventMatcher(Broadcaster *broadcaster, const ConstString *broadcaster_names, uint32_t num_broadcaster_names, uint32_t event_type_mask)
Definition: Listener.cpp:229
Event * PeekAtNextEventForBroadcaster(Broadcaster *broadcaster)
Definition: Listener.cpp:325
void Printf(const char *format,...) __attribute__((format(printf
Definition: Log.cpp:113
bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:148
Listener(const char *name)
Definition: Listener.cpp:40
bool GetEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:386