1254721Semaste//===-- BreakpointOptions.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_BreakpointOptions_h_
11254721Semaste#define liblldb_BreakpointOptions_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 BreakpointOptions BreakpointOptions.h "lldb/Breakpoint/BreakpointOptions.h"
25254721Semaste/// @brief Class that manages the options on a breakpoint or breakpoint location.
26254721Semaste//----------------------------------------------------------------------
27254721Semaste
28254721Semasteclass BreakpointOptions
29254721Semaste{
30254721Semastepublic:
31254721Semaste    //------------------------------------------------------------------
32254721Semaste    // Constructors and Destructors
33254721Semaste    //------------------------------------------------------------------
34254721Semaste    //------------------------------------------------------------------
35254721Semaste    /// Default constructor.  The breakpoint is enabled, and has no condition,
36254721Semaste    /// callback, ignore count, etc...
37254721Semaste    //------------------------------------------------------------------
38254721Semaste    BreakpointOptions();
39254721Semaste    BreakpointOptions(const BreakpointOptions& rhs);
40254721Semaste
41254721Semaste    static BreakpointOptions *
42254721Semaste    CopyOptionsNoCallback (BreakpointOptions &rhs);
43254721Semaste    //------------------------------------------------------------------
44254721Semaste    /// This constructor allows you to specify all the breakpoint options.
45254721Semaste    ///
46254721Semaste    /// @param[in] condition
47254721Semaste    ///    The expression which if it evaluates to \b true if we are to stop
48254721Semaste    ///
49254721Semaste    /// @param[in] callback
50254721Semaste    ///    This is the plugin for some code that gets run, returns \b true if we are to stop.
51254721Semaste    ///
52254721Semaste    /// @param[in] baton
53254721Semaste    ///    Client data that will get passed to the callback.
54254721Semaste    ///
55254721Semaste    /// @param[in] enabled
56254721Semaste    ///    Is this breakpoint enabled.
57254721Semaste    ///
58254721Semaste    /// @param[in] ignore
59254721Semaste    ///    How many breakpoint hits we should ignore before stopping.
60254721Semaste    ///
61254721Semaste    /// @param[in] thread_id
62254721Semaste    ///    Only stop if \a thread_id hits the breakpoint.
63254721Semaste    //------------------------------------------------------------------
64254721Semaste    BreakpointOptions(void *condition,
65254721Semaste                      BreakpointHitCallback callback,
66254721Semaste                      void *baton,
67254721Semaste                      bool enabled = true,
68254721Semaste                      int32_t ignore = 0,
69254721Semaste                      lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID,
70254721Semaste                      bool one_shot = false);
71254721Semaste
72254721Semaste    virtual ~BreakpointOptions();
73254721Semaste
74254721Semaste    //------------------------------------------------------------------
75254721Semaste    // Operators
76254721Semaste    //------------------------------------------------------------------
77254721Semaste    const BreakpointOptions&
78254721Semaste    operator=(const BreakpointOptions& rhs);
79254721Semaste
80254721Semaste    //------------------------------------------------------------------
81254721Semaste    // Callbacks
82254721Semaste    //
83254721Semaste    // Breakpoint callbacks come in two forms, synchronous and asynchronous.  Synchronous callbacks will get
84254721Semaste    // run before any of the thread plans are consulted, and if they return false the target will continue
85254721Semaste    // "under the radar" of the thread plans.  There are a couple of restrictions to synchronous callbacks:
86254721Semaste    // 1) They should NOT resume the target themselves.  Just return false if you want the target to restart.
87254721Semaste    // 2) Breakpoints with synchronous callbacks can't have conditions (or rather, they can have them, but they
88254721Semaste    //    won't do anything.  Ditto with ignore counts, etc...  You are supposed to control that all through the
89254721Semaste    //    callback.
90254721Semaste    // Asynchronous callbacks get run as part of the "ShouldStop" logic in the thread plan.  The logic there is:
91254721Semaste    //   a) If the breakpoint is thread specific and not for this thread, continue w/o running the callback.
92254721Semaste    //   b) If the ignore count says we shouldn't stop, then ditto.
93254721Semaste    //   c) If the condition says we shouldn't stop, then ditto.
94254721Semaste    //   d) Otherwise, the callback will get run, and if it returns true we will stop, and if false we won't.
95254721Semaste    //  The asynchronous callback can run the target itself, but at present that should be the last action the
96254721Semaste    //  callback does.  We will relax this condition at some point, but it will take a bit of plumbing to get
97254721Semaste    //  that to work.
98254721Semaste    //
99254721Semaste    //------------------------------------------------------------------
100254721Semaste
101254721Semaste    //------------------------------------------------------------------
102254721Semaste    /// Adds a callback to the breakpoint option set.
103254721Semaste    ///
104254721Semaste    /// @param[in] callback
105254721Semaste    ///    The function to be called when the breakpoint gets hit.
106254721Semaste    ///
107254721Semaste    /// @param[in] baton_sp
108254721Semaste    ///    A baton which will get passed back to the callback when it is invoked.
109254721Semaste    ///
110254721Semaste    /// @param[in] synchronous
111254721Semaste    ///    Whether this is a synchronous or asynchronous callback.  See discussion above.
112254721Semaste    //------------------------------------------------------------------
113254721Semaste    void SetCallback (BreakpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous = false);
114254721Semaste
115254721Semaste
116254721Semaste    //------------------------------------------------------------------
117254721Semaste    /// Remove the callback from this option set.
118254721Semaste    //------------------------------------------------------------------
119254721Semaste    void ClearCallback ();
120254721Semaste
121254721Semaste    // The rest of these functions are meant to be used only within the breakpoint handling mechanism.
122254721Semaste
123254721Semaste    //------------------------------------------------------------------
124254721Semaste    /// Use this function to invoke the callback for a specific stop.
125254721Semaste    ///
126254721Semaste    /// @param[in] context
127254721Semaste    ///    The context in which the callback is to be invoked.  This includes the stop event, the
128254721Semaste    ///    execution context of the stop (since you might hit the same breakpoint on multiple threads) and
129254721Semaste    ///    whether we are currently executing synchronous or asynchronous callbacks.
130254721Semaste    ///
131254721Semaste    /// @param[in] break_id
132254721Semaste    ///    The breakpoint ID that owns this option set.
133254721Semaste    ///
134254721Semaste    /// @param[in] break_loc_id
135254721Semaste    ///    The breakpoint location ID that owns this option set.
136254721Semaste    ///
137254721Semaste    /// @return
138254721Semaste    ///     The callback return value.
139254721Semaste    //------------------------------------------------------------------
140254721Semaste    bool InvokeCallback (StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
141254721Semaste
142254721Semaste    //------------------------------------------------------------------
143254721Semaste    /// Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
144254721Semaste    ///
145254721Semaste    /// @return
146254721Semaste    ///     The synchronicity of our callback.
147254721Semaste    //------------------------------------------------------------------
148254721Semaste    bool IsCallbackSynchronous () {
149254721Semaste        return m_callback_is_synchronous;
150254721Semaste    }
151254721Semaste
152254721Semaste    //------------------------------------------------------------------
153254721Semaste    /// Fetch the baton from the callback.
154254721Semaste    ///
155254721Semaste    /// @return
156254721Semaste    ///     The baton.
157254721Semaste    //------------------------------------------------------------------
158254721Semaste    Baton *GetBaton ();
159254721Semaste
160254721Semaste    //------------------------------------------------------------------
161254721Semaste    /// Fetch  a const version of the baton from the callback.
162254721Semaste    ///
163254721Semaste    /// @return
164254721Semaste    ///     The baton.
165254721Semaste    //------------------------------------------------------------------
166254721Semaste    const Baton *GetBaton () const;
167254721Semaste
168254721Semaste    //------------------------------------------------------------------
169254721Semaste    // Condition
170254721Semaste    //------------------------------------------------------------------
171254721Semaste    //------------------------------------------------------------------
172254721Semaste    /// Set the breakpoint option's condition.
173254721Semaste    ///
174254721Semaste    /// @param[in] condition
175254721Semaste    ///    The condition expression to evaluate when the breakpoint is hit.
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 (size_t *hash = NULL) const;
187254721Semaste
188254721Semaste    //------------------------------------------------------------------
189254721Semaste    // Enabled/Ignore Count
190254721Semaste    //------------------------------------------------------------------
191254721Semaste
192254721Semaste    //------------------------------------------------------------------
193254721Semaste    /// Check the Enable/Disable state.
194254721Semaste    /// @return
195254721Semaste    ///     \b true if the breakpoint is enabled, \b false if disabled.
196254721Semaste    //------------------------------------------------------------------
197254721Semaste    bool
198254721Semaste    IsEnabled () const
199254721Semaste    {
200254721Semaste        return m_enabled;
201254721Semaste    }
202254721Semaste
203254721Semaste    //------------------------------------------------------------------
204254721Semaste    /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
205254721Semaste    //------------------------------------------------------------------
206254721Semaste    void
207254721Semaste    SetEnabled (bool enabled)
208254721Semaste    {
209254721Semaste        m_enabled = enabled;
210254721Semaste    }
211254721Semaste
212254721Semaste    //------------------------------------------------------------------
213254721Semaste    /// Check the One-shot state.
214254721Semaste    /// @return
215254721Semaste    ///     \b true if the breakpoint is one-shot, \b false otherwise.
216254721Semaste    //------------------------------------------------------------------
217254721Semaste    bool
218254721Semaste    IsOneShot () const
219254721Semaste    {
220254721Semaste        return m_one_shot;
221254721Semaste    }
222254721Semaste
223254721Semaste    //------------------------------------------------------------------
224254721Semaste    /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
225254721Semaste    //------------------------------------------------------------------
226254721Semaste    void
227254721Semaste    SetOneShot (bool one_shot)
228254721Semaste    {
229254721Semaste        m_one_shot = one_shot;
230254721Semaste    }
231254721Semaste
232254721Semaste    //------------------------------------------------------------------
233254721Semaste    /// Set the breakpoint to ignore the next \a count breakpoint hits.
234254721Semaste    /// @param[in] count
235254721Semaste    ///    The number of breakpoint hits to ignore.
236254721Semaste    //------------------------------------------------------------------
237254721Semaste
238254721Semaste    void
239254721Semaste    SetIgnoreCount (uint32_t n)
240254721Semaste    {
241254721Semaste        m_ignore_count = n;
242254721Semaste    }
243254721Semaste
244254721Semaste    //------------------------------------------------------------------
245254721Semaste    /// Return the current Ignore Count.
246254721Semaste    /// @return
247254721Semaste    ///     The number of breakpoint hits to be ignored.
248254721Semaste    //------------------------------------------------------------------
249254721Semaste    uint32_t
250254721Semaste    GetIgnoreCount () const
251254721Semaste    {
252254721Semaste        return m_ignore_count;
253254721Semaste    }
254254721Semaste
255254721Semaste    //------------------------------------------------------------------
256254721Semaste    /// Return the current thread spec for this option.  This will return NULL if the no thread
257254721Semaste    /// specifications have been set for this Option yet.
258254721Semaste    /// @return
259254721Semaste    ///     The thread specification pointer for this option, or NULL if none has
260254721Semaste    ///     been set yet.
261254721Semaste    //------------------------------------------------------------------
262254721Semaste    const ThreadSpec *
263254721Semaste    GetThreadSpecNoCreate () const;
264254721Semaste
265254721Semaste    //------------------------------------------------------------------
266254721Semaste    /// Returns a pointer to the ThreadSpec for this option, creating it.
267254721Semaste    /// if it hasn't been created already.   This API is used for setting the
268254721Semaste    /// ThreadSpec items for this option.
269254721Semaste    //------------------------------------------------------------------
270254721Semaste    ThreadSpec *
271254721Semaste    GetThreadSpec ();
272254721Semaste
273254721Semaste    void
274254721Semaste    SetThreadID(lldb::tid_t thread_id);
275254721Semaste
276254721Semaste    void
277254721Semaste    GetDescription (Stream *s, lldb::DescriptionLevel level) const;
278254721Semaste
279254721Semaste    //------------------------------------------------------------------
280254721Semaste    /// Returns true if the breakpoint option has a callback set.
281254721Semaste    //------------------------------------------------------------------
282254721Semaste    bool
283254721Semaste    HasCallback();
284254721Semaste
285254721Semaste    //------------------------------------------------------------------
286254721Semaste    /// This is the default empty callback.
287254721Semaste    /// @return
288254721Semaste    ///     The thread id for which the breakpoint hit will stop,
289254721Semaste    ///     LLDB_INVALID_THREAD_ID for all threads.
290254721Semaste    //------------------------------------------------------------------
291254721Semaste    static bool
292254721Semaste    NullCallback (void *baton,
293254721Semaste                  StoppointCallbackContext *context,
294254721Semaste                  lldb::user_id_t break_id,
295254721Semaste                  lldb::user_id_t break_loc_id);
296254721Semaste
297254721Semaste
298254721Semaste    struct CommandData
299254721Semaste    {
300254721Semaste        CommandData () :
301254721Semaste            user_source(),
302254721Semaste            script_source(),
303254721Semaste            stop_on_error(true)
304254721Semaste        {
305254721Semaste        }
306254721Semaste
307254721Semaste        ~CommandData ()
308254721Semaste        {
309254721Semaste        }
310254721Semaste
311254721Semaste        StringList user_source;
312254721Semaste        std::string script_source;
313254721Semaste        bool stop_on_error;
314254721Semaste    };
315254721Semaste
316254721Semaste    class CommandBaton : public Baton
317254721Semaste    {
318254721Semaste    public:
319254721Semaste        CommandBaton (CommandData *data) :
320254721Semaste            Baton (data)
321254721Semaste        {
322254721Semaste        }
323254721Semaste
324254721Semaste        virtual
325254721Semaste        ~CommandBaton ()
326254721Semaste        {
327254721Semaste            delete ((CommandData *)m_data);
328254721Semaste            m_data = NULL;
329254721Semaste        }
330254721Semaste
331254721Semaste        virtual void
332254721Semaste        GetDescription (Stream *s, lldb::DescriptionLevel level) const;
333254721Semaste
334254721Semaste    };
335254721Semaste
336254721Semasteprotected:
337254721Semaste    //------------------------------------------------------------------
338254721Semaste    // Classes that inherit from BreakpointOptions can see and modify these
339254721Semaste    //------------------------------------------------------------------
340254721Semaste
341254721Semasteprivate:
342254721Semaste    //------------------------------------------------------------------
343254721Semaste    // For BreakpointOptions only
344254721Semaste    //------------------------------------------------------------------
345254721Semaste    BreakpointHitCallback m_callback; // This is the callback function pointer
346254721Semaste    lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
347254721Semaste    bool m_callback_is_synchronous;
348254721Semaste    bool m_enabled;
349254721Semaste    bool m_one_shot;
350254721Semaste    uint32_t m_ignore_count; // Number of times to ignore this breakpoint
351254721Semaste    std::unique_ptr<ThreadSpec> m_thread_spec_ap; // Thread for which this breakpoint will take
352254721Semaste    std::string m_condition_text;  // The condition to test.
353254721Semaste    size_t m_condition_text_hash; // Its hash, so that locations know when the condition is updated.
354254721Semaste};
355254721Semaste
356254721Semaste} // namespace lldb_private
357254721Semaste
358254721Semaste#endif  // liblldb_BreakpointOptions_h_
359