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
);
87 m_data_sp
->DoOnRemoval(this);
88 // Now that the event has been handled by the primary event Listener, forward
89 // it to the other Listeners.
90 EventSP me_sp
= shared_from_this();
91 for (auto listener_sp
: m_pending_listeners
)
92 listener_sp
->AddEvent(me_sp
);
93 m_pending_listeners
.clear();
97 #pragma mark EventData
99 // EventData functions
101 EventData::EventData() = default;
103 EventData::~EventData() = default;
105 void EventData::Dump(Stream
*s
) const { s
->PutCString("Generic Event Data"); }
108 #pragma mark EventDataBytes
110 // EventDataBytes functions
112 EventDataBytes::EventDataBytes() : m_bytes() {}
114 EventDataBytes::EventDataBytes(const char *cstr
) : m_bytes() {
115 SetBytesFromCString(cstr
);
118 EventDataBytes::EventDataBytes(llvm::StringRef str
) : m_bytes() {
119 SetBytes(str
.data(), str
.size());
122 EventDataBytes::EventDataBytes(const void *src
, size_t src_len
) : m_bytes() {
123 SetBytes(src
, src_len
);
126 EventDataBytes::~EventDataBytes() = default;
128 llvm::StringRef
EventDataBytes::GetFlavorString() { return "EventDataBytes"; }
130 llvm::StringRef
EventDataBytes::GetFlavor() const {
131 return EventDataBytes::GetFlavorString();
134 void EventDataBytes::Dump(Stream
*s
) const {
135 if (llvm::all_of(m_bytes
, llvm::isPrint
))
136 s
->Format("\"{0}\"", m_bytes
);
138 s
->Format("{0:$[ ]@[x-2]}", llvm::make_range(
139 reinterpret_cast<const uint8_t *>(m_bytes
.data()),
140 reinterpret_cast<const uint8_t *>(m_bytes
.data() +
144 const void *EventDataBytes::GetBytes() const {
145 return (m_bytes
.empty() ? nullptr : m_bytes
.data());
148 size_t EventDataBytes::GetByteSize() const { return m_bytes
.size(); }
150 void EventDataBytes::SetBytes(const void *src
, size_t src_len
) {
151 if (src
!= nullptr && src_len
> 0)
152 m_bytes
.assign(static_cast<const char *>(src
), src_len
);
157 void EventDataBytes::SetBytesFromCString(const char *cstr
) {
158 if (cstr
!= nullptr && cstr
[0])
159 m_bytes
.assign(cstr
);
164 const void *EventDataBytes::GetBytesFromEvent(const Event
*event_ptr
) {
165 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
167 return e
->GetBytes();
171 size_t EventDataBytes::GetByteSizeFromEvent(const Event
*event_ptr
) {
172 const EventDataBytes
*e
= GetEventDataFromEvent(event_ptr
);
174 return e
->GetByteSize();
178 const EventDataBytes
*
179 EventDataBytes::GetEventDataFromEvent(const Event
*event_ptr
) {
180 if (event_ptr
!= nullptr) {
181 const EventData
*event_data
= event_ptr
->GetData();
183 event_data
->GetFlavor() == EventDataBytes::GetFlavorString())
184 return static_cast<const EventDataBytes
*>(event_data
);
189 void EventDataBytes::SwapBytes(std::string
&new_bytes
) {
190 m_bytes
.swap(new_bytes
);
193 llvm::StringRef
EventDataReceipt::GetFlavorString() {
194 return "Process::ProcessEventData";
198 #pragma mark EventStructuredData
200 // EventDataStructuredData definitions
202 EventDataStructuredData::EventDataStructuredData()
203 : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
205 EventDataStructuredData::EventDataStructuredData(
206 const ProcessSP
&process_sp
, const StructuredData::ObjectSP
&object_sp
,
207 const lldb::StructuredDataPluginSP
&plugin_sp
)
208 : EventData(), m_process_sp(process_sp
), m_object_sp(object_sp
),
209 m_plugin_sp(plugin_sp
) {}
211 EventDataStructuredData::~EventDataStructuredData() = default;
213 // EventDataStructuredData member functions
215 llvm::StringRef
EventDataStructuredData::GetFlavor() const {
216 return EventDataStructuredData::GetFlavorString();
219 void EventDataStructuredData::Dump(Stream
*s
) const {
224 m_object_sp
->Dump(*s
);
227 const ProcessSP
&EventDataStructuredData::GetProcess() const {
231 const StructuredData::ObjectSP
&EventDataStructuredData::GetObject() const {
235 const lldb::StructuredDataPluginSP
&
236 EventDataStructuredData::GetStructuredDataPlugin() const {
240 void EventDataStructuredData::SetProcess(const ProcessSP
&process_sp
) {
241 m_process_sp
= process_sp
;
244 void EventDataStructuredData::SetObject(
245 const StructuredData::ObjectSP
&object_sp
) {
246 m_object_sp
= object_sp
;
249 void EventDataStructuredData::SetStructuredDataPlugin(
250 const lldb::StructuredDataPluginSP
&plugin_sp
) {
251 m_plugin_sp
= plugin_sp
;
254 // EventDataStructuredData static functions
256 const EventDataStructuredData
*
257 EventDataStructuredData::GetEventDataFromEvent(const Event
*event_ptr
) {
258 if (event_ptr
== nullptr)
261 const EventData
*event_data
= event_ptr
->GetData();
263 event_data
->GetFlavor() != EventDataStructuredData::GetFlavorString())
266 return static_cast<const EventDataStructuredData
*>(event_data
);
269 ProcessSP
EventDataStructuredData::GetProcessFromEvent(const Event
*event_ptr
) {
270 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
272 return event_data
->GetProcess();
277 StructuredData::ObjectSP
278 EventDataStructuredData::GetObjectFromEvent(const Event
*event_ptr
) {
279 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
281 return event_data
->GetObject();
283 return StructuredData::ObjectSP();
286 lldb::StructuredDataPluginSP
287 EventDataStructuredData::GetPluginFromEvent(const Event
*event_ptr
) {
288 auto event_data
= EventDataStructuredData::GetEventDataFromEvent(event_ptr
);
290 return event_data
->GetStructuredDataPlugin();
292 return StructuredDataPluginSP();
295 llvm::StringRef
EventDataStructuredData::GetFlavorString() {
296 return "EventDataStructuredData";