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/Event.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Interpreter/CommandInterpreter.h"
18 #include "lldb/Target/Process.h"
19 #include "lldb/Utility/ConstString.h"
20 #include "lldb/Utility/Stream.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   LLDB_LOGV(log, "({0}) (SBBroadcaster({1}): {2}) => {3}", get(),
114             broadcaster.get(), broadcaster.GetName(), success);
115 
116   return success;
117 }
118 
119 void SBEvent::Clear() {
120   Event *lldb_event = get();
121   if (lldb_event)
122     lldb_event->Clear();
123 }
124 
125 EventSP &SBEvent::GetSP() const { return m_event_sp; }
126 
127 Event *SBEvent::get() const {
128   // There is a dangerous accessor call GetSharedPtr which can be used, so if
129   // we have anything valid in m_event_sp, we must use that since if it gets
130   // used by a function that puts something in there, then it won't update
131   // m_opaque_ptr...
132   if (m_event_sp)
133     m_opaque_ptr = m_event_sp.get();
134 
135   return m_opaque_ptr;
136 }
137 
138 void SBEvent::reset(EventSP &event_sp) {
139   m_event_sp = event_sp;
140   m_opaque_ptr = m_event_sp.get();
141 }
142 
143 void SBEvent::reset(Event *event_ptr) {
144   m_opaque_ptr = event_ptr;
145   m_event_sp.reset();
146 }
147 
148 bool SBEvent::IsValid() const {
149   // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get()
150   // accessor. See comments in SBEvent::get()....
151   return SBEvent::get() != NULL;
152 }
153 
154 const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
155   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
156 
157   if (log)
158     log->Printf("SBEvent(%p)::GetCStringFromEvent () => \"%s\"",
159                 static_cast<void *>(event.get()),
160                 reinterpret_cast<const char *>(
161                     EventDataBytes::GetBytesFromEvent(event.get())));
162 
163   return reinterpret_cast<const char *>(
164       EventDataBytes::GetBytesFromEvent(event.get()));
165 }
166 
167 bool SBEvent::GetDescription(SBStream &description) {
168   Stream &strm = description.ref();
169 
170   if (get()) {
171     m_opaque_ptr->Dump(&strm);
172   } else
173     strm.PutCString("No value");
174 
175   return true;
176 }
177 
178 bool SBEvent::GetDescription(SBStream &description) const {
179   Stream &strm = description.ref();
180 
181   if (get()) {
182     m_opaque_ptr->Dump(&strm);
183   } else
184     strm.PutCString("No value");
185 
186   return true;
187 }
188