1cfca06d7SDimitry Andric //===-- SBEvent.cpp -------------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/API/SBEvent.h"
10f034231aSEd Maste #include "lldb/API/SBBroadcaster.h"
11f034231aSEd Maste #include "lldb/API/SBStream.h"
126f8fc217SDimitry Andric #include "lldb/Utility/Instrumentation.h"
13f034231aSEd Maste
1414f1b3e8SDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
15f034231aSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
1614f1b3e8SDimitry Andric #include "lldb/Target/Process.h"
1774a628f7SDimitry Andric #include "lldb/Utility/ConstString.h"
1894994d37SDimitry Andric #include "lldb/Utility/Event.h"
1974a628f7SDimitry Andric #include "lldb/Utility/Stream.h"
20f034231aSEd Maste
21f034231aSEd Maste using namespace lldb;
22f034231aSEd Maste using namespace lldb_private;
23f034231aSEd Maste
SBEvent()246f8fc217SDimitry Andric SBEvent::SBEvent() { LLDB_INSTRUMENT_VA(this); }
25f034231aSEd Maste
SBEvent(uint32_t event_type,const char * cstr,uint32_t cstr_len)2614f1b3e8SDimitry Andric SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
27ac9a064cSDimitry Andric : m_event_sp(new Event(
28ac9a064cSDimitry Andric event_type, new EventDataBytes(llvm::StringRef(cstr, cstr_len)))),
295f29bb8aSDimitry Andric m_opaque_ptr(m_event_sp.get()) {
306f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, event_type, cstr, cstr_len);
315f29bb8aSDimitry Andric }
32f034231aSEd Maste
SBEvent(EventSP & event_sp)3314f1b3e8SDimitry Andric SBEvent::SBEvent(EventSP &event_sp)
345f29bb8aSDimitry Andric : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {
356f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, event_sp);
365f29bb8aSDimitry Andric }
37f034231aSEd Maste
SBEvent(Event * event_ptr)386f8fc217SDimitry Andric SBEvent::SBEvent(Event *event_ptr) : m_opaque_ptr(event_ptr) {
396f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, event_ptr);
405f29bb8aSDimitry Andric }
41f034231aSEd Maste
SBEvent(const SBEvent & rhs)4214f1b3e8SDimitry Andric SBEvent::SBEvent(const SBEvent &rhs)
435f29bb8aSDimitry Andric : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {
446f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
455f29bb8aSDimitry Andric }
46205afe67SEd Maste
operator =(const SBEvent & rhs)4714f1b3e8SDimitry Andric const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
486f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, rhs);
495f29bb8aSDimitry Andric
5014f1b3e8SDimitry Andric if (this != &rhs) {
51f034231aSEd Maste m_event_sp = rhs.m_event_sp;
52f034231aSEd Maste m_opaque_ptr = rhs.m_opaque_ptr;
53f034231aSEd Maste }
546f8fc217SDimitry Andric return *this;
55f034231aSEd Maste }
56f034231aSEd Maste
57cfca06d7SDimitry Andric SBEvent::~SBEvent() = default;
58f034231aSEd Maste
GetDataFlavor()5914f1b3e8SDimitry Andric const char *SBEvent::GetDataFlavor() {
606f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
615f29bb8aSDimitry Andric
62f034231aSEd Maste Event *lldb_event = get();
6314f1b3e8SDimitry Andric if (lldb_event) {
64f034231aSEd Maste EventData *event_data = lldb_event->GetData();
65f034231aSEd Maste if (event_data)
667fa27ce4SDimitry Andric return ConstString(lldb_event->GetData()->GetFlavor()).GetCString();
67f034231aSEd Maste }
685f29bb8aSDimitry Andric return nullptr;
69f034231aSEd Maste }
70f034231aSEd Maste
GetType() const7114f1b3e8SDimitry Andric uint32_t SBEvent::GetType() const {
726f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
73f034231aSEd Maste
74f034231aSEd Maste const Event *lldb_event = get();
75f034231aSEd Maste uint32_t event_type = 0;
76f034231aSEd Maste if (lldb_event)
77f034231aSEd Maste event_type = lldb_event->GetType();
78f034231aSEd Maste
79f034231aSEd Maste
80f034231aSEd Maste return event_type;
81f034231aSEd Maste }
82f034231aSEd Maste
GetBroadcaster() const8314f1b3e8SDimitry Andric SBBroadcaster SBEvent::GetBroadcaster() const {
846f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
855f29bb8aSDimitry Andric
86f034231aSEd Maste SBBroadcaster broadcaster;
87f034231aSEd Maste const Event *lldb_event = get();
88f034231aSEd Maste if (lldb_event)
89f034231aSEd Maste broadcaster.reset(lldb_event->GetBroadcaster(), false);
906f8fc217SDimitry Andric return broadcaster;
91f034231aSEd Maste }
92f034231aSEd Maste
GetBroadcasterClass() const9314f1b3e8SDimitry Andric const char *SBEvent::GetBroadcasterClass() const {
946f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
955f29bb8aSDimitry Andric
96f034231aSEd Maste const Event *lldb_event = get();
97f034231aSEd Maste if (lldb_event)
98ac9a064cSDimitry Andric return ConstString(lldb_event->GetBroadcaster()->GetBroadcasterClass())
99ac9a064cSDimitry Andric .AsCString();
100f034231aSEd Maste else
101f034231aSEd Maste return "unknown class";
102f034231aSEd Maste }
103f034231aSEd Maste
BroadcasterMatchesPtr(const SBBroadcaster * broadcaster)10414f1b3e8SDimitry Andric bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {
1056f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, broadcaster);
1065f29bb8aSDimitry Andric
107f034231aSEd Maste if (broadcaster)
108f034231aSEd Maste return BroadcasterMatchesRef(*broadcaster);
109f034231aSEd Maste return false;
110f034231aSEd Maste }
111f034231aSEd Maste
BroadcasterMatchesRef(const SBBroadcaster & broadcaster)11214f1b3e8SDimitry Andric bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {
1136f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, broadcaster);
114f034231aSEd Maste
115f034231aSEd Maste Event *lldb_event = get();
116f034231aSEd Maste bool success = false;
117f034231aSEd Maste if (lldb_event)
118f034231aSEd Maste success = lldb_event->BroadcasterIs(broadcaster.get());
119f034231aSEd Maste
120f034231aSEd Maste
121f034231aSEd Maste return success;
122f034231aSEd Maste }
123f034231aSEd Maste
Clear()12414f1b3e8SDimitry Andric void SBEvent::Clear() {
1256f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1265f29bb8aSDimitry Andric
127f034231aSEd Maste Event *lldb_event = get();
128f034231aSEd Maste if (lldb_event)
129f034231aSEd Maste lldb_event->Clear();
130f034231aSEd Maste }
131f034231aSEd Maste
GetSP() const13214f1b3e8SDimitry Andric EventSP &SBEvent::GetSP() const { return m_event_sp; }
133f034231aSEd Maste
get() const13414f1b3e8SDimitry Andric Event *SBEvent::get() const {
135f034231aSEd Maste // There is a dangerous accessor call GetSharedPtr which can be used, so if
136f034231aSEd Maste // we have anything valid in m_event_sp, we must use that since if it gets
137f034231aSEd Maste // used by a function that puts something in there, then it won't update
138f034231aSEd Maste // m_opaque_ptr...
139f034231aSEd Maste if (m_event_sp)
140f034231aSEd Maste m_opaque_ptr = m_event_sp.get();
141f034231aSEd Maste
142f034231aSEd Maste return m_opaque_ptr;
143f034231aSEd Maste }
144f034231aSEd Maste
reset(EventSP & event_sp)14514f1b3e8SDimitry Andric void SBEvent::reset(EventSP &event_sp) {
146f034231aSEd Maste m_event_sp = event_sp;
147f034231aSEd Maste m_opaque_ptr = m_event_sp.get();
148f034231aSEd Maste }
149f034231aSEd Maste
reset(Event * event_ptr)15014f1b3e8SDimitry Andric void SBEvent::reset(Event *event_ptr) {
151f034231aSEd Maste m_opaque_ptr = event_ptr;
152f034231aSEd Maste m_event_sp.reset();
153f034231aSEd Maste }
154f034231aSEd Maste
IsValid() const15514f1b3e8SDimitry Andric bool SBEvent::IsValid() const {
1566f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1575f29bb8aSDimitry Andric return this->operator bool();
1585f29bb8aSDimitry Andric }
operator bool() const1595f29bb8aSDimitry Andric SBEvent::operator bool() const {
1606f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this);
1615f29bb8aSDimitry Andric
162f73363f1SDimitry Andric // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
163f73363f1SDimitry Andric // See comments in SBEvent::get()....
1645f29bb8aSDimitry Andric return SBEvent::get() != nullptr;
165f034231aSEd Maste }
166f034231aSEd Maste
GetCStringFromEvent(const SBEvent & event)16714f1b3e8SDimitry Andric const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
1686f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(event);
169f034231aSEd Maste
1707fa27ce4SDimitry Andric return ConstString(static_cast<const char *>(
1717fa27ce4SDimitry Andric EventDataBytes::GetBytesFromEvent(event.get())))
1727fa27ce4SDimitry Andric .GetCString();
173f034231aSEd Maste }
174f034231aSEd Maste
GetDescription(SBStream & description)17514f1b3e8SDimitry Andric bool SBEvent::GetDescription(SBStream &description) {
1766f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
1775f29bb8aSDimitry Andric
178f034231aSEd Maste Stream &strm = description.ref();
179f034231aSEd Maste
18014f1b3e8SDimitry Andric if (get()) {
181f034231aSEd Maste m_opaque_ptr->Dump(&strm);
18214f1b3e8SDimitry Andric } else
183f034231aSEd Maste strm.PutCString("No value");
184f034231aSEd Maste
185f034231aSEd Maste return true;
186f034231aSEd Maste }
187f034231aSEd Maste
GetDescription(SBStream & description) const18814f1b3e8SDimitry Andric bool SBEvent::GetDescription(SBStream &description) const {
1896f8fc217SDimitry Andric LLDB_INSTRUMENT_VA(this, description);
1905f29bb8aSDimitry Andric
191f034231aSEd Maste Stream &strm = description.ref();
192f034231aSEd Maste
19314f1b3e8SDimitry Andric if (get()) {
194f034231aSEd Maste m_opaque_ptr->Dump(&strm);
19514f1b3e8SDimitry Andric } else
196f034231aSEd Maste strm.PutCString("No value");
197f034231aSEd Maste
198f034231aSEd Maste return true;
199f034231aSEd Maste }
200