1 //===-- SBListener.cpp ----------------------------------------------------===//
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 "lldb/API/SBBroadcaster.h"
11 #include "lldb/API/SBDebugger.h"
12 #include "lldb/API/SBEvent.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Utility/Broadcaster.h"
16 #include "lldb/Utility/Instrumentation.h"
17 #include "lldb/Utility/Listener.h"
18 #include "lldb/Utility/StreamString.h"
21 using namespace lldb_private
;
23 SBListener::SBListener() { LLDB_INSTRUMENT_VA(this); }
25 SBListener::SBListener(const char *name
)
26 : m_opaque_sp(Listener::MakeListener(name
)) {
27 LLDB_INSTRUMENT_VA(this, name
);
30 SBListener::SBListener(const SBListener
&rhs
) : m_opaque_sp(rhs
.m_opaque_sp
) {
31 LLDB_INSTRUMENT_VA(this, rhs
);
34 const lldb::SBListener
&SBListener::operator=(const lldb::SBListener
&rhs
) {
35 LLDB_INSTRUMENT_VA(this, rhs
);
38 m_opaque_sp
= rhs
.m_opaque_sp
;
39 m_unused_ptr
= nullptr;
44 SBListener::SBListener(const lldb::ListenerSP
&listener_sp
)
45 : m_opaque_sp(listener_sp
) {}
47 SBListener::~SBListener() = default;
49 bool SBListener::IsValid() const {
50 LLDB_INSTRUMENT_VA(this);
51 return this->operator bool();
53 SBListener::operator bool() const {
54 LLDB_INSTRUMENT_VA(this);
56 return m_opaque_sp
!= nullptr;
59 void SBListener::AddEvent(const SBEvent
&event
) {
60 LLDB_INSTRUMENT_VA(this, event
);
62 EventSP
&event_sp
= event
.GetSP();
64 m_opaque_sp
->AddEvent(event_sp
);
67 void SBListener::Clear() {
68 LLDB_INSTRUMENT_VA(this);
74 uint32_t SBListener::StartListeningForEventClass(SBDebugger
&debugger
,
75 const char *broadcaster_class
,
76 uint32_t event_mask
) {
77 LLDB_INSTRUMENT_VA(this, debugger
, broadcaster_class
, event_mask
);
80 Debugger
*lldb_debugger
= debugger
.get();
83 BroadcastEventSpec
event_spec(ConstString(broadcaster_class
), event_mask
);
84 return m_opaque_sp
->StartListeningForEventSpec(
85 lldb_debugger
->GetBroadcasterManager(), event_spec
);
90 bool SBListener::StopListeningForEventClass(SBDebugger
&debugger
,
91 const char *broadcaster_class
,
92 uint32_t event_mask
) {
93 LLDB_INSTRUMENT_VA(this, debugger
, broadcaster_class
, event_mask
);
96 Debugger
*lldb_debugger
= debugger
.get();
99 BroadcastEventSpec
event_spec(ConstString(broadcaster_class
), event_mask
);
100 return m_opaque_sp
->StopListeningForEventSpec(
101 lldb_debugger
->GetBroadcasterManager(), event_spec
);
106 uint32_t SBListener::StartListeningForEvents(const SBBroadcaster
&broadcaster
,
107 uint32_t event_mask
) {
108 LLDB_INSTRUMENT_VA(this, broadcaster
, event_mask
);
110 uint32_t acquired_event_mask
= 0;
111 if (m_opaque_sp
&& broadcaster
.IsValid()) {
112 acquired_event_mask
=
113 m_opaque_sp
->StartListeningForEvents(broadcaster
.get(), event_mask
);
116 return acquired_event_mask
;
119 bool SBListener::StopListeningForEvents(const SBBroadcaster
&broadcaster
,
120 uint32_t event_mask
) {
121 LLDB_INSTRUMENT_VA(this, broadcaster
, event_mask
);
123 if (m_opaque_sp
&& broadcaster
.IsValid()) {
124 return m_opaque_sp
->StopListeningForEvents(broadcaster
.get(), event_mask
);
129 bool SBListener::WaitForEvent(uint32_t timeout_secs
, SBEvent
&event
) {
130 LLDB_INSTRUMENT_VA(this, timeout_secs
, event
);
132 bool success
= false;
135 Timeout
<std::micro
> timeout(std::nullopt
);
136 if (timeout_secs
!= UINT32_MAX
) {
137 assert(timeout_secs
!= 0); // Take this out after all calls with timeout
138 // set to zero have been removed....
139 timeout
= std::chrono::seconds(timeout_secs
);
142 if (m_opaque_sp
->GetEvent(event_sp
, timeout
)) {
143 event
.reset(event_sp
);
149 event
.reset(nullptr);
153 bool SBListener::WaitForEventForBroadcaster(uint32_t num_seconds
,
154 const SBBroadcaster
&broadcaster
,
156 LLDB_INSTRUMENT_VA(this, num_seconds
, broadcaster
, event
);
158 if (m_opaque_sp
&& broadcaster
.IsValid()) {
159 Timeout
<std::micro
> timeout(std::nullopt
);
160 if (num_seconds
!= UINT32_MAX
)
161 timeout
= std::chrono::seconds(num_seconds
);
163 if (m_opaque_sp
->GetEventForBroadcaster(broadcaster
.get(), event_sp
,
165 event
.reset(event_sp
);
169 event
.reset(nullptr);
173 bool SBListener::WaitForEventForBroadcasterWithType(
174 uint32_t num_seconds
, const SBBroadcaster
&broadcaster
,
175 uint32_t event_type_mask
, SBEvent
&event
) {
176 LLDB_INSTRUMENT_VA(this, num_seconds
, broadcaster
, event_type_mask
, event
);
178 if (m_opaque_sp
&& broadcaster
.IsValid()) {
179 Timeout
<std::micro
> timeout(std::nullopt
);
180 if (num_seconds
!= UINT32_MAX
)
181 timeout
= std::chrono::seconds(num_seconds
);
183 if (m_opaque_sp
->GetEventForBroadcasterWithType(
184 broadcaster
.get(), event_type_mask
, event_sp
, timeout
)) {
185 event
.reset(event_sp
);
189 event
.reset(nullptr);
193 bool SBListener::PeekAtNextEvent(SBEvent
&event
) {
194 LLDB_INSTRUMENT_VA(this, event
);
197 event
.reset(m_opaque_sp
->PeekAtNextEvent());
198 return event
.IsValid();
200 event
.reset(nullptr);
204 bool SBListener::PeekAtNextEventForBroadcaster(const SBBroadcaster
&broadcaster
,
206 LLDB_INSTRUMENT_VA(this, broadcaster
, event
);
208 if (m_opaque_sp
&& broadcaster
.IsValid()) {
209 event
.reset(m_opaque_sp
->PeekAtNextEventForBroadcaster(broadcaster
.get()));
210 return event
.IsValid();
212 event
.reset(nullptr);
216 bool SBListener::PeekAtNextEventForBroadcasterWithType(
217 const SBBroadcaster
&broadcaster
, uint32_t event_type_mask
,
219 LLDB_INSTRUMENT_VA(this, broadcaster
, event_type_mask
, event
);
221 if (m_opaque_sp
&& broadcaster
.IsValid()) {
222 event
.reset(m_opaque_sp
->PeekAtNextEventForBroadcasterWithType(
223 broadcaster
.get(), event_type_mask
));
224 return event
.IsValid();
226 event
.reset(nullptr);
230 bool SBListener::GetNextEvent(SBEvent
&event
) {
231 LLDB_INSTRUMENT_VA(this, event
);
235 if (m_opaque_sp
->GetEvent(event_sp
, std::chrono::seconds(0))) {
236 event
.reset(event_sp
);
240 event
.reset(nullptr);
244 bool SBListener::GetNextEventForBroadcaster(const SBBroadcaster
&broadcaster
,
246 LLDB_INSTRUMENT_VA(this, broadcaster
, event
);
248 if (m_opaque_sp
&& broadcaster
.IsValid()) {
250 if (m_opaque_sp
->GetEventForBroadcaster(broadcaster
.get(), event_sp
,
251 std::chrono::seconds(0))) {
252 event
.reset(event_sp
);
256 event
.reset(nullptr);
260 bool SBListener::GetNextEventForBroadcasterWithType(
261 const SBBroadcaster
&broadcaster
, uint32_t event_type_mask
,
263 LLDB_INSTRUMENT_VA(this, broadcaster
, event_type_mask
, event
);
265 if (m_opaque_sp
&& broadcaster
.IsValid()) {
267 if (m_opaque_sp
->GetEventForBroadcasterWithType(broadcaster
.get(),
268 event_type_mask
, event_sp
,
269 std::chrono::seconds(0))) {
270 event
.reset(event_sp
);
274 event
.reset(nullptr);
278 bool SBListener::HandleBroadcastEvent(const SBEvent
&event
) {
279 LLDB_INSTRUMENT_VA(this, event
);
282 return m_opaque_sp
->HandleBroadcastEvent(event
.GetSP());
286 lldb::ListenerSP
SBListener::GetSP() { return m_opaque_sp
; }
288 Listener
*SBListener::operator->() const { return m_opaque_sp
.get(); }
290 Listener
*SBListener::get() const { return m_opaque_sp
.get(); }
292 void SBListener::reset(ListenerSP listener_sp
) {
293 m_opaque_sp
= listener_sp
;
294 m_unused_ptr
= nullptr;