BreakpointOptions.h revision 2753a024a5a314232baa579c0ada87551aa86988
1//===-- BreakpointOptions.h -------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_BreakpointOptions_h_
11#define liblldb_BreakpointOptions_h_
12
13// C Includes
14// C++ Includes
15#include <memory>
16// Other libraries and framework includes
17// Project includes
18#include "lldb/lldb-private.h"
19#include "lldb/Core/Baton.h"
20#include "lldb/Core/StringList.h"
21
22namespace lldb_private {
23
24//----------------------------------------------------------------------
25/// @class BreakpointOptions BreakpointOptions.h "lldb/Breakpoint/BreakpointOptions.h"
26/// @brief Class that manages the options on a breakpoint or breakpoint location.
27//----------------------------------------------------------------------
28
29class BreakpointOptions
30{
31public:
32    //------------------------------------------------------------------
33    // Constructors and Destructors
34    //------------------------------------------------------------------
35    //------------------------------------------------------------------
36    /// Default constructor.  The breakpoint is enabled, and has no condition,
37    /// callback, ignore count, etc...
38    //------------------------------------------------------------------
39    BreakpointOptions();
40    BreakpointOptions(const BreakpointOptions& rhs);
41
42    static BreakpointOptions *
43    CopyOptionsNoCallback (BreakpointOptions &rhs);
44    //------------------------------------------------------------------
45    /// This constructor allows you to specify all the breakpoint options.
46    ///
47    /// @param[in] condition
48    ///    The expression which if it evaluates to \b true if we are to stop
49    ///
50    /// @param[in] callback
51    ///    This is the plugin for some code that gets run, returns \b true if we are to stop.
52    ///
53    /// @param[in] baton
54    ///    Client data that will get passed to the callback.
55    ///
56    /// @param[in] enabled
57    ///    Is this breakpoint enabled.
58    ///
59    /// @param[in] ignore
60    ///    How many breakpoint hits we should ignore before stopping.
61    ///
62    /// @param[in] thread_id
63    ///    Only stop if \a thread_id hits the breakpoint.
64    //------------------------------------------------------------------
65    BreakpointOptions(void *condition,
66                      BreakpointHitCallback callback,
67                      void *baton,
68                      bool enabled = true,
69                      int32_t ignore = 0,
70                      lldb::tid_t thread_id = LLDB_INVALID_THREAD_ID,
71                      bool one_shot = false);
72
73    virtual ~BreakpointOptions();
74
75    //------------------------------------------------------------------
76    // Operators
77    //------------------------------------------------------------------
78    const BreakpointOptions&
79    operator=(const BreakpointOptions& rhs);
80
81    //------------------------------------------------------------------
82    // Callbacks
83    //
84    // Breakpoint callbacks come in two forms, synchronous and asynchronous.  Synchronous callbacks will get
85    // run before any of the thread plans are consulted, and if they return false the target will continue
86    // "under the radar" of the thread plans.  There are a couple of restrictions to synchronous callbacks:
87    // 1) They should NOT resume the target themselves.  Just return false if you want the target to restart.
88    // 2) Breakpoints with synchronous callbacks can't have conditions (or rather, they can have them, but they
89    //    won't do anything.  Ditto with ignore counts, etc...  You are supposed to control that all through the
90    //    callback.
91    // Asynchronous callbacks get run as part of the "ShouldStop" logic in the thread plan.  The logic there is:
92    //   a) If the breakpoint is thread specific and not for this thread, continue w/o running the callback.
93    //   b) If the ignore count says we shouldn't stop, then ditto.
94    //   c) If the condition says we shouldn't stop, then ditto.
95    //   d) Otherwise, the callback will get run, and if it returns true we will stop, and if false we won't.
96    //  The asynchronous callback can run the target itself, but at present that should be the last action the
97    //  callback does.  We will relax this condition at some point, but it will take a bit of plumbing to get
98    //  that to work.
99    //
100    //------------------------------------------------------------------
101
102    //------------------------------------------------------------------
103    /// Adds a callback to the breakpoint option set.
104    ///
105    /// @param[in] callback
106    ///    The function to be called when the breakpoint gets hit.
107    ///
108    /// @param[in] baton_sp
109    ///    A baton which will get passed back to the callback when it is invoked.
110    ///
111    /// @param[in] synchronous
112    ///    Whether this is a synchronous or asynchronous callback.  See discussion above.
113    //------------------------------------------------------------------
114    void SetCallback (BreakpointHitCallback callback, const lldb::BatonSP &baton_sp, bool synchronous = false);
115
116
117    //------------------------------------------------------------------
118    /// Remove the callback from this option set.
119    //------------------------------------------------------------------
120    void ClearCallback ();
121
122    // The rest of these functions are meant to be used only within the breakpoint handling mechanism.
123
124    //------------------------------------------------------------------
125    /// Use this function to invoke the callback for a specific stop.
126    ///
127    /// @param[in] context
128    ///    The context in which the callback is to be invoked.  This includes the stop event, the
129    ///    execution context of the stop (since you might hit the same breakpoint on multiple threads) and
130    ///    whether we are currently executing synchronous or asynchronous callbacks.
131    ///
132    /// @param[in] break_id
133    ///    The breakpoint ID that owns this option set.
134    ///
135    /// @param[in] break_loc_id
136    ///    The breakpoint location ID that owns this option set.
137    ///
138    /// @return
139    ///     The callback return value.
140    //------------------------------------------------------------------
141    bool InvokeCallback (StoppointCallbackContext *context, lldb::user_id_t break_id, lldb::user_id_t break_loc_id);
142
143    //------------------------------------------------------------------
144    /// Used in InvokeCallback to tell whether it is the right time to run this kind of callback.
145    ///
146    /// @return
147    ///     The synchronicity of our callback.
148    //------------------------------------------------------------------
149    bool IsCallbackSynchronous () {
150        return m_callback_is_synchronous;
151    }
152
153    //------------------------------------------------------------------
154    /// Fetch the baton from the callback.
155    ///
156    /// @return
157    ///     The baton.
158    //------------------------------------------------------------------
159    Baton *GetBaton ();
160
161    //------------------------------------------------------------------
162    /// Fetch  a const version of the baton from the callback.
163    ///
164    /// @return
165    ///     The baton.
166    //------------------------------------------------------------------
167    const Baton *GetBaton () const;
168
169    //------------------------------------------------------------------
170    // Condition
171    //------------------------------------------------------------------
172    //------------------------------------------------------------------
173    /// Set the breakpoint option's condition.
174    ///
175    /// @param[in] condition
176    ///    The condition expression to evaluate when the breakpoint is hit.
177    //------------------------------------------------------------------
178    void SetCondition (const char *condition);
179
180    //------------------------------------------------------------------
181    /// Return a pointer to the text of the condition expression.
182    ///
183    /// @return
184    ///    A pointer to the condition expression text, or NULL if no
185    //     condition has been set.
186    //------------------------------------------------------------------
187    const char *GetConditionText () const;
188
189    //------------------------------------------------------------------
190    // Enabled/Ignore Count
191    //------------------------------------------------------------------
192
193    //------------------------------------------------------------------
194    /// Check the Enable/Disable state.
195    /// @return
196    ///     \b true if the breakpoint is enabled, \b false if disabled.
197    //------------------------------------------------------------------
198    bool
199    IsEnabled () const
200    {
201        return m_enabled;
202    }
203
204    //------------------------------------------------------------------
205    /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
206    //------------------------------------------------------------------
207    void
208    SetEnabled (bool enabled)
209    {
210        m_enabled = enabled;
211    }
212
213    //------------------------------------------------------------------
214    /// Check the One-shot state.
215    /// @return
216    ///     \b true if the breakpoint is one-shot, \b false otherwise.
217    //------------------------------------------------------------------
218    bool
219    IsOneShot () const
220    {
221        return m_one_shot;
222    }
223
224    //------------------------------------------------------------------
225    /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
226    //------------------------------------------------------------------
227    void
228    SetOneShot (bool one_shot)
229    {
230        m_one_shot = one_shot;
231    }
232
233    //------------------------------------------------------------------
234    /// Set the breakpoint to ignore the next \a count breakpoint hits.
235    /// @param[in] count
236    ///    The number of breakpoint hits to ignore.
237    //------------------------------------------------------------------
238
239    void
240    SetIgnoreCount (uint32_t n)
241    {
242        m_ignore_count = n;
243    }
244
245    //------------------------------------------------------------------
246    /// Return the current Ignore Count.
247    /// @return
248    ///     The number of breakpoint hits to be ignored.
249    //------------------------------------------------------------------
250    uint32_t
251    GetIgnoreCount () const
252    {
253        return m_ignore_count;
254    }
255
256    //------------------------------------------------------------------
257    /// Return the current thread spec for this option.  This will return NULL if the no thread
258    /// specifications have been set for this Option yet.
259    /// @return
260    ///     The thread specification pointer for this option, or NULL if none has
261    ///     been set yet.
262    //------------------------------------------------------------------
263    const ThreadSpec *
264    GetThreadSpecNoCreate () const;
265
266    //------------------------------------------------------------------
267    /// Returns a pointer to the ThreadSpec for this option, creating it.
268    /// if it hasn't been created already.   This API is used for setting the
269    /// ThreadSpec items for this option.
270    //------------------------------------------------------------------
271    ThreadSpec *
272    GetThreadSpec ();
273
274    void
275    SetThreadID(lldb::tid_t thread_id);
276
277    void
278    GetDescription (Stream *s, lldb::DescriptionLevel level) const;
279
280    //------------------------------------------------------------------
281    /// Returns true if the breakpoint option has a callback set.
282    //------------------------------------------------------------------
283    bool
284    HasCallback();
285
286    //------------------------------------------------------------------
287    /// This is the default empty callback.
288    /// @return
289    ///     The thread id for which the breakpoint hit will stop,
290    ///     LLDB_INVALID_THREAD_ID for all threads.
291    //------------------------------------------------------------------
292    static bool
293    NullCallback (void *baton,
294                  StoppointCallbackContext *context,
295                  lldb::user_id_t break_id,
296                  lldb::user_id_t break_loc_id);
297
298
299    struct CommandData
300    {
301        CommandData () :
302            user_source(),
303            script_source(),
304            stop_on_error(true)
305        {
306        }
307
308        ~CommandData ()
309        {
310        }
311
312        StringList user_source;
313        std::string script_source;
314        bool stop_on_error;
315    };
316
317    class CommandBaton : public Baton
318    {
319    public:
320        CommandBaton (CommandData *data) :
321            Baton (data)
322        {
323        }
324
325        virtual
326        ~CommandBaton ()
327        {
328            delete ((CommandData *)m_data);
329            m_data = NULL;
330        }
331
332        virtual void
333        GetDescription (Stream *s, lldb::DescriptionLevel level) const;
334
335    };
336
337protected:
338    //------------------------------------------------------------------
339    // Classes that inherit from BreakpointOptions can see and modify these
340    //------------------------------------------------------------------
341
342private:
343    //------------------------------------------------------------------
344    // For BreakpointOptions only
345    //------------------------------------------------------------------
346    BreakpointHitCallback m_callback; // This is the callback function pointer
347    lldb::BatonSP m_callback_baton_sp; // This is the client data for the callback
348    bool m_callback_is_synchronous;
349    bool m_enabled;
350    bool m_one_shot;
351    uint32_t m_ignore_count; // Number of times to ignore this breakpoint
352    std::auto_ptr<ThreadSpec> m_thread_spec_ap; // Thread for which this breakpoint will take
353    std::auto_ptr<ClangUserExpression> m_condition_ap;  // The condition to test.
354
355};
356
357} // namespace lldb_private
358
359#endif  // liblldb_BreakpointOptions_h_
360