1 //===-- SBEvent.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 #include "lldb/lldb-python.h"
11 
12 #include "lldb/API/SBEvent.h"
13 #include "lldb/API/SBBroadcaster.h"
14 #include "lldb/API/SBStream.h"
15 
16 #include "lldb/Core/Event.h"
17 #include "lldb/Core/Stream.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Core/ConstString.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Breakpoint/Breakpoint.h"
22 #include "lldb/Interpreter/CommandInterpreter.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 
28 SBEvent::SBEvent () :
29     m_event_sp (),
30     m_opaque_ptr (NULL)
31 {
32 }
33 
34 SBEvent::SBEvent (uint32_t event_type, const char *cstr, uint32_t cstr_len) :
35     m_event_sp (new Event (event_type, new EventDataBytes (cstr, cstr_len))),
36     m_opaque_ptr (m_event_sp.get())
37 {
38 }
39 
40 SBEvent::SBEvent (EventSP &event_sp) :
41     m_event_sp (event_sp),
42     m_opaque_ptr (event_sp.get())
43 {
44 }
45 
46 SBEvent::SBEvent (Event *event_ptr) :
47     m_event_sp (),
48     m_opaque_ptr (event_ptr)
49 {
50 }
51 
52 SBEvent::SBEvent (const SBEvent &rhs) :
53     m_event_sp (rhs.m_event_sp),
54     m_opaque_ptr (rhs.m_opaque_ptr)
55 {
56 
57 }
58 
59 const SBEvent &
60 SBEvent::operator = (const SBEvent &rhs)
61 {
62     if (this != &rhs)
63     {
64         m_event_sp = rhs.m_event_sp;
65         m_opaque_ptr = rhs.m_opaque_ptr;
66     }
67     return *this;
68 }
69 
70 SBEvent::~SBEvent()
71 {
72 }
73 
74 const char *
75 SBEvent::GetDataFlavor ()
76 {
77     Event *lldb_event = get();
78     if (lldb_event)
79     {
80         EventData *event_data = lldb_event->GetData();
81         if (event_data)
82             return lldb_event->GetData()->GetFlavor().AsCString();
83     }
84     return NULL;
85 }
86 
87 uint32_t
88 SBEvent::GetType () const
89 {
90     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
91 
92     const Event *lldb_event = get();
93     uint32_t event_type = 0;
94     if (lldb_event)
95         event_type = lldb_event->GetType();
96 
97     if (log)
98     {
99         StreamString sstr;
100         if (lldb_event && lldb_event->GetBroadcaster() && lldb_event->GetBroadcaster()->GetEventNames(sstr, event_type, true))
101             log->Printf ("SBEvent(%p)::GetType () => 0x%8.8x (%s)",
102                          static_cast<void*>(get()), event_type, sstr.GetData());
103         else
104             log->Printf ("SBEvent(%p)::GetType () => 0x%8.8x",
105                          static_cast<void*>(get()), event_type);
106 
107     }
108 
109     return event_type;
110 }
111 
112 SBBroadcaster
113 SBEvent::GetBroadcaster () const
114 {
115     SBBroadcaster broadcaster;
116     const Event *lldb_event = get();
117     if (lldb_event)
118         broadcaster.reset (lldb_event->GetBroadcaster(), false);
119     return broadcaster;
120 }
121 
122 const char *
123 SBEvent::GetBroadcasterClass () const
124 {
125     const Event *lldb_event = get();
126     if (lldb_event)
127         return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
128     else
129         return "unknown class";
130 }
131 
132 bool
133 SBEvent::BroadcasterMatchesPtr (const SBBroadcaster *broadcaster)
134 {
135     if (broadcaster)
136         return BroadcasterMatchesRef (*broadcaster);
137     return false;
138 }
139 
140 bool
141 SBEvent::BroadcasterMatchesRef (const SBBroadcaster &broadcaster)
142 {
143 
144     Event *lldb_event = get();
145     bool success = false;
146     if (lldb_event)
147         success = lldb_event->BroadcasterIs (broadcaster.get());
148 
149     // For logging, this gets a little chatty so only enable this when verbose logging is on
150     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API | LIBLLDB_LOG_VERBOSE));
151     if (log)
152         log->Printf ("SBEvent(%p)::BroadcasterMatchesRef (SBBroadcaster(%p): %s) => %i",
153                      static_cast<void*>(get()),
154                      static_cast<void*>(broadcaster.get()),
155                      broadcaster.GetName(), success);
156 
157     return success;
158 }
159 
160 void
161 SBEvent::Clear()
162 {
163     Event *lldb_event = get();
164     if (lldb_event)
165         lldb_event->Clear();
166 }
167 
168 EventSP &
169 SBEvent::GetSP () const
170 {
171     return m_event_sp;
172 }
173 
174 Event *
175 SBEvent::get() const
176 {
177     // There is a dangerous accessor call GetSharedPtr which can be used, so if
178     // we have anything valid in m_event_sp, we must use that since if it gets
179     // used by a function that puts something in there, then it won't update
180     // m_opaque_ptr...
181     if (m_event_sp)
182         m_opaque_ptr = m_event_sp.get();
183 
184     return m_opaque_ptr;
185 }
186 
187 void
188 SBEvent::reset (EventSP &event_sp)
189 {
190     m_event_sp = event_sp;
191     m_opaque_ptr = m_event_sp.get();
192 }
193 
194 void
195 SBEvent::reset (Event* event_ptr)
196 {
197     m_opaque_ptr = event_ptr;
198     m_event_sp.reset();
199 }
200 
201 bool
202 SBEvent::IsValid() const
203 {
204     // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get()
205     // accessor. See comments in SBEvent::get()....
206     return SBEvent::get() != NULL;
207 
208 }
209 
210 const char *
211 SBEvent::GetCStringFromEvent (const SBEvent &event)
212 {
213     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
214 
215     if (log)
216         log->Printf ("SBEvent(%p)::GetCStringFromEvent () => \"%s\"",
217                      static_cast<void*>(event.get()),
218                      reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event.get())));
219 
220     return reinterpret_cast<const char *>(EventDataBytes::GetBytesFromEvent (event.get()));
221 }
222 
223 
224 bool
225 SBEvent::GetDescription (SBStream &description)
226 {
227     Stream &strm = description.ref();
228 
229     if (get())
230     {
231         m_opaque_ptr->Dump (&strm);
232     }
233     else
234         strm.PutCString ("No value");
235 
236     return true;
237 }
238 
239 bool
240 SBEvent::GetDescription (SBStream &description) const
241 {
242     Stream &strm = description.ref();
243 
244     if (get())
245     {
246         m_opaque_ptr->Dump (&strm);
247     }
248     else
249         strm.PutCString ("No value");
250 
251     return true;
252 }
253