1 //===-- SBEvent.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/API/SBEvent.h" 11 #include "lldb/API/SBBroadcaster.h" 12 #include "lldb/API/SBStream.h" 13 14 #include "lldb/Core/Event.h" 15 #include "lldb/Core/Stream.h" 16 #include "lldb/Core/StreamFile.h" 17 #include "lldb/Core/ConstString.h" 18 #include "lldb/Target/Process.h" 19 #include "lldb/Breakpoint/Breakpoint.h" 20 #include "lldb/Interpreter/CommandInterpreter.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 26 SBEvent::SBEvent () : 27 m_event_sp (), 28 m_opaque_ptr (NULL) 29 { 30 } 31 32 SBEvent::SBEvent (uint32_t event_type, const char *cstr, uint32_t cstr_len) : 33 m_event_sp (new Event (event_type, new EventDataBytes (cstr, cstr_len))), 34 m_opaque_ptr (m_event_sp.get()) 35 { 36 } 37 38 SBEvent::SBEvent (EventSP &event_sp) : 39 m_event_sp (event_sp), 40 m_opaque_ptr (event_sp.get()) 41 { 42 } 43 44 SBEvent::SBEvent (Event *event_ptr) : 45 m_event_sp (), 46 m_opaque_ptr (event_ptr) 47 { 48 } 49 50 SBEvent::SBEvent (const SBEvent &rhs) : 51 m_event_sp (rhs.m_event_sp), 52 m_opaque_ptr (rhs.m_opaque_ptr) 53 { 54 55 } 56 57 const SBEvent & 58 SBEvent::operator = (const SBEvent &rhs) 59 { 60 if (this != &rhs) 61 { 62 m_event_sp = rhs.m_event_sp; 63 m_opaque_ptr = rhs.m_opaque_ptr; 64 } 65 return *this; 66 } 67 68 SBEvent::~SBEvent() 69 { 70 } 71 72 const char * 73 SBEvent::GetDataFlavor () 74 { 75 Event *lldb_event = get(); 76 if (lldb_event) 77 { 78 EventData *event_data = lldb_event->GetData(); 79 if (event_data) 80 return lldb_event->GetData()->GetFlavor().AsCString(); 81 } 82 return NULL; 83 } 84 85 uint32_t 86 SBEvent::GetType () const 87 { 88 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 89 90 const Event *lldb_event = get(); 91 uint32_t event_type = 0; 92 if (lldb_event) 93 event_type = lldb_event->GetType(); 94 95 if (log) 96 { 97 StreamString sstr; 98 if (lldb_event && lldb_event->GetBroadcaster() && lldb_event->GetBroadcaster()->GetEventNames(sstr, event_type, true)) 99 log->Printf ("SBEvent(%p)::GetType () => 0x%8.8x (%s)", 100 static_cast<void*>(get()), event_type, sstr.GetData()); 101 else 102 log->Printf ("SBEvent(%p)::GetType () => 0x%8.8x", 103 static_cast<void*>(get()), event_type); 104 105 } 106 107 return event_type; 108 } 109 110 SBBroadcaster 111 SBEvent::GetBroadcaster () const 112 { 113 SBBroadcaster broadcaster; 114 const Event *lldb_event = get(); 115 if (lldb_event) 116 broadcaster.reset (lldb_event->GetBroadcaster(), false); 117 return broadcaster; 118 } 119 120 const char * 121 SBEvent::GetBroadcasterClass () const 122 { 123 const Event *lldb_event = get(); 124 if (lldb_event) 125 return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString(); 126 else 127 return "unknown class"; 128 } 129 130 bool 131 SBEvent::BroadcasterMatchesPtr (const SBBroadcaster *broadcaster) 132 { 133 if (broadcaster) 134 return BroadcasterMatchesRef (*broadcaster); 135 return false; 136 } 137 138 bool 139 SBEvent::BroadcasterMatchesRef (const SBBroadcaster &broadcaster) 140 { 141 142 Event *lldb_event = get(); 143 bool success = false; 144 if (lldb_event) 145 success = lldb_event->BroadcasterIs (broadcaster.get()); 146 147 // For logging, this gets a little chatty so only enable this when verbose logging is on 148 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE)); 149 if (log) 150 log->Printf ("SBEvent(%p)::BroadcasterMatchesRef (SBBroadcaster(%p): %s) => %i", 151 static_cast<void*>(get()), 152 static_cast<void*>(broadcaster.get()), 153 broadcaster.GetName(), success); 154 155 return success; 156 } 157 158 void 159 SBEvent::Clear() 160 { 161 Event *lldb_event = get(); 162 if (lldb_event) 163 lldb_event->Clear(); 164 } 165 166 EventSP & 167 SBEvent::GetSP () const 168 { 169 return m_event_sp; 170 } 171 172 Event * 173 SBEvent::get() const 174 { 175 // There is a dangerous accessor call GetSharedPtr which can be used, so if 176 // we have anything valid in m_event_sp, we must use that since if it gets 177 // used by a function that puts something in there, then it won't update 178 // m_opaque_ptr... 179 if (m_event_sp) 180 m_opaque_ptr = m_event_sp.get(); 181 182 return m_opaque_ptr; 183 } 184 185 void 186 SBEvent::reset (EventSP &event_sp) 187 { 188 m_event_sp = event_sp; 189 m_opaque_ptr = m_event_sp.get(); 190 } 191 192 void 193 SBEvent::reset (Event* event_ptr) 194 { 195 m_opaque_ptr = event_ptr; 196 m_event_sp.reset(); 197 } 198 199 bool 200 SBEvent::IsValid() const 201 { 202 // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() 203 // accessor. See comments in SBEvent::get().... 204 return SBEvent::get() != NULL; 205 206 } 207 208 const char * 209 SBEvent::GetCStringFromEvent (const SBEvent &event) 210 { 211 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 212 213 if (log) 214 log->Printf ("SBEvent(%p)::GetCStringFromEvent () => \"%s\"", 215 static_cast<void*>(event.get()), 216 reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event.get()))); 217 218 return reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event.get())); 219 } 220 221 222 bool 223 SBEvent::GetDescription (SBStream &description) 224 { 225 Stream &strm = description.ref(); 226 227 if (get()) 228 { 229 m_opaque_ptr->Dump (&strm); 230 } 231 else 232 strm.PutCString ("No value"); 233 234 return true; 235 } 236 237 bool 238 SBEvent::GetDescription (SBStream &description) const 239 { 240 Stream &strm = description.ref(); 241 242 if (get()) 243 { 244 m_opaque_ptr->Dump (&strm); 245 } 246 else 247 strm.PutCString ("No value"); 248 249 return true; 250 } 251