blob: 8e2006330b5a454e7128ca6f399ce8d3908c7f48 [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) :
Jim Ingham583bbb12016-03-07 21:50:25 +000031 m_broadcaster_wp (broadcaster->GetBroadcasterImpl()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032 m_type (event_type),
33 m_data_ap (data)
34{
35}
36
37Event::Event(uint32_t event_type, EventData *data) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038 m_type (event_type),
39 m_data_ap (data)
40{
41}
42
43
44//----------------------------------------------------------------------
45// Event destructor
46//----------------------------------------------------------------------
47Event::~Event ()
48{
49}
50
51void
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052Event::Dump (Stream *s) const
53{
Jim Ingham583bbb12016-03-07 21:50:25 +000054 Broadcaster *broadcaster;
55 Broadcaster::BroadcasterImplSP broadcaster_impl_sp(m_broadcaster_wp.lock());
56 if (broadcaster_impl_sp)
57 broadcaster = broadcaster_impl_sp->GetBroadcaster();
58 else
59 broadcaster = nullptr;
60
61 if (broadcaster)
Greg Clayton6a3efac2012-10-29 18:08:18 +000062 {
63 StreamString event_name;
Jim Ingham583bbb12016-03-07 21:50:25 +000064 if (broadcaster->GetEventNames (event_name, m_type, false))
Greg Clayton6a3efac2012-10-29 18:08:18 +000065 s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x (%s), data = ",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000066 static_cast<const void*>(this),
Jim Ingham583bbb12016-03-07 21:50:25 +000067 static_cast<void*>(broadcaster),
68 broadcaster->GetBroadcasterName().GetCString(),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000069 m_type, event_name.GetString().c_str());
Greg Clayton6a3efac2012-10-29 18:08:18 +000070 else
71 s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000072 static_cast<const void*>(this),
Jim Ingham583bbb12016-03-07 21:50:25 +000073 static_cast<void*>(broadcaster),
74 broadcaster->GetBroadcasterName().GetCString(), m_type);
Greg Clayton6a3efac2012-10-29 18:08:18 +000075 }
76 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +000077 s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ",
78 static_cast<const void*>(this), m_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80 if (m_data_ap.get() == NULL)
81 s->Printf ("<NULL>");
82 else
83 {
84 s->PutChar('{');
85 m_data_ap->Dump (s);
86 s->PutChar('}');
87 }
88}
89
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090void
91Event::DoOnRemoval ()
92{
93 if (m_data_ap.get())
94 m_data_ap->DoOnRemoval (this);
95}
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097EventData::EventData()
98{
99}
100
101EventData::~EventData()
102{
103}
104
105void
106EventData::Dump (Stream *s) const
107{
108 s->PutCString ("Generic Event Data");
109}
110
111EventDataBytes::EventDataBytes () :
112 m_bytes()
113{
114}
115
116EventDataBytes::EventDataBytes (const char *cstr) :
117 m_bytes()
118{
119 SetBytesFromCString (cstr);
120}
121
122EventDataBytes::EventDataBytes (const void *src, size_t src_len) :
123 m_bytes()
124{
125 SetBytes (src, src_len);
126}
127
128EventDataBytes::~EventDataBytes()
129{
130}
131
132const ConstString &
133EventDataBytes::GetFlavorString ()
134{
135 static ConstString g_flavor ("EventDataBytes");
136 return g_flavor;
137}
138
139const ConstString &
140EventDataBytes::GetFlavor () const
141{
142 return EventDataBytes::GetFlavorString ();
143}
144
145void
146EventDataBytes::Dump (Stream *s) const
147{
148 size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint);
149 if (num_printable_chars == m_bytes.size())
150 {
151 s->Printf("\"%s\"", m_bytes.c_str());
152 }
Greg Clayton471b31c2010-07-20 22:52:08 +0000153 else if (m_bytes.size() > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 {
155 DataExtractor data;
Bruce Mitchener9ccb9702015-11-07 04:40:13 +0000156 data.SetData(&m_bytes[0], m_bytes.size(), endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
158 }
159}
160
161const void *
162EventDataBytes::GetBytes() const
163{
164 if (m_bytes.empty())
165 return NULL;
Greg Clayton471b31c2010-07-20 22:52:08 +0000166 return &m_bytes[0];
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169size_t
170EventDataBytes::GetByteSize() const
171{
172 return m_bytes.size ();
173}
174
175void
176EventDataBytes::SetBytes (const void *src, size_t src_len)
177{
178 if (src && src_len > 0)
179 m_bytes.assign ((const char *)src, src_len);
180 else
181 m_bytes.clear();
182}
183
184void
185EventDataBytes::SetBytesFromCString (const char *cstr)
186{
187 if (cstr && cstr[0])
188 m_bytes.assign (cstr);
189 else
190 m_bytes.clear();
191}
192
193
194const void *
195EventDataBytes::GetBytesFromEvent (const Event *event_ptr)
196{
197 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
198 if (e)
199 return e->GetBytes();
200 return NULL;
201}
202
203size_t
204EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr)
205{
206 const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
207 if (e)
208 return e->GetByteSize();
209 return 0;
210}
211
212const EventDataBytes *
213EventDataBytes::GetEventDataFromEvent (const Event *event_ptr)
214{
215 if (event_ptr)
216 {
217 const EventData *event_data = event_ptr->GetData();
218 if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString())
219 return static_cast <const EventDataBytes *> (event_data);
220 }
221 return NULL;
222}
223
Caroline Tice969ed3d2011-05-02 20:41:46 +0000224void
225EventDataBytes::SwapBytes (std::string &new_bytes)
226{
227 m_bytes.swap (new_bytes);
228}
229
230