LLDB  mainline
SBEvent.cpp
Go to the documentation of this file.
1 //===-- SBEvent.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/API/SBEvent.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBBroadcaster.h"
12 #include "lldb/API/SBStream.h"
13 
15 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/Event.h"
20 #include "lldb/Utility/Stream.h"
21 
22 using namespace lldb;
23 using namespace lldb_private;
24 
25 SBEvent::SBEvent() : m_event_sp(), m_opaque_ptr(NULL) {
27 }
28 
29 SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
30  : m_event_sp(new Event(event_type, new EventDataBytes(cstr, cstr_len))),
31  m_opaque_ptr(m_event_sp.get()) {
33  event_type, cstr, cstr_len);
34 }
35 
36 SBEvent::SBEvent(EventSP &event_sp)
37  : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {
38  LLDB_RECORD_CONSTRUCTOR(SBEvent, (lldb::EventSP &), event_sp);
39 }
40 
41 SBEvent::SBEvent(Event *event_ptr) : m_event_sp(), m_opaque_ptr(event_ptr) {
43 }
44 
46  : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
48 }
49 
50 const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
52  SBEvent, operator=,(const lldb::SBEvent &), rhs);
53 
54  if (this != &rhs) {
55  m_event_sp = rhs.m_event_sp;
56  m_opaque_ptr = rhs.m_opaque_ptr;
57  }
58  return LLDB_RECORD_RESULT(*this);
59 }
60 
62 
63 const char *SBEvent::GetDataFlavor() {
65 
66  Event *lldb_event = get();
67  if (lldb_event) {
68  EventData *event_data = lldb_event->GetData();
69  if (event_data)
70  return lldb_event->GetData()->GetFlavor().AsCString();
71  }
72  return NULL;
73 }
74 
77 
78 
79  const Event *lldb_event = get();
80  uint32_t event_type = 0;
81  if (lldb_event)
82  event_type = lldb_event->GetType();
83 
84 
85  return event_type;
86 }
87 
91 
92  SBBroadcaster broadcaster;
93  const Event *lldb_event = get();
94  if (lldb_event)
95  broadcaster.reset(lldb_event->GetBroadcaster(), false);
96  return LLDB_RECORD_RESULT(broadcaster);
97 }
98 
99 const char *SBEvent::GetBroadcasterClass() const {
101 
102  const Event *lldb_event = get();
103  if (lldb_event)
104  return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
105  else
106  return "unknown class";
107 }
108 
111  (const lldb::SBBroadcaster *), broadcaster);
112 
113  if (broadcaster)
114  return BroadcasterMatchesRef(*broadcaster);
115  return false;
116 }
117 
120  (const lldb::SBBroadcaster &), broadcaster);
121 
122  Event *lldb_event = get();
123  bool success = false;
124  if (lldb_event)
125  success = lldb_event->BroadcasterIs(broadcaster.get());
126 
127 
128  return success;
129 }
130 
133 
134  Event *lldb_event = get();
135  if (lldb_event)
136  lldb_event->Clear();
137 }
138 
139 EventSP &SBEvent::GetSP() const { return m_event_sp; }
140 
141 Event *SBEvent::get() const {
142  // There is a dangerous accessor call GetSharedPtr which can be used, so if
143  // we have anything valid in m_event_sp, we must use that since if it gets
144  // used by a function that puts something in there, then it won't update
145  // m_opaque_ptr...
146  if (m_event_sp)
147  m_opaque_ptr = m_event_sp.get();
148 
149  return m_opaque_ptr;
150 }
151 
152 void SBEvent::reset(EventSP &event_sp) {
153  m_event_sp = event_sp;
154  m_opaque_ptr = m_event_sp.get();
155 }
156 
157 void SBEvent::reset(Event *event_ptr) {
158  m_opaque_ptr = event_ptr;
159  m_event_sp.reset();
160 }
161 
162 bool SBEvent::IsValid() const {
164  return this->operator bool();
165 }
166 SBEvent::operator bool() const {
167  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent, operator bool);
168 
169  // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
170  // See comments in SBEvent::get()....
171  return SBEvent::get() != NULL;
172 }
173 
174 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
176  (const lldb::SBEvent &), event);
177 
178  return reinterpret_cast<const char *>(
179  EventDataBytes::GetBytesFromEvent(event.get()));
180 }
181 
182 bool SBEvent::GetDescription(SBStream &description) {
184  description);
185 
186  Stream &strm = description.ref();
187 
188  if (get()) {
189  m_opaque_ptr->Dump(&strm);
190  } else
191  strm.PutCString("No value");
192 
193  return true;
194 }
195 
196 bool SBEvent::GetDescription(SBStream &description) const {
198  description);
199 
200  Stream &strm = description.ref();
201 
202  if (get()) {
203  m_opaque_ptr->Dump(&strm);
204  } else
205  strm.PutCString("No value");
206 
207  return true;
208 }
209 
210 namespace lldb_private {
211 namespace repro {
212 
213 template <>
217  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (lldb::EventSP &));
221  SBEvent, operator=,(const lldb::SBEvent &));
222  LLDB_REGISTER_METHOD(const char *, SBEvent, GetDataFlavor, ());
225  ());
228  (const lldb::SBBroadcaster *));
230  (const lldb::SBBroadcaster &));
231  LLDB_REGISTER_METHOD(void, SBEvent, Clear, ());
233  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, operator bool, ());
235  (const lldb::SBEvent &));
238  (lldb::SBStream &));
239 }
240 
241 }
242 }
bool BroadcasterMatchesPtr(const lldb::SBBroadcaster *broadcaster)
Definition: SBEvent.cpp:109
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:224
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature,...)
lldb_private::Broadcaster * get() const
const char * GetBroadcasterClass() const
Definition: SBEvent.cpp:99
Broadcaster * GetBroadcaster() const
Definition: Event.h:208
lldb::EventSP & GetSP() const
Definition: SBEvent.cpp:139
lldb_private::Event * get() const
Definition: SBEvent.cpp:141
bool BroadcasterIs(Broadcaster *broadcaster)
Definition: Event.h:217
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
bool IsValid() const
Definition: SBEvent.cpp:162
uint32_t GetType() const
Definition: Event.h:204
virtual ConstString & GetBroadcasterClass() const
void Dump(Stream *s) const
Definition: Event.cpp:47
bool BroadcasterMatchesRef(const lldb::SBBroadcaster &broadcaster)
Definition: SBEvent.cpp:118
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
void RegisterMethods< SBEvent >(Registry &R)
Definition: SBEvent.cpp:214
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
void reset(lldb::EventSP &event_sp)
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
const char * GetDataFlavor()
Definition: SBEvent.cpp:63
void reset(lldb_private::Broadcaster *broadcaster, bool owns)
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
static const char * GetCStringFromEvent(const lldb::SBEvent &event)
Definition: SBEvent.cpp:174
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
EventData * GetData()
Definition: Event.h:198
Definition: SBAddress.h:15
lldb::SBBroadcaster GetBroadcaster() const
Definition: SBEvent.cpp:88
uint32_t GetType() const
Definition: SBEvent.cpp:75
void Clear()
Definition: SBEvent.cpp:131
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
const SBEvent & operator=(const lldb::SBEvent &rhs)
Definition: SBEvent.cpp:50
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
bool GetDescription(lldb::SBStream &description)
Definition: SBEvent.cpp:182
virtual ConstString GetFlavor() const =0
#define LLDB_RECORD_RESULT(Result)
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)