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 
13 #include "lldb/Utility/Timeout.h"
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 <stddef.h>
27 #include <stdint.h>
28 
29 namespace lldb_private {
30 class ConstString;
31 }
32 namespace lldb_private {
33 class Event;
34 }
35 
36 namespace lldb_private {
37 
38 class Listener : public std::enable_shared_from_this<Listener> {
39 public:
40  typedef bool (*HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton);
41 
42  friend class Broadcaster;
43  friend class BroadcasterManager;
44 
45  // Constructors and Destructors
46  //
47  // Listeners have to be constructed into shared pointers - at least if you
48  // want them to listen to Broadcasters,
49 protected:
50  Listener(const char *name);
51 
52 public:
53  static lldb::ListenerSP MakeListener(const char *name);
54 
55  ~Listener();
56 
57  void AddEvent(lldb::EventSP &event);
58 
59  void Clear();
60 
61  const char *GetName() { return m_name.c_str(); }
62 
63  uint32_t StartListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp,
64  const BroadcastEventSpec &event_spec);
65 
66  bool StopListeningForEventSpec(lldb::BroadcasterManagerSP manager_sp,
67  const BroadcastEventSpec &event_spec);
68 
70  uint32_t event_mask);
71 
73  uint32_t event_mask,
74  HandleBroadcastCallback callback,
75  void *callback_user_data);
76 
77  bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask);
78 
80 
82 
84  uint32_t event_type_mask);
85 
86  // Returns true if an event was received, false if we timed out.
87  bool GetEvent(lldb::EventSP &event_sp, const Timeout<std::micro> &timeout);
88 
89  bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp,
90  const Timeout<std::micro> &timeout);
91 
93  uint32_t event_type_mask,
94  lldb::EventSP &event_sp,
95  const Timeout<std::micro> &timeout);
96 
97  size_t HandleBroadcastEvent(lldb::EventSP &event_sp);
98 
99 private:
100  // Classes that inherit from Listener can see and modify these
101  struct BroadcasterInfo {
102  BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb = nullptr,
103  void *ud = nullptr)
104  : event_mask(mask), callback(cb), callback_user_data(ud) {}
105 
106  uint32_t event_mask;
107  HandleBroadcastCallback callback;
108  void *callback_user_data;
109  };
110 
111  typedef std::multimap<Broadcaster::BroadcasterImplWP, BroadcasterInfo,
112  std::owner_less<Broadcaster::BroadcasterImplWP>>
113  broadcaster_collection;
114  typedef std::list<lldb::EventSP> event_collection;
115  typedef std::vector<lldb::BroadcasterManagerWP>
116  broadcaster_manager_collection;
117 
118  bool
119  FindNextEventInternal(std::unique_lock<std::mutex> &lock,
120  Broadcaster *broadcaster, // nullptr for any broadcaster
121  const ConstString *sources, // nullptr for any event
122  uint32_t num_sources, uint32_t event_type_mask,
123  lldb::EventSP &event_sp, bool remove);
124 
125  bool GetEventInternal(const Timeout<std::micro> &timeout,
126  Broadcaster *broadcaster, // nullptr for any broadcaster
127  const ConstString *sources, // nullptr for any event
128  uint32_t num_sources, uint32_t event_type_mask,
129  lldb::EventSP &event_sp);
130 
131  std::string m_name;
132  broadcaster_collection m_broadcasters;
133  std::recursive_mutex m_broadcasters_mutex; // Protects m_broadcasters
134  event_collection m_events;
135  std::mutex m_events_mutex; // Protects m_broadcasters and m_events
136  std::condition_variable m_events_condition;
137  broadcaster_manager_collection m_broadcaster_managers;
138 
139  void BroadcasterWillDestruct(Broadcaster *);
140 
141  void BroadcasterManagerWillDestruct(lldb::BroadcasterManagerSP manager_sp);
142 
143  // broadcaster_collection::iterator
144  // FindBroadcasterWithMask (Broadcaster *broadcaster,
145  // uint32_t event_mask,
146  // bool exact);
147 
148  // For Listener only
149  DISALLOW_COPY_AND_ASSIGN(Listener);
150 };
151 
152 } // namespace lldb_private
153 
154 #endif // LLDB_UTILITY_LISTENER_H
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
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
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
void AddEvent(lldb::EventSP &event)
Definition: Listener.cpp:202
bool(* HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton)
Definition: Listener.h:40
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:465
uint32_t StartListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:86
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:542
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
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
Event * PeekAtNextEventForBroadcaster(Broadcaster *broadcaster)
Definition: Listener.cpp:325
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