Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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" |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 20 | #include "lldb/Host/Endian.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/Process.h" |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace lldb; |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | //---------------------------------------------------------------------- |
| 28 | // Event constructor |
| 29 | //---------------------------------------------------------------------- |
| 30 | Event::Event (Broadcaster *broadcaster, uint32_t event_type, EventData *data) : |
| 31 | m_broadcaster (broadcaster), |
| 32 | m_type (event_type), |
| 33 | m_data_ap (data) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | Event::Event(uint32_t event_type, EventData *data) : |
| 38 | m_broadcaster (NULL), // Set by the broadcaster when this event gets broadcast |
| 39 | m_type (event_type), |
| 40 | m_data_ap (data) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | //---------------------------------------------------------------------- |
| 46 | // Event destructor |
| 47 | //---------------------------------------------------------------------- |
| 48 | Event::~Event () |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | Event::Dump (Stream *s) const |
| 54 | { |
Greg Clayton | 6a3efac | 2012-10-29 18:08:18 +0000 | [diff] [blame] | 55 | if (m_broadcaster) |
| 56 | { |
| 57 | StreamString event_name; |
| 58 | if (m_broadcaster->GetEventNames (event_name, m_type, false)) |
| 59 | s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 60 | static_cast<const void*>(this), |
| 61 | static_cast<void*>(m_broadcaster), |
Greg Clayton | 6a3efac | 2012-10-29 18:08:18 +0000 | [diff] [blame] | 62 | m_broadcaster->GetBroadcasterName().GetCString(), |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 63 | m_type, event_name.GetString().c_str()); |
Greg Clayton | 6a3efac | 2012-10-29 18:08:18 +0000 | [diff] [blame] | 64 | else |
| 65 | s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ", |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 66 | static_cast<const void*>(this), |
| 67 | static_cast<void*>(m_broadcaster), |
| 68 | m_broadcaster->GetBroadcasterName().GetCString(), m_type); |
Greg Clayton | 6a3efac | 2012-10-29 18:08:18 +0000 | [diff] [blame] | 69 | } |
| 70 | else |
Saleem Abdulrasool | 324a103 | 2014-04-04 04:06:10 +0000 | [diff] [blame] | 71 | s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ", |
| 72 | static_cast<const void*>(this), m_type); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
| 74 | if (m_data_ap.get() == NULL) |
| 75 | s->Printf ("<NULL>"); |
| 76 | else |
| 77 | { |
| 78 | s->PutChar('{'); |
| 79 | m_data_ap->Dump (s); |
| 80 | s->PutChar('}'); |
| 81 | } |
| 82 | } |
| 83 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | void |
| 85 | Event::DoOnRemoval () |
| 86 | { |
| 87 | if (m_data_ap.get()) |
| 88 | m_data_ap->DoOnRemoval (this); |
| 89 | } |
| 90 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 91 | EventData::EventData() |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | EventData::~EventData() |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | EventData::Dump (Stream *s) const |
| 101 | { |
| 102 | s->PutCString ("Generic Event Data"); |
| 103 | } |
| 104 | |
| 105 | EventDataBytes::EventDataBytes () : |
| 106 | m_bytes() |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | EventDataBytes::EventDataBytes (const char *cstr) : |
| 111 | m_bytes() |
| 112 | { |
| 113 | SetBytesFromCString (cstr); |
| 114 | } |
| 115 | |
| 116 | EventDataBytes::EventDataBytes (const void *src, size_t src_len) : |
| 117 | m_bytes() |
| 118 | { |
| 119 | SetBytes (src, src_len); |
| 120 | } |
| 121 | |
| 122 | EventDataBytes::~EventDataBytes() |
| 123 | { |
| 124 | } |
| 125 | |
| 126 | const ConstString & |
| 127 | EventDataBytes::GetFlavorString () |
| 128 | { |
| 129 | static ConstString g_flavor ("EventDataBytes"); |
| 130 | return g_flavor; |
| 131 | } |
| 132 | |
| 133 | const ConstString & |
| 134 | EventDataBytes::GetFlavor () const |
| 135 | { |
| 136 | return EventDataBytes::GetFlavorString (); |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | EventDataBytes::Dump (Stream *s) const |
| 141 | { |
| 142 | size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint); |
| 143 | if (num_printable_chars == m_bytes.size()) |
| 144 | { |
| 145 | s->Printf("\"%s\"", m_bytes.c_str()); |
| 146 | } |
Greg Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 147 | else if (m_bytes.size() > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | { |
| 149 | DataExtractor data; |
Greg Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 150 | data.SetData(&m_bytes[0], m_bytes.size(), lldb::endian::InlHostByteOrder()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | const void * |
| 156 | EventDataBytes::GetBytes() const |
| 157 | { |
| 158 | if (m_bytes.empty()) |
| 159 | return NULL; |
Greg Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 160 | return &m_bytes[0]; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | size_t |
| 164 | EventDataBytes::GetByteSize() const |
| 165 | { |
| 166 | return m_bytes.size (); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | EventDataBytes::SetBytes (const void *src, size_t src_len) |
| 171 | { |
| 172 | if (src && src_len > 0) |
| 173 | m_bytes.assign ((const char *)src, src_len); |
| 174 | else |
| 175 | m_bytes.clear(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | EventDataBytes::SetBytesFromCString (const char *cstr) |
| 180 | { |
| 181 | if (cstr && cstr[0]) |
| 182 | m_bytes.assign (cstr); |
| 183 | else |
| 184 | m_bytes.clear(); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | const void * |
| 189 | EventDataBytes::GetBytesFromEvent (const Event *event_ptr) |
| 190 | { |
| 191 | const EventDataBytes *e = GetEventDataFromEvent (event_ptr); |
| 192 | if (e) |
| 193 | return e->GetBytes(); |
| 194 | return NULL; |
| 195 | } |
| 196 | |
| 197 | size_t |
| 198 | EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr) |
| 199 | { |
| 200 | const EventDataBytes *e = GetEventDataFromEvent (event_ptr); |
| 201 | if (e) |
| 202 | return e->GetByteSize(); |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | const EventDataBytes * |
| 207 | EventDataBytes::GetEventDataFromEvent (const Event *event_ptr) |
| 208 | { |
| 209 | if (event_ptr) |
| 210 | { |
| 211 | const EventData *event_data = event_ptr->GetData(); |
| 212 | if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString()) |
| 213 | return static_cast <const EventDataBytes *> (event_data); |
| 214 | } |
| 215 | return NULL; |
| 216 | } |
| 217 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 218 | void |
| 219 | EventDataBytes::SwapBytes (std::string &new_bytes) |
| 220 | { |
| 221 | m_bytes.swap (new_bytes); |
| 222 | } |
| 223 | |
| 224 | |