Subversion Repositories HelenOS

Rev

Rev 2071 | Rev 2745 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2071 Rev 2719
1
/*
1
/*
2
 * Copyright (c) 2005 Ondrej Palkovsky
2
 * Copyright (c) 2005 Ondrej Palkovsky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup mips32debug
29
/** @addtogroup mips32debug
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/debugger.h>
35
#include <arch/debugger.h>
36
#include <memstr.h>
36
#include <memstr.h>
37
#include <console/kconsole.h>
37
#include <console/kconsole.h>
38
#include <console/cmd.h>
38
#include <console/cmd.h>
39
#include <symtab.h>
39
#include <symtab.h>
40
#include <print.h>
40
#include <print.h>
41
#include <panic.h>
41
#include <panic.h>
42
#include <arch.h>
42
#include <arch.h>
43
#include <arch/cp0.h>
43
#include <arch/cp0.h>
44
#include <func.h>
44
#include <func.h>
45
 
45
 
46
bpinfo_t breakpoints[BKPOINTS_MAX];
46
bpinfo_t breakpoints[BKPOINTS_MAX];
47
SPINLOCK_INITIALIZE(bkpoint_lock);
47
SPINLOCK_INITIALIZE(bkpoint_lock);
48
 
48
 
49
static int cmd_print_breakpoints(cmd_arg_t *argv);
49
static int cmd_print_breakpoints(cmd_arg_t *argv);
50
static cmd_info_t bkpts_info = {
50
static cmd_info_t bkpts_info = {
51
    .name = "bkpts",
51
    .name = "bkpts",
52
    .description = "Print breakpoint table.",
52
    .description = "Print breakpoint table.",
53
    .func = cmd_print_breakpoints,
53
    .func = cmd_print_breakpoints,
54
    .argc = 0,
54
    .argc = 0,
55
};
55
};
56
 
56
 
57
static int cmd_del_breakpoint(cmd_arg_t *argv);
57
static int cmd_del_breakpoint(cmd_arg_t *argv);
58
static cmd_arg_t del_argv = {
58
static cmd_arg_t del_argv = {
59
    .type = ARG_TYPE_INT
59
    .type = ARG_TYPE_INT
60
};
60
};
61
static cmd_info_t delbkpt_info = {
61
static cmd_info_t delbkpt_info = {
62
    .name = "delbkpt",
62
    .name = "delbkpt",
63
    .description = "delbkpt <number> - Delete breakpoint.",
63
    .description = "delbkpt <number> - Delete breakpoint.",
64
    .func = cmd_del_breakpoint,
64
    .func = cmd_del_breakpoint,
65
    .argc = 1,
65
    .argc = 1,
66
    .argv = &del_argv
66
    .argv = &del_argv
67
};
67
};
68
 
68
 
69
static int cmd_add_breakpoint(cmd_arg_t *argv);
69
static int cmd_add_breakpoint(cmd_arg_t *argv);
70
static cmd_arg_t add_argv = {
70
static cmd_arg_t add_argv = {
71
    .type = ARG_TYPE_INT
71
    .type = ARG_TYPE_INT
72
};
72
};
73
static cmd_info_t addbkpt_info = {
73
static cmd_info_t addbkpt_info = {
74
    .name = "addbkpt",
74
    .name = "addbkpt",
75
    .description = "addbkpt <&symbol> - new bkpoint. Break on J/Branch insts unsupported.",
75
    .description = "addbkpt <&symbol> - new bkpoint. Break on J/Branch insts unsupported.",
76
    .func = cmd_add_breakpoint,
76
    .func = cmd_add_breakpoint,
77
    .argc = 1,
77
    .argc = 1,
78
    .argv = &add_argv
78
    .argv = &add_argv
79
};
79
};
80
 
80
 
81
static cmd_arg_t adde_argv[] = {
81
static cmd_arg_t adde_argv[] = {
82
    { .type = ARG_TYPE_INT },
82
    { .type = ARG_TYPE_INT },
83
    { .type = ARG_TYPE_INT }
83
    { .type = ARG_TYPE_INT }
84
};
84
};
85
static cmd_info_t addbkpte_info = {
85
static cmd_info_t addbkpte_info = {
86
    .name = "addbkpte",
86
    .name = "addbkpte",
87
    .description = "addebkpte <&symbol> <&func> - new bkpoint. Call func(or Nothing if 0).",
87
    .description = "addebkpte <&symbol> <&func> - new bkpoint. Call func(or Nothing if 0).",
88
    .func = cmd_add_breakpoint,
88
    .func = cmd_add_breakpoint,
89
    .argc = 2,
89
    .argc = 2,
90
    .argv = adde_argv
90
    .argv = adde_argv
91
};
91
};
92
 
92
 
93
static struct {
93
static struct {
94
    uint32_t andmask;
94
    uint32_t andmask;
95
    uint32_t value;
95
    uint32_t value;
96
}jmpinstr[] = {
96
}jmpinstr[] = {
97
    {0xf3ff0000, 0x41000000}, /* BCzF */
97
    {0xf3ff0000, 0x41000000}, /* BCzF */
98
    {0xf3ff0000, 0x41020000}, /* BCzFL */
98
    {0xf3ff0000, 0x41020000}, /* BCzFL */
99
    {0xf3ff0000, 0x41010000}, /* BCzT */
99
    {0xf3ff0000, 0x41010000}, /* BCzT */
100
    {0xf3ff0000, 0x41030000}, /* BCzTL */
100
    {0xf3ff0000, 0x41030000}, /* BCzTL */
101
    {0xfc000000, 0x10000000}, /* BEQ */
101
    {0xfc000000, 0x10000000}, /* BEQ */
102
    {0xfc000000, 0x50000000}, /* BEQL */
102
    {0xfc000000, 0x50000000}, /* BEQL */
103
    {0xfc1f0000, 0x04010000}, /* BEQL */
103
    {0xfc1f0000, 0x04010000}, /* BEQL */
104
    {0xfc1f0000, 0x04110000}, /* BGEZAL */
104
    {0xfc1f0000, 0x04110000}, /* BGEZAL */
105
    {0xfc1f0000, 0x04130000}, /* BGEZALL */
105
    {0xfc1f0000, 0x04130000}, /* BGEZALL */
106
    {0xfc1f0000, 0x04030000}, /* BGEZL */
106
    {0xfc1f0000, 0x04030000}, /* BGEZL */
107
    {0xfc1f0000, 0x1c000000}, /* BGTZ */
107
    {0xfc1f0000, 0x1c000000}, /* BGTZ */
108
    {0xfc1f0000, 0x5c000000}, /* BGTZL */
108
    {0xfc1f0000, 0x5c000000}, /* BGTZL */
109
    {0xfc1f0000, 0x18000000}, /* BLEZ */
109
    {0xfc1f0000, 0x18000000}, /* BLEZ */
110
    {0xfc1f0000, 0x58000000}, /* BLEZL */
110
    {0xfc1f0000, 0x58000000}, /* BLEZL */
111
    {0xfc1f0000, 0x04000000}, /* BLTZ */
111
    {0xfc1f0000, 0x04000000}, /* BLTZ */
112
    {0xfc1f0000, 0x04100000}, /* BLTZAL */
112
    {0xfc1f0000, 0x04100000}, /* BLTZAL */
113
    {0xfc1f0000, 0x04120000}, /* BLTZALL */
113
    {0xfc1f0000, 0x04120000}, /* BLTZALL */
114
    {0xfc1f0000, 0x04020000}, /* BLTZL */
114
    {0xfc1f0000, 0x04020000}, /* BLTZL */
115
    {0xfc000000, 0x14000000}, /* BNE */
115
    {0xfc000000, 0x14000000}, /* BNE */
116
    {0xfc000000, 0x54000000}, /* BNEL */
116
    {0xfc000000, 0x54000000}, /* BNEL */
117
    {0xfc000000, 0x08000000}, /* J */
117
    {0xfc000000, 0x08000000}, /* J */
118
    {0xfc000000, 0x0c000000}, /* JAL */
118
    {0xfc000000, 0x0c000000}, /* JAL */
119
    {0xfc1f07ff, 0x00000009}, /* JALR */
119
    {0xfc1f07ff, 0x00000009}, /* JALR */
120
    {0,0} /* EndOfTable */
120
    {0,0} /* EndOfTable */
121
};
121
};
122
 
122
 
123
/** Test, if the given instruction is a jump or branch instruction
123
/** Test, if the given instruction is a jump or branch instruction
124
 *
124
 *
125
 * @param instr Instruction code
125
 * @param instr Instruction code
126
 * @return true - it is jump instruction, false otherwise
126
 * @return true - it is jump instruction, false otherwise
127
 */
127
 */
128
static bool is_jump(unative_t instr)
128
static bool is_jump(unative_t instr)
129
{
129
{
130
    int i;
130
    int i;
131
 
131
 
132
    for (i=0;jmpinstr[i].andmask;i++) {
132
    for (i=0;jmpinstr[i].andmask;i++) {
133
        if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
133
        if ((instr & jmpinstr[i].andmask) == jmpinstr[i].value)
134
            return true;
134
            return true;
135
    }
135
    }
136
 
136
 
137
    return false;
137
    return false;
138
}
138
}
139
 
139
 
140
/** Add new breakpoint to table */
140
/** Add new breakpoint to table */
141
int cmd_add_breakpoint(cmd_arg_t *argv)
141
int cmd_add_breakpoint(cmd_arg_t *argv)
142
{
142
{
143
    bpinfo_t *cur = NULL;
143
    bpinfo_t *cur = NULL;
144
    ipl_t ipl;
144
    ipl_t ipl;
145
    int i;
145
    int i;
146
 
146
 
147
    if (argv->intval & 0x3) {
147
    if (argv->intval & 0x3) {
148
        printf("Not aligned instruction, forgot to use &symbol?\n");
148
        printf("Not aligned instruction, forgot to use &symbol?\n");
149
        return 1;
149
        return 1;
150
    }
150
    }
151
    ipl = interrupts_disable();
151
    ipl = interrupts_disable();
152
    spinlock_lock(&bkpoint_lock);
152
    spinlock_lock(&bkpoint_lock);
153
 
153
 
154
    /* Check, that the breakpoints do not conflict */
154
    /* Check, that the breakpoints do not conflict */
155
    for (i=0; i<BKPOINTS_MAX; i++) {
155
    for (i=0; i<BKPOINTS_MAX; i++) {
156
        if (breakpoints[i].address == (uintptr_t)argv->intval) {
156
        if (breakpoints[i].address == (uintptr_t)argv->intval) {
157
            printf("Duplicate breakpoint %d.\n", i);
157
            printf("Duplicate breakpoint %d.\n", i);
158
            spinlock_unlock(&bkpoints_lock);
158
            spinlock_unlock(&bkpoints_lock);
159
            return 0;
159
            return 0;
160
        } else if (breakpoints[i].address == (uintptr_t)argv->intval + sizeof(unative_t) || \
160
        } else if (breakpoints[i].address == (uintptr_t)argv->intval + sizeof(unative_t) || \
161
               breakpoints[i].address == (uintptr_t)argv->intval - sizeof(unative_t)) {
161
               breakpoints[i].address == (uintptr_t)argv->intval - sizeof(unative_t)) {
162
            printf("Adjacent breakpoints not supported, conflict with %d.\n", i);
162
            printf("Adjacent breakpoints not supported, conflict with %d.\n", i);
163
            spinlock_unlock(&bkpoints_lock);
163
            spinlock_unlock(&bkpoints_lock);
164
            return 0;
164
            return 0;
165
        }
165
        }
166
           
166
           
167
    }
167
    }
168
 
168
 
169
    for (i=0; i<BKPOINTS_MAX; i++)
169
    for (i=0; i<BKPOINTS_MAX; i++)
170
        if (!breakpoints[i].address) {
170
        if (!breakpoints[i].address) {
171
            cur = &breakpoints[i];
171
            cur = &breakpoints[i];
172
            break;
172
            break;
173
        }
173
        }
174
    if (!cur) {
174
    if (!cur) {
175
        printf("Too many breakpoints.\n");
175
        printf("Too many breakpoints.\n");
176
        spinlock_unlock(&bkpoint_lock);
176
        spinlock_unlock(&bkpoint_lock);
177
        interrupts_restore(ipl);
177
        interrupts_restore(ipl);
178
        return 0;
178
        return 0;
179
    }
179
    }
180
    cur->address = (uintptr_t) argv->intval;
180
    cur->address = (uintptr_t) argv->intval;
181
    printf("Adding breakpoint on address: %p\n", argv->intval);
181
    printf("Adding breakpoint on address: %p\n", argv->intval);
182
    cur->instruction = ((unative_t *)cur->address)[0];
182
    cur->instruction = ((unative_t *)cur->address)[0];
183
    cur->nextinstruction = ((unative_t *)cur->address)[1];
183
    cur->nextinstruction = ((unative_t *)cur->address)[1];
184
    if (argv == &add_argv) {
184
    if (argv == &add_argv) {
185
        cur->flags = 0;
185
        cur->flags = 0;
186
    } else { /* We are add extended */
186
    } else { /* We are add extended */
187
        cur->flags = BKPOINT_FUNCCALL;
187
        cur->flags = BKPOINT_FUNCCALL;
188
        cur->bkfunc =   (void (*)(void *, istate_t *)) argv[1].intval;
188
        cur->bkfunc =   (void (*)(void *, istate_t *)) argv[1].intval;
189
    }
189
    }
190
    if (is_jump(cur->instruction))
190
    if (is_jump(cur->instruction))
191
        cur->flags |= BKPOINT_ONESHOT;
191
        cur->flags |= BKPOINT_ONESHOT;
192
    cur->counter = 0;
192
    cur->counter = 0;
193
 
193
 
194
    /* Set breakpoint */
194
    /* Set breakpoint */
195
    *((unative_t *)cur->address) = 0x0d;
195
    *((unative_t *)cur->address) = 0x0d;
196
 
196
 
197
    spinlock_unlock(&bkpoint_lock);
197
    spinlock_unlock(&bkpoint_lock);
198
    interrupts_restore(ipl);
198
    interrupts_restore(ipl);
199
 
199
 
200
    return 1;
200
    return 1;
201
}
201
}
202
 
202
 
203
 
203
 
204
 
204
 
205
/** Remove breakpoint from table */
205
/** Remove breakpoint from table */
206
int cmd_del_breakpoint(cmd_arg_t *argv)
206
int cmd_del_breakpoint(cmd_arg_t *argv)
207
{
207
{
208
    bpinfo_t *cur;
208
    bpinfo_t *cur;
209
    ipl_t ipl;
209
    ipl_t ipl;
210
 
210
 
211
    if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
211
    if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
212
        printf("Invalid breakpoint number.\n");
212
        printf("Invalid breakpoint number.\n");
213
        return 0;
213
        return 0;
214
    }
214
    }
215
    ipl = interrupts_disable();
215
    ipl = interrupts_disable();
216
    spinlock_lock(&bkpoint_lock);
216
    spinlock_lock(&bkpoint_lock);
217
 
217
 
218
    cur = &breakpoints[argv->intval];
218
    cur = &breakpoints[argv->intval];
219
    if (!cur->address) {
219
    if (!cur->address) {
220
        printf("Breakpoint does not exist.\n");
220
        printf("Breakpoint does not exist.\n");
221
        spinlock_unlock(&bkpoint_lock);
221
        spinlock_unlock(&bkpoint_lock);
222
        interrupts_restore(ipl);
222
        interrupts_restore(ipl);
223
        return 0;
223
        return 0;
224
    }
224
    }
225
    if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
225
    if ((cur->flags & BKPOINT_INPROG) && (cur->flags & BKPOINT_ONESHOT)) {
226
        printf("Cannot remove one-shot breakpoint in-progress\n");
226
        printf("Cannot remove one-shot breakpoint in-progress\n");
227
        spinlock_unlock(&bkpoint_lock);
227
        spinlock_unlock(&bkpoint_lock);
228
        interrupts_restore(ipl);
228
        interrupts_restore(ipl);
229
        return 0;
229
        return 0;
230
    }
230
    }
231
    ((uint32_t *)cur->address)[0] = cur->instruction;
231
    ((uint32_t *)cur->address)[0] = cur->instruction;
232
    ((uint32_t *)cur->address)[1] = cur->nextinstruction;
232
    ((uint32_t *)cur->address)[1] = cur->nextinstruction;
233
 
233
 
234
    cur->address = NULL;
234
    cur->address = NULL;
235
 
235
 
236
    spinlock_unlock(&bkpoint_lock);
236
    spinlock_unlock(&bkpoint_lock);
237
    interrupts_restore(ipl);
237
    interrupts_restore(ipl);
238
    return 1;
238
    return 1;
239
}
239
}
240
 
240
 
241
/** Print table of active breakpoints */
241
/** Print table of active breakpoints */
242
int cmd_print_breakpoints(cmd_arg_t *argv)
242
int cmd_print_breakpoints(cmd_arg_t *argv)
243
{
243
{
244
    int i;
244
    unsigned int i;
245
    char *symbol;
245
    char *symbol;
246
 
246
   
247
    printf("Breakpoint table.\n");
247
    printf("#  Count Address    INPROG ONESHOT FUNCCALL In symbol\n");
-
 
248
    printf("-- ----- ---------- ------ ------- -------- ---------\n");
-
 
249
   
248
    for (i=0; i < BKPOINTS_MAX; i++)
250
    for (i = 0; i < BKPOINTS_MAX; i++)
249
        if (breakpoints[i].address) {
251
        if (breakpoints[i].address) {
250
            symbol = get_symtab_entry(breakpoints[i].address);
252
            symbol = get_symtab_entry(breakpoints[i].address);
-
 
253
           
251
            printf("%d. %p in %s\n",i,
254
            printf("%-2u %-5d %#10zx %-6s %-7s %-8s %s\n", i,
252
                   breakpoints[i].address, symbol);
-
 
253
            printf("     Count(%d) ", breakpoints[i].counter);
255
                breakpoints[i].counter, breakpoints[i].address,
254
            if (breakpoints[i].flags & BKPOINT_INPROG)
256
                ((breakpoints[i].flags & BKPOINT_INPROG) ? "true" : "false"),
255
                printf("INPROG ");
-
 
256
            if (breakpoints[i].flags & BKPOINT_ONESHOT)
257
                ((breakpoints[i].flags & BKPOINT_ONESHOT) ? "true" : "false"),
257
                printf("ONESHOT ");
-
 
258
            if (breakpoints[i].flags & BKPOINT_FUNCCALL)
258
                ((breakpoints[i].flags & BKPOINT_FUNCCALL) ? "true" : "false"),
259
                printf("FUNCCALL ");
-
 
260
            printf("\n");
259
                symbol);
261
        }
260
        }
262
    return 1;
261
    return 1;
263
}
262
}
264
 
263
 
265
/** Initialize debugger */
264
/** Initialize debugger */
266
void debugger_init()
265
void debugger_init()
267
{
266
{
268
    int i;
267
    int i;
269
 
268
 
270
    for (i=0; i<BKPOINTS_MAX; i++)
269
    for (i=0; i<BKPOINTS_MAX; i++)
271
        breakpoints[i].address = NULL;
270
        breakpoints[i].address = NULL;
272
   
271
   
273
    cmd_initialize(&bkpts_info);
272
    cmd_initialize(&bkpts_info);
274
    if (!cmd_register(&bkpts_info))
273
    if (!cmd_register(&bkpts_info))
275
        panic("could not register command %s\n", bkpts_info.name);
274
        panic("could not register command %s\n", bkpts_info.name);
276
 
275
 
277
    cmd_initialize(&delbkpt_info);
276
    cmd_initialize(&delbkpt_info);
278
    if (!cmd_register(&delbkpt_info))
277
    if (!cmd_register(&delbkpt_info))
279
        panic("could not register command %s\n", delbkpt_info.name);
278
        panic("could not register command %s\n", delbkpt_info.name);
280
 
279
 
281
    cmd_initialize(&addbkpt_info);
280
    cmd_initialize(&addbkpt_info);
282
    if (!cmd_register(&addbkpt_info))
281
    if (!cmd_register(&addbkpt_info))
283
        panic("could not register command %s\n", addbkpt_info.name);
282
        panic("could not register command %s\n", addbkpt_info.name);
284
 
283
 
285
    cmd_initialize(&addbkpte_info);
284
    cmd_initialize(&addbkpte_info);
286
    if (!cmd_register(&addbkpte_info))
285
    if (!cmd_register(&addbkpte_info))
287
        panic("could not register command %s\n", addbkpte_info.name);
286
        panic("could not register command %s\n", addbkpte_info.name);
288
}
287
}
289
 
288
 
290
/** Handle breakpoint
289
/** Handle breakpoint
291
 *
290
 *
292
 * Find breakpoint in breakpoint table.
291
 * Find breakpoint in breakpoint table.
293
 * If found, call kconsole, set break on next instruction and reexecute.
292
 * If found, call kconsole, set break on next instruction and reexecute.
294
 * If we are on "next instruction", set it back on the first and reexecute.
293
 * If we are on "next instruction", set it back on the first and reexecute.
295
 * If breakpoint not found in breakpoint table, call kconsole and start
294
 * If breakpoint not found in breakpoint table, call kconsole and start
296
 * next instruction.
295
 * next instruction.
297
 */
296
 */
298
void debugger_bpoint(istate_t *istate)
297
void debugger_bpoint(istate_t *istate)
299
{
298
{
300
    bpinfo_t *cur = NULL;
299
    bpinfo_t *cur = NULL;
301
    uintptr_t fireaddr = istate->epc;
300
    uintptr_t fireaddr = istate->epc;
302
    int i;
301
    int i;
303
 
302
 
304
    /* test branch delay slot */
303
    /* test branch delay slot */
305
    if (cp0_cause_read() & 0x80000000)
304
    if (cp0_cause_read() & 0x80000000)
306
        panic("Breakpoint in branch delay slot not supported.\n");
305
        panic("Breakpoint in branch delay slot not supported.\n");
307
 
306
 
308
    spinlock_lock(&bkpoint_lock);
307
    spinlock_lock(&bkpoint_lock);
309
    for (i=0; i<BKPOINTS_MAX; i++) {
308
    for (i=0; i<BKPOINTS_MAX; i++) {
310
        /* Normal breakpoint */
309
        /* Normal breakpoint */
311
        if (fireaddr == breakpoints[i].address \
310
        if (fireaddr == breakpoints[i].address \
312
            && !(breakpoints[i].flags & BKPOINT_REINST)) {
311
            && !(breakpoints[i].flags & BKPOINT_REINST)) {
313
            cur = &breakpoints[i];
312
            cur = &breakpoints[i];
314
            break;
313
            break;
315
        }
314
        }
316
        /* Reinst only breakpoint */
315
        /* Reinst only breakpoint */
317
        if ((breakpoints[i].flags & BKPOINT_REINST) \
316
        if ((breakpoints[i].flags & BKPOINT_REINST) \
318
            && (fireaddr ==breakpoints[i].address+sizeof(unative_t))) {
317
            && (fireaddr ==breakpoints[i].address+sizeof(unative_t))) {
319
            cur = &breakpoints[i];
318
            cur = &breakpoints[i];
320
            break;
319
            break;
321
        }
320
        }
322
    }
321
    }
323
    if (cur) {
322
    if (cur) {
324
        if (cur->flags & BKPOINT_REINST) {
323
        if (cur->flags & BKPOINT_REINST) {
325
            /* Set breakpoint on first instruction */
324
            /* Set breakpoint on first instruction */
326
            ((uint32_t *)cur->address)[0] = 0x0d;
325
            ((uint32_t *)cur->address)[0] = 0x0d;
327
            /* Return back the second */
326
            /* Return back the second */
328
            ((uint32_t *)cur->address)[1] = cur->nextinstruction;
327
            ((uint32_t *)cur->address)[1] = cur->nextinstruction;
329
            cur->flags &= ~BKPOINT_REINST;
328
            cur->flags &= ~BKPOINT_REINST;
330
            spinlock_unlock(&bkpoint_lock);
329
            spinlock_unlock(&bkpoint_lock);
331
            return;
330
            return;
332
        }
331
        }
333
        if (cur->flags & BKPOINT_INPROG)
332
        if (cur->flags & BKPOINT_INPROG)
334
            printf("Warning: breakpoint recursion\n");
333
            printf("Warning: breakpoint recursion\n");
335
       
334
       
336
        if (!(cur->flags & BKPOINT_FUNCCALL))
335
        if (!(cur->flags & BKPOINT_FUNCCALL))
337
            printf("***Breakpoint %d: %p in %s.\n", i,
336
            printf("***Breakpoint %d: %p in %s.\n", i,
338
                   fireaddr, get_symtab_entry(istate->epc));
337
                   fireaddr, get_symtab_entry(istate->epc));
339
 
338
 
340
        /* Return first instruction back */
339
        /* Return first instruction back */
341
        ((uint32_t *)cur->address)[0] = cur->instruction;
340
        ((uint32_t *)cur->address)[0] = cur->instruction;
342
 
341
 
343
        if (! (cur->flags & BKPOINT_ONESHOT)) {
342
        if (! (cur->flags & BKPOINT_ONESHOT)) {
344
            /* Set Breakpoint on next instruction */
343
            /* Set Breakpoint on next instruction */
345
            ((uint32_t *)cur->address)[1] = 0x0d;
344
            ((uint32_t *)cur->address)[1] = 0x0d;
346
            cur->flags |= BKPOINT_REINST;
345
            cur->flags |= BKPOINT_REINST;
347
        }
346
        }
348
        cur->flags |= BKPOINT_INPROG;
347
        cur->flags |= BKPOINT_INPROG;
349
    } else {
348
    } else {
350
        printf("***Breakpoint %p in %s.\n", fireaddr,
349
        printf("***Breakpoint %p in %s.\n", fireaddr,
351
               get_symtab_entry(fireaddr));
350
               get_symtab_entry(fireaddr));
352
        /* Move on to next instruction */
351
        /* Move on to next instruction */
353
        istate->epc += 4;
352
        istate->epc += 4;
354
    }
353
    }
355
    if (cur)
354
    if (cur)
356
        cur->counter++;
355
        cur->counter++;
357
    if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
356
    if (cur && (cur->flags & BKPOINT_FUNCCALL)) {
358
        /* Allow zero bkfunc, just for counting */
357
        /* Allow zero bkfunc, just for counting */
359
        if (cur->bkfunc)
358
        if (cur->bkfunc)
360
            cur->bkfunc(cur, istate);
359
            cur->bkfunc(cur, istate);
361
    } else {
360
    } else {
362
        printf("***Type 'exit' to exit kconsole.\n");
361
        printf("***Type 'exit' to exit kconsole.\n");
363
        /* This disables all other processors - we are not SMP,
362
        /* This disables all other processors - we are not SMP,
364
         * actually this gets us to cpu_halt, if scheduler() is run
363
         * actually this gets us to cpu_halt, if scheduler() is run
365
         * - we generally do not want scheduler to be run from debug,
364
         * - we generally do not want scheduler to be run from debug,
366
         *   so this is a good idea
365
         *   so this is a good idea
367
         */
366
         */
368
        atomic_set(&haltstate,1);
367
        atomic_set(&haltstate,1);
369
        spinlock_unlock(&bkpoint_lock);
368
        spinlock_unlock(&bkpoint_lock);
370
 
369
 
371
        kconsole("debug");
370
        kconsole("debug");
372
 
371
 
373
        spinlock_lock(&bkpoint_lock);
372
        spinlock_lock(&bkpoint_lock);
374
        atomic_set(&haltstate,0);
373
        atomic_set(&haltstate,0);
375
    }
374
    }
376
    if (cur && cur->address == fireaddr && (cur->flags & BKPOINT_INPROG)) {
375
    if (cur && cur->address == fireaddr && (cur->flags & BKPOINT_INPROG)) {
377
        /* Remove one-shot breakpoint */
376
        /* Remove one-shot breakpoint */
378
        if ((cur->flags & BKPOINT_ONESHOT))
377
        if ((cur->flags & BKPOINT_ONESHOT))
379
            cur->address = NULL;
378
            cur->address = NULL;
380
        /* Remove in-progress flag */
379
        /* Remove in-progress flag */
381
        cur->flags &= ~BKPOINT_INPROG;
380
        cur->flags &= ~BKPOINT_INPROG;
382
    }
381
    }
383
    spinlock_unlock(&bkpoint_lock);
382
    spinlock_unlock(&bkpoint_lock);
384
}
383
}
385
 
384
 
386
/** @}
385
/** @}
387
 */
386
 */
388
 
387