SBEvent.cpp revision 360784
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
14#include "lldb/Breakpoint/Breakpoint.h"
15#include "lldb/Core/StreamFile.h"
16#include "lldb/Interpreter/CommandInterpreter.h"
17#include "lldb/Target/Process.h"
18#include "lldb/Utility/ConstString.h"
19#include "lldb/Utility/Event.h"
20#include "lldb/Utility/Stream.h"
21
22using namespace lldb;
23using namespace lldb_private;
24
25SBEvent::SBEvent() : m_event_sp(), m_opaque_ptr(nullptr) {
26  LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEvent);
27}
28
29SBEvent::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()) {
32  LLDB_RECORD_CONSTRUCTOR(SBEvent, (uint32_t, const char *, uint32_t),
33                          event_type, cstr, cstr_len);
34}
35
36SBEvent::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
41SBEvent::SBEvent(Event *event_ptr) : m_event_sp(), m_opaque_ptr(event_ptr) {
42  LLDB_RECORD_CONSTRUCTOR(SBEvent, (lldb_private::Event *), event_ptr);
43}
44
45SBEvent::SBEvent(const SBEvent &rhs)
46    : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
47  LLDB_RECORD_CONSTRUCTOR(SBEvent, (const lldb::SBEvent &), rhs);
48}
49
50const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
51  LLDB_RECORD_METHOD(const lldb::SBEvent &,
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
61SBEvent::~SBEvent() {}
62
63const char *SBEvent::GetDataFlavor() {
64  LLDB_RECORD_METHOD_NO_ARGS(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 nullptr;
73}
74
75uint32_t SBEvent::GetType() const {
76  LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBEvent, GetType);
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
88SBBroadcaster SBEvent::GetBroadcaster() const {
89  LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBBroadcaster, SBEvent,
90                                   GetBroadcaster);
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
99const char *SBEvent::GetBroadcasterClass() const {
100  LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBEvent, GetBroadcasterClass);
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
109bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {
110  LLDB_RECORD_METHOD(bool, SBEvent, BroadcasterMatchesPtr,
111                     (const lldb::SBBroadcaster *), broadcaster);
112
113  if (broadcaster)
114    return BroadcasterMatchesRef(*broadcaster);
115  return false;
116}
117
118bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {
119  LLDB_RECORD_METHOD(bool, SBEvent, BroadcasterMatchesRef,
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
131void SBEvent::Clear() {
132  LLDB_RECORD_METHOD_NO_ARGS(void, SBEvent, Clear);
133
134  Event *lldb_event = get();
135  if (lldb_event)
136    lldb_event->Clear();
137}
138
139EventSP &SBEvent::GetSP() const { return m_event_sp; }
140
141Event *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
152void SBEvent::reset(EventSP &event_sp) {
153  m_event_sp = event_sp;
154  m_opaque_ptr = m_event_sp.get();
155}
156
157void SBEvent::reset(Event *event_ptr) {
158  m_opaque_ptr = event_ptr;
159  m_event_sp.reset();
160}
161
162bool SBEvent::IsValid() const {
163  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent, IsValid);
164  return this->operator bool();
165}
166SBEvent::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() != nullptr;
172}
173
174const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
175  LLDB_RECORD_STATIC_METHOD(const char *, SBEvent, GetCStringFromEvent,
176                            (const lldb::SBEvent &), event);
177
178  return static_cast<const char *>(
179      EventDataBytes::GetBytesFromEvent(event.get()));
180}
181
182bool SBEvent::GetDescription(SBStream &description) {
183  LLDB_RECORD_METHOD(bool, SBEvent, GetDescription, (lldb::SBStream &),
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
196bool SBEvent::GetDescription(SBStream &description) const {
197  LLDB_RECORD_METHOD_CONST(bool, SBEvent, GetDescription, (lldb::SBStream &),
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
210namespace lldb_private {
211namespace repro {
212
213template <>
214void RegisterMethods<SBEvent>(Registry &R) {
215  LLDB_REGISTER_CONSTRUCTOR(SBEvent, ());
216  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (uint32_t, const char *, uint32_t));
217  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (lldb::EventSP &));
218  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (lldb_private::Event *));
219  LLDB_REGISTER_CONSTRUCTOR(SBEvent, (const lldb::SBEvent &));
220  LLDB_REGISTER_METHOD(const lldb::SBEvent &,
221                       SBEvent, operator=,(const lldb::SBEvent &));
222  LLDB_REGISTER_METHOD(const char *, SBEvent, GetDataFlavor, ());
223  LLDB_REGISTER_METHOD_CONST(uint32_t, SBEvent, GetType, ());
224  LLDB_REGISTER_METHOD_CONST(lldb::SBBroadcaster, SBEvent, GetBroadcaster,
225                             ());
226  LLDB_REGISTER_METHOD_CONST(const char *, SBEvent, GetBroadcasterClass, ());
227  LLDB_REGISTER_METHOD(bool, SBEvent, BroadcasterMatchesPtr,
228                       (const lldb::SBBroadcaster *));
229  LLDB_REGISTER_METHOD(bool, SBEvent, BroadcasterMatchesRef,
230                       (const lldb::SBBroadcaster &));
231  LLDB_REGISTER_METHOD(void, SBEvent, Clear, ());
232  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, IsValid, ());
233  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, operator bool, ());
234  LLDB_REGISTER_STATIC_METHOD(const char *, SBEvent, GetCStringFromEvent,
235                              (const lldb::SBEvent &));
236  LLDB_REGISTER_METHOD(bool, SBEvent, GetDescription, (lldb::SBStream &));
237  LLDB_REGISTER_METHOD_CONST(bool, SBEvent, GetDescription,
238                             (lldb::SBStream &));
239}
240
241}
242}
243