451ab91e |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus - dbg_breakpoints.c * |
3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
4 | * Copyright (C) 2008 DarkJeztr HyperHacker * |
5 | * * |
6 | * This program is free software; you can redistribute it and/or modify * |
7 | * it under the terms of the GNU General Public License as published by * |
8 | * the Free Software Foundation; either version 2 of the License, or * |
9 | * (at your option) any later version. * |
10 | * * |
11 | * This program is distributed in the hope that it will be useful, * |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 | * GNU General Public License for more details. * |
15 | * * |
16 | * You should have received a copy of the GNU General Public License * |
17 | * along with this program; if not, write to the * |
18 | * Free Software Foundation, Inc., * |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
20 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
21 | |
22 | #include <SDL.h> |
23 | #include <SDL_thread.h> |
24 | |
25 | #include "dbg_types.h" |
26 | #include "debugger.h" |
27 | #include "dbg_breakpoints.h" |
28 | #include "dbg_memory.h" |
29 | |
30 | #include "api/m64p_types.h" |
31 | #include "api/callbacks.h" |
32 | |
33 | int g_NumBreakpoints=0; |
34 | breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER]; |
35 | |
36 | |
37 | int add_breakpoint( uint32 address ) |
38 | { |
39 | if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) { |
40 | DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached."); |
41 | return -1; |
42 | } |
43 | g_Breakpoints[g_NumBreakpoints].address=address; |
44 | g_Breakpoints[g_NumBreakpoints].endaddr=address; |
45 | BPT_SET_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_EXEC); |
46 | |
47 | enable_breakpoint(g_NumBreakpoints); |
48 | |
49 | return g_NumBreakpoints++; |
50 | } |
51 | |
52 | int add_breakpoint_struct(breakpoint* newbp) |
53 | { |
54 | if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) { |
55 | DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached."); |
56 | return -1; |
57 | } |
58 | |
59 | memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(breakpoint)); |
60 | |
61 | if(BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED)) |
62 | { |
63 | BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED); |
64 | enable_breakpoint( g_NumBreakpoints ); |
65 | } |
66 | |
67 | return g_NumBreakpoints++; |
68 | } |
69 | |
70 | void enable_breakpoint( int bpt) |
71 | { |
72 | breakpoint *curBpt = g_Breakpoints + bpt; |
73 | uint64 bptAddr; |
74 | |
75 | if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) { |
76 | for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000) |
77 | if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1) |
78 | activate_memory_break_read((uint32) bptAddr); |
79 | } |
80 | |
81 | if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) { |
82 | for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000) |
83 | if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1) |
84 | activate_memory_break_write((uint32) bptAddr); |
85 | } |
86 | |
87 | BPT_SET_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED); |
88 | } |
89 | |
90 | void disable_breakpoint( int bpt ) |
91 | { |
92 | breakpoint *curBpt = g_Breakpoints + bpt; |
93 | uint64 bptAddr; |
94 | |
95 | BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED); |
96 | |
97 | if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) { |
98 | for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000) |
99 | if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1) |
100 | deactivate_memory_break_read((uint32) bptAddr); |
101 | } |
102 | |
103 | if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) { |
104 | for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000) |
105 | if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1) |
106 | deactivate_memory_break_write((uint32) bptAddr); |
107 | } |
108 | |
109 | BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED); |
110 | } |
111 | |
112 | void remove_breakpoint_by_num( int bpt ) |
113 | { |
114 | int curBpt; |
115 | |
116 | if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED)) |
117 | disable_breakpoint( bpt ); |
118 | |
119 | for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++) |
120 | g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt]; |
121 | |
122 | g_NumBreakpoints--; |
123 | } |
124 | |
125 | void remove_breakpoint_by_address( uint32 address ) |
126 | { |
127 | int bpt = lookup_breakpoint( address, 1, 0 ); |
128 | if(bpt==-1) |
129 | { |
130 | DebugMessage(M64MSG_ERROR, "Tried to remove Nonexistant breakpoint %x!", address); |
131 | } |
132 | else |
133 | remove_breakpoint_by_num( bpt ); |
134 | } |
135 | |
136 | void replace_breakpoint_num( int bpt, breakpoint* copyofnew ) |
137 | { |
138 | |
139 | if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED)) |
140 | disable_breakpoint( bpt ); |
141 | |
142 | memcpy(&(g_Breakpoints[bpt]), copyofnew, sizeof(breakpoint)); |
143 | |
144 | if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED)) |
145 | { |
146 | BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED); |
147 | enable_breakpoint( bpt ); |
148 | } |
149 | } |
150 | |
151 | int lookup_breakpoint( uint32 address, uint32 size, uint32 flags) |
152 | { |
153 | int i; |
154 | uint64 endaddr = ((uint64)address) + ((uint64)size) - 1; |
155 | |
156 | for( i=0; i < g_NumBreakpoints; i++) |
157 | { |
158 | if((g_Breakpoints[i].flags & flags) == flags) |
159 | { |
160 | if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address) |
161 | { |
162 | if((endaddr >= g_Breakpoints[i].address) || |
163 | (address <= g_Breakpoints[i].endaddr)) |
164 | return i; |
165 | } |
166 | else // endaddr >= address |
167 | { |
168 | if((endaddr >= g_Breakpoints[i].address) && |
169 | (address <= g_Breakpoints[i].endaddr)) |
170 | return i; |
171 | } |
172 | } |
173 | } |
174 | return -1; |
175 | } |
176 | |
177 | int check_breakpoints( uint32 address ) |
178 | { |
179 | return lookup_breakpoint( address, 1, BPT_FLAG_ENABLED | BPT_FLAG_EXEC ); |
180 | } |
181 | |
182 | |
183 | int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags ) |
184 | { |
185 | //This function handles memory read/write breakpoints. size specifies the address |
186 | //range to check, flags specifies the flags that all need to be set. |
187 | //It automatically stops and updates the debugger on hit, so the memory access |
188 | //functions only need to call it and can discard the result. |
189 | int bpt; |
190 | if(run == 2) |
191 | { |
192 | bpt=lookup_breakpoint( address, size, flags ); |
193 | if(bpt != -1) |
194 | { |
195 | if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_LOG)) |
196 | log_breakpoint(pc, flags, address); |
197 | |
198 | run = 0; |
199 | update_debugger(pc); |
200 | |
201 | return bpt; |
202 | } |
203 | } |
204 | return -1; |
205 | } |
206 | |
207 | int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access) |
208 | { |
209 | char msg[32]; |
210 | |
211 | if(Flag & BPT_FLAG_READ) sprintf(msg, "0x%08X read 0x%08X", PC, Access); |
212 | else if(Flag & BPT_FLAG_WRITE) sprintf(msg, "0x%08X wrote 0x%08X", PC, Access); |
213 | else sprintf(msg, "0x%08X executed", PC); |
214 | DebugMessage(M64MSG_INFO, "BPT: %s", msg); |
215 | return 0; |
216 | } |
217 | |