1 //===-- Event.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/Utility/Event.h"
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"
23 using namespace lldb_private
;
30 Event::Event(Broadcaster
*broadcaster
, uint32_t event_type
, EventData
*data
)
31 : m_broadcaster_wp(broadcaster
->GetBroadcasterImpl()), m_type(event_type
),
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
) {}
39 Event::Event(uint32_t event_type
, EventData
*data
)
40 : m_broadcaster_wp(), m_type(event_type
), m_data_sp(data
) {}
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
) {}
45 Event::~Event() = default;
47 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();
53 broadcaster
= nullptr;
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());
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
);
69 s
->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
70 static_cast<const void *>(this), m_type
);
80 void Event::DoOnRemoval() {
82 m_data_sp
->DoOnRemoval(this);
86 #pragma mark EventData
88 // EventData functions
90 EventData::EventData() = default;
92 EventData::~EventData() = default;
94 void EventData::Dump(Stream
*s
) const { s
->PutCString("Generic Event Data"); }
97 #pragma mark EventDataBytes
99 // EventDataBytes functions
101 EventDataBytes::EventDataBytes() : m_bytes() {}
103 EventDataBytes::EventDataBytes(const char *cstr
) : m_bytes() {
104 SetBytesFromCString(cstr
);
107 EventDataBytes::EventDataBytes(llvm::StringRef str
) : m_bytes() {
108 SetBytes(str
.data(), str
.size());
111 EventDataBytes::EventDataBytes(const void *src
, size_t src_len
) : m_bytes() {
112 SetBytes(src
, src_len
);
115 EventDataBytes::~EventDataBytes() = default;
117 ConstString
EventDataBytes::GetFlavorString() {
118 static ConstString
g_flavor("EventDataBytes");
122 ConstString
EventDataBytes::GetFlavor() const {
123 return EventDataBytes::GetFlavorString();
126 void EventDataBytes::Dump(Stream
*s
) const {
127 size_t num_printable_chars
=
128 std::count_if(m_bytes
.begin(), m_bytes
.end(), llvm::isPrint
);
129 if (num_printable_chars
== m_bytes
.size())
130 s
->Format("\"{0}\"", m_bytes
);
132 s
->Format("{0:$[ ]@[x-2]}", llvm::make_range(
133 reinterpret_cast<const uint8_t *>(m_bytes
.data()),
134 reinterpret_cast<const uint8_t *>(m_bytes
.data() +
138 const void *EventDataBytes::GetBytes() const {
139 return (m_bytes
.empty() ? nullptr : m_bytes
.data());
142 size_t EventDataBytes::GetByteSize() const { return m_bytes
.size(); }
144 void EventDataBytes::SetBytes(const void *src
, size_t src_len
) {
145 if (src
!= nullptr && src_len
> 0)
146 m_bytes
.assign(static_cast<const char *>(src
), src_len
);
151 void EventDataBytes::SetBytesFromCString(const char *cstr
) {
152 if (cstr
!= nullptr && cstr
[0])
153 m_bytes
.assign(cstr
);
158 const void *EventDataBytes::GetBytesFromEvent(const Event
*event_ptr
) {
159 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
161 return e
->GetBytes();
165 size_t EventDataBytes::GetByteSizeFromEvent(const Event
*event_ptr
) {
166 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
168 return e
->GetByteSize();
172 const EventDataBytes
*
173 EventDataBytes::GetEventDataFromEvent(const Event
*event_ptr
) {
174 if (event_ptr
!= nullptr) {
175 const EventData
*event_data
= event_ptr
->GetData();
177 event_data
->GetFlavor() == EventDataBytes::GetFlavorString())
178 return static_cast<const EventDataBytes
*>(event_data
);
183 void EventDataBytes::SwapBytes(std::string
&new_bytes
) {
184 m_bytes
.swap(new_bytes
);
188 #pragma mark EventStructuredData
190 // EventDataStructuredData definitions
192 EventDataStructuredData::EventDataStructuredData()
193 : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
195 EventDataStructuredData::EventDataStructuredData(
196 const ProcessSP
&process_sp
, const StructuredData::ObjectSP
&object_sp
,
197 const lldb::StructuredDataPluginSP
&plugin_sp
)
198 : EventData(), m_process_sp(process_sp
), m_object_sp(object_sp
),
199 m_plugin_sp(plugin_sp
) {}
201 EventDataStructuredData::~EventDataStructuredData() = default;
203 // EventDataStructuredData member functions
205 ConstString
EventDataStructuredData::GetFlavor() const {
206 return EventDataStructuredData::GetFlavorString();
209 void EventDataStructuredData::Dump(Stream
*s
) const {
214 m_object_sp
->Dump(*s
);
217 const ProcessSP
&EventDataStructuredData::GetProcess() const {
221 const StructuredData::ObjectSP
&EventDataStructuredData::GetObject() const {
225 const lldb::StructuredDataPluginSP
&
226 EventDataStructuredData::GetStructuredDataPlugin() const {
230 void EventDataStructuredData::SetProcess(const ProcessSP
&process_sp
) {
231 m_process_sp
= process_sp
;
234 void EventDataStructuredData::SetObject(
235 const StructuredData::ObjectSP
&object_sp
) {
236 m_object_sp
= object_sp
;
239 void EventDataStructuredData::SetStructuredDataPlugin(
240 const lldb::StructuredDataPluginSP
&plugin_sp
) {
241 m_plugin_sp
= plugin_sp
;
244 // EventDataStructuredData static functions
246 const EventDataStructuredData
*
247 EventDataStructuredData::GetEventDataFromEvent(const Event
*event_ptr
) {
248 if (event_ptr
== nullptr)
251 const EventData
*event_data
= event_ptr
->GetData();
253 event_data
->GetFlavor() != EventDataStructuredData::GetFlavorString())
256 return static_cast<const EventDataStructuredData
*>(event_data
);
259 ProcessSP
EventDataStructuredData::GetProcessFromEvent(const Event
*event_ptr
) {
260 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
262 return event_data
->GetProcess();
267 StructuredData::ObjectSP
268 EventDataStructuredData::GetObjectFromEvent(const Event
*event_ptr
) {
269 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
271 return event_data
->GetObject();
273 return StructuredData::ObjectSP();
276 lldb::StructuredDataPluginSP
277 EventDataStructuredData::GetPluginFromEvent(const Event
*event_ptr
) {
278 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
280 return event_data
->GetStructuredDataPlugin();
282 return StructuredDataPluginSP();
285 ConstString
EventDataStructuredData::GetFlavorString() {
286 static ConstString
s_flavor("EventDataStructuredData");