#include <mach/boolean.h>
#include <machine/db_machdep.h>
#include <ddb/db_lex.h>
#include <ddb/db_break.h>
#include <ddb/db_access.h>
#include <ddb/db_run.h>
#include <ddb/db_cond.h>
#include <ddb/db_examine.h>
#include <ddb/db_output.h>
#include <ddb/db_watch.h>
#include <kern/misc_protos.h>
#include <kern/debug.h>
boolean_t db_sstep_print;
int db_loop_count;
int db_call_depth;
int db_inst_count;
int db_last_inst_count;
int db_load_count;
int db_store_count;
int db_max_inst_count = 1000;
#ifndef db_set_single_step
void db_set_task_single_step(
register db_regs_t *regs,
task_t task);
#else
#define db_set_task_single_step(regs,task) db_set_single_step(regs)
#endif
#ifndef db_clear_single_step
void db_clear_task_single_step(
db_regs_t *regs,
task_t task);
#else
#define db_clear_task_single_step(regs,task) db_clear_single_step(regs)
#endif
extern jmp_buf_t *db_recover;
boolean_t db_step_again(void);
boolean_t
db_stop_at_pc(
boolean_t *is_breakpoint,
task_t task,
task_t space)
{
register db_addr_t pc;
register db_thread_breakpoint_t bkpt;
db_clear_task_single_step(DDB_REGS, space);
db_clear_breakpoints();
db_clear_watchpoints();
pc = PC_REGS(DDB_REGS);
#ifdef FIXUP_PC_AFTER_BREAK
if (*is_breakpoint) {
FIXUP_PC_AFTER_BREAK
pc = PC_REGS(DDB_REGS);
}
#endif
bkpt = db_find_thread_breakpoint_here(space, pc);
if (bkpt) {
if (db_cond_check(bkpt)) {
*is_breakpoint = TRUE;
return (TRUE);
}
}
*is_breakpoint = FALSE;
if (db_run_mode == STEP_INVISIBLE) {
db_run_mode = STEP_CONTINUE;
return (FALSE);
}
if (db_run_mode == STEP_COUNT) {
return (FALSE);
}
if (db_run_mode == STEP_ONCE) {
if (--db_loop_count > 0) {
if (db_sstep_print) {
db_print_loc_and_inst(pc, task);
}
return (FALSE);
}
}
if (db_run_mode == STEP_RETURN) {
jmp_buf_t *prev;
jmp_buf_t db_jmpbuf;
db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space);
prev = db_recover;
if (_setjmp(db_recover = &db_jmpbuf) == 0) {
if (!inst_trap_return(ins) &&
(!inst_return(ins) || --db_call_depth != 0)) {
if (db_sstep_print) {
if (inst_call(ins) || inst_return(ins)) {
register int i;
db_printf("[after %6d /%4d] ",
db_inst_count,
db_inst_count - db_last_inst_count);
db_last_inst_count = db_inst_count;
for (i = db_call_depth; --i > 0; )
db_printf(" ");
db_print_loc_and_inst(pc, task);
db_printf("\n");
}
}
if (inst_call(ins))
db_call_depth++;
db_recover = prev;
if (db_step_again())
return (FALSE);
}
}
db_recover = prev;
}
if (db_run_mode == STEP_CALLT) {
db_expr_t ins = db_get_task_value(pc, sizeof(int), FALSE, space);
if (!inst_call(ins) &&
!inst_return(ins) &&
!inst_trap_return(ins)) {
if (db_step_again())
return (FALSE);
}
}
if (db_find_breakpoint_here(space, pc))
return(FALSE);
db_run_mode = STEP_NONE;
return (TRUE);
}
void
db_restart_at_pc(
boolean_t watchpt,
task_t task)
{
register db_addr_t pc = PC_REGS(DDB_REGS), brpc;
if ((db_run_mode == STEP_COUNT) ||
(db_run_mode == STEP_RETURN) ||
(db_run_mode == STEP_CALLT)) {
db_expr_t ins;
ins = db_get_task_value(pc, sizeof(int), FALSE, task);
db_inst_count++;
db_load_count += db_inst_load(ins);
db_store_count += db_inst_store(ins);
#ifdef SOFTWARE_SSTEP
brpc = next_instr_address(pc,1,task);
if ((brpc != pc) && (inst_branch(ins) || inst_call(ins))) {
ins = db_get_task_value(brpc, sizeof(int), FALSE, task);
db_inst_count++;
db_load_count += db_inst_load(ins);
db_store_count += db_inst_store(ins);
}
#endif
}
if (db_run_mode == STEP_CONTINUE) {
if (watchpt || db_find_breakpoint_here(task, pc)) {
db_run_mode = STEP_INVISIBLE;
db_set_task_single_step(DDB_REGS, task);
} else {
db_set_breakpoints();
db_set_watchpoints();
}
} else {
db_set_task_single_step(DDB_REGS, task);
}
}
boolean_t
db_step_again(void)
{
if (db_inst_count && !(db_inst_count%db_max_inst_count)) {
char c;
db_printf("%d instructions, continue ? (y/n) ",
db_inst_count);
c = cngetc();
db_printf("\n");
if(c == 'n')
return(FALSE);
}
return(TRUE);
}
void
db_single_step(
db_regs_t *regs,
task_t task)
{
if (db_run_mode == STEP_CONTINUE) {
db_run_mode = STEP_INVISIBLE;
db_set_task_single_step(regs, task);
}
}
#ifdef SOFTWARE_SSTEP
db_breakpoint_t db_not_taken_bkpt = 0;
db_breakpoint_t db_taken_bkpt = 0;
db_breakpoint_t
db_find_temp_breakpoint(
task_t task,
db_addr_t addr)
{
if (db_taken_bkpt && (db_taken_bkpt->address == addr) &&
db_taken_bkpt->task == task)
return db_taken_bkpt;
if (db_not_taken_bkpt && (db_not_taken_bkpt->address == addr) &&
db_not_taken_bkpt->task == task)
return db_not_taken_bkpt;
return 0;
}
void
db_set_task_single_step(
register db_regs_t *regs,
task_t task)
{
db_addr_t pc = PC_REGS(regs), brpc;
register unsigned int inst;
register boolean_t unconditional;
inst = db_get_task_value(pc, sizeof(int), FALSE, task);
if (inst_branch(inst) || inst_call(inst)) {
extern db_expr_t getreg_val();
brpc = branch_taken(inst, pc, getreg_val, (unsigned char*)regs);
if (brpc != pc) {
db_taken_bkpt = db_set_temp_breakpoint(task, brpc);
} else
db_taken_bkpt = 0;
pc = next_instr_address(pc,1,task);
} else
pc = next_instr_address(pc,0,task);
unconditional = inst_unconditional_flow_transfer(inst);
if (!unconditional && db_find_breakpoint_here(task, pc) == 0 &&
(db_taken_bkpt == 0 || db_taken_bkpt->address != pc)) {
db_not_taken_bkpt = db_set_temp_breakpoint(task, pc);
} else
db_not_taken_bkpt = 0;
}
void
db_clear_task_single_step(
db_regs_t *regs,
task_t task)
{
if (db_taken_bkpt != 0) {
db_delete_temp_breakpoint(task, db_taken_bkpt);
db_taken_bkpt = 0;
}
if (db_not_taken_bkpt != 0) {
db_delete_temp_breakpoint(task, db_not_taken_bkpt);
db_not_taken_bkpt = 0;
}
}
#endif
extern int db_cmd_loop_done;
void
db_single_step_cmd(
db_expr_t addr,
int have_addr,
db_expr_t count,
char * modif)
{
boolean_t print = FALSE;
if (count == -1)
count = 1;
if (modif[0] == 'p')
print = TRUE;
db_run_mode = STEP_ONCE;
db_loop_count = count;
db_sstep_print = print;
db_inst_count = 0;
db_last_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
void
db_trace_until_call_cmd(
db_expr_t addr,
int have_addr,
db_expr_t count,
char * modif)
{
boolean_t print = FALSE;
if (modif[0] == 'p')
print = TRUE;
db_run_mode = STEP_CALLT;
db_sstep_print = print;
db_inst_count = 0;
db_last_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
void
db_trace_until_matching_cmd(
db_expr_t addr,
int have_addr,
db_expr_t count,
char * modif)
{
boolean_t print = FALSE;
if (modif[0] == 'p')
print = TRUE;
db_run_mode = STEP_RETURN;
db_call_depth = 1;
db_sstep_print = print;
db_inst_count = 0;
db_last_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
void
db_continue_cmd(
db_expr_t addr,
int have_addr,
db_expr_t count,
char * modif)
{
#if 0
if (modif[0] == 'c')
db_run_mode = STEP_COUNT;
else
db_run_mode = STEP_CONTINUE;
#else
db_run_mode = STEP_CONTINUE;
#endif
db_inst_count = 0;
db_last_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
void
db_continue_gdb(
db_expr_t addr,
int have_addr,
db_expr_t count,
char * modif)
{
#if defined(__ppc__)
db_to_gdb();
#endif
db_run_mode = STEP_CONTINUE;
db_inst_count = 0;
db_last_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
boolean_t
db_in_single_step(void)
{
return(db_run_mode != STEP_NONE && db_run_mode != STEP_CONTINUE);
}