Event.cpp revision d168690e51f9020b926d3d0d57dc9a2cfb2095a8
1//===-- Event.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// C Includes 11// C++ Includes 12// Other libraries and framework includes 13// Project includes 14#include "lldb/Core/Event.h" 15#include "lldb/Core/Broadcaster.h" 16#include "lldb/Core/DataExtractor.h" 17#include "lldb/Core/Log.h" 18#include "lldb/Core/State.h" 19#include "lldb/Core/Stream.h" 20#include "lldb/Target/Process.h" 21#include <algorithm> 22 23using namespace lldb; 24using namespace lldb_private; 25 26//---------------------------------------------------------------------- 27// Event constructor 28//---------------------------------------------------------------------- 29Event::Event (Broadcaster *broadcaster, uint32_t event_type, EventData *data) : 30 m_broadcaster (broadcaster), 31 m_type (event_type), 32 m_data_ap (data) 33{ 34} 35 36Event::Event(uint32_t event_type, EventData *data) : 37 m_broadcaster (NULL), // Set by the broadcaster when this event gets broadcast 38 m_type (event_type), 39 m_data_ap (data) 40{ 41} 42 43 44//---------------------------------------------------------------------- 45// Event destructor 46//---------------------------------------------------------------------- 47Event::~Event () 48{ 49} 50 51void 52Event::Dump (Stream *s) const 53{ 54 s->Printf("%p Event: broadcaster = %p, type = 0x%8.8x, data = ", this, m_broadcaster, m_type); 55 56 if (m_data_ap.get() == NULL) 57 s->Printf ("<NULL>"); 58 else 59 { 60 s->PutChar('{'); 61 m_data_ap->Dump (s); 62 s->PutChar('}'); 63 } 64} 65 66void 67Event::DoOnRemoval () 68{ 69 if (m_data_ap.get()) 70 m_data_ap->DoOnRemoval (this); 71} 72 73EventData::EventData() 74{ 75} 76 77EventData::~EventData() 78{ 79} 80 81void 82EventData::Dump (Stream *s) const 83{ 84 s->PutCString ("Generic Event Data"); 85} 86 87EventDataBytes::EventDataBytes () : 88 m_bytes() 89{ 90} 91 92EventDataBytes::EventDataBytes (const char *cstr) : 93 m_bytes() 94{ 95 SetBytesFromCString (cstr); 96} 97 98EventDataBytes::EventDataBytes (const void *src, size_t src_len) : 99 m_bytes() 100{ 101 SetBytes (src, src_len); 102} 103 104EventDataBytes::~EventDataBytes() 105{ 106} 107 108const ConstString & 109EventDataBytes::GetFlavorString () 110{ 111 static ConstString g_flavor ("EventDataBytes"); 112 return g_flavor; 113} 114 115const ConstString & 116EventDataBytes::GetFlavor () const 117{ 118 return EventDataBytes::GetFlavorString (); 119} 120 121void 122EventDataBytes::Dump (Stream *s) const 123{ 124 size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint); 125 if (num_printable_chars == m_bytes.size()) 126 { 127 s->Printf("\"%s\"", m_bytes.c_str()); 128 } 129 else if (m_bytes.size() > 0) 130 { 131 DataExtractor data; 132 data.SetData(&m_bytes[0], m_bytes.size(), eByteOrderHost); 133 data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0); 134 } 135} 136 137const void * 138EventDataBytes::GetBytes() const 139{ 140 if (m_bytes.empty()) 141 return NULL; 142 return &m_bytes[0]; 143} 144 145size_t 146EventDataBytes::GetByteSize() const 147{ 148 return m_bytes.size (); 149} 150 151void 152EventDataBytes::SetBytes (const void *src, size_t src_len) 153{ 154 if (src && src_len > 0) 155 m_bytes.assign ((const char *)src, src_len); 156 else 157 m_bytes.clear(); 158} 159 160void 161EventDataBytes::SetBytesFromCString (const char *cstr) 162{ 163 if (cstr && cstr[0]) 164 m_bytes.assign (cstr); 165 else 166 m_bytes.clear(); 167} 168 169 170const void * 171EventDataBytes::GetBytesFromEvent (const Event *event_ptr) 172{ 173 const EventDataBytes *e = GetEventDataFromEvent (event_ptr); 174 if (e) 175 return e->GetBytes(); 176 return NULL; 177} 178 179size_t 180EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr) 181{ 182 const EventDataBytes *e = GetEventDataFromEvent (event_ptr); 183 if (e) 184 return e->GetByteSize(); 185 return 0; 186} 187 188const EventDataBytes * 189EventDataBytes::GetEventDataFromEvent (const Event *event_ptr) 190{ 191 if (event_ptr) 192 { 193 const EventData *event_data = event_ptr->GetData(); 194 if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString()) 195 return static_cast <const EventDataBytes *> (event_data); 196 } 197 return NULL; 198} 199 200