1 //===-- BreakpointSite.h ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_BreakpointSite_h_
11 #define liblldb_BreakpointSite_h_
12 
13 // C Includes
14 
15 // C++ Includes
16 #include <list>
17 
18 // Other libraries and framework includes
19 
20 // Project includes
21 #include "lldb/lldb-private.h"
22 #include "lldb/Core/UserID.h"
23 #include "lldb/Breakpoint/StoppointLocation.h"
24 #include "lldb/Breakpoint/BreakpointLocationCollection.h"
25 
26 namespace lldb_private {
27 
28 //----------------------------------------------------------------------
29 /// @class BreakpointSite BreakpointSite.h "lldb/Breakpoint/BreakpointSite.h"
30 /// @brief Class that manages the actual breakpoint that will be inserted
31 /// into the running program.
32 ///
33 /// The BreakpointSite class handles the physical breakpoint that is
34 /// actually inserted in the target program.  As such, it is also the
35 /// one that  gets hit, when the program stops. It keeps a list of all
36 /// BreakpointLocations that share this phsyical site. When the
37 /// breakpoint is hit, all the locations are informed by the breakpoint
38 /// site. Breakpoint sites are owned by the process.
39 //----------------------------------------------------------------------
40 
41 class BreakpointSite :
42     public std::enable_shared_from_this<BreakpointSite>,
43     public StoppointLocation
44 {
45 public:
46 
47     enum Type
48     {
49         eSoftware,  // Breakpoint opcode has been written to memory and m_saved_opcode
50                     // and m_trap_opcode contain the saved and written opcode.
51         eHardware,  // Breakpoint site is set as a hardware breakpoint
52         eExternal   // Breakpoint site is managed by an external debug nub or
53                     // debug interface where memory reads trasparently will not
54                     // display any breakpoint opcodes.
55     };
56 
57     virtual ~BreakpointSite ();
58 
59     //----------------------------------------------------------------------
60     // This section manages the breakpoint traps
61     //----------------------------------------------------------------------
62 
63     //------------------------------------------------------------------
64     /// Returns the Opcode Bytes for this breakpoint
65     //------------------------------------------------------------------
66     uint8_t *
67     GetTrapOpcodeBytes ();
68 
69     //------------------------------------------------------------------
70     /// Returns the Opcode Bytes for this breakpoint - const version
71     //------------------------------------------------------------------
72     const uint8_t *
73     GetTrapOpcodeBytes () const;
74 
75     //------------------------------------------------------------------
76     /// Get the size of the trap opcode for this address
77     //------------------------------------------------------------------
78     size_t
79     GetTrapOpcodeMaxByteSize () const;
80 
81     //------------------------------------------------------------------
82     /// Sets the trap opcode
83     //------------------------------------------------------------------
84     bool
85     SetTrapOpcode (const uint8_t *trap_opcode,
86                    uint32_t trap_opcode_size);
87 
88     //------------------------------------------------------------------
89     /// Gets the original instruction bytes that were overwritten by the trap
90     //------------------------------------------------------------------
91     uint8_t *
92     GetSavedOpcodeBytes ();
93 
94     //------------------------------------------------------------------
95     /// Gets the original instruction bytes that were overwritten by the trap const version
96     //------------------------------------------------------------------
97     const uint8_t *
98     GetSavedOpcodeBytes () const;
99 
100     //------------------------------------------------------------------
101     /// Says whether \a addr and size \a size intersects with the address \a intersect_addr
102     //------------------------------------------------------------------
103     bool
104     IntersectsRange (lldb::addr_t addr,
105                      size_t size,
106                      lldb::addr_t *intersect_addr,
107                      size_t *intersect_size,
108                      size_t *opcode_offset) const;
109 
110     //------------------------------------------------------------------
111     /// Tells whether the current breakpoint site is enabled or not
112     ///
113     /// This is a low-level enable bit for the breakpoint sites.  If a
114     /// breakpoint site has no enabled owners, it should just get
115     /// removed.  This enable/disable is for the low-level target code
116     /// to enable and disable breakpoint sites when single stepping,
117     /// etc.
118     //------------------------------------------------------------------
119     bool
120     IsEnabled () const;
121 
122     //------------------------------------------------------------------
123     /// Sets whether the current breakpoint site is enabled or not
124     ///
125     /// @param[in] enabled
126     ///    \b true if the breakoint is enabled, \b false otherwise.
127     //------------------------------------------------------------------
128     void
129     SetEnabled (bool enabled);
130 
131     //------------------------------------------------------------------
132     /// Enquires of the breakpoint locations that produced this breakpoint site whether
133     /// we should stop at this location.
134     ///
135     /// @param[in] context
136     ///    This contains the information about this stop.
137     ///
138     /// @return
139     ///    \b true if we should stop, \b false otherwise.
140     //------------------------------------------------------------------
141     virtual bool
142     ShouldStop (StoppointCallbackContext *context);
143 
144     //------------------------------------------------------------------
145     /// Standard Dump method
146     ///
147     /// @param[in] context
148     ///    The stream to dump this output.
149     //------------------------------------------------------------------
150     void
151     Dump (Stream *s) const;
152 
153     //------------------------------------------------------------------
154     /// The "Owners" are the breakpoint locations that share this
155     /// breakpoint site. The method adds the \a owner to this breakpoint
156     /// site's owner list.
157     ///
158     /// @param[in] context
159     ///    \a owner is the Breakpoint Location to add.
160     //------------------------------------------------------------------
161     void
162     AddOwner (const lldb::BreakpointLocationSP &owner);
163 
164     //------------------------------------------------------------------
165     /// This method returns the number of breakpoint locations currently
166     /// located at this breakpoint site.
167     ///
168     /// @return
169     ///    The number of owners.
170     //------------------------------------------------------------------
171     size_t
172     GetNumberOfOwners ();
173 
174     //------------------------------------------------------------------
175     /// This method returns the the breakpoint location at index \a index
176     /// located at this breakpoint site.  The owners are listed ordinally
177     /// from 0 to GetNumberOfOwners() - 1 so you can use this method to iterate
178     /// over the owners
179     ///
180     /// @param[in] index
181     ///     The index in the list of owners for which you wish the owner location.
182     /// @return
183     ///    A shared pointer to the breakpoint location at that index.
184     //------------------------------------------------------------------
185     lldb::BreakpointLocationSP
186     GetOwnerAtIndex (size_t idx);
187 
188     //------------------------------------------------------------------
189     /// Check whether the owners of this breakpoint site have any
190     /// thread specifiers, and if yes, is \a thread contained in any
191     /// of these specifiers.
192     ///
193     /// @param[in] thread
194     ///     The thread against which to test.
195     ///
196     /// return
197     ///     \b true if the collection contains at least one location that
198     ///     would be valid for this thread, false otherwise.
199     //------------------------------------------------------------------
200     bool
201     ValidForThisThread (Thread *thread);
202 
203 
204     //------------------------------------------------------------------
205     /// Print a description of this breakpoint site to the stream \a s.
206     /// GetDescription tells you about the breakpoint site's owners.
207     /// Use BreakpointSite::Dump(Stream *) to get information about the
208     /// breakpoint site itself.
209     ///
210     /// @param[in] s
211     ///     The stream to which to print the description.
212     ///
213     /// @param[in] level
214     ///     The description level that indicates the detail level to
215     ///     provide.
216     ///
217     /// @see lldb::DescriptionLevel
218     //------------------------------------------------------------------
219     void
220     GetDescription (Stream *s,
221                     lldb::DescriptionLevel level);
222 
223     //------------------------------------------------------------------
224     /// Tell whether a breakpoint has a location at this site.
225     ///
226     /// @param[in] bp_id
227     ///     The breakpoint id to query.
228     ///
229     /// @result
230     ///     \b true if bp_id has a location that is at this site,
231     ///     \b false otherwise.
232     //------------------------------------------------------------------
233     bool
234     IsBreakpointAtThisSite (lldb::break_id_t bp_id);
235 
236     //------------------------------------------------------------------
237     /// Tell whether ALL the breakpoints in the location collection are internal.
238     ///
239     /// @result
240     ///     \b true if all breakpoint locations are owned by internal breakpoints,
241     ///     \b false otherwise.
242     //------------------------------------------------------------------
243     bool
244     IsInternal () const;
245 
246     BreakpointSite::Type
GetType()247     GetType () const
248     {
249         return m_type;
250     }
251 
252     void
SetType(BreakpointSite::Type type)253     SetType (BreakpointSite::Type type)
254     {
255         m_type = type;
256     }
257 
258 private:
259     friend class Process;
260 
261     //------------------------------------------------------------------
262     /// The method removes the owner at \a break_loc_id from this breakpoint list.
263     ///
264     /// @param[in] context
265     ///    \a break_loc_id is the Breakpoint Location to remove.
266     //------------------------------------------------------------------
267     size_t
268     RemoveOwner (lldb::break_id_t break_id,
269                  lldb::break_id_t break_loc_id);
270 
271     BreakpointSite::Type m_type;///< The type of this breakpoint site.
272     uint8_t m_saved_opcode[8];  ///< The saved opcode bytes if this breakpoint site uses trap opcodes.
273     uint8_t m_trap_opcode[8];   ///< The opcode that was used to create the breakpoint if it is a software breakpoint site.
274     bool m_enabled;             ///< Boolean indicating if this breakpoint site enabled or not.
275 
276     // Consider adding an optimization where if there is only one
277     // owner, we don't store a list.  The usual case will be only one owner...
278     BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations that share this breakpoint site.
279 
280     static lldb::break_id_t
281     GetNextID();
282 
283     // Only the Process can create breakpoint sites in
284     // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
285     BreakpointSite (BreakpointSiteList *list,
286                     const lldb::BreakpointLocationSP& owner,
287                     lldb::addr_t m_addr,
288                     bool use_hardware);
289 
290     DISALLOW_COPY_AND_ASSIGN(BreakpointSite);
291 };
292 
293 } // namespace lldb_private
294 
295 #endif  // liblldb_BreakpointSite_h_
296