~ubuntu-branches/ubuntu/precise/mupen64plus/precise

« back to all changes in this revision

Viewing changes to debugger/breakpoints.c

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2009-09-08 22:17:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090908221700-yela0ckgc1xwiqtn
Tags: upstream-1.5+dfsg1
ImportĀ upstreamĀ versionĀ 1.5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 
2
 *   Mupen64plus - decoder.h                                               *
 
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 "breakpoints.h"
 
26
 
 
27
int g_NumBreakpoints=0;
 
28
breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER];
 
29
 
 
30
 
 
31
int add_breakpoint( uint32 address )
 
32
{
 
33
    if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
 
34
        printf("BREAKPOINTS_MAX_NUMBER have been reached.\n");//REMOVE ME
 
35
        return -1;
 
36
    }
 
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);
 
40
 
 
41
    enable_breakpoint(g_NumBreakpoints);
 
42
 
 
43
    return g_NumBreakpoints++;
 
44
}
 
45
 
 
46
int add_breakpoint_struct(breakpoint* newbp)
 
47
{
 
48
     if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
 
49
        printf("BREAKPOINTS_MAX_NUMBER have been reached.\n");//REMOVE ME
 
50
        return -1;
 
51
    }
 
52
 
 
53
    memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(breakpoint));
 
54
 
 
55
    if(BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED))
 
56
    {
 
57
        BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], BPT_FLAG_ENABLED);
 
58
        enable_breakpoint( g_NumBreakpoints );
 
59
    }
 
60
    
 
61
    return g_NumBreakpoints++;
 
62
}
 
63
 
 
64
void enable_breakpoint( int bpt)
 
65
{
 
66
    breakpoint *curBpt = g_Breakpoints + bpt;
 
67
    uint64 bptAddr;
 
68
    
 
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);
 
73
    }
 
74
 
 
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);
 
79
    }
 
80
    
 
81
    BPT_SET_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
 
82
}
 
83
 
 
84
void disable_breakpoint( int bpt )
 
85
{
 
86
    breakpoint *curBpt = g_Breakpoints + bpt;
 
87
    uint64 bptAddr;
 
88
 
 
89
    BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
 
90
 
 
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);
 
95
    }
 
96
 
 
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);
 
101
    }
 
102
 
 
103
    BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
 
104
}
 
105
 
 
106
void remove_breakpoint_by_num( int bpt )
 
107
{
 
108
    int curBpt;
 
109
    
 
110
    if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
 
111
        disable_breakpoint( bpt );
 
112
 
 
113
    for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++)
 
114
        g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt];
 
115
    
 
116
    g_NumBreakpoints--;
 
117
}
 
118
 
 
119
void remove_breakpoint_by_address( uint32 address )
 
120
{
 
121
    int bpt = lookup_breakpoint( address, 0, 0 );
 
122
    if(bpt==-1)
 
123
        {
 
124
        printf("Tried to remove Nonexistant breakpoint %x!", address);
 
125
        }
 
126
    else
 
127
        remove_breakpoint_by_num( bpt );
 
128
}
 
129
 
 
130
void replace_breakpoint_num( int bpt, breakpoint* copyofnew )
 
131
{
 
132
    
 
133
    if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
 
134
        disable_breakpoint( bpt );
 
135
 
 
136
    memcpy(&(g_Breakpoints[bpt]), copyofnew, sizeof(breakpoint));
 
137
 
 
138
    if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED))
 
139
    {
 
140
        BPT_CLEAR_FLAG(g_Breakpoints[bpt], BPT_FLAG_ENABLED);
 
141
        enable_breakpoint( bpt );
 
142
    }
 
143
}
 
144
 
 
145
int lookup_breakpoint( uint32 address, uint32 size, uint32 flags)
 
146
{
 
147
    int i;
 
148
    uint64 endaddr = ((uint64)address) + ((uint64)size);
 
149
    
 
150
    for( i=0; i < g_NumBreakpoints; i++)
 
151
    {
 
152
        if((g_Breakpoints[i].flags & flags) == flags)
 
153
        {
 
154
            if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address)
 
155
            {
 
156
                if((endaddr >= g_Breakpoints[i].address) || 
 
157
                    (address <= g_Breakpoints[i].endaddr))
 
158
                        return i;
 
159
            }
 
160
            else // endaddr >= address
 
161
            {
 
162
                if((endaddr >= g_Breakpoints[i].address) && 
 
163
                    (address <= g_Breakpoints[i].endaddr))
 
164
                        return i;
 
165
            }
 
166
        }
 
167
    }
 
168
    return -1;
 
169
}
 
170
 
 
171
int check_breakpoints( uint32 address )
 
172
{
 
173
    return lookup_breakpoint( address, 0, BPT_FLAG_ENABLED | BPT_FLAG_EXEC );
 
174
}
 
175
 
 
176
 
 
177
int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags )
 
178
{
 
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.
 
183
    int i, bpt;
 
184
    if(run == 2)
 
185
    {
 
186
        bpt=lookup_breakpoint( address, size, flags );
 
187
        if(bpt != -1)
 
188
        {
 
189
            if(BPT_CHECK_FLAG(g_Breakpoints[bpt], BPT_FLAG_LOG))
 
190
                log_breakpoint(pc, flags, address);
 
191
            
 
192
            run = 0;
 
193
            switch_button_to_run();
 
194
            update_debugger(pc);
 
195
        
 
196
            return bpt;
 
197
        }
 
198
    }
 
199
    return -1;
 
200
}
 
201
 
 
202
int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access)
 
203
{
 
204
    char msg[32];
 
205
    
 
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);
 
210
    //todo: log to file
 
211
}
 
212