1 //===-- BreakpointSite.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_BreakpointSite_h_
11 #define liblldb_BreakpointSite_h_
19 // Other libraries and framework includes
22 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
23 #include "lldb/Breakpoint/StoppointLocation.h"
24 #include "lldb/Utility/UserID.h"
25 #include "lldb/lldb-forward.h"
27 namespace lldb_private {
29 //----------------------------------------------------------------------
30 /// @class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
31 /// Class that manages the actual breakpoint that will be inserted into the
34 /// The BreakpointSite class handles the physical breakpoint that is actually
35 /// inserted in the target program. As such, it is also the one that gets
36 /// hit, when the program stops. It keeps a list of all BreakpointLocations
37 /// that share this physical site. When the breakpoint is hit, all the
38 /// locations are informed by the breakpoint site. Breakpoint sites are owned
40 //----------------------------------------------------------------------
42 class BreakpointSite : public std::enable_shared_from_this<BreakpointSite>,
43 public StoppointLocation {
46 eSoftware, // Breakpoint opcode has been written to memory and
48 // and m_trap_opcode contain the saved and written opcode.
49 eHardware, // Breakpoint site is set as a hardware breakpoint
50 eExternal // Breakpoint site is managed by an external debug nub or
51 // debug interface where memory reads transparently will not
52 // display any breakpoint opcodes.
55 ~BreakpointSite() override;
57 //----------------------------------------------------------------------
58 // This section manages the breakpoint traps
59 //----------------------------------------------------------------------
61 //------------------------------------------------------------------
62 /// Returns the Opcode Bytes for this breakpoint
63 //------------------------------------------------------------------
64 uint8_t *GetTrapOpcodeBytes();
66 //------------------------------------------------------------------
67 /// Returns the Opcode Bytes for this breakpoint - const version
68 //------------------------------------------------------------------
69 const uint8_t *GetTrapOpcodeBytes() const;
71 //------------------------------------------------------------------
72 /// Get the size of the trap opcode for this address
73 //------------------------------------------------------------------
74 size_t GetTrapOpcodeMaxByteSize() const;
76 //------------------------------------------------------------------
77 /// Sets the trap opcode
78 //------------------------------------------------------------------
79 bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size);
81 //------------------------------------------------------------------
82 /// Gets the original instruction bytes that were overwritten by the trap
83 //------------------------------------------------------------------
84 uint8_t *GetSavedOpcodeBytes();
86 //------------------------------------------------------------------
87 /// Gets the original instruction bytes that were overwritten by the trap
89 //------------------------------------------------------------------
90 const uint8_t *GetSavedOpcodeBytes() const;
92 //------------------------------------------------------------------
93 /// Says whether \a addr and size \a size intersects with the address \a
95 //------------------------------------------------------------------
96 bool IntersectsRange(lldb::addr_t addr, size_t size,
97 lldb::addr_t *intersect_addr, size_t *intersect_size,
98 size_t *opcode_offset) const;
100 //------------------------------------------------------------------
101 /// Tells whether the current breakpoint site is enabled or not
103 /// This is a low-level enable bit for the breakpoint sites. If a
104 /// breakpoint site has no enabled owners, it should just get removed. This
105 /// enable/disable is for the low-level target code to enable and disable
106 /// breakpoint sites when single stepping, etc.
107 //------------------------------------------------------------------
108 bool IsEnabled() const;
110 //------------------------------------------------------------------
111 /// Sets whether the current breakpoint site is enabled or not
113 /// @param[in] enabled
114 /// \b true if the breakpoint is enabled, \b false otherwise.
115 //------------------------------------------------------------------
116 void SetEnabled(bool enabled);
118 //------------------------------------------------------------------
119 /// Enquires of the breakpoint locations that produced this breakpoint site
120 /// whether we should stop at this location.
122 /// @param[in] context
123 /// This contains the information about this stop.
126 /// \b true if we should stop, \b false otherwise.
127 //------------------------------------------------------------------
128 bool ShouldStop(StoppointCallbackContext *context) override;
130 //------------------------------------------------------------------
131 /// Standard Dump method
133 /// @param[in] context
134 /// The stream to dump this output.
135 //------------------------------------------------------------------
136 void Dump(Stream *s) const override;
138 //------------------------------------------------------------------
139 /// The "Owners" are the breakpoint locations that share this breakpoint
140 /// site. The method adds the \a owner to this breakpoint site's owner list.
142 /// @param[in] context
143 /// \a owner is the Breakpoint Location to add.
144 //------------------------------------------------------------------
145 void AddOwner(const lldb::BreakpointLocationSP &owner);
147 //------------------------------------------------------------------
148 /// This method returns the number of breakpoint locations currently located
149 /// at this breakpoint site.
152 /// The number of owners.
153 //------------------------------------------------------------------
154 size_t GetNumberOfOwners();
156 //------------------------------------------------------------------
157 /// This method returns the breakpoint location at index \a index located at
158 /// this breakpoint site. The owners are listed ordinally from 0 to
159 /// GetNumberOfOwners() - 1 so you can use this method to iterate over the
163 /// The index in the list of owners for which you wish the owner location.
165 /// A shared pointer to the breakpoint location at that index.
166 //------------------------------------------------------------------
167 lldb::BreakpointLocationSP GetOwnerAtIndex(size_t idx);
169 //------------------------------------------------------------------
170 /// This method copies the breakpoint site's owners into a new collection.
171 /// It does this while the owners mutex is locked.
173 /// @param[out] out_collection
174 /// The BreakpointLocationCollection into which to put the owners
175 /// of this breakpoint site.
178 /// The number of elements copied into out_collection.
179 //------------------------------------------------------------------
180 size_t CopyOwnersList(BreakpointLocationCollection &out_collection);
182 //------------------------------------------------------------------
183 /// Check whether the owners of this breakpoint site have any thread
184 /// specifiers, and if yes, is \a thread contained in any of these
187 /// @param[in] thread
188 /// The thread against which to test.
191 /// \b true if the collection contains at least one location that
192 /// would be valid for this thread, false otherwise.
193 //------------------------------------------------------------------
194 bool ValidForThisThread(Thread *thread);
196 //------------------------------------------------------------------
197 /// Print a description of this breakpoint site to the stream \a s.
198 /// GetDescription tells you about the breakpoint site's owners. Use
199 /// BreakpointSite::Dump(Stream *) to get information about the breakpoint
203 /// The stream to which to print the description.
206 /// The description level that indicates the detail level to
209 /// @see lldb::DescriptionLevel
210 //------------------------------------------------------------------
211 void GetDescription(Stream *s, lldb::DescriptionLevel level);
213 //------------------------------------------------------------------
214 /// Tell whether a breakpoint has a location at this site.
217 /// The breakpoint id to query.
220 /// \b true if bp_id has a location that is at this site,
221 /// \b false otherwise.
222 //------------------------------------------------------------------
223 bool IsBreakpointAtThisSite(lldb::break_id_t bp_id);
225 //------------------------------------------------------------------
226 /// Tell whether ALL the breakpoints in the location collection are
230 /// \b true if all breakpoint locations are owned by internal breakpoints,
231 /// \b false otherwise.
232 //------------------------------------------------------------------
233 bool IsInternal() const;
235 BreakpointSite::Type GetType() const { return m_type; }
237 void SetType(BreakpointSite::Type type) { m_type = type; }
240 friend class Process;
241 friend class BreakpointLocation;
242 // The StopInfoBreakpoint knows when it is processing a hit for a thread for
243 // a site, so let it be the one to manage setting the location hit count once
245 friend class StopInfoBreakpoint;
247 void BumpHitCounts();
249 //------------------------------------------------------------------
250 /// The method removes the owner at \a break_loc_id from this breakpoint
253 /// @param[in] context
254 /// \a break_loc_id is the Breakpoint Location to remove.
255 //------------------------------------------------------------------
256 size_t RemoveOwner(lldb::break_id_t break_id, lldb::break_id_t break_loc_id);
258 BreakpointSite::Type m_type; ///< The type of this breakpoint site.
259 uint8_t m_saved_opcode[8]; ///< The saved opcode bytes if this breakpoint site
260 ///uses trap opcodes.
261 uint8_t m_trap_opcode[8]; ///< The opcode that was used to create the
262 ///breakpoint if it is a software breakpoint site.
264 m_enabled; ///< Boolean indicating if this breakpoint site enabled or not.
266 // Consider adding an optimization where if there is only one owner, we don't
267 // store a list. The usual case will be only one owner...
268 BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations
269 ///that share this breakpoint site.
271 m_owners_mutex; ///< This mutex protects the owners collection.
273 static lldb::break_id_t GetNextID();
275 // Only the Process can create breakpoint sites in
276 // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
277 BreakpointSite(BreakpointSiteList *list,
278 const lldb::BreakpointLocationSP &owner, lldb::addr_t m_addr,
281 DISALLOW_COPY_AND_ASSIGN(BreakpointSite);
284 } // namespace lldb_private
286 #endif // liblldb_BreakpointSite_h_