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