Subversion Repositories HelenOS

Rev

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

Rev 1072 Rev 1074
1
/*
1
/*
2
 * Copyright (C) 2006 Ondrej Palkovsky
2
 * Copyright (C) 2006 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
#include <arch/debugger.h>
29
#include <arch/debugger.h>
30
#include <console/kconsole.h>
30
#include <console/kconsole.h>
31
#include <console/cmd.h>
31
#include <console/cmd.h>
32
#include <symtab.h>
32
#include <symtab.h>
33
#include <print.h>
33
#include <print.h>
34
#include <panic.h>
34
#include <panic.h>
35
#include <interrupt.h>
35
#include <interrupt.h>
36
#include <arch/asm.h>
36
#include <arch/asm.h>
37
#include <arch/cpu.h>
37
#include <arch/cpu.h>
38
#include <debug.h>
38
#include <debug.h>
39
#include <func.h>
39
#include <func.h>
40
 
40
 
41
typedef struct  {
41
typedef struct  {
42
    __address address;      /**< Breakpoint address */
42
    __address address;      /**< Breakpoint address */
43
    int flags;              /**< Flags regarding breakpoint */
43
    int flags;              /**< Flags regarding breakpoint */
44
    int counter;            /**< How many times the exception occured */
44
    int counter;            /**< How many times the exception occured */
45
} bpinfo_t;
45
} bpinfo_t;
46
 
46
 
47
static bpinfo_t breakpoints[BKPOINTS_MAX];
47
static bpinfo_t breakpoints[BKPOINTS_MAX];
48
SPINLOCK_INITIALIZE(bkpoint_lock);
48
SPINLOCK_INITIALIZE(bkpoint_lock);
49
 
49
 
50
static int cmd_print_breakpoints(cmd_arg_t *argv);
50
static int cmd_print_breakpoints(cmd_arg_t *argv);
51
static cmd_info_t bkpts_info = {
51
static cmd_info_t bkpts_info = {
52
    .name = "bkpts",
52
    .name = "bkpts",
53
    .description = "Print breakpoint table.",
53
    .description = "Print breakpoint table.",
54
    .func = cmd_print_breakpoints,
54
    .func = cmd_print_breakpoints,
55
    .argc = 0,
55
    .argc = 0,
56
};
56
};
57
 
57
 
58
static int cmd_del_breakpoint(cmd_arg_t *argv);
58
static int cmd_del_breakpoint(cmd_arg_t *argv);
59
static cmd_arg_t del_argv = {
59
static cmd_arg_t del_argv = {
60
    .type = ARG_TYPE_INT
60
    .type = ARG_TYPE_INT
61
};
61
};
62
static cmd_info_t delbkpt_info = {
62
static cmd_info_t delbkpt_info = {
63
    .name = "delbkpt",
63
    .name = "delbkpt",
64
    .description = "delbkpt <number> - Delete breakpoint.",
64
    .description = "delbkpt <number> - Delete breakpoint.",
65
    .func = cmd_del_breakpoint,
65
    .func = cmd_del_breakpoint,
66
    .argc = 1,
66
    .argc = 1,
67
    .argv = &del_argv
67
    .argv = &del_argv
68
};
68
};
69
 
69
 
70
static int cmd_add_breakpoint(cmd_arg_t *argv);
70
static int cmd_add_breakpoint(cmd_arg_t *argv);
71
static cmd_arg_t add_argv = {
71
static cmd_arg_t add_argv = {
72
    .type = ARG_TYPE_INT
72
    .type = ARG_TYPE_INT
73
};
73
};
74
static cmd_info_t addbkpt_info = {
74
static cmd_info_t addbkpt_info = {
75
    .name = "addbkpt",
75
    .name = "addbkpt",
76
    .description = "addbkpt <&symbol> - new breakpoint.",
76
    .description = "addbkpt <&symbol> - new breakpoint.",
77
    .func = cmd_add_breakpoint,
77
    .func = cmd_add_breakpoint,
78
    .argc = 1,
78
    .argc = 1,
79
    .argv = &add_argv
79
    .argv = &add_argv
80
};
80
};
81
 
81
 
82
static cmd_arg_t addw_argv = {
82
static cmd_arg_t addw_argv = {
83
    .type = ARG_TYPE_INT
83
    .type = ARG_TYPE_INT
84
};
84
};
85
static cmd_info_t addwatchp_info = {
85
static cmd_info_t addwatchp_info = {
86
    .name = "addwatchp",
86
    .name = "addwatchp",
87
    .description = "addbwatchp <&symbol> - new write watchpoint.",
87
    .description = "addbwatchp <&symbol> - new write watchpoint.",
88
    .func = cmd_add_breakpoint,
88
    .func = cmd_add_breakpoint,
89
    .argc = 1,
89
    .argc = 1,
90
    .argv = &addw_argv
90
    .argv = &addw_argv
91
};
91
};
92
 
92
 
93
 
93
 
94
/** Print table of active breakpoints */
94
/** Print table of active breakpoints */
95
int cmd_print_breakpoints(cmd_arg_t *argv)
95
int cmd_print_breakpoints(cmd_arg_t *argv)
96
{
96
{
97
    int i;
97
    int i;
98
    char *symbol;
98
    char *symbol;
99
 
99
 
100
    printf("Breakpoint table.\n");
100
    printf("Breakpoint table.\n");
101
    for (i=0; i < BKPOINTS_MAX; i++)
101
    for (i=0; i < BKPOINTS_MAX; i++)
102
        if (breakpoints[i].address) {
102
        if (breakpoints[i].address) {
103
            symbol = get_symtab_entry(breakpoints[i].address);
103
            symbol = get_symtab_entry(breakpoints[i].address);
104
            printf("%d. 0x%p in %s\n",i,
104
            printf("%d. 0x%p in %s\n",i,
105
                   breakpoints[i].address, symbol);
105
                   breakpoints[i].address, symbol);
106
            printf("     Count(%d) ", breakpoints[i].counter);
106
            printf("     Count(%d) ", breakpoints[i].counter);
107
            printf("\n");
107
            printf("\n");
108
        }
108
        }
109
    return 1;
109
    return 1;
110
}
110
}
111
 
111
 
112
/** Enable hardware breakpoint
112
/** Enable hardware breakpoint
113
 *
113
 *
114
 *
114
 *
115
 * @param where Address of HW breakpoint
115
 * @param where Address of HW breakpoint
116
 * @param flags Type of breakpoint (EXECUTE, WRITE)
116
 * @param flags Type of breakpoint (EXECUTE, WRITE)
117
 * @return Debug slot on success, -1 - no available HW breakpoint
117
 * @return Debug slot on success, -1 - no available HW breakpoint
118
 */
118
 */
119
int breakpoint_add(void * where, int flags)
119
int breakpoint_add(void * where, int flags)
120
{
120
{
121
    bpinfo_t *cur = NULL;
121
    bpinfo_t *cur = NULL;
122
    int curidx;
122
    int curidx;
123
    ipl_t ipl;
123
    ipl_t ipl;
124
    int i;
124
    int i;
125
    __native dr7;
125
    __native dr7;
126
 
126
 
127
    ASSERT( flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
127
    ASSERT( flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE));
128
 
128
 
129
    ipl = interrupts_disable();
129
    ipl = interrupts_disable();
130
    spinlock_lock(&bkpoint_lock);
130
    spinlock_lock(&bkpoint_lock);
131
   
131
   
132
    /* Find free space in slots */
132
    /* Find free space in slots */
133
    for (i=0; i<BKPOINTS_MAX; i++)
133
    for (i=0; i<BKPOINTS_MAX; i++)
134
        if (!breakpoints[i].address) {
134
        if (!breakpoints[i].address) {
135
            cur = &breakpoints[i];
135
            cur = &breakpoints[i];
136
            curidx = i;
136
            curidx = i;
137
            break;
137
            break;
138
        }
138
        }
139
    if (!cur) {
139
    if (!cur) {
140
        /* Too many breakpoints */
140
        /* Too many breakpoints */
141
        spinlock_unlock(&bkpoint_lock);
141
        spinlock_unlock(&bkpoint_lock);
142
        interrupts_restore(ipl);
142
        interrupts_restore(ipl);
143
        return -1;
143
        return -1;
144
    }
144
    }
145
    cur->address = (__address) where;
145
    cur->address = (__address) where;
146
    cur->flags = flags;
146
    cur->flags = flags;
147
    cur->counter = 0;
147
    cur->counter = 0;
148
 
148
 
149
    /* Set breakpoint to debug registers */
149
    /* Set breakpoint to debug registers */
150
    switch (curidx) {
150
    switch (curidx) {
151
    case 0:
151
    case 0:
152
        write_dr0(cur->address);
152
        write_dr0(cur->address);
153
        break;
153
        break;
154
    case 1:
154
    case 1:
155
        write_dr1(cur->address);
155
        write_dr1(cur->address);
156
        break;
156
        break;
157
    case 2:
157
    case 2:
158
        write_dr2(cur->address);
158
        write_dr2(cur->address);
159
        break;
159
        break;
160
    case 3:
160
    case 3:
161
        write_dr3(cur->address);
161
        write_dr3(cur->address);
162
        break;
162
        break;
163
    }
163
    }
164
    dr7 = read_dr7();
164
    dr7 = read_dr7();
165
    /* Set type to requested breakpoint & length*/
165
    /* Set type to requested breakpoint & length*/
166
    dr7 &= ~ (0x3 << (16 + 4*curidx));
166
    dr7 &= ~ (0x3 << (16 + 4*curidx));
167
    dr7 &= ~ (0x3 << (18 + 4*curidx));
167
    dr7 &= ~ (0x3 << (18 + 4*curidx));
168
    if ((flags & BKPOINT_INSTR)) {
168
    if ((flags & BKPOINT_INSTR)) {
169
        printf("Instr breakpoint\n");
169
        printf("Instr breakpoint\n");
170
        ;
170
        ;
171
    } else {
171
    } else {
172
        if (sizeof(int) == 4)
172
        if (sizeof(int) == 4)
173
            dr7 |= 0x3 << (18 + 4*curidx);
173
            dr7 |= 0x3 << (18 + 4*curidx);
174
        else /* 8 */
174
        else /* 8 */
175
            dr7 |= 0x2 << (18 + 4*curidx);
175
            dr7 |= 0x2 << (18 + 4*curidx);
176
           
176
           
177
        if ((flags & BKPOINT_WRITE))
177
        if ((flags & BKPOINT_WRITE))
178
            dr7 |= 0x1 << (16 + 4*curidx);
178
            dr7 |= 0x1 << (16 + 4*curidx);
179
        else if ((flags & BKPOINT_READ_WRITE))
179
        else if ((flags & BKPOINT_READ_WRITE))
180
            dr7 |= 0x3 << (16 + 4*curidx);
180
            dr7 |= 0x3 << (16 + 4*curidx);
181
    }
181
    }
182
 
182
 
183
    /* Enable global breakpoint */
183
    /* Enable global breakpoint */
184
    dr7 |= 0x2 << (curidx*2);
184
    dr7 |= 0x2 << (curidx*2);
185
 
185
 
186
    write_dr7(dr7);
186
    write_dr7(dr7);
187
 
187
 
188
    spinlock_unlock(&bkpoint_lock);
188
    spinlock_unlock(&bkpoint_lock);
189
    interrupts_restore(ipl);
189
    interrupts_restore(ipl);
190
 
190
 
191
    return curidx;
191
    return curidx;
192
}
192
}
193
 
193
 
-
 
194
#ifdef amd64
-
 
195
# define getip(x)  ((x)->rip)
-
 
196
#else
-
 
197
# define getip(x)  ((x)->eip)
-
 
198
#endif
-
 
199
 
194
static void handle_exception(int slot, istate_t *istate)
200
static void handle_exception(int slot, istate_t *istate)
195
{
201
{
196
    ASSERT(breakpoints[slot].address);
202
    ASSERT(breakpoints[slot].address);
197
 
203
 
198
    /* Handle zero checker */
204
    /* Handle zero checker */
199
    if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
205
    if (! (breakpoints[slot].flags & BKPOINT_INSTR)) {
200
        if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
206
        if ((breakpoints[slot].flags & BKPOINT_CHECK_ZERO)) {
201
            if (*((__native *) breakpoints[slot].address) != 0)
207
            if (*((__native *) breakpoints[slot].address) != 0)
202
                return;
208
                return;
203
            printf("**** Found ZERO on address %P ****\n",
209
            printf("**** Found ZERO on address %P ****\n",
204
                   slot, breakpoints[slot].address);
210
                   slot, breakpoints[slot].address);
205
        } else {
211
        } else {
206
            printf("Data watchpoint - new data: %P\n",
212
            printf("Data watchpoint - new data: %P\n",
207
                   *((__native *) breakpoints[slot].address));
213
                   *((__native *) breakpoints[slot].address));
208
        }
214
        }
209
    }
215
    }
210
    printf("Reached breakpoint %d:%P(%s)\n", slot, istate->rip,
216
    printf("Reached breakpoint %d:%P(%s)\n", slot, getip(istate),
211
           get_symtab_entry(istate->rip));
217
           get_symtab_entry(getip(istate)));
212
    printf("***Type 'exit' to exit kconsole.\n");
218
    printf("***Type 'exit' to exit kconsole.\n");
213
    atomic_set(&haltstate,1);
219
    atomic_set(&haltstate,1);
214
    kconsole("debug");
220
    kconsole("debug");
215
    atomic_set(&haltstate,0);
221
    atomic_set(&haltstate,0);
216
}
222
}
217
 
223
 
218
static void debug_exception(int n, istate_t *istate)
224
static void debug_exception(int n, istate_t *istate)
219
{
225
{
220
    __native dr6;
226
    __native dr6;
221
    int i;
227
    int i;
222
   
228
   
223
    /* Set RF to restart the instruction  */
229
    /* Set RF to restart the instruction  */
-
 
230
#ifdef amd64       
224
    istate->rflags |= RFLAGS_RF;
231
    istate->rflags |= RFLAGS_RF;
-
 
232
#else
-
 
233
    istate->eflags |= EFLAGS_RF;
-
 
234
#endif
225
 
235
 
226
    dr6 = read_dr6();
236
    dr6 = read_dr6();
227
    for (i=0; i < BKPOINTS_MAX; i++) {
237
    for (i=0; i < BKPOINTS_MAX; i++) {
228
        if (dr6 & (1 << i)) {
238
        if (dr6 & (1 << i)) {
229
            dr6 &= ~ (1 << i);
239
            dr6 &= ~ (1 << i);
230
            write_dr6(dr6);
240
            write_dr6(dr6);
231
           
241
           
232
            handle_exception(i, istate);
242
            handle_exception(i, istate);
233
        }
243
        }
234
    }
244
    }
235
}
245
}
236
 
246
 
237
void breakpoint_del(int slot)
247
void breakpoint_del(int slot)
238
{
248
{
239
    bpinfo_t *cur;
249
    bpinfo_t *cur;
240
    ipl_t ipl;
250
    ipl_t ipl;
241
    __native dr7;
251
    __native dr7;
242
 
252
 
243
    ipl = interrupts_disable();
253
    ipl = interrupts_disable();
244
    spinlock_lock(&bkpoint_lock);
254
    spinlock_lock(&bkpoint_lock);
245
 
255
 
246
    cur = &breakpoints[slot];
256
    cur = &breakpoints[slot];
247
    if (!cur->address) {
257
    if (!cur->address) {
248
        spinlock_unlock(&bkpoint_lock);
258
        spinlock_unlock(&bkpoint_lock);
249
        interrupts_restore(ipl);
259
        interrupts_restore(ipl);
250
        return;
260
        return;
251
    }
261
    }
252
 
262
 
253
    cur->address = NULL;
263
    cur->address = NULL;
254
 
264
 
255
    /* Disable breakpoint in DR7 */
265
    /* Disable breakpoint in DR7 */
256
    dr7 = read_dr7();
266
    dr7 = read_dr7();
257
    dr7 &= ~(0x2 << (slot*2));
267
    dr7 &= ~(0x2 << (slot*2));
258
    write_dr7(dr7);
268
    write_dr7(dr7);
259
 
269
 
260
    spinlock_unlock(&bkpoint_lock);
270
    spinlock_unlock(&bkpoint_lock);
261
    interrupts_restore(ipl);
271
    interrupts_restore(ipl);
262
}
272
}
263
 
273
 
264
/** Remove breakpoint from table */
274
/** Remove breakpoint from table */
265
int cmd_del_breakpoint(cmd_arg_t *argv)
275
int cmd_del_breakpoint(cmd_arg_t *argv)
266
{
276
{
267
    if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
277
    if (argv->intval < 0 || argv->intval > BKPOINTS_MAX) {
268
        printf("Invalid breakpoint number.\n");
278
        printf("Invalid breakpoint number.\n");
269
        return 0;
279
        return 0;
270
    }
280
    }
271
    breakpoint_del(argv->intval);
281
    breakpoint_del(argv->intval);
272
    return 1;
282
    return 1;
273
}
283
}
274
 
284
 
275
/** Add new breakpoint to table */
285
/** Add new breakpoint to table */
276
static int cmd_add_breakpoint(cmd_arg_t *argv)
286
static int cmd_add_breakpoint(cmd_arg_t *argv)
277
{
287
{
278
    int flags;
288
    int flags;
279
 
289
 
280
    if (argv == &add_argv) {
290
    if (argv == &add_argv) {
281
        flags = BKPOINT_INSTR;
291
        flags = BKPOINT_INSTR;
282
    } else { /* addwatchp */
292
    } else { /* addwatchp */
283
        flags = BKPOINT_WRITE;
293
        flags = BKPOINT_WRITE;
284
    }
294
    }
285
    printf("Adding breakpoint on address: %p\n", argv->intval);
295
    printf("Adding breakpoint on address: %p\n", argv->intval);
286
    if (breakpoint_add((void *)argv->intval, flags))
296
    if (breakpoint_add((void *)argv->intval, flags))
287
        printf("Add breakpoint failed.\n");
297
        printf("Add breakpoint failed.\n");
288
   
298
   
289
    return 1;
299
    return 1;
290
}
300
}
291
 
301
 
292
/** Initialize debugger */
302
/** Initialize debugger */
293
void debugger_init()
303
void debugger_init()
294
{
304
{
295
    int i;
305
    int i;
296
 
306
 
297
    for (i=0; i<BKPOINTS_MAX; i++)
307
    for (i=0; i<BKPOINTS_MAX; i++)
298
        breakpoints[i].address = NULL;
308
        breakpoints[i].address = NULL;
299
   
309
   
300
    cmd_initialize(&bkpts_info);
310
    cmd_initialize(&bkpts_info);
301
    if (!cmd_register(&bkpts_info))
311
    if (!cmd_register(&bkpts_info))
302
        panic("could not register command %s\n", bkpts_info.name);
312
        panic("could not register command %s\n", bkpts_info.name);
303
 
313
 
304
    cmd_initialize(&delbkpt_info);
314
    cmd_initialize(&delbkpt_info);
305
    if (!cmd_register(&delbkpt_info))
315
    if (!cmd_register(&delbkpt_info))
306
        panic("could not register command %s\n", delbkpt_info.name);
316
        panic("could not register command %s\n", delbkpt_info.name);
307
 
317
 
308
    cmd_initialize(&addbkpt_info);
318
    cmd_initialize(&addbkpt_info);
309
    if (!cmd_register(&addbkpt_info))
319
    if (!cmd_register(&addbkpt_info))
310
        panic("could not register command %s\n", addbkpt_info.name);
320
        panic("could not register command %s\n", addbkpt_info.name);
311
 
321
 
312
    cmd_initialize(&addwatchp_info);
322
    cmd_initialize(&addwatchp_info);
313
    if (!cmd_register(&addwatchp_info))
323
    if (!cmd_register(&addwatchp_info))
314
        panic("could not register command %s\n", addwatchp_info.name);
324
        panic("could not register command %s\n", addwatchp_info.name);
315
   
325
   
316
    exc_register(VECTOR_DEBUG, "debugger",
326
    exc_register(VECTOR_DEBUG, "debugger",
317
             debug_exception);
327
             debug_exception);
318
}
328
}
319
 
329