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 22 using namespace lldb; 23 using namespace lldb_private; 24 25 SBEvent::SBEvent() : m_event_sp(), m_opaque_ptr(NULL) { 26 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEvent); 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()) { 32 LLDB_RECORD_CONSTRUCTOR(SBEvent, (uint32_t, const char *, uint32_t), 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) { 42 LLDB_RECORD_CONSTRUCTOR(SBEvent, (lldb_private::Event *), event_ptr); 43 } 44 45 SBEvent::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 50 const 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 *this; 59 } 60 61 SBEvent::~SBEvent() {} 62 63 const 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 NULL; 73 } 74 75 uint32_t SBEvent::GetType() const { 76 LLDB_RECORD_METHOD_CONST_NO_ARGS(uint32_t, SBEvent, GetType); 77 78 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 79 80 const Event *lldb_event = get(); 81 uint32_t event_type = 0; 82 if (lldb_event) 83 event_type = lldb_event->GetType(); 84 85 if (log) { 86 StreamString sstr; 87 if (lldb_event && lldb_event->GetBroadcaster() && 88 lldb_event->GetBroadcaster()->GetEventNames(sstr, event_type, true)) 89 log->Printf("SBEvent(%p)::GetType () => 0x%8.8x (%s)", 90 static_cast<void *>(get()), event_type, sstr.GetData()); 91 else 92 log->Printf("SBEvent(%p)::GetType () => 0x%8.8x", 93 static_cast<void *>(get()), event_type); 94 } 95 96 return event_type; 97 } 98 99 SBBroadcaster SBEvent::GetBroadcaster() const { 100 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::SBBroadcaster, SBEvent, 101 GetBroadcaster); 102 103 SBBroadcaster broadcaster; 104 const Event *lldb_event = get(); 105 if (lldb_event) 106 broadcaster.reset(lldb_event->GetBroadcaster(), false); 107 return LLDB_RECORD_RESULT(broadcaster); 108 } 109 110 const char *SBEvent::GetBroadcasterClass() const { 111 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBEvent, GetBroadcasterClass); 112 113 const Event *lldb_event = get(); 114 if (lldb_event) 115 return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString(); 116 else 117 return "unknown class"; 118 } 119 120 bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) { 121 LLDB_RECORD_METHOD(bool, SBEvent, BroadcasterMatchesPtr, 122 (const lldb::SBBroadcaster *), broadcaster); 123 124 if (broadcaster) 125 return BroadcasterMatchesRef(*broadcaster); 126 return false; 127 } 128 129 bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) { 130 LLDB_RECORD_METHOD(bool, SBEvent, BroadcasterMatchesRef, 131 (const lldb::SBBroadcaster &), broadcaster); 132 133 Event *lldb_event = get(); 134 bool success = false; 135 if (lldb_event) 136 success = lldb_event->BroadcasterIs(broadcaster.get()); 137 138 // For logging, this gets a little chatty so only enable this when verbose 139 // logging is on 140 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 141 LLDB_LOGV(log, "({0}) (SBBroadcaster({1}): {2}) => {3}", get(), 142 broadcaster.get(), broadcaster.GetName(), success); 143 144 return success; 145 } 146 147 void SBEvent::Clear() { 148 LLDB_RECORD_METHOD_NO_ARGS(void, SBEvent, Clear); 149 150 Event *lldb_event = get(); 151 if (lldb_event) 152 lldb_event->Clear(); 153 } 154 155 EventSP &SBEvent::GetSP() const { return m_event_sp; } 156 157 Event *SBEvent::get() const { 158 // There is a dangerous accessor call GetSharedPtr which can be used, so if 159 // we have anything valid in m_event_sp, we must use that since if it gets 160 // used by a function that puts something in there, then it won't update 161 // m_opaque_ptr... 162 if (m_event_sp) 163 m_opaque_ptr = m_event_sp.get(); 164 165 return m_opaque_ptr; 166 } 167 168 void SBEvent::reset(EventSP &event_sp) { 169 m_event_sp = event_sp; 170 m_opaque_ptr = m_event_sp.get(); 171 } 172 173 void SBEvent::reset(Event *event_ptr) { 174 m_opaque_ptr = event_ptr; 175 m_event_sp.reset(); 176 } 177 178 bool SBEvent::IsValid() const { 179 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBEvent, IsValid); 180 181 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor. 182 // See comments in SBEvent::get().... 183 return SBEvent::get() != NULL; 184 } 185 186 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) { 187 LLDB_RECORD_STATIC_METHOD(const char *, SBEvent, GetCStringFromEvent, 188 (const lldb::SBEvent &), event); 189 190 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 191 192 if (log) 193 log->Printf("SBEvent(%p)::GetCStringFromEvent () => \"%s\"", 194 static_cast<void *>(event.get()), 195 reinterpret_cast<const char *>( 196 EventDataBytes::GetBytesFromEvent(event.get()))); 197 198 return reinterpret_cast<const char *>( 199 EventDataBytes::GetBytesFromEvent(event.get())); 200 } 201 202 bool SBEvent::GetDescription(SBStream &description) { 203 LLDB_RECORD_METHOD(bool, SBEvent, GetDescription, (lldb::SBStream &), 204 description); 205 206 Stream &strm = description.ref(); 207 208 if (get()) { 209 m_opaque_ptr->Dump(&strm); 210 } else 211 strm.PutCString("No value"); 212 213 return true; 214 } 215 216 bool SBEvent::GetDescription(SBStream &description) const { 217 LLDB_RECORD_METHOD_CONST(bool, SBEvent, GetDescription, (lldb::SBStream &), 218 description); 219 220 Stream &strm = description.ref(); 221 222 if (get()) { 223 m_opaque_ptr->Dump(&strm); 224 } else 225 strm.PutCString("No value"); 226 227 return true; 228 } 229