1ac7ddfbfSEd Maste //===-- SBEvent.cpp ---------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/API/SBEvent.h"
11ac7ddfbfSEd Maste #include "lldb/API/SBBroadcaster.h"
12ac7ddfbfSEd Maste #include "lldb/API/SBStream.h"
13ac7ddfbfSEd Maste 
14435933ddSDimitry Andric #include "lldb/Breakpoint/Breakpoint.h"
15ac7ddfbfSEd Maste #include "lldb/Core/StreamFile.h"
16ac7ddfbfSEd Maste #include "lldb/Interpreter/CommandInterpreter.h"
17435933ddSDimitry Andric #include "lldb/Target/Process.h"
18f678e45dSDimitry Andric #include "lldb/Utility/ConstString.h"
19*b5893f02SDimitry Andric #include "lldb/Utility/Event.h"
20f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
21ac7ddfbfSEd Maste 
22ac7ddfbfSEd Maste using namespace lldb;
23ac7ddfbfSEd Maste using namespace lldb_private;
24ac7ddfbfSEd Maste 
SBEvent()25435933ddSDimitry Andric SBEvent::SBEvent() : m_event_sp(), m_opaque_ptr(NULL) {}
26ac7ddfbfSEd Maste 
SBEvent(uint32_t event_type,const char * cstr,uint32_t cstr_len)27435933ddSDimitry Andric SBEvent::SBEvent(uint32_t event_type, const char *cstr, uint32_t cstr_len)
28435933ddSDimitry Andric     : m_event_sp(new Event(event_type, new EventDataBytes(cstr, cstr_len))),
29435933ddSDimitry Andric       m_opaque_ptr(m_event_sp.get()) {}
30ac7ddfbfSEd Maste 
SBEvent(EventSP & event_sp)31435933ddSDimitry Andric SBEvent::SBEvent(EventSP &event_sp)
32435933ddSDimitry Andric     : m_event_sp(event_sp), m_opaque_ptr(event_sp.get()) {}
33ac7ddfbfSEd Maste 
SBEvent(Event * event_ptr)34435933ddSDimitry Andric SBEvent::SBEvent(Event *event_ptr) : m_event_sp(), m_opaque_ptr(event_ptr) {}
35ac7ddfbfSEd Maste 
SBEvent(const SBEvent & rhs)36435933ddSDimitry Andric SBEvent::SBEvent(const SBEvent &rhs)
37435933ddSDimitry Andric     : m_event_sp(rhs.m_event_sp), m_opaque_ptr(rhs.m_opaque_ptr) {}
387aa51b79SEd Maste 
operator =(const SBEvent & rhs)39435933ddSDimitry Andric const SBEvent &SBEvent::operator=(const SBEvent &rhs) {
40435933ddSDimitry Andric   if (this != &rhs) {
41ac7ddfbfSEd Maste     m_event_sp = rhs.m_event_sp;
42ac7ddfbfSEd Maste     m_opaque_ptr = rhs.m_opaque_ptr;
43ac7ddfbfSEd Maste   }
44ac7ddfbfSEd Maste   return *this;
45ac7ddfbfSEd Maste }
46ac7ddfbfSEd Maste 
~SBEvent()47435933ddSDimitry Andric SBEvent::~SBEvent() {}
48ac7ddfbfSEd Maste 
GetDataFlavor()49435933ddSDimitry Andric const char *SBEvent::GetDataFlavor() {
50ac7ddfbfSEd Maste   Event *lldb_event = get();
51435933ddSDimitry Andric   if (lldb_event) {
52ac7ddfbfSEd Maste     EventData *event_data = lldb_event->GetData();
53ac7ddfbfSEd Maste     if (event_data)
54ac7ddfbfSEd Maste       return lldb_event->GetData()->GetFlavor().AsCString();
55ac7ddfbfSEd Maste   }
56ac7ddfbfSEd Maste   return NULL;
57ac7ddfbfSEd Maste }
58ac7ddfbfSEd Maste 
GetType() const59435933ddSDimitry Andric uint32_t SBEvent::GetType() const {
60ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
61ac7ddfbfSEd Maste 
62ac7ddfbfSEd Maste   const Event *lldb_event = get();
63ac7ddfbfSEd Maste   uint32_t event_type = 0;
64ac7ddfbfSEd Maste   if (lldb_event)
65ac7ddfbfSEd Maste     event_type = lldb_event->GetType();
66ac7ddfbfSEd Maste 
67435933ddSDimitry Andric   if (log) {
68ac7ddfbfSEd Maste     StreamString sstr;
69435933ddSDimitry Andric     if (lldb_event && lldb_event->GetBroadcaster() &&
70435933ddSDimitry Andric         lldb_event->GetBroadcaster()->GetEventNames(sstr, event_type, true))
710127ef0fSEd Maste       log->Printf("SBEvent(%p)::GetType () => 0x%8.8x (%s)",
720127ef0fSEd Maste                   static_cast<void *>(get()), event_type, sstr.GetData());
73ac7ddfbfSEd Maste     else
740127ef0fSEd Maste       log->Printf("SBEvent(%p)::GetType () => 0x%8.8x",
750127ef0fSEd Maste                   static_cast<void *>(get()), event_type);
76ac7ddfbfSEd Maste   }
77ac7ddfbfSEd Maste 
78ac7ddfbfSEd Maste   return event_type;
79ac7ddfbfSEd Maste }
80ac7ddfbfSEd Maste 
GetBroadcaster() const81435933ddSDimitry Andric SBBroadcaster SBEvent::GetBroadcaster() const {
82ac7ddfbfSEd Maste   SBBroadcaster broadcaster;
83ac7ddfbfSEd Maste   const Event *lldb_event = get();
84ac7ddfbfSEd Maste   if (lldb_event)
85ac7ddfbfSEd Maste     broadcaster.reset(lldb_event->GetBroadcaster(), false);
86ac7ddfbfSEd Maste   return broadcaster;
87ac7ddfbfSEd Maste }
88ac7ddfbfSEd Maste 
GetBroadcasterClass() const89435933ddSDimitry Andric const char *SBEvent::GetBroadcasterClass() const {
90ac7ddfbfSEd Maste   const Event *lldb_event = get();
91ac7ddfbfSEd Maste   if (lldb_event)
92ac7ddfbfSEd Maste     return lldb_event->GetBroadcaster()->GetBroadcasterClass().AsCString();
93ac7ddfbfSEd Maste   else
94ac7ddfbfSEd Maste     return "unknown class";
95ac7ddfbfSEd Maste }
96ac7ddfbfSEd Maste 
BroadcasterMatchesPtr(const SBBroadcaster * broadcaster)97435933ddSDimitry Andric bool SBEvent::BroadcasterMatchesPtr(const SBBroadcaster *broadcaster) {
98ac7ddfbfSEd Maste   if (broadcaster)
99ac7ddfbfSEd Maste     return BroadcasterMatchesRef(*broadcaster);
100ac7ddfbfSEd Maste   return false;
101ac7ddfbfSEd Maste }
102ac7ddfbfSEd Maste 
BroadcasterMatchesRef(const SBBroadcaster & broadcaster)103435933ddSDimitry Andric bool SBEvent::BroadcasterMatchesRef(const SBBroadcaster &broadcaster) {
104ac7ddfbfSEd Maste 
105ac7ddfbfSEd Maste   Event *lldb_event = get();
106ac7ddfbfSEd Maste   bool success = false;
107ac7ddfbfSEd Maste   if (lldb_event)
108ac7ddfbfSEd Maste     success = lldb_event->BroadcasterIs(broadcaster.get());
109ac7ddfbfSEd Maste 
110435933ddSDimitry Andric   // For logging, this gets a little chatty so only enable this when verbose
111435933ddSDimitry Andric   // logging is on
112f678e45dSDimitry Andric   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
113f678e45dSDimitry Andric   LLDB_LOGV(log, "({0}) (SBBroadcaster({1}): {2}) => {3}", get(),
114f678e45dSDimitry Andric             broadcaster.get(), broadcaster.GetName(), success);
115ac7ddfbfSEd Maste 
116ac7ddfbfSEd Maste   return success;
117ac7ddfbfSEd Maste }
118ac7ddfbfSEd Maste 
Clear()119435933ddSDimitry Andric void SBEvent::Clear() {
120ac7ddfbfSEd Maste   Event *lldb_event = get();
121ac7ddfbfSEd Maste   if (lldb_event)
122ac7ddfbfSEd Maste     lldb_event->Clear();
123ac7ddfbfSEd Maste }
124ac7ddfbfSEd Maste 
GetSP() const125435933ddSDimitry Andric EventSP &SBEvent::GetSP() const { return m_event_sp; }
126ac7ddfbfSEd Maste 
get() const127435933ddSDimitry Andric Event *SBEvent::get() const {
128ac7ddfbfSEd Maste   // There is a dangerous accessor call GetSharedPtr which can be used, so if
129ac7ddfbfSEd Maste   // we have anything valid in m_event_sp, we must use that since if it gets
130ac7ddfbfSEd Maste   // used by a function that puts something in there, then it won't update
131ac7ddfbfSEd Maste   // m_opaque_ptr...
132ac7ddfbfSEd Maste   if (m_event_sp)
133ac7ddfbfSEd Maste     m_opaque_ptr = m_event_sp.get();
134ac7ddfbfSEd Maste 
135ac7ddfbfSEd Maste   return m_opaque_ptr;
136ac7ddfbfSEd Maste }
137ac7ddfbfSEd Maste 
reset(EventSP & event_sp)138435933ddSDimitry Andric void SBEvent::reset(EventSP &event_sp) {
139ac7ddfbfSEd Maste   m_event_sp = event_sp;
140ac7ddfbfSEd Maste   m_opaque_ptr = m_event_sp.get();
141ac7ddfbfSEd Maste }
142ac7ddfbfSEd Maste 
reset(Event * event_ptr)143435933ddSDimitry Andric void SBEvent::reset(Event *event_ptr) {
144ac7ddfbfSEd Maste   m_opaque_ptr = event_ptr;
145ac7ddfbfSEd Maste   m_event_sp.reset();
146ac7ddfbfSEd Maste }
147ac7ddfbfSEd Maste 
IsValid() const148435933ddSDimitry Andric bool SBEvent::IsValid() const {
1494ba319b5SDimitry Andric   // Do NOT use m_opaque_ptr directly!!! Must use the SBEvent::get() accessor.
1504ba319b5SDimitry Andric   // See comments in SBEvent::get()....
151ac7ddfbfSEd Maste   return SBEvent::get() != NULL;
152ac7ddfbfSEd Maste }
153ac7ddfbfSEd Maste 
GetCStringFromEvent(const SBEvent & event)154435933ddSDimitry Andric const char *SBEvent::GetCStringFromEvent(const SBEvent &event) {
155ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
156ac7ddfbfSEd Maste 
157ac7ddfbfSEd Maste   if (log)
158ac7ddfbfSEd Maste     log->Printf("SBEvent(%p)::GetCStringFromEvent () => \"%s\"",
1590127ef0fSEd Maste                 static_cast<void *>(event.get()),
160435933ddSDimitry Andric                 reinterpret_cast<const char *>(
161435933ddSDimitry Andric                     EventDataBytes::GetBytesFromEvent(event.get())));
162ac7ddfbfSEd Maste 
163435933ddSDimitry Andric   return reinterpret_cast<const char *>(
164435933ddSDimitry Andric       EventDataBytes::GetBytesFromEvent(event.get()));
165ac7ddfbfSEd Maste }
166ac7ddfbfSEd Maste 
GetDescription(SBStream & description)167435933ddSDimitry Andric bool SBEvent::GetDescription(SBStream &description) {
168ac7ddfbfSEd Maste   Stream &strm = description.ref();
169ac7ddfbfSEd Maste 
170435933ddSDimitry Andric   if (get()) {
171ac7ddfbfSEd Maste     m_opaque_ptr->Dump(&strm);
172435933ddSDimitry Andric   } else
173ac7ddfbfSEd Maste     strm.PutCString("No value");
174ac7ddfbfSEd Maste 
175ac7ddfbfSEd Maste   return true;
176ac7ddfbfSEd Maste }
177ac7ddfbfSEd Maste 
GetDescription(SBStream & description) const178435933ddSDimitry Andric bool SBEvent::GetDescription(SBStream &description) const {
179ac7ddfbfSEd Maste   Stream &strm = description.ref();
180ac7ddfbfSEd Maste 
181435933ddSDimitry Andric   if (get()) {
182ac7ddfbfSEd Maste     m_opaque_ptr->Dump(&strm);
183435933ddSDimitry Andric   } else
184ac7ddfbfSEd Maste     strm.PutCString("No value");
185ac7ddfbfSEd Maste 
186ac7ddfbfSEd Maste   return true;
187ac7ddfbfSEd Maste }
188