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_
18 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
19 #include "lldb/Breakpoint/StoppointLocation.h"
20 #include "lldb/Utility/UserID.h"
21 #include "lldb/lldb-forward.h"
23 namespace lldb_private {
25 //----------------------------------------------------------------------
26 /// @class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
27 /// Class that manages the actual breakpoint that will be inserted into the
30 /// The BreakpointSite class handles the physical breakpoint that is actually
31 /// inserted in the target program. As such, it is also the one that gets
32 /// hit, when the program stops. It keeps a list of all BreakpointLocations
33 /// that share this physical site. When the breakpoint is hit, all the
34 /// locations are informed by the breakpoint site. Breakpoint sites are owned
36 //----------------------------------------------------------------------
38 class BreakpointSite : public std::enable_shared_from_this<BreakpointSite>,
39 public StoppointLocation {
42 eSoftware, // Breakpoint opcode has been written to memory and
44 // and m_trap_opcode contain the saved and written opcode.
45 eHardware, // Breakpoint site is set as a hardware breakpoint
46 eExternal // Breakpoint site is managed by an external debug nub or
47 // debug interface where memory reads transparently will not
48 // display any breakpoint opcodes.
51 ~BreakpointSite() override;
53 //----------------------------------------------------------------------
54 // This section manages the breakpoint traps
55 //----------------------------------------------------------------------
57 //------------------------------------------------------------------
58 /// Returns the Opcode Bytes for this breakpoint
59 //------------------------------------------------------------------
60 uint8_t *GetTrapOpcodeBytes();
62 //------------------------------------------------------------------
63 /// Returns the Opcode Bytes for this breakpoint - const version
64 //------------------------------------------------------------------
65 const uint8_t *GetTrapOpcodeBytes() const;
67 //------------------------------------------------------------------
68 /// Get the size of the trap opcode for this address
69 //------------------------------------------------------------------
70 size_t GetTrapOpcodeMaxByteSize() const;
72 //------------------------------------------------------------------
73 /// Sets the trap opcode
74 //------------------------------------------------------------------
75 bool SetTrapOpcode(const uint8_t *trap_opcode, uint32_t trap_opcode_size);
77 //------------------------------------------------------------------
78 /// Gets the original instruction bytes that were overwritten by the trap
79 //------------------------------------------------------------------
80 uint8_t *GetSavedOpcodeBytes();
82 //------------------------------------------------------------------
83 /// Gets the original instruction bytes that were overwritten by the trap
85 //------------------------------------------------------------------
86 const uint8_t *GetSavedOpcodeBytes() const;
88 //------------------------------------------------------------------
89 /// Says whether \a addr and size \a size intersects with the address \a
91 //------------------------------------------------------------------
92 bool IntersectsRange(lldb::addr_t addr, size_t size,
93 lldb::addr_t *intersect_addr, size_t *intersect_size,
94 size_t *opcode_offset) const;
96 //------------------------------------------------------------------
97 /// Tells whether the current breakpoint site is enabled or not
99 /// This is a low-level enable bit for the breakpoint sites. If a
100 /// breakpoint site has no enabled owners, it should just get removed. This
101 /// enable/disable is for the low-level target code to enable and disable
102 /// breakpoint sites when single stepping, etc.
103 //------------------------------------------------------------------
104 bool IsEnabled() const;
106 //------------------------------------------------------------------
107 /// Sets whether the current breakpoint site is enabled or not
109 /// @param[in] enabled
110 /// \b true if the breakpoint is enabled, \b false otherwise.
111 //------------------------------------------------------------------
112 void SetEnabled(bool enabled);
114 //------------------------------------------------------------------
115 /// Enquires of the breakpoint locations that produced this breakpoint site
116 /// whether we should stop at this location.
118 /// @param[in] context
119 /// This contains the information about this stop.
122 /// \b true if we should stop, \b false otherwise.
123 //------------------------------------------------------------------
124 bool ShouldStop(StoppointCallbackContext *context) override;
126 //------------------------------------------------------------------
127 /// Standard Dump method
129 /// @param[in] context
130 /// The stream to dump this output.
131 //------------------------------------------------------------------
132 void Dump(Stream *s) const override;
134 //------------------------------------------------------------------
135 /// The "Owners" are the breakpoint locations that share this breakpoint
136 /// site. The method adds the \a owner to this breakpoint site's owner list.
138 /// @param[in] context
139 /// \a owner is the Breakpoint Location to add.
140 //------------------------------------------------------------------
141 void AddOwner(const lldb::BreakpointLocationSP &owner);
143 //------------------------------------------------------------------
144 /// This method returns the number of breakpoint locations currently located
145 /// at this breakpoint site.
148 /// The number of owners.
149 //------------------------------------------------------------------
150 size_t GetNumberOfOwners();
152 //------------------------------------------------------------------
153 /// This method returns the breakpoint location at index \a index located at
154 /// this breakpoint site. The owners are listed ordinally from 0 to
155 /// GetNumberOfOwners() - 1 so you can use this method to iterate over the
159 /// The index in the list of owners for which you wish the owner location.
161 /// A shared pointer to the breakpoint location at that index.
162 //------------------------------------------------------------------
163 lldb::BreakpointLocationSP GetOwnerAtIndex(size_t idx);
165 //------------------------------------------------------------------
166 /// This method copies the breakpoint site's owners into a new collection.
167 /// It does this while the owners mutex is locked.
169 /// @param[out] out_collection
170 /// The BreakpointLocationCollection into which to put the owners
171 /// of this breakpoint site.
174 /// The number of elements copied into out_collection.
175 //------------------------------------------------------------------
176 size_t CopyOwnersList(BreakpointLocationCollection &out_collection);
178 //------------------------------------------------------------------
179 /// Check whether the owners of this breakpoint site have any thread
180 /// specifiers, and if yes, is \a thread contained in any of these
183 /// @param[in] thread
184 /// The thread against which to test.
187 /// \b true if the collection contains at least one location that
188 /// would be valid for this thread, false otherwise.
189 //------------------------------------------------------------------
190 bool ValidForThisThread(Thread *thread);
192 //------------------------------------------------------------------
193 /// Print a description of this breakpoint site to the stream \a s.
194 /// GetDescription tells you about the breakpoint site's owners. Use
195 /// BreakpointSite::Dump(Stream *) to get information about the breakpoint
199 /// The stream to which to print the description.
202 /// The description level that indicates the detail level to
205 /// @see lldb::DescriptionLevel
206 //------------------------------------------------------------------
207 void GetDescription(Stream *s, lldb::DescriptionLevel level);
209 //------------------------------------------------------------------
210 /// Tell whether a breakpoint has a location at this site.
213 /// The breakpoint id to query.
216 /// \b true if bp_id has a location that is at this site,
217 /// \b false otherwise.
218 //------------------------------------------------------------------
219 bool IsBreakpointAtThisSite(lldb::break_id_t bp_id);
221 //------------------------------------------------------------------
222 /// Tell whether ALL the breakpoints in the location collection are
226 /// \b true if all breakpoint locations are owned by internal breakpoints,
227 /// \b false otherwise.
228 //------------------------------------------------------------------
229 bool IsInternal() const;
231 BreakpointSite::Type GetType() const { return m_type; }
233 void SetType(BreakpointSite::Type type) { m_type = type; }
236 friend class Process;
237 friend class BreakpointLocation;
238 // The StopInfoBreakpoint knows when it is processing a hit for a thread for
239 // a site, so let it be the one to manage setting the location hit count once
241 friend class StopInfoBreakpoint;
243 void BumpHitCounts();
245 //------------------------------------------------------------------
246 /// The method removes the owner at \a break_loc_id from this breakpoint
249 /// @param[in] context
250 /// \a break_loc_id is the Breakpoint Location to remove.
251 //------------------------------------------------------------------
252 size_t RemoveOwner(lldb::break_id_t break_id, lldb::break_id_t break_loc_id);
254 BreakpointSite::Type m_type; ///< The type of this breakpoint site.
255 uint8_t m_saved_opcode[8]; ///< The saved opcode bytes if this breakpoint site
256 ///uses trap opcodes.
257 uint8_t m_trap_opcode[8]; ///< The opcode that was used to create the
258 ///breakpoint if it is a software breakpoint site.
260 m_enabled; ///< Boolean indicating if this breakpoint site enabled or not.
262 // Consider adding an optimization where if there is only one owner, we don't
263 // store a list. The usual case will be only one owner...
264 BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations
265 ///that share this breakpoint site.
267 m_owners_mutex; ///< This mutex protects the owners collection.
269 static lldb::break_id_t GetNextID();
271 // Only the Process can create breakpoint sites in
272 // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
273 BreakpointSite(BreakpointSiteList *list,
274 const lldb::BreakpointLocationSP &owner, lldb::addr_t m_addr,
277 DISALLOW_COPY_AND_ASSIGN(BreakpointSite);
280 } // namespace lldb_private
282 #endif // liblldb_BreakpointSite_h_