BreakpointLocation.h revision 296417
1//===-- BreakpointLocation.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_BreakpointLocation_h_
11#define liblldb_BreakpointLocation_h_
12
13// C Includes
14// C++ Includes
15#include <memory>
16
17// Other libraries and framework includes
18// Project includes
19#include "lldb/lldb-private.h"
20#include "lldb/Breakpoint/StoppointLocation.h"
21#include "lldb/Core/Address.h"
22#include "lldb/Core/UserID.h"
23#include "lldb/Host/Mutex.h"
24
25namespace lldb_private {
26
27//----------------------------------------------------------------------
28/// @class BreakpointLocation BreakpointLocation.h "lldb/Breakpoint/BreakpointLocation.h"
29/// @brief Class that manages one unique (by address) instance of a logical breakpoint.
30//----------------------------------------------------------------------
31
32//----------------------------------------------------------------------
33/// General Outline:
34/// A breakpoint location is defined by the breakpoint that produces it,
35/// and the address that resulted in this particular instantiation.
36/// Each breakpoint location also may have a breakpoint site if its
37/// address has been loaded into the program.
38/// Finally it has a settable options object.
39///
40/// FIXME: Should we also store some fingerprint for the location, so
41/// we can map one location to the "equivalent location" on rerun?  This
42/// would be useful if you've set options on the locations.
43//----------------------------------------------------------------------
44
45class BreakpointLocation :
46    public std::enable_shared_from_this<BreakpointLocation>,
47    public StoppointLocation
48{
49public:
50    ~BreakpointLocation() override;
51
52    //------------------------------------------------------------------
53    /// Gets the load address for this breakpoint location
54    /// @return
55    ///     Returns breakpoint location load address, \b
56    ///     LLDB_INVALID_ADDRESS if not yet set.
57    //------------------------------------------------------------------
58    lldb::addr_t
59    GetLoadAddress() const override;
60
61    //------------------------------------------------------------------
62    /// Gets the Address for this breakpoint location
63    /// @return
64    ///     Returns breakpoint location Address.
65    //------------------------------------------------------------------
66    Address &
67    GetAddress ();
68    //------------------------------------------------------------------
69    /// Gets the Breakpoint that created this breakpoint location
70    /// @return
71    ///     Returns the owning breakpoint.
72    //------------------------------------------------------------------
73    Breakpoint &
74    GetBreakpoint ();
75
76    Target &
77    GetTarget();
78
79    //------------------------------------------------------------------
80    /// Determines whether we should stop due to a hit at this
81    /// breakpoint location.
82    ///
83    /// Side Effects: This may evaluate the breakpoint condition, and
84    /// run the callback.  So this command may do a considerable amount
85    /// of work.
86    ///
87    /// @return
88    ///     \b true if this breakpoint location thinks we should stop,
89    ///     \b false otherwise.
90    //------------------------------------------------------------------
91    bool
92    ShouldStop(StoppointCallbackContext *context) override;
93
94    //------------------------------------------------------------------
95    // The next section deals with various breakpoint options.
96    //------------------------------------------------------------------
97
98    //------------------------------------------------------------------
99    /// If \a enable is \b true, enable the breakpoint, if \b false
100    /// disable it.
101    //------------------------------------------------------------------
102    void
103    SetEnabled(bool enabled);
104
105    //------------------------------------------------------------------
106    /// Check the Enable/Disable state.
107    ///
108    /// @return
109    ///     \b true if the breakpoint is enabled, \b false if disabled.
110    //------------------------------------------------------------------
111    bool
112    IsEnabled () const;
113
114    //------------------------------------------------------------------
115    /// Return the current Ignore Count.
116    ///
117    /// @return
118    ///     The number of breakpoint hits to be ignored.
119    //------------------------------------------------------------------
120    uint32_t
121    GetIgnoreCount ();
122
123    //------------------------------------------------------------------
124    /// Set the breakpoint to ignore the next \a count breakpoint hits.
125    ///
126    /// @param[in] count
127    ///    The number of breakpoint hits to ignore.
128    //------------------------------------------------------------------
129    void
130    SetIgnoreCount (uint32_t n);
131
132    //------------------------------------------------------------------
133    /// Set the callback action invoked when the breakpoint is hit.
134    ///
135    /// The callback will return a bool indicating whether the target
136    /// should stop at this breakpoint or not.
137    ///
138    /// @param[in] callback
139    ///     The method that will get called when the breakpoint is hit.
140    ///
141    /// @param[in] callback_baton_sp
142    ///     A shared pointer to a Baton that provides the void * needed
143    ///     for the callback.
144    ///
145    /// @see lldb_private::Baton
146    //------------------------------------------------------------------
147    void
148    SetCallback (BreakpointHitCallback callback,
149                 const lldb::BatonSP &callback_baton_sp,
150                 bool is_synchronous);
151
152    void
153    SetCallback (BreakpointHitCallback callback,
154                 void *baton,
155                 bool is_synchronous);
156
157    void
158    ClearCallback ();
159
160    //------------------------------------------------------------------
161    /// Set the breakpoint location's condition.
162    ///
163    /// @param[in] condition
164    ///    The condition expression to evaluate when the breakpoint is hit.
165    //------------------------------------------------------------------
166    void
167    SetCondition (const char *condition);
168
169    //------------------------------------------------------------------
170    /// Return a pointer to the text of the condition expression.
171    ///
172    /// @return
173    ///    A pointer to the condition expression text, or nullptr if no
174    //     condition has been set.
175    //------------------------------------------------------------------
176    const char *
177    GetConditionText(size_t *hash = nullptr) const;
178
179    bool
180    ConditionSaysStop (ExecutionContext &exe_ctx, Error &error);
181
182    //------------------------------------------------------------------
183    /// Set the valid thread to be checked when the breakpoint is hit.
184    ///
185    /// @param[in] thread_id
186    ///    If this thread hits the breakpoint, we stop, otherwise not.
187    //------------------------------------------------------------------
188    void
189    SetThreadID (lldb::tid_t thread_id);
190
191    lldb::tid_t
192    GetThreadID ();
193
194    void
195    SetThreadIndex (uint32_t index);
196
197    uint32_t
198    GetThreadIndex() const;
199
200    void
201    SetThreadName (const char *thread_name);
202
203    const char *
204    GetThreadName () const;
205
206    void
207    SetQueueName (const char *queue_name);
208
209    const char *
210    GetQueueName () const;
211
212    //------------------------------------------------------------------
213    // The next section deals with this location's breakpoint sites.
214    //------------------------------------------------------------------
215
216    //------------------------------------------------------------------
217    /// Try to resolve the breakpoint site for this location.
218    ///
219    /// @return
220    ///     \b true if we were successful at setting a breakpoint site,
221    ///     \b false otherwise.
222    //------------------------------------------------------------------
223    bool
224    ResolveBreakpointSite ();
225
226    //------------------------------------------------------------------
227    /// Clear this breakpoint location's breakpoint site - for instance
228    /// when disabling the breakpoint.
229    ///
230    /// @return
231    ///     \b true if there was a breakpoint site to be cleared, \b false
232    ///     otherwise.
233    //------------------------------------------------------------------
234    bool
235    ClearBreakpointSite ();
236
237    //------------------------------------------------------------------
238    /// Return whether this breakpoint location has a breakpoint site.
239    /// @return
240    ///     \b true if there was a breakpoint site for this breakpoint
241    ///     location, \b false otherwise.
242    //------------------------------------------------------------------
243    bool
244    IsResolved () const;
245
246    lldb::BreakpointSiteSP
247    GetBreakpointSite() const;
248
249    //------------------------------------------------------------------
250    // The next section are generic report functions.
251    //------------------------------------------------------------------
252
253    //------------------------------------------------------------------
254    /// Print a description of this breakpoint location to the stream
255    /// \a s.
256    ///
257    /// @param[in] s
258    ///     The stream to which to print the description.
259    ///
260    /// @param[in] level
261    ///     The description level that indicates the detail level to
262    ///     provide.
263    ///
264    /// @see lldb::DescriptionLevel
265    //------------------------------------------------------------------
266    void
267    GetDescription (Stream *s, lldb::DescriptionLevel level);
268
269    //------------------------------------------------------------------
270    /// Standard "Dump" method.  At present it does nothing.
271    //------------------------------------------------------------------
272    void
273    Dump(Stream *s) const override;
274
275    //------------------------------------------------------------------
276    /// Use this to set location specific breakpoint options.
277    ///
278    /// It will create a copy of the containing breakpoint's options if
279    /// that hasn't been done already
280    ///
281    /// @return
282    ///    A pointer to the breakpoint options.
283    //------------------------------------------------------------------
284    BreakpointOptions *
285    GetLocationOptions ();
286
287    //------------------------------------------------------------------
288    /// Use this to access breakpoint options from this breakpoint location.
289    /// This will point to the owning breakpoint's options unless options have
290    /// been set specifically on this location.
291    ///
292    /// @return
293    ///     A pointer to the containing breakpoint's options if this
294    ///     location doesn't have its own copy.
295    //------------------------------------------------------------------
296    const BreakpointOptions *
297    GetOptionsNoCreate () const;
298
299    bool
300    ValidForThisThread (Thread *thread);
301
302    //------------------------------------------------------------------
303    /// Invoke the callback action when the breakpoint is hit.
304    ///
305    /// Meant to be used by the BreakpointLocation class.
306    ///
307    /// @param[in] context
308    ///    Described the breakpoint event.
309    ///
310    /// @param[in] bp_loc_id
311    ///    Which breakpoint location hit this breakpoint.
312    ///
313    /// @return
314    ///     \b true if the target should stop at this breakpoint and \b
315    ///     false not.
316    //------------------------------------------------------------------
317    bool
318    InvokeCallback (StoppointCallbackContext *context);
319
320    //------------------------------------------------------------------
321    /// Returns whether we should resolve Indirect functions in setting the breakpoint site
322    /// for this location.
323    ///
324    /// @return
325    ///     \b true if the breakpoint SITE for this location should be set on the
326    ///     resolved location for Indirect functions.
327    //------------------------------------------------------------------
328    bool
329    ShouldResolveIndirectFunctions ()
330    {
331        return m_should_resolve_indirect_functions;
332    }
333
334    //------------------------------------------------------------------
335    /// Returns whether the address set in the breakpoint site for this location was found by resolving
336    /// an indirect symbol.
337    ///
338    /// @return
339    ///     \b true or \b false as given in the description above.
340    //------------------------------------------------------------------
341    bool
342    IsIndirect ()
343    {
344        return m_is_indirect;
345    }
346
347    void
348    SetIsIndirect (bool is_indirect)
349    {
350        m_is_indirect = is_indirect;
351    }
352
353    //------------------------------------------------------------------
354    /// Returns whether the address set in the breakpoint location was re-routed to the target of a
355    /// re-exported symbol.
356    ///
357    /// @return
358    ///     \b true or \b false as given in the description above.
359    //------------------------------------------------------------------
360    bool
361    IsReExported ()
362    {
363        return m_is_reexported;
364    }
365
366    void
367    SetIsReExported (bool is_reexported)
368    {
369        m_is_reexported = is_reexported;
370    }
371
372    //------------------------------------------------------------------
373    /// Returns whether the two breakpoint locations might represent "equivalent locations".
374    /// This is used when modules changed to determine if a Location in the old module might
375    /// be the "same as" the input location.
376    ///
377    /// @param[in] location
378    ///    The location to compare against.
379    ///
380    /// @return
381    ///     \b true or \b false as given in the description above.
382    //------------------------------------------------------------------
383    bool EquivalentToLocation(BreakpointLocation &location);
384
385protected:
386    friend class BreakpointSite;
387    friend class BreakpointLocationList;
388    friend class Process;
389    friend class StopInfoBreakpoint;
390
391    //------------------------------------------------------------------
392    /// Set the breakpoint site for this location to \a bp_site_sp.
393    ///
394    /// @param[in] bp_site_sp
395    ///      The breakpoint site we are setting for this location.
396    ///
397    /// @return
398    ///     \b true if we were successful at setting the breakpoint site,
399    ///     \b false otherwise.
400    //------------------------------------------------------------------
401    bool
402    SetBreakpointSite (lldb::BreakpointSiteSP& bp_site_sp);
403
404    void
405    DecrementIgnoreCount();
406
407    bool
408    IgnoreCountShouldStop();
409
410private:
411    void
412    SwapLocation (lldb::BreakpointLocationSP swap_from);
413
414    void
415    BumpHitCount();
416
417    void
418    UndoBumpHitCount();
419
420    //------------------------------------------------------------------
421    // Constructors and Destructors
422    //
423    // Only the Breakpoint can make breakpoint locations, and it owns
424    // them.
425    //------------------------------------------------------------------
426
427    //------------------------------------------------------------------
428    /// Constructor.
429    ///
430    /// @param[in] owner
431    ///     A back pointer to the breakpoint that owns this location.
432    ///
433    /// @param[in] addr
434    ///     The Address defining this location.
435    ///
436    /// @param[in] tid
437    ///     The thread for which this breakpoint location is valid, or
438    ///     LLDB_INVALID_THREAD_ID if it is valid for all threads.
439    ///
440    /// @param[in] hardware
441    ///     \b true if a hardware breakpoint is requested.
442    //------------------------------------------------------------------
443
444    BreakpointLocation (lldb::break_id_t bid,
445                        Breakpoint &owner,
446                        const Address &addr,
447                        lldb::tid_t tid,
448                        bool hardware,
449                        bool check_for_resolver = true);
450
451    //------------------------------------------------------------------
452    // Data members:
453    //------------------------------------------------------------------
454    bool m_being_created;
455    bool m_should_resolve_indirect_functions;
456    bool m_is_reexported;
457    bool m_is_indirect;
458    Address m_address; ///< The address defining this location.
459    Breakpoint &m_owner; ///< The breakpoint that produced this object.
460    std::unique_ptr<BreakpointOptions> m_options_ap; ///< Breakpoint options pointer, nullptr if we're using our breakpoint's options.
461    lldb::BreakpointSiteSP m_bp_site_sp; ///< Our breakpoint site (it may be shared by more than one location.)
462    lldb::UserExpressionSP m_user_expression_sp; ///< The compiled expression to use in testing our condition.
463    Mutex m_condition_mutex; ///< Guards parsing and evaluation of the condition, which could be evaluated by multiple processes.
464    size_t m_condition_hash; ///< For testing whether the condition source code changed.
465
466    void
467    SetShouldResolveIndirectFunctions (bool do_resolve)
468    {
469        m_should_resolve_indirect_functions = do_resolve;
470    }
471
472    void
473    SendBreakpointLocationChangedEvent (lldb::BreakpointEventType eventKind);
474
475    DISALLOW_COPY_AND_ASSIGN (BreakpointLocation);
476};
477
478} // namespace lldb_private
479
480#endif // liblldb_BreakpointLocation_h_
481