1 //===-- SBListener.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
16 #include "lldb/Utility/Broadcaster.h"
17 #include "lldb/Utility/Listener.h"
18 #include "lldb/Utility/StreamString.h"
21 using namespace lldb_private
;
23 SBListener::SBListener() : m_opaque_sp(), m_unused_ptr(nullptr) {
24 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBListener
);
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
);
32 SBListener::SBListener(const SBListener
&rhs
)
33 : m_opaque_sp(rhs
.m_opaque_sp
), m_unused_ptr(nullptr) {
34 LLDB_RECORD_CONSTRUCTOR(SBListener
, (const lldb::SBListener
&), rhs
);
37 const lldb::SBListener
&SBListener::operator=(const lldb::SBListener
&rhs
) {
38 LLDB_RECORD_METHOD(const lldb::SBListener
&,
39 SBListener
, operator=,(const lldb::SBListener
&), rhs
);
42 m_opaque_sp
= rhs
.m_opaque_sp
;
43 m_unused_ptr
= nullptr;
45 return LLDB_RECORD_RESULT(*this);
48 SBListener::SBListener(const lldb::ListenerSP
&listener_sp
)
49 : m_opaque_sp(listener_sp
), m_unused_ptr(nullptr) {}
51 SBListener::~SBListener() {}
53 bool SBListener::IsValid() const {
54 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBListener
, IsValid
);
55 return this->operator bool();
57 SBListener::operator bool() const {
58 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBListener
, operator bool);
60 return m_opaque_sp
!= nullptr;
63 void SBListener::AddEvent(const SBEvent
&event
) {
64 LLDB_RECORD_METHOD(void, SBListener
, AddEvent
, (const lldb::SBEvent
&),
67 EventSP
&event_sp
= event
.GetSP();
69 m_opaque_sp
->AddEvent(event_sp
);
72 void SBListener::Clear() {
73 LLDB_RECORD_METHOD_NO_ARGS(void, SBListener
, Clear
);
79 uint32_t SBListener::StartListeningForEventClass(SBDebugger
&debugger
,
80 const char *broadcaster_class
,
81 uint32_t event_mask
) {
82 LLDB_RECORD_METHOD(uint32_t, SBListener
, StartListeningForEventClass
,
83 (lldb::SBDebugger
&, const char *, uint32_t), debugger
,
84 broadcaster_class
, event_mask
);
87 Debugger
*lldb_debugger
= debugger
.get();
90 BroadcastEventSpec
event_spec(ConstString(broadcaster_class
), event_mask
);
91 return m_opaque_sp
->StartListeningForEventSpec(
92 lldb_debugger
->GetBroadcasterManager(), event_spec
);
97 bool SBListener::StopListeningForEventClass(SBDebugger
&debugger
,
98 const char *broadcaster_class
,
99 uint32_t event_mask
) {
100 LLDB_RECORD_METHOD(bool, SBListener
, StopListeningForEventClass
,
101 (lldb::SBDebugger
&, const char *, uint32_t), debugger
,
102 broadcaster_class
, event_mask
);
105 Debugger
*lldb_debugger
= debugger
.get();
108 BroadcastEventSpec
event_spec(ConstString(broadcaster_class
), event_mask
);
109 return m_opaque_sp
->StopListeningForEventSpec(
110 lldb_debugger
->GetBroadcasterManager(), event_spec
);
115 uint32_t SBListener::StartListeningForEvents(const SBBroadcaster
&broadcaster
,
116 uint32_t event_mask
) {
117 LLDB_RECORD_METHOD(uint32_t, SBListener
, StartListeningForEvents
,
118 (const lldb::SBBroadcaster
&, uint32_t), broadcaster
,
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
);
127 return acquired_event_mask
;
130 bool SBListener::StopListeningForEvents(const SBBroadcaster
&broadcaster
,
131 uint32_t event_mask
) {
132 LLDB_RECORD_METHOD(bool, SBListener
, StopListeningForEvents
,
133 (const lldb::SBBroadcaster
&, uint32_t), broadcaster
,
136 if (m_opaque_sp
&& broadcaster
.IsValid()) {
137 return m_opaque_sp
->StopListeningForEvents(broadcaster
.get(), event_mask
);
142 bool SBListener::WaitForEvent(uint32_t timeout_secs
, SBEvent
&event
) {
143 LLDB_RECORD_METHOD(bool, SBListener
, WaitForEvent
,
144 (uint32_t, lldb::SBEvent
&), timeout_secs
, event
);
146 bool success
= false;
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
);
156 if (m_opaque_sp
->GetEvent(event_sp
, timeout
)) {
157 event
.reset(event_sp
);
163 event
.reset(nullptr);
167 bool SBListener::WaitForEventForBroadcaster(uint32_t num_seconds
,
168 const SBBroadcaster
&broadcaster
,
170 LLDB_RECORD_METHOD(bool, SBListener
, WaitForEventForBroadcaster
,
171 (uint32_t, const lldb::SBBroadcaster
&, lldb::SBEvent
&),
172 num_seconds
, broadcaster
, event
);
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
);
179 if (m_opaque_sp
->GetEventForBroadcaster(broadcaster
.get(), event_sp
,
181 event
.reset(event_sp
);
185 event
.reset(nullptr);
189 bool SBListener::WaitForEventForBroadcasterWithType(
190 uint32_t num_seconds
, const SBBroadcaster
&broadcaster
,
191 uint32_t event_type_mask
, SBEvent
&event
) {
193 bool, SBListener
, WaitForEventForBroadcasterWithType
,
194 (uint32_t, const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&),
195 num_seconds
, broadcaster
, event_type_mask
, event
);
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
);
202 if (m_opaque_sp
->GetEventForBroadcasterWithType(
203 broadcaster
.get(), event_type_mask
, event_sp
, timeout
)) {
204 event
.reset(event_sp
);
208 event
.reset(nullptr);
212 bool SBListener::PeekAtNextEvent(SBEvent
&event
) {
213 LLDB_RECORD_METHOD(bool, SBListener
, PeekAtNextEvent
, (lldb::SBEvent
&),
217 event
.reset(m_opaque_sp
->PeekAtNextEvent());
218 return event
.IsValid();
220 event
.reset(nullptr);
224 bool SBListener::PeekAtNextEventForBroadcaster(const SBBroadcaster
&broadcaster
,
226 LLDB_RECORD_METHOD(bool, SBListener
, PeekAtNextEventForBroadcaster
,
227 (const lldb::SBBroadcaster
&, lldb::SBEvent
&),
230 if (m_opaque_sp
&& broadcaster
.IsValid()) {
231 event
.reset(m_opaque_sp
->PeekAtNextEventForBroadcaster(broadcaster
.get()));
232 return event
.IsValid();
234 event
.reset(nullptr);
238 bool SBListener::PeekAtNextEventForBroadcasterWithType(
239 const SBBroadcaster
&broadcaster
, uint32_t event_type_mask
,
241 LLDB_RECORD_METHOD(bool, SBListener
, PeekAtNextEventForBroadcasterWithType
,
242 (const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&),
243 broadcaster
, event_type_mask
, event
);
245 if (m_opaque_sp
&& broadcaster
.IsValid()) {
246 event
.reset(m_opaque_sp
->PeekAtNextEventForBroadcasterWithType(
247 broadcaster
.get(), event_type_mask
));
248 return event
.IsValid();
250 event
.reset(nullptr);
254 bool SBListener::GetNextEvent(SBEvent
&event
) {
255 LLDB_RECORD_METHOD(bool, SBListener
, GetNextEvent
, (lldb::SBEvent
&), event
);
259 if (m_opaque_sp
->GetEvent(event_sp
, std::chrono::seconds(0))) {
260 event
.reset(event_sp
);
264 event
.reset(nullptr);
268 bool SBListener::GetNextEventForBroadcaster(const SBBroadcaster
&broadcaster
,
270 LLDB_RECORD_METHOD(bool, SBListener
, GetNextEventForBroadcaster
,
271 (const lldb::SBBroadcaster
&, lldb::SBEvent
&),
274 if (m_opaque_sp
&& broadcaster
.IsValid()) {
276 if (m_opaque_sp
->GetEventForBroadcaster(broadcaster
.get(), event_sp
,
277 std::chrono::seconds(0))) {
278 event
.reset(event_sp
);
282 event
.reset(nullptr);
286 bool SBListener::GetNextEventForBroadcasterWithType(
287 const SBBroadcaster
&broadcaster
, uint32_t event_type_mask
,
289 LLDB_RECORD_METHOD(bool, SBListener
, GetNextEventForBroadcasterWithType
,
290 (const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&),
291 broadcaster
, event_type_mask
, event
);
293 if (m_opaque_sp
&& broadcaster
.IsValid()) {
295 if (m_opaque_sp
->GetEventForBroadcasterWithType(broadcaster
.get(),
296 event_type_mask
, event_sp
,
297 std::chrono::seconds(0))) {
298 event
.reset(event_sp
);
302 event
.reset(nullptr);
306 bool SBListener::HandleBroadcastEvent(const SBEvent
&event
) {
307 LLDB_RECORD_METHOD(bool, SBListener
, HandleBroadcastEvent
,
308 (const lldb::SBEvent
&), event
);
311 return m_opaque_sp
->HandleBroadcastEvent(event
.GetSP());
315 lldb::ListenerSP
SBListener::GetSP() { return m_opaque_sp
; }
317 Listener
*SBListener::operator->() const { return m_opaque_sp
.get(); }
319 Listener
*SBListener::get() const { return m_opaque_sp
.get(); }
321 void SBListener::reset(ListenerSP listener_sp
) {
322 m_opaque_sp
= listener_sp
;
323 m_unused_ptr
= nullptr;
326 namespace lldb_private
{
330 void RegisterMethods
<SBListener
>(Registry
&R
) {
331 LLDB_REGISTER_CONSTRUCTOR(SBListener
, ());
332 LLDB_REGISTER_CONSTRUCTOR(SBListener
, (const char *));
333 LLDB_REGISTER_CONSTRUCTOR(SBListener
, (const lldb::SBListener
&));
334 LLDB_REGISTER_METHOD(const lldb::SBListener
&,
335 SBListener
, operator=,(const lldb::SBListener
&));
336 LLDB_REGISTER_METHOD_CONST(bool, SBListener
, IsValid
, ());
337 LLDB_REGISTER_METHOD_CONST(bool, SBListener
, operator bool, ());
338 LLDB_REGISTER_METHOD(void, SBListener
, AddEvent
, (const lldb::SBEvent
&));
339 LLDB_REGISTER_METHOD(void, SBListener
, Clear
, ());
340 LLDB_REGISTER_METHOD(uint32_t, SBListener
, StartListeningForEventClass
,
341 (lldb::SBDebugger
&, const char *, uint32_t));
342 LLDB_REGISTER_METHOD(bool, SBListener
, StopListeningForEventClass
,
343 (lldb::SBDebugger
&, const char *, uint32_t));
344 LLDB_REGISTER_METHOD(uint32_t, SBListener
, StartListeningForEvents
,
345 (const lldb::SBBroadcaster
&, uint32_t));
346 LLDB_REGISTER_METHOD(bool, SBListener
, StopListeningForEvents
,
347 (const lldb::SBBroadcaster
&, uint32_t));
348 LLDB_REGISTER_METHOD(bool, SBListener
, WaitForEvent
,
349 (uint32_t, lldb::SBEvent
&));
350 LLDB_REGISTER_METHOD(
351 bool, SBListener
, WaitForEventForBroadcaster
,
352 (uint32_t, const lldb::SBBroadcaster
&, lldb::SBEvent
&));
353 LLDB_REGISTER_METHOD(
354 bool, SBListener
, WaitForEventForBroadcasterWithType
,
355 (uint32_t, const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&));
356 LLDB_REGISTER_METHOD(bool, SBListener
, PeekAtNextEvent
, (lldb::SBEvent
&));
357 LLDB_REGISTER_METHOD(bool, SBListener
, PeekAtNextEventForBroadcaster
,
358 (const lldb::SBBroadcaster
&, lldb::SBEvent
&));
359 LLDB_REGISTER_METHOD(
360 bool, SBListener
, PeekAtNextEventForBroadcasterWithType
,
361 (const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&));
362 LLDB_REGISTER_METHOD(bool, SBListener
, GetNextEvent
, (lldb::SBEvent
&));
363 LLDB_REGISTER_METHOD(bool, SBListener
, GetNextEventForBroadcaster
,
364 (const lldb::SBBroadcaster
&, lldb::SBEvent
&));
365 LLDB_REGISTER_METHOD(
366 bool, SBListener
, GetNextEventForBroadcasterWithType
,
367 (const lldb::SBBroadcaster
&, uint32_t, lldb::SBEvent
&));
368 LLDB_REGISTER_METHOD(bool, SBListener
, HandleBroadcastEvent
,
369 (const lldb::SBEvent
&));