1254721Semaste//===-- WatchpointOptions.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_WatchpointOptions_h_
11254721Semaste#define liblldb_WatchpointOptions_h_
12254721Semaste
13254721Semaste// C Includes
14254721Semaste// C++ Includes
15254721Semaste// Other libraries and framework includes
16254721Semaste// Project includes
17254721Semaste#include "lldb/lldb-private.h"
18254721Semaste#include "lldb/Core/Baton.h"
19254721Semaste#include "lldb/Core/StringList.h"
20254721Semaste
21254721Semastenamespace lldb_private {
22254721Semaste
23254721Semaste//----------------------------------------------------------------------
24254721Semaste/// @class WatchpointOptions WatchpointOptions.h "lldb/Breakpoint/WatchpointOptions.h"
25254721Semaste/// @brief Class that manages the options on a watchpoint.
26254721Semaste//----------------------------------------------------------------------
27254721Semaste
28254721Semasteclass WatchpointOptions
29254721Semaste{
30254721Semastepublic:
31254721Semaste    //------------------------------------------------------------------
32254721Semaste    // Constructors and Destructors
33254721Semaste    //------------------------------------------------------------------
34254721Semaste    //------------------------------------------------------------------
35254721Semaste    /// Default constructor.  The watchpoint is enabled, and has no condition,
36254721Semaste    /// callback, ignore count, etc...
37254721Semaste    //------------------------------------------------------------------
38254721Semaste    WatchpointOptions();
39254721Semaste    WatchpointOptions(const WatchpointOptions& rhs);
40254721Semaste
41254721Semaste    static WatchpointOptions *
42254721Semaste    CopyOptionsNoCallback (WatchpointOptions &rhs);
43254721Semaste    //------------------------------------------------------------------
44254721Semaste    /// This constructor allows you to specify all the watchpoint options.
45254721Semaste    ///
46254721Semaste    /// @param[in] callback
47254721Semaste    ///    This is the plugin for some code that gets run, returns \b true if we are to stop.
48254721Semaste    ///
49254721Semaste    /// @param[in] baton
50254721Semaste    ///    Client data that will get passed to the callback.
51254721Semaste    ///
52254721Semaste    /// @param[in] thread_id
53254721Semaste    ///    Only stop if \a thread_id hits the watchpoint.
54254721Semaste    //------------------------------------------------------------------
55254721Semaste    WatchpointOptions(WatchpointHitCallback callback,
56254721Semaste                      void *baton,
57254721Semaste                      lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID);
58254721Semaste
59254721Semaste    virtual ~WatchpointOptions();
60254721Semaste
61254721Semaste    //------------------------------------------------------------------
62254721Semaste    // Operators
63254721Semaste    //------------------------------------------------------------------
64254721Semaste    const WatchpointOptions&
65254721Semaste    operator=(const WatchpointOptions& rhs);
66254721Semaste
67254721Semaste    //------------------------------------------------------------------
68254721Semaste    // Callbacks
69254721Semaste    //
70254721Semaste    // Watchpoint callbacks come in two forms, synchronous and asynchronous.  Synchronous callbacks will get
71254721Semaste    // run before any of the thread plans are consulted, and if they return false the target will continue
72254721Semaste    // "under the radar" of the thread plans.  There are a couple of restrictions to synchronous callbacks:
73254721Semaste    // 1) They should NOT resume the target themselves.  Just return false if you want the target to restart.
74254721Semaste    // 2) Watchpoints with synchronous callbacks can't have conditions (or rather, they can have them, but they
75254721Semaste    //    won't do anything.  Ditto with ignore counts, etc...  You are supposed to control that all through the
76254721Semaste    //    callback.
77254721Semaste    // Asynchronous callbacks get run as part of the "ShouldStop" logic in the thread plan.  The logic there is:
78254721Semaste    //   a) If the watchpoint is thread specific and not for this thread, continue w/o running the callback.
79254721Semaste    //   b) If the ignore count says we shouldn't stop, then ditto.
80254721Semaste    //   c) If the condition says we shouldn't stop, then ditto.
81254721Semaste    //   d) Otherwise, the callback will get run, and if it returns true we will stop, and if false we won't.
82254721Semaste    //  The asynchronous callback can run the target itself, but at present that should be the last action the
83254721Semaste    //  callback does.  We will relax this condition at some point, but it will take a bit of plumbing to get
84254721Semaste    //  that to work.
85254721Semaste    //
86254721Semaste    //------------------------------------------------------------------
87254721Semaste
88254721Semaste    //------------------------------------------------------------------
89254721Semaste    /// Adds a callback to the watchpoint option set.
90254721Semaste    ///
91254721Semaste    /// @param[in] callback
92254721Semaste    ///    The function to be called when the watchpoint gets hit.
93254721Semaste    ///
94254721Semaste    /// @param[in] baton_sp
95254721Semaste    ///    A baton which will get passed back to the callback when it is invoked.
96254721Semaste    ///
97254721Semaste    /// @param[in] synchronous
98254721Semaste    ///    Whether this is a synchronous or asynchronous callback.  See discussion above.
99254721Semaste    //------------------------------------------------------------------
100254721Semaste    void SetCallback (WatchpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous = false);
101254721Semaste
102254721Semaste
103254721Semaste    //------------------------------------------------------------------
104254721Semaste    /// Remove the callback from this option set.
105254721Semaste    //------------------------------------------------------------------
106254721Semaste    void ClearCallback ();
107254721Semaste
108254721Semaste    // The rest of these functions are meant to be used only within the watchpoint handling mechanism.
109254721Semaste
110254721Semaste    //------------------------------------------------------------------
111254721Semaste    /// Use this function to invoke the callback for a specific stop.
112254721Semaste    ///
113254721Semaste    /// @param[in] context
114254721Semaste    ///    The context in which the callback is to be invoked.  This includes the stop event, the
115254721Semaste    ///    execution context of the stop (since you might hit the same watchpoint on multiple threads) and
116254721Semaste    ///    whether we are currently executing synchronous or asynchronous callbacks.
117254721Semaste    ///
118254721Semaste    /// @param[in] watch_id
119254721Semaste    ///    The watchpoint ID that owns this option set.
120254721Semaste    ///
121254721Semaste    /// @return
122254721Semaste    ///     The callback return value.
123254721Semaste    //------------------------------------------------------------------
124254721Semaste    bool InvokeCallback (StoppointCallbackContext *context, lldb::user_id_t watch_id);
125254721Semaste
126254721Semaste    //------------------------------------------------------------------
127254721Semaste    /// Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
128254721Semaste    ///
129254721Semaste    /// @return
130254721Semaste    ///     The synchronicity of our callback.
131254721Semaste    //------------------------------------------------------------------
132254721Semaste    bool IsCallbackSynchronous () {
133254721Semaste        return m_callback_is_synchronous;
134254721Semaste    }
135254721Semaste
136254721Semaste    //------------------------------------------------------------------
137254721Semaste    /// Fetch the baton from the callback.
138254721Semaste    ///
139254721Semaste    /// @return
140254721Semaste    ///     The baton.
141254721Semaste    //------------------------------------------------------------------
142254721Semaste    Baton *GetBaton ();
143254721Semaste
144254721Semaste    //------------------------------------------------------------------
145254721Semaste    /// Fetch  a const version of the baton from the callback.
146254721Semaste    ///
147254721Semaste    /// @return
148254721Semaste    ///     The baton.
149254721Semaste    //------------------------------------------------------------------
150254721Semaste    const Baton *GetBaton () const;
151254721Semaste
152254721Semaste    //------------------------------------------------------------------
153254721Semaste    /// Return the current thread spec for this option.  This will return NULL if the no thread
154254721Semaste    /// specifications have been set for this Option yet.
155254721Semaste    /// @return
156254721Semaste    ///     The thread specification pointer for this option, or NULL if none has
157254721Semaste    ///     been set yet.
158254721Semaste    //------------------------------------------------------------------
159254721Semaste    const ThreadSpec *
160254721Semaste    GetThreadSpecNoCreate () const;
161254721Semaste
162254721Semaste    //------------------------------------------------------------------
163254721Semaste    /// Returns a pointer to the ThreadSpec for this option, creating it.
164254721Semaste    /// if it hasn't been created already.   This API is used for setting the
165254721Semaste    /// ThreadSpec items for this option.
166254721Semaste    //------------------------------------------------------------------
167254721Semaste    ThreadSpec *
168254721Semaste    GetThreadSpec ();
169254721Semaste
170254721Semaste    void
171254721Semaste    SetThreadID(lldb::tid_t thread_id);
172254721Semaste
173254721Semaste    void
174254721Semaste    GetDescription (Stream *s, lldb::DescriptionLevel level) const;
175254721Semaste
176254721Semaste    //------------------------------------------------------------------
177254721Semaste    /// Get description for callback only.
178254721Semaste    //------------------------------------------------------------------
179254721Semaste    void
180254721Semaste    GetCallbackDescription (Stream *s, lldb::DescriptionLevel level) const;
181254721Semaste
182254721Semaste    //------------------------------------------------------------------
183254721Semaste    /// Returns true if the watchpoint option has a callback set.
184254721Semaste    //------------------------------------------------------------------
185254721Semaste    bool
186254721Semaste    HasCallback();
187254721Semaste
188254721Semaste    //------------------------------------------------------------------
189254721Semaste    /// This is the default empty callback.
190254721Semaste    /// @return
191254721Semaste    ///     The thread id for which the watchpoint hit will stop,
192254721Semaste    ///     LLDB_INVALID_THREAD_ID for all threads.
193254721Semaste    //------------------------------------------------------------------
194254721Semaste    static bool
195254721Semaste    NullCallback (void *baton,
196254721Semaste                  StoppointCallbackContext *context,
197254721Semaste                  lldb::user_id_t watch_id);
198254721Semaste
199254721Semaste
200254721Semaste    struct CommandData
201254721Semaste    {
202254721Semaste        CommandData () :
203254721Semaste            user_source(),
204254721Semaste            script_source(),
205254721Semaste            stop_on_error(true)
206254721Semaste        {
207254721Semaste        }
208254721Semaste
209254721Semaste        ~CommandData ()
210254721Semaste        {
211254721Semaste        }
212254721Semaste
213254721Semaste        StringList user_source;
214254721Semaste        std::string script_source;
215254721Semaste        bool stop_on_error;
216254721Semaste    };
217254721Semaste
218254721Semaste    class CommandBaton : public Baton
219254721Semaste    {
220254721Semaste    public:
221254721Semaste        CommandBaton (CommandData *data) :
222254721Semaste            Baton (data)
223254721Semaste        {
224254721Semaste        }
225254721Semaste
226254721Semaste        virtual
227254721Semaste        ~CommandBaton ()
228254721Semaste        {
229254721Semaste            delete ((CommandData *)m_data);
230254721Semaste            m_data = NULL;
231254721Semaste        }
232254721Semaste
233254721Semaste        virtual void
234254721Semaste        GetDescription (Stream *s, lldb::DescriptionLevel level) const;
235254721Semaste
236254721Semaste    };
237254721Semaste
238254721Semasteprotected:
239254721Semaste    //------------------------------------------------------------------
240254721Semaste    // Classes that inherit from WatchpointOptions can see and modify these
241254721Semaste    //------------------------------------------------------------------
242254721Semaste
243254721Semasteprivate:
244254721Semaste    //------------------------------------------------------------------
245254721Semaste    // For WatchpointOptions only
246254721Semaste    //------------------------------------------------------------------
247254721Semaste    WatchpointHitCallback m_callback; // This is the callback function pointer
248254721Semaste    lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
249254721Semaste    bool m_callback_is_synchronous;
250254721Semaste    std::unique_ptr<ThreadSpec> m_thread_spec_ap; // Thread for which this watchpoint will take
251254721Semaste};
252254721Semaste
253254721Semaste} // namespace lldb_private
254254721Semaste
255254721Semaste#endif  // liblldb_WatchpointOptions_h_
256