LLDB  mainline
Event.cpp
Go to the documentation of this file.
1 //===-- Event.cpp -----------------------------------------------*- C++ -*-===//
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 
9 #include "lldb/Utility/Event.h"
10 
13 #include "lldb/Utility/Endian.h"
14 #include "lldb/Utility/Stream.h"
16 #include "lldb/lldb-enumerations.h"
17 
18 #include <algorithm>
19 
20 #include <ctype.h>
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 #pragma mark -
26 #pragma mark Event
27 
28 // Event functions
29 
30 Event::Event(Broadcaster *broadcaster, uint32_t event_type, EventData *data)
31  : m_broadcaster_wp(broadcaster->GetBroadcasterImpl()), m_type(event_type),
32  m_data_sp(data) {}
33 
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) {}
38 
39 Event::Event(uint32_t event_type, EventData *data)
40  : m_broadcaster_wp(), m_type(event_type), m_data_sp(data) {}
41 
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) {}
44 
45 Event::~Event() = default;
46 
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();
52  else
53  broadcaster = nullptr;
54 
55  if (broadcaster) {
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());
63  else
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);
68  } else
69  s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
70  static_cast<const void *>(this), m_type);
71 
72  if (m_data_sp) {
73  s->PutChar('{');
74  m_data_sp->Dump(s);
75  s->PutChar('}');
76  } else
77  s->Printf("<NULL>");
78 }
79 
80 void Event::DoOnRemoval() {
81  if (m_data_sp)
82  m_data_sp->DoOnRemoval(this);
83 }
84 
85 #pragma mark -
86 #pragma mark EventData
87 
88 // EventData functions
89 
90 EventData::EventData() = default;
91 
92 EventData::~EventData() = default;
93 
94 void EventData::Dump(Stream *s) const { s->PutCString("Generic Event Data"); }
95 
96 #pragma mark -
97 #pragma mark EventDataBytes
98 
99 // EventDataBytes functions
100 
102 
103 EventDataBytes::EventDataBytes(const char *cstr) : m_bytes() {
104  SetBytesFromCString(cstr);
105 }
106 
107 EventDataBytes::EventDataBytes(llvm::StringRef str) : m_bytes() {
108  SetBytes(str.data(), str.size());
109 }
110 
111 EventDataBytes::EventDataBytes(const void *src, size_t src_len) : m_bytes() {
112  SetBytes(src, src_len);
113 }
114 
116 
118  static ConstString g_flavor("EventDataBytes");
119  return g_flavor;
120 }
121 
124 }
125 
126 void EventDataBytes::Dump(Stream *s) const {
127  size_t num_printable_chars =
128  std::count_if(m_bytes.begin(), m_bytes.end(), isprint);
129  if (num_printable_chars == m_bytes.size())
130  s->Format("\"{0}\"", m_bytes);
131  else
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() +
135  m_bytes.size())));
136 }
137 
138 const void *EventDataBytes::GetBytes() const {
139  return (m_bytes.empty() ? nullptr : m_bytes.data());
140 }
141 
142 size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }
143 
144 void EventDataBytes::SetBytes(const void *src, size_t src_len) {
145  if (src != nullptr && src_len > 0)
146  m_bytes.assign((const char *)src, src_len);
147  else
148  m_bytes.clear();
149 }
150 
151 void EventDataBytes::SetBytesFromCString(const char *cstr) {
152  if (cstr != nullptr && cstr[0])
153  m_bytes.assign(cstr);
154  else
155  m_bytes.clear();
156 }
157 
158 const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {
159  const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
160  if (e != nullptr)
161  return e->GetBytes();
162  return nullptr;
163 }
164 
165 size_t EventDataBytes::GetByteSizeFromEvent(const Event *event_ptr) {
166  const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
167  if (e != nullptr)
168  return e->GetByteSize();
169  return 0;
170 }
171 
172 const EventDataBytes *
174  if (event_ptr != nullptr) {
175  const EventData *event_data = event_ptr->GetData();
176  if (event_data &&
177  event_data->GetFlavor() == EventDataBytes::GetFlavorString())
178  return static_cast<const EventDataBytes *>(event_data);
179  }
180  return nullptr;
181 }
182 
183 void EventDataBytes::SwapBytes(std::string &new_bytes) {
184  m_bytes.swap(new_bytes);
185 }
186 
187 #pragma mark -
188 #pragma mark EventStructuredData
189 
190 // EventDataStructuredData definitions
191 
193  : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
194 
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) {}
200 
202 
203 // EventDataStructuredData member functions
204 
207 }
208 
210  if (!s)
211  return;
212 
213  if (m_object_sp)
214  m_object_sp->Dump(*s);
215 }
216 
217 const ProcessSP &EventDataStructuredData::GetProcess() const {
218  return m_process_sp;
219 }
220 
222  return m_object_sp;
223 }
224 
225 const lldb::StructuredDataPluginSP &
227  return m_plugin_sp;
228 }
229 
230 void EventDataStructuredData::SetProcess(const ProcessSP &process_sp) {
231  m_process_sp = process_sp;
232 }
233 
235  const StructuredData::ObjectSP &object_sp) {
236  m_object_sp = object_sp;
237 }
238 
240  const lldb::StructuredDataPluginSP &plugin_sp) {
241  m_plugin_sp = plugin_sp;
242 }
243 
244 // EventDataStructuredData static functions
245 
248  if (event_ptr == nullptr)
249  return nullptr;
250 
251  const EventData *event_data = event_ptr->GetData();
252  if (!event_data ||
254  return nullptr;
255 
256  return static_cast<const EventDataStructuredData *>(event_data);
257 }
258 
260  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
261  if (event_data)
262  return event_data->GetProcess();
263  else
264  return ProcessSP();
265 }
266 
269  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
270  if (event_data)
271  return event_data->GetObject();
272  else
273  return StructuredData::ObjectSP();
274 }
275 
276 lldb::StructuredDataPluginSP
278  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
279  if (event_data)
280  return event_data->GetStructuredDataPlugin();
281  else
282  return StructuredDataPluginSP();
283 }
284 
286  static ConstString s_flavor("EventDataStructuredData");
287  return s_flavor;
288 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
static size_t GetByteSizeFromEvent(const Event *event_ptr)
Definition: Event.cpp:165
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
static bool isprint(char32_t codepoint)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition: Event.cpp:259
void SwapBytes(std::string &new_bytes)
Definition: Event.cpp:183
static const EventDataStructuredData * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:247
An event broadcasting class.
Definition: Broadcaster.h:250
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:128
void SetBytes(const void *src, size_t src_len)
Definition: Event.cpp:144
const void * GetBytes() const
Definition: Event.cpp:138
const char * GetData() const
Definition: StreamString.h:43
static const EventDataBytes * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:173
const StructuredData::ObjectSP & GetObject() const
Definition: Event.cpp:221
ConstString GetFlavor() const override
Definition: Event.cpp:122
void Dump(Stream *s) const
Definition: Event.cpp:47
static ConstString GetFlavorString()
Definition: Event.cpp:285
void Format(const char *format, Args &&... args)
Definition: Stream.h:422
const lldb::ProcessSP & GetProcess() const
Definition: Event.cpp:217
void SetObject(const StructuredData::ObjectSP &object_sp)
Definition: Event.cpp:234
static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr)
Definition: Event.cpp:268
BroadcasterImplSP GetBroadcasterImpl()
Definition: Broadcaster.h:544
size_t GetByteSize() const
Definition: Event.cpp:142
const lldb::StructuredDataPluginSP & GetStructuredDataPlugin() const
Definition: Event.cpp:226
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition: Event.cpp:158
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:541
virtual void Dump(Stream *s) const
Definition: Event.cpp:94
void SetProcess(const lldb::ProcessSP &process_sp)
Definition: Event.cpp:230
ConstString GetBroadcasterName()
Get the NULL terminated C string name of this Broadcaster object.
Definition: Broadcaster.h:338
void SetBytesFromCString(const char *cstr)
Definition: Event.cpp:151
size_t PutChar(char ch)
Definition: Stream.cpp:103
ConstString GetFlavor() const override
Definition: Event.cpp:205
A uniqued constant string class.
Definition: ConstString.h:38
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:247
void Dump(Stream *s) const override
Definition: Event.cpp:209
void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp)
Definition: Event.cpp:239
static lldb::StructuredDataPluginSP GetPluginFromEvent(const Event *event_ptr)
Definition: Event.cpp:277
EventData * GetData()
Definition: Event.h:198
Definition: SBAddress.h:15
std::shared_ptr< Object > ObjectSP
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:347
void Dump(Stream *s) const override
Definition: Event.cpp:126
virtual ConstString GetFlavor() const =0
static ConstString GetFlavorString()
Definition: Event.cpp:117