blob: ac122858c71d16bd6ae8774970583968cbab7fc4 [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"
Greg Clayton7fb56d02011-02-01 01:31:41 +000020#include "lldb/Host/Endian.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
Eli Friedman88966972010-06-09 08:50:27 +000022#include <algorithm>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
27//----------------------------------------------------------------------
28// Event constructor
29//----------------------------------------------------------------------
30Event::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
37Event::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//----------------------------------------------------------------------
48Event::~Event ()
49{
50}
51
52void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053Event::Dump (Stream *s) const
54{
55 s->Printf("%p Event: broadcaster = %p, type = 0x%8.8x, data = ", this, m_broadcaster, m_type);
56
57 if (m_data_ap.get() == NULL)
58 s->Printf ("<NULL>");
59 else
60 {
61 s->PutChar('{');
62 m_data_ap->Dump (s);
63 s->PutChar('}');
64 }
65}
66
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067void
68Event::DoOnRemoval ()
69{
70 if (m_data_ap.get())
71 m_data_ap->DoOnRemoval (this);
72}
73
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074EventData::EventData()
75{
76}
77
78EventData::~EventData()
79{
80}
81
82void
83EventData::Dump (Stream *s) const
84{
85 s->PutCString ("Generic Event Data");
86}
87
88EventDataBytes::EventDataBytes () :
89 m_bytes()
90{
91}
92
93EventDataBytes::EventDataBytes (const char *cstr) :
94 m_bytes()
95{
96 SetBytesFromCString (cstr);
97}
98
99EventDataBytes::EventDataBytes (const void *src, size_t src_len) :
100 m_bytes()
101{
102 SetBytes (src, src_len);
103}
104
105EventDataBytes::~EventDataBytes()
106{
107}
108
109const ConstString &
110EventDataBytes::GetFlavorString ()
111{
112 static ConstString g_flavor ("EventDataBytes");
113 return g_flavor;
114}
115
116const ConstString &
117EventDataBytes::GetFlavor () const
118{
119 return EventDataBytes::GetFlavorString ();
120}
121
122void
123EventDataBytes::Dump (Stream *s) const
124{
125 size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint);
126 if (num_printable_chars == m_bytes.size())
127 {
128 s->Printf("\"%s\"", m_bytes.c_str());
129 }
Greg Clayton471b31c2010-07-20 22:52:08 +0000130 else if (m_bytes.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
132 DataExtractor data;
Greg Clayton7fb56d02011-02-01 01:31:41 +0000133 data.SetData(&m_bytes[0], m_bytes.size(), lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134 data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
135 }
136}
137
138const void *
139EventDataBytes::GetBytes() const
140{
141 if (m_bytes.empty())
142 return NULL;
Greg Clayton471b31c2010-07-20 22:52:08 +0000143 return &m_bytes[0];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
146size_t
147EventDataBytes::GetByteSize() const
148{
149 return m_bytes.size ();
150}
151
152void
153EventDataBytes::SetBytes (const void *src, size_t src_len)
154{
155 if (src && src_len > 0)
156 m_bytes.assign ((const char *)src, src_len);
157 else
158 m_bytes.clear();
159}
160
161void
162EventDataBytes::SetBytesFromCString (const char *cstr)
163{
164 if (cstr && cstr[0])
165 m_bytes.assign (cstr);
166 else
167 m_bytes.clear();
168}
169
170
171const void *
172EventDataBytes::GetBytesFromEvent (const Event *event_ptr)
173{
174 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
175 if (e)
176 return e->GetBytes();
177 return NULL;
178}
179
180size_t
181EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr)
182{
183 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
184 if (e)
185 return e->GetByteSize();
186 return 0;
187}
188
189const EventDataBytes *
190EventDataBytes::GetEventDataFromEvent (const Event *event_ptr)
191{
192 if (event_ptr)
193 {
194 const EventData *event_data = event_ptr->GetData();
195 if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString())
196 return static_cast <const EventDataBytes *> (event_data);
197 }
198 return NULL;
199}
200
Caroline Tice969ed3d2011-05-02 20:41:46 +0000201void
202EventDataBytes::SwapBytes (std::string &new_bytes)
203{
204 m_bytes.swap (new_bytes);
205}
206
207