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"
11#include "lldb/API/SBStream.h"
13
16#include "lldb/Target/Process.h"
18#include "lldb/Utility/Event.h"
19#include "lldb/Utility/Stream.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
25
26SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
27 : m_event_sp(new Event(
28 event_type, new EventDataBytes(llvm::StringRef(cstr, cstr_len)))),
29 m_opaque_ptr(m_event_sp.get()) {
30 LLDB_INSTRUMENT_VA(this, event_type, cstr, cstr_len);
31}
32
34 : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {
35 LLDB_INSTRUMENT_VA(this, event_sp);
36}
37
38SBEvent::SBEvent(Event *event_ptr) : m_opaque_ptr(event_ptr) {
39 LLDB_INSTRUMENT_VA(this, event_ptr);
40}
41
43 : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
44 LLDB_INSTRUMENT_VA(this, rhs);
45}
46
48 LLDB_INSTRUMENT_VA(this, rhs);
49
50 if (this != &rhs) {
53 }
54 return *this;
55}
56
57SBEvent::~SBEvent() = default;
58
61
62 Event *lldb_event = get();
63 if (lldb_event) {
64 EventData *event_data = lldb_event->GetData();
65 if (event_data)
66 return ConstString(lldb_event->GetData()->GetFlavor()).GetCString();
67 }
68 return nullptr;
69}
70
71uint32_t SBEvent::GetType() const {
73
74 const Event *lldb_event = get();
75 uint32_t event_type = 0;
76 if (lldb_event)
77 event_type = lldb_event->GetType();
78
79
80 return event_type;
81}
82
85
86 SBBroadcaster broadcaster;
87 const Event *lldb_event = get();
88 if (lldb_event)
89 broadcaster.reset(lldb_event->GetBroadcaster(), false);
90 return broadcaster;
91}
92
93const char *SBEvent::GetBroadcasterClass() const {
95
96 const Event *lldb_event = get();
97 if (lldb_event)
98 return ConstString(lldb_event->GetBroadcaster()->GetBroadcasterClass())
99 .AsCString();
100 else
101 return "unknown class";
102}
103
105 LLDB_INSTRUMENT_VA(this, broadcaster);
106
107 if (broadcaster)
108 return BroadcasterMatchesRef(*broadcaster);
109 return false;
110}
111
113 LLDB_INSTRUMENT_VA(this, broadcaster);
114
115 Event *lldb_event = get();
116 bool success = false;
117 if (lldb_event)
118 success = lldb_event->BroadcasterIs(broadcaster.get());
119
120
121 return success;
122}
123
125 LLDB_INSTRUMENT_VA(this);
126
127 Event *lldb_event = get();
128 if (lldb_event)
129 lldb_event->Clear();
130}
131
133
135 // There is a dangerous accessor call GetSharedPtr which can be used, so if
136 // we have anything valid in m_event_sp, we must use that since if it gets
137 // used by a function that puts something in there, then it won't update
138 // m_opaque_ptr...
139 if (m_event_sp)
140 m_opaque_ptr = m_event_sp.get();
141
142 return m_opaque_ptr;
143}
144
145void SBEvent::reset(EventSP &event_sp) {
146 m_event_sp = event_sp;
147 m_opaque_ptr = m_event_sp.get();
148}
149
150void SBEvent::reset(Event *event_ptr) {
151 m_opaque_ptr = event_ptr;
152 m_event_sp.reset();
153}
154
155bool SBEvent::IsValid() const {
156 LLDB_INSTRUMENT_VA(this);
157 return this->operator bool();
158}
159SBEvent::operator bool() const {
160 LLDB_INSTRUMENT_VA(this);
161
162 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
163 // See comments in SBEvent::get()....
164 return SBEvent::get() != nullptr;
165}
166
167const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
168 LLDB_INSTRUMENT_VA(event);
169
170 return ConstString(static_cast<const char *>(
172 .GetCString();
173}
174
176 LLDB_INSTRUMENT_VA(this, description);
177
178 Stream &strm = description.ref();
179
180 if (get()) {
181 m_opaque_ptr->Dump(&strm);
182 } else
183 strm.PutCString("No value");
184
185 return true;
186}
187
188bool SBEvent::GetDescription(SBStream &description) const {
189 LLDB_INSTRUMENT_VA(this, description);
190
191 Stream &strm = description.ref();
192
193 if (get()) {
194 m_opaque_ptr->Dump(&strm);
195 } else
196 strm.PutCString("No value");
197
198 return true;
199}
#define LLDB_INSTRUMENT_VA(...)
void reset(lldb_private::Broadcaster *broadcaster, bool owns)
lldb_private::Broadcaster * get() const
lldb_private::Event * m_opaque_ptr
Definition: SBEvent.h:94
uint32_t GetType() const
Definition: SBEvent.cpp:71
void Clear()
Definition: SBEvent.cpp:124
const char * GetDataFlavor()
Definition: SBEvent.cpp:59
bool GetDescription(lldb::SBStream &description)
Definition: SBEvent.cpp:175
lldb::EventSP m_event_sp
Definition: SBEvent.h:93
lldb_private::Event * get() const
Definition: SBEvent.cpp:134
bool BroadcasterMatchesPtr(const lldb::SBBroadcaster *broadcaster)
Definition: SBEvent.cpp:104
lldb::SBBroadcaster GetBroadcaster() const
Definition: SBEvent.cpp:83
bool BroadcasterMatchesRef(const lldb::SBBroadcaster &broadcaster)
Definition: SBEvent.cpp:112
const char * GetBroadcasterClass() const
Definition: SBEvent.cpp:93
void reset(lldb::EventSP &event_sp)
Definition: SBEvent.cpp:145
bool IsValid() const
Definition: SBEvent.cpp:155
static const char * GetCStringFromEvent(const lldb::SBEvent &event)
Definition: SBEvent.cpp:167
const SBEvent & operator=(const lldb::SBEvent &rhs)
Definition: SBEvent.cpp:47
lldb::EventSP & GetSP() const
Definition: SBEvent.cpp:132
lldb_private::Stream & ref()
Definition: SBStream.cpp:177
virtual llvm::StringRef GetBroadcasterClass() const
This needs to be filled in if you are going to register the broadcaster with the broadcaster manager ...
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
static const void * GetBytesFromEvent(const Event *event_ptr)
Definition: Event.cpp:146
virtual llvm::StringRef GetFlavor() const =0
EventData * GetData()
Definition: Event.h:200
bool BroadcasterIs(Broadcaster *broadcaster)
Definition: Event.h:219
uint32_t GetType() const
Definition: Event.h:206
void Dump(Stream *s) const
Definition: Event.cpp:50
Broadcaster * GetBroadcaster() const
Definition: Event.h:210
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::Event > EventSP
Definition: lldb-forward.h:342
Definition: Debugger.h:54