Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:43

0001 //===-- BreakpointLocation.h ------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
0010 #define LLDB_BREAKPOINT_BREAKPOINTLOCATION_H
0011 
0012 #include <memory>
0013 #include <mutex>
0014 #include <optional>
0015 
0016 #include "lldb/Breakpoint/BreakpointOptions.h"
0017 #include "lldb/Breakpoint/StoppointHitCounter.h"
0018 #include "lldb/Core/Address.h"
0019 #include "lldb/Symbol/LineEntry.h"
0020 #include "lldb/Utility/UserID.h"
0021 #include "lldb/lldb-private.h"
0022 
0023 namespace lldb_private {
0024 
0025 /// \class BreakpointLocation BreakpointLocation.h
0026 /// "lldb/Breakpoint/BreakpointLocation.h" Class that manages one unique (by
0027 /// address) instance of a logical breakpoint.
0028 
0029 /// General Outline:
0030 /// A breakpoint location is defined by the breakpoint that produces it,
0031 /// and the address that resulted in this particular instantiation. Each
0032 /// breakpoint location also may have a breakpoint site if its address has
0033 /// been loaded into the program. Finally it has a settable options object.
0034 ///
0035 /// FIXME: Should we also store some fingerprint for the location, so
0036 /// we can map one location to the "equivalent location" on rerun?  This would
0037 /// be useful if you've set options on the locations.
0038 
0039 class BreakpointLocation
0040     : public std::enable_shared_from_this<BreakpointLocation> {
0041 public:
0042   ~BreakpointLocation();
0043 
0044   /// Gets the load address for this breakpoint location \return
0045   ///     Returns breakpoint location load address, \b
0046   ///     LLDB_INVALID_ADDRESS if not yet set.
0047   lldb::addr_t GetLoadAddress() const;
0048 
0049   /// Gets the Address for this breakpoint location \return
0050   ///     Returns breakpoint location Address.
0051   Address &GetAddress();
0052   /// Gets the Breakpoint that created this breakpoint location \return
0053   ///     Returns the owning breakpoint.
0054   Breakpoint &GetBreakpoint();
0055 
0056   Target &GetTarget();
0057 
0058   /// Determines whether we should stop due to a hit at this breakpoint
0059   /// location.
0060   ///
0061   /// Side Effects: This may evaluate the breakpoint condition, and run the
0062   /// callback.  So this command may do a considerable amount of work.
0063   ///
0064   /// \return
0065   ///     \b true if this breakpoint location thinks we should stop,
0066   ///     \b false otherwise.
0067   bool ShouldStop(StoppointCallbackContext *context);
0068 
0069   // The next section deals with various breakpoint options.
0070 
0071   /// If \a enabled is \b true, enable the breakpoint, if \b false disable it.
0072   void SetEnabled(bool enabled);
0073 
0074   /// Check the Enable/Disable state.
0075   ///
0076   /// \return
0077   ///     \b true if the breakpoint is enabled, \b false if disabled.
0078   bool IsEnabled() const;
0079 
0080   /// If \a auto_continue is \b true, set the breakpoint to continue when hit.
0081   void SetAutoContinue(bool auto_continue);
0082 
0083   /// Check the AutoContinue state.
0084   ///
0085   /// \return
0086   ///     \b true if the breakpoint is set to auto-continue, \b false if not.
0087   bool IsAutoContinue() const;
0088 
0089   /// Return the current Hit Count.
0090   uint32_t GetHitCount() const { return m_hit_counter.GetValue(); }
0091 
0092   /// Resets the current Hit Count.
0093   void ResetHitCount() { m_hit_counter.Reset(); }
0094 
0095   /// Return the current Ignore Count.
0096   ///
0097   /// \return
0098   ///     The number of breakpoint hits to be ignored.
0099   uint32_t GetIgnoreCount() const;
0100 
0101   /// Set the breakpoint to ignore the next \a count breakpoint hits.
0102   ///
0103   /// \param[in] n
0104   ///    The number of breakpoint hits to ignore.
0105   void SetIgnoreCount(uint32_t n);
0106 
0107   /// Set the callback action invoked when the breakpoint is hit.
0108   ///
0109   /// The callback will return a bool indicating whether the target should
0110   /// stop at this breakpoint or not.
0111   ///
0112   /// \param[in] callback
0113   ///     The method that will get called when the breakpoint is hit.
0114   ///
0115   /// \param[in] callback_baton_sp
0116   ///     A shared pointer to a Baton that provides the void * needed
0117   ///     for the callback.
0118   ///
0119   /// \see lldb_private::Baton
0120   void SetCallback(BreakpointHitCallback callback,
0121                    const lldb::BatonSP &callback_baton_sp, bool is_synchronous);
0122 
0123   void SetCallback(BreakpointHitCallback callback, void *baton,
0124                    bool is_synchronous);
0125 
0126   void ClearCallback();
0127 
0128   /// Set the breakpoint location's condition.
0129   ///
0130   /// \param[in] condition
0131   ///    The condition expression to evaluate when the breakpoint is hit.
0132   void SetCondition(const char *condition);
0133 
0134   /// Return a pointer to the text of the condition expression.
0135   ///
0136   /// \return
0137   ///    A pointer to the condition expression text, or nullptr if no
0138   //     condition has been set.
0139   const char *GetConditionText(size_t *hash = nullptr) const;
0140 
0141   bool ConditionSaysStop(ExecutionContext &exe_ctx, Status &error);
0142 
0143   /// Set the valid thread to be checked when the breakpoint is hit.
0144   ///
0145   /// \param[in] thread_id
0146   ///    If this thread hits the breakpoint, we stop, otherwise not.
0147   void SetThreadID(lldb::tid_t thread_id);
0148 
0149   lldb::tid_t GetThreadID();
0150 
0151   void SetThreadIndex(uint32_t index);
0152 
0153   uint32_t GetThreadIndex() const;
0154 
0155   void SetThreadName(const char *thread_name);
0156 
0157   const char *GetThreadName() const;
0158 
0159   void SetQueueName(const char *queue_name);
0160 
0161   const char *GetQueueName() const;
0162 
0163   // The next section deals with this location's breakpoint sites.
0164 
0165   /// Try to resolve the breakpoint site for this location.
0166   ///
0167   /// \return
0168   ///     \b true if we were successful at setting a breakpoint site,
0169   ///     \b false otherwise.
0170   bool ResolveBreakpointSite();
0171 
0172   /// Clear this breakpoint location's breakpoint site - for instance when
0173   /// disabling the breakpoint.
0174   ///
0175   /// \return
0176   ///     \b true if there was a breakpoint site to be cleared, \b false
0177   ///     otherwise.
0178   bool ClearBreakpointSite();
0179 
0180   /// Return whether this breakpoint location has a breakpoint site. \return
0181   ///     \b true if there was a breakpoint site for this breakpoint
0182   ///     location, \b false otherwise.
0183   bool IsResolved() const;
0184 
0185   lldb::BreakpointSiteSP GetBreakpointSite() const;
0186 
0187   // The next section are generic report functions.
0188 
0189   /// Print a description of this breakpoint location to the stream \a s.
0190   ///
0191   /// \param[in] s
0192   ///     The stream to which to print the description.
0193   ///
0194   /// \param[in] level
0195   ///     The description level that indicates the detail level to
0196   ///     provide.
0197   ///
0198   /// \see lldb::DescriptionLevel
0199   void GetDescription(Stream *s, lldb::DescriptionLevel level);
0200 
0201   /// Standard "Dump" method.  At present it does nothing.
0202   void Dump(Stream *s) const;
0203 
0204   /// Use this to set location specific breakpoint options.
0205   ///
0206   /// It will create a copy of the containing breakpoint's options if that
0207   /// hasn't been done already
0208   ///
0209   /// \return
0210   ///    A reference to the breakpoint options.
0211   BreakpointOptions &GetLocationOptions();
0212 
0213   /// Use this to access breakpoint options from this breakpoint location.
0214   /// This will return the options that have a setting for the specified
0215   /// BreakpointOptions kind.
0216   ///
0217   /// \param[in] kind
0218   ///     The particular option you are looking up.
0219   /// \return
0220   ///     A pointer to the containing breakpoint's options if this
0221   ///     location doesn't have its own copy.
0222   const BreakpointOptions &
0223   GetOptionsSpecifyingKind(BreakpointOptions::OptionKind kind) const;
0224 
0225   bool ValidForThisThread(Thread &thread);
0226 
0227   /// Invoke the callback action when the breakpoint is hit.
0228   ///
0229   /// Meant to be used by the BreakpointLocation class.
0230   ///
0231   /// \param[in] context
0232   ///    Described the breakpoint event.
0233   ///
0234   /// \return
0235   ///     \b true if the target should stop at this breakpoint and \b
0236   ///     false not.
0237   bool InvokeCallback(StoppointCallbackContext *context);
0238   
0239   /// Report whether the callback for this location is synchronous or not.
0240   ///
0241   /// \return
0242   ///     \b true if the callback is synchronous and \b false if not.
0243   bool IsCallbackSynchronous();
0244 
0245   /// Returns whether we should resolve Indirect functions in setting the
0246   /// breakpoint site for this location.
0247   ///
0248   /// \return
0249   ///     \b true if the breakpoint SITE for this location should be set on the
0250   ///     resolved location for Indirect functions.
0251   bool ShouldResolveIndirectFunctions() {
0252     return m_should_resolve_indirect_functions;
0253   }
0254 
0255   /// Returns whether the address set in the breakpoint site for this location
0256   /// was found by resolving an indirect symbol.
0257   ///
0258   /// \return
0259   ///     \b true or \b false as given in the description above.
0260   bool IsIndirect() { return m_is_indirect; }
0261 
0262   void SetIsIndirect(bool is_indirect) { m_is_indirect = is_indirect; }
0263 
0264   /// Returns whether the address set in the breakpoint location was re-routed
0265   /// to the target of a re-exported symbol.
0266   ///
0267   /// \return
0268   ///     \b true or \b false as given in the description above.
0269   bool IsReExported() { return m_is_reexported; }
0270 
0271   void SetIsReExported(bool is_reexported) { m_is_reexported = is_reexported; }
0272 
0273   /// Returns whether the two breakpoint locations might represent "equivalent
0274   /// locations". This is used when modules changed to determine if a Location
0275   /// in the old module might be the "same as" the input location.
0276   ///
0277   /// \param[in] location
0278   ///    The location to compare against.
0279   ///
0280   /// \return
0281   ///     \b true or \b false as given in the description above.
0282   bool EquivalentToLocation(BreakpointLocation &location);
0283 
0284   /// Returns the breakpoint location ID.
0285   lldb::break_id_t GetID() const { return m_loc_id; }
0286 
0287   /// Set the line entry that should be shown to users for this location.
0288   /// It is up to the caller to verify that this is a valid entry to show.
0289   /// The current use of this is to distinguish among line entries from a
0290   /// virtual inlined call stack that all share the same address.
0291   /// The line entry must have the same start address as the address for this
0292   /// location.
0293   bool SetPreferredLineEntry(const LineEntry &line_entry) {
0294     if (m_address == line_entry.range.GetBaseAddress()) {
0295       m_preferred_line_entry = line_entry;
0296       return true;
0297     }
0298     assert(0 && "Tried to set a preferred line entry with a different address");
0299     return false;
0300   }
0301 
0302   const std::optional<LineEntry> GetPreferredLineEntry() {
0303     return m_preferred_line_entry;
0304   }
0305 
0306 protected:
0307   friend class BreakpointSite;
0308   friend class BreakpointLocationList;
0309   friend class Process;
0310   friend class StopInfoBreakpoint;
0311 
0312   /// Set the breakpoint site for this location to \a bp_site_sp.
0313   ///
0314   /// \param[in] bp_site_sp
0315   ///      The breakpoint site we are setting for this location.
0316   ///
0317   /// \return
0318   ///     \b true if we were successful at setting the breakpoint site,
0319   ///     \b false otherwise.
0320   bool SetBreakpointSite(lldb::BreakpointSiteSP &bp_site_sp);
0321 
0322   void DecrementIgnoreCount();
0323 
0324   /// BreakpointLocation::IgnoreCountShouldStop  can only be called once
0325   /// per stop.  This method checks first against the loc and then the owner.
0326   /// It also takes care of decrementing the ignore counters.
0327   /// If it returns false we should continue, otherwise stop.
0328   bool IgnoreCountShouldStop();
0329 
0330   /// If this location knows that the virtual stack frame it represents is
0331   /// not frame 0, return the suggested stack frame instead.  This will happen
0332   /// when the location's address contains a "virtual inlined call stack" and
0333   /// the breakpoint was set on a file & line that are not at the bottom of that
0334   /// stack.  For now we key off the "preferred line entry" - looking for that
0335   /// in the blocks that start with the stop PC.
0336   /// This version of the API doesn't take an "inlined" parameter because it
0337   /// only changes frames in the inline stack.
0338   std::optional<uint32_t> GetSuggestedStackFrameIndex();
0339 
0340 private:
0341   void SwapLocation(lldb::BreakpointLocationSP swap_from);
0342 
0343   void BumpHitCount();
0344 
0345   void UndoBumpHitCount();
0346 
0347   /// Updates the thread ID internally.
0348   ///
0349   /// This method was created to handle actually mutating the thread ID
0350   /// internally because SetThreadID broadcasts an event in addition to mutating
0351   /// state. The constructor calls this instead of SetThreadID to avoid the
0352   /// broadcast.
0353   ///
0354   /// \param[in] thread_id
0355   ///   The new thread ID.
0356   void SetThreadIDInternal(lldb::tid_t thread_id);
0357 
0358   // Constructors and Destructors
0359   //
0360   // Only the Breakpoint can make breakpoint locations, and it owns them.
0361 
0362   /// Constructor.
0363   ///
0364   /// \param[in] owner
0365   ///     A back pointer to the breakpoint that owns this location.
0366   ///
0367   /// \param[in] addr
0368   ///     The Address defining this location.
0369   ///
0370   /// \param[in] tid
0371   ///     The thread for which this breakpoint location is valid, or
0372   ///     LLDB_INVALID_THREAD_ID if it is valid for all threads.
0373   ///
0374   /// \param[in] hardware
0375   ///     \b true if a hardware breakpoint is requested.
0376 
0377   BreakpointLocation(lldb::break_id_t bid, Breakpoint &owner,
0378                      const Address &addr, lldb::tid_t tid, bool hardware,
0379                      bool check_for_resolver = true);
0380 
0381   // Data members:
0382   bool m_should_resolve_indirect_functions;
0383   bool m_is_reexported;
0384   bool m_is_indirect;
0385   Address m_address;   ///< The address defining this location.
0386   Breakpoint &m_owner; ///< The breakpoint that produced this object.
0387   std::unique_ptr<BreakpointOptions> m_options_up; ///< Breakpoint options
0388                                                    /// pointer, nullptr if we're
0389                                                    /// using our breakpoint's
0390                                                    /// options.
0391   lldb::BreakpointSiteSP m_bp_site_sp; ///< Our breakpoint site (it may be
0392                                        ///shared by more than one location.)
0393   lldb::UserExpressionSP m_user_expression_sp; ///< The compiled expression to
0394                                                ///use in testing our condition.
0395   std::mutex m_condition_mutex; ///< Guards parsing and evaluation of the
0396                                 ///condition, which could be evaluated by
0397                                 /// multiple processes.
0398   size_t m_condition_hash; ///< For testing whether the condition source code
0399                            ///changed.
0400   lldb::break_id_t m_loc_id; ///< Breakpoint location ID.
0401   StoppointHitCounter m_hit_counter; ///< Number of times this breakpoint
0402                                      /// location has been hit.
0403   /// If this exists, use it to print the stop description rather than the
0404   /// LineEntry m_address resolves to directly.  Use this for instance when the
0405   /// location was given somewhere in the virtual inlined call stack since the
0406   /// Address always resolves to the lowest entry in the stack.
0407   std::optional<LineEntry> m_preferred_line_entry;
0408 
0409   void SetShouldResolveIndirectFunctions(bool do_resolve) {
0410     m_should_resolve_indirect_functions = do_resolve;
0411   }
0412 
0413   void SendBreakpointLocationChangedEvent(lldb::BreakpointEventType eventKind);
0414 
0415   BreakpointLocation(const BreakpointLocation &) = delete;
0416   const BreakpointLocation &operator=(const BreakpointLocation &) = delete;
0417 };
0418 
0419 } // namespace lldb_private
0420 
0421 #endif // LLDB_BREAKPOINT_BREAKPOINTLOCATION_H