LLDB mainline
Event.cpp
Go to the documentation of this file.
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
10
13#include "lldb/Utility/Endian.h"
15#include "lldb/Utility/Stream.h"
18
19#include "llvm/ADT/StringExtras.h"
20
21#include <algorithm>
22
23#include <cctype>
24
25using namespace lldb;
26using namespace lldb_private;
27
28#pragma mark -
29#pragma mark Event
30
31// Event functions
32
33Event::Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data)
34 : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
35 m_data_sp(data) {}
36
37Event::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) {}
41
42Event::Event(uint32_t event_type, EventData *data)
43 : m_broadcaster_wp(), m_type(event_type), m_data_sp(data) {}
44
45Event::Event(uint32_t event_type, const EventDataSP &event_data_sp)
46 : m_broadcaster_wp(), m_type(event_type), m_data_sp(event_data_sp) {}
47
48Event::~Event() = default;
49
50void 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();
55 else
56 broadcaster = nullptr;
57
58 if (broadcaster) {
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());
66 else
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);
71 } else
72 s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
73 static_cast<const void *>(this), m_type);
74
75 if (m_data_sp) {
76 s->PutChar('{');
77 m_data_sp->Dump(s);
78 s->PutChar('}');
79 } else
80 s->Printf("<NULL>");
81}
82
84 std::lock_guard<std::mutex> guard(m_listeners_mutex);
85
86 if (!m_data_sp)
87 return;
88
89 m_data_sp->DoOnRemoval(this);
90
91 // Now that the event has been handled by the primary event Listener, forward
92 // it to the other Listeners.
93
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();
99 }
100}
101
102#pragma mark -
103#pragma mark EventData
104
105// EventData functions
106
107EventData::EventData() = default;
108
109EventData::~EventData() = default;
110
111void EventData::Dump(Stream *s) const { s->PutCString("Generic Event Data"); }
112
113#pragma mark -
114#pragma mark EventDataBytes
115
116// EventDataBytes functions
117
119
120EventDataBytes::EventDataBytes(llvm::StringRef str) : m_bytes(str.str()) {}
121
123
124llvm::StringRef EventDataBytes::GetFlavorString() { return "EventDataBytes"; }
125
126llvm::StringRef EventDataBytes::GetFlavor() const {
128}
129
131 if (llvm::all_of(m_bytes, llvm::isPrint))
132 s->Format("\"{0}\"", m_bytes);
133 else
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() +
137 m_bytes.size())));
138}
139
140const void *EventDataBytes::GetBytes() const {
141 return (m_bytes.empty() ? nullptr : m_bytes.data());
142}
143
144size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }
145
146const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {
147 const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
148 if (e != nullptr)
149 return e->GetBytes();
150 return nullptr;
151}
152
154 const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
155 if (e != nullptr)
156 return e->GetByteSize();
157 return 0;
158}
159
160const EventDataBytes *
162 if (event_ptr != nullptr) {
163 const EventData *event_data = event_ptr->GetData();
164 if (event_data &&
166 return static_cast<const EventDataBytes *>(event_data);
167 }
168 return nullptr;
169}
170
172 return "Process::ProcessEventData";
173}
174
175#pragma mark -
176#pragma mark EventStructuredData
177
178// EventDataStructuredData definitions
179
181 : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
182
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) {}
188
190
191// EventDataStructuredData member functions
192
193llvm::StringRef EventDataStructuredData::GetFlavor() const {
195}
196
198 if (!s)
199 return;
200
201 if (m_object_sp)
202 m_object_sp->Dump(*s);
203}
204
206 return m_process_sp;
207}
208
210 return m_object_sp;
211}
212
215 return m_plugin_sp;
216}
217
219 m_process_sp = process_sp;
220}
221
223 const StructuredData::ObjectSP &object_sp) {
224 m_object_sp = object_sp;
225}
226
228 const lldb::StructuredDataPluginSP &plugin_sp) {
229 m_plugin_sp = plugin_sp;
230}
231
232// EventDataStructuredData static functions
233
236 if (event_ptr == nullptr)
237 return nullptr;
238
239 const EventData *event_data = event_ptr->GetData();
240 if (!event_data ||
242 return nullptr;
243
244 return static_cast<const EventDataStructuredData *>(event_data);
245}
246
248 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
249 if (event_data)
250 return event_data->GetProcess();
251 else
252 return ProcessSP();
253}
254
257 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
258 if (event_data)
259 return event_data->GetObject();
260 else
262}
263
266 auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
267 if (event_data)
268 return event_data->GetStructuredDataPlugin();
269 else
270 return StructuredDataPluginSP();
271}
272
274 return "EventDataStructuredData";
275}
An event broadcasting class.
Definition: Broadcaster.h:146
const std::string & GetBroadcasterName()
Get this broadcaster's name.
Definition: Broadcaster.h:223
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:464
bool GetEventNames(Stream &s, const uint32_t event_mask, bool prefix_with_broadcaster_name) const
Get the event name(s) for one or more event bits.
Definition: Broadcaster.h:232
const void * GetBytes() const
Definition: Event.cpp:140
static llvm::StringRef GetFlavorString()
Definition: Event.cpp:124
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition: Event.cpp:146
static size_t GetByteSizeFromEvent(const Event *event_ptr)
Definition: Event.cpp:153
llvm::StringRef GetFlavor() const override
Definition: Event.cpp:126
void Dump(Stream *s) const override
Definition: Event.cpp:130
static const EventDataBytes * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:161
size_t GetByteSize() const
Definition: Event.cpp:144
static llvm::StringRef GetFlavorString()
Definition: Event.cpp:171
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:128
llvm::StringRef GetFlavor() const override
Definition: Event.cpp:193
const StructuredData::ObjectSP & GetObject() const
Definition: Event.cpp:209
const lldb::ProcessSP & GetProcess() const
Definition: Event.cpp:205
void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp)
Definition: Event.cpp:227
lldb::StructuredDataPluginSP m_plugin_sp
Definition: Event.h:172
const lldb::StructuredDataPluginSP & GetStructuredDataPlugin() const
Definition: Event.cpp:214
void SetObject(const StructuredData::ObjectSP &object_sp)
Definition: Event.cpp:222
StructuredData::ObjectSP m_object_sp
Definition: Event.h:171
static llvm::StringRef GetFlavorString()
Definition: Event.cpp:273
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition: Event.cpp:247
void SetProcess(const lldb::ProcessSP &process_sp)
Definition: Event.cpp:218
static lldb::StructuredDataPluginSP GetPluginFromEvent(const Event *event_ptr)
Definition: Event.cpp:265
static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr)
Definition: Event.cpp:256
static const EventDataStructuredData * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:235
void Dump(Stream *s) const override
Definition: Event.cpp:197
virtual void Dump(Stream *s) const
Definition: Event.cpp:111
virtual llvm::StringRef GetFlavor() const =0
EventData * GetData()
Definition: Event.h:200
Broadcaster::BroadcasterImplWP m_broadcaster_wp
Definition: Event.h:251
lldb::EventDataSP m_data_sp
Definition: Event.h:253
void DoOnRemoval()
Definition: Event.cpp:83
uint32_t m_type
Definition: Event.h:252
std::mutex m_listeners_mutex
Definition: Event.h:255
void Dump(Stream *s) const
Definition: Event.cpp:50
std::vector< lldb::ListenerSP > m_pending_listeners
Definition: Event.h:254
const char * GetData() const
Definition: StreamString.h:45
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
void Format(const char *format, Args &&... args)
Definition: Stream.h:353
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
size_t PutChar(char ch)
Definition: Stream.cpp:131
std::shared_ptr< Object > ObjectSP
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::StructuredDataPlugin > StructuredDataPluginSP
Definition: lldb-forward.h:434
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:343
std::shared_ptr< lldb_private::EventData > EventDataSP
Definition: lldb-forward.h:344