LLDB  mainline
SBListener.cpp
Go to the documentation of this file.
1 //===-- SBListener.cpp ------------------------------------------*- 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 #include "lldb/API/SBListener.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBBroadcaster.h"
12 #include "lldb/API/SBDebugger.h"
13 #include "lldb/API/SBEvent.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Core/Debugger.h"
17 #include "lldb/Utility/Listener.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
23 SBListener::SBListener() : m_opaque_sp(), m_unused_ptr(NULL) {
25 }
26 
27 SBListener::SBListener(const char *name)
28  : m_opaque_sp(Listener::MakeListener(name)), m_unused_ptr(nullptr) {
29  LLDB_RECORD_CONSTRUCTOR(SBListener, (const char *), name);
30 }
31 
33  : m_opaque_sp(rhs.m_opaque_sp), m_unused_ptr(nullptr) {
35 }
36 
39  SBListener, operator=,(const lldb::SBListener &), rhs);
40 
41  if (this != &rhs) {
42  m_opaque_sp = rhs.m_opaque_sp;
43  m_unused_ptr = nullptr;
44  }
45  return LLDB_RECORD_RESULT(*this);
46 }
47 
48 SBListener::SBListener(const lldb::ListenerSP &listener_sp)
49  : m_opaque_sp(listener_sp), m_unused_ptr(nullptr) {}
50 
52 
53 bool SBListener::IsValid() const {
55  return this->operator bool();
56 }
57 SBListener::operator bool() const {
58  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBListener, operator bool);
59 
60  return m_opaque_sp != nullptr;
61 }
62 
63 void SBListener::AddEvent(const SBEvent &event) {
65  event);
66 
67  EventSP &event_sp = event.GetSP();
68  if (event_sp)
69  m_opaque_sp->AddEvent(event_sp);
70 }
71 
74 
75  if (m_opaque_sp)
76  m_opaque_sp->Clear();
77 }
78 
80  const char *broadcaster_class,
81  uint32_t event_mask) {
83  (lldb::SBDebugger &, const char *, uint32_t), debugger,
84  broadcaster_class, event_mask);
85 
86  if (m_opaque_sp) {
87  Debugger *lldb_debugger = debugger.get();
88  if (!lldb_debugger)
89  return 0;
90  BroadcastEventSpec event_spec(ConstString(broadcaster_class), event_mask);
91  return m_opaque_sp->StartListeningForEventSpec(
92  lldb_debugger->GetBroadcasterManager(), event_spec);
93  } else
94  return 0;
95 }
96 
98  const char *broadcaster_class,
99  uint32_t event_mask) {
101  (lldb::SBDebugger &, const char *, uint32_t), debugger,
102  broadcaster_class, event_mask);
103 
104  if (m_opaque_sp) {
105  Debugger *lldb_debugger = debugger.get();
106  if (!lldb_debugger)
107  return false;
108  BroadcastEventSpec event_spec(ConstString(broadcaster_class), event_mask);
109  return m_opaque_sp->StopListeningForEventSpec(
110  lldb_debugger->GetBroadcasterManager(), event_spec);
111  } else
112  return false;
113 }
114 
116  uint32_t event_mask) {
118  (const lldb::SBBroadcaster &, uint32_t), broadcaster,
119  event_mask);
120 
121  uint32_t acquired_event_mask = 0;
122  if (m_opaque_sp && broadcaster.IsValid()) {
123  acquired_event_mask =
124  m_opaque_sp->StartListeningForEvents(broadcaster.get(), event_mask);
125  }
126 
127  return acquired_event_mask;
128 }
129 
131  uint32_t event_mask) {
133  (const lldb::SBBroadcaster &, uint32_t), broadcaster,
134  event_mask);
135 
136  if (m_opaque_sp && broadcaster.IsValid()) {
137  return m_opaque_sp->StopListeningForEvents(broadcaster.get(), event_mask);
138  }
139  return false;
140 }
141 
142 bool SBListener::WaitForEvent(uint32_t timeout_secs, SBEvent &event) {
144  (uint32_t, lldb::SBEvent &), timeout_secs, event);
145 
146  bool success = false;
147 
148  if (m_opaque_sp) {
149  Timeout<std::micro> timeout(llvm::None);
150  if (timeout_secs != UINT32_MAX) {
151  assert(timeout_secs != 0); // Take this out after all calls with timeout
152  // set to zero have been removed....
153  timeout = std::chrono::seconds(timeout_secs);
154  }
155  EventSP event_sp;
156  if (m_opaque_sp->GetEvent(event_sp, timeout)) {
157  event.reset(event_sp);
158  success = true;
159  }
160  }
161 
162  if (!success)
163  event.reset(NULL);
164  return success;
165 }
166 
168  const SBBroadcaster &broadcaster,
169  SBEvent &event) {
172  num_seconds, broadcaster, event);
173 
174  if (m_opaque_sp && broadcaster.IsValid()) {
175  Timeout<std::micro> timeout(llvm::None);
176  if (num_seconds != UINT32_MAX)
177  timeout = std::chrono::seconds(num_seconds);
178  EventSP event_sp;
179  if (m_opaque_sp->GetEventForBroadcaster(broadcaster.get(), event_sp,
180  timeout)) {
181  event.reset(event_sp);
182  return true;
183  }
184  }
185  event.reset(NULL);
186  return false;
187 }
188 
190  uint32_t num_seconds, const SBBroadcaster &broadcaster,
191  uint32_t event_type_mask, SBEvent &event) {
195  num_seconds, broadcaster, event_type_mask, event);
196 
197  if (m_opaque_sp && broadcaster.IsValid()) {
198  Timeout<std::micro> timeout(llvm::None);
199  if (num_seconds != UINT32_MAX)
200  timeout = std::chrono::seconds(num_seconds);
201  EventSP event_sp;
202  if (m_opaque_sp->GetEventForBroadcasterWithType(
203  broadcaster.get(), event_type_mask, event_sp, timeout)) {
204  event.reset(event_sp);
205  return true;
206  }
207  }
208  event.reset(NULL);
209  return false;
210 }
211 
214  event);
215 
216  if (m_opaque_sp) {
217  event.reset(m_opaque_sp->PeekAtNextEvent());
218  return event.IsValid();
219  }
220  event.reset(NULL);
221  return false;
222 }
223 
225  SBEvent &event) {
227  (const lldb::SBBroadcaster &, lldb::SBEvent &),
228  broadcaster, event);
229 
230  if (m_opaque_sp && broadcaster.IsValid()) {
231  event.reset(m_opaque_sp->PeekAtNextEventForBroadcaster(broadcaster.get()));
232  return event.IsValid();
233  }
234  event.reset(NULL);
235  return false;
236 }
237 
239  const SBBroadcaster &broadcaster, uint32_t event_type_mask,
240  SBEvent &event) {
243  broadcaster, event_type_mask, event);
244 
245  if (m_opaque_sp && broadcaster.IsValid()) {
246  event.reset(m_opaque_sp->PeekAtNextEventForBroadcasterWithType(
247  broadcaster.get(), event_type_mask));
248  return event.IsValid();
249  }
250  event.reset(NULL);
251  return false;
252 }
253 
256 
257  if (m_opaque_sp) {
258  EventSP event_sp;
259  if (m_opaque_sp->GetEvent(event_sp, std::chrono::seconds(0))) {
260  event.reset(event_sp);
261  return true;
262  }
263  }
264  event.reset(NULL);
265  return false;
266 }
267 
269  SBEvent &event) {
271  (const lldb::SBBroadcaster &, lldb::SBEvent &),
272  broadcaster, event);
273 
274  if (m_opaque_sp && broadcaster.IsValid()) {
275  EventSP event_sp;
276  if (m_opaque_sp->GetEventForBroadcaster(broadcaster.get(), event_sp,
277  std::chrono::seconds(0))) {
278  event.reset(event_sp);
279  return true;
280  }
281  }
282  event.reset(NULL);
283  return false;
284 }
285 
287  const SBBroadcaster &broadcaster, uint32_t event_type_mask,
288  SBEvent &event) {
291  broadcaster, event_type_mask, event);
292 
293  if (m_opaque_sp && broadcaster.IsValid()) {
294  EventSP event_sp;
295  if (m_opaque_sp->GetEventForBroadcasterWithType(broadcaster.get(),
296  event_type_mask, event_sp,
297  std::chrono::seconds(0))) {
298  event.reset(event_sp);
299  return true;
300  }
301  }
302  event.reset(NULL);
303  return false;
304 }
305 
308  (const lldb::SBEvent &), event);
309 
310  if (m_opaque_sp)
311  return m_opaque_sp->HandleBroadcastEvent(event.GetSP());
312  return false;
313 }
314 
315 lldb::ListenerSP SBListener::GetSP() { return m_opaque_sp; }
316 
317 Listener *SBListener::operator->() const { return m_opaque_sp.get(); }
318 
319 Listener *SBListener::get() const { return m_opaque_sp.get(); }
320 
321 void SBListener::reset(ListenerSP listener_sp) {
322  m_opaque_sp = listener_sp;
323  m_unused_ptr = nullptr;
324 }
325 
326 namespace lldb_private {
327 namespace repro {
328 
329 template <>
332  LLDB_REGISTER_CONSTRUCTOR(SBListener, (const char *));
335  SBListener, operator=,(const lldb::SBListener &));
337  LLDB_REGISTER_METHOD_CONST(bool, SBListener, operator bool, ());
341  (lldb::SBDebugger &, const char *, uint32_t));
343  (lldb::SBDebugger &, const char *, uint32_t));
345  (const lldb::SBBroadcaster &, uint32_t));
347  (const lldb::SBBroadcaster &, uint32_t));
349  (uint32_t, lldb::SBEvent &));
358  (const lldb::SBBroadcaster &, lldb::SBEvent &));
364  (const lldb::SBBroadcaster &, lldb::SBEvent &));
369  (const lldb::SBEvent &));
370 }
371 
372 }
373 }
A class to manage flag bits.
Definition: Debugger.h:82
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
bool WaitForEventForBroadcaster(uint32_t num_seconds, const lldb::SBBroadcaster &broadcaster, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:167
const lldb::SBListener & operator=(const lldb::SBListener &rhs)
Definition: SBListener.cpp:37
bool HandleBroadcastEvent(const lldb::SBEvent &event)
Definition: SBListener.cpp:306
bool GetNextEvent(lldb::SBEvent &sb_event)
Definition: SBListener.cpp:254
lldb_private::Broadcaster * get() const
bool WaitForEvent(uint32_t num_seconds, lldb::SBEvent &event)
Definition: SBListener.cpp:142
bool PeekAtNextEvent(lldb::SBEvent &sb_event)
Definition: SBListener.cpp:212
lldb::EventSP & GetSP() const
Definition: SBEvent.cpp:139
void RegisterMethods< SBListener >(Registry &R)
Definition: SBListener.cpp:330
bool StopListeningForEventClass(SBDebugger &debugger, const char *broadcaster_class, uint32_t event_mask)
Definition: SBListener.cpp:97
bool IsValid() const
Definition: SBListener.cpp:53
bool PeekAtNextEventForBroadcasterWithType(const lldb::SBBroadcaster &broadcaster, uint32_t event_type_mask, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:238
lldb::ListenerSP GetSP()
Definition: SBListener.cpp:315
#define UINT32_MAX
Definition: lldb-defines.h:31
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
uint32_t StartListeningForEvents(const lldb::SBBroadcaster &broadcaster, uint32_t event_mask)
Definition: SBListener.cpp:115
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
uint32_t StartListeningForEventClass(SBDebugger &debugger, const char *broadcaster_class, uint32_t event_mask)
Definition: SBListener.cpp:79
bool PeekAtNextEventForBroadcaster(const lldb::SBBroadcaster &broadcaster, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:224
bool GetNextEventForBroadcaster(const lldb::SBBroadcaster &broadcaster, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:268
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
bool WaitForEventForBroadcasterWithType(uint32_t num_seconds, const lldb::SBBroadcaster &broadcaster, uint32_t event_type_mask, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:189
bool GetNextEventForBroadcasterWithType(const lldb::SBBroadcaster &broadcaster, uint32_t event_type_mask, lldb::SBEvent &sb_event)
Definition: SBListener.cpp:286
lldb::BroadcasterManagerSP GetBroadcasterManager()
Definition: Debugger.h:329
A uniqued constant string class.
Definition: ConstString.h:38
Definition: SBAddress.h:15
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
void AddEvent(const lldb::SBEvent &event)
Definition: SBListener.cpp:63
#define LLDB_RECORD_RESULT(Result)
bool StopListeningForEvents(const lldb::SBBroadcaster &broadcaster, uint32_t event_mask)
Definition: SBListener.cpp:130
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)