1 //===-- SBEvent.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/SBEvent.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBBroadcaster.h"
12 #include "lldb/API/SBStream.h"
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Core/StreamFile.h"
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Target/Process.h"
18 #include "lldb/Utility/ConstString.h"
19 #include "lldb/Utility/Event.h"
20 #include "lldb/Utility/Stream.h"
23 using namespace lldb_private
;
25 SBEvent::SBEvent() : m_event_sp(), m_opaque_ptr(nullptr) {
26 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEvent
);
29 SBEvent::SBEvent(uint32_t event_type
, const char *cstr
, uint32_t cstr_len
)
30 : m_event_sp(new Event(event_type
, new EventDataBytes(cstr
, cstr_len
))),
31 m_opaque_ptr(m_event_sp
.get()) {
32 LLDB_RECORD_CONSTRUCTOR(SBEvent
, (uint32_t, const char *, uint32_t),
33 event_type
, cstr
, cstr_len
);
36 SBEvent::SBEvent(EventSP
&event_sp
)
37 : m_event_sp(event_sp
), m_opaque_ptr(event_sp
.get()) {
38 LLDB_RECORD_CONSTRUCTOR(SBEvent
, (lldb::EventSP
&), event_sp
);
41 SBEvent::SBEvent(Event
*event_ptr
) : m_event_sp(), m_opaque_ptr(event_ptr
) {
42 LLDB_RECORD_CONSTRUCTOR(SBEvent
, (lldb_private::Event
*), event_ptr
);
45 SBEvent::SBEvent(const SBEvent
&rhs
)
46 : m_event_sp(rhs
.m_event_sp
), m_opaque_ptr(rhs
.m_opaque_ptr
) {
47 LLDB_RECORD_CONSTRUCTOR(SBEvent
, (const lldb::SBEvent
&), rhs
);
50 const SBEvent
&SBEvent::operator=(const SBEvent
&rhs
) {
51 LLDB_RECORD_METHOD(const lldb::SBEvent
&,
52 SBEvent
, operator=,(const lldb::SBEvent
&), rhs
);
55 m_event_sp
= rhs
.m_event_sp
;
56 m_opaque_ptr
= rhs
.m_opaque_ptr
;
58 return LLDB_RECORD_RESULT(*this);
61 SBEvent::~SBEvent() {}
63 const char *SBEvent::GetDataFlavor() {
64 LLDB_RECORD_METHOD_NO_ARGS(const char *, SBEvent
, GetDataFlavor
);
66 Event
*lldb_event
= get();
68 EventData
*event_data
= lldb_event
->GetData();
70 return lldb_event
->GetData()->GetFlavor().AsCString();
75 uint32_t SBEvent::GetType() const {
76 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBEvent
, GetType
);
79 const Event
*lldb_event
= get();
80 uint32_t event_type
= 0;
82 event_type
= lldb_event
->GetType();
88 SBBroadcaster
SBEvent::GetBroadcaster() const {
89 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBBroadcaster
, SBEvent
,
92 SBBroadcaster broadcaster
;
93 const Event
*lldb_event
= get();
95 broadcaster
.reset(lldb_event
->GetBroadcaster(), false);
96 return LLDB_RECORD_RESULT(broadcaster
);
99 const char *SBEvent::GetBroadcasterClass() const {
100 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBEvent
, GetBroadcasterClass
);
102 const Event
*lldb_event
= get();
104 return lldb_event
->GetBroadcaster()->GetBroadcasterClass().AsCString();
106 return "unknown class";
109 bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster
*broadcaster
) {
110 LLDB_RECORD_METHOD(bool, SBEvent
, BroadcasterMatchesPtr
,
111 (const lldb::SBBroadcaster
*), broadcaster
);
114 return BroadcasterMatchesRef(*broadcaster
);
118 bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster
&broadcaster
) {
119 LLDB_RECORD_METHOD(bool, SBEvent
, BroadcasterMatchesRef
,
120 (const lldb::SBBroadcaster
&), broadcaster
);
122 Event
*lldb_event
= get();
123 bool success
= false;
125 success
= lldb_event
->BroadcasterIs(broadcaster
.get());
131 void SBEvent::Clear() {
132 LLDB_RECORD_METHOD_NO_ARGS(void, SBEvent
, Clear
);
134 Event
*lldb_event
= get();
139 EventSP
&SBEvent::GetSP() const { return m_event_sp
; }
141 Event
*SBEvent::get() const {
142 // There is a dangerous accessor call GetSharedPtr which can be used, so if
143 // we have anything valid in m_event_sp, we must use that since if it gets
144 // used by a function that puts something in there, then it won't update
147 m_opaque_ptr
= m_event_sp
.get();
152 void SBEvent::reset(EventSP
&event_sp
) {
153 m_event_sp
= event_sp
;
154 m_opaque_ptr
= m_event_sp
.get();
157 void SBEvent::reset(Event
*event_ptr
) {
158 m_opaque_ptr
= event_ptr
;
162 bool SBEvent::IsValid() const {
163 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent
, IsValid
);
164 return this->operator bool();
166 SBEvent::operator bool() const {
167 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent
, operator bool);
169 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
170 // See comments in SBEvent::get()....
171 return SBEvent::get() != nullptr;
174 const char *SBEvent::GetCStringFromEvent(const SBEvent
&event
) {
175 LLDB_RECORD_STATIC_METHOD(const char *, SBEvent
, GetCStringFromEvent
,
176 (const lldb::SBEvent
&), event
);
178 return static_cast<const char *>(
179 EventDataBytes::GetBytesFromEvent(event
.get()));
182 bool SBEvent::GetDescription(SBStream
&description
) {
183 LLDB_RECORD_METHOD(bool, SBEvent
, GetDescription
, (lldb::SBStream
&),
186 Stream
&strm
= description
.ref();
189 m_opaque_ptr
->Dump(&strm
);
191 strm
.PutCString("No value");
196 bool SBEvent::GetDescription(SBStream
&description
) const {
197 LLDB_RECORD_METHOD_CONST(bool, SBEvent
, GetDescription
, (lldb::SBStream
&),
200 Stream
&strm
= description
.ref();
203 m_opaque_ptr
->Dump(&strm
);
205 strm
.PutCString("No value");
210 namespace lldb_private
{
214 void RegisterMethods
<SBEvent
>(Registry
&R
) {
215 LLDB_REGISTER_CONSTRUCTOR(SBEvent
, ());
216 LLDB_REGISTER_CONSTRUCTOR(SBEvent
, (uint32_t, const char *, uint32_t));
217 LLDB_REGISTER_CONSTRUCTOR(SBEvent
, (lldb::EventSP
&));
218 LLDB_REGISTER_CONSTRUCTOR(SBEvent
, (lldb_private::Event
*));
219 LLDB_REGISTER_CONSTRUCTOR(SBEvent
, (const lldb::SBEvent
&));
220 LLDB_REGISTER_METHOD(const lldb::SBEvent
&,
221 SBEvent
, operator=,(const lldb::SBEvent
&));
222 LLDB_REGISTER_METHOD(const char *, SBEvent
, GetDataFlavor
, ());
223 LLDB_REGISTER_METHOD_CONST(uint32_t, SBEvent
, GetType
, ());
224 LLDB_REGISTER_METHOD_CONST(lldb::SBBroadcaster
, SBEvent
, GetBroadcaster
,
226 LLDB_REGISTER_METHOD_CONST(const char *, SBEvent
, GetBroadcasterClass
, ());
227 LLDB_REGISTER_METHOD(bool, SBEvent
, BroadcasterMatchesPtr
,
228 (const lldb::SBBroadcaster
*));
229 LLDB_REGISTER_METHOD(bool, SBEvent
, BroadcasterMatchesRef
,
230 (const lldb::SBBroadcaster
&));
231 LLDB_REGISTER_METHOD(void, SBEvent
, Clear
, ());
232 LLDB_REGISTER_METHOD_CONST(bool, SBEvent
, IsValid
, ());
233 LLDB_REGISTER_METHOD_CONST(bool, SBEvent
, operator bool, ());
234 LLDB_REGISTER_STATIC_METHOD(const char *, SBEvent
, GetCStringFromEvent
,
235 (const lldb::SBEvent
&));
236 LLDB_REGISTER_METHOD(bool, SBEvent
, GetDescription
, (lldb::SBStream
&));
237 LLDB_REGISTER_METHOD_CONST(bool, SBEvent
, GetDescription
,