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 #include <memory>
14 #include <string>
15 #include <unordered_set>
16 #include <vector>
17 
18 #include "lldb/Breakpoint/BreakpointID.h"
19 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
20 #include "lldb/Breakpoint/BreakpointLocationList.h"
21 #include "lldb/Breakpoint/BreakpointName.h"
22 #include "lldb/Breakpoint/BreakpointOptions.h"
23 #include "lldb/Breakpoint/Stoppoint.h"
24 #include "lldb/Core/SearchFilter.h"
25 #include "lldb/Utility/Event.h"
26 #include "lldb/Utility/StringList.h"
27 #include "lldb/Utility/StructuredData.h"
28 
29 namespace lldb_private {
30 
31 //----------------------------------------------------------------------
32 /// @class Breakpoint Breakpoint.h "lldb/Breakpoint/Breakpoint.h" Class that
33 /// manages logical breakpoint setting.
34 //----------------------------------------------------------------------
35 
36 //----------------------------------------------------------------------
37 /// General Outline:
38 /// A breakpoint has four main parts, a filter, a resolver, the list of
39 /// breakpoint
40 /// locations that have been determined for the filter/resolver pair, and
41 /// finally a set of options for the breakpoint.
42 ///
43 /// \b Filter:
44 /// This is an object derived from SearchFilter.  It manages the search for
45 /// breakpoint location matches through the symbols in the module list of the
46 /// target that owns it.  It also filters out locations based on whatever
47 /// logic it wants.
48 ///
49 /// \b Resolver:
50 /// This is an object derived from BreakpointResolver.  It provides a callback
51 /// to the filter that will find breakpoint locations.  How it does this is
52 /// determined by what kind of resolver it is.
53 ///
54 /// The Breakpoint class also provides constructors for the common breakpoint
55 /// cases which make the appropriate filter and resolver for you.
56 ///
57 /// \b Location List:
58 /// This stores the breakpoint locations that have been determined to date.
59 /// For a given breakpoint, there will be only one location with a given
60 /// address.  Adding a location at an already taken address will just return
61 /// the location already at that address.  Locations can be looked up by ID,
62 /// or by address.
63 ///
64 /// \b Options:
65 /// This includes:
66 ///    \b Enabled/Disabled
67 ///    \b Ignore Count
68 ///    \b Callback
69 ///    \b Condition
70 /// Note, these options can be set on the breakpoint, and they can also be set
71 /// on the individual locations.  The options set on the breakpoint take
72 /// precedence over the options set on the individual location. So for
73 /// instance disabling the breakpoint will cause NONE of the locations to get
74 /// hit. But if the breakpoint is enabled, then the location's enabled state
75 /// will be checked to determine whether to insert that breakpoint location.
76 /// Similarly, if the breakpoint condition says "stop", we won't check the
77 /// location's condition. But if the breakpoint condition says "continue",
78 /// then we will check the location for whether to actually stop or not. One
79 /// subtle point worth observing here is that you don't actually stop at a
80 /// Breakpoint, you always stop at one of its locations.  So the "should stop"
81 /// tests are done by the location, not by the breakpoint.
82 //----------------------------------------------------------------------
83 class Breakpoint : public std::enable_shared_from_this<Breakpoint>,
84                    public Stoppoint {
85 public:
86   static const ConstString &GetEventIdentifier();
87 
88   //------------------------------------------------------------------
89   /// An enum specifying the match style for breakpoint settings.  At present
90   /// only used for function name style breakpoints.
91   //------------------------------------------------------------------
92   typedef enum { Exact, Regexp, Glob } MatchType;
93 
94 private:
95   enum class OptionNames : uint32_t { Names = 0, Hardware, LastOptionName };
96 
97   static const char
98       *g_option_names[static_cast<uint32_t>(OptionNames::LastOptionName)];
99 
GetKey(OptionNames enum_value)100   static const char *GetKey(OptionNames enum_value) {
101     return g_option_names[static_cast<uint32_t>(enum_value)];
102   }
103 
104 public:
105   class BreakpointEventData : public EventData {
106   public:
107     BreakpointEventData(lldb::BreakpointEventType sub_type,
108                         const lldb::BreakpointSP &new_breakpoint_sp);
109 
110     ~BreakpointEventData() override;
111 
112     static const ConstString &GetFlavorString();
113 
114     const ConstString &GetFlavor() const override;
115 
116     lldb::BreakpointEventType GetBreakpointEventType() const;
117 
118     lldb::BreakpointSP &GetBreakpoint();
119 
GetBreakpointLocationCollection()120     BreakpointLocationCollection &GetBreakpointLocationCollection() {
121       return m_locations;
122     }
123 
124     void Dump(Stream *s) const override;
125 
126     static lldb::BreakpointEventType
127     GetBreakpointEventTypeFromEvent(const lldb::EventSP &event_sp);
128 
129     static lldb::BreakpointSP
130     GetBreakpointFromEvent(const lldb::EventSP &event_sp);
131 
132     static lldb::BreakpointLocationSP
133     GetBreakpointLocationAtIndexFromEvent(const lldb::EventSP &event_sp,
134                                           uint32_t loc_idx);
135 
136     static size_t
137     GetNumBreakpointLocationsFromEvent(const lldb::EventSP &event_sp);
138 
139     static const BreakpointEventData *
140     GetEventDataFromEvent(const Event *event_sp);
141 
142   private:
143     lldb::BreakpointEventType m_breakpoint_event;
144     lldb::BreakpointSP m_new_breakpoint_sp;
145     BreakpointLocationCollection m_locations;
146 
147     DISALLOW_COPY_AND_ASSIGN(BreakpointEventData);
148   };
149 
150   class BreakpointPrecondition {
151   public:
152     virtual ~BreakpointPrecondition() = default;
153 
154     virtual bool EvaluatePrecondition(StoppointCallbackContext &context);
155 
156     virtual Status ConfigurePrecondition(Args &options);
157 
158     virtual void GetDescription(Stream &stream, lldb::DescriptionLevel level);
159   };
160 
161   typedef std::shared_ptr<BreakpointPrecondition> BreakpointPreconditionSP;
162 
163   // Saving & restoring breakpoints:
164   static lldb::BreakpointSP CreateFromStructuredData(
165       Target &target, StructuredData::ObjectSP &data_object_sp, Status &error);
166 
167   static bool
168   SerializedBreakpointMatchesNames(StructuredData::ObjectSP &bkpt_object_sp,
169                                    std::vector<std::string> &names);
170 
171   virtual StructuredData::ObjectSP SerializeToStructuredData();
172 
GetSerializationKey()173   static const char *GetSerializationKey() { return "Breakpoint"; }
174   //------------------------------------------------------------------
175   /// Destructor.
176   ///
177   /// The destructor is not virtual since there should be no reason to
178   /// subclass breakpoints.  The varieties of breakpoints are specified
179   /// instead by providing different resolvers & filters.
180   //------------------------------------------------------------------
181   ~Breakpoint() override;
182 
183   //------------------------------------------------------------------
184   // Methods
185   //------------------------------------------------------------------
186 
187   //------------------------------------------------------------------
188   /// Tell whether this breakpoint is an "internal" breakpoint. @return
189   ///     Returns \b true if this is an internal breakpoint, \b false otherwise.
190   //------------------------------------------------------------------
191   bool IsInternal() const;
192 
193   //------------------------------------------------------------------
194   /// Standard "Dump" method.  At present it does nothing.
195   //------------------------------------------------------------------
196   void Dump(Stream *s) override;
197 
198   //------------------------------------------------------------------
199   // The next set of methods provide ways to tell the breakpoint to update it's
200   // location list - usually done when modules appear or disappear.
201   //------------------------------------------------------------------
202 
203   //------------------------------------------------------------------
204   /// Tell this breakpoint to clear all its breakpoint sites.  Done when the
205   /// process holding the breakpoint sites is destroyed.
206   //------------------------------------------------------------------
207   void ClearAllBreakpointSites();
208 
209   //------------------------------------------------------------------
210   /// Tell this breakpoint to scan it's target's module list and resolve any
211   /// new locations that match the breakpoint's specifications.
212   //------------------------------------------------------------------
213   void ResolveBreakpoint();
214 
215   //------------------------------------------------------------------
216   /// Tell this breakpoint to scan a given module list and resolve any new
217   /// locations that match the breakpoint's specifications.
218   ///
219   /// @param[in] module_list
220   ///    The list of modules to look in for new locations.
221   ///
222   /// @param[in]  send_event
223   ///     If \b true, send a breakpoint location added event for non-internal
224   ///     breakpoints.
225   //------------------------------------------------------------------
226   void ResolveBreakpointInModules(ModuleList &module_list,
227                                   bool send_event = true);
228 
229   //------------------------------------------------------------------
230   /// Tell this breakpoint to scan a given module list and resolve any new
231   /// locations that match the breakpoint's specifications.
232   ///
233   /// @param[in] changed_modules
234   ///    The list of modules to look in for new locations.
235   ///
236   /// @param[in]  new_locations
237   ///     Fills new_locations with the new locations that were made.
238   //------------------------------------------------------------------
239   void ResolveBreakpointInModules(ModuleList &module_list,
240                                   BreakpointLocationCollection &new_locations);
241 
242   //------------------------------------------------------------------
243   /// Like ResolveBreakpointInModules, but allows for "unload" events, in
244   /// which case we will remove any locations that are in modules that got
245   /// unloaded.
246   ///
247   /// @param[in] changedModules
248   ///    The list of modules to look in for new locations.
249   /// @param[in] load_event
250   ///    If \b true then the modules were loaded, if \b false, unloaded.
251   /// @param[in] delete_locations
252   ///    If \b true then the modules were unloaded delete any locations in the
253   ///    changed modules.
254   //------------------------------------------------------------------
255   void ModulesChanged(ModuleList &changed_modules, bool load_event,
256                       bool delete_locations = false);
257 
258   //------------------------------------------------------------------
259   /// Tells the breakpoint the old module \a old_module_sp has been replaced
260   /// by new_module_sp (usually because the underlying file has been rebuilt,
261   /// and the old version is gone.)
262   ///
263   /// @param[in] old_module_sp
264   ///    The old module that is going away.
265   /// @param[in] new_module_sp
266   ///    The new module that is replacing it.
267   //------------------------------------------------------------------
268   void ModuleReplaced(lldb::ModuleSP old_module_sp,
269                       lldb::ModuleSP new_module_sp);
270 
271   //------------------------------------------------------------------
272   // The next set of methods provide access to the breakpoint locations for
273   // this breakpoint.
274   //------------------------------------------------------------------
275 
276   //------------------------------------------------------------------
277   /// Add a location to the breakpoint's location list.  This is only meant to
278   /// be called by the breakpoint's resolver.  FIXME: how do I ensure that?
279   ///
280   /// @param[in] addr
281   ///    The Address specifying the new location.
282   /// @param[out] new_location
283   ///    Set to \b true if a new location was created, to \b false if there
284   ///    already was a location at this Address.
285   /// @return
286   ///    Returns a pointer to the new location.
287   //------------------------------------------------------------------
288   lldb::BreakpointLocationSP AddLocation(const Address &addr,
289                                          bool *new_location = nullptr);
290 
291   //------------------------------------------------------------------
292   /// Find a breakpoint location by Address.
293   ///
294   /// @param[in] addr
295   ///    The Address specifying the location.
296   /// @return
297   ///    Returns a shared pointer to the location at \a addr.  The pointer
298   ///    in the shared pointer will be nullptr if there is no location at that
299   ///    address.
300   //------------------------------------------------------------------
301   lldb::BreakpointLocationSP FindLocationByAddress(const Address &addr);
302 
303   //------------------------------------------------------------------
304   /// Find a breakpoint location ID by Address.
305   ///
306   /// @param[in] addr
307   ///    The Address specifying the location.
308   /// @return
309   ///    Returns the UID of the location at \a addr, or \b LLDB_INVALID_ID if
310   ///    there is no breakpoint location at that address.
311   //------------------------------------------------------------------
312   lldb::break_id_t FindLocationIDByAddress(const Address &addr);
313 
314   //------------------------------------------------------------------
315   /// Find a breakpoint location for a given breakpoint location ID.
316   ///
317   /// @param[in] bp_loc_id
318   ///    The ID specifying the location.
319   /// @return
320   ///    Returns a shared pointer to the location with ID \a bp_loc_id.  The
321   ///    pointer
322   ///    in the shared pointer will be nullptr if there is no location with that
323   ///    ID.
324   //------------------------------------------------------------------
325   lldb::BreakpointLocationSP FindLocationByID(lldb::break_id_t bp_loc_id);
326 
327   //------------------------------------------------------------------
328   /// Get breakpoint locations by index.
329   ///
330   /// @param[in] index
331   ///    The location index.
332   ///
333   /// @return
334   ///     Returns a shared pointer to the location with index \a
335   ///     index. The shared pointer might contain nullptr if \a index is
336   ///     greater than then number of actual locations.
337   //------------------------------------------------------------------
338   lldb::BreakpointLocationSP GetLocationAtIndex(size_t index);
339 
340   //------------------------------------------------------------------
341   /// Removes all invalid breakpoint locations.
342   ///
343   /// Removes all breakpoint locations with architectures that aren't
344   /// compatible with \a arch. Also remove any breakpoint locations with whose
345   /// locations have address where the section has been deleted (module and
346   /// object files no longer exist).
347   ///
348   /// This is typically used after the process calls exec, or anytime the
349   /// architecture of the target changes.
350   ///
351   /// @param[in] arch
352   ///     If valid, check the module in each breakpoint to make sure
353   ///     they are compatible, otherwise, ignore architecture.
354   //------------------------------------------------------------------
355   void RemoveInvalidLocations(const ArchSpec &arch);
356 
357   //------------------------------------------------------------------
358   // The next section deals with various breakpoint options.
359   //------------------------------------------------------------------
360 
361   //------------------------------------------------------------------
362   /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
363   //------------------------------------------------------------------
364   void SetEnabled(bool enable) override;
365 
366   //------------------------------------------------------------------
367   /// Check the Enable/Disable state.
368   /// @return
369   ///     \b true if the breakpoint is enabled, \b false if disabled.
370   //------------------------------------------------------------------
371   bool IsEnabled() override;
372 
373   //------------------------------------------------------------------
374   /// Set the breakpoint to ignore the next \a count breakpoint hits.
375   /// @param[in] count
376   ///    The number of breakpoint hits to ignore.
377   //------------------------------------------------------------------
378   void SetIgnoreCount(uint32_t count);
379 
380   //------------------------------------------------------------------
381   /// Return the current ignore count/
382   /// @return
383   ///     The number of breakpoint hits to be ignored.
384   //------------------------------------------------------------------
385   uint32_t GetIgnoreCount() const;
386 
387   //------------------------------------------------------------------
388   /// Return the current hit count for all locations. @return
389   ///     The current hit count for all locations.
390   //------------------------------------------------------------------
391   uint32_t GetHitCount() const;
392 
393   //------------------------------------------------------------------
394   /// If \a one_shot is \b true, breakpoint will be deleted on first hit.
395   //------------------------------------------------------------------
396   void SetOneShot(bool one_shot);
397 
398   //------------------------------------------------------------------
399   /// Check the OneShot state.
400   /// @return
401   ///     \b true if the breakpoint is one shot, \b false otherwise.
402   //------------------------------------------------------------------
403   bool IsOneShot() const;
404 
405   //------------------------------------------------------------------
406   /// If \a auto_continue is \b true, breakpoint will auto-continue when on
407   /// hit.
408   //------------------------------------------------------------------
409   void SetAutoContinue(bool auto_continue);
410 
411   //------------------------------------------------------------------
412   /// Check the AutoContinue state.
413   /// @return
414   ///     \b true if the breakpoint is set to auto-continue, \b false otherwise.
415   //------------------------------------------------------------------
416   bool IsAutoContinue() const;
417 
418   //------------------------------------------------------------------
419   /// Set the valid thread to be checked when the breakpoint is hit.
420   /// @param[in] thread_id
421   ///    If this thread hits the breakpoint, we stop, otherwise not.
422   //------------------------------------------------------------------
423   void SetThreadID(lldb::tid_t thread_id);
424 
425   //------------------------------------------------------------------
426   /// Return the current stop thread value.
427   /// @return
428   ///     The thread id for which the breakpoint hit will stop,
429   ///     LLDB_INVALID_THREAD_ID for all threads.
430   //------------------------------------------------------------------
431   lldb::tid_t GetThreadID() const;
432 
433   void SetThreadIndex(uint32_t index);
434 
435   uint32_t GetThreadIndex() const;
436 
437   void SetThreadName(const char *thread_name);
438 
439   const char *GetThreadName() const;
440 
441   void SetQueueName(const char *queue_name);
442 
443   const char *GetQueueName() const;
444 
445   //------------------------------------------------------------------
446   /// Set the callback action invoked when the breakpoint is hit.
447   ///
448   /// @param[in] callback
449   ///    The method that will get called when the breakpoint is hit.
450   /// @param[in] baton
451   ///    A void * pointer that will get passed back to the callback function.
452   /// @param[in] is_synchronous
453   ///    If \b true the callback will be run on the private event thread
454   ///    before the stop event gets reported.  If false, the callback will get
455   ///    handled on the public event thread after the stop has been posted.
456   ///
457   /// @return
458   ///    \b true if the process should stop when you hit the breakpoint.
459   ///    \b false if it should continue.
460   //------------------------------------------------------------------
461   void SetCallback(BreakpointHitCallback callback, void *baton,
462                    bool is_synchronous = false);
463 
464   void SetCallback(BreakpointHitCallback callback,
465                    const lldb::BatonSP &callback_baton_sp,
466                    bool is_synchronous = false);
467 
468   void ClearCallback();
469 
470   //------------------------------------------------------------------
471   /// Set the breakpoint's condition.
472   ///
473   /// @param[in] condition
474   ///    The condition expression to evaluate when the breakpoint is hit.
475   ///    Pass in nullptr to clear the condition.
476   //------------------------------------------------------------------
477   void SetCondition(const char *condition);
478 
479   //------------------------------------------------------------------
480   /// Return a pointer to the text of the condition expression.
481   ///
482   /// @return
483   ///    A pointer to the condition expression text, or nullptr if no
484   //     condition has been set.
485   //------------------------------------------------------------------
486   const char *GetConditionText() const;
487 
488   //------------------------------------------------------------------
489   // The next section are various utility functions.
490   //------------------------------------------------------------------
491 
492   //------------------------------------------------------------------
493   /// Return the number of breakpoint locations that have resolved to actual
494   /// breakpoint sites.
495   ///
496   /// @return
497   ///     The number locations resolved breakpoint sites.
498   //------------------------------------------------------------------
499   size_t GetNumResolvedLocations() const;
500 
501   //------------------------------------------------------------------
502   /// Return whether this breakpoint has any resolved locations.
503   ///
504   /// @return
505   ///     True if GetNumResolvedLocations > 0
506   //------------------------------------------------------------------
507   bool HasResolvedLocations() const;
508 
509   //------------------------------------------------------------------
510   /// Return the number of breakpoint locations.
511   ///
512   /// @return
513   ///     The number breakpoint locations.
514   //------------------------------------------------------------------
515   size_t GetNumLocations() const;
516 
517   //------------------------------------------------------------------
518   /// Put a description of this breakpoint into the stream \a s.
519   ///
520   /// @param[in] s
521   ///     Stream into which to dump the description.
522   ///
523   /// @param[in] level
524   ///     The description level that indicates the detail level to
525   ///     provide.
526   ///
527   /// @see lldb::DescriptionLevel
528   //------------------------------------------------------------------
529   void GetDescription(Stream *s, lldb::DescriptionLevel level,
530                       bool show_locations = false);
531 
532   //------------------------------------------------------------------
533   /// Set the "kind" description for a breakpoint.  If the breakpoint is hit
534   /// the stop info will show this "kind" description instead of the
535   /// breakpoint number.  Mostly useful for internal breakpoints, where the
536   /// breakpoint number doesn't have meaning to the user.
537   ///
538   /// @param[in] kind
539   ///     New "kind" description.
540   //------------------------------------------------------------------
SetBreakpointKind(const char * kind)541   void SetBreakpointKind(const char *kind) { m_kind_description.assign(kind); }
542 
543   //------------------------------------------------------------------
544   /// Return the "kind" description for a breakpoint.
545   ///
546   /// @return
547   ///     The breakpoint kind, or nullptr if none is set.
548   //------------------------------------------------------------------
GetBreakpointKind()549   const char *GetBreakpointKind() const { return m_kind_description.c_str(); }
550 
551   //------------------------------------------------------------------
552   /// Accessor for the breakpoint Target.
553   /// @return
554   ///     This breakpoint's Target.
555   //------------------------------------------------------------------
GetTarget()556   Target &GetTarget() { return m_target; }
557 
GetTarget()558   const Target &GetTarget() const { return m_target; }
559 
560   const lldb::TargetSP GetTargetSP();
561 
562   void GetResolverDescription(Stream *s);
563 
564   //------------------------------------------------------------------
565   /// Find breakpoint locations which match the (filename, line_number)
566   /// description. The breakpoint location collection is to be filled with the
567   /// matching locations. It should be initialized with 0 size by the API
568   /// client.
569   ///
570   /// @return
571   ///     True if there is a match
572   ///
573   ///     The locations which match the filename and line_number in loc_coll.
574   ///     If its
575   ///     size is 0 and true is returned, it means the breakpoint fully matches
576   ///     the
577   ///     description.
578   //------------------------------------------------------------------
579   bool GetMatchingFileLine(const ConstString &filename, uint32_t line_number,
580                            BreakpointLocationCollection &loc_coll);
581 
582   void GetFilterDescription(Stream *s);
583 
584   //------------------------------------------------------------------
585   /// Returns the BreakpointOptions structure set at the breakpoint level.
586   ///
587   /// Meant to be used by the BreakpointLocation class.
588   ///
589   /// @return
590   ///     A pointer to this breakpoint's BreakpointOptions.
591   //------------------------------------------------------------------
592   BreakpointOptions *GetOptions();
593 
594   //------------------------------------------------------------------
595   /// Returns the BreakpointOptions structure set at the breakpoint level.
596   ///
597   /// Meant to be used by the BreakpointLocation class.
598   ///
599   /// @return
600   ///     A pointer to this breakpoint's BreakpointOptions.
601   //------------------------------------------------------------------
602   const BreakpointOptions *GetOptions() const;
603 
604   //------------------------------------------------------------------
605   /// Invoke the callback action when the breakpoint is hit.
606   ///
607   /// Meant to be used by the BreakpointLocation class.
608   ///
609   /// @param[in] context
610   ///     Described the breakpoint event.
611   ///
612   /// @param[in] bp_loc_id
613   ///     Which breakpoint location hit this breakpoint.
614   ///
615   /// @return
616   ///     \b true if the target should stop at this breakpoint and \b false not.
617   //------------------------------------------------------------------
618   bool InvokeCallback(StoppointCallbackContext *context,
619                       lldb::break_id_t bp_loc_id);
620 
IsHardware()621   bool IsHardware() const { return m_hardware; }
622 
GetResolver()623   lldb::BreakpointResolverSP GetResolver() { return m_resolver_sp; }
624 
GetSearchFilter()625   lldb::SearchFilterSP GetSearchFilter() { return m_filter_sp; }
626 
627 private: // The target needs to manage adding & removing names.  It will do the
628          // checking for name validity as well.
629   bool AddName(llvm::StringRef new_name);
630 
RemoveName(const char * name_to_remove)631   void RemoveName(const char *name_to_remove) {
632     if (name_to_remove)
633       m_name_list.erase(name_to_remove);
634   }
635 
636 public:
MatchesName(const char * name)637   bool MatchesName(const char *name) {
638     return m_name_list.find(name) != m_name_list.end();
639   }
640 
GetNames(std::vector<std::string> & names)641   void GetNames(std::vector<std::string> &names) {
642     names.clear();
643     for (auto name : m_name_list) {
644       names.push_back(name);
645     }
646   }
647 
648   //------------------------------------------------------------------
649   /// Set a pre-condition filter that overrides all user provided
650   /// filters/callbacks etc.
651   ///
652   /// Used to define fancy breakpoints that can do dynamic hit detection
653   /// without taking up the condition slot - which really belongs to the user
654   /// anyway...
655   ///
656   /// The Precondition should not continue the target, it should return true
657   /// if the condition says to stop and false otherwise.
658   ///
659   //------------------------------------------------------------------
SetPrecondition(BreakpointPreconditionSP precondition_sp)660   void SetPrecondition(BreakpointPreconditionSP precondition_sp) {
661     m_precondition_sp = precondition_sp;
662   }
663 
664   bool EvaluatePrecondition(StoppointCallbackContext &context);
665 
GetPrecondition()666   BreakpointPreconditionSP GetPrecondition() { return m_precondition_sp; }
667 
668   // Produces the OR'ed values for all the names assigned to this breakpoint.
GetPermissions()669   const BreakpointName::Permissions &GetPermissions() const {
670       return m_permissions;
671   }
672 
GetPermissions()673   BreakpointName::Permissions &GetPermissions() {
674       return m_permissions;
675   }
676 
AllowList()677   bool AllowList() const {
678     return GetPermissions().GetAllowList();
679   }
AllowDisable()680   bool AllowDisable() const {
681     return GetPermissions().GetAllowDisable();
682   }
AllowDelete()683   bool AllowDelete() const {
684     return GetPermissions().GetAllowDelete();
685   }
686 
687 protected:
688   friend class Target;
689   //------------------------------------------------------------------
690   // Protected Methods
691   //------------------------------------------------------------------
692 
693   //------------------------------------------------------------------
694   /// Constructors and Destructors
695   /// Only the Target can make a breakpoint, and it owns the breakpoint
696   /// lifespans. The constructor takes a filter and a resolver.  Up in Target
697   /// there are convenience variants that make breakpoints for some common
698   /// cases.
699   ///
700   /// @param[in] target
701   ///    The target in which the breakpoint will be set.
702   ///
703   /// @param[in] filter_sp
704   ///    Shared pointer to the search filter that restricts the search domain of
705   ///    the breakpoint.
706   ///
707   /// @param[in] resolver_sp
708   ///    Shared pointer to the resolver object that will determine breakpoint
709   ///    matches.
710   ///
711   /// @param hardware
712   ///    If true, request a hardware breakpoint to be used to implement the
713   ///    breakpoint locations.
714   ///
715   /// @param resolve_indirect_symbols
716   ///    If true, and the address of a given breakpoint location in this
717   ///    breakpoint is set on an
718   ///    indirect symbol (i.e. Symbol::IsIndirect returns true) then the actual
719   ///    breakpoint site will
720   ///    be set on the target of the indirect symbol.
721   //------------------------------------------------------------------
722   // This is the generic constructor
723   Breakpoint(Target &target, lldb::SearchFilterSP &filter_sp,
724              lldb::BreakpointResolverSP &resolver_sp, bool hardware,
725              bool resolve_indirect_symbols = true);
726 
727   friend class BreakpointLocation; // To call the following two when determining
728                                    // whether to stop.
729 
730   void DecrementIgnoreCount();
731 
732   // BreakpointLocation::IgnoreCountShouldStop &
733   // Breakpoint::IgnoreCountShouldStop can only be called once per stop, and
734   // BreakpointLocation::IgnoreCountShouldStop should be tested first, and if
735   // it returns false we should continue, otherwise we should test
736   // Breakpoint::IgnoreCountShouldStop.
737 
738   bool IgnoreCountShouldStop();
739 
IncrementHitCount()740   void IncrementHitCount() { m_hit_count++; }
741 
DecrementHitCount()742   void DecrementHitCount() {
743     assert(m_hit_count > 0);
744     m_hit_count--;
745   }
746 
747 private:
748   // This one should only be used by Target to copy breakpoints from target to
749   // target - primarily from the dummy target to prime new targets.
750   Breakpoint(Target &new_target, Breakpoint &bp_to_copy_from);
751 
752   //------------------------------------------------------------------
753   // For Breakpoint only
754   //------------------------------------------------------------------
755   bool m_being_created;
756   bool
757       m_hardware; // If this breakpoint is required to use a hardware breakpoint
758   Target &m_target; // The target that holds this breakpoint.
759   std::unordered_set<std::string> m_name_list; // If not empty, this is the name
760                                                // of this breakpoint (many
761                                                // breakpoints can share the same
762                                                // name.)
763   lldb::SearchFilterSP
764       m_filter_sp; // The filter that constrains the breakpoint's domain.
765   lldb::BreakpointResolverSP
766       m_resolver_sp; // The resolver that defines this breakpoint.
767   BreakpointPreconditionSP m_precondition_sp; // The precondition is a
768                                               // breakpoint-level hit filter
769                                               // that can be used
770   // to skip certain breakpoint hits.  For instance, exception breakpoints use
771   // this to limit the stop to certain exception classes, while leaving the
772   // condition & callback free for user specification.
773   std::unique_ptr<BreakpointOptions>
774       m_options_up; // Settable breakpoint options
775   BreakpointLocationList
776       m_locations; // The list of locations currently found for this breakpoint.
777   std::string m_kind_description;
778   bool m_resolve_indirect_symbols;
779   uint32_t m_hit_count; // Number of times this breakpoint/watchpoint has been
780                         // hit.  This is kept
781   // separately from the locations hit counts, since locations can go away when
782   // their backing library gets unloaded, and we would lose hit counts.
783   BreakpointName::Permissions m_permissions;
784 
785   void SendBreakpointChangedEvent(lldb::BreakpointEventType eventKind);
786 
787   void SendBreakpointChangedEvent(BreakpointEventData *data);
788 
789   DISALLOW_COPY_AND_ASSIGN(Breakpoint);
790 };
791 
792 } // namespace lldb_private
793 
794 #endif // liblldb_Breakpoint_h_
795