1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
* Mupen64plus - decoder.h *
3
* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4
* Copyright (C) 2008 DarkJeztr HyperHacker *
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. *
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. *
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
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
23
#include <SDL_thread.h>
25
#include "breakpoints.h"
27
int g_NumBreakpoints=0;
28
breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER];
31
int add_breakpoint( uint32 address )
33
if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
34
printf("BREAKPOINTS_MAX_NUMBER have been reached.\n");//REMOVE ME
37
g_Breakpoints[g_NumBreakpoints].address=address;
38
g_Breakpoints[g_NumBreakpoints].endaddr=address;
39
BPT_SET_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_EXEC);
41
enable_breakpoint(g_NumBreakpoints);
43
return g_NumBreakpoints++;
46
int add_breakpoint_struct(breakpoint* newbp)
48
if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
49
printf("BREAKPOINTS_MAX_NUMBER have been reached.\n");//REMOVE ME
53
memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(breakpoint));
55
if(BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED))
57
BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED);
58
enable_breakpoint( g_NumBreakpoints );
61
return g_NumBreakpoints++;
64
void enable_breakpoint( int bpt)
66
breakpoint *curBpt = g_Breakpoints + bpt;
69
if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) {
70
for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
71
if(lookup_breakpoint(bptAddr & 0xFFFF0000, 0xFFFF, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1)
72
activate_memory_break_read(bptAddr);
75
if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) {
76
for(bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
77
if(lookup_breakpoint(bptAddr & 0xFFFF0000, 0xFFFF, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1)
78
activate_memory_break_write(bptAddr);
81
BPT_SET_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
84
void disable_breakpoint( int bpt )
86
breakpoint *curBpt = g_Breakpoints + bpt;
89
BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
91
if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_READ)) {
92
for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
93
if(lookup_breakpoint(bptAddr & 0xFFFF0000, 0xFFFF, BPT_FLAG_ENABLED | BPT_FLAG_READ) == -1)
94
deactivate_memory_break_read(bptAddr);
97
if(BPT_CHECK_FLAG((*curBpt), BPT_FLAG_WRITE)) {
98
for(bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
99
if(lookup_breakpoint(bptAddr & 0xFFFF0000, 0xFFFF, BPT_FLAG_ENABLED | BPT_FLAG_WRITE) == -1)
100
deactivate_memory_break_write(bptAddr);
103
BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
106
void remove_breakpoint_by_num( int bpt )
110
if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
111
disable_breakpoint( bpt );
113
for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++)
114
g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt];
119
void remove_breakpoint_by_address( uint32 address )
121
int bpt = lookup_breakpoint( address, 0, 0 );
124
printf("Tried to remove Nonexistant breakpoint %x!", address);
127
remove_breakpoint_by_num( bpt );
130
void replace_breakpoint_num( int bpt, breakpoint* copyofnew )
133
if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
134
disable_breakpoint( bpt );
136
memcpy(&(g_Breakpoints[bpt]), copyofnew, sizeof(breakpoint));
138
if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
140
BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
141
enable_breakpoint( bpt );
145
int lookup_breakpoint( uint32 address, uint32 size, uint32 flags)
148
uint64 endaddr = ((uint64)address) + ((uint64)size);
150
for( i=0; i < g_NumBreakpoints; i++)
152
if((g_Breakpoints[i].flags & flags) == flags)
154
if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address)
156
if((endaddr >= g_Breakpoints[i].address) ||
157
(address <= g_Breakpoints[i].endaddr))
160
else // endaddr >= address
162
if((endaddr >= g_Breakpoints[i].address) &&
163
(address <= g_Breakpoints[i].endaddr))
171
int check_breakpoints( uint32 address )
173
return lookup_breakpoint( address, 0, BPT_FLAG_ENABLED | BPT_FLAG_EXEC );
177
int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags )
179
//This function handles memory read/write breakpoints. size specifies the address
180
//range to check, flags specifies the flags that all need to be set.
181
//It automatically stops and updates the debugger on hit, so the memory access
182
//functions only need to call it and can discard the result.
186
bpt=lookup_breakpoint( address, size, flags );
189
if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_LOG))
190
log_breakpoint(pc, flags, address);
193
switch_button_to_run();
202
int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access)
206
if(Flag & BPT_FLAG_READ) sprintf(msg, "0x%08X read 0x%08X", PC, Access);
207
else if(Flag & BPT_FLAG_WRITE) sprintf(msg, "0x%08X wrote 0x%08X", PC, Access);
208
else sprintf(msg, "0x%08X executed", PC);
209
printf("BPT: %s\n", msg);