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