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
29 #include "api/m64p_types.h"
30 #include "api/callbacks.h"
31
32 #include "memory/memory.h"
33
34 int g_NumBreakpoints=0;
35 breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER];
36
37
add_breakpoint(uint32 address)38 int add_breakpoint( uint32 address )
39 {
40 if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
41 DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
42 return -1;
43 }
44 g_Breakpoints[g_NumBreakpoints].address=address;
45 g_Breakpoints[g_NumBreakpoints].endaddr=address;
46 BPT_SET_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_EXEC);
47
48 enable_breakpoint(g_NumBreakpoints);
49
50 return g_NumBreakpoints++;
51 }
52
add_breakpoint_struct(breakpoint * newbp)53 int add_breakpoint_struct(breakpoint* newbp)
54 {
55 if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
56 DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
57 return -1;
58 }
59
60 memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(breakpoint));
61
62 if(BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED))
63 {
64 BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED);
65 enable_breakpoint( g_NumBreakpoints );
66 }
67
68 return g_NumBreakpoints++;
69 }
70
enable_breakpoint(int bpt)71 void enable_breakpoint( int bpt)
72 {
73 breakpoint *curBpt = g_Breakpoints + bpt;
74 uint64 bptAddr;
75
76 if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) {
77 for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
78 if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1)
79 activate_memory_break_read((uint32) bptAddr);
80 }
81
82 if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) {
83 for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
84 if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1)
85 activate_memory_break_write((uint32) bptAddr);
86 }
87
88 BPT_SET_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
89 }
90
disable_breakpoint(int bpt)91 void disable_breakpoint( int bpt )
92 {
93 breakpoint *curBpt = g_Breakpoints + bpt;
94 uint64 bptAddr;
95
96 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
97
98 if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) {
99 for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
100 if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1)
101 deactivate_memory_break_read((uint32) bptAddr);
102 }
103
104 if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) {
105 for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
106 if(lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1)
107 deactivate_memory_break_write((uint32) bptAddr);
108 }
109
110 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
111 }
112
remove_breakpoint_by_num(int bpt)113 void remove_breakpoint_by_num( int bpt )
114 {
115 int curBpt;
116
117 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
118 disable_breakpoint( bpt );
119
120 for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++)
121 g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt];
122
123 g_NumBreakpoints--;
124 }
125
remove_breakpoint_by_address(uint32 address)126 void remove_breakpoint_by_address( uint32 address )
127 {
128 int bpt = lookup_breakpoint( address, 1, 0 );
129 if(bpt==-1)
130 {
131 DebugMessage(M64MSG_ERROR, "Tried to remove Nonexistant breakpoint %x!", address);
132 }
133 else
134 remove_breakpoint_by_num( bpt );
135 }
136
replace_breakpoint_num(int bpt,breakpoint * copyofnew)137 void replace_breakpoint_num( int bpt, breakpoint* copyofnew )
138 {
139
140 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
141 disable_breakpoint( bpt );
142
143 memcpy(&(g_Breakpoints[bpt]), copyofnew, sizeof(breakpoint));
144
145 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
146 {
147 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
148 enable_breakpoint( bpt );
149 }
150 }
151
lookup_breakpoint(uint32 address,uint32 size,uint32 flags)152 int lookup_breakpoint( uint32 address, uint32 size, uint32 flags)
153 {
154 int i;
155 uint64 endaddr = ((uint64)address) + ((uint64)size) - 1;
156
157 for( i=0; i < g_NumBreakpoints; i++)
158 {
159 if((g_Breakpoints[i].flags & flags) == flags)
160 {
161 if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address)
162 {
163 if((endaddr >= g_Breakpoints[i].address) ||
164 (address <= g_Breakpoints[i].endaddr))
165 return i;
166 }
167 else // endaddr >= address
168 {
169 if((endaddr >= g_Breakpoints[i].address) &&
170 (address <= g_Breakpoints[i].endaddr))
171 return i;
172 }
173 }
174 }
175 return -1;
176 }
177
check_breakpoints(uint32 address)178 int check_breakpoints( uint32 address )
179 {
180 return lookup_breakpoint( address, 1, BPT_FLAG_ENABLED | BPT_FLAG_EXEC );
181 }
182
183
check_breakpoints_on_mem_access(uint32 pc,uint32 address,uint32 size,uint32 flags)184 int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags )
185 {
186 //This function handles memory read/write breakpoints. size specifies the address
187 //range to check, flags specifies the flags that all need to be set.
188 //It automatically stops and updates the debugger on hit, so the memory access
189 //functions only need to call it and can discard the result.
190 int bpt;
191 if(run == 2)
192 {
193 bpt=lookup_breakpoint( address, size, flags );
194 if(bpt != -1)
195 {
196 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_LOG))
197 log_breakpoint(pc, flags, address);
198
199 run = 0;
200 update_debugger(pc);
201
202 return bpt;
203 }
204 }
205 return -1;
206 }
207
log_breakpoint(uint32 PC,uint32 Flag,uint32 Access)208 int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access)
209 {
210 char msg[32];
211
212 if(Flag & BPT_FLAG_READ) sprintf(msg, "0x%08X read 0x%08X", PC, Access);
213 else if(Flag & BPT_FLAG_WRITE) sprintf(msg, "0x%08X wrote 0x%08X", PC, Access);
214 else sprintf(msg, "0x%08X executed", PC);
215 DebugMessage(M64MSG_INFO, "BPT: %s", msg);
216 return 0;
217 }
218
219