[Lldb-commits] [lldb] r170400 - in /lldb/trunk: include/lldb/ include/lldb/API/ include/lldb/Breakpoint/ include/lldb/Target/ scripts/Python/interface/ source/API/ source/Breakpoint/ source/Plugins/Process/MacOSX-Kernel/ source/Plugins/Process/gdb-remote/ source/Target/ test/functionalities/watchpoint/watchpoint_events/

Jim Ingham jingham at apple.com
Mon Dec 17 18:03:50 PST 2012


Author: jingham
Date: Mon Dec 17 20:03:49 2012
New Revision: 170400

URL: http://llvm.org/viewvc/llvm-project?rev=170400&view=rev
Log:
Adding events when watchpoints are set or changed.

<rdar://problem/11597849>

Added:
    lldb/trunk/test/functionalities/watchpoint/watchpoint_events/
    lldb/trunk/test/functionalities/watchpoint/watchpoint_events/Makefile
    lldb/trunk/test/functionalities/watchpoint/watchpoint_events/TestWatchpointEvents.py
    lldb/trunk/test/functionalities/watchpoint/watchpoint_events/main.c
Modified:
    lldb/trunk/include/lldb/API/SBEvent.h
    lldb/trunk/include/lldb/API/SBFrame.h
    lldb/trunk/include/lldb/API/SBTarget.h
    lldb/trunk/include/lldb/API/SBWatchpoint.h
    lldb/trunk/include/lldb/Breakpoint/Watchpoint.h
    lldb/trunk/include/lldb/Breakpoint/WatchpointList.h
    lldb/trunk/include/lldb/Target/Process.h
    lldb/trunk/include/lldb/Target/Target.h
    lldb/trunk/include/lldb/lldb-enumerations.h
    lldb/trunk/scripts/Python/interface/SBTarget.i
    lldb/trunk/scripts/Python/interface/SBWatchpoint.i
    lldb/trunk/source/API/SBWatchpoint.cpp
    lldb/trunk/source/Breakpoint/BreakpointList.cpp
    lldb/trunk/source/Breakpoint/Watchpoint.cpp
    lldb/trunk/source/Breakpoint/WatchpointList.cpp
    lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
    lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
    lldb/trunk/source/Target/Process.cpp
    lldb/trunk/source/Target/StopInfo.cpp
    lldb/trunk/source/Target/Target.cpp

Modified: lldb/trunk/include/lldb/API/SBEvent.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBEvent.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBEvent.h (original)
+++ lldb/trunk/include/lldb/API/SBEvent.h Mon Dec 17 20:03:49 2012
@@ -75,6 +75,7 @@
     friend class SBDebugger;
     friend class SBProcess;
     friend class SBThread;
+    friend class SBWatchpoint;
 
     SBEvent (lldb::EventSP &event_sp);
 

Modified: lldb/trunk/include/lldb/API/SBFrame.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBFrame.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBFrame.h (original)
+++ lldb/trunk/include/lldb/API/SBFrame.h Mon Dec 17 20:03:49 2012
@@ -12,7 +12,6 @@
 
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBValueList.h"
-#include "lldb/API/SBWatchpoint.h"
 
 namespace lldb {
 

Modified: lldb/trunk/include/lldb/API/SBTarget.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBTarget.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBTarget.h (original)
+++ lldb/trunk/include/lldb/API/SBTarget.h Mon Dec 17 20:03:49 2012
@@ -235,7 +235,8 @@
     {
         eBroadcastBitBreakpointChanged  = (1 << 0),
         eBroadcastBitModulesLoaded      = (1 << 1),
-        eBroadcastBitModulesUnloaded    = (1 << 2)
+        eBroadcastBitModulesUnloaded    = (1 << 2),
+        eBroadcastBitWatchpointChanged  = (1 << 3)
     };
 
     //------------------------------------------------------------------

Modified: lldb/trunk/include/lldb/API/SBWatchpoint.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/API/SBWatchpoint.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/API/SBWatchpoint.h (original)
+++ lldb/trunk/include/lldb/API/SBWatchpoint.h Mon Dec 17 20:03:49 2012
@@ -81,6 +81,15 @@
     void
     SetSP (const lldb::WatchpointSP &sp);
 
+    static bool
+    EventIsWatchpointEvent (const lldb::SBEvent &event);
+    
+    static lldb::WatchpointEventType
+    GetWatchpointEventTypeFromEvent (const lldb::SBEvent& event);
+
+    static lldb::SBWatchpoint
+    GetWatchpointFromEvent (const lldb::SBEvent& event);
+
 private:
     friend class SBTarget;
     friend class SBValue;

Modified: lldb/trunk/include/lldb/Breakpoint/Watchpoint.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Breakpoint/Watchpoint.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Breakpoint/Watchpoint.h (original)
+++ lldb/trunk/include/lldb/Breakpoint/Watchpoint.h Mon Dec 17 20:03:49 2012
@@ -28,10 +28,54 @@
 namespace lldb_private {
 
 class Watchpoint :
+    public STD_ENABLE_SHARED_FROM_THIS(Watchpoint),
     public StoppointLocation
 {
 public:
 
+    class WatchpointEventData :
+        public EventData
+    {
+    public:
+
+        static const ConstString &
+        GetFlavorString ();
+
+        virtual const ConstString &
+        GetFlavor () const;
+
+        WatchpointEventData (lldb::WatchpointEventType sub_type,
+                             const lldb::WatchpointSP &new_watchpoint_sp);
+
+        virtual
+        ~WatchpointEventData();
+
+        lldb::WatchpointEventType
+        GetWatchpointEventType () const;
+
+        lldb::WatchpointSP &
+        GetWatchpoint ();
+        
+        virtual void
+        Dump (Stream *s) const;
+
+        static lldb::WatchpointEventType
+        GetWatchpointEventTypeFromEvent (const lldb::EventSP &event_sp);
+
+        static lldb::WatchpointSP
+        GetWatchpointFromEvent (const lldb::EventSP &event_sp);
+
+        static const WatchpointEventData *
+        GetEventDataFromEvent (const Event *event_sp);
+
+    private:
+
+        lldb::WatchpointEventType m_watchpoint_event;
+        lldb::WatchpointSP m_new_watchpoint_sp;
+
+        DISALLOW_COPY_AND_ASSIGN (WatchpointEventData);
+    };
+
     Watchpoint (Target& target, lldb::addr_t addr, size_t size, const ClangASTType *type, bool hardware = true);
     ~Watchpoint ();
 
@@ -42,7 +86,7 @@
     IsEnabled () const;
 
     void
-    SetEnabled (bool enabled);
+    SetEnabled (bool enabled, bool notify = true);
 
     virtual bool
     IsHardware () const;
@@ -54,7 +98,7 @@
     bool        WatchpointWrite () const;
     uint32_t    GetIgnoreCount () const;
     void        SetIgnoreCount (uint32_t n);
-    void        SetWatchpointType (uint32_t type);
+    void        SetWatchpointType (uint32_t type, bool notify = true);
     void        SetDeclInfo (const std::string &str);
     std::string GetWatchSpec();
     void        SetWatchSpec (const std::string &str);
@@ -188,10 +232,17 @@
     Error       m_error;               // An error object describing errors associated with this watchpoint.
     WatchpointOptions m_options;       // Settable watchpoint options, which is a delegate to handle
                                        // the callback machinery.
+    bool        m_being_created;
 
     std::auto_ptr<ClangUserExpression> m_condition_ap;  // The condition to test.
 
     void SetID(lldb::watch_id_t id) { m_loc_id = id; }
+    
+    void
+    SendWatchpointChangedEvent (lldb::WatchpointEventType eventKind);
+
+    void
+    SendWatchpointChangedEvent (WatchpointEventData *data);
 
     DISALLOW_COPY_AND_ASSIGN (Watchpoint);
 };

Modified: lldb/trunk/include/lldb/Breakpoint/WatchpointList.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Breakpoint/WatchpointList.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Breakpoint/WatchpointList.h (original)
+++ lldb/trunk/include/lldb/Breakpoint/WatchpointList.h Mon Dec 17 20:03:49 2012
@@ -58,7 +58,7 @@
     ///    The ID of the Watchpoint in the list.
     //------------------------------------------------------------------
     lldb::watch_id_t
-    Add (const lldb::WatchpointSP& wp_sp);
+    Add (const lldb::WatchpointSP& wp_sp, bool notify);
 
     //------------------------------------------------------------------
     /// Standard "Dump" method.
@@ -180,7 +180,7 @@
     ///   \b true if the watchpoint \a watchID was in the list.
     //------------------------------------------------------------------
     bool
-    Remove (lldb::watch_id_t watchID);
+    Remove (lldb::watch_id_t watchID, bool notify);
 
     //------------------------------------------------------------------
     /// Returns the number hit count of all watchpoints in this list.
@@ -241,7 +241,7 @@
     SetEnabledAll (bool enabled);
 
     void
-    RemoveAll ();
+    RemoveAll (bool notify);
     
     //------------------------------------------------------------------
     /// Sets the passed in Locker to hold the Watchpoint List mutex.

Modified: lldb/trunk/include/lldb/Target/Process.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Process.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Process.h (original)
+++ lldb/trunk/include/lldb/Target/Process.h Mon Dec 17 20:03:49 2012
@@ -3113,10 +3113,10 @@
     // Process Watchpoints (optional)
     //----------------------------------------------------------------------
     virtual Error
-    EnableWatchpoint (Watchpoint *wp);
+    EnableWatchpoint (Watchpoint *wp, bool notify = true);
 
     virtual Error
-    DisableWatchpoint (Watchpoint *wp);
+    DisableWatchpoint (Watchpoint *wp, bool notify = true);
 
     //------------------------------------------------------------------
     // Thread Queries

Modified: lldb/trunk/include/lldb/Target/Target.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Target.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Target.h (original)
+++ lldb/trunk/include/lldb/Target/Target.h Mon Dec 17 20:03:49 2012
@@ -278,7 +278,8 @@
     {
         eBroadcastBitBreakpointChanged  = (1 << 0),
         eBroadcastBitModulesLoaded      = (1 << 1),
-        eBroadcastBitModulesUnloaded    = (1 << 2)
+        eBroadcastBitModulesUnloaded    = (1 << 2),
+        eBroadcastBitWatchpointChanged  = (1 << 3)
     };
     
     // These two functions fill out the Broadcaster interface:

Modified: lldb/trunk/include/lldb/lldb-enumerations.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/lldb-enumerations.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/include/lldb/lldb-enumerations.h (original)
+++ lldb/trunk/include/lldb/lldb-enumerations.h Mon Dec 17 20:03:49 2012
@@ -300,6 +300,20 @@
         eBreakpointEventTypeThreadChanged       = (1u << 11)
     } BreakpointEventType;
 
+    typedef enum WatchpointEventType
+    {
+        eWatchpointEventTypeInvalidType         = (1u << 0),
+        eWatchpointEventTypeAdded               = (1u << 1),
+        eWatchpointEventTypeRemoved             = (1u << 2),
+        eWatchpointEventTypeEnabled             = (1u << 6),
+        eWatchpointEventTypeDisabled            = (1u << 7),
+        eWatchpointEventTypeCommandChanged      = (1u << 8),
+        eWatchpointEventTypeConditionChanged    = (1u << 9),
+        eWatchpointEventTypeIgnoreChanged       = (1u << 10),
+        eWatchpointEventTypeThreadChanged       = (1u << 11),
+        eWatchpointEventTypeTypeChanged         = (1u << 12)
+    } WatchpointEventType;
+
 
     //----------------------------------------------------------------------
     /// Programming language type.

Modified: lldb/trunk/scripts/Python/interface/SBTarget.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBTarget.i?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBTarget.i (original)
+++ lldb/trunk/scripts/Python/interface/SBTarget.i Mon Dec 17 20:03:49 2012
@@ -238,7 +238,8 @@
     {
         eBroadcastBitBreakpointChanged  = (1 << 0),
         eBroadcastBitModulesLoaded      = (1 << 1),
-        eBroadcastBitModulesUnloaded    = (1 << 2)
+        eBroadcastBitModulesUnloaded    = (1 << 2),
+        eBroadcastBitWatchpointChanged  = (1 << 3)
     };
 
     //------------------------------------------------------------------

Modified: lldb/trunk/scripts/Python/interface/SBWatchpoint.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBWatchpoint.i?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBWatchpoint.i (original)
+++ lldb/trunk/scripts/Python/interface/SBWatchpoint.i Mon Dec 17 20:03:49 2012
@@ -84,6 +84,16 @@
     
     bool
     GetDescription (lldb::SBStream &description, DescriptionLevel level);
+
+    static bool
+    EventIsWatchpointEvent (const lldb::SBEvent &event);
+    
+    static lldb::WatchpointEventType
+    GetWatchpointEventTypeFromEvent (const lldb::SBEvent& event);
+
+    static lldb::SBWatchpoint
+    GetWatchpointFromEvent (const lldb::SBEvent& event);
+
 };
 
 } // namespace lldb

Modified: lldb/trunk/source/API/SBWatchpoint.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBWatchpoint.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/API/SBWatchpoint.cpp (original)
+++ lldb/trunk/source/API/SBWatchpoint.cpp Mon Dec 17 20:03:49 2012
@@ -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;
+}

Modified: lldb/trunk/source/Breakpoint/BreakpointList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/BreakpointList.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/BreakpointList.cpp (original)
+++ lldb/trunk/source/Breakpoint/BreakpointList.cpp Mon Dec 17 20:03:49 2012
@@ -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());
 }

Modified: lldb/trunk/source/Breakpoint/Watchpoint.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/Watchpoint.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/Watchpoint.cpp (original)
+++ lldb/trunk/source/Breakpoint/Watchpoint.cpp Mon Dec 17 20:03:49 2012
@@ -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;
+}

Modified: lldb/trunk/source/Breakpoint/WatchpointList.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Breakpoint/WatchpointList.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Breakpoint/WatchpointList.cpp (original)
+++ lldb/trunk/source/Breakpoint/WatchpointList.cpp Mon Dec 17 20:03:49 2012
@@ -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();
 }
 

Modified: lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp (original)
+++ lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.cpp Mon Dec 17 20:03:49 2012
@@ -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");

Modified: lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h (original)
+++ lldb/trunk/source/Plugins/Process/MacOSX-Kernel/ProcessKDP.h Mon Dec 17 20:03:49 2012
@@ -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()

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp Mon Dec 17 20:03:49 2012
@@ -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

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/ProcessGDBRemote.h Mon Dec 17 20:03:49 2012
@@ -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);

Modified: lldb/trunk/source/Target/Process.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Process.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Target/Process.cpp (original)
+++ lldb/trunk/source/Target/Process.cpp Mon Dec 17 20:03:49 2012
@@ -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");

Modified: lldb/trunk/source/Target/StopInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/StopInfo.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Target/StopInfo.cpp (original)
+++ lldb/trunk/source/Target/StopInfo.cpp Mon Dec 17 20:03:49 2012
@@ -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();
             }
         }

Modified: lldb/trunk/source/Target/Target.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Target.cpp?rev=170400&r1=170399&r2=170400&view=diff
==============================================================================
--- lldb/trunk/source/Target/Target.cpp (original)
+++ lldb/trunk/source/Target/Target.cpp Mon Dec 17 20:03:49 2012
@@ -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;

Added: lldb/trunk/test/functionalities/watchpoint/watchpoint_events/Makefile
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/watchpoint_events/Makefile?rev=170400&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/watchpoint/watchpoint_events/Makefile (added)
+++ lldb/trunk/test/functionalities/watchpoint/watchpoint_events/Makefile Mon Dec 17 20:03:49 2012
@@ -0,0 +1,5 @@
+LEVEL = ../../../make
+
+C_SOURCES := main.c
+
+include $(LEVEL)/Makefile.rules

Added: lldb/trunk/test/functionalities/watchpoint/watchpoint_events/TestWatchpointEvents.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/watchpoint_events/TestWatchpointEvents.py?rev=170400&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/watchpoint/watchpoint_events/TestWatchpointEvents.py (added)
+++ lldb/trunk/test/functionalities/watchpoint/watchpoint_events/TestWatchpointEvents.py Mon Dec 17 20:03:49 2012
@@ -0,0 +1,103 @@
+"""Test that adding, deleting and modifying watchpoints sends the appropriate events."""
+
+import os, time
+import unittest2
+import lldb
+import lldbutil
+from lldbtest import *
+
+class TestWatchpointEvents (TestBase):
+
+    mydir = os.path.join("functionalities", "watchpoint", "watchpoint_events")
+
+    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
+    @python_api_test
+    @dsym_test
+    def test_with_dsym_and_python_api(self):
+        """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
+        self.buildDsym()
+        self.step_over_stepping()
+
+    @expectedFailureLinux # bugzilla 14437
+    @python_api_test
+    @dwarf_test
+    def test_with_dwarf_and_python_api(self):
+        """Test that adding, deleting and modifying watchpoints sends the appropriate events."""
+        self.buildDwarf()
+        self.step_over_stepping()
+
+    def setUp(self):
+        # Call super's setUp().
+        TestBase.setUp(self)
+        # Find the line numbers that we will step to in main:
+        self.main_source = "main.c"
+
+    def GetWatchpointEvent (self, event_type):
+        # We added a watchpoint so we should get a watchpoint added event.
+        event = lldb.SBEvent()
+        success = self.listener.WaitForEvent (1, event)
+        self.assertTrue(success == True, "Successfully got watchpoint event")
+        self.assertTrue (lldb.SBWatchpoint.EventIsWatchpointEvent(event), "Event is a watchpoint event.")
+        found_type = lldb.SBWatchpoint.GetWatchpointEventTypeFromEvent (event)
+        self.assertTrue (found_type == event_type, "Event is not correct type, expected: %d, found: %d"%(event_type, found_type))
+        # There shouldn't be another event waiting around:
+        found_event = self.listener.PeekAtNextEventForBroadcasterWithType (self.target_bcast, lldb.SBTarget.eBroadcastBitBreakpointChanged, event)
+        if found_event:
+            print "Found an event I didn't expect: ", event
+
+        self.assertTrue (not found_event, "Only one event per change.")
+
+    def step_over_stepping(self):
+        """Use Python APIs to test stepping over and hitting breakpoints."""
+        exe = os.path.join(os.getcwd(), "a.out")
+
+        target = self.dbg.CreateTarget(exe)
+        self.assertTrue(target, VALID_TARGET)
+
+        self.main_source_spec = lldb.SBFileSpec (self.main_source)
+
+        break_in_main = target.BreakpointCreateBySourceRegex ('// Put a breakpoint here.', self.main_source_spec)
+        self.assertTrue(break_in_main, VALID_BREAKPOINT)
+
+        # Now launch the process, and do not stop at entry point.
+        process = target.LaunchSimple (None, None, os.getcwd())
+
+        self.assertTrue(process, PROCESS_IS_VALID)
+
+        # The stop reason of the thread should be breakpoint.
+        threads = lldbutil.get_threads_stopped_at_breakpoint (process, break_in_main)
+
+        if len(threads) != 1:
+            self.fail ("Failed to stop at first breakpoint in main.")
+
+        thread = threads[0]
+        frame = thread.GetFrameAtIndex(0)
+        local_var = frame.FindVariable ("local_var")
+        self.assertTrue (local_var.IsValid())
+
+        self.listener = lldb.SBListener("com.lldb.testsuite_listener")
+        self.target_bcast = target.GetBroadcaster()
+        self.target_bcast.AddListener (self.listener, lldb.SBTarget.eBroadcastBitWatchpointChanged)
+        self.listener.StartListeningForEvents (self.target_bcast, lldb.SBTarget.eBroadcastBitWatchpointChanged)
+
+        error = lldb.SBError()
+        local_watch = local_var.Watch(True, True, True, error)
+        if not error.Success():
+            self.fail ("Failed to make watchpoint for local_var: %s"%(error.GetCString()))
+
+        self.GetWatchpointEvent (lldb.eWatchpointEventTypeAdded)
+        # Now change some of the features of this watchpoint and make sure we get events:
+        local_watch.SetEnabled(False)
+        self.GetWatchpointEvent (lldb.eWatchpointEventTypeDisabled)
+
+        local_watch.SetIgnoreCount(10)
+        self.GetWatchpointEvent (lldb.eWatchpointEventTypeIgnoreChanged)
+
+        local_watch.SetCondition ("1 == 2")
+        self.GetWatchpointEvent (lldb.eWatchpointEventTypeConditionChanged)
+
+if __name__ == '__main__':
+    import atexit
+    lldb.SBDebugger.Initialize()
+    atexit.register(lambda: lldb.SBDebugger.Terminate())
+    unittest2.main()

Added: lldb/trunk/test/functionalities/watchpoint/watchpoint_events/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/functionalities/watchpoint/watchpoint_events/main.c?rev=170400&view=auto
==============================================================================
--- lldb/trunk/test/functionalities/watchpoint/watchpoint_events/main.c (added)
+++ lldb/trunk/test/functionalities/watchpoint/watchpoint_events/main.c Mon Dec 17 20:03:49 2012
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+int 
+main (int argc, char **argv)
+{
+  int local_var = 10; 
+  printf ("local_var is: %d.\n", local_var++); // Put a breakpoint here.
+  return local_var;
+}





More information about the lldb-commits mailing list