BreakpointSite.h revision 598df88bd6fc33c6fb330bc859bdc277795501f3
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
26namespace 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
41class BreakpointSite :
42    public STD_ENABLE_SHARED_FROM_THIS(BreakpointSite),
43    public StoppointLocation
44{
45public:
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                   size_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    uint32_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 (uint32_t index);
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    bool
224    IsBreakpointAtThisSite (lldb::break_id_t bp_id);
225
226    BreakpointSite::Type
227    GetType () const
228    {
229        return m_type;
230    }
231
232    void
233    SetType (BreakpointSite::Type type)
234    {
235        m_type = type;
236    }
237
238private:
239    friend class Process;
240
241    //------------------------------------------------------------------
242    /// The method removes the owner at \a break_loc_id from this breakpoint list.
243    ///
244    /// @param[in] context
245    ///    \a break_loc_id is the Breakpoint Location to remove.
246    //------------------------------------------------------------------
247    uint32_t
248    RemoveOwner (lldb::break_id_t break_id,
249                 lldb::break_id_t break_loc_id);
250
251    BreakpointSite::Type m_type;///< The type of this breakpoint site.
252    uint8_t m_saved_opcode[8];  ///< The saved opcode bytes if this breakpoint site uses trap opcodes.
253    uint8_t m_trap_opcode[8];   ///< The opcode that was used to create the breakpoint if it is a software breakpoint site.
254    bool m_enabled;             ///< Boolean indicating if this breakpoint site enabled or not.
255
256    // Consider adding an optimization where if there is only one
257    // owner, we don't store a list.  The usual case will be only one owner...
258    BreakpointLocationCollection m_owners; ///< This has the BreakpointLocations that share this breakpoint site.
259
260    static lldb::break_id_t
261    GetNextID();
262
263    // Only the Process can create breakpoint sites in
264    // Process::CreateBreakpointSite (lldb::BreakpointLocationSP &, bool).
265    BreakpointSite (BreakpointSiteList *list,
266                    const lldb::BreakpointLocationSP& owner,
267                    lldb::addr_t m_addr,
268                    lldb::tid_t tid,
269                    bool use_hardware);
270
271    DISALLOW_COPY_AND_ASSIGN(BreakpointSite);
272};
273
274} // namespace lldb_private
275
276#endif  // liblldb_BreakpointSite_h_
277