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