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 <cstddef>
27 #include <cstdint>
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 
132  std::recursive_mutex m_broadcasters_mutex; // Protects m_broadcasters
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
lldb_private::Listener::m_broadcasters_mutex
std::recursive_mutex m_broadcasters_mutex
Definition: Listener.h:132
lldb_private::Listener::FindNextEventInternal
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
lldb_private::Listener::m_events_mutex
std::mutex m_events_mutex
Definition: Listener.h:134
lldb_private::Listener::HandleBroadcastEvent
size_t HandleBroadcastEvent(lldb::EventSP &event_sp)
Definition: Listener.cpp:402
lldb_private::Listener::operator=
const Listener & operator=(const Listener &)=delete
lldb_private::Event
Definition: Event.h:180
lldb_private::Listener::m_broadcasters
broadcaster_collection m_broadcasters
Definition: Listener.h:131
lldb_private::Listener::StartListeningForEventSpec
uint32_t StartListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:426
lldb-defines.h
lldb_private::Listener::BroadcasterInfo::callback_user_data
void * callback_user_data
Definition: Listener.h:107
lldb_private::Listener::GetEventInternal
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
lldb_private::Listener::PeekAtNextEventForBroadcasterWithType
Event * PeekAtNextEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask)
Definition: Listener.cpp:335
lldb_private::Broadcaster::BroadcasterImplWP
std::weak_ptr< BroadcasterImpl > BroadcasterImplWP
Definition: Broadcaster.h:522
lldb_private::Listener::GetName
const char * GetName()
Definition: Listener.h:59
Broadcaster.h
lldb_private::Listener::GetEventForBroadcasterWithType
bool GetEventForBroadcasterWithType(Broadcaster *broadcaster, uint32_t event_type_mask, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:385
lldb_private::Listener::broadcaster_collection
std::multimap< Broadcaster::BroadcasterImplWP, BroadcasterInfo, std::owner_less< Broadcaster::BroadcasterImplWP > > broadcaster_collection
Definition: Listener.h:112
lldb_private::Listener::~Listener
~Listener()
Definition: Listener.cpp:49
lldb_private::Listener::BroadcasterInfo
Definition: Listener.h:100
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Listener::StartListeningForEvents
uint32_t StartListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:84
lldb_private::Listener::AddEvent
void AddEvent(lldb::EventSP &event)
Definition: Listener.cpp:202
lldb_private::Listener::PeekAtNextEventForBroadcaster
Event * PeekAtNextEventForBroadcaster(Broadcaster *broadcaster)
Definition: Listener.cpp:326
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Listener::PeekAtNextEvent
Event * PeekAtNextEvent()
Definition: Listener.cpp:318
lldb_private::Listener
Definition: Listener.h:36
lldb_private::Listener::Clear
void Clear()
Definition: Listener.cpp:58
lldb_private::Listener::StopListeningForEventSpec
bool StopListeningForEventSpec(const lldb::BroadcasterManagerSP &manager_sp, const BroadcastEventSpec &event_spec)
Definition: Listener.cpp:454
lldb_private::Listener::BroadcasterInfo::event_mask
uint32_t event_mask
Definition: Listener.h:105
lldb_private::Listener::Listener
Listener(const char *name)
Definition: Listener.cpp:40
lldb_private::Listener::BroadcasterInfo::BroadcasterInfo
BroadcasterInfo(uint32_t mask, HandleBroadcastCallback cb=nullptr, void *ud=nullptr)
Definition: Listener.h:101
lldb_private::Timeout< std::micro >
lldb_private::Listener::GetEvent
bool GetEvent(lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:398
uint32_t
lldb_private::BroadcasterManager
Definition: Broadcaster.h:74
lldb_private::Listener::BroadcasterManagerWillDestruct
void BroadcasterManagerWillDestruct(lldb::BroadcasterManagerSP manager_sp)
Definition: Listener.cpp:188
lldb_private::Listener::m_events
event_collection m_events
Definition: Listener.h:133
lldb_private::Listener::event_collection
std::list< lldb::EventSP > event_collection
Definition: Listener.h:113
lldb_private::BroadcastEventSpec
lldb::BroadcastEventSpec
Definition: Broadcaster.h:40
lldb_private::Listener::m_name
std::string m_name
Definition: Listener.h:130
lldb_private::Listener::StopListeningForEvents
bool StopListeningForEvents(Broadcaster *broadcaster, uint32_t event_mask)
Definition: Listener.cpp:148
lldb_private::Listener::BroadcasterWillDestruct
void BroadcasterWillDestruct(Broadcaster *)
Definition: Listener.cpp:166
lldb_private::Listener::HandleBroadcastCallback
bool(* HandleBroadcastCallback)(lldb::EventSP &event_sp, void *baton)
Definition: Listener.h:38
Timeout.h
lldb-forward.h
lldb_private::Listener::broadcaster_manager_collection
std::vector< lldb::BroadcasterManagerWP > broadcaster_manager_collection
Definition: Listener.h:115
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Listener::BroadcasterInfo::callback
HandleBroadcastCallback callback
Definition: Listener.h:106
lldb_private::Listener::m_broadcaster_managers
broadcaster_manager_collection m_broadcaster_managers
Definition: Listener.h:136
lldb_private::Listener::MakeListener
static lldb::ListenerSP MakeListener(const char *name)
Definition: Listener.cpp:464
lldb_private::Broadcaster
Definition: Broadcaster.h:242
lldb_private::Listener::GetEventForBroadcaster
bool GetEventForBroadcaster(Broadcaster *broadcaster, lldb::EventSP &event_sp, const Timeout< std::micro > &timeout)
Definition: Listener.cpp:392
lldb_private::Listener::m_events_condition
std::condition_variable m_events_condition
Definition: Listener.h:135