1 //===-- SBEvent.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/SBEvent.h"
10 #include "lldb/API/SBBroadcaster.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
14 #include "lldb/Breakpoint/Breakpoint.h"
15 #include "lldb/Interpreter/CommandInterpreter.h"
16 #include "lldb/Target/Process.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/Utility/Event.h"
19 #include "lldb/Utility/Stream.h"
22 using namespace lldb_private
;
24 SBEvent::SBEvent() { LLDB_INSTRUMENT_VA(this); }
26 SBEvent::SBEvent(uint32_t event_type
, const char *cstr
, uint32_t cstr_len
)
27 : m_event_sp(new Event(event_type
, new EventDataBytes(cstr
, cstr_len
))),
28 m_opaque_ptr(m_event_sp
.get()) {
29 LLDB_INSTRUMENT_VA(this, event_type
, cstr
, cstr_len
);
32 SBEvent::SBEvent(EventSP
&event_sp
)
33 : m_event_sp(event_sp
), m_opaque_ptr(event_sp
.get()) {
34 LLDB_INSTRUMENT_VA(this, event_sp
);
37 SBEvent::SBEvent(Event
*event_ptr
) : m_opaque_ptr(event_ptr
) {
38 LLDB_INSTRUMENT_VA(this, event_ptr
);
41 SBEvent::SBEvent(const SBEvent
&rhs
)
42 : m_event_sp(rhs
.m_event_sp
), m_opaque_ptr(rhs
.m_opaque_ptr
) {
43 LLDB_INSTRUMENT_VA(this, rhs
);
46 const SBEvent
&SBEvent::operator=(const SBEvent
&rhs
) {
47 LLDB_INSTRUMENT_VA(this, rhs
);
50 m_event_sp
= rhs
.m_event_sp
;
51 m_opaque_ptr
= rhs
.m_opaque_ptr
;
56 SBEvent::~SBEvent() = default;
58 const char *SBEvent::GetDataFlavor() {
59 LLDB_INSTRUMENT_VA(this);
61 Event
*lldb_event
= get();
63 EventData
*event_data
= lldb_event
->GetData();
65 return ConstString(lldb_event
->GetData()->GetFlavor()).GetCString();
70 uint32_t SBEvent::GetType() const {
71 LLDB_INSTRUMENT_VA(this);
73 const Event
*lldb_event
= get();
74 uint32_t event_type
= 0;
76 event_type
= lldb_event
->GetType();
82 SBBroadcaster
SBEvent::GetBroadcaster() const {
83 LLDB_INSTRUMENT_VA(this);
85 SBBroadcaster broadcaster
;
86 const Event
*lldb_event
= get();
88 broadcaster
.reset(lldb_event
->GetBroadcaster(), false);
92 const char *SBEvent::GetBroadcasterClass() const {
93 LLDB_INSTRUMENT_VA(this);
95 const Event
*lldb_event
= get();
97 return lldb_event
->GetBroadcaster()->GetBroadcasterClass().AsCString();
99 return "unknown class";
102 bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster
*broadcaster
) {
103 LLDB_INSTRUMENT_VA(this, broadcaster
);
106 return BroadcasterMatchesRef(*broadcaster
);
110 bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster
&broadcaster
) {
111 LLDB_INSTRUMENT_VA(this, broadcaster
);
113 Event
*lldb_event
= get();
114 bool success
= false;
116 success
= lldb_event
->BroadcasterIs(broadcaster
.get());
122 void SBEvent::Clear() {
123 LLDB_INSTRUMENT_VA(this);
125 Event
*lldb_event
= get();
130 EventSP
&SBEvent::GetSP() const { return m_event_sp
; }
132 Event
*SBEvent::get() const {
133 // There is a dangerous accessor call GetSharedPtr which can be used, so if
134 // we have anything valid in m_event_sp, we must use that since if it gets
135 // used by a function that puts something in there, then it won't update
138 m_opaque_ptr
= m_event_sp
.get();
143 void SBEvent::reset(EventSP
&event_sp
) {
144 m_event_sp
= event_sp
;
145 m_opaque_ptr
= m_event_sp
.get();
148 void SBEvent::reset(Event
*event_ptr
) {
149 m_opaque_ptr
= event_ptr
;
153 bool SBEvent::IsValid() const {
154 LLDB_INSTRUMENT_VA(this);
155 return this->operator bool();
157 SBEvent::operator bool() const {
158 LLDB_INSTRUMENT_VA(this);
160 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
161 // See comments in SBEvent::get()....
162 return SBEvent::get() != nullptr;
165 const char *SBEvent::GetCStringFromEvent(const SBEvent
&event
) {
166 LLDB_INSTRUMENT_VA(event
);
168 return ConstString(static_cast<const char *>(
169 EventDataBytes::GetBytesFromEvent(event
.get())))
173 bool SBEvent::GetDescription(SBStream
&description
) {
174 LLDB_INSTRUMENT_VA(this, description
);
176 Stream
&strm
= description
.ref();
179 m_opaque_ptr
->Dump(&strm
);
181 strm
.PutCString("No value");
186 bool SBEvent::GetDescription(SBStream
&description
) const {
187 LLDB_INSTRUMENT_VA(this, description
);
189 Stream
&strm
= description
.ref();
192 m_opaque_ptr
->Dump(&strm
);
194 strm
.PutCString("No value");