1 //===-- Broadcaster.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/Broadcaster.h"
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"
16 #include "lldb/Utility/StreamString.h"
26 using namespace lldb_private
;
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
) {
31 Log
*log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT
));
32 LLDB_LOG(log
, "{0} Broadcaster::Broadcaster(\"{1}\")",
33 static_cast<void *>(this), GetBroadcasterName());
36 Broadcaster::BroadcasterImpl::BroadcasterImpl(Broadcaster
&broadcaster
)
37 : m_broadcaster(broadcaster
), m_listeners(), m_listeners_mutex(),
38 m_hijacking_listeners(), m_hijacking_masks() {}
40 Broadcaster::~Broadcaster() {
41 Log
*log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT
));
42 LLDB_LOG(log
, "{0} Broadcaster::~Broadcaster(\"{1}\")",
43 static_cast<void *>(this), GetBroadcasterName());
48 void Broadcaster::CheckInWithManager() {
50 m_manager_sp
->SignUpListenersForBroadcaster(*this);
54 llvm::SmallVector
<std::pair
<ListenerSP
, uint32_t &>, 4>
55 Broadcaster::BroadcasterImpl::GetListeners() {
56 llvm::SmallVector
<std::pair
<ListenerSP
, uint32_t &>, 4> listeners
;
57 listeners
.reserve(m_listeners
.size());
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
);
65 it
= m_listeners
.erase(it
);
71 void Broadcaster::BroadcasterImpl::Clear() {
72 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
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
);
82 Broadcaster
*Broadcaster::BroadcasterImpl::GetBroadcaster() {
83 return &m_broadcaster
;
86 bool Broadcaster::BroadcasterImpl::GetEventNames(
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) {
94 event_names_map::const_iterator pos
= m_event_names
.find(bit
);
96 if (num_names_added
> 0)
99 if (prefix_with_broadcaster_name
) {
100 s
.PutCString(GetBroadcasterName());
103 s
.PutCString(pos
->second
);
109 return num_names_added
> 0;
112 void Broadcaster::AddInitialEventsToListener(
113 const lldb::ListenerSP
&listener_sp
, uint32_t requested_events
) {}
116 Broadcaster::BroadcasterImpl::AddListener(const lldb::ListenerSP
&listener_sp
,
117 uint32_t event_mask
) {
121 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
123 // See if we already have this listener, and if so, update its mask
125 bool handled
= false;
127 for (auto &pair
: GetListeners()) {
128 if (pair
.first
== listener_sp
) {
130 pair
.second
|= event_mask
;
131 m_broadcaster
.AddInitialEventsToListener(listener_sp
, event_mask
);
137 // Grant a new listener the available event bits
138 m_listeners
.push_back(
139 std::make_pair(lldb::ListenerWP(listener_sp
), event_mask
));
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
);
146 // Return the event bits that were granted to the listener
150 bool Broadcaster::BroadcasterImpl::EventTypeHasListeners(uint32_t event_type
) {
151 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
153 if (!m_hijacking_listeners
.empty() && event_type
& m_hijacking_masks
.back())
156 for (auto &pair
: GetListeners()) {
157 if (pair
.second
& event_type
)
163 bool Broadcaster::BroadcasterImpl::RemoveListener(
164 lldb_private::Listener
*listener
, uint32_t event_mask
) {
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
;
178 bool Broadcaster::BroadcasterImpl::RemoveListener(
179 const lldb::ListenerSP
&listener_sp
, uint32_t event_mask
) {
180 return RemoveListener(listener_sp
.get(), event_mask
);
183 void Broadcaster::BroadcasterImpl::BroadcastEvent(EventSP
&event_sp
) {
184 return PrivateBroadcastEvent(event_sp
, false);
187 void Broadcaster::BroadcasterImpl::BroadcastEventIfUnique(EventSP
&event_sp
) {
188 return PrivateBroadcastEvent(event_sp
, true);
191 void Broadcaster::BroadcasterImpl::PrivateBroadcastEvent(EventSP
&event_sp
,
193 // Can't add a nullptr event...
197 // Update the broadcaster on this event
198 event_sp
->SetBroadcaster(&m_broadcaster
);
200 const uint32_t event_type
= event_sp
->GetType();
202 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
204 ListenerSP hijacking_listener_sp
;
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();
213 if (Log
*log
= lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS
)) {
214 StreamString event_description
;
215 event_sp
->Dump(&event_description
);
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()));
224 if (hijacking_listener_sp
) {
225 if (unique
&& hijacking_listener_sp
->PeekAtNextEventForBroadcasterWithType(
226 &m_broadcaster
, event_type
))
228 hijacking_listener_sp
->AddEvent(event_sp
);
230 for (auto &pair
: GetListeners()) {
231 if (!(pair
.second
& event_type
))
233 if (unique
&& pair
.first
->PeekAtNextEventForBroadcasterWithType(
234 &m_broadcaster
, event_type
))
237 pair
.first
->AddEvent(event_sp
);
242 void Broadcaster::BroadcasterImpl::BroadcastEvent(uint32_t event_type
,
243 EventData
*event_data
) {
244 auto event_sp
= std::make_shared
<Event
>(event_type
, event_data
);
245 PrivateBroadcastEvent(event_sp
, false);
248 void Broadcaster::BroadcasterImpl::BroadcastEvent(
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);
254 void Broadcaster::BroadcasterImpl::BroadcastEventIfUnique(
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);
260 bool Broadcaster::BroadcasterImpl::HijackBroadcaster(
261 const lldb::ListenerSP
&listener_sp
, uint32_t event_mask
) {
262 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
264 Log
*log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS
));
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
);
275 bool Broadcaster::BroadcasterImpl::IsHijackedForEvent(uint32_t event_mask
) {
276 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
278 if (!m_hijacking_listeners
.empty())
279 return (event_mask
& m_hijacking_masks
.back()) != 0;
283 const char *Broadcaster::BroadcasterImpl::GetHijackingListenerName() {
284 if (m_hijacking_listeners
.size()) {
285 return m_hijacking_listeners
.back()->GetName();
290 void Broadcaster::BroadcasterImpl::RestoreBroadcaster() {
291 std::lock_guard
<std::recursive_mutex
> guard(m_listeners_mutex
);
293 if (!m_hijacking_listeners
.empty()) {
294 ListenerSP listener_sp
= m_hijacking_listeners
.back();
295 Log
*log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_EVENTS
));
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();
304 if (!m_hijacking_masks
.empty())
305 m_hijacking_masks
.pop_back();
308 ConstString
&Broadcaster::GetBroadcasterClass() const {
309 static ConstString
class_name("lldb.anonymous");
313 bool BroadcastEventSpec::operator<(const BroadcastEventSpec
&rhs
) const {
314 if (GetBroadcasterClass() == rhs
.GetBroadcasterClass()) {
315 return GetEventBits() < rhs
.GetEventBits();
317 return GetBroadcasterClass() < rhs
.GetBroadcasterClass();
320 BroadcasterManager::BroadcasterManager() : m_manager_mutex() {}
322 lldb::BroadcasterManagerSP
BroadcasterManager::MakeBroadcasterManager() {
323 return lldb::BroadcasterManagerSP(new BroadcasterManager());
326 uint32_t BroadcasterManager::RegisterListenerForEvents(
327 const lldb::ListenerSP
&listener_sp
, const BroadcastEventSpec
&event_spec
) {
328 std::lock_guard
<std::recursive_mutex
> guard(m_manager_mutex
);
330 collection::iterator iter
= m_event_map
.begin(), end_iter
= m_event_map
.end();
331 uint32_t available_bits
= event_spec
.GetEventBits();
333 while (iter
!= end_iter
&&
334 (iter
= find_if(iter
, end_iter
,
335 BroadcasterClassMatches(
336 event_spec
.GetBroadcasterClass()))) != end_iter
) {
337 available_bits
&= ~((*iter
).first
.GetEventBits());
341 if (available_bits
!= 0) {
342 m_event_map
.insert(event_listener_key(
343 BroadcastEventSpec(event_spec
.GetBroadcasterClass(), available_bits
),
345 m_listeners
.insert(listener_sp
);
348 return available_bits
;
351 bool BroadcasterManager::UnregisterListenerForEvents(
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;
356 if (m_listeners
.erase(listener_sp
) == 0)
359 ListenerMatchesAndSharedBits
predicate(event_spec
, listener_sp
);
360 std::vector
<BroadcastEventSpec
> to_be_readded
;
361 uint32_t event_bits_to_remove
= event_spec
.GetEventBits();
363 // Go through the map and delete the exact matches, and build a list of
364 // matches that weren't exact to re-add:
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
) {
371 uint32_t iter_event_bits
= (*iter
).first
.GetEventBits();
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(),
379 m_event_map
.erase(iter
);
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
));
390 ListenerSP
BroadcasterManager::GetListenerForEventSpec(
391 const BroadcastEventSpec
&event_spec
) const {
392 std::lock_guard
<std::recursive_mutex
> guard(m_manager_mutex
);
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
;
403 void BroadcasterManager::RemoveListener(Listener
*listener
) {
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();
409 iter
= std::find_if(iter
, end_iter
, predicate
);
410 if (iter
!= end_iter
)
411 m_listeners
.erase(iter
);
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
)
419 m_event_map
.erase(iter
);
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
);
427 if (m_listeners
.erase(listener_sp
) == 0)
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
)
436 m_event_map
.erase(iter
);
440 void BroadcasterManager::SignUpListenersForBroadcaster(
441 Broadcaster
&broadcaster
) {
442 std::lock_guard
<std::recursive_mutex
> guard(m_manager_mutex
);
444 collection::iterator iter
= m_event_map
.begin(), end_iter
= m_event_map
.end();
446 while (iter
!= end_iter
&&
447 (iter
= find_if(iter
, end_iter
,
448 BroadcasterClassMatches(
449 broadcaster
.GetBroadcasterClass()))) != end_iter
) {
450 (*iter
).second
->StartListeningForEvents(&broadcaster
,
451 (*iter
).first
.GetEventBits());
456 void BroadcasterManager::Clear() {
457 std::lock_guard
<std::recursive_mutex
> guard(m_manager_mutex
);
458 listener_collection::iterator end_iter
= m_listeners
.end();
460 for (listener_collection::iterator iter
= m_listeners
.begin();
461 iter
!= end_iter
; iter
++)
462 (*iter
)->BroadcasterManagerWillDestruct(this->shared_from_this());