File: dbg_breakpoints.c

package info (click to toggle)
mupen64plus-core 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,776 kB
  • ctags: 7,778
  • sloc: ansic: 59,320; asm: 1,994; cpp: 1,823; python: 619; makefile: 600; sh: 311
file content (215 lines) | stat: -rw-r--r-- 7,802 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *   Mupen64plus - dbg_breakpoints.c                                       *
 *   Mupen64Plus homepage: http://code.google.com/p/mupen64plus/           *
 *   Copyright (C) 2008 DarkJeztr HyperHacker                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <SDL.h>
#include <SDL_thread.h>

#include "api/callbacks.h"
#include "api/m64p_types.h"
#include "dbg_breakpoints.h"
#include "dbg_debugger.h"
#include "dbg_types.h"
#include "memory/memory.h"

#ifdef DBG

int g_NumBreakpoints=0;
m64p_breakpoint g_Breakpoints[BREAKPOINTS_MAX_NUMBER];

int add_breakpoint( uint32 address )
{
    if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
        DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
        return -1;
    }
    g_Breakpoints[g_NumBreakpoints].address=address;
    g_Breakpoints[g_NumBreakpoints].endaddr=address;
    BPT_SET_FLAG(g_Breakpoints[g_NumBreakpoints], M64P_BKP_FLAG_EXEC);

    enable_breakpoint(g_NumBreakpoints);

    return g_NumBreakpoints++;
}

int add_breakpoint_struct(m64p_breakpoint *newbp)
{
     if( g_NumBreakpoints == BREAKPOINTS_MAX_NUMBER ) {
        DebugMessage(M64MSG_ERROR, "BREAKPOINTS_MAX_NUMBER have been reached.");
        return -1;
    }

    memcpy(&g_Breakpoints[g_NumBreakpoints], newbp, sizeof(m64p_breakpoint));

    if (BPT_CHECK_FLAG(g_Breakpoints[g_NumBreakpoints], M64P_BKP_FLAG_ENABLED)) {
        BPT_CLEAR_FLAG(g_Breakpoints[g_NumBreakpoints], M64P_BKP_FLAG_ENABLED);
        enable_breakpoint( g_NumBreakpoints );
    }
    
    return g_NumBreakpoints++;
}

void enable_breakpoint( int bpt)
{
    m64p_breakpoint *curBpt = g_Breakpoints + bpt;
    uint64 bptAddr;
    
    if (BPT_CHECK_FLAG((*curBpt), M64P_BKP_FLAG_READ)) {
        for (bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
            if (lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, M64P_BKP_FLAG_ENABLED | M64P_BKP_FLAG_READ) == -1)
                activate_memory_break_read((uint32) bptAddr);
    }

    if (BPT_CHECK_FLAG((*curBpt), M64P_BKP_FLAG_WRITE)) {
        for (bptAddr = curBpt->address; bptAddr <= (curBpt->endaddr | 0xFFFF); bptAddr+=0x10000)
            if (lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, M64P_BKP_FLAG_ENABLED | M64P_BKP_FLAG_WRITE) == -1)
                activate_memory_break_write((uint32) bptAddr);
    }
    
    BPT_SET_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED);
}

void disable_breakpoint( int bpt )
{
    m64p_breakpoint *curBpt = g_Breakpoints + bpt;
    uint64 bptAddr;

    BPT_CLEAR_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED);

    if (BPT_CHECK_FLAG((*curBpt), M64P_BKP_FLAG_READ)) {
        for (bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
            if (lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, M64P_BKP_FLAG_ENABLED | M64P_BKP_FLAG_READ) == -1)
                deactivate_memory_break_read((uint32) bptAddr);
    }

    if (BPT_CHECK_FLAG((*curBpt), M64P_BKP_FLAG_WRITE)) {
        for (bptAddr = curBpt->address; bptAddr <= ((unsigned long)(curBpt->endaddr | 0xFFFF)); bptAddr+=0x10000)
            if (lookup_breakpoint((uint32) bptAddr & 0xFFFF0000, 0x10000, M64P_BKP_FLAG_ENABLED | M64P_BKP_FLAG_WRITE) == -1)
                deactivate_memory_break_write((uint32) bptAddr);
    }

    BPT_CLEAR_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED);
}

void remove_breakpoint_by_num( int bpt )
{
    int curBpt;
    
    if (BPT_CHECK_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED))
        disable_breakpoint( bpt );

    for(curBpt=bpt+1; curBpt<g_NumBreakpoints; curBpt++)
        g_Breakpoints[curBpt-1]=g_Breakpoints[curBpt];
    
    g_NumBreakpoints--;
}

void remove_breakpoint_by_address( uint32 address )
{
    int bpt = lookup_breakpoint( address, 1, 0 );
    if(bpt==-1)
    {
        DebugMessage(M64MSG_ERROR, "Tried to remove nonexistent breakpoint %x!", address);
    }
    else
        remove_breakpoint_by_num( bpt );
}

void replace_breakpoint_num( int bpt, m64p_breakpoint *copyofnew )
{
    if (BPT_CHECK_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED))
        disable_breakpoint(bpt);

    memcpy(&g_Breakpoints[bpt], copyofnew, sizeof(m64p_breakpoint));

    if (BPT_CHECK_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED)) {
        BPT_CLEAR_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_ENABLED);
        enable_breakpoint(bpt);
    }
}

int lookup_breakpoint( uint32 address, uint32 size, uint32 flags)
{
    int i;
    uint64 endaddr = ((uint64)address) + ((uint64)size) - 1;
    
    for( i=0; i < g_NumBreakpoints; i++)
    {
        if((g_Breakpoints[i].flags & flags) == flags)
        {
            if(g_Breakpoints[i].endaddr < g_Breakpoints[i].address)
            {
                if((endaddr >= g_Breakpoints[i].address) || 
                    (address <= g_Breakpoints[i].endaddr))
                        return i;
            }
            else // endaddr >= address
            {
                if((endaddr >= g_Breakpoints[i].address) && 
                    (address <= g_Breakpoints[i].endaddr))
                        return i;
            }
        }
    }
    return -1;
}

int check_breakpoints( uint32 address )
{
    return lookup_breakpoint(address, 1, M64P_BKP_FLAG_ENABLED | M64P_BKP_FLAG_EXEC);
}


int check_breakpoints_on_mem_access( uint32 pc, uint32 address, uint32 size, uint32 flags )
{
    //This function handles memory read/write breakpoints. size specifies the address
    //range to check, flags specifies the flags that all need to be set.
    //It automatically stops and updates the debugger on hit, so the memory access
    //functions only need to call it and can discard the result.
    int bpt;
    if (g_dbg_runstate == M64P_DBG_RUNSTATE_RUNNING) {
        bpt = lookup_breakpoint(address, size, flags);
        if (bpt != -1) {
            if (BPT_CHECK_FLAG(g_Breakpoints[bpt], M64P_BKP_FLAG_LOG))
                log_breakpoint(pc, flags, address);

            g_dbg_runstate = M64P_DBG_RUNSTATE_PAUSED;
            update_debugger(pc);

            return bpt;
        }
    }
    return -1;
}

int log_breakpoint(uint32 PC, uint32 Flag, uint32 Access)
{
    char msg[32];
    if (Flag & M64P_BKP_FLAG_READ)
        sprintf(msg, "0x%08X read 0x%08X", PC, Access);
    else if (Flag & M64P_BKP_FLAG_WRITE)
        sprintf(msg, "0x%08X wrote 0x%08X", PC, Access);
    else
        sprintf(msg, "0x%08X executed", PC);
    DebugMessage(M64MSG_INFO, "BPT: %s", msg);
    return 0;
}

#endif