blob: c848cd48307197876542d5ffa9d9447278b3db9c [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
Eli Friedman88966972010-06-09 08:50:27 +000021#include <algorithm>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
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::Clear()
53{
54 m_data_ap.reset();
55}
56
57void
58Event::Dump (Stream *s) const
59{
60 s->Printf("%p Event: broadcaster = %p, type = 0x%8.8x, data = ", this, m_broadcaster, m_type);
61
62 if (m_data_ap.get() == NULL)
63 s->Printf ("<NULL>");
64 else
65 {
66 s->PutChar('{');
67 m_data_ap->Dump (s);
68 s->PutChar('}');
69 }
70}
71
72Broadcaster *
73Event::GetBroadcaster () const
74{
75 return m_broadcaster;
76}
77
78bool
79Event::BroadcasterIs (Broadcaster *broadcaster)
80{
81 return broadcaster == m_broadcaster;
82}
83
84uint32_t
85Event::GetType() const
86{
87 return m_type;
88}
89
90
91EventData *
92Event::GetData ()
93{
94 return m_data_ap.get();
95}
96
97const EventData *
98Event::GetData () const
99{
100 return m_data_ap.get();
101}
102
103void
104Event::DoOnRemoval ()
105{
106 if (m_data_ap.get())
107 m_data_ap->DoOnRemoval (this);
108}
109
110void
111Event::SetBroadcaster (Broadcaster *broadcaster)
112{
113 m_broadcaster = broadcaster;
114}
115
116EventData::EventData()
117{
118}
119
120EventData::~EventData()
121{
122}
123
124void
125EventData::Dump (Stream *s) const
126{
127 s->PutCString ("Generic Event Data");
128}
129
130EventDataBytes::EventDataBytes () :
131 m_bytes()
132{
133}
134
135EventDataBytes::EventDataBytes (const char *cstr) :
136 m_bytes()
137{
138 SetBytesFromCString (cstr);
139}
140
141EventDataBytes::EventDataBytes (const void *src, size_t src_len) :
142 m_bytes()
143{
144 SetBytes (src, src_len);
145}
146
147EventDataBytes::~EventDataBytes()
148{
149}
150
151const ConstString &
152EventDataBytes::GetFlavorString ()
153{
154 static ConstString g_flavor ("EventDataBytes");
155 return g_flavor;
156}
157
158const ConstString &
159EventDataBytes::GetFlavor () const
160{
161 return EventDataBytes::GetFlavorString ();
162}
163
164void
165EventDataBytes::Dump (Stream *s) const
166{
167 size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint);
168 if (num_printable_chars == m_bytes.size())
169 {
170 s->Printf("\"%s\"", m_bytes.c_str());
171 }
172 else
173 {
174 DataExtractor data;
175 data.SetData(m_bytes.data(), m_bytes.size(), eByteOrderHost);
176 data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
177 }
178}
179
180const void *
181EventDataBytes::GetBytes() const
182{
183 if (m_bytes.empty())
184 return NULL;
185 return m_bytes.data();
186}
187
188size_t
189EventDataBytes::GetByteSize() const
190{
191 return m_bytes.size ();
192}
193
194void
195EventDataBytes::SetBytes (const void *src, size_t src_len)
196{
197 if (src && src_len > 0)
198 m_bytes.assign ((const char *)src, src_len);
199 else
200 m_bytes.clear();
201}
202
203void
204EventDataBytes::SetBytesFromCString (const char *cstr)
205{
206 if (cstr && cstr[0])
207 m_bytes.assign (cstr);
208 else
209 m_bytes.clear();
210}
211
212
213const void *
214EventDataBytes::GetBytesFromEvent (const Event *event_ptr)
215{
216 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
217 if (e)
218 return e->GetBytes();
219 return NULL;
220}
221
222size_t
223EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr)
224{
225 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
226 if (e)
227 return e->GetByteSize();
228 return 0;
229}
230
231const EventDataBytes *
232EventDataBytes::GetEventDataFromEvent (const Event *event_ptr)
233{
234 if (event_ptr)
235 {
236 const EventData *event_data = event_ptr->GetData();
237 if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString())
238 return static_cast <const EventDataBytes *> (event_data);
239 }
240 return NULL;
241}
242