Adding events when watchpoints are set or changed.

<rdar://problem/11597849>


git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@170400 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/API/SBWatchpoint.cpp b/source/API/SBWatchpoint.cpp
index 4794ba4..eb50fa8 100644
--- a/source/API/SBWatchpoint.cpp
+++ b/source/API/SBWatchpoint.cpp
@@ -11,6 +11,7 @@
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBAddress.h"
 #include "lldb/API/SBDebugger.h"
+#include "lldb/API/SBEvent.h"
 #include "lldb/API/SBStream.h"
 
 #include "lldb/lldb-types.h"
@@ -271,3 +272,27 @@
 {
     m_opaque_sp = sp;
 }
+
+bool
+SBWatchpoint::EventIsWatchpointEvent (const lldb::SBEvent &event)
+{
+    return Watchpoint::WatchpointEventData::GetEventDataFromEvent(event.get()) != NULL;
+
+}
+
+WatchpointEventType
+SBWatchpoint::GetWatchpointEventTypeFromEvent (const SBEvent& event)
+{
+    if (event.IsValid())
+        return Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent (event.GetSP());
+    return eWatchpointEventTypeInvalidType;
+}
+
+SBWatchpoint
+SBWatchpoint::GetWatchpointFromEvent (const lldb::SBEvent& event)
+{
+    SBWatchpoint sb_watchpoint;
+    if (event.IsValid())
+        sb_watchpoint.m_opaque_sp = Watchpoint::WatchpointEventData::GetWatchpointFromEvent (event.GetSP());
+    return sb_watchpoint;
+}
diff --git a/source/Breakpoint/BreakpointList.cpp b/source/Breakpoint/BreakpointList.cpp
index 6a91bd6..5b40e2e 100644
--- a/source/Breakpoint/BreakpointList.cpp
+++ b/source/Breakpoint/BreakpointList.cpp
@@ -88,9 +88,14 @@
     {
         bp_collection::iterator pos, end = m_breakpoints.end();
         for (pos = m_breakpoints.begin(); pos != end; ++pos)
+        {
             if ((*pos)->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
+            {
                 (*pos)->GetTarget().BroadcastEvent (Target::eBroadcastBitBreakpointChanged,
-                                                    new Breakpoint::BreakpointEventData (eBreakpointEventTypeRemoved, *pos));
+                                                    new Breakpoint::BreakpointEventData (eBreakpointEventTypeRemoved,
+                                                                                         *pos));
+            }
+        }
     }
     m_breakpoints.erase (m_breakpoints.begin(), m_breakpoints.end());
 }
diff --git a/source/Breakpoint/Watchpoint.cpp b/source/Breakpoint/Watchpoint.cpp
index 4aadb95..09cf2b6 100644
--- a/source/Breakpoint/Watchpoint.cpp
+++ b/source/Breakpoint/Watchpoint.cpp
@@ -45,7 +45,8 @@
     m_watch_spec_str(),
     m_type(),
     m_error(),
-    m_options ()
+    m_options (),
+    m_being_created(true)
 {
     if (type && type->IsValid())
         m_type = *type;
@@ -64,6 +65,7 @@
         m_target.GetProcessSP()->CalculateExecutionContext(exe_ctx);
         CaptureWatchedValue (exe_ctx);
     }
+    m_being_created = false;
 }
 
 Watchpoint::~Watchpoint()
@@ -78,7 +80,7 @@
     // or delete it when it goes goes out of scope.
     m_options.SetCallback(callback, BatonSP (new Baton(baton)), is_synchronous);
     
-    //SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
+    SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
 }
 
 // This function is used when a baton needs to be freed and therefore is 
@@ -87,12 +89,14 @@
 Watchpoint::SetCallback (WatchpointHitCallback callback, const BatonSP &callback_baton_sp, bool is_synchronous)
 {
     m_options.SetCallback(callback, callback_baton_sp, is_synchronous);
+    SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
 }
 
 void
 Watchpoint::ClearCallback ()
 {
     m_options.ClearCallback ();
+    SendWatchpointChangedEvent (eWatchpointEventTypeCommandChanged);
 }
 
 void
@@ -297,7 +301,7 @@
 }
 
 void
-Watchpoint::SetEnabled(bool enabled)
+Watchpoint::SetEnabled(bool enabled, bool notify)
 {
     if (!enabled)
     {
@@ -309,14 +313,21 @@
         // Don't clear the snapshots for now.
         // Within StopInfo.cpp, we purposely do disable/enable watchpoint while performing watchpoint actions.
     }
+    bool changed = enabled != m_enabled;
     m_enabled = enabled;
+    if (notify && !m_is_ephemeral && changed)
+        SendWatchpointChangedEvent (enabled ? eWatchpointEventTypeEnabled : eWatchpointEventTypeDisabled);
 }
 
 void
-Watchpoint::SetWatchpointType (uint32_t type)
+Watchpoint::SetWatchpointType (uint32_t type, bool notify)
 {
+    int old_watch_read = m_watch_read;
+    int old_watch_write = m_watch_write;
     m_watch_read = (type & LLDB_WATCH_TYPE_READ) != 0;
     m_watch_write = (type & LLDB_WATCH_TYPE_WRITE) != 0;
+    if (notify && (old_watch_read != m_watch_read || old_watch_write != m_watch_write))
+        SendWatchpointChangedEvent (eWatchpointEventTypeTypeChanged);
 }
 
 bool
@@ -338,7 +349,10 @@
 void
 Watchpoint::SetIgnoreCount (uint32_t n)
 {
+    bool changed = m_ignore_count != n;
     m_ignore_count = n;
+    if (changed)
+        SendWatchpointChangedEvent (eWatchpointEventTypeIgnoreChanged);
 }
 
 bool
@@ -360,6 +374,7 @@
         // Pass NULL for expr_prefix (no translation-unit level definitions).
         m_condition_ap.reset(new ClangUserExpression (condition, NULL, lldb::eLanguageTypeUnknown, ClangUserExpression::eResultTypeAny));
     }
+    SendWatchpointChangedEvent (eWatchpointEventTypeConditionChanged);
 }
 
 const char *
@@ -371,3 +386,105 @@
         return NULL;
 }
 
+void
+Watchpoint::SendWatchpointChangedEvent (lldb::WatchpointEventType eventKind)
+{
+    if (!m_being_created
+        && GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
+    {
+        WatchpointEventData *data = new Watchpoint::WatchpointEventData (eventKind, shared_from_this());
+        GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged, data);
+    }
+}
+
+void
+Watchpoint::SendWatchpointChangedEvent (WatchpointEventData *data)
+{
+
+    if (data == NULL)
+        return;
+        
+    if (!m_being_created
+        && GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
+        GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged, data);
+    else
+        delete data;
+}
+
+Watchpoint::WatchpointEventData::WatchpointEventData (WatchpointEventType sub_type, 
+                                                      const WatchpointSP &new_watchpoint_sp) :
+    EventData (),
+    m_watchpoint_event (sub_type),
+    m_new_watchpoint_sp (new_watchpoint_sp)
+{
+}
+
+Watchpoint::WatchpointEventData::~WatchpointEventData ()
+{
+}
+
+const ConstString &
+Watchpoint::WatchpointEventData::GetFlavorString ()
+{
+    static ConstString g_flavor ("Watchpoint::WatchpointEventData");
+    return g_flavor;
+}
+
+const ConstString &
+Watchpoint::WatchpointEventData::GetFlavor () const
+{
+    return WatchpointEventData::GetFlavorString ();
+}
+
+
+WatchpointSP &
+Watchpoint::WatchpointEventData::GetWatchpoint ()
+{
+    return m_new_watchpoint_sp;
+}
+
+WatchpointEventType
+Watchpoint::WatchpointEventData::GetWatchpointEventType () const
+{
+    return m_watchpoint_event;
+}
+
+void
+Watchpoint::WatchpointEventData::Dump (Stream *s) const
+{
+}
+
+const Watchpoint::WatchpointEventData *
+Watchpoint::WatchpointEventData::GetEventDataFromEvent (const Event *event)
+{
+    if (event)
+    {
+        const EventData *event_data = event->GetData();
+        if (event_data && event_data->GetFlavor() == WatchpointEventData::GetFlavorString())
+            return static_cast <const WatchpointEventData *> (event->GetData());
+    }
+    return NULL;
+}
+
+WatchpointEventType
+Watchpoint::WatchpointEventData::GetWatchpointEventTypeFromEvent (const EventSP &event_sp)
+{
+    const WatchpointEventData *data = GetEventDataFromEvent (event_sp.get());
+
+    if (data == NULL)
+        return eWatchpointEventTypeInvalidType;
+    else
+        return data->GetWatchpointEventType();
+}
+
+WatchpointSP
+Watchpoint::WatchpointEventData::GetWatchpointFromEvent (const EventSP &event_sp)
+{
+    WatchpointSP wp_sp;
+
+    const WatchpointEventData *data = GetEventDataFromEvent (event_sp.get());
+    if (data)
+        wp_sp = data->m_new_watchpoint_sp;
+
+    return wp_sp;
+}
diff --git a/source/Breakpoint/WatchpointList.cpp b/source/Breakpoint/WatchpointList.cpp
index ad4e873..6d62dff 100644
--- a/source/Breakpoint/WatchpointList.cpp
+++ b/source/Breakpoint/WatchpointList.cpp
@@ -31,11 +31,17 @@
 
 // Add a watchpoint to the list.
 lldb::watch_id_t
-WatchpointList::Add (const WatchpointSP &wp_sp)
+WatchpointList::Add (const WatchpointSP &wp_sp, bool notify)
 {
     Mutex::Locker locker (m_mutex);
     wp_sp->SetID(++m_next_wp_id);
     m_watchpoints.push_back(wp_sp);
+    if (notify)
+    {
+        if (wp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
+            wp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
+                                               new Watchpoint::WatchpointEventData (eWatchpointEventTypeAdded, wp_sp));
+    }
     return wp_sp->GetID();
 }
 
@@ -200,12 +206,19 @@
 }
 
 bool
-WatchpointList::Remove (lldb::watch_id_t watch_id)
+WatchpointList::Remove (lldb::watch_id_t watch_id, bool notify)
 {
     Mutex::Locker locker (m_mutex);
     wp_collection::iterator pos = GetIDIterator(watch_id);
     if (pos != m_watchpoints.end())
     {
+        WatchpointSP wp_sp = *pos;
+        if (notify)
+        {
+            if (wp_sp->GetTarget().EventTypeHasListeners(Target::eBroadcastBitWatchpointChanged))
+                wp_sp->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
+                                                   new Watchpoint::WatchpointEventData (eWatchpointEventTypeRemoved, wp_sp));
+        }
         m_watchpoints.erase(pos);
         return true;
     }
@@ -264,9 +277,25 @@
 }
 
 void
-WatchpointList::RemoveAll ()
+WatchpointList::RemoveAll (bool notify)
 {
     Mutex::Locker locker(m_mutex);
+    if (notify)
+    {
+        
+        {
+            wp_collection::iterator pos, end = m_watchpoints.end();
+            for (pos = m_watchpoints.begin(); pos != end; ++pos)
+            {
+                if ((*pos)->GetTarget().EventTypeHasListeners(Target::eBroadcastBitBreakpointChanged))
+                {
+                    (*pos)->GetTarget().BroadcastEvent (Target::eBroadcastBitWatchpointChanged,
+                                                        new Watchpoint::WatchpointEventData (eWatchpointEventTypeRemoved,
+                                                                                             *pos));
+                }
+            }
+        }
+    }
     m_watchpoints.clear();
 }
 
diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
index 991cf68..7d33cce 100644
--- a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
+++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
@@ -650,7 +650,7 @@
 }
 
 Error
-ProcessKDP::EnableWatchpoint (Watchpoint *wp)
+ProcessKDP::EnableWatchpoint (Watchpoint *wp, bool notify)
 {
     Error error;
     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
@@ -658,7 +658,7 @@
 }
 
 Error
-ProcessKDP::DisableWatchpoint (Watchpoint *wp)
+ProcessKDP::DisableWatchpoint (Watchpoint *wp, bool notify)
 {
     Error error;
     error.SetErrorString ("watchpoints are not suppported in kdp remote debugging");
diff --git a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
index 9b24b4f..bc26f6e 100644
--- a/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
+++ b/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
@@ -183,10 +183,10 @@
     // Process Watchpoints
     //----------------------------------------------------------------------
     virtual lldb_private::Error
-    EnableWatchpoint (lldb_private::Watchpoint *wp);
+    EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
     
     virtual lldb_private::Error
-    DisableWatchpoint (lldb_private::Watchpoint *wp);
+    DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
     
     CommunicationKDP &
     GetCommunication()
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 9269c26..0b6f12e 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -2341,7 +2341,7 @@
 }
 
 Error
-ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp)
+ProcessGDBRemote::EnableWatchpoint (Watchpoint *wp, bool notify)
 {
     Error error;
     if (wp)
@@ -2364,7 +2364,7 @@
         {
             if (m_gdb_comm.SendGDBStoppointTypePacket(type, true, addr, wp->GetByteSize()) == 0)
             {
-                wp->SetEnabled(true);
+                wp->SetEnabled(true, notify);
                 return error;
             }
             else
@@ -2383,7 +2383,7 @@
 }
 
 Error
-ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp)
+ProcessGDBRemote::DisableWatchpoint (Watchpoint *wp, bool notify)
 {
     Error error;
     if (wp)
@@ -2393,6 +2393,7 @@
         LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_WATCHPOINTS));
 
         addr_t addr = wp->GetLoadAddress();
+
         if (log)
             log->Printf ("ProcessGDBRemote::DisableWatchpoint (watchID = %" PRIu64 ") addr = 0x%8.8" PRIx64, watchID, (uint64_t)addr);
 
@@ -2403,7 +2404,7 @@
             // See also 'class WatchpointSentry' within StopInfo.cpp.
             // This disabling attempt might come from the user-supplied actions, we'll route it in order for
             // the watchpoint object to intelligently process this action.
-            wp->SetEnabled(false);
+            wp->SetEnabled(false, notify);
             return error;
         }
         
@@ -2413,7 +2414,7 @@
             // Pass down an appropriate z/Z packet...
             if (m_gdb_comm.SendGDBStoppointTypePacket(type, false, addr, wp->GetByteSize()) == 0)
             {
-                wp->SetEnabled(false);
+                wp->SetEnabled(false, notify);
                 return error;
             }
             else
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
index d19878f..520dfc4 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
@@ -202,10 +202,10 @@
     // Process Watchpoints
     //----------------------------------------------------------------------
     virtual lldb_private::Error
-    EnableWatchpoint (lldb_private::Watchpoint *wp);
+    EnableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
 
     virtual lldb_private::Error
-    DisableWatchpoint (lldb_private::Watchpoint *wp);
+    DisableWatchpoint (lldb_private::Watchpoint *wp, bool notify = true);
 
     virtual lldb_private::Error
     GetWatchpointSupportInfo (uint32_t &num);
diff --git a/source/Target/Process.cpp b/source/Target/Process.cpp
index 38622f0..aac9385 100644
--- a/source/Target/Process.cpp
+++ b/source/Target/Process.cpp
@@ -2579,7 +2579,7 @@
 }
 
 Error
-Process::EnableWatchpoint (Watchpoint *watchpoint)
+Process::EnableWatchpoint (Watchpoint *watchpoint, bool notify)
 {
     Error error;
     error.SetErrorString("watchpoints are not supported");
@@ -2587,7 +2587,7 @@
 }
 
 Error
-Process::DisableWatchpoint (Watchpoint *watchpoint)
+Process::DisableWatchpoint (Watchpoint *watchpoint, bool notify)
 {
     Error error;
     error.SetErrorString("watchpoints are not supported");
diff --git a/source/Target/StopInfo.cpp b/source/Target/StopInfo.cpp
index 9d62dd9..77a960a 100644
--- a/source/Target/StopInfo.cpp
+++ b/source/Target/StopInfo.cpp
@@ -435,8 +435,9 @@
         {
             if (process && watchpoint)
             {
+                const bool notify = false;
                 watchpoint->TurnOnEphemeralMode();
-                process->DisableWatchpoint(watchpoint);
+                process->DisableWatchpoint(watchpoint, notify);
             }
         }
         ~WatchpointSentry()
@@ -444,7 +445,10 @@
             if (process && watchpoint)
             {
                 if (!watchpoint->IsDisabledDuringEphemeralMode())
-                    process->EnableWatchpoint(watchpoint);
+                {
+                    const bool notify = false;
+                    process->EnableWatchpoint(watchpoint, notify);
+                }
                 watchpoint->TurnOffEphemeralMode();
             }
         }
diff --git a/source/Target/Target.cpp b/source/Target/Target.cpp
index e373efe..de28ab1 100644
--- a/source/Target/Target.cpp
+++ b/source/Target/Target.cpp
@@ -101,6 +101,7 @@
     SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
     SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
     SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
+    SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
     
     CheckInWithManager();
 
@@ -575,6 +576,7 @@
     // of watchpoints limited by the hardware which the inferior is running on.
 
     // Grab the list mutex while doing operations.
+    const bool notify = false;   // Don't notify about all the state changes we do on creating the watchpoint.
     Mutex::Locker locker;
     this->GetWatchpointList().GetListMutex(locker);
     WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
@@ -587,28 +589,22 @@
         // Return the existing watchpoint if both size and type match.
         if (size == old_size && kind == old_type) {
             wp_sp = matched_sp;
-            wp_sp->SetEnabled(false);
+            wp_sp->SetEnabled(false, notify);
         } else {
             // Nil the matched watchpoint; we will be creating a new one.
-            m_process_sp->DisableWatchpoint(matched_sp.get());
-            m_watchpoint_list.Remove(matched_sp->GetID());
+            m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
+            m_watchpoint_list.Remove(matched_sp->GetID(), true);
         }
     }
 
     if (!wp_sp) 
     {
-        Watchpoint *new_wp = new Watchpoint(*this, addr, size, type);
-        if (!new_wp) 
-        {
-            error.SetErrorString("Watchpoint ctor failed, out of memory?");
-            return wp_sp;
-        }
-        new_wp->SetWatchpointType(kind);
-        wp_sp.reset(new_wp);
-        m_watchpoint_list.Add(wp_sp);
+        wp_sp.reset(new Watchpoint(*this, addr, size, type));
+        wp_sp->SetWatchpointType(kind, notify);
+        m_watchpoint_list.Add (wp_sp, true);
     }
 
-    error = m_process_sp->EnableWatchpoint(wp_sp.get());
+    error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
     if (log)
         log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
                     __FUNCTION__,
@@ -619,7 +615,7 @@
     {
         // Enabling the watchpoint on the device side failed.
         // Remove the said watchpoint from the list maintained by the target instance.
-        m_watchpoint_list.Remove(wp_sp->GetID());
+        m_watchpoint_list.Remove (wp_sp->GetID(), true);
         // See if we could provide more helpful error message.
         if (!CheckIfWatchpointsExhausted(this, error))
         {
@@ -755,7 +751,7 @@
         log->Printf ("Target::%s\n", __FUNCTION__);
 
     if (!end_to_end) {
-        m_watchpoint_list.RemoveAll();
+        m_watchpoint_list.RemoveAll(true);
         return true;
     }
 
@@ -775,7 +771,7 @@
         if (rc.Fail())
             return false;
     }
-    m_watchpoint_list.RemoveAll ();
+    m_watchpoint_list.RemoveAll (true);
     return true; // Success!
 }
 
@@ -945,7 +941,7 @@
 
     if (DisableWatchpointByID (watch_id))
     {
-        m_watchpoint_list.Remove(watch_id);
+        m_watchpoint_list.Remove(watch_id, true);
         return true;
     }
     return false;