1 //===-- DNBBreakpoint.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 //  Created by Greg Clayton on 6/29/07.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef __DNBBreakpoint_h__
15 #define __DNBBreakpoint_h__
16 
17 #include <list>
18 
19 #include "DNBDefs.h"
20 
21 class DNBBreakpoint
22 {
23 public:
24     DNBBreakpoint(nub_addr_t m_addr, nub_size_t byte_size, nub_thread_t tid, bool hardware);
25     ~DNBBreakpoint();
26 
27     nub_break_t GetID() const { return m_breakID; }
28     nub_size_t  ByteSize() const { return m_byte_size; }
29     uint8_t *   SavedOpcodeBytes() { return &m_opcode[0]; }
30     const uint8_t *
31                 SavedOpcodeBytes() const { return &m_opcode[0]; }
32     nub_addr_t  Address() const { return m_addr; }
33     nub_thread_t ThreadID() const { return m_tid; }
34     bool        IsEnabled() const { return m_enabled; }
35     bool        IntersectsRange(nub_addr_t addr, nub_size_t size, nub_addr_t *intersect_addr, nub_size_t *intersect_size, nub_size_t *opcode_offset) const
36                 {
37                     // We only use software traps for software breakpoints
38                     if (IsBreakpoint() && IsEnabled() && !IsHardware())
39                     {
40                         if (m_byte_size > 0)
41                         {
42                             const nub_addr_t bp_end_addr = m_addr + m_byte_size;
43                             const nub_addr_t end_addr = addr + size;
44                             // Is the breakpoint end address before the passed in start address?
45                             if (bp_end_addr <= addr)
46                                 return false;
47                             // Is the breakpoint start address after passed in end address?
48                             if (end_addr <= m_addr)
49                                 return false;
50                             if (intersect_addr || intersect_size || opcode_offset)
51                             {
52                                 if (m_addr < addr)
53                                 {
54                                     if (intersect_addr)
55                                         *intersect_addr = addr;
56                                     if (intersect_size)
57                                         *intersect_size = std::min<nub_addr_t>(bp_end_addr, end_addr) - addr;
58                                     if (opcode_offset)
59                                         *opcode_offset = addr - m_addr;
60                                 }
61                                 else
62                                 {
63                                     if (intersect_addr)
64                                         *intersect_addr = m_addr;
65                                     if (intersect_size)
66                                         *intersect_size = std::min<nub_addr_t>(bp_end_addr, end_addr) - m_addr;
67                                     if (opcode_offset)
68                                         *opcode_offset = 0;
69                                 }
70                             }
71                             return true;
72                         }
73                     }
74                     return false;
75                 }
76     void        SetEnabled(bool enabled)
77                 {
78                     if (!enabled)
79                         SetHardwareIndex(INVALID_NUB_HW_INDEX);
80                     m_enabled = enabled;
81                 }
82     void        SetIsWatchpoint (uint32_t type)
83                 {
84                     m_is_watchpoint = 1;
85                     m_watch_read = (type & WATCH_TYPE_READ) != 0;
86                     m_watch_write = (type & WATCH_TYPE_WRITE) != 0;
87                 }
88     bool        IsBreakpoint() const { return m_is_watchpoint == 0; }
89     bool        IsWatchpoint() const { return m_is_watchpoint == 1; }
90     bool        WatchpointRead() const { return m_watch_read != 0; }
91     bool        WatchpointWrite() const { return m_watch_write != 0; }
92     bool        HardwarePreferred() const { return m_hw_preferred; }
93     bool        IsHardware() const { return m_hw_index != INVALID_NUB_HW_INDEX; }
94     uint32_t    GetHardwareIndex() const { return m_hw_index; }
95     void        SetHardwareIndex(uint32_t hw_index) { m_hw_index = hw_index; }
96 //  StateType   GetState() const { return m_state; }
97 //  void        SetState(StateType newState) { m_state = newState; }
98     int32_t     GetHitCount() const { return m_hit_count; }
99     int32_t     GetIgnoreCount() const { return m_ignore_count; }
100     void        SetIgnoreCount(int32_t n) { m_ignore_count = n; }
101     bool        BreakpointHit(nub_process_t pid, nub_thread_t tid);
102     void        SetCallback(DNBCallbackBreakpointHit callback, void *callback_baton);
103     void        Dump() const;
104 
105 private:
106     nub_break_t m_breakID;          // The unique identifier for this breakpoint
107     nub_thread_t m_tid;             // Thread ID for the breakpoint (can be INVALID_NUB_THREAD for all threads)
108     nub_size_t  m_byte_size;        // Length in bytes of the breakpoint if set in memory
109     uint8_t     m_opcode[8];        // Saved opcode bytes
110     nub_addr_t  m_addr;             // Address of this breakpoint
111     uint32_t    m_enabled:1,        // Flags for this breakpoint
112                 m_hw_preferred:1,   // 1 if this point has been requested to be set using hardware (which may fail due to lack of resources)
113                 m_is_watchpoint:1,  // 1 if this is a watchpoint
114                 m_watch_read:1,     // 1 if we stop when the watched data is read from
115                 m_watch_write:1;    // 1 if we stop when the watched data is written to
116     uint32_t    m_hw_index;         // The hardware resource index for this breakpoint/watchpoint
117     int32_t     m_hit_count;        // Number of times this breakpoint has been hit
118     int32_t     m_ignore_count;     // Number of times to ignore this breakpoint
119     DNBCallbackBreakpointHit
120                 m_callback;         // Callback to call when this breakpoint gets hit
121     void *      m_callback_baton;   // Callback user data to pass to callback
122 
123     static nub_break_t GetNextID();
124 
125 };
126 
127 
128 class DNBBreakpointList
129 {
130 public:
131                                 DNBBreakpointList();
132                                 ~DNBBreakpointList();
133 
134             nub_break_t         Add (const DNBBreakpoint& bp);
135             nub_break_t         FindIDByAddress (nub_addr_t addr);
136             bool                ShouldStop (nub_process_t pid, nub_thread_t tid, nub_break_t breakID);
137             bool                Remove (nub_break_t breakID);
138             bool                SetCallback (nub_break_t breakID, DNBCallbackBreakpointHit callback, void *callback_baton);
139             DNBBreakpoint *     FindByAddress (nub_addr_t addr);
140     const   DNBBreakpoint *     FindByAddress (nub_addr_t addr) const;
141             DNBBreakpoint *     FindByID (nub_break_t breakID);
142     const   DNBBreakpoint *     FindByID (nub_break_t breakID) const;
143             void                Dump () const;
144 
145             size_t              Size() const { return m_breakpoints.size(); }
146             DNBBreakpoint *     GetByIndex (uint32_t i);
147     const   DNBBreakpoint *     GetByIndex (uint32_t i) const;
148 
149 protected:
150     typedef std::list<DNBBreakpoint>    collection;
151     typedef collection::iterator        iterator;
152     typedef collection::const_iterator  const_iterator;
153             iterator                    GetBreakIDIterator(nub_break_t breakID);
154             const_iterator              GetBreakIDConstIterator(nub_break_t breakID) const;
155             collection                  m_breakpoints;
156 };
157 
158 #endif
159 
160