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/Host/Endian.h"
21 #include "lldb/Target/Process.h"
22 #include <algorithm>
23
24 using namespace lldb;
25 using namespace lldb_private;
26
27 //----------------------------------------------------------------------
28 // Event constructor
29 //----------------------------------------------------------------------
Event(Broadcaster * broadcaster,uint32_t event_type,EventData * data)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
Event(uint32_t event_type,EventData * data)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 //----------------------------------------------------------------------
~Event()48 Event::~Event ()
49 {
50 }
51
52 void
Dump(Stream * s) const53 Event::Dump (Stream *s) const
54 {
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 = ",
60 static_cast<const void*>(this),
61 static_cast<void*>(m_broadcaster),
62 m_broadcaster->GetBroadcasterName().GetCString(),
63 m_type, event_name.GetString().c_str());
64 else
65 s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
66 static_cast<const void*>(this),
67 static_cast<void*>(m_broadcaster),
68 m_broadcaster->GetBroadcasterName().GetCString(), m_type);
69 }
70 else
71 s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
72 static_cast<const void*>(this), m_type);
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
84 void
DoOnRemoval()85 Event::DoOnRemoval ()
86 {
87 if (m_data_ap.get())
88 m_data_ap->DoOnRemoval (this);
89 }
90
EventData()91 EventData::EventData()
92 {
93 }
94
~EventData()95 EventData::~EventData()
96 {
97 }
98
99 void
Dump(Stream * s) const100 EventData::Dump (Stream *s) const
101 {
102 s->PutCString ("Generic Event Data");
103 }
104
EventDataBytes()105 EventDataBytes::EventDataBytes () :
106 m_bytes()
107 {
108 }
109
EventDataBytes(const char * cstr)110 EventDataBytes::EventDataBytes (const char *cstr) :
111 m_bytes()
112 {
113 SetBytesFromCString (cstr);
114 }
115
EventDataBytes(const void * src,size_t src_len)116 EventDataBytes::EventDataBytes (const void *src, size_t src_len) :
117 m_bytes()
118 {
119 SetBytes (src, src_len);
120 }
121
~EventDataBytes()122 EventDataBytes::~EventDataBytes()
123 {
124 }
125
126 const ConstString &
GetFlavorString()127 EventDataBytes::GetFlavorString ()
128 {
129 static ConstString g_flavor ("EventDataBytes");
130 return g_flavor;
131 }
132
133 const ConstString &
GetFlavor() const134 EventDataBytes::GetFlavor () const
135 {
136 return EventDataBytes::GetFlavorString ();
137 }
138
139 void
Dump(Stream * s) const140 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 }
147 else if (m_bytes.size() > 0)
148 {
149 DataExtractor data;
150 data.SetData(&m_bytes[0], m_bytes.size(), lldb::endian::InlHostByteOrder());
151 data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
152 }
153 }
154
155 const void *
GetBytes() const156 EventDataBytes::GetBytes() const
157 {
158 if (m_bytes.empty())
159 return NULL;
160 return &m_bytes[0];
161 }
162
163 size_t
GetByteSize() const164 EventDataBytes::GetByteSize() const
165 {
166 return m_bytes.size ();
167 }
168
169 void
SetBytes(const void * src,size_t src_len)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
SetBytesFromCString(const char * cstr)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 *
GetBytesFromEvent(const Event * event_ptr)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
GetByteSizeFromEvent(const Event * event_ptr)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 *
GetEventDataFromEvent(const Event * event_ptr)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
218 void
SwapBytes(std::string & new_bytes)219 EventDataBytes::SwapBytes (std::string &new_bytes)
220 {
221 m_bytes.swap (new_bytes);
222 }
223
224
225