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