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
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
53Event::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                      this,
61                      m_broadcaster,
62                      m_broadcaster->GetBroadcasterName().GetCString(),
63                      m_type,
64                      event_name.GetString().c_str());
65        else
66            s->Printf("%p Event: broadcaster = %p (%s), type = 0x%8.8x, data = ",
67                      this,
68                      m_broadcaster,
69                      m_broadcaster->GetBroadcasterName().GetCString(),
70                      m_type);
71    }
72    else
73        s->Printf("%p Event: broadcaster = NULL, type = 0x%8.8x, data = ", this, m_type);
74
75    if (m_data_ap.get() == NULL)
76        s->Printf ("<NULL>");
77    else
78    {
79        s->PutChar('{');
80        m_data_ap->Dump (s);
81        s->PutChar('}');
82    }
83}
84
85void
86Event::DoOnRemoval ()
87{
88    if (m_data_ap.get())
89        m_data_ap->DoOnRemoval (this);
90}
91
92EventData::EventData()
93{
94}
95
96EventData::~EventData()
97{
98}
99
100void
101EventData::Dump (Stream *s) const
102{
103    s->PutCString ("Generic Event Data");
104}
105
106EventDataBytes::EventDataBytes () :
107    m_bytes()
108{
109}
110
111EventDataBytes::EventDataBytes (const char *cstr) :
112    m_bytes()
113{
114    SetBytesFromCString (cstr);
115}
116
117EventDataBytes::EventDataBytes (const void *src, size_t src_len) :
118    m_bytes()
119{
120    SetBytes (src, src_len);
121}
122
123EventDataBytes::~EventDataBytes()
124{
125}
126
127const ConstString &
128EventDataBytes::GetFlavorString ()
129{
130    static ConstString g_flavor ("EventDataBytes");
131    return g_flavor;
132}
133
134const ConstString &
135EventDataBytes::GetFlavor () const
136{
137    return EventDataBytes::GetFlavorString ();
138}
139
140void
141EventDataBytes::Dump (Stream *s) const
142{
143    size_t num_printable_chars = std::count_if (m_bytes.begin(), m_bytes.end(), isprint);
144    if (num_printable_chars == m_bytes.size())
145    {
146        s->Printf("\"%s\"", m_bytes.c_str());
147    }
148    else if (m_bytes.size() > 0)
149    {
150        DataExtractor data;
151        data.SetData(&m_bytes[0], m_bytes.size(), lldb::endian::InlHostByteOrder());
152        data.Dump(s, 0, eFormatBytes, 1, m_bytes.size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
153    }
154}
155
156const void *
157EventDataBytes::GetBytes() const
158{
159    if (m_bytes.empty())
160        return NULL;
161    return &m_bytes[0];
162}
163
164size_t
165EventDataBytes::GetByteSize() const
166{
167    return m_bytes.size ();
168}
169
170void
171EventDataBytes::SetBytes (const void *src, size_t src_len)
172{
173    if (src && src_len > 0)
174        m_bytes.assign ((const char *)src, src_len);
175    else
176        m_bytes.clear();
177}
178
179void
180EventDataBytes::SetBytesFromCString (const char *cstr)
181{
182    if (cstr && cstr[0])
183        m_bytes.assign (cstr);
184    else
185        m_bytes.clear();
186}
187
188
189const void *
190EventDataBytes::GetBytesFromEvent (const Event *event_ptr)
191{
192    const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
193    if (e)
194        return e->GetBytes();
195    return NULL;
196}
197
198size_t
199EventDataBytes::GetByteSizeFromEvent (const Event *event_ptr)
200{
201    const EventDataBytes *e = GetEventDataFromEvent (event_ptr);
202    if (e)
203        return e->GetByteSize();
204    return 0;
205}
206
207const EventDataBytes *
208EventDataBytes::GetEventDataFromEvent (const Event *event_ptr)
209{
210    if (event_ptr)
211    {
212        const EventData *event_data = event_ptr->GetData();
213        if (event_data && event_data->GetFlavor() == EventDataBytes::GetFlavorString())
214            return static_cast <const EventDataBytes *> (event_data);
215    }
216    return NULL;
217}
218
219void
220EventDataBytes::SwapBytes (std::string &new_bytes)
221{
222    m_bytes.swap (new_bytes);
223}
224
225
226