1 //===-- BreakpointLocation.h ------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
10 #define LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
11 
12 #include <memory>
13 #include <mutex>
14 
15 #include "lldb/Breakpoint/BreakpointOptions.h"
16 #include "lldb/Breakpoint/StoppointHitCounter.h"
17 #include "lldb/Core/Address.h"
18 #include "lldb/Utility/UserID.h"
19 #include "lldb/lldb-private.h"
20 
21 namespace lldb_private {
22 
23 /// \class BreakpointLocation BreakpointLocation.h
24 /// "lldb/Breakpoint/BreakpointLocation.h" Class that manages one unique (by
25 /// address) instance of a logical breakpoint.
26 
27 /// General Outline:
28 /// A breakpoint location is defined by the breakpoint that produces it,
29 /// and the address that resulted in this particular instantiation. Each
30 /// breakpoint location also may have a breakpoint site if its address has
31 /// been loaded into the program. Finally it has a settable options object.
32 ///
33 /// FIXME: Should we also store some fingerprint for the location, so
34 /// we can map one location to the "equivalent location" on rerun?  This would
35 /// be useful if you've set options on the locations.
36 
37 class BreakpointLocation
38     : public std::enable_shared_from_this<BreakpointLocation> {
39 public:
40   ~BreakpointLocation();
41 
42   /// Gets the load address for this breakpoint location \return
43   ///     Returns breakpoint location load address, \b
44   ///     LLDB_INVALID_ADDRESS if not yet set.
45   lldb::addr_t GetLoadAddress() const;
46 
47   /// Gets the Address for this breakpoint location \return
48   ///     Returns breakpoint location Address.
49   Address &GetAddress();
50   /// Gets the Breakpoint that created this breakpoint location \return
51   ///     Returns the owning breakpoint.
52   Breakpoint &GetBreakpoint();
53 
54   Target &GetTarget();
55 
56   /// Determines whether we should stop due to a hit at this breakpoint
57   /// location.
58   ///
59   /// Side Effects: This may evaluate the breakpoint condition, and run the
60   /// callback.  So this command may do a considerable amount of work.
61   ///
62   /// \return
63   ///     \b true if this breakpoint location thinks we should stop,
64   ///     \b false otherwise.
65   bool ShouldStop(StoppointCallbackContext *context);
66 
67   // The next section deals with various breakpoint options.
68 
69   /// If \a enabled is \b true, enable the breakpoint, if \b false disable it.
70   void SetEnabled(bool enabled);
71 
72   /// Check the Enable/Disable state.
73   ///
74   /// \return
75   ///     \b true if the breakpoint is enabled, \b false if disabled.
76   bool IsEnabled() const;
77 
78   /// If \a auto_continue is \b true, set the breakpoint to continue when hit.
79   void SetAutoContinue(bool auto_continue);
80 
81   /// Check the AutoContinue state.
82   ///
83   /// \return
84   ///     \b true if the breakpoint is set to auto-continue, \b false if not.
85   bool IsAutoContinue() const;
86 
87   /// Return the current Hit Count.
GetHitCount()88   uint32_t GetHitCount() const { return m_hit_counter.GetValue(); }
89 
90   /// Return the current Ignore Count.
91   ///
92   /// \return
93   ///     The number of breakpoint hits to be ignored.
94   uint32_t GetIgnoreCount() const;
95 
96   /// Set the breakpoint to ignore the next \a count breakpoint hits.
97   ///
98   /// \param[in] n
99   ///    The number of breakpoint hits to ignore.
100   void SetIgnoreCount(uint32_t n);
101 
102   /// Set the callback action invoked when the breakpoint is hit.
103   ///
104   /// The callback will return a bool indicating whether the target should
105   /// stop at this breakpoint or not.
106   ///
107   /// \param[in] callback
108   ///     The method that will get called when the breakpoint is hit.
109   ///
110   /// \param[in] callback_baton_sp
111   ///     A shared pointer to a Baton that provides the void * needed
112   ///     for the callback.
113   ///
114   /// \see lldb_private::Baton
115   void SetCallback(BreakpointHitCallback callback,
116                    const lldb::BatonSP &callback_baton_sp, bool is_synchronous);
117 
118   void SetCallback(BreakpointHitCallback callback, void *baton,
119                    bool is_synchronous);
120 
121   void ClearCallback();
122 
123   /// Set the breakpoint location's condition.
124   ///
125   /// \param[in] condition
126   ///    The condition expression to evaluate when the breakpoint is hit.
127   void SetCondition(const char *condition);
128 
129   /// Return a pointer to the text of the condition expression.
130   ///
131   /// \return
132   ///    A pointer to the condition expression text, or nullptr if no
133   //     condition has been set.
134   const char *GetConditionText(size_t *hash = nullptr) const;
135 
136   bool ConditionSaysStop(ExecutionContext &exe_ctx, Status &error);
137 
138   /// Set the valid thread to be checked when the breakpoint is hit.
139   ///
140   /// \param[in] thread_id
141   ///    If this thread hits the breakpoint, we stop, otherwise not.
142   void SetThreadID(lldb::tid_t thread_id);
143 
144   lldb::tid_t GetThreadID();
145 
146   void SetThreadIndex(uint32_t index);
147 
148   uint32_t GetThreadIndex() const;
149 
150   void SetThreadName(const char *thread_name);
151 
152   const char *GetThreadName() const;
153 
154   void SetQueueName(const char *queue_name);
155 
156   const char *GetQueueName() const;
157 
158   // The next section deals with this location's breakpoint sites.
159 
160   /// Try to resolve the breakpoint site for this location.
161   ///
162   /// \return
163   ///     \b true if we were successful at setting a breakpoint site,
164   ///     \b false otherwise.
165   bool ResolveBreakpointSite();
166 
167   /// Clear this breakpoint location's breakpoint site - for instance when
168   /// disabling the breakpoint.
169   ///
170   /// \return
171   ///     \b true if there was a breakpoint site to be cleared, \b false
172   ///     otherwise.
173   bool ClearBreakpointSite();
174 
175   /// Return whether this breakpoint location has a breakpoint site. \return
176   ///     \b true if there was a breakpoint site for this breakpoint
177   ///     location, \b false otherwise.
178   bool IsResolved() const;
179 
180   lldb::BreakpointSiteSP GetBreakpointSite() const;
181 
182   // The next section are generic report functions.
183 
184   /// Print a description of this breakpoint location to the stream \a s.
185   ///
186   /// \param[in] s
187   ///     The stream to which to print the description.
188   ///
189   /// \param[in] level
190   ///     The description level that indicates the detail level to
191   ///     provide.
192   ///
193   /// \see lldb::DescriptionLevel
194   void GetDescription(Stream *s, lldb::DescriptionLevel level);
195 
196   /// Standard "Dump" method.  At present it does nothing.
197   void Dump(Stream *s) const;
198 
199   /// Use this to set location specific breakpoint options.
200   ///
201   /// It will create a copy of the containing breakpoint's options if that
202   /// hasn't been done already
203   ///
204   /// \return
205   ///    A pointer to the breakpoint options.
206   BreakpointOptions *GetLocationOptions();
207 
208   /// Use this to access breakpoint options from this breakpoint location.
209   /// This will return the options that have a setting for the specified
210   /// BreakpointOptions kind.
211   ///
212   /// \param[in] kind
213   ///     The particular option you are looking up.
214   /// \return
215   ///     A pointer to the containing breakpoint's options if this
216   ///     location doesn't have its own copy.
217   const BreakpointOptions *GetOptionsSpecifyingKind(
218       BreakpointOptions::OptionKind kind) const;
219 
220   bool ValidForThisThread(Thread *thread);
221 
222   /// Invoke the callback action when the breakpoint is hit.
223   ///
224   /// Meant to be used by the BreakpointLocation class.
225   ///
226   /// \param[in] context
227   ///    Described the breakpoint event.
228   ///
229   /// \return
230   ///     \b true if the target should stop at this breakpoint and \b
231   ///     false not.
232   bool InvokeCallback(StoppointCallbackContext *context);
233 
234   /// Returns whether we should resolve Indirect functions in setting the
235   /// breakpoint site for this location.
236   ///
237   /// \return
238   ///     \b true if the breakpoint SITE for this location should be set on the
239   ///     resolved location for Indirect functions.
ShouldResolveIndirectFunctions()240   bool ShouldResolveIndirectFunctions() {
241     return m_should_resolve_indirect_functions;
242   }
243 
244   /// Returns whether the address set in the breakpoint site for this location
245   /// was found by resolving an indirect symbol.
246   ///
247   /// \return
248   ///     \b true or \b false as given in the description above.
IsIndirect()249   bool IsIndirect() { return m_is_indirect; }
250 
SetIsIndirect(bool is_indirect)251   void SetIsIndirect(bool is_indirect) { m_is_indirect = is_indirect; }
252 
253   /// Returns whether the address set in the breakpoint location was re-routed
254   /// to the target of a re-exported symbol.
255   ///
256   /// \return
257   ///     \b true or \b false as given in the description above.
IsReExported()258   bool IsReExported() { return m_is_reexported; }
259 
SetIsReExported(bool is_reexported)260   void SetIsReExported(bool is_reexported) { m_is_reexported = is_reexported; }
261 
262   /// Returns whether the two breakpoint locations might represent "equivalent
263   /// locations". This is used when modules changed to determine if a Location
264   /// in the old module might be the "same as" the input location.
265   ///
266   /// \param[in] location
267   ///    The location to compare against.
268   ///
269   /// \return
270   ///     \b true or \b false as given in the description above.
271   bool EquivalentToLocation(BreakpointLocation &location);
272 
273   /// Returns the breakpoint location ID.
GetID()274   lldb::break_id_t GetID() const { return m_loc_id; }
275 
276 protected:
277   friend class BreakpointSite;
278   friend class BreakpointLocationList;
279   friend class Process;
280   friend class StopInfoBreakpoint;
281 
282   /// Set the breakpoint site for this location to \a bp_site_sp.
283   ///
284   /// \param[in] bp_site_sp
285   ///      The breakpoint site we are setting for this location.
286   ///
287   /// \return
288   ///     \b true if we were successful at setting the breakpoint site,
289   ///     \b false otherwise.
290   bool SetBreakpointSite(lldb::BreakpointSiteSP &bp_site_sp);
291 
292   void DecrementIgnoreCount();
293 
294   bool IgnoreCountShouldStop();
295 
296 private:
297   void SwapLocation(lldb::BreakpointLocationSP swap_from);
298 
299   void BumpHitCount();
300 
301   void UndoBumpHitCount();
302 
303   // Constructors and Destructors
304   //
305   // Only the Breakpoint can make breakpoint locations, and it owns them.
306 
307   /// Constructor.
308   ///
309   /// \param[in] owner
310   ///     A back pointer to the breakpoint that owns this location.
311   ///
312   /// \param[in] addr
313   ///     The Address defining this location.
314   ///
315   /// \param[in] tid
316   ///     The thread for which this breakpoint location is valid, or
317   ///     LLDB_INVALID_THREAD_ID if it is valid for all threads.
318   ///
319   /// \param[in] hardware
320   ///     \b true if a hardware breakpoint is requested.
321 
322   BreakpointLocation(lldb::break_id_t bid, Breakpoint &owner,
323                      const Address &addr, lldb::tid_t tid, bool hardware,
324                      bool check_for_resolver = true);
325 
326   // Data members:
327   bool m_being_created;
328   bool m_should_resolve_indirect_functions;
329   bool m_is_reexported;
330   bool m_is_indirect;
331   Address m_address;   ///< The address defining this location.
332   Breakpoint &m_owner; ///< The breakpoint that produced this object.
333   std::unique_ptr<BreakpointOptions> m_options_up; ///< Breakpoint options
334                                                    /// pointer, nullptr if we're
335                                                    /// using our breakpoint's
336                                                    /// options.
337   lldb::BreakpointSiteSP m_bp_site_sp; ///< Our breakpoint site (it may be
338                                        ///shared by more than one location.)
339   lldb::UserExpressionSP m_user_expression_sp; ///< The compiled expression to
340                                                ///use in testing our condition.
341   std::mutex m_condition_mutex; ///< Guards parsing and evaluation of the
342                                 ///condition, which could be evaluated by
343                                 /// multiple processes.
344   size_t m_condition_hash; ///< For testing whether the condition source code
345                            ///changed.
346   lldb::break_id_t m_loc_id; ///< Breakpoint location ID.
347   StoppointHitCounter m_hit_counter; ///< Number of times this breakpoint
348                                      /// location has been hit.
349 
SetShouldResolveIndirectFunctions(bool do_resolve)350   void SetShouldResolveIndirectFunctions(bool do_resolve) {
351     m_should_resolve_indirect_functions = do_resolve;
352   }
353 
354   void SendBreakpointLocationChangedEvent(lldb::BreakpointEventType eventKind);
355 
356   BreakpointLocation(const BreakpointLocation &) = delete;
357   const BreakpointLocation &operator=(const BreakpointLocation &) = delete;
358 };
359 
360 } // namespace lldb_private
361 
362 #endif // LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
363