LLDB mainline
Listener.h
Go to the documentation of this file.
1//===-- Listener.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_LISTENER_H
10#define LLDB_UTILITY_LISTENER_H
11
14#include "lldb/lldb-defines.h"
15#include "lldb/lldb-forward.h"
16
17#include <condition_variable>
18#include <list>
19#include <map>
20#include <memory>
21#include <mutex>
22#include <ratio>
23#include <string>
24#include <vector>
25
26#include <cstddef>
27#include <cstdint>
28
29namespace lldb_private {
30class Event;
31}
32
33namespace lldb_private {
34
35class Listener : public std::enable_shared_from_this<Listener> {
36public:
37 typedef bool (*HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton);
38
39 friend class Broadcaster;
40 friend class BroadcasterManager;
41
42 // Constructors and Destructors
43 //
44 // Listeners have to be constructed into shared pointers - at least if you
45 // want them to listen to Broadcasters,
46protected:
47 Listener(const char *name);
48
49public:
50 static lldb::ListenerSP MakeListener(const char *name);
51
52 ~Listener();
53
54 void AddEvent(lldb::EventSP &event);
55
56 void Clear();
57
58 const char *GetName() { return m_name.c_str(); }
59
60 uint32_t
62 const BroadcastEventSpec &event_spec);
63
65 const BroadcastEventSpec &event_spec);
66
67 uint32_t StartListeningForEvents(Broadcaster *broadcaster,
68 uint32_t event_mask);
69
70 uint32_t StartListeningForEvents(Broadcaster *broadcaster,
71 uint32_t event_mask,
73 void *callback_user_data);
74
75 bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask);
76
78
80
82 uint32_t event_type_mask);
83
84 // Returns true if an event was received, false if we timed out.
85 bool GetEvent(lldb::EventSP &event_sp, const Timeout<std::micro> &timeout);
86
87 bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp,
88 const Timeout<std::micro> &timeout);
89
91 uint32_t event_type_mask,
92 lldb::EventSP &event_sp,
93 const Timeout<std::micro> &timeout);
94
95 size_t HandleBroadcastEvent(lldb::EventSP &event_sp);
96
97private:
98 // Classes that inherit from Listener can see and modify these
100 BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = nullptr,
101 void *ud = nullptr)
102 : event_mask(mask), callback(cb), callback_user_data(ud) {}
103
104 uint32_t event_mask;
107 };
108
109 typedef std::multimap<Broadcaster::BroadcasterImplWP, BroadcasterInfo,
110 std::owner_less<Broadcaster::BroadcasterImplWP>>
112 typedef std::list<lldb::EventSP> event_collection;
113 typedef std::vector<lldb::BroadcasterManagerWP>
115
116 bool
117 FindNextEventInternal(std::unique_lock<std::mutex> &lock,
118 Broadcaster *broadcaster, // nullptr for any broadcaster
119 uint32_t event_type_mask, lldb::EventSP &event_sp,
120 bool remove);
121
122 bool GetEventInternal(const Timeout<std::micro> &timeout,
123 Broadcaster *broadcaster, // nullptr for any broadcaster
124 uint32_t event_type_mask, lldb::EventSP &event_sp);
125
126 std::string m_name;
128 std::mutex m_broadcasters_mutex; // Protects m_broadcasters
130 std::mutex m_events_mutex; // Protects m_broadcasters and m_events
131 std::condition_variable m_events_condition;
133
135
137
138 // broadcaster_collection::iterator
139 // FindBroadcasterWithMask (Broadcaster *broadcaster,
140 // uint32_t event_mask,
141 // bool exact);
142
143 // For Listener only
144 Listener(const Listener &) = delete;
145 const Listener &operator=(const Listener &) = delete;
146};
147
148} // namespace lldb_private
149
150#endif // LLDB_UTILITY_LISTENER_H
lldb::BroadcastEventSpec
Definition: Broadcaster.h:40
An event broadcasting class.
Definition: Broadcaster.h:146
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:465
size_t HandleBroadcastEvent(lldb::EventSP &event_sp)
Definition: Listener.cpp:312
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:376
event_collection m_events
Definition: Listener.h:129
Event * PeekAtNextEventForBroadcaster(Broadcaster *broadcaster)
Definition: Listener.cpp:240
Event * PeekAtNextEvent()
Definition: Listener.cpp:232
uint32_t StartListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:58
std::condition_variable m_events_condition
Definition: Listener.h:131
uint32_t StartListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:336
bool StopListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:363
bool(* HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton)
Definition: Listener.h:37
std::string m_name
Definition: Listener.h:126
std::mutex m_broadcasters_mutex
Definition: Listener.h:128
broadcaster_manager_collection m_broadcaster_managers
Definition: Listener.h:132
std::multimap< Broadcaster::BroadcasterImplWP, BroadcasterInfo, std::owner_less< Broadcaster::BroadcasterImplWP > > broadcaster_collection
Definition: Listener.h:111
bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:302
bool FindNextEventInternal(std::unique_lock< std::mutex > &lock, Broadcaster *broadcaster, uint32_t event_type_mask, lldb::EventSP &event_sp, bool remove)
Definition: Listener.cpp:179
std::vector< lldb::BroadcasterManagerWP > broadcaster_manager_collection
Definition: Listener.h:114
const char * GetName()
Definition: Listener.h:58
Event * PeekAtNextEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask)
Definition: Listener.cpp:249
bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:120
bool GetEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:296
std::list< lldb::EventSP > event_collection
Definition: Listener.h:112
const Listener & operator=(const Listener &)=delete
Listener(const Listener &)=delete
void BroadcasterWillDestruct(Broadcaster *)
Definition: Listener.cpp:137
void BroadcasterManagerWillDestruct(lldb::BroadcasterManagerSP manager_sp)
Definition: Listener.cpp:158
broadcaster_collection m_broadcasters
Definition: Listener.h:127
bool GetEventInternal(const Timeout< std::micro > &timeout, Broadcaster *broadcaster, uint32_t event_type_mask, lldb::EventSP &event_sp)
Definition: Listener.cpp:259
std::mutex m_events_mutex
Definition: Listener.h:130
void AddEvent(lldb::EventSP &event)
Definition: Listener.cpp:167
bool GetEvent(lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:308
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::BroadcasterManager > BroadcasterManagerSP
Definition: lldb-forward.h:328
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:343
std::shared_ptr< lldb_private::Listener > ListenerSP
Definition: lldb-forward.h:366
HandleBroadcastCallback callback
Definition: Listener.h:105
BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb=nullptr, void *ud=nullptr)
Definition: Listener.h:100