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 
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 <cctype>
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 
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  if (llvm::all_of(m_bytes, llvm::isPrint))
128  s->Format("\"{0}\"", m_bytes);
129  else
130  s->Format("{0:$[ ]@[x-2]}", llvm::make_range(
131  reinterpret_cast<const uint8_t *>(m_bytes.data()),
132  reinterpret_cast<const uint8_t *>(m_bytes.data() +
133  m_bytes.size())));
134 }
135 
136 const void *EventDataBytes::GetBytes() const {
137  return (m_bytes.empty() ? nullptr : m_bytes.data());
138 }
139 
140 size_t EventDataBytes::GetByteSize() const { return m_bytes.size(); }
141 
142 void EventDataBytes::SetBytes(const void *src, size_t src_len) {
143  if (src != nullptr && src_len > 0)
144  m_bytes.assign(static_cast<const char *>(src), src_len);
145  else
146  m_bytes.clear();
147 }
148 
149 void EventDataBytes::SetBytesFromCString(const char *cstr) {
150  if (cstr != nullptr && cstr[0])
151  m_bytes.assign(cstr);
152  else
153  m_bytes.clear();
154 }
155 
156 const void *EventDataBytes::GetBytesFromEvent(const Event *event_ptr) {
157  const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
158  if (e != nullptr)
159  return e->GetBytes();
160  return nullptr;
161 }
162 
163 size_t EventDataBytes::GetByteSizeFromEvent(const Event *event_ptr) {
164  const EventDataBytes *e = GetEventDataFromEvent(event_ptr);
165  if (e != nullptr)
166  return e->GetByteSize();
167  return 0;
168 }
169 
170 const EventDataBytes *
172  if (event_ptr != nullptr) {
173  const EventData *event_data = event_ptr->GetData();
174  if (event_data &&
175  event_data->GetFlavor() == EventDataBytes::GetFlavorString())
176  return static_cast<const EventDataBytes *>(event_data);
177  }
178  return nullptr;
179 }
180 
182  m_bytes.swap(new_bytes);
183 }
184 
185 #pragma mark -
186 #pragma mark EventStructuredData
187 
188 // EventDataStructuredData definitions
189 
191  : EventData(), m_process_sp(), m_object_sp(), m_plugin_sp() {}
192 
194  const ProcessSP &process_sp, const StructuredData::ObjectSP &object_sp,
195  const lldb::StructuredDataPluginSP &plugin_sp)
196  : EventData(), m_process_sp(process_sp), m_object_sp(object_sp),
197  m_plugin_sp(plugin_sp) {}
198 
200 
201 // EventDataStructuredData member functions
202 
205 }
206 
208  if (!s)
209  return;
210 
211  if (m_object_sp)
212  m_object_sp->Dump(*s);
213 }
214 
215 const ProcessSP &EventDataStructuredData::GetProcess() const {
216  return m_process_sp;
217 }
218 
220  return m_object_sp;
221 }
222 
223 const lldb::StructuredDataPluginSP &
225  return m_plugin_sp;
226 }
227 
228 void EventDataStructuredData::SetProcess(const ProcessSP &process_sp) {
229  m_process_sp = process_sp;
230 }
231 
233  const StructuredData::ObjectSP &object_sp) {
234  m_object_sp = object_sp;
235 }
236 
238  const lldb::StructuredDataPluginSP &plugin_sp) {
239  m_plugin_sp = plugin_sp;
240 }
241 
242 // EventDataStructuredData static functions
243 
246  if (event_ptr == nullptr)
247  return nullptr;
248 
249  const EventData *event_data = event_ptr->GetData();
250  if (!event_data ||
252  return nullptr;
253 
254  return static_cast<const EventDataStructuredData *>(event_data);
255 }
256 
258  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
259  if (event_data)
260  return event_data->GetProcess();
261  else
262  return ProcessSP();
263 }
264 
267  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
268  if (event_data)
269  return event_data->GetObject();
270  else
271  return StructuredData::ObjectSP();
272 }
273 
274 lldb::StructuredDataPluginSP
276  auto event_data = EventDataStructuredData::GetEventDataFromEvent(event_ptr);
277  if (event_data)
278  return event_data->GetStructuredDataPlugin();
279  else
280  return StructuredDataPluginSP();
281 }
282 
284  static ConstString s_flavor("EventDataStructuredData");
285  return s_flavor;
286 }
lldb_private::Stream::Format
void Format(const char *format, Args &&... args)
Definition: Stream.h:309
lldb_private::EventData::GetFlavor
virtual ConstString GetFlavor() const =0
lldb_private::Event
Definition: Event.h:182
lldb_private::EventDataBytes::GetByteSize
size_t GetByteSize() const
Definition: Event.cpp:140
lldb_private::EventData::~EventData
virtual ~EventData()
lldb_private::EventDataBytes::GetBytes
const void * GetBytes() const
Definition: Event.cpp:136
lldb_private::EventData
Definition: Event.h:36
lldb_private::EventDataStructuredData::Dump
void Dump(Stream *s) const override
Definition: Event.cpp:207
lldb_private::EventDataStructuredData::SetProcess
void SetProcess(const lldb::ProcessSP &process_sp)
Definition: Event.cpp:228
lldb_private::EventDataStructuredData::GetFlavor
ConstString GetFlavor() const override
Definition: Event.cpp:203
lldb_private::EventDataStructuredData::EventDataStructuredData
EventDataStructuredData()
Definition: Event.cpp:190
lldb_private::Stream
Definition: Stream.h:28
lldb_private::EventDataBytes::GetByteSizeFromEvent
static size_t GetByteSizeFromEvent(const Event *event_ptr)
Definition: Event.cpp:163
lldb_private::EventDataBytes::GetEventDataFromEvent
static const EventDataBytes * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:171
lldb_private::EventDataBytes::SwapBytes
void SwapBytes(std::string &new_bytes)
Definition: Event.cpp:181
lldb_private::EventDataStructuredData::GetFlavorString
static ConstString GetFlavorString()
Definition: Event.cpp:283
Broadcaster.h
lldb_private::EventDataStructuredData::GetStructuredDataPlugin
const lldb::StructuredDataPluginSP & GetStructuredDataPlugin() const
Definition: Event.cpp:224
lldb_private::EventDataStructuredData::m_plugin_sp
lldb::StructuredDataPluginSP m_plugin_sp
Definition: Event.h:174
lldb_private::Event::GetData
EventData * GetData()
Definition: Event.h:202
lldb_private::Event::Dump
void Dump(Stream *s) const
Definition: Event.cpp:47
lldb_private::EventDataBytes::m_bytes
std::string m_bytes
Definition: Event.h:96
lldb_private::EventDataBytes::SetBytesFromCString
void SetBytesFromCString(const char *cstr)
Definition: Event.cpp:149
lldb_private::EventDataStructuredData::GetObjectFromEvent
static StructuredData::ObjectSP GetObjectFromEvent(const Event *event_ptr)
Definition: Event.cpp:266
lldb_private::EventDataStructuredData::m_process_sp
lldb::ProcessSP m_process_sp
Definition: Event.h:172
lldb_private::Event::~Event
~Event()
lldb_private::Event::m_broadcaster_wp
Broadcaster::BroadcasterImplWP m_broadcaster_wp
Definition: Event.h:247
lldb_private::EventData::Dump
virtual void Dump(Stream *s) const
Definition: Event.cpp:94
lldb_private::Event::DoOnRemoval
void DoOnRemoval()
Definition: Event.cpp:80
lldb_private::EventDataBytes::EventDataBytes
EventDataBytes()
Definition: Event.cpp:101
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
lldb_private::StreamString::GetData
const char * GetData() const
Definition: StreamString.h:43
StreamString.h
lldb_private::ConstString
Definition: ConstString.h:39
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::EventDataBytes::Dump
void Dump(Stream *s) const override
Definition: Event.cpp:126
lldb-enumerations.h
lldb_private::Event::Event
Event()=delete
lldb_private::EventDataBytes::SetBytes
void SetBytes(const void *src, size_t src_len)
Definition: Event.cpp:142
lldb_private::EventDataBytes
Definition: Event.h:58
lldb_private::EventDataStructuredData::m_object_sp
StructuredData::ObjectSP m_object_sp
Definition: Event.h:173
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::EventDataStructuredData::SetStructuredDataPlugin
void SetStructuredDataPlugin(const lldb::StructuredDataPluginSP &plugin_sp)
Definition: Event.cpp:237
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
Event.h
lldb_private::EventDataStructuredData::GetPluginFromEvent
static lldb::StructuredDataPluginSP GetPluginFromEvent(const Event *event_ptr)
Definition: Event.cpp:275
lldb_private::Broadcaster::BroadcasterImplSP
std::shared_ptr< BroadcasterImpl > BroadcasterImplSP
Definition: Broadcaster.h:521
lldb_private::Event::m_type
uint32_t m_type
Definition: Event.h:248
lldb_private::EventDataStructuredData::~EventDataStructuredData
~EventDataStructuredData() override
uint32_t
lldb_private::EventDataBytes::GetFlavor
ConstString GetFlavor() const override
Definition: Event.cpp:122
lldb_private::EventDataBytes::GetBytesFromEvent
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition: Event.cpp:156
lldb_private::ConstString::GetCString
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
DataExtractor.h
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private::Broadcaster::GetBroadcasterName
ConstString GetBroadcasterName()
Get the NULL terminated C string name of this Broadcaster object.
Definition: Broadcaster.h:317
lldb_private::EventDataStructuredData::GetObject
const StructuredData::ObjectSP & GetObject() const
Definition: Event.cpp:219
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::EventDataBytes::GetFlavorString
static ConstString GetFlavorString()
Definition: Event.cpp:117
lldb_private::EventDataBytes::~EventDataBytes
~EventDataBytes() override
lldb_private::EventDataStructuredData::GetProcess
const lldb::ProcessSP & GetProcess() const
Definition: Event.cpp:215
lldb_private::Event::m_data_sp
lldb::EventDataSP m_data_sp
Definition: Event.h:249
Stream.h
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb_private::EventDataStructuredData::GetProcessFromEvent
static lldb::ProcessSP GetProcessFromEvent(const Event *event_ptr)
Definition: Event.cpp:257
lldb_private::EventData::EventData
EventData()
lldb_private::EventDataStructuredData::GetEventDataFromEvent
static const EventDataStructuredData * GetEventDataFromEvent(const Event *event_ptr)
Definition: Event.cpp:245
lldb_private::Broadcaster
Definition: Broadcaster.h:242
lldb
Definition: SBAddress.h:15
Endian.h
lldb_private::EventDataStructuredData::SetObject
void SetObject(const StructuredData::ObjectSP &object_sp)
Definition: Event.cpp:232
lldb_private::Broadcaster::GetEventNames
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:326
lldb_private::EventDataStructuredData
This class handles one or more StructuredData::Dictionary entries that are raised for structured data...
Definition: Event.h:130