LLDB  mainline
SBEvent.cpp
Go to the documentation of this file.
1 //===-- SBEvent.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/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 
26 
27 SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
28  : m_event_sp(new Event(event_type, new EventDataBytes(cstr, cstr_len))),
29  m_opaque_ptr(m_event_sp.get()) {
31  event_type, cstr, cstr_len);
32 }
33 
34 SBEvent::SBEvent(EventSP &event_sp)
35  : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {
36  LLDB_RECORD_CONSTRUCTOR(SBEvent, (lldb::EventSP &), event_sp);
37 }
38 
39 SBEvent::SBEvent(Event *event_ptr) : m_event_sp(), m_opaque_ptr(event_ptr) {
41 }
42 
44  : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
46 }
47 
48 const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
50  SBEvent, operator=,(const lldb::SBEvent &), rhs);
51 
52  if (this != &rhs) {
53  m_event_sp = rhs.m_event_sp;
55  }
56  return LLDB_RECORD_RESULT(*this);
57 }
58 
59 SBEvent::~SBEvent() = default;
60 
61 const char *SBEvent::GetDataFlavor() {
63 
64  Event *lldb_event = get();
65  if (lldb_event) {
66  EventData *event_data = lldb_event->GetData();
67  if (event_data)
68  return lldb_event->GetData()->GetFlavor().AsCString();
69  }
70  return nullptr;
71 }
72 
75 
76 
77  const Event *lldb_event = get();
78  uint32_t event_type = 0;
79  if (lldb_event)
80  event_type = lldb_event->GetType();
81 
82 
83  return event_type;
84 }
85 
89 
90  SBBroadcaster broadcaster;
91  const Event *lldb_event = get();
92  if (lldb_event)
93  broadcaster.reset(lldb_event->GetBroadcaster(), false);
94  return LLDB_RECORD_RESULT(broadcaster);
95 }
96 
97 const char *SBEvent::GetBroadcasterClass() const {
99 
100  const Event *lldb_event = get();
101  if (lldb_event)
102  return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
103  else
104  return "unknown class";
105 }
106 
109  (const lldb::SBBroadcaster *), broadcaster);
110 
111  if (broadcaster)
112  return BroadcasterMatchesRef(*broadcaster);
113  return false;
114 }
115 
118  (const lldb::SBBroadcaster &), broadcaster);
119 
120  Event *lldb_event = get();
121  bool success = false;
122  if (lldb_event)
123  success = lldb_event->BroadcasterIs(broadcaster.get());
124 
125 
126  return success;
127 }
128 
131 
132  Event *lldb_event = get();
133  if (lldb_event)
134  lldb_event->Clear();
135 }
136 
137 EventSP &SBEvent::GetSP() const { return m_event_sp; }
138 
139 Event *SBEvent::get() const {
140  // There is a dangerous accessor call GetSharedPtr which can be used, so if
141  // we have anything valid in m_event_sp, we must use that since if it gets
142  // used by a function that puts something in there, then it won't update
143  // m_opaque_ptr...
144  if (m_event_sp)
145  m_opaque_ptr = m_event_sp.get();
146 
147  return m_opaque_ptr;
148 }
149 
150 void SBEvent::reset(EventSP &event_sp) {
151  m_event_sp = event_sp;
152  m_opaque_ptr = m_event_sp.get();
153 }
154 
155 void SBEvent::reset(Event *event_ptr) {
156  m_opaque_ptr = event_ptr;
157  m_event_sp.reset();
158 }
159 
160 bool SBEvent::IsValid() const {
162  return this->operator bool();
163 }
164 SBEvent::operator bool() const {
165  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent, operator bool);
166 
167  // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
168  // See comments in SBEvent::get()....
169  return SBEvent::get() != nullptr;
170 }
171 
172 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
174  (const lldb::SBEvent &), event);
175 
176  return static_cast<const char *>(
178 }
179 
180 bool SBEvent::GetDescription(SBStream &description) {
182  description);
183 
184  Stream &strm = description.ref();
185 
186  if (get()) {
187  m_opaque_ptr->Dump(&strm);
188  } else
189  strm.PutCString("No value");
190 
191  return true;
192 }
193 
194 bool SBEvent::GetDescription(SBStream &description) const {
196  description);
197 
198  Stream &strm = description.ref();
199 
200  if (get()) {
201  m_opaque_ptr->Dump(&strm);
202  } else
203  strm.PutCString("No value");
204 
205  return true;
206 }
207 
208 namespace lldb_private {
209 namespace repro {
210 
211 template <>
215  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (lldb::EventSP &));
219  SBEvent, operator=,(const lldb::SBEvent &));
220  LLDB_REGISTER_METHOD(const char *, SBEvent, GetDataFlavor, ());
223  ());
224  LLDB_REGISTER_METHOD_CONST(const char *, SBEvent, GetBroadcasterClass, ());
225  LLDB_REGISTER_METHOD(bool, SBEvent, BroadcasterMatchesPtr,
226  (const lldb::SBBroadcaster *));
227  LLDB_REGISTER_METHOD(bool, SBEvent, BroadcasterMatchesRef,
228  (const lldb::SBBroadcaster &));
229  LLDB_REGISTER_METHOD(void, SBEvent, Clear, ());
230  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, IsValid, ());
231  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, operator bool, ());
232  LLDB_REGISTER_STATIC_METHOD(const char *, SBEvent, GetCStringFromEvent,
233  (const lldb::SBEvent &));
234  LLDB_REGISTER_METHOD(bool, SBEvent, GetDescription, (lldb::SBStream &));
235  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, GetDescription,
236  (lldb::SBStream &));
237 }
238 
239 }
240 }
lldb::SBEvent::get
lldb_private::Event * get() const
Definition: SBEvent.cpp:139
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
lldb_private::EventData::GetFlavor
virtual ConstString GetFlavor() const =0
lldb_private::Event
Definition: Event.h:180
lldb_private::Event::Clear
void Clear()
Definition: Event.h:228
lldb::SBEvent::m_event_sp
lldb::EventSP m_event_sp
Definition: SBEvent.h:81
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb_private::Broadcaster::GetBroadcasterClass
virtual ConstString & GetBroadcasterClass() const
This needs to be filled in if you are going to register the broadcaster with the broadcaster manager ...
Definition: Broadcaster.cpp:308
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb_private::EventData
Definition: Event.h:36
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb::SBEvent::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBEvent.cpp:180
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:193
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
lldb::SBEvent
Definition: SBEvent.h:21
lldb_private::Stream
Definition: Stream.h:28
SBReproducerPrivate.h
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBEvent::GetCStringFromEvent
static const char * GetCStringFromEvent(const lldb::SBEvent &event)
Definition: SBEvent.cpp:172
Process.h
lldb::SBEvent::operator=
const SBEvent & operator=(const lldb::SBEvent &rhs)
Definition: SBEvent.cpp:48
lldb::SBEvent::BroadcasterMatchesRef
bool BroadcasterMatchesRef(const lldb::SBBroadcaster &broadcaster)
Definition: SBEvent.cpp:116
lldb_private::Event::GetType
uint32_t GetType() const
Definition: Event.h:206
lldb_private::Event::GetData
EventData * GetData()
Definition: Event.h:200
lldb::SBEvent::m_opaque_ptr
lldb_private::Event * m_opaque_ptr
Definition: SBEvent.h:82
SBStream.h
lldb_private::Event::Dump
void Dump(Stream *s) const
Definition: Event.cpp:47
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb::SBBroadcaster::reset
void reset(lldb_private::Broadcaster *broadcaster, bool owns)
Definition: SBBroadcaster.cpp:131
lldb::SBEvent::GetSP
lldb::EventSP & GetSP() const
Definition: SBEvent.cpp:137
lldb::SBEvent::reset
void reset(lldb::EventSP &event_sp)
SBBroadcaster.h
lldb::SBEvent::SBEvent
SBEvent()
Definition: SBEvent.cpp:25
lldb_private::EventDataBytes
Definition: Event.h:56
LLDB_REGISTER_STATIC_METHOD
#define LLDB_REGISTER_STATIC_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:103
Event.h
lldb::SBEvent::GetType
uint32_t GetType() const
Definition: SBEvent.cpp:73
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
StreamFile.h
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
uint32_t
lldb::SBBroadcaster::get
lldb_private::Broadcaster * get() const
Definition: SBBroadcaster.cpp:129
lldb_private::EventDataBytes::GetBytesFromEvent
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition: Event.cpp:158
lldb::SBEvent::GetBroadcasterClass
const char * GetBroadcasterClass() const
Definition: SBEvent.cpp:97
lldb_private::Event::BroadcasterIs
bool BroadcasterIs(Broadcaster *broadcaster)
Definition: Event.h:219
lldb::SBEvent::GetBroadcaster
lldb::SBBroadcaster GetBroadcaster() const
Definition: SBEvent.cpp:86
lldb::SBEvent::BroadcasterMatchesPtr
bool BroadcasterMatchesPtr(const lldb::SBBroadcaster *broadcaster)
Definition: SBEvent.cpp:107
SBEvent.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBEvent::GetDataFlavor
const char * GetDataFlavor()
Definition: SBEvent.cpp:61
lldb_private::Event::GetBroadcaster
Broadcaster * GetBroadcaster() const
Definition: Event.h:210
Breakpoint.h
lldb::SBEvent::IsValid
bool IsValid() const
Definition: SBEvent.cpp:160
CommandInterpreter.h
lldb_private::repro::RegisterMethods< SBEvent >
void RegisterMethods< SBEvent >(Registry &R)
Definition: SBEvent.cpp:212
ConstString.h
lldb::SBBroadcaster
Definition: SBBroadcaster.h:16
LLDB_RECORD_STATIC_METHOD
#define LLDB_RECORD_STATIC_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:171
Stream.h
lldb::SBEvent::~SBEvent
~SBEvent()
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBStream
Definition: SBStream.h:18
LLDB_RECORD_METHOD_CONST
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:161
lldb::SBEvent::Clear
void Clear()
Definition: SBEvent.cpp:129
lldb
Definition: SBAddress.h:15
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93