1 //===-- Event.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/Utility/Event.h"
10
11 #include "lldb/Utility/Broadcaster.h"
12 #include "lldb/Utility/DataExtractor.h"
13 #include "lldb/Utility/Endian.h"
14 #include "lldb/Utility/Stream.h"
15 #include "lldb/Utility/StreamString.h"
16 #include "lldb/lldb-enumerations.h"
17
18 #include <algorithm>
19
20 #include <cctype>
21
22 using namespace lldb;
23 using namespace lldb_private;
24
25 #pragma mark -
26 #pragma mark Event
27
28 // Event functions
29
Event(Broadcaster * broadcaster,uint32_t event_type,EventData * data)30 Event::Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data)
31 : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
32 m_data_sp(data) {}
33
Event(Broadcaster * broadcaster,uint32_t event_type,const EventDataSP & event_data_sp)34 Event::Event(Broadcaster *broadcaster, uint32_t event_type,
35 const EventDataSP &event_data_sp)
36 : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
37 m_data_sp(event_data_sp) {}
38
Event(uint32_t event_type,EventData * data)39 Event::Event(uint32_t event_type, EventData *data)
40 : m_broadcaster_wp(), m_type(event_type), m_data_sp(data) {}
41
Event(uint32_t event_type,const EventDataSP & event_data_sp)42 Event::Event(uint32_t event_type, const EventDataSP &event_data_sp)
43 : m_broadcaster_wp(), m_type(event_type), m_data_sp(event_data_sp) {}
44
45 Event::~Event() = default;
46
Dump(Stream * s) const47 void Event::Dump(Stream *s) const {
48 Broadcaster *broadcaster;
49 Broadcaster::BroadcasterImplSP broadcaster_impl_sp(m_broadcaster_wp.lock());
50 if (broadcaster_impl_sp)
51 broadcaster = broadcaster_impl_sp->GetBroadcaster();
52 else
53 broadcaster = nullptr;
54
55 if (broadcaster) {
56 StreamString event_name;
57 if (broadcaster->GetEventNames(event_name, m_type, false))
58 s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ",
59 static_cast<const void *>(this),
60 static_cast<void *>(broadcaster),
61 broadcaster->GetBroadcasterName().GetCString(), m_type,
62 event_name.GetData());
63 else
64 s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
65 static_cast<const void *>(this),
66 static_cast<void *>(broadcaster),
67 broadcaster->GetBroadcasterName().GetCString(), m_type);
68 } else
69 s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
70 static_cast<const void *>(this), m_type);
71
72 if (m_data_sp) {
73 s->PutChar('{');
74 m_data_sp->Dump(s);
75 s->PutChar('}');
76 } else
77 s->Printf("<NULL>");
78 }
79
DoOnRemoval()80 void Event::DoOnRemoval() {
81 if (m_data_sp)
82 m_data_sp->DoOnRemoval(this);
83 }
84
85 #pragma mark -
86 #pragma mark EventData
87
88 // EventData functions
89
90 EventData::EventData() = default;
91
92 EventData::~EventData() = default;
93
Dump(Stream * s) const94 void EventData::Dump(Stream *s) const { s->PutCString("Generic Event Data"); }
95
96 #pragma mark -
97 #pragma mark EventDataBytes
98
99 // EventDataBytes functions
100
EventDataBytes()101 EventDataBytes::EventDataBytes() : m_bytes() {}
102
EventDataBytes(const char * cstr)103 EventDataBytes::EventDataBytes(const char *cstr) : m_bytes() {
104 SetBytesFromCString(cstr);
105 }
106
EventDataBytes(llvm::StringRef str)107 EventDataBytes::EventDataBytes(llvm::StringRef str) : m_bytes() {
108 SetBytes(str.data(), str.size());
109 }
110
EventDataBytes(const void * src,size_t src_len)111 EventDataBytes::EventDataBytes(const void *src, size_t src_len) : m_bytes() {
112 SetBytes(src, src_len);
113 }
114
115 EventDataBytes::~EventDataBytes() = default;
116
GetFlavorString()117 ConstString EventDataBytes::GetFlavorString() {
118 static ConstString g_flavor("EventDataBytes");
119 return g_flavor;
120 }
121
GetFlavor() const122 ConstString EventDataBytes::GetFlavor() const {
123 return EventDataBytes::GetFlavorString();
124 }
125
Dump(Stream * s) const126 void EventDataBytes::Dump(Stream *s) const {
127 if (llvm::all_of(m_bytes, llvm::isPrint))
128 s->Format("\"{0}\"", m_bytes);
129 else
130 s->Format("{0:$[ ]@[x-2]}", llvm::make_range(
131 reinterpret_cast<const uint8_t *>(m_bytes.data()),
132 reinterpret_cast<const uint8_t *>(m_bytes.data() +
133 m_bytes.size())));
134 }
135
GetBytes() const136 const void *EventDataBytes::GetBytes() const {
137 return (m_bytes.empty() ? nullptr : m_bytes.data());
138 }
139
GetByteSize() const140 size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }
141
SetBytes(const void * src,size_t src_len)142 void EventDataBytes::SetBytes(const void *src, size_t src_len) {
143 if (src != nullptr && src_len > 0)
144 m_bytes.assign(static_cast<const char *>(src), src_len);
145 else
146 m_bytes.clear();
147 }
148
SetBytesFromCString(const char * cstr)149 void EventDataBytes::SetBytesFromCString(const char *cstr) {
150 if (cstr != nullptr && cstr[0])
151 m_bytes.assign(cstr);
152 else
153 m_bytes.clear();
154 }
155
GetBytesFromEvent(const Event * event_ptr)156 const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {
157 const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
158 if (e != nullptr)
159 return e->GetBytes();
160 return nullptr;
161 }
162
GetByteSizeFromEvent(const Event * event_ptr)163 size_t EventDataBytes::GetByteSizeFromEvent(const Event *event_ptr) {
164 const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
165 if (e != nullptr)
166 return e->GetByteSize();
167 return 0;
168 }
169
170 const EventDataBytes *
GetEventDataFromEvent(const Event * event_ptr)171 EventDataBytes::GetEventDataFromEvent(const Event *event_ptr) {
172 if (event_ptr != nullptr) {
173 const EventData *event_data = event_ptr->GetData();
174 if (event_data &&
175 event_data->GetFlavor() == EventDataBytes::GetFlavorString())
176 return static_cast<const EventDataBytes *>(event_data);
177 }
178 return nullptr;
179 }
180
SwapBytes(std::string & new_bytes)181 void EventDataBytes::SwapBytes(std::string &new_bytes) {
182 m_bytes.swap(new_bytes);
183 }
184
185 #pragma mark -
186 #pragma mark EventStructuredData
187
188 // EventDataStructuredData definitions
189
EventDataStructuredData()190 EventDataStructuredData::EventDataStructuredData()
191 : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
192
EventDataStructuredData(const ProcessSP & process_sp,const StructuredData::ObjectSP & object_sp,const lldb::StructuredDataPluginSP & plugin_sp)193 EventDataStructuredData::EventDataStructuredData(
194 const ProcessSP &process_sp, const StructuredData::ObjectSP &object_sp,
195 const lldb::StructuredDataPluginSP &plugin_sp)
196 : EventData(), m_process_sp(process_sp), m_object_sp(object_sp),
197 m_plugin_sp(plugin_sp) {}
198
199 EventDataStructuredData::~EventDataStructuredData() = default;
200
201 // EventDataStructuredData member functions
202
GetFlavor() const203 ConstString EventDataStructuredData::GetFlavor() const {
204 return EventDataStructuredData::GetFlavorString();
205 }
206
Dump(Stream * s) const207 void EventDataStructuredData::Dump(Stream *s) const {
208 if (!s)
209 return;
210
211 if (m_object_sp)
212 m_object_sp->Dump(*s);
213 }
214
GetProcess() const215 const ProcessSP &EventDataStructuredData::GetProcess() const {
216 return m_process_sp;
217 }
218
GetObject() const219 const StructuredData::ObjectSP &EventDataStructuredData::GetObject() const {
220 return m_object_sp;
221 }
222
223 const lldb::StructuredDataPluginSP &
GetStructuredDataPlugin() const224 EventDataStructuredData::GetStructuredDataPlugin() const {
225 return m_plugin_sp;
226 }
227
SetProcess(const ProcessSP & process_sp)228 void EventDataStructuredData::SetProcess(const ProcessSP &process_sp) {
229 m_process_sp = process_sp;
230 }
231
SetObject(const StructuredData::ObjectSP & object_sp)232 void EventDataStructuredData::SetObject(
233 const StructuredData::ObjectSP &object_sp) {
234 m_object_sp = object_sp;
235 }
236
SetStructuredDataPlugin(const lldb::StructuredDataPluginSP & plugin_sp)237 void EventDataStructuredData::SetStructuredDataPlugin(
238 const lldb::StructuredDataPluginSP &plugin_sp) {
239 m_plugin_sp = plugin_sp;
240 }
241
242 // EventDataStructuredData static functions
243
244 const EventDataStructuredData *
GetEventDataFromEvent(const Event * event_ptr)245 EventDataStructuredData::GetEventDataFromEvent(const Event *event_ptr) {
246 if (event_ptr == nullptr)
247 return nullptr;
248
249 const EventData *event_data = event_ptr->GetData();
250 if (!event_data ||
251 event_data->GetFlavor() != EventDataStructuredData::GetFlavorString())
252 return nullptr;
253
254 return static_cast<const EventDataStructuredData *>(event_data);
255 }
256
GetProcessFromEvent(const Event * event_ptr)257 ProcessSP EventDataStructuredData::GetProcessFromEvent(const Event *event_ptr) {
258 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
259 if (event_data)
260 return event_data->GetProcess();
261 else
262 return ProcessSP();
263 }
264
265 StructuredData::ObjectSP
GetObjectFromEvent(const Event * event_ptr)266 EventDataStructuredData::GetObjectFromEvent(const Event *event_ptr) {
267 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
268 if (event_data)
269 return event_data->GetObject();
270 else
271 return StructuredData::ObjectSP();
272 }
273
274 lldb::StructuredDataPluginSP
GetPluginFromEvent(const Event * event_ptr)275 EventDataStructuredData::GetPluginFromEvent(const Event *event_ptr) {
276 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
277 if (event_data)
278 return event_data->GetStructuredDataPlugin();
279 else
280 return StructuredDataPluginSP();
281 }
282
GetFlavorString()283 ConstString EventDataStructuredData::GetFlavorString() {
284 static ConstString s_flavor("EventDataStructuredData");
285 return s_flavor;
286 }
287