1254721Semaste//===-- Watchpoint.h --------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef liblldb_Watchpoint_h_
11254721Semaste#define liblldb_Watchpoint_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste
15254721Semaste// C++ Includes
16254721Semaste#include <list>
17254721Semaste#include <string>
18254721Semaste
19254721Semaste// Other libraries and framework includes
20254721Semaste
21254721Semaste// Project includes
22254721Semaste#include "lldb/lldb-private.h"
23254721Semaste#include "lldb/Target/Target.h"
24254721Semaste#include "lldb/Core/UserID.h"
25254721Semaste#include "lldb/Breakpoint/WatchpointOptions.h"
26254721Semaste#include "lldb/Breakpoint/StoppointLocation.h"
27254721Semaste
28254721Semastenamespace lldb_private {
29254721Semaste
30254721Semasteclass Watchpoint :
31254721Semaste    public std::enable_shared_from_this<Watchpoint>,
32254721Semaste    public StoppointLocation
33254721Semaste{
34254721Semastepublic:
35254721Semaste
36254721Semaste    class WatchpointEventData :
37254721Semaste        public EventData
38254721Semaste    {
39254721Semaste    public:
40254721Semaste
41254721Semaste        static const ConstString &
42254721Semaste        GetFlavorString ();
43254721Semaste
44254721Semaste        virtual const ConstString &
45254721Semaste        GetFlavor () const;
46254721Semaste
47254721Semaste        WatchpointEventData (lldb::WatchpointEventType sub_type,
48254721Semaste                             const lldb::WatchpointSP &new_watchpoint_sp);
49254721Semaste
50254721Semaste        virtual
51254721Semaste        ~WatchpointEventData();
52254721Semaste
53254721Semaste        lldb::WatchpointEventType
54254721Semaste        GetWatchpointEventType () const;
55254721Semaste
56254721Semaste        lldb::WatchpointSP &
57254721Semaste        GetWatchpoint ();
58254721Semaste
59254721Semaste        virtual void
60254721Semaste        Dump (Stream *s) const;
61254721Semaste
62254721Semaste        static lldb::WatchpointEventType
63254721Semaste        GetWatchpointEventTypeFromEvent (const lldb::EventSP &event_sp);
64254721Semaste
65254721Semaste        static lldb::WatchpointSP
66254721Semaste        GetWatchpointFromEvent (const lldb::EventSP &event_sp);
67254721Semaste
68254721Semaste        static const WatchpointEventData *
69254721Semaste        GetEventDataFromEvent (const Event *event_sp);
70254721Semaste
71254721Semaste    private:
72254721Semaste
73254721Semaste        lldb::WatchpointEventType m_watchpoint_event;
74254721Semaste        lldb::WatchpointSP m_new_watchpoint_sp;
75254721Semaste
76254721Semaste        DISALLOW_COPY_AND_ASSIGN (WatchpointEventData);
77254721Semaste    };
78254721Semaste
79254721Semaste    Watchpoint (Target& target, lldb::addr_t addr, uint32_t size, const ClangASTType *type, bool hardware = true);
80254721Semaste    ~Watchpoint ();
81254721Semaste
82254721Semaste    void
83254721Semaste    IncrementFalseAlarmsAndReviseHitCount();
84254721Semaste
85254721Semaste    bool
86254721Semaste    IsEnabled () const;
87254721Semaste
88254721Semaste    void
89254721Semaste    SetEnabled (bool enabled, bool notify = true);
90254721Semaste
91254721Semaste    virtual bool
92254721Semaste    IsHardware () const;
93254721Semaste
94254721Semaste    virtual bool
95254721Semaste    ShouldStop (StoppointCallbackContext *context);
96254721Semaste
97254721Semaste    bool        WatchpointRead () const;
98254721Semaste    bool        WatchpointWrite () const;
99254721Semaste    uint32_t    GetIgnoreCount () const;
100254721Semaste    void        SetIgnoreCount (uint32_t n);
101254721Semaste    void        SetWatchpointType (uint32_t type, bool notify = true);
102254721Semaste    void        SetDeclInfo (const std::string &str);
103254721Semaste    std::string GetWatchSpec();
104254721Semaste    void        SetWatchSpec (const std::string &str);
105254721Semaste
106254721Semaste    // Snapshot management interface.
107254721Semaste    bool        IsWatchVariable() const;
108254721Semaste    void        SetWatchVariable(bool val);
109254721Semaste    bool        CaptureWatchedValue (const ExecutionContext &exe_ctx);
110254721Semaste
111254721Semaste    void        GetDescription (Stream *s, lldb::DescriptionLevel level);
112254721Semaste    void        Dump (Stream *s) const;
113254721Semaste    void        DumpSnapshots (Stream *s, const char * prefix = NULL) const;
114254721Semaste    void        DumpWithLevel (Stream *s, lldb::DescriptionLevel description_level) const;
115254721Semaste    Target      &GetTarget() { return m_target; }
116254721Semaste    const Error &GetError() { return m_error; }
117254721Semaste
118254721Semaste    //------------------------------------------------------------------
119254721Semaste    /// Returns the WatchpointOptions structure set for this watchpoint.
120254721Semaste    ///
121254721Semaste    /// @return
122254721Semaste    ///     A pointer to this watchpoint's WatchpointOptions.
123254721Semaste    //------------------------------------------------------------------
124254721Semaste    WatchpointOptions *
125254721Semaste    GetOptions () { return &m_options; }
126254721Semaste
127254721Semaste    //------------------------------------------------------------------
128254721Semaste    /// Set the callback action invoked when the watchpoint is hit.
129254721Semaste    ///
130254721Semaste    /// @param[in] callback
131254721Semaste    ///    The method that will get called when the watchpoint is hit.
132254721Semaste    /// @param[in] callback_baton
133254721Semaste    ///    A void * pointer that will get passed back to the callback function.
134254721Semaste    /// @param[in] is_synchronous
135254721Semaste    ///    If \b true the callback will be run on the private event thread
136254721Semaste    ///    before the stop event gets reported.  If false, the callback will get
137254721Semaste    ///    handled on the public event thead after the stop has been posted.
138254721Semaste    ///
139254721Semaste    /// @return
140254721Semaste    ///    \b true if the process should stop when you hit the watchpoint.
141254721Semaste    ///    \b false if it should continue.
142254721Semaste    //------------------------------------------------------------------
143254721Semaste    void
144254721Semaste    SetCallback (WatchpointHitCallback callback,
145254721Semaste                 void *callback_baton,
146254721Semaste                 bool is_synchronous = false);
147254721Semaste
148254721Semaste    void
149254721Semaste    SetCallback (WatchpointHitCallback callback,
150254721Semaste                 const lldb::BatonSP &callback_baton_sp,
151254721Semaste                 bool is_synchronous = false);
152254721Semaste
153254721Semaste    void        ClearCallback();
154254721Semaste
155254721Semaste    //------------------------------------------------------------------
156254721Semaste    /// Invoke the callback action when the watchpoint is hit.
157254721Semaste    ///
158254721Semaste    /// @param[in] context
159254721Semaste    ///     Described the watchpoint event.
160254721Semaste    ///
161254721Semaste    /// @return
162254721Semaste    ///     \b true if the target should stop at this watchpoint and \b false not.
163254721Semaste    //------------------------------------------------------------------
164254721Semaste    bool
165254721Semaste    InvokeCallback (StoppointCallbackContext *context);
166254721Semaste
167254721Semaste    //------------------------------------------------------------------
168254721Semaste    // Condition
169254721Semaste    //------------------------------------------------------------------
170254721Semaste    //------------------------------------------------------------------
171254721Semaste    /// Set the watchpoint's condition.
172254721Semaste    ///
173254721Semaste    /// @param[in] condition
174254721Semaste    ///    The condition expression to evaluate when the watchpoint is hit.
175254721Semaste    ///    Pass in NULL to clear the condition.
176254721Semaste    //------------------------------------------------------------------
177254721Semaste    void SetCondition (const char *condition);
178254721Semaste
179254721Semaste    //------------------------------------------------------------------
180254721Semaste    /// Return a pointer to the text of the condition expression.
181254721Semaste    ///
182254721Semaste    /// @return
183254721Semaste    ///    A pointer to the condition expression text, or NULL if no
184254721Semaste    //     condition has been set.
185254721Semaste    //------------------------------------------------------------------
186254721Semaste    const char *GetConditionText () const;
187254721Semaste
188254721Semaste    void
189254721Semaste    TurnOnEphemeralMode();
190254721Semaste
191254721Semaste    void
192254721Semaste    TurnOffEphemeralMode();
193254721Semaste
194254721Semaste    bool
195254721Semaste    IsDisabledDuringEphemeralMode();
196254721Semaste
197254721Semaste    const ClangASTType &
198254721Semaste    GetClangASTType()
199254721Semaste    {
200254721Semaste        return m_type;
201254721Semaste    }
202254721Semaste
203254721Semaste
204254721Semasteprivate:
205254721Semaste    friend class Target;
206254721Semaste    friend class WatchpointList;
207254721Semaste
208254721Semaste    void        ResetHitCount() { m_hit_count = 0; }
209254721Semaste
210254721Semaste    Target      &m_target;
211254721Semaste    bool        m_enabled;             // Is this watchpoint enabled
212254721Semaste    bool        m_is_hardware;         // Is this a hardware watchpoint
213254721Semaste    bool        m_is_watch_variable;   // True if set via 'watchpoint set variable'.
214254721Semaste    bool        m_is_ephemeral;        // True if the watchpoint is in the ephemeral mode, meaning that it is
215254721Semaste                                       // undergoing a pair of temporary disable/enable actions to avoid recursively
216254721Semaste                                       // triggering further watchpoint events.
217254721Semaste    uint32_t    m_disabled_count;      // Keep track of the count that the watchpoint is disabled while in ephemeral mode.
218254721Semaste                                       // At the end of the ephemeral mode when the watchpoint is to be enabled agian,
219254721Semaste                                       // we check the count, if it is more than 1, it means the user-supplied actions
220254721Semaste                                       // actually want the watchpoint to be disabled!
221254721Semaste    uint32_t    m_watch_read:1,        // 1 if we stop when the watched data is read from
222254721Semaste                m_watch_write:1,       // 1 if we stop when the watched data is written to
223254721Semaste                m_watch_was_read:1,    // Set to 1 when watchpoint is hit for a read access
224254721Semaste                m_watch_was_written:1; // Set to 1 when watchpoint is hit for a write access
225254721Semaste    uint32_t    m_ignore_count;        // Number of times to ignore this watchpoint
226254721Semaste    uint32_t    m_false_alarms;        // Number of false alarms.
227254721Semaste    std::string m_decl_str;            // Declaration information, if any.
228254721Semaste    std::string m_watch_spec_str;      // Spec for the watchpoint.
229254721Semaste    lldb::ValueObjectSP m_old_value_sp;
230254721Semaste    lldb::ValueObjectSP m_new_value_sp;
231254721Semaste    ClangASTType m_type;
232254721Semaste    Error       m_error;               // An error object describing errors associated with this watchpoint.
233254721Semaste    WatchpointOptions m_options;       // Settable watchpoint options, which is a delegate to handle
234254721Semaste                                       // the callback machinery.
235254721Semaste    bool        m_being_created;
236254721Semaste
237254721Semaste    std::unique_ptr<ClangUserExpression> m_condition_ap;  // The condition to test.
238254721Semaste
239254721Semaste    void SetID(lldb::watch_id_t id) { m_loc_id = id; }
240254721Semaste
241254721Semaste    void
242254721Semaste    SendWatchpointChangedEvent (lldb::WatchpointEventType eventKind);
243254721Semaste
244254721Semaste    void
245254721Semaste    SendWatchpointChangedEvent (WatchpointEventData *data);
246254721Semaste
247254721Semaste    DISALLOW_COPY_AND_ASSIGN (Watchpoint);
248254721Semaste};
249254721Semaste
250254721Semaste} // namespace lldb_private
251254721Semaste
252254721Semaste#endif  // liblldb_Watchpoint_h_
253