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/Listener.h"
15 #include "lldb/Utility/Stream.h"
16 #include "lldb/Utility/StreamString.h"
17 #include "lldb/lldb-enumerations.h"
19 #include "llvm/ADT/StringExtras.h"
26 using namespace lldb_private
;
33 Event::Event(Broadcaster
*broadcaster
, uint32_t event_type
, EventData
*data
)
34 : m_broadcaster_wp(broadcaster
->GetBroadcasterImpl()), m_type(event_type
),
37 Event::Event(Broadcaster
*broadcaster
, uint32_t event_type
,
38 const EventDataSP
&event_data_sp
)
39 : m_broadcaster_wp(broadcaster
->GetBroadcasterImpl()), m_type(event_type
),
40 m_data_sp(event_data_sp
) {}
42 Event::Event(uint32_t event_type
, EventData
*data
)
43 : m_broadcaster_wp(), m_type(event_type
), m_data_sp(data
) {}
45 Event::Event(uint32_t event_type
, const EventDataSP
&event_data_sp
)
46 : m_broadcaster_wp(), m_type(event_type
), m_data_sp(event_data_sp
) {}
48 Event::~Event() = default;
50 void Event::Dump(Stream
*s
) const {
51 Broadcaster
*broadcaster
;
52 Broadcaster::BroadcasterImplSP
broadcaster_impl_sp(m_broadcaster_wp
.lock());
53 if (broadcaster_impl_sp
)
54 broadcaster
= broadcaster_impl_sp
->GetBroadcaster();
56 broadcaster
= nullptr;
59 StreamString event_name
;
60 if (broadcaster
->GetEventNames(event_name
, m_type
, false))
61 s
->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ",
62 static_cast<const void *>(this),
63 static_cast<void *>(broadcaster
),
64 broadcaster
->GetBroadcasterName().c_str(), m_type
,
65 event_name
.GetData());
67 s
->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
68 static_cast<const void *>(this),
69 static_cast<void *>(broadcaster
),
70 broadcaster
->GetBroadcasterName().c_str(), m_type
);
72 s
->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
73 static_cast<const void *>(this), m_type
);
83 void Event::DoOnRemoval() {
84 std::lock_guard
<std::mutex
> guard(m_listeners_mutex
);
89 m_data_sp
->DoOnRemoval(this);
91 // Now that the event has been handled by the primary event Listener, forward
92 // it to the other Listeners.
94 EventSP me_sp
= shared_from_this();
95 if (m_data_sp
->ForwardEventToPendingListeners(this)) {
96 for (auto listener_sp
: m_pending_listeners
)
97 listener_sp
->AddEvent(me_sp
);
98 m_pending_listeners
.clear();
103 #pragma mark EventData
105 // EventData functions
107 EventData::EventData() = default;
109 EventData::~EventData() = default;
111 void EventData::Dump(Stream
*s
) const { s
->PutCString("Generic Event Data"); }
114 #pragma mark EventDataBytes
116 // EventDataBytes functions
118 EventDataBytes::EventDataBytes() : m_bytes() {}
120 EventDataBytes::EventDataBytes(llvm::StringRef str
) : m_bytes(str
.str()) {}
122 EventDataBytes::~EventDataBytes() = default;
124 llvm::StringRef
EventDataBytes::GetFlavorString() { return "EventDataBytes"; }
126 llvm::StringRef
EventDataBytes::GetFlavor() const {
127 return EventDataBytes::GetFlavorString();
130 void EventDataBytes::Dump(Stream
*s
) const {
131 if (llvm::all_of(m_bytes
, llvm::isPrint
))
132 s
->Format("\"{0}\"", m_bytes
);
134 s
->Format("{0:$[ ]@[x-2]}", llvm::make_range(
135 reinterpret_cast<const uint8_t *>(m_bytes
.data()),
136 reinterpret_cast<const uint8_t *>(m_bytes
.data() +
140 const void *EventDataBytes::GetBytes() const {
141 return (m_bytes
.empty() ? nullptr : m_bytes
.data());
144 size_t EventDataBytes::GetByteSize() const { return m_bytes
.size(); }
146 const void *EventDataBytes::GetBytesFromEvent(const Event
*event_ptr
) {
147 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
149 return e
->GetBytes();
153 size_t EventDataBytes::GetByteSizeFromEvent(const Event
*event_ptr
) {
154 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
156 return e
->GetByteSize();
160 const EventDataBytes
*
161 EventDataBytes::GetEventDataFromEvent(const Event
*event_ptr
) {
162 if (event_ptr
!= nullptr) {
163 const EventData
*event_data
= event_ptr
->GetData();
165 event_data
->GetFlavor() == EventDataBytes::GetFlavorString())
166 return static_cast<const EventDataBytes
*>(event_data
);
171 llvm::StringRef
EventDataReceipt::GetFlavorString() {
172 return "Process::ProcessEventData";
176 #pragma mark EventStructuredData
178 // EventDataStructuredData definitions
180 EventDataStructuredData::EventDataStructuredData()
181 : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
183 EventDataStructuredData::EventDataStructuredData(
184 const ProcessSP
&process_sp
, const StructuredData::ObjectSP
&object_sp
,
185 const lldb::StructuredDataPluginSP
&plugin_sp
)
186 : EventData(), m_process_sp(process_sp
), m_object_sp(object_sp
),
187 m_plugin_sp(plugin_sp
) {}
189 EventDataStructuredData::~EventDataStructuredData() = default;
191 // EventDataStructuredData member functions
193 llvm::StringRef
EventDataStructuredData::GetFlavor() const {
194 return EventDataStructuredData::GetFlavorString();
197 void EventDataStructuredData::Dump(Stream
*s
) const {
202 m_object_sp
->Dump(*s
);
205 const ProcessSP
&EventDataStructuredData::GetProcess() const {
209 const StructuredData::ObjectSP
&EventDataStructuredData::GetObject() const {
213 const lldb::StructuredDataPluginSP
&
214 EventDataStructuredData::GetStructuredDataPlugin() const {
218 void EventDataStructuredData::SetProcess(const ProcessSP
&process_sp
) {
219 m_process_sp
= process_sp
;
222 void EventDataStructuredData::SetObject(
223 const StructuredData::ObjectSP
&object_sp
) {
224 m_object_sp
= object_sp
;
227 void EventDataStructuredData::SetStructuredDataPlugin(
228 const lldb::StructuredDataPluginSP
&plugin_sp
) {
229 m_plugin_sp
= plugin_sp
;
232 // EventDataStructuredData static functions
234 const EventDataStructuredData
*
235 EventDataStructuredData::GetEventDataFromEvent(const Event
*event_ptr
) {
236 if (event_ptr
== nullptr)
239 const EventData
*event_data
= event_ptr
->GetData();
241 event_data
->GetFlavor() != EventDataStructuredData::GetFlavorString())
244 return static_cast<const EventDataStructuredData
*>(event_data
);
247 ProcessSP
EventDataStructuredData::GetProcessFromEvent(const Event
*event_ptr
) {
248 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
250 return event_data
->GetProcess();
255 StructuredData::ObjectSP
256 EventDataStructuredData::GetObjectFromEvent(const Event
*event_ptr
) {
257 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
259 return event_data
->GetObject();
261 return StructuredData::ObjectSP();
264 lldb::StructuredDataPluginSP
265 EventDataStructuredData::GetPluginFromEvent(const Event
*event_ptr
) {
266 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
268 return event_data
->GetStructuredDataPlugin();
270 return StructuredDataPluginSP();
273 llvm::StringRef
EventDataStructuredData::GetFlavorString() {
274 return "EventDataStructuredData";