Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===-- BreakpointSite.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_BREAKPOINTSITE_H
0010 #define LLDB_BREAKPOINT_BREAKPOINTSITE_H
0011 
0012 #include <list>
0013 #include <mutex>
0014 
0015 
0016 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
0017 #include "lldb/Breakpoint/StoppointSite.h"
0018 #include "lldb/Utility/LLDBAssert.h"
0019 #include "lldb/Utility/UserID.h"
0020 #include "lldb/lldb-forward.h"
0021 
0022 namespace lldb_private {
0023 
0024 /// \class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
0025 /// Class that manages the actual breakpoint that will be inserted into the
0026 /// running program.
0027 ///
0028 /// The BreakpointSite class handles the physical breakpoint that is actually
0029 /// inserted in the target program.  As such, it is also the one that  gets
0030 /// hit, when the program stops. It keeps a list of all BreakpointLocations
0031 /// that share this physical site. When the breakpoint is hit, all the
0032 /// locations are informed by the breakpoint site. Breakpoint sites are owned
0033 /// by the process.
0034 
0035 class BreakpointSite : public std::enable_shared_from_this<BreakpointSite>,
0036                        public StoppointSite {
0037 public:
0038   enum Type {
0039     eSoftware, // Breakpoint opcode has been written to memory and
0040                // m_saved_opcode
0041                // and m_trap_opcode contain the saved and written opcode.
0042     eHardware, // Breakpoint site is set as a hardware breakpoint
0043     eExternal  // Breakpoint site is managed by an external debug nub or
0044                // debug interface where memory reads transparently will not
0045                // display any breakpoint opcodes.
0046   };
0047 
0048   typedef lldb::break_id_t SiteID;
0049   typedef lldb::break_id_t ConstituentID;
0050 
0051   ~BreakpointSite() override;
0052 
0053   // This section manages the breakpoint traps
0054 
0055   /// Returns the Opcode Bytes for this breakpoint
0056   uint8_t *GetTrapOpcodeBytes();
0057 
0058   /// Returns the Opcode Bytes for this breakpoint - const version
0059   const uint8_t *GetTrapOpcodeBytes() const;
0060 
0061   /// Get the size of the trap opcode for this address
0062   size_t GetTrapOpcodeMaxByteSize() const;
0063 
0064   /// Sets the trap opcode
0065   bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size);
0066 
0067   /// Gets the original instruction bytes that were overwritten by the trap
0068   uint8_t *GetSavedOpcodeBytes();
0069 
0070   /// Gets the original instruction bytes that were overwritten by the trap
0071   /// const version
0072   const uint8_t *GetSavedOpcodeBytes() const;
0073 
0074   /// Says whether \a addr and size \a size intersects with the address \a
0075   /// intersect_addr
0076   bool IntersectsRange(lldb::addr_t addr, size_t size,
0077                        lldb::addr_t *intersect_addr, size_t *intersect_size,
0078                        size_t *opcode_offset) const;
0079 
0080   /// Tells whether the current breakpoint site is enabled or not
0081   ///
0082   /// This is a low-level enable bit for the breakpoint sites.  If a
0083   /// breakpoint site has no enabled constituents, it should just get removed.
0084   /// This enable/disable is for the low-level target code to enable and disable
0085   /// breakpoint sites when single stepping, etc.
0086   bool IsEnabled() const;
0087 
0088   /// Sets whether the current breakpoint site is enabled or not
0089   ///
0090   /// \param[in] enabled
0091   ///    \b true if the breakpoint is enabled, \b false otherwise.
0092   void SetEnabled(bool enabled);
0093 
0094   /// Enquires of the breakpoint locations that produced this breakpoint site
0095   /// whether we should stop at this location.
0096   ///
0097   /// \param[in] context
0098   ///    This contains the information about this stop.
0099   ///
0100   /// \return
0101   ///    \b true if we should stop, \b false otherwise.
0102   bool ShouldStop(StoppointCallbackContext *context) override;
0103 
0104   /// Standard Dump method
0105   void Dump(Stream *s) const override;
0106 
0107   /// The "Constituents" are the breakpoint locations that share this breakpoint
0108   /// site. The method adds the \a constituent to this breakpoint site's
0109   /// constituent list.
0110   ///
0111   /// \param[in] constituent
0112   ///    \a constituent is the Breakpoint Location to add.
0113   void AddConstituent(const lldb::BreakpointLocationSP &constituent);
0114 
0115   /// This method returns the number of breakpoint locations currently located
0116   /// at this breakpoint site.
0117   ///
0118   /// \return
0119   ///    The number of constituents.
0120   size_t GetNumberOfConstituents();
0121 
0122   /// This method returns the breakpoint location at index \a index located at
0123   /// this breakpoint site.  The constituents are listed ordinally from 0 to
0124   /// GetNumberOfConstituents() - 1 so you can use this method to iterate over
0125   /// the constituents
0126   ///
0127   /// \param[in] idx
0128   ///     The index in the list of constituents for which you wish the
0129   ///     constituent location.
0130   ///
0131   /// \return
0132   ///    A shared pointer to the breakpoint location at that index.
0133   lldb::BreakpointLocationSP GetConstituentAtIndex(size_t idx);
0134 
0135   /// This method copies the breakpoint site's constituents into a new
0136   /// collection. It does this while the constituents mutex is locked.
0137   ///
0138   /// \param[out] out_collection
0139   ///    The BreakpointLocationCollection into which to put the constituents
0140   ///    of this breakpoint site.
0141   ///
0142   /// \return
0143   ///    The number of elements copied into out_collection.
0144   size_t CopyConstituentsList(BreakpointLocationCollection &out_collection);
0145 
0146   /// Check whether the constituents of this breakpoint site have any thread
0147   /// specifiers, and if yes, is \a thread contained in any of these
0148   /// specifiers.
0149   ///
0150   /// \param[in] thread
0151   ///     The thread against which to test.
0152   ///
0153   /// return
0154   ///     \b true if the collection contains at least one location that
0155   ///     would be valid for this thread, false otherwise.
0156   bool ValidForThisThread(Thread &thread);
0157 
0158   /// Print a description of this breakpoint site to the stream \a s.
0159   /// GetDescription tells you about the breakpoint site's constituents. Use
0160   /// BreakpointSite::Dump(Stream *) to get information about the breakpoint
0161   /// site itself.
0162   ///
0163   /// \param[in] s
0164   ///     The stream to which to print the description.
0165   ///
0166   /// \param[in] level
0167   ///     The description level that indicates the detail level to
0168   ///     provide.
0169   ///
0170   /// \see lldb::DescriptionLevel
0171   void GetDescription(Stream *s, lldb::DescriptionLevel level);
0172 
0173   // This runs through all the breakpoint locations owning this site and returns
0174   // the greatest of their suggested stack frame indexes.  This only handles
0175   // inlined stack changes.
0176   std::optional<uint32_t> GetSuggestedStackFrameIndex();
0177 
0178   /// Tell whether a breakpoint has a location at this site.
0179   ///
0180   /// \param[in] bp_id
0181   ///     The breakpoint id to query.
0182   ///
0183   /// \result
0184   ///     \b true if bp_id has a location that is at this site,
0185   ///     \b false otherwise.
0186   bool IsBreakpointAtThisSite(lldb::break_id_t bp_id);
0187 
0188   /// Tell whether ALL the breakpoints in the location collection are
0189   /// internal.
0190   ///
0191   /// \result
0192   ///     \b true if all breakpoint locations are owned by internal breakpoints,
0193   ///     \b false otherwise.
0194   bool IsInternal() const;
0195 
0196   bool IsHardware() const override {
0197     lldbassert(BreakpointSite::Type::eHardware == GetType() ||
0198                !HardwareRequired());
0199     return BreakpointSite::Type::eHardware == GetType();
0200   }
0201 
0202   BreakpointSite::Type GetType() const { return m_type; }
0203 
0204   void SetType(BreakpointSite::Type type) { m_type = type; }
0205 
0206 private:
0207   friend class Process;
0208   friend class BreakpointLocation;
0209   // The StopInfoBreakpoint knows when it is processing a hit for a thread for
0210   // a site, so let it be the one to manage setting the location hit count once
0211   // and only once.
0212   friend class StopInfoBreakpoint;
0213 
0214   void BumpHitCounts();
0215 
0216   /// The method removes the constituent at \a break_loc_id from this breakpoint
0217   /// list.
0218   size_t RemoveConstituent(lldb::break_id_t break_id,
0219                            lldb::break_id_t break_loc_id);
0220 
0221   BreakpointSite::Type m_type; ///< The type of this breakpoint site.
0222   uint8_t m_saved_opcode[8]; ///< The saved opcode bytes if this breakpoint site
0223                              ///uses trap opcodes.
0224   uint8_t m_trap_opcode[8];  ///< The opcode that was used to create the
0225                              ///breakpoint if it is a software breakpoint site.
0226   bool
0227       m_enabled; ///< Boolean indicating if this breakpoint site enabled or not.
0228 
0229   // Consider adding an optimization where if there is only one constituent, we
0230   // don't store a list.  The usual case will be only one constituent...
0231   BreakpointLocationCollection
0232       m_constituents; ///< This has the BreakpointLocations
0233                       /// that share this breakpoint site.
0234   std::recursive_mutex m_constituents_mutex; ///< This mutex protects the
0235                                              ///< constituents collection.
0236 
0237   static lldb::break_id_t GetNextID();
0238 
0239   // Only the Process can create breakpoint sites in
0240   // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
0241   BreakpointSite(const lldb::BreakpointLocationSP &constituent,
0242                  lldb::addr_t m_addr, bool use_hardware);
0243 
0244   BreakpointSite(const BreakpointSite &) = delete;
0245   const BreakpointSite &operator=(const BreakpointSite &) = delete;
0246 };
0247 
0248 } // namespace lldb_private
0249 
0250 #endif // LLDB_BREAKPOINT_BREAKPOINTSITE_H