1 //===-- SBEvent.cpp -------------------------------------------------------===//
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/SBEvent.h"
10 #include "lldb/API/SBBroadcaster.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Utility/Instrumentation.h"
13 
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"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBEvent::SBEvent() { LLDB_INSTRUMENT_VA(this); }
25 
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);
30 }
31 
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);
35 }
36 
37 SBEvent::SBEvent(Event *event_ptr) : m_opaque_ptr(event_ptr) {
38   LLDB_INSTRUMENT_VA(this, event_ptr);
39 }
40 
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);
44 }
45 
46 const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
47   LLDB_INSTRUMENT_VA(this, rhs);
48 
49   if (this != &rhs) {
50     m_event_sp = rhs.m_event_sp;
51     m_opaque_ptr = rhs.m_opaque_ptr;
52   }
53   return *this;
54 }
55 
56 SBEvent::~SBEvent() = default;
57 
58 const char *SBEvent::GetDataFlavor() {
59   LLDB_INSTRUMENT_VA(this);
60 
61   Event *lldb_event = get();
62   if (lldb_event) {
63     EventData *event_data = lldb_event->GetData();
64     if (event_data)
65       return ConstString(lldb_event->GetData()->GetFlavor()).GetCString();
66   }
67   return nullptr;
68 }
69 
70 uint32_t SBEvent::GetType() const {
71   LLDB_INSTRUMENT_VA(this);
72 
73   const Event *lldb_event = get();
74   uint32_t event_type = 0;
75   if (lldb_event)
76     event_type = lldb_event->GetType();
77 
78 
79   return event_type;
80 }
81 
82 SBBroadcaster SBEvent::GetBroadcaster() const {
83   LLDB_INSTRUMENT_VA(this);
84 
85   SBBroadcaster broadcaster;
86   const Event *lldb_event = get();
87   if (lldb_event)
88     broadcaster.reset(lldb_event->GetBroadcaster(), false);
89   return broadcaster;
90 }
91 
92 const char *SBEvent::GetBroadcasterClass() const {
93   LLDB_INSTRUMENT_VA(this);
94 
95   const Event *lldb_event = get();
96   if (lldb_event)
97     return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
98   else
99     return "unknown class";
100 }
101 
102 bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {
103   LLDB_INSTRUMENT_VA(this, broadcaster);
104 
105   if (broadcaster)
106     return BroadcasterMatchesRef(*broadcaster);
107   return false;
108 }
109 
110 bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {
111   LLDB_INSTRUMENT_VA(this, broadcaster);
112 
113   Event *lldb_event = get();
114   bool success = false;
115   if (lldb_event)
116     success = lldb_event->BroadcasterIs(broadcaster.get());
117 
118 
119   return success;
120 }
121 
122 void SBEvent::Clear() {
123   LLDB_INSTRUMENT_VA(this);
124 
125   Event *lldb_event = get();
126   if (lldb_event)
127     lldb_event->Clear();
128 }
129 
130 EventSP &SBEvent::GetSP() const { return m_event_sp; }
131 
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
136   // m_opaque_ptr...
137   if (m_event_sp)
138     m_opaque_ptr = m_event_sp.get();
139 
140   return m_opaque_ptr;
141 }
142 
143 void SBEvent::reset(EventSP &event_sp) {
144   m_event_sp = event_sp;
145   m_opaque_ptr = m_event_sp.get();
146 }
147 
148 void SBEvent::reset(Event *event_ptr) {
149   m_opaque_ptr = event_ptr;
150   m_event_sp.reset();
151 }
152 
153 bool SBEvent::IsValid() const {
154   LLDB_INSTRUMENT_VA(this);
155   return this->operator bool();
156 }
157 SBEvent::operator bool() const {
158   LLDB_INSTRUMENT_VA(this);
159 
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;
163 }
164 
165 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
166   LLDB_INSTRUMENT_VA(event);
167 
168   return ConstString(static_cast<const char *>(
169                          EventDataBytes::GetBytesFromEvent(event.get())))
170       .GetCString();
171 }
172 
173 bool SBEvent::GetDescription(SBStream &description) {
174   LLDB_INSTRUMENT_VA(this, description);
175 
176   Stream &strm = description.ref();
177 
178   if (get()) {
179     m_opaque_ptr->Dump(&strm);
180   } else
181     strm.PutCString("No value");
182 
183   return true;
184 }
185 
186 bool SBEvent::GetDescription(SBStream &description) const {
187   LLDB_INSTRUMENT_VA(this, description);
188 
189   Stream &strm = description.ref();
190 
191   if (get()) {
192     m_opaque_ptr->Dump(&strm);
193   } else
194     strm.PutCString("No value");
195 
196   return true;
197 }
198