37,6 → 37,7 |
#include <arch/cpu.h> |
#include <debug.h> |
#include <func.h> |
#include <smp/ipi.h> |
|
typedef struct { |
__address address; /**< Breakpoint address */ |
55,6 → 56,8 |
.argc = 0, |
}; |
|
#ifndef CONFIG_DEBUG_AS_WATCHPOINT |
|
static int cmd_del_breakpoint(cmd_arg_t *argv); |
static cmd_arg_t del_argv = { |
.type = ARG_TYPE_INT |
90,6 → 93,7 |
.argv = &addw_argv |
}; |
|
#endif |
|
/** Print table of active breakpoints */ |
int cmd_print_breakpoints(cmd_arg_t *argv) |
109,6 → 113,58 |
return 1; |
} |
|
/* Setup DR register according to table */ |
static void setup_dr(int curidx) |
{ |
__native dr7; |
bpinfo_t *cur = &breakpoints[curidx]; |
int flags = breakpoints[curidx].flags; |
|
/* Disable breakpoint in DR7 */ |
dr7 = read_dr7(); |
dr7 &= ~(0x2 << (curidx*2)); |
|
if (cur->address) { /* Setup DR register */ |
/* Set breakpoint to debug registers */ |
switch (curidx) { |
case 0: |
write_dr0(cur->address); |
break; |
case 1: |
write_dr1(cur->address); |
break; |
case 2: |
write_dr2(cur->address); |
break; |
case 3: |
write_dr3(cur->address); |
break; |
} |
/* Set type to requested breakpoint & length*/ |
dr7 &= ~ (0x3 << (16 + 4*curidx)); |
dr7 &= ~ (0x3 << (18 + 4*curidx)); |
if ((flags & BKPOINT_INSTR)) { |
; |
} else { |
if (sizeof(int) == 4) |
dr7 |= ((__native) 0x3) << (18 + 4*curidx); |
else /* 8 */ |
dr7 |= ((__native) 0x2) << (18 + 4*curidx); |
|
if ((flags & BKPOINT_WRITE)) |
dr7 |= ((__native) 0x1) << (16 + 4*curidx); |
else if ((flags & BKPOINT_READ_WRITE)) |
dr7 |= ((__native) 0x3) << (16 + 4*curidx); |
} |
|
/* Enable global breakpoint */ |
dr7 |= 0x2 << (curidx*2); |
|
write_dr7(dr7); |
|
} |
} |
|
/** Enable hardware breakpoint |
* |
* |
116,13 → 172,11 |
* @param flags Type of breakpoint (EXECUTE, WRITE) |
* @return Debug slot on success, -1 - no available HW breakpoint |
*/ |
int breakpoint_add(void * where, int flags) |
int breakpoint_add(void * where, int flags, int curidx) |
{ |
bpinfo_t *cur = NULL; |
int curidx; |
ipl_t ipl; |
int i; |
__native dr7; |
bpinfo_t *cur; |
|
ASSERT( flags & (BKPOINT_INSTR | BKPOINT_WRITE | BKPOINT_READ_WRITE)); |
|
129,65 → 183,36 |
ipl = interrupts_disable(); |
spinlock_lock(&bkpoint_lock); |
|
/* Find free space in slots */ |
for (i=0; i<BKPOINTS_MAX; i++) |
if (!breakpoints[i].address) { |
cur = &breakpoints[i]; |
curidx = i; |
break; |
if (curidx == -1) { |
/* Find free space in slots */ |
for (i=0; i<BKPOINTS_MAX; i++) |
if (!breakpoints[i].address) { |
curidx = i; |
break; |
} |
if (curidx == -1) { |
/* Too many breakpoints */ |
spinlock_unlock(&bkpoint_lock); |
interrupts_restore(ipl); |
return -1; |
} |
if (!cur) { |
/* Too many breakpoints */ |
spinlock_unlock(&bkpoint_lock); |
interrupts_restore(ipl); |
return -1; |
} |
cur = &breakpoints[curidx]; |
|
cur->address = (__address) where; |
cur->flags = flags; |
cur->counter = 0; |
|
/* Set breakpoint to debug registers */ |
switch (curidx) { |
case 0: |
write_dr0(cur->address); |
break; |
case 1: |
write_dr1(cur->address); |
break; |
case 2: |
write_dr2(cur->address); |
break; |
case 3: |
write_dr3(cur->address); |
break; |
} |
dr7 = read_dr7(); |
/* Set type to requested breakpoint & length*/ |
dr7 &= ~ (0x3 << (16 + 4*curidx)); |
dr7 &= ~ (0x3 << (18 + 4*curidx)); |
if ((flags & BKPOINT_INSTR)) { |
printf("Instr breakpoint\n"); |
; |
} else { |
if (sizeof(int) == 4) |
dr7 |= 0x3 << (18 + 4*curidx); |
else /* 8 */ |
dr7 |= 0x2 << (18 + 4*curidx); |
|
if ((flags & BKPOINT_WRITE)) |
dr7 |= 0x1 << (16 + 4*curidx); |
else if ((flags & BKPOINT_READ_WRITE)) |
dr7 |= 0x3 << (16 + 4*curidx); |
} |
setup_dr(curidx); |
|
/* Enable global breakpoint */ |
dr7 |= 0x2 << (curidx*2); |
|
write_dr7(dr7); |
|
spinlock_unlock(&bkpoint_lock); |
interrupts_restore(ipl); |
|
/* Send IPI */ |
#ifdef CONFIG_SMP |
// ipi_broadcast(VECTOR_DEBUG_IPI); |
#endif |
|
return curidx; |
} |
|
221,34 → 246,10 |
atomic_set(&haltstate,0); |
} |
|
static void debug_exception(int n, istate_t *istate) |
{ |
__native dr6; |
int i; |
|
/* Set RF to restart the instruction */ |
#ifdef amd64 |
istate->rflags |= RFLAGS_RF; |
#else |
istate->eflags |= EFLAGS_RF; |
#endif |
|
dr6 = read_dr6(); |
for (i=0; i < BKPOINTS_MAX; i++) { |
if (dr6 & (1 << i)) { |
dr6 &= ~ (1 << i); |
write_dr6(dr6); |
|
handle_exception(i, istate); |
} |
} |
} |
|
void breakpoint_del(int slot) |
{ |
bpinfo_t *cur; |
ipl_t ipl; |
__native dr7; |
|
ipl = interrupts_disable(); |
spinlock_lock(&bkpoint_lock); |
262,15 → 263,17 |
|
cur->address = NULL; |
|
/* Disable breakpoint in DR7 */ |
dr7 = read_dr7(); |
dr7 &= ~(0x2 << (slot*2)); |
write_dr7(dr7); |
setup_dr(slot); |
|
spinlock_unlock(&bkpoint_lock); |
interrupts_restore(ipl); |
#ifdef CONFIG_SMP |
// ipi_broadcast(VECTOR_DEBUG_IPI); |
#endif |
} |
|
#ifndef CONFIG_DEBUG_AS_WATCHPOINT |
|
/** Remove breakpoint from table */ |
int cmd_del_breakpoint(cmd_arg_t *argv) |
{ |
286,6 → 289,7 |
static int cmd_add_breakpoint(cmd_arg_t *argv) |
{ |
int flags; |
int id; |
|
if (argv == &add_argv) { |
flags = BKPOINT_INSTR; |
293,12 → 297,51 |
flags = BKPOINT_WRITE; |
} |
printf("Adding breakpoint on address: %p\n", argv->intval); |
if (breakpoint_add((void *)argv->intval, flags)) |
id = breakpoint_add((void *)argv->intval, flags, -1); |
if (id < 0) |
printf("Add breakpoint failed.\n"); |
else |
printf("Added breakpoint %d.\n", id); |
|
return 1; |
} |
#endif |
|
static void debug_exception(int n, istate_t *istate) |
{ |
__native dr6; |
int i; |
|
/* Set RF to restart the instruction */ |
#ifdef amd64 |
istate->rflags |= RFLAGS_RF; |
#else |
istate->eflags |= EFLAGS_RF; |
#endif |
|
dr6 = read_dr6(); |
for (i=0; i < BKPOINTS_MAX; i++) { |
if (dr6 & (1 << i)) { |
dr6 &= ~ (1 << i); |
write_dr6(dr6); |
|
handle_exception(i, istate); |
} |
} |
} |
|
#ifdef CONFIG_SMP |
static void debug_ipi(int n, istate_t *istate) |
{ |
int i; |
|
spinlock_lock(&bkpoint_lock); |
for (i=0; i < BKPOINTS_MAX; i++) |
setup_dr(i); |
spinlock_unlock(&bkpoint_lock); |
} |
#endif |
|
/** Initialize debugger */ |
void debugger_init() |
{ |
311,6 → 354,7 |
if (!cmd_register(&bkpts_info)) |
panic("could not register command %s\n", bkpts_info.name); |
|
#ifndef CONFIG_DEBUG_AS_WATCHPOINT |
cmd_initialize(&delbkpt_info); |
if (!cmd_register(&delbkpt_info)) |
panic("could not register command %s\n", delbkpt_info.name); |
322,7 → 366,12 |
cmd_initialize(&addwatchp_info); |
if (!cmd_register(&addwatchp_info)) |
panic("could not register command %s\n", addwatchp_info.name); |
#endif |
|
exc_register(VECTOR_DEBUG, "debugger", |
debug_exception); |
#ifdef CONFIG_SMP |
exc_register(VECTOR_DEBUG_IPI, "debugger_smp", |
debug_ipi); |
#endif |
} |