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