1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * Mupen64plus - dbg_breakpoints.c *
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 "dbg_types.h"
27 #include "dbg_breakpoints.h"
28 #include "dbg_memory.h"
30 #include "api/m64p_types.h"
31 #include "api/callbacks.h"
33 int g_NumBreakpoints=0;
34 breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER];
37 int add_breakpoint( uint32 address )
39 if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
40 DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
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);
47 enable_breakpoint(g_NumBreakpoints);
49 return g_NumBreakpoints++;
52 int add_breakpoint_struct(breakpoint* newbp)
54 if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
55 DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
59 memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(breakpoint));
61 if(BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED))
63 BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED);
64 enable_breakpoint( g_NumBreakpoints );
67 return g_NumBreakpoints++;
70 void enable_breakpoint( int bpt)
72 breakpoint *curBpt = g_Breakpoints + bpt;
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);
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);
87 BPT_SET_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
90 void disable_breakpoint( int bpt )
92 breakpoint *curBpt = g_Breakpoints + bpt;
95 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
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);
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);
109 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
112 void remove_breakpoint_by_num( int bpt )
116 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
117 disable_breakpoint( bpt );
119 for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++)
120 g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt];
125 void remove_breakpoint_by_address( uint32 address )
127 int bpt = lookup_breakpoint( address, 1, 0 );
130 DebugMessage(M64MSG_ERROR, "Tried to remove Nonexistant breakpoint %x!", address);
133 remove_breakpoint_by_num( bpt );
136 void replace_breakpoint_num( int bpt, breakpoint* copyofnew )
139 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
140 disable_breakpoint( bpt );
142 memcpy(&(g_Breakpoints[bpt]), copyofnew, sizeof(breakpoint));
144 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
146 BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
147 enable_breakpoint( bpt );
151 int lookup_breakpoint( uint32 address, uint32 size, uint32 flags)
154 uint64 endaddr = ((uint64)address) + ((uint64)size) - 1;
156 for( i=0; i < g_NumBreakpoints; i++)
158 if((g_Breakpoints[i].flags & flags) == flags)
160 if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address)
162 if((endaddr >= g_Breakpoints[i].address) ||
163 (address <= g_Breakpoints[i].endaddr))
166 else // endaddr >= address
168 if((endaddr >= g_Breakpoints[i].address) &&
169 (address <= g_Breakpoints[i].endaddr))
177 int check_breakpoints( uint32 address )
179 return lookup_breakpoint( address, 1, BPT_FLAG_ENABLED | BPT_FLAG_EXEC );
183 int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags )
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.
192 bpt=lookup_breakpoint( address, size, flags );
195 if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_LOG))
196 log_breakpoint(pc, flags, address);
207 int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access)
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);