180814287SRaphael Isemann //===-- Event.cpp ---------------------------------------------------------===//
2181b823bSPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6181b823bSPavel Labath //
7181b823bSPavel Labath //===----------------------------------------------------------------------===//
8181b823bSPavel Labath 
9181b823bSPavel Labath #include "lldb/Utility/Event.h"
10181b823bSPavel Labath 
11181b823bSPavel Labath #include "lldb/Utility/Broadcaster.h"
12181b823bSPavel Labath #include "lldb/Utility/DataExtractor.h"
13181b823bSPavel Labath #include "lldb/Utility/Endian.h"
14181b823bSPavel Labath #include "lldb/Utility/Stream.h"
15181b823bSPavel Labath #include "lldb/Utility/StreamString.h"
16181b823bSPavel Labath #include "lldb/lldb-enumerations.h"
17181b823bSPavel Labath 
18181b823bSPavel Labath #include <algorithm>
19181b823bSPavel Labath 
2076e47d48SRaphael Isemann #include <cctype>
21181b823bSPavel Labath 
22181b823bSPavel Labath using namespace lldb;
23181b823bSPavel Labath using namespace lldb_private;
24181b823bSPavel Labath 
25181b823bSPavel Labath #pragma mark -
26181b823bSPavel Labath #pragma mark Event
27181b823bSPavel Labath 
28181b823bSPavel Labath // Event functions
29181b823bSPavel Labath 
Event(Broadcaster * broadcaster,uint32_t event_type,EventData * data)30181b823bSPavel Labath Event::Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data)
31181b823bSPavel Labath     : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
32181b823bSPavel Labath       m_data_sp(data) {}
33181b823bSPavel Labath 
Event(Broadcaster * broadcaster,uint32_t event_type,const EventDataSP & event_data_sp)34181b823bSPavel Labath Event::Event(Broadcaster *broadcaster, uint32_t event_type,
35181b823bSPavel Labath              const EventDataSP &event_data_sp)
36181b823bSPavel Labath     : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
37181b823bSPavel Labath       m_data_sp(event_data_sp) {}
38181b823bSPavel Labath 
Event(uint32_t event_type,EventData * data)39181b823bSPavel Labath Event::Event(uint32_t event_type, EventData *data)
40181b823bSPavel Labath     : m_broadcaster_wp(), m_type(event_type), m_data_sp(data) {}
41181b823bSPavel Labath 
Event(uint32_t event_type,const EventDataSP & event_data_sp)42181b823bSPavel Labath Event::Event(uint32_t event_type, const EventDataSP &event_data_sp)
43181b823bSPavel Labath     : m_broadcaster_wp(), m_type(event_type), m_data_sp(event_data_sp) {}
44181b823bSPavel Labath 
45181b823bSPavel Labath Event::~Event() = default;
46181b823bSPavel Labath 
Dump(Stream * s) const47181b823bSPavel Labath void Event::Dump(Stream *s) const {
48181b823bSPavel Labath   Broadcaster *broadcaster;
49181b823bSPavel Labath   Broadcaster::BroadcasterImplSP broadcaster_impl_sp(m_broadcaster_wp.lock());
50181b823bSPavel Labath   if (broadcaster_impl_sp)
51181b823bSPavel Labath     broadcaster = broadcaster_impl_sp->GetBroadcaster();
52181b823bSPavel Labath   else
53181b823bSPavel Labath     broadcaster = nullptr;
54181b823bSPavel Labath 
55181b823bSPavel Labath   if (broadcaster) {
56181b823bSPavel Labath     StreamString event_name;
57181b823bSPavel Labath     if (broadcaster->GetEventNames(event_name, m_type, false))
58181b823bSPavel Labath       s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ",
59181b823bSPavel Labath                 static_cast<const void *>(this),
60181b823bSPavel Labath                 static_cast<void *>(broadcaster),
61181b823bSPavel Labath                 broadcaster->GetBroadcasterName().GetCString(), m_type,
62181b823bSPavel Labath                 event_name.GetData());
63181b823bSPavel Labath     else
64181b823bSPavel Labath       s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
65181b823bSPavel Labath                 static_cast<const void *>(this),
66181b823bSPavel Labath                 static_cast<void *>(broadcaster),
67181b823bSPavel Labath                 broadcaster->GetBroadcasterName().GetCString(), m_type);
68181b823bSPavel Labath   } else
69181b823bSPavel Labath     s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
70181b823bSPavel Labath               static_cast<const void *>(this), m_type);
71181b823bSPavel Labath 
72181b823bSPavel Labath   if (m_data_sp) {
73181b823bSPavel Labath     s->PutChar('{');
74181b823bSPavel Labath     m_data_sp->Dump(s);
75181b823bSPavel Labath     s->PutChar('}');
76181b823bSPavel Labath   } else
77181b823bSPavel Labath     s->Printf("<NULL>");
78181b823bSPavel Labath }
79181b823bSPavel Labath 
DoOnRemoval()80181b823bSPavel Labath void Event::DoOnRemoval() {
81181b823bSPavel Labath   if (m_data_sp)
82181b823bSPavel Labath     m_data_sp->DoOnRemoval(this);
83181b823bSPavel Labath }
84181b823bSPavel Labath 
85181b823bSPavel Labath #pragma mark -
86181b823bSPavel Labath #pragma mark EventData
87181b823bSPavel Labath 
88181b823bSPavel Labath // EventData functions
89181b823bSPavel Labath 
90181b823bSPavel Labath EventData::EventData() = default;
91181b823bSPavel Labath 
92181b823bSPavel Labath EventData::~EventData() = default;
93181b823bSPavel Labath 
Dump(Stream * s) const94181b823bSPavel Labath void EventData::Dump(Stream *s) const { s->PutCString("Generic Event Data"); }
95181b823bSPavel Labath 
96181b823bSPavel Labath #pragma mark -
97181b823bSPavel Labath #pragma mark EventDataBytes
98181b823bSPavel Labath 
99181b823bSPavel Labath // EventDataBytes functions
100181b823bSPavel Labath 
EventDataBytes()101181b823bSPavel Labath EventDataBytes::EventDataBytes() : m_bytes() {}
102181b823bSPavel Labath 
EventDataBytes(const char * cstr)103181b823bSPavel Labath EventDataBytes::EventDataBytes(const char *cstr) : m_bytes() {
104181b823bSPavel Labath   SetBytesFromCString(cstr);
105181b823bSPavel Labath }
106181b823bSPavel Labath 
EventDataBytes(llvm::StringRef str)107181b823bSPavel Labath EventDataBytes::EventDataBytes(llvm::StringRef str) : m_bytes() {
108181b823bSPavel Labath   SetBytes(str.data(), str.size());
109181b823bSPavel Labath }
110181b823bSPavel Labath 
EventDataBytes(const void * src,size_t src_len)111181b823bSPavel Labath EventDataBytes::EventDataBytes(const void *src, size_t src_len) : m_bytes() {
112181b823bSPavel Labath   SetBytes(src, src_len);
113181b823bSPavel Labath }
114181b823bSPavel Labath 
115181b823bSPavel Labath EventDataBytes::~EventDataBytes() = default;
116181b823bSPavel Labath 
GetFlavorString()1170e4c4821SAdrian Prantl ConstString EventDataBytes::GetFlavorString() {
118181b823bSPavel Labath   static ConstString g_flavor("EventDataBytes");
119181b823bSPavel Labath   return g_flavor;
120181b823bSPavel Labath }
121181b823bSPavel Labath 
GetFlavor() const1220e4c4821SAdrian Prantl ConstString EventDataBytes::GetFlavor() const {
123181b823bSPavel Labath   return EventDataBytes::GetFlavorString();
124181b823bSPavel Labath }
125181b823bSPavel Labath 
Dump(Stream * s) const126181b823bSPavel Labath void EventDataBytes::Dump(Stream *s) const {
127181b823bSPavel Labath   size_t num_printable_chars =
128f5eaa2afSRaphael Isemann       std::count_if(m_bytes.begin(), m_bytes.end(), llvm::isPrint);
129181b823bSPavel Labath   if (num_printable_chars == m_bytes.size())
130181b823bSPavel Labath     s->Format("\"{0}\"", m_bytes);
131181b823bSPavel Labath   else
132181b823bSPavel Labath     s->Format("{0:$[ ]@[x-2]}", llvm::make_range(
133181b823bSPavel Labath                          reinterpret_cast<const uint8_t *>(m_bytes.data()),
134181b823bSPavel Labath                          reinterpret_cast<const uint8_t *>(m_bytes.data() +
135181b823bSPavel Labath                                                            m_bytes.size())));
136181b823bSPavel Labath }
137181b823bSPavel Labath 
GetBytes() const138181b823bSPavel Labath const void *EventDataBytes::GetBytes() const {
139181b823bSPavel Labath   return (m_bytes.empty() ? nullptr : m_bytes.data());
140181b823bSPavel Labath }
141181b823bSPavel Labath 
GetByteSize() const142181b823bSPavel Labath size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }
143181b823bSPavel Labath 
SetBytes(const void * src,size_t src_len)144181b823bSPavel Labath void EventDataBytes::SetBytes(const void *src, size_t src_len) {
145181b823bSPavel Labath   if (src != nullptr && src_len > 0)
14624374aefSJonas Devlieghere     m_bytes.assign(static_cast<const char *>(src), src_len);
147181b823bSPavel Labath   else
148181b823bSPavel Labath     m_bytes.clear();
149181b823bSPavel Labath }
150181b823bSPavel Labath 
SetBytesFromCString(const char * cstr)151181b823bSPavel Labath void EventDataBytes::SetBytesFromCString(const char *cstr) {
152181b823bSPavel Labath   if (cstr != nullptr && cstr[0])
153181b823bSPavel Labath     m_bytes.assign(cstr);
154181b823bSPavel Labath   else
155181b823bSPavel Labath     m_bytes.clear();
156181b823bSPavel Labath }
157181b823bSPavel Labath 
GetBytesFromEvent(const Event * event_ptr)158181b823bSPavel Labath const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {
159181b823bSPavel Labath   const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
160181b823bSPavel Labath   if (e != nullptr)
161181b823bSPavel Labath     return e->GetBytes();
162181b823bSPavel Labath   return nullptr;
163181b823bSPavel Labath }
164181b823bSPavel Labath 
GetByteSizeFromEvent(const Event * event_ptr)165181b823bSPavel Labath size_t EventDataBytes::GetByteSizeFromEvent(const Event *event_ptr) {
166181b823bSPavel Labath   const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
167181b823bSPavel Labath   if (e != nullptr)
168181b823bSPavel Labath     return e->GetByteSize();
169181b823bSPavel Labath   return 0;
170181b823bSPavel Labath }
171181b823bSPavel Labath 
172181b823bSPavel Labath const EventDataBytes *
GetEventDataFromEvent(const Event * event_ptr)173181b823bSPavel Labath EventDataBytes::GetEventDataFromEvent(const Event *event_ptr) {
174181b823bSPavel Labath   if (event_ptr != nullptr) {
175181b823bSPavel Labath     const EventData *event_data = event_ptr->GetData();
176181b823bSPavel Labath     if (event_data &&
177181b823bSPavel Labath         event_data->GetFlavor() == EventDataBytes::GetFlavorString())
178181b823bSPavel Labath       return static_cast<const EventDataBytes *>(event_data);
179181b823bSPavel Labath   }
180181b823bSPavel Labath   return nullptr;
181181b823bSPavel Labath }
182181b823bSPavel Labath 
SwapBytes(std::string & new_bytes)183181b823bSPavel Labath void EventDataBytes::SwapBytes(std::string &new_bytes) {
184181b823bSPavel Labath   m_bytes.swap(new_bytes);
185181b823bSPavel Labath }
186181b823bSPavel Labath 
187181b823bSPavel Labath #pragma mark -
188181b823bSPavel Labath #pragma mark EventStructuredData
189181b823bSPavel Labath 
190181b823bSPavel Labath // EventDataStructuredData definitions
191181b823bSPavel Labath 
EventDataStructuredData()192181b823bSPavel Labath EventDataStructuredData::EventDataStructuredData()
193181b823bSPavel Labath     : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
194181b823bSPavel Labath 
EventDataStructuredData(const ProcessSP & process_sp,const StructuredData::ObjectSP & object_sp,const lldb::StructuredDataPluginSP & plugin_sp)195181b823bSPavel Labath EventDataStructuredData::EventDataStructuredData(
196181b823bSPavel Labath     const ProcessSP &process_sp, const StructuredData::ObjectSP &object_sp,
197181b823bSPavel Labath     const lldb::StructuredDataPluginSP &plugin_sp)
198181b823bSPavel Labath     : EventData(), m_process_sp(process_sp), m_object_sp(object_sp),
199181b823bSPavel Labath       m_plugin_sp(plugin_sp) {}
200181b823bSPavel Labath 
201*fd2433e1SJonas Devlieghere EventDataStructuredData::~EventDataStructuredData() = default;
202181b823bSPavel Labath 
203181b823bSPavel Labath // EventDataStructuredData member functions
204181b823bSPavel Labath 
GetFlavor() const2050e4c4821SAdrian Prantl ConstString EventDataStructuredData::GetFlavor() const {
206181b823bSPavel Labath   return EventDataStructuredData::GetFlavorString();
207181b823bSPavel Labath }
208181b823bSPavel Labath 
Dump(Stream * s) const209181b823bSPavel Labath void EventDataStructuredData::Dump(Stream *s) const {
210181b823bSPavel Labath   if (!s)
211181b823bSPavel Labath     return;
212181b823bSPavel Labath 
213181b823bSPavel Labath   if (m_object_sp)
214181b823bSPavel Labath     m_object_sp->Dump(*s);
215181b823bSPavel Labath }
216181b823bSPavel Labath 
GetProcess() const217181b823bSPavel Labath const ProcessSP &EventDataStructuredData::GetProcess() const {
218181b823bSPavel Labath   return m_process_sp;
219181b823bSPavel Labath }
220181b823bSPavel Labath 
GetObject() const221181b823bSPavel Labath const StructuredData::ObjectSP &EventDataStructuredData::GetObject() const {
222181b823bSPavel Labath   return m_object_sp;
223181b823bSPavel Labath }
224181b823bSPavel Labath 
225181b823bSPavel Labath const lldb::StructuredDataPluginSP &
GetStructuredDataPlugin() const226181b823bSPavel Labath EventDataStructuredData::GetStructuredDataPlugin() const {
227181b823bSPavel Labath   return m_plugin_sp;
228181b823bSPavel Labath }
229181b823bSPavel Labath 
SetProcess(const ProcessSP & process_sp)230181b823bSPavel Labath void EventDataStructuredData::SetProcess(const ProcessSP &process_sp) {
231181b823bSPavel Labath   m_process_sp = process_sp;
232181b823bSPavel Labath }
233181b823bSPavel Labath 
SetObject(const StructuredData::ObjectSP & object_sp)234181b823bSPavel Labath void EventDataStructuredData::SetObject(
235181b823bSPavel Labath     const StructuredData::ObjectSP &object_sp) {
236181b823bSPavel Labath   m_object_sp = object_sp;
237181b823bSPavel Labath }
238181b823bSPavel Labath 
SetStructuredDataPlugin(const lldb::StructuredDataPluginSP & plugin_sp)239181b823bSPavel Labath void EventDataStructuredData::SetStructuredDataPlugin(
240181b823bSPavel Labath     const lldb::StructuredDataPluginSP &plugin_sp) {
241181b823bSPavel Labath   m_plugin_sp = plugin_sp;
242181b823bSPavel Labath }
243181b823bSPavel Labath 
244181b823bSPavel Labath // EventDataStructuredData static functions
245181b823bSPavel Labath 
246181b823bSPavel Labath const EventDataStructuredData *
GetEventDataFromEvent(const Event * event_ptr)247181b823bSPavel Labath EventDataStructuredData::GetEventDataFromEvent(const Event *event_ptr) {
248181b823bSPavel Labath   if (event_ptr == nullptr)
249181b823bSPavel Labath     return nullptr;
250181b823bSPavel Labath 
251181b823bSPavel Labath   const EventData *event_data = event_ptr->GetData();
252181b823bSPavel Labath   if (!event_data ||
253181b823bSPavel Labath       event_data->GetFlavor() != EventDataStructuredData::GetFlavorString())
254181b823bSPavel Labath     return nullptr;
255181b823bSPavel Labath 
256181b823bSPavel Labath   return static_cast<const EventDataStructuredData *>(event_data);
257181b823bSPavel Labath }
258181b823bSPavel Labath 
GetProcessFromEvent(const Event * event_ptr)259181b823bSPavel Labath ProcessSP EventDataStructuredData::GetProcessFromEvent(const Event *event_ptr) {
260181b823bSPavel Labath   auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
261181b823bSPavel Labath   if (event_data)
262181b823bSPavel Labath     return event_data->GetProcess();
263181b823bSPavel Labath   else
264181b823bSPavel Labath     return ProcessSP();
265181b823bSPavel Labath }
266181b823bSPavel Labath 
267181b823bSPavel Labath StructuredData::ObjectSP
GetObjectFromEvent(const Event * event_ptr)268181b823bSPavel Labath EventDataStructuredData::GetObjectFromEvent(const Event *event_ptr) {
269181b823bSPavel Labath   auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
270181b823bSPavel Labath   if (event_data)
271181b823bSPavel Labath     return event_data->GetObject();
272181b823bSPavel Labath   else
273181b823bSPavel Labath     return StructuredData::ObjectSP();
274181b823bSPavel Labath }
275181b823bSPavel Labath 
276181b823bSPavel Labath lldb::StructuredDataPluginSP
GetPluginFromEvent(const Event * event_ptr)277181b823bSPavel Labath EventDataStructuredData::GetPluginFromEvent(const Event *event_ptr) {
278181b823bSPavel Labath   auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
279181b823bSPavel Labath   if (event_data)
280181b823bSPavel Labath     return event_data->GetStructuredDataPlugin();
281181b823bSPavel Labath   else
282181b823bSPavel Labath     return StructuredDataPluginSP();
283181b823bSPavel Labath }
284181b823bSPavel Labath 
GetFlavorString()2850e4c4821SAdrian Prantl ConstString EventDataStructuredData::GetFlavorString() {
286181b823bSPavel Labath   static ConstString s_flavor("EventDataStructuredData");
287181b823bSPavel Labath   return s_flavor;
288181b823bSPavel Labath }
289