freebsd-dev/sys/ddb/db_run.c

377 lines
8.3 KiB
C
Raw Normal View History

/*-
1993-06-12 14:58:17 +00:00
* Mach Operating System
* Copyright (c) 1991,1990 Carnegie Mellon University
* All Rights Reserved.
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
1995-05-30 08:16:23 +00:00
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
1993-06-12 14:58:17 +00:00
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Carnegie Mellon requests users of this software to return to
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*/
/*
* Author: David B. Golub, Carnegie Mellon University
* Date: 7/90
*/
/*
* Commands to run process.
*/
2003-06-10 22:09:23 +00:00
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/kdb.h>
#include <sys/proc.h>
#include <machine/kdb.h>
#include <machine/pcb.h>
1995-12-10 19:08:32 +00:00
#include <vm/vm.h>
1993-06-12 14:58:17 +00:00
1995-12-10 19:08:32 +00:00
#include <ddb/ddb.h>
1993-06-12 14:58:17 +00:00
#include <ddb/db_break.h>
#include <ddb/db_access.h>
1995-12-10 13:32:43 +00:00
static int db_run_mode;
1993-06-12 14:58:17 +00:00
#define STEP_NONE 0
#define STEP_ONCE 1
#define STEP_RETURN 2
#define STEP_CALLT 3
#define STEP_CONTINUE 4
#define STEP_INVISIBLE 5
1993-06-12 14:58:17 +00:00
#define STEP_COUNT 6
static bool db_sstep_print;
1995-12-10 13:32:43 +00:00
static int db_loop_count;
static int db_call_depth;
1993-06-12 14:58:17 +00:00
int db_inst_count;
int db_load_count;
int db_store_count;
#ifndef db_set_single_step
void db_set_single_step(void);
1993-06-12 14:58:17 +00:00
#endif
#ifndef db_clear_single_step
void db_clear_single_step(void);
#endif
#ifdef SOFTWARE_SSTEP
db_breakpoint_t db_not_taken_bkpt = 0;
db_breakpoint_t db_taken_bkpt = 0;
#endif
bool
db_stop_at_pc(bool *is_breakpoint)
1993-06-12 14:58:17 +00:00
{
2015-05-23 14:59:27 +00:00
db_addr_t pc;
db_breakpoint_t bkpt;
1993-06-12 14:58:17 +00:00
pc = PC_REGS();
#ifdef SOFTWARE_SSTEP
if ((db_not_taken_bkpt != 0 && pc == db_not_taken_bkpt->address)
|| (db_taken_bkpt != 0 && pc == db_taken_bkpt->address))
*is_breakpoint = false;
#endif
db_clear_single_step();
1993-06-12 14:58:17 +00:00
db_clear_breakpoints();
db_clear_watchpoints();
#ifdef FIXUP_PC_AFTER_BREAK
if (*is_breakpoint) {
/*
* Breakpoint trap. Fix up the PC if the
* machine requires it.
*/
FIXUP_PC_AFTER_BREAK
pc = PC_REGS();
1993-06-12 14:58:17 +00:00
}
#endif
/*
* Now check for a breakpoint at this address.
*/
bkpt = db_find_breakpoint_here(pc);
if (bkpt) {
if (--bkpt->count == 0) {
bkpt->count = bkpt->init_count;
*is_breakpoint = true;
return (true); /* stop here */
1993-06-12 14:58:17 +00:00
}
} else if (*is_breakpoint) {
#ifdef BKPT_SKIP
BKPT_SKIP;
#endif
1993-06-12 14:58:17 +00:00
}
1995-05-30 08:16:23 +00:00
*is_breakpoint = false;
1993-06-12 14:58:17 +00:00
if (db_run_mode == STEP_INVISIBLE) {
db_run_mode = STEP_CONTINUE;
return (false); /* continue */
1993-06-12 14:58:17 +00:00
}
if (db_run_mode == STEP_COUNT) {
return (false); /* continue */
1993-06-12 14:58:17 +00:00
}
if (db_run_mode == STEP_ONCE) {
if (--db_loop_count > 0) {
if (db_sstep_print) {
db_printf("\t\t");
db_print_loc_and_inst(pc);
db_printf("\n");
}
return (false); /* continue */
1993-06-12 14:58:17 +00:00
}
}
if (db_run_mode == STEP_RETURN) {
/* continue until matching return */
db_expr_t ins;
1993-06-12 14:58:17 +00:00
ins = db_get_value(pc, sizeof(int), false);
1993-06-12 14:58:17 +00:00
if (!inst_trap_return(ins) &&
(!inst_return(ins) || --db_call_depth != 0)) {
if (db_sstep_print) {
if (inst_call(ins) || inst_return(ins)) {
2015-05-23 14:59:27 +00:00
int i;
1993-06-12 14:58:17 +00:00
db_printf("[after %6d] ", db_inst_count);
for (i = db_call_depth; --i > 0; )
db_printf(" ");
db_print_loc_and_inst(pc);
db_printf("\n");
}
}
if (inst_call(ins))
db_call_depth++;
return (false); /* continue */
1993-06-12 14:58:17 +00:00
}
}
if (db_run_mode == STEP_CALLT) {
/* continue until call or return */
db_expr_t ins;
1993-06-12 14:58:17 +00:00
ins = db_get_value(pc, sizeof(int), false);
1993-06-12 14:58:17 +00:00
if (!inst_call(ins) &&
!inst_return(ins) &&
!inst_trap_return(ins)) {
return (false); /* continue */
1993-06-12 14:58:17 +00:00
}
}
db_run_mode = STEP_NONE;
return (true);
1993-06-12 14:58:17 +00:00
}
void
db_restart_at_pc(bool watchpt)
1993-06-12 14:58:17 +00:00
{
2015-05-23 14:59:27 +00:00
db_addr_t pc = PC_REGS();
1993-06-12 14:58:17 +00:00
if ((db_run_mode == STEP_COUNT) ||
(db_run_mode == STEP_RETURN) ||
(db_run_mode == STEP_CALLT)) {
/*
* We are about to execute this instruction,
* so count it now.
*/
#ifdef SOFTWARE_SSTEP
db_expr_t ins =
#endif
db_get_value(pc, sizeof(int), false);
1993-06-12 14:58:17 +00:00
db_inst_count++;
db_load_count += inst_load(ins);
db_store_count += inst_store(ins);
#ifdef SOFTWARE_SSTEP
/* XXX works on mips, but... */
if (inst_branch(ins) || inst_call(ins)) {
ins = db_get_value(next_instr_address(pc,1),
sizeof(int), false);
1993-06-12 14:58:17 +00:00
db_inst_count++;
db_load_count += inst_load(ins);
db_store_count += inst_store(ins);
}
2001-08-15 03:38:49 +00:00
#endif /* SOFTWARE_SSTEP */
1993-06-12 14:58:17 +00:00
}
if (db_run_mode == STEP_CONTINUE) {
if (watchpt || db_find_breakpoint_here(pc)) {
/*
* Step over breakpoint/watchpoint.
*/
db_run_mode = STEP_INVISIBLE;
db_set_single_step();
1993-06-12 14:58:17 +00:00
} else {
db_set_breakpoints();
db_set_watchpoints();
}
} else {
db_set_single_step();
1993-06-12 14:58:17 +00:00
}
}
#ifdef SOFTWARE_SSTEP
/*
* Software implementation of single-stepping.
* If your machine does not have a trace mode
* similar to the vax or sun ones you can use
* this implementation, done for the mips.
* Just define the above conditional and provide
* the functions/macros defined below.
*
* extern bool
1993-06-12 14:58:17 +00:00
* inst_branch(), returns true if the instruction might branch
* extern unsigned
* branch_taken(), return the address the instruction might
* branch to
* db_getreg_val(); return the value of a user register,
* as indicated in the hardware instruction
* encoding, e.g. 8 for r8
1995-05-30 08:16:23 +00:00
*
1993-06-12 14:58:17 +00:00
* next_instr_address(pc,bd) returns the address of the first
* instruction following the one at "pc",
* which is either in the taken path of
* the branch (bd==1) or not. This is
* for machines (mips) with branch delays.
*
* A single-step may involve at most 2 breakpoints -
* one for branch-not-taken and one for branch taken.
* If one of these addresses does not already have a breakpoint,
* we allocate a breakpoint and save it here.
* These breakpoints are deleted on return.
1995-05-30 08:16:23 +00:00
*/
1993-06-12 14:58:17 +00:00
void
db_set_single_step(void)
1993-06-12 14:58:17 +00:00
{
db_addr_t pc = PC_REGS(), brpc;
unsigned inst;
1993-06-12 14:58:17 +00:00
/*
* User was stopped at pc, e.g. the instruction
* at pc was not executed.
*/
inst = db_get_value(pc, sizeof(int), false);
if (inst_branch(inst) || inst_call(inst) || inst_return(inst)) {
brpc = branch_taken(inst, pc);
if (brpc != pc) { /* self-branches are hopeless */
db_taken_bkpt = db_set_temp_breakpoint(brpc);
}
pc = next_instr_address(pc, 1);
1993-06-12 14:58:17 +00:00
}
pc = next_instr_address(pc, 0);
1993-06-12 14:58:17 +00:00
db_not_taken_bkpt = db_set_temp_breakpoint(pc);
}
void
db_clear_single_step(void)
1993-06-12 14:58:17 +00:00
{
if (db_not_taken_bkpt != 0) {
db_delete_temp_breakpoint(db_not_taken_bkpt);
db_not_taken_bkpt = 0;
1993-06-12 14:58:17 +00:00
}
if (db_taken_bkpt != 0) {
db_delete_temp_breakpoint(db_taken_bkpt);
db_taken_bkpt = 0;
}
1993-06-12 14:58:17 +00:00
}
2001-08-15 03:38:49 +00:00
#endif /* SOFTWARE_SSTEP */
1993-06-12 14:58:17 +00:00
extern int db_cmd_loop_done;
/* single-step */
/*ARGSUSED*/
void
db_single_step_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
1993-06-12 14:58:17 +00:00
{
bool print = false;
1993-06-12 14:58:17 +00:00
if (count == -1)
count = 1;
if (modif[0] == 'p')
print = true;
1993-06-12 14:58:17 +00:00
db_run_mode = STEP_ONCE;
db_loop_count = count;
db_sstep_print = print;
db_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
/* trace and print until call/return */
/*ARGSUSED*/
void
db_trace_until_call_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
char *modif)
1993-06-12 14:58:17 +00:00
{
bool print = false;
1993-06-12 14:58:17 +00:00
if (modif[0] == 'p')
print = true;
1993-06-12 14:58:17 +00:00
db_run_mode = STEP_CALLT;
db_sstep_print = print;
db_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
/*ARGSUSED*/
void
db_trace_until_matching_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
char *modif)
1993-06-12 14:58:17 +00:00
{
bool print = false;
1993-06-12 14:58:17 +00:00
if (modif[0] == 'p')
print = true;
1993-06-12 14:58:17 +00:00
db_run_mode = STEP_RETURN;
db_call_depth = 1;
db_sstep_print = print;
db_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}
/* continue */
/*ARGSUSED*/
void
db_continue_cmd(db_expr_t addr, bool have_addr, db_expr_t count, char *modif)
1993-06-12 14:58:17 +00:00
{
if (modif[0] == 'c')
db_run_mode = STEP_COUNT;
else
db_run_mode = STEP_CONTINUE;
db_inst_count = 0;
db_load_count = 0;
db_store_count = 0;
db_cmd_loop_done = 1;
}