Breakpoint.h revision fdbd10abe4f1f0e97647355823a8e2cd5f50856b
1//===-- Breakpoint.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_Breakpoint_h_
11#define liblldb_Breakpoint_h_
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/Breakpoint/BreakpointLocationList.h"
18#include "lldb/Breakpoint/BreakpointOptions.h"
19#include "lldb/Breakpoint/BreakpointLocationCollection.h"
20#include "lldb/Breakpoint/Stoppoint.h"
21#include "lldb/Core/SearchFilter.h"
22#include "lldb/Core/Event.h"
23#include "lldb/Core/StringList.h"
24
25namespace lldb_private {
26
27//----------------------------------------------------------------------
28/// @class Breakpoint Breakpoint.h "lldb/Breakpoint/Breakpoint.h"
29/// @brief Class that manages logical breakpoint setting.
30//----------------------------------------------------------------------
31
32//----------------------------------------------------------------------
33/// General Outline:
34/// A breakpoint has four main parts, a filter, a resolver, the list of breakpoint
35/// locations that have been determined for the filter/resolver pair, and finally
36/// a set of options for the breakpoint.
37///
38/// \b Filter:
39/// This is an object derived from SearchFilter.  It manages the search
40/// for breakpoint location matches through the symbols in the module list of the target
41/// that owns it.  It also filters out locations based on whatever logic it wants.
42///
43/// \b Resolver:
44/// This is an object derived from BreakpointResolver.  It provides a
45/// callback to the filter that will find breakpoint locations.  How it does this is
46/// determined by what kind of resolver it is.
47///
48/// The Breakpoint class also provides constructors for the common breakpoint cases
49/// which make the appropriate filter and resolver for you.
50///
51/// \b Location List:
52/// This stores the breakpoint locations that have been determined
53/// to date.  For a given breakpoint, there will be only one location with a given
54/// address.  Adding a location at an already taken address will just return the location
55/// already at that address.  Locations can be looked up by ID, or by address.
56///
57/// \b Options:
58/// This includes:
59///    \b Enabled/Disabled
60///    \b Ignore Count
61///    \b Callback
62///    \b Condition
63/// Note, these options can be set on the breakpoint, and they can also be set on the
64/// individual locations.  The options set on the breakpoint take precedence over the
65/// options set on the individual location.
66/// So for instance disabling the breakpoint will cause NONE of the locations to get hit.
67/// But if the breakpoint is enabled, then the location's enabled state will be checked
68/// to determine whether to insert that breakpoint location.
69/// Similarly, if the breakpoint condition says "stop", we won't check the location's condition.
70/// But if the breakpoint condition says "continue", then we will check the location for whether
71/// to actually stop or not.
72/// One subtle point worth observing here is that you don't actually stop at a Breakpoint, you
73/// always stop at one of its locations.  So the "should stop" tests are done by the location,
74/// not by the breakpoint.
75//----------------------------------------------------------------------
76class Breakpoint:
77    public STD_ENABLE_SHARED_FROM_THIS(Breakpoint),
78    public Stoppoint
79{
80public:
81
82    static const ConstString &
83    GetEventIdentifier ();
84
85
86    //------------------------------------------------------------------
87    /// An enum specifying the match style for breakpoint settings.  At
88    /// present only used for function name style breakpoints.
89    //------------------------------------------------------------------
90    typedef enum
91    {
92        Exact,
93        Regexp,
94        Glob
95    } MatchType;
96
97    class BreakpointEventData :
98        public EventData
99    {
100    public:
101
102        static const ConstString &
103        GetFlavorString ();
104
105        virtual const ConstString &
106        GetFlavor () const;
107
108        BreakpointEventData (lldb::BreakpointEventType sub_type,
109                             const lldb::BreakpointSP &new_breakpoint_sp);
110
111        virtual
112        ~BreakpointEventData();
113
114        lldb::BreakpointEventType
115        GetBreakpointEventType () const;
116
117        lldb::BreakpointSP &
118        GetBreakpoint ();
119
120        BreakpointLocationCollection &
121        GetBreakpointLocationCollection()
122        {
123            return m_locations;
124        }
125
126
127        virtual void
128        Dump (Stream *s) const;
129
130        static lldb::BreakpointEventType
131        GetBreakpointEventTypeFromEvent (const lldb::EventSP &event_sp);
132
133        static lldb::BreakpointSP
134        GetBreakpointFromEvent (const lldb::EventSP &event_sp);
135
136        static lldb::BreakpointLocationSP
137        GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t loc_idx);
138
139        static uint32_t
140        GetNumBreakpointLocationsFromEvent (const lldb::EventSP &event_sp);
141
142        static const BreakpointEventData *
143        GetEventDataFromEvent (const Event *event_sp);
144
145    private:
146
147        lldb::BreakpointEventType m_breakpoint_event;
148        lldb::BreakpointSP m_new_breakpoint_sp;
149        BreakpointLocationCollection m_locations;
150
151        DISALLOW_COPY_AND_ASSIGN (BreakpointEventData);
152    };
153
154
155    //------------------------------------------------------------------
156    /// Destructor.
157    ///
158    /// The destructor is not virtual since there should be no reason to subclass
159    /// breakpoints.  The varieties of breakpoints are specified instead by
160    /// providing different resolvers & filters.
161    //------------------------------------------------------------------
162    ~Breakpoint();
163
164    //------------------------------------------------------------------
165    // Methods
166    //------------------------------------------------------------------
167
168    //------------------------------------------------------------------
169    /// Tell whether this breakpoint is an "internal" breakpoint.
170    /// @return
171    ///     Returns \b true if this is an internal breakpoint, \b false otherwise.
172    //------------------------------------------------------------------
173    bool
174    IsInternal () const;
175
176    //------------------------------------------------------------------
177    /// Standard "Dump" method.  At present it does nothing.
178    //------------------------------------------------------------------
179    void
180    Dump (Stream *s);
181
182    //------------------------------------------------------------------
183    // The next set of methods provide ways to tell the breakpoint to update
184    // it's location list - usually done when modules appear or disappear.
185    //------------------------------------------------------------------
186
187
188    //------------------------------------------------------------------
189    /// Tell this breakpoint to clear all its breakpoint sites.  Done
190    /// when the process holding the breakpoint sites is destroyed.
191    //------------------------------------------------------------------
192    void
193    ClearAllBreakpointSites ();
194
195    //------------------------------------------------------------------
196    /// Tell this breakpoint to scan it's target's module list and resolve any
197    /// new locations that match the breakpoint's specifications.
198    //------------------------------------------------------------------
199    void
200    ResolveBreakpoint ();
201
202    //------------------------------------------------------------------
203    /// Tell this breakpoint to scan a given module list and resolve any
204    /// new locations that match the breakpoint's specifications.
205    ///
206    /// @param[in] changed_modules
207    ///    The list of modules to look in for new locations.
208    //------------------------------------------------------------------
209    void
210    ResolveBreakpointInModules (ModuleList &changed_modules);
211
212
213    //------------------------------------------------------------------
214    /// Like ResolveBreakpointInModules, but allows for "unload" events, in
215    /// which case we will remove any locations that are in modules that got
216    /// unloaded.
217    ///
218    /// @param[in] changedModules
219    ///    The list of modules to look in for new locations.
220    /// @param[in] load_event
221    ///    If \b true then the modules were loaded, if \b false, unloaded.
222    /// @param[in] delete_locations
223    ///    If \b true then the modules were unloaded delete any locations in the changed modules.
224    //------------------------------------------------------------------
225    void
226    ModulesChanged (ModuleList &changed_modules,
227                    bool load_event,
228                    bool delete_locations = false);
229
230
231    //------------------------------------------------------------------
232    /// Tells the breakpoint the old module \a old_module_sp has been
233    /// replaced by new_module_sp (usually because the underlying file has been
234    /// rebuilt, and the old version is gone.)
235    ///
236    /// @param[in] old_module_sp
237    ///    The old module that is going away.
238    /// @param[in] new_module_sp
239    ///    The new module that is replacing it.
240    //------------------------------------------------------------------
241    void
242    ModuleReplaced (lldb::ModuleSP old_module_sp, lldb::ModuleSP new_module_sp);
243
244    //------------------------------------------------------------------
245    // The next set of methods provide access to the breakpoint locations
246    // for this breakpoint.
247    //------------------------------------------------------------------
248
249    //------------------------------------------------------------------
250    /// Add a location to the breakpoint's location list.  This is only meant
251    /// to be called by the breakpoint's resolver.  FIXME: how do I ensure that?
252    ///
253    /// @param[in] addr
254    ///    The Address specifying the new location.
255    /// @param[out] new_location
256    ///    Set to \b true if a new location was created, to \b false if there
257    ///    already was a location at this Address.
258    /// @return
259    ///    Returns a pointer to the new location.
260    //------------------------------------------------------------------
261    lldb::BreakpointLocationSP
262    AddLocation (const Address &addr,
263                 bool *new_location = NULL);
264
265    //------------------------------------------------------------------
266    /// Find a breakpoint location by Address.
267    ///
268    /// @param[in] addr
269    ///    The Address specifying the location.
270    /// @return
271    ///    Returns a shared pointer to the location at \a addr.  The pointer
272    ///    in the shared pointer will be NULL if there is no location at that address.
273    //------------------------------------------------------------------
274    lldb::BreakpointLocationSP
275    FindLocationByAddress (const Address &addr);
276
277    //------------------------------------------------------------------
278    /// Find a breakpoint location ID by Address.
279    ///
280    /// @param[in] addr
281    ///    The Address specifying the location.
282    /// @return
283    ///    Returns the UID of the location at \a addr, or \b LLDB_INVALID_ID if
284    ///    there is no breakpoint location at that address.
285    //------------------------------------------------------------------
286    lldb::break_id_t
287    FindLocationIDByAddress (const Address &addr);
288
289    //------------------------------------------------------------------
290    /// Find a breakpoint location for a given breakpoint location ID.
291    ///
292    /// @param[in] bp_loc_id
293    ///    The ID specifying the location.
294    /// @return
295    ///    Returns a shared pointer to the location with ID \a bp_loc_id.  The pointer
296    ///    in the shared pointer will be NULL if there is no location with that ID.
297    //------------------------------------------------------------------
298    lldb::BreakpointLocationSP
299    FindLocationByID (lldb::break_id_t bp_loc_id);
300
301    //------------------------------------------------------------------
302    /// Get breakpoint locations by index.
303    ///
304    /// @param[in] index
305    ///    The location index.
306    ///
307    /// @return
308    ///     Returns a shared pointer to the location with index \a
309    ///     index. The shared pointer might contain NULL if \a index is
310    ///     greater than then number of actual locations.
311    //------------------------------------------------------------------
312    lldb::BreakpointLocationSP
313    GetLocationAtIndex (uint32_t index);
314
315    //------------------------------------------------------------------
316    // The next section deals with various breakpoint options.
317    //------------------------------------------------------------------
318
319    //------------------------------------------------------------------
320    /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
321    //------------------------------------------------------------------
322    void
323    SetEnabled (bool enable);
324
325    //------------------------------------------------------------------
326    /// Check the Enable/Disable state.
327    /// @return
328    ///     \b true if the breakpoint is enabled, \b false if disabled.
329    //------------------------------------------------------------------
330    bool
331    IsEnabled ();
332
333    //------------------------------------------------------------------
334    /// Set the breakpoint to ignore the next \a count breakpoint hits.
335    /// @param[in] count
336    ///    The number of breakpoint hits to ignore.
337    //------------------------------------------------------------------
338    void
339    SetIgnoreCount (uint32_t count);
340
341    //------------------------------------------------------------------
342    /// Return the current ignore count/
343    /// @return
344    ///     The number of breakpoint hits to be ignored.
345    //------------------------------------------------------------------
346    uint32_t
347    GetIgnoreCount () const;
348
349    //------------------------------------------------------------------
350    /// Return the current hit count for all locations.
351    /// @return
352    ///     The current hit count for all locations.
353    //------------------------------------------------------------------
354    uint32_t
355    GetHitCount () const;
356
357
358    //------------------------------------------------------------------
359    /// Set the valid thread to be checked when the breakpoint is hit.
360    /// @param[in] thread_id
361    ///    If this thread hits the breakpoint, we stop, otherwise not.
362    //------------------------------------------------------------------
363    void
364    SetThreadID (lldb::tid_t thread_id);
365
366    //------------------------------------------------------------------
367    /// Return the current stop thread value.
368    /// @return
369    ///     The thread id for which the breakpoint hit will stop, LLDB_INVALID_THREAD_ID for all threads.
370    //------------------------------------------------------------------
371    lldb::tid_t
372    GetThreadID () const;
373
374    void
375    SetThreadIndex (uint32_t index);
376
377    uint32_t
378    GetThreadIndex() const;
379
380    void
381    SetThreadName (const char *thread_name);
382
383    const char *
384    GetThreadName () const;
385
386    void
387    SetQueueName (const char *queue_name);
388
389    const char *
390    GetQueueName () const;
391
392    //------------------------------------------------------------------
393    /// Set the callback action invoked when the breakpoint is hit.
394    ///
395    /// @param[in] callback
396    ///    The method that will get called when the breakpoint is hit.
397    /// @param[in] baton
398    ///    A void * pointer that will get passed back to the callback function.
399    /// @param[in] is_synchronous
400    ///    If \b true the callback will be run on the private event thread
401    ///    before the stop event gets reported.  If false, the callback will get
402    ///    handled on the public event thead after the stop has been posted.
403    ///
404    /// @return
405    ///    \b true if the process should stop when you hit the breakpoint.
406    ///    \b false if it should continue.
407    //------------------------------------------------------------------
408    void
409    SetCallback (BreakpointHitCallback callback,
410                 void *baton,
411                 bool is_synchronous = false);
412
413    void
414    SetCallback (BreakpointHitCallback callback,
415                 const lldb::BatonSP &callback_baton_sp,
416                 bool is_synchronous = false);
417
418    void
419    ClearCallback ();
420
421    //------------------------------------------------------------------
422    /// Set the breakpoint's condition.
423    ///
424    /// @param[in] condition
425    ///    The condition expression to evaluate when the breakpoint is hit.
426    ///    Pass in NULL to clear the condition.
427    //------------------------------------------------------------------
428    void SetCondition (const char *condition);
429
430    //------------------------------------------------------------------
431    /// Return a pointer to the text of the condition expression.
432    ///
433    /// @return
434    ///    A pointer to the condition expression text, or NULL if no
435    //     condition has been set.
436    //------------------------------------------------------------------
437    const char *GetConditionText () const;
438
439    //------------------------------------------------------------------
440    // The next section are various utility functions.
441    //------------------------------------------------------------------
442
443    //------------------------------------------------------------------
444    /// Return the number of breakpoint locations that have resolved to
445    /// actual breakpoint sites.
446    ///
447    /// @return
448    ///     The number locations resolved breakpoint sites.
449    //------------------------------------------------------------------
450    size_t
451    GetNumResolvedLocations() const;
452
453    //------------------------------------------------------------------
454    /// Return the number of breakpoint locations.
455    ///
456    /// @return
457    ///     The number breakpoint locations.
458    //------------------------------------------------------------------
459    size_t
460    GetNumLocations() const;
461
462    //------------------------------------------------------------------
463    /// Put a description of this breakpoint into the stream \a s.
464    ///
465    /// @param[in] s
466    ///     Stream into which to dump the description.
467    ///
468    /// @param[in] level
469    ///     The description level that indicates the detail level to
470    ///     provide.
471    ///
472    /// @see lldb::DescriptionLevel
473    //------------------------------------------------------------------
474    void
475    GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations = false);
476
477    //------------------------------------------------------------------
478    /// Accessor for the breakpoint Target.
479    /// @return
480    ///     This breakpoint's Target.
481    //------------------------------------------------------------------
482    Target &
483    GetTarget ();
484
485    const Target &
486    GetTarget () const;
487
488    void
489    GetResolverDescription (Stream *s);
490
491    //------------------------------------------------------------------
492    /// Find breakpoint locations which match the (filename, line_number) description.
493    /// The breakpoint location collection is to be filled with the matching locations.
494    /// It should be initialized with 0 size by the API client.
495    ///
496    /// @return
497    ///     True if there is a match
498    ///
499    ///     The locations which match the filename and line_number in loc_coll.  If its
500    ///     size is 0 and true is returned, it means the breakpoint fully matches the
501    ///     description.
502    //------------------------------------------------------------------
503    bool GetMatchingFileLine(const ConstString &filename, uint32_t line_number,
504                             BreakpointLocationCollection &loc_coll);
505
506    void
507    GetFilterDescription (Stream *s);
508
509    //------------------------------------------------------------------
510    /// Returns the BreakpointOptions structure set at the breakpoint level.
511    ///
512    /// Meant to be used by the BreakpointLocation class.
513    ///
514    /// @return
515    ///     A pointer to this breakpoint's BreakpointOptions.
516    //------------------------------------------------------------------
517    BreakpointOptions *
518    GetOptions ();
519
520
521    //------------------------------------------------------------------
522    /// Invoke the callback action when the breakpoint is hit.
523    ///
524    /// Meant to be used by the BreakpointLocation class.
525    ///
526    /// @param[in] context
527    ///     Described the breakpoint event.
528    ///
529    /// @param[in] bp_loc_id
530    ///     Which breakpoint location hit this breakpoint.
531    ///
532    /// @return
533    ///     \b true if the target should stop at this breakpoint and \b false not.
534    //------------------------------------------------------------------
535    bool
536    InvokeCallback (StoppointCallbackContext *context,
537                    lldb::break_id_t bp_loc_id);
538
539protected:
540    friend class Target;
541    //------------------------------------------------------------------
542    // Protected Methods
543    //------------------------------------------------------------------
544
545
546    //------------------------------------------------------------------
547    /// Constructors and Destructors
548    /// Only the Target can make a breakpoint, and it owns the breakpoint lifespans.
549    /// The constructor takes a filter and a resolver.  Up in Target there are convenience
550    /// variants that make breakpoints for some common cases.
551    //------------------------------------------------------------------
552    // This is the generic constructor
553    Breakpoint(Target &target, lldb::SearchFilterSP &filter_sp, lldb::BreakpointResolverSP &resolver_sp);
554
555    friend class BreakpointLocation;  // To call the following two when determining whether to stop.
556
557    void
558    DecrementIgnoreCount();
559
560    // BreakpointLocation::IgnoreCountShouldStop & Breakpoint::IgnoreCountShouldStop can only be called once per stop,
561    // and BreakpointLocation::IgnoreCountShouldStop should be tested first, and if it returns false we should
562    // continue, otherwise we should test Breakpoint::IgnoreCountShouldStop.
563
564    bool
565    IgnoreCountShouldStop ();
566
567private:
568    //------------------------------------------------------------------
569    // For Breakpoint only
570    //------------------------------------------------------------------
571    bool m_being_created;
572    Target &m_target;                         // The target that holds this breakpoint.
573    lldb::SearchFilterSP m_filter_sp;         // The filter that constrains the breakpoint's domain.
574    lldb::BreakpointResolverSP m_resolver_sp; // The resolver that defines this breakpoint.
575    BreakpointOptions m_options;              // Settable breakpoint options
576    BreakpointLocationList m_locations;       // The list of locations currently found for this breakpoint.
577
578    void
579    SendBreakpointChangedEvent (lldb::BreakpointEventType eventKind);
580
581    void
582    SendBreakpointChangedEvent (BreakpointEventData *data);
583
584    DISALLOW_COPY_AND_ASSIGN(Breakpoint);
585};
586
587} // namespace lldb_private
588
589#endif  // liblldb_Breakpoint_h_
590