1 //===-- Breakpoint.h --------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Breakpoint_h_
11 #define liblldb_Breakpoint_h_
17 #include <unordered_set>
20 // Other libraries and framework includes
22 #include "lldb/Breakpoint/BreakpointID.h"
23 #include "lldb/Breakpoint/BreakpointLocationList.h"
24 #include "lldb/Breakpoint/BreakpointOptions.h"
25 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
26 #include "lldb/Breakpoint/Stoppoint.h"
27 #include "lldb/Core/SearchFilter.h"
28 #include "lldb/Core/Event.h"
29 #include "lldb/Core/StringList.h"
31 namespace lldb_private {
33 //----------------------------------------------------------------------
34 /// @class Breakpoint Breakpoint.h "lldb/Breakpoint/Breakpoint.h"
35 /// @brief Class that manages logical breakpoint setting.
36 //----------------------------------------------------------------------
38 //----------------------------------------------------------------------
40 /// A breakpoint has four main parts, a filter, a resolver, the list of breakpoint
41 /// locations that have been determined for the filter/resolver pair, and finally
42 /// a set of options for the breakpoint.
45 /// This is an object derived from SearchFilter. It manages the search
46 /// for breakpoint location matches through the symbols in the module list of the target
47 /// that owns it. It also filters out locations based on whatever logic it wants.
50 /// This is an object derived from BreakpointResolver. It provides a
51 /// callback to the filter that will find breakpoint locations. How it does this is
52 /// determined by what kind of resolver it is.
54 /// The Breakpoint class also provides constructors for the common breakpoint cases
55 /// which make the appropriate filter and resolver for you.
58 /// This stores the breakpoint locations that have been determined
59 /// to date. 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 the location
61 /// already at that address. Locations can be looked up by ID, or by address.
65 /// \b Enabled/Disabled
69 /// Note, these options can be set on the breakpoint, and they can also be set on the
70 /// individual locations. The options set on the breakpoint take precedence over the
71 /// options set on the individual location.
72 /// So for instance disabling the breakpoint will cause NONE of the locations to get hit.
73 /// But if the breakpoint is enabled, then the location's enabled state will be checked
74 /// to determine whether to insert that breakpoint location.
75 /// Similarly, if the breakpoint condition says "stop", we won't check the location's condition.
76 /// But if the breakpoint condition says "continue", then we will check the location for whether
77 /// to actually stop or not.
78 /// One subtle point worth observing here is that you don't actually stop at a Breakpoint, you
79 /// always stop at one of its locations. So the "should stop" tests are done by the location,
80 /// not by the breakpoint.
81 //----------------------------------------------------------------------
83 public std::enable_shared_from_this<Breakpoint>,
87 static const ConstString &
88 GetEventIdentifier ();
90 //------------------------------------------------------------------
91 /// An enum specifying the match style for breakpoint settings. At
92 /// present only used for function name style breakpoints.
93 //------------------------------------------------------------------
101 class BreakpointEventData :
105 BreakpointEventData (lldb::BreakpointEventType sub_type,
106 const lldb::BreakpointSP &new_breakpoint_sp);
108 ~BreakpointEventData() override;
110 static const ConstString &
114 GetFlavor() const override;
116 lldb::BreakpointEventType
117 GetBreakpointEventType () const;
122 BreakpointLocationCollection &
123 GetBreakpointLocationCollection()
129 Dump(Stream *s) const override;
131 static lldb::BreakpointEventType
132 GetBreakpointEventTypeFromEvent (const lldb::EventSP &event_sp);
134 static lldb::BreakpointSP
135 GetBreakpointFromEvent (const lldb::EventSP &event_sp);
137 static lldb::BreakpointLocationSP
138 GetBreakpointLocationAtIndexFromEvent (const lldb::EventSP &event_sp, uint32_t loc_idx);
141 GetNumBreakpointLocationsFromEvent (const lldb::EventSP &event_sp);
143 static const BreakpointEventData *
144 GetEventDataFromEvent (const Event *event_sp);
147 lldb::BreakpointEventType m_breakpoint_event;
148 lldb::BreakpointSP m_new_breakpoint_sp;
149 BreakpointLocationCollection m_locations;
151 DISALLOW_COPY_AND_ASSIGN (BreakpointEventData);
154 class BreakpointPrecondition
157 virtual ~BreakpointPrecondition() = default;
160 EvaluatePrecondition(StoppointCallbackContext &context);
163 ConfigurePrecondition(Args &options);
166 GetDescription(Stream &stream, lldb::DescriptionLevel level);
169 typedef std::shared_ptr<BreakpointPrecondition> BreakpointPreconditionSP;
171 //------------------------------------------------------------------
174 /// The destructor is not virtual since there should be no reason to subclass
175 /// breakpoints. The varieties of breakpoints are specified instead by
176 /// providing different resolvers & filters.
177 //------------------------------------------------------------------
178 ~Breakpoint() override;
180 //------------------------------------------------------------------
182 //------------------------------------------------------------------
184 //------------------------------------------------------------------
185 /// Tell whether this breakpoint is an "internal" breakpoint.
187 /// Returns \b true if this is an internal breakpoint, \b false otherwise.
188 //------------------------------------------------------------------
192 //------------------------------------------------------------------
193 /// Standard "Dump" method. At present it does nothing.
194 //------------------------------------------------------------------
196 Dump(Stream *s) override;
198 //------------------------------------------------------------------
199 // The next set of methods provide ways to tell the breakpoint to update
200 // it's location list - usually done when modules appear or disappear.
201 //------------------------------------------------------------------
203 //------------------------------------------------------------------
204 /// Tell this breakpoint to clear all its breakpoint sites. Done
205 /// when the process holding the breakpoint sites is destroyed.
206 //------------------------------------------------------------------
208 ClearAllBreakpointSites ();
210 //------------------------------------------------------------------
211 /// Tell this breakpoint to scan it's target's module list and resolve any
212 /// new locations that match the breakpoint's specifications.
213 //------------------------------------------------------------------
215 ResolveBreakpoint ();
217 //------------------------------------------------------------------
218 /// Tell this breakpoint to scan a given module list and resolve any
219 /// new locations that match the breakpoint's specifications.
221 /// @param[in] module_list
222 /// The list of modules to look in for new locations.
224 /// @param[in] send_event
225 /// If \b true, send a breakpoint location added event for non-internal breakpoints.
226 //------------------------------------------------------------------
228 ResolveBreakpointInModules (ModuleList &module_list, bool send_event = true);
230 //------------------------------------------------------------------
231 /// Tell this breakpoint to scan a given module list and resolve any
232 /// new locations that match the breakpoint's specifications.
234 /// @param[in] changed_modules
235 /// The list of modules to look in for new locations.
237 /// @param[in] new_locations
238 /// Fills new_locations with the new locations that were made.
239 //------------------------------------------------------------------
241 ResolveBreakpointInModules (ModuleList &module_list, BreakpointLocationCollection &new_locations);
243 //------------------------------------------------------------------
244 /// Like ResolveBreakpointInModules, but allows for "unload" events, in
245 /// which case we will remove any locations that are in modules that got
248 /// @param[in] changedModules
249 /// The list of modules to look in for new locations.
250 /// @param[in] load_event
251 /// If \b true then the modules were loaded, if \b false, unloaded.
252 /// @param[in] delete_locations
253 /// If \b true then the modules were unloaded delete any locations in the changed modules.
254 //------------------------------------------------------------------
256 ModulesChanged (ModuleList &changed_modules,
258 bool delete_locations = false);
260 //------------------------------------------------------------------
261 /// Tells the breakpoint the old module \a old_module_sp has been
262 /// replaced by new_module_sp (usually because the underlying file has been
263 /// rebuilt, and the old version is gone.)
265 /// @param[in] old_module_sp
266 /// The old module that is going away.
267 /// @param[in] new_module_sp
268 /// The new module that is replacing it.
269 //------------------------------------------------------------------
271 ModuleReplaced (lldb::ModuleSP old_module_sp, lldb::ModuleSP new_module_sp);
273 //------------------------------------------------------------------
274 // The next set of methods provide access to the breakpoint locations
275 // for this breakpoint.
276 //------------------------------------------------------------------
278 //------------------------------------------------------------------
279 /// Add a location to the breakpoint's location list. This is only meant
280 /// to be called by the breakpoint's resolver. FIXME: how do I ensure that?
283 /// The Address specifying the new location.
284 /// @param[out] new_location
285 /// Set to \b true if a new location was created, to \b false if there
286 /// already was a location at this Address.
288 /// Returns a pointer to the new location.
289 //------------------------------------------------------------------
290 lldb::BreakpointLocationSP
291 AddLocation(const Address &addr,
292 bool *new_location = nullptr);
294 //------------------------------------------------------------------
295 /// Find a breakpoint location by Address.
298 /// The Address specifying the location.
300 /// Returns a shared pointer to the location at \a addr. The pointer
301 /// in the shared pointer will be nullptr if there is no location at that address.
302 //------------------------------------------------------------------
303 lldb::BreakpointLocationSP
304 FindLocationByAddress (const Address &addr);
306 //------------------------------------------------------------------
307 /// Find a breakpoint location ID by Address.
310 /// The Address specifying the location.
312 /// Returns the UID of the location at \a addr, or \b LLDB_INVALID_ID if
313 /// there is no breakpoint location at that address.
314 //------------------------------------------------------------------
316 FindLocationIDByAddress (const Address &addr);
318 //------------------------------------------------------------------
319 /// Find a breakpoint location for a given breakpoint location ID.
321 /// @param[in] bp_loc_id
322 /// The ID specifying the location.
324 /// Returns a shared pointer to the location with ID \a bp_loc_id. The pointer
325 /// in the shared pointer will be nullptr if there is no location with that ID.
326 //------------------------------------------------------------------
327 lldb::BreakpointLocationSP
328 FindLocationByID (lldb::break_id_t bp_loc_id);
330 //------------------------------------------------------------------
331 /// Get breakpoint locations by index.
334 /// The location index.
337 /// Returns a shared pointer to the location with index \a
338 /// index. The shared pointer might contain nullptr if \a index is
339 /// greater than then number of actual locations.
340 //------------------------------------------------------------------
341 lldb::BreakpointLocationSP
342 GetLocationAtIndex (size_t index);
344 //------------------------------------------------------------------
345 /// Removes all invalid breakpoint locations.
347 /// Removes all breakpoint locations with architectures that aren't
348 /// compatible with \a arch. Also remove any breakpoint locations
349 /// with whose locations have address where the section has been
350 /// deleted (module and object files no longer exist).
352 /// This is typically used after the process calls exec, or anytime
353 /// the architecture of the target changes.
356 /// If valid, check the module in each breakpoint to make sure
357 /// they are compatible, otherwise, ignore architecture.
358 //------------------------------------------------------------------
360 RemoveInvalidLocations (const ArchSpec &arch);
362 //------------------------------------------------------------------
363 // The next section deals with various breakpoint options.
364 //------------------------------------------------------------------
366 //------------------------------------------------------------------
367 /// If \a enable is \b true, enable the breakpoint, if \b false disable it.
368 //------------------------------------------------------------------
370 SetEnabled(bool enable) override;
372 //------------------------------------------------------------------
373 /// Check the Enable/Disable state.
375 /// \b true if the breakpoint is enabled, \b false if disabled.
376 //------------------------------------------------------------------
378 IsEnabled() override;
380 //------------------------------------------------------------------
381 /// Set the breakpoint to ignore the next \a count breakpoint hits.
383 /// The number of breakpoint hits to ignore.
384 //------------------------------------------------------------------
386 SetIgnoreCount (uint32_t count);
388 //------------------------------------------------------------------
389 /// Return the current ignore count/
391 /// The number of breakpoint hits to be ignored.
392 //------------------------------------------------------------------
394 GetIgnoreCount () const;
396 //------------------------------------------------------------------
397 /// Return the current hit count for all locations.
399 /// The current hit count for all locations.
400 //------------------------------------------------------------------
402 GetHitCount () const;
404 //------------------------------------------------------------------
405 /// If \a one_shot is \b true, breakpoint will be deleted on first hit.
406 //------------------------------------------------------------------
408 SetOneShot (bool one_shot);
410 //------------------------------------------------------------------
411 /// Check the OneShot state.
413 /// \b true if the breakpoint is one shot, \b false otherwise.
414 //------------------------------------------------------------------
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 //------------------------------------------------------------------
424 SetThreadID (lldb::tid_t thread_id);
426 //------------------------------------------------------------------
427 /// Return the current stop thread value.
429 /// The thread id for which the breakpoint hit will stop, LLDB_INVALID_THREAD_ID for all threads.
430 //------------------------------------------------------------------
432 GetThreadID () const;
435 SetThreadIndex (uint32_t index);
438 GetThreadIndex() const;
441 SetThreadName (const char *thread_name);
444 GetThreadName () const;
447 SetQueueName (const char *queue_name);
450 GetQueueName () const;
452 //------------------------------------------------------------------
453 /// Set the callback action invoked when the breakpoint is hit.
455 /// @param[in] callback
456 /// The method that will get called when the breakpoint is hit.
458 /// A void * pointer that will get passed back to the callback function.
459 /// @param[in] is_synchronous
460 /// If \b true the callback will be run on the private event thread
461 /// before the stop event gets reported. If false, the callback will get
462 /// handled on the public event thread after the stop has been posted.
465 /// \b true if the process should stop when you hit the breakpoint.
466 /// \b false if it should continue.
467 //------------------------------------------------------------------
469 SetCallback (BreakpointHitCallback callback,
471 bool is_synchronous = false);
474 SetCallback (BreakpointHitCallback callback,
475 const lldb::BatonSP &callback_baton_sp,
476 bool is_synchronous = false);
481 //------------------------------------------------------------------
482 /// Set the breakpoint's condition.
484 /// @param[in] condition
485 /// The condition expression to evaluate when the breakpoint is hit.
486 /// Pass in nullptr to clear the condition.
487 //------------------------------------------------------------------
488 void SetCondition (const char *condition);
490 //------------------------------------------------------------------
491 /// Return a pointer to the text of the condition expression.
494 /// A pointer to the condition expression text, or nullptr if no
495 // condition has been set.
496 //------------------------------------------------------------------
497 const char *GetConditionText () const;
499 //------------------------------------------------------------------
500 // The next section are various utility functions.
501 //------------------------------------------------------------------
503 //------------------------------------------------------------------
504 /// Return the number of breakpoint locations that have resolved to
505 /// actual breakpoint sites.
508 /// The number locations resolved breakpoint sites.
509 //------------------------------------------------------------------
511 GetNumResolvedLocations() const;
513 //------------------------------------------------------------------
514 /// Return the number of breakpoint locations.
517 /// The number breakpoint locations.
518 //------------------------------------------------------------------
520 GetNumLocations() const;
522 //------------------------------------------------------------------
523 /// Put a description of this breakpoint into the stream \a s.
526 /// Stream into which to dump the description.
529 /// The description level that indicates the detail level to
532 /// @see lldb::DescriptionLevel
533 //------------------------------------------------------------------
535 GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_locations = false);
537 //------------------------------------------------------------------
538 /// Set the "kind" description for a breakpoint. If the breakpoint is hit
539 /// the stop info will show this "kind" description instead of the breakpoint
540 /// number. Mostly useful for internal breakpoints, where the breakpoint number
541 /// doesn't have meaning to the user.
544 /// New "kind" description.
545 //------------------------------------------------------------------
547 SetBreakpointKind (const char *kind)
549 m_kind_description.assign (kind);
552 //------------------------------------------------------------------
553 /// Return the "kind" description for a breakpoint.
556 /// The breakpoint kind, or nullptr if none is set.
557 //------------------------------------------------------------------
558 const char *GetBreakpointKind () const
560 return m_kind_description.c_str();
563 //------------------------------------------------------------------
564 /// Accessor for the breakpoint Target.
566 /// This breakpoint's Target.
567 //------------------------------------------------------------------
584 GetResolverDescription (Stream *s);
586 //------------------------------------------------------------------
587 /// Find breakpoint locations which match the (filename, line_number) description.
588 /// The breakpoint location collection is to be filled with the matching locations.
589 /// It should be initialized with 0 size by the API client.
592 /// True if there is a match
594 /// The locations which match the filename and line_number in loc_coll. If its
595 /// size is 0 and true is returned, it means the breakpoint fully matches the
597 //------------------------------------------------------------------
598 bool GetMatchingFileLine(const ConstString &filename, uint32_t line_number,
599 BreakpointLocationCollection &loc_coll);
602 GetFilterDescription (Stream *s);
604 //------------------------------------------------------------------
605 /// Returns the BreakpointOptions structure set at the breakpoint level.
607 /// Meant to be used by the BreakpointLocation class.
610 /// A pointer to this breakpoint's BreakpointOptions.
611 //------------------------------------------------------------------
615 //------------------------------------------------------------------
616 /// Invoke the callback action when the breakpoint is hit.
618 /// Meant to be used by the BreakpointLocation class.
620 /// @param[in] context
621 /// Described the breakpoint event.
623 /// @param[in] bp_loc_id
624 /// Which breakpoint location hit this breakpoint.
627 /// \b true if the target should stop at this breakpoint and \b false not.
628 //------------------------------------------------------------------
630 InvokeCallback (StoppointCallbackContext *context,
631 lldb::break_id_t bp_loc_id);
639 lldb::BreakpointResolverSP
642 return m_resolver_sp;
652 AddName (const char *new_name, Error &error);
655 RemoveName (const char *name_to_remove)
658 m_name_list.erase(name_to_remove);
662 MatchesName (const char *name)
664 return m_name_list.find(name) != m_name_list.end();
668 GetNames (std::vector<std::string> &names)
671 for (auto name : m_name_list)
673 names.push_back(name);
677 //------------------------------------------------------------------
678 /// Set a pre-condition filter that overrides all user provided filters/callbacks etc.
680 /// Used to define fancy breakpoints that can do dynamic hit detection without taking up the condition slot -
681 /// which really belongs to the user anyway...
683 /// The Precondition should not continue the target, it should return true if the condition says to stop and
686 //------------------------------------------------------------------
688 SetPrecondition(BreakpointPreconditionSP precondition_sp)
690 m_precondition_sp = precondition_sp;
694 EvaluatePrecondition (StoppointCallbackContext &context);
696 BreakpointPreconditionSP
699 return m_precondition_sp;
704 //------------------------------------------------------------------
706 //------------------------------------------------------------------
708 //------------------------------------------------------------------
709 /// Constructors and Destructors
710 /// Only the Target can make a breakpoint, and it owns the breakpoint lifespans.
711 /// The constructor takes a filter and a resolver. Up in Target there are convenience
712 /// variants that make breakpoints for some common cases.
714 /// @param[in] target
715 /// The target in which the breakpoint will be set.
717 /// @param[in] filter_sp
718 /// Shared pointer to the search filter that restricts the search domain of the breakpoint.
720 /// @param[in] resolver_sp
721 /// Shared pointer to the resolver object that will determine breakpoint matches.
724 /// If true, request a hardware breakpoint to be used to implement the breakpoint locations.
726 /// @param resolve_indirect_symbols
727 /// If true, and the address of a given breakpoint location in this breakpoint is set on an
728 /// indirect symbol (i.e. Symbol::IsIndirect returns true) then the actual breakpoint site will
729 /// be set on the target of the indirect symbol.
730 //------------------------------------------------------------------
731 // This is the generic constructor
732 Breakpoint(Target &target,
733 lldb::SearchFilterSP &filter_sp,
734 lldb::BreakpointResolverSP &resolver_sp,
736 bool resolve_indirect_symbols = true);
738 friend class BreakpointLocation; // To call the following two when determining whether to stop.
741 DecrementIgnoreCount();
743 // BreakpointLocation::IgnoreCountShouldStop & Breakpoint::IgnoreCountShouldStop can only be called once per stop,
744 // and BreakpointLocation::IgnoreCountShouldStop should be tested first, and if it returns false we should
745 // continue, otherwise we should test Breakpoint::IgnoreCountShouldStop.
748 IgnoreCountShouldStop ();
759 assert (m_hit_count > 0);
764 // This one should only be used by Target to copy breakpoints from target to target - primarily from the dummy
765 // target to prime new targets.
766 Breakpoint (Target &new_target,
767 Breakpoint &bp_to_copy_from);
769 //------------------------------------------------------------------
770 // For Breakpoint only
771 //------------------------------------------------------------------
772 bool m_being_created;
773 bool m_hardware; // If this breakpoint is required to use a hardware breakpoint
774 Target &m_target; // The target that holds this breakpoint.
775 std::unordered_set<std::string> m_name_list; // If not empty, this is the name of this breakpoint (many breakpoints can share the same name.)
776 lldb::SearchFilterSP m_filter_sp; // The filter that constrains the breakpoint's domain.
777 lldb::BreakpointResolverSP m_resolver_sp; // The resolver that defines this breakpoint.
778 BreakpointPreconditionSP m_precondition_sp; // The precondition is a breakpoint-level hit filter that can be used
779 // to skip certain breakpoint hits. For instance, exception breakpoints
780 // use this to limit the stop to certain exception classes, while leaving
781 // the condition & callback free for user specification.
782 BreakpointOptions m_options; // Settable breakpoint options
783 BreakpointLocationList m_locations; // The list of locations currently found for this breakpoint.
784 std::string m_kind_description;
785 bool m_resolve_indirect_symbols;
786 uint32_t m_hit_count; // Number of times this breakpoint/watchpoint has been hit. This is kept
787 // separately from the locations hit counts, since locations can go away when
788 // their backing library gets unloaded, and we would lose hit counts.
791 SendBreakpointChangedEvent (lldb::BreakpointEventType eventKind);
794 SendBreakpointChangedEvent (BreakpointEventData *data);
796 DISALLOW_COPY_AND_ASSIGN(Breakpoint);
799 } // namespace lldb_private
801 #endif // liblldb_Breakpoint_h_