BreakpointSite.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //===-- BreakpointSite.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. #ifndef LLDB_BREAKPOINT_BREAKPOINTSITE_H
  9. #define LLDB_BREAKPOINT_BREAKPOINTSITE_H
  10. #include <list>
  11. #include <mutex>
  12. #include "lldb/Breakpoint/BreakpointLocationCollection.h"
  13. #include "lldb/Breakpoint/StoppointSite.h"
  14. #include "lldb/Utility/LLDBAssert.h"
  15. #include "lldb/Utility/UserID.h"
  16. #include "lldb/lldb-forward.h"
  17. namespace lldb_private {
  18. /// \class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
  19. /// Class that manages the actual breakpoint that will be inserted into the
  20. /// running program.
  21. ///
  22. /// The BreakpointSite class handles the physical breakpoint that is actually
  23. /// inserted in the target program. As such, it is also the one that gets
  24. /// hit, when the program stops. It keeps a list of all BreakpointLocations
  25. /// that share this physical site. When the breakpoint is hit, all the
  26. /// locations are informed by the breakpoint site. Breakpoint sites are owned
  27. /// by the process.
  28. class BreakpointSite : public std::enable_shared_from_this<BreakpointSite>,
  29. public StoppointSite {
  30. public:
  31. enum Type {
  32. eSoftware, // Breakpoint opcode has been written to memory and
  33. // m_saved_opcode
  34. // and m_trap_opcode contain the saved and written opcode.
  35. eHardware, // Breakpoint site is set as a hardware breakpoint
  36. eExternal // Breakpoint site is managed by an external debug nub or
  37. // debug interface where memory reads transparently will not
  38. // display any breakpoint opcodes.
  39. };
  40. ~BreakpointSite() override;
  41. // This section manages the breakpoint traps
  42. /// Returns the Opcode Bytes for this breakpoint
  43. uint8_t *GetTrapOpcodeBytes();
  44. /// Returns the Opcode Bytes for this breakpoint - const version
  45. const uint8_t *GetTrapOpcodeBytes() const;
  46. /// Get the size of the trap opcode for this address
  47. size_t GetTrapOpcodeMaxByteSize() const;
  48. /// Sets the trap opcode
  49. bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size);
  50. /// Gets the original instruction bytes that were overwritten by the trap
  51. uint8_t *GetSavedOpcodeBytes();
  52. /// Gets the original instruction bytes that were overwritten by the trap
  53. /// const version
  54. const uint8_t *GetSavedOpcodeBytes() const;
  55. /// Says whether \a addr and size \a size intersects with the address \a
  56. /// intersect_addr
  57. bool IntersectsRange(lldb::addr_t addr, size_t size,
  58. lldb::addr_t *intersect_addr, size_t *intersect_size,
  59. size_t *opcode_offset) const;
  60. /// Tells whether the current breakpoint site is enabled or not
  61. ///
  62. /// This is a low-level enable bit for the breakpoint sites. If a
  63. /// breakpoint site has no enabled owners, it should just get removed. This
  64. /// enable/disable is for the low-level target code to enable and disable
  65. /// breakpoint sites when single stepping, etc.
  66. bool IsEnabled() const;
  67. /// Sets whether the current breakpoint site is enabled or not
  68. ///
  69. /// \param[in] enabled
  70. /// \b true if the breakpoint is enabled, \b false otherwise.
  71. void SetEnabled(bool enabled);
  72. /// Enquires of the breakpoint locations that produced this breakpoint site
  73. /// whether we should stop at this location.
  74. ///
  75. /// \param[in] context
  76. /// This contains the information about this stop.
  77. ///
  78. /// \return
  79. /// \b true if we should stop, \b false otherwise.
  80. bool ShouldStop(StoppointCallbackContext *context) override;
  81. /// Standard Dump method
  82. void Dump(Stream *s) const override;
  83. /// The "Owners" are the breakpoint locations that share this breakpoint
  84. /// site. The method adds the \a owner to this breakpoint site's owner list.
  85. ///
  86. /// \param[in] owner
  87. /// \a owner is the Breakpoint Location to add.
  88. void AddOwner(const lldb::BreakpointLocationSP &owner);
  89. /// This method returns the number of breakpoint locations currently located
  90. /// at this breakpoint site.
  91. ///
  92. /// \return
  93. /// The number of owners.
  94. size_t GetNumberOfOwners();
  95. /// This method returns the breakpoint location at index \a index located at
  96. /// this breakpoint site. The owners are listed ordinally from 0 to
  97. /// GetNumberOfOwners() - 1 so you can use this method to iterate over the
  98. /// owners
  99. ///
  100. /// \param[in] idx
  101. /// The index in the list of owners for which you wish the owner location.
  102. ///
  103. /// \return
  104. /// A shared pointer to the breakpoint location at that index.
  105. lldb::BreakpointLocationSP GetOwnerAtIndex(size_t idx);
  106. /// This method copies the breakpoint site's owners into a new collection.
  107. /// It does this while the owners mutex is locked.
  108. ///
  109. /// \param[out] out_collection
  110. /// The BreakpointLocationCollection into which to put the owners
  111. /// of this breakpoint site.
  112. ///
  113. /// \return
  114. /// The number of elements copied into out_collection.
  115. size_t CopyOwnersList(BreakpointLocationCollection &out_collection);
  116. /// Check whether the owners of this breakpoint site have any thread
  117. /// specifiers, and if yes, is \a thread contained in any of these
  118. /// specifiers.
  119. ///
  120. /// \param[in] thread
  121. /// The thread against which to test.
  122. ///
  123. /// return
  124. /// \b true if the collection contains at least one location that
  125. /// would be valid for this thread, false otherwise.
  126. bool ValidForThisThread(Thread *thread);
  127. /// Print a description of this breakpoint site to the stream \a s.
  128. /// GetDescription tells you about the breakpoint site's owners. Use
  129. /// BreakpointSite::Dump(Stream *) to get information about the breakpoint
  130. /// site itself.
  131. ///
  132. /// \param[in] s
  133. /// The stream to which to print the description.
  134. ///
  135. /// \param[in] level
  136. /// The description level that indicates the detail level to
  137. /// provide.
  138. ///
  139. /// \see lldb::DescriptionLevel
  140. void GetDescription(Stream *s, lldb::DescriptionLevel level);
  141. /// Tell whether a breakpoint has a location at this site.
  142. ///
  143. /// \param[in] bp_id
  144. /// The breakpoint id to query.
  145. ///
  146. /// \result
  147. /// \b true if bp_id has a location that is at this site,
  148. /// \b false otherwise.
  149. bool IsBreakpointAtThisSite(lldb::break_id_t bp_id);
  150. /// Tell whether ALL the breakpoints in the location collection are
  151. /// internal.
  152. ///
  153. /// \result
  154. /// \b true if all breakpoint locations are owned by internal breakpoints,
  155. /// \b false otherwise.
  156. bool IsInternal() const;
  157. bool IsHardware() const override {
  158. lldbassert(BreakpointSite::Type::eHardware == GetType() ||
  159. !HardwareRequired());
  160. return BreakpointSite::Type::eHardware == GetType();
  161. }
  162. BreakpointSite::Type GetType() const { return m_type; }
  163. void SetType(BreakpointSite::Type type) { m_type = type; }
  164. private:
  165. friend class Process;
  166. friend class BreakpointLocation;
  167. // The StopInfoBreakpoint knows when it is processing a hit for a thread for
  168. // a site, so let it be the one to manage setting the location hit count once
  169. // and only once.
  170. friend class StopInfoBreakpoint;
  171. void BumpHitCounts();
  172. /// The method removes the owner at \a break_loc_id from this breakpoint
  173. /// list.
  174. size_t RemoveOwner(lldb::break_id_t break_id, lldb::break_id_t break_loc_id);
  175. BreakpointSite::Type m_type; ///< The type of this breakpoint site.
  176. uint8_t m_saved_opcode[8]; ///< The saved opcode bytes if this breakpoint site
  177. ///uses trap opcodes.
  178. uint8_t m_trap_opcode[8]; ///< The opcode that was used to create the
  179. ///breakpoint if it is a software breakpoint site.
  180. bool
  181. m_enabled; ///< Boolean indicating if this breakpoint site enabled or not.
  182. // Consider adding an optimization where if there is only one owner, we don't
  183. // store a list. The usual case will be only one owner...
  184. BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations
  185. ///that share this breakpoint site.
  186. std::recursive_mutex
  187. m_owners_mutex; ///< This mutex protects the owners collection.
  188. static lldb::break_id_t GetNextID();
  189. // Only the Process can create breakpoint sites in
  190. // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
  191. BreakpointSite(BreakpointSiteList *list,
  192. const lldb::BreakpointLocationSP &owner, lldb::addr_t m_addr,
  193. bool use_hardware);
  194. BreakpointSite(const BreakpointSite &) = delete;
  195. const BreakpointSite &operator=(const BreakpointSite &) = delete;
  196. };
  197. } // namespace lldb_private
  198. #endif // LLDB_BREAKPOINT_BREAKPOINTSITE_H