Home | History | Annotate | Line # | Download | only in ddb
db_run.c revision 1.13
      1  1.13        pk /*	$NetBSD: db_run.c,v 1.13 1997/12/10 23:09:31 pk Exp $	*/
      2   1.5       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Mach Operating System
      5  1.11   thorpej  * Copyright (c) 1993-1990 Carnegie Mellon University
      6   1.1       cgd  * All Rights Reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1       cgd  * notice and this permission notice appear in all copies of the
     11   1.1       cgd  * software, derivative works or modified versions, and any portions
     12   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13   1.1       cgd  *
     14   1.1       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
     15   1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17   1.1       cgd  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19   1.1       cgd  *
     20   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1       cgd  *  School of Computer Science
     22   1.1       cgd  *  Carnegie Mellon University
     23   1.1       cgd  *  Pittsburgh PA 15213-3890
     24   1.1       cgd  *
     25   1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26   1.1       cgd  * rights to redistribute these changes.
     27   1.2       cgd  *
     28   1.1       cgd  * 	Author: David B. Golub, Carnegie Mellon University
     29   1.1       cgd  *	Date:	7/90
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.1       cgd /*
     33   1.1       cgd  * Commands to run process.
     34   1.1       cgd  */
     35   1.7   mycroft #include <sys/param.h>
     36   1.7   mycroft #include <sys/proc.h>
     37   1.7   mycroft 
     38   1.7   mycroft #include <machine/db_machdep.h>
     39   1.7   mycroft 
     40   1.6   mycroft #include <ddb/db_run.h>
     41   1.1       cgd #include <ddb/db_lex.h>
     42   1.1       cgd #include <ddb/db_break.h>
     43   1.1       cgd #include <ddb/db_access.h>
     44   1.8  christos #include <ddb/db_watch.h>
     45   1.8  christos #include <ddb/db_output.h>
     46   1.8  christos #include <ddb/db_sym.h>
     47   1.8  christos #include <ddb/db_extern.h>
     48   1.1       cgd 
     49   1.1       cgd int	db_run_mode;
     50   1.1       cgd #define	STEP_NONE	0
     51   1.1       cgd #define	STEP_ONCE	1
     52   1.1       cgd #define	STEP_RETURN	2
     53   1.1       cgd #define	STEP_CALLT	3
     54   1.1       cgd #define	STEP_CONTINUE	4
     55   1.1       cgd #define STEP_INVISIBLE	5
     56   1.1       cgd #define	STEP_COUNT	6
     57   1.1       cgd 
     58   1.1       cgd boolean_t	db_sstep_print;
     59   1.1       cgd int		db_loop_count;
     60   1.1       cgd int		db_call_depth;
     61   1.1       cgd 
     62  1.13        pk #ifdef	SOFTWARE_SSTEP
     63  1.13        pk db_breakpoint_t	db_not_taken_bkpt = 0;
     64  1.13        pk db_breakpoint_t	db_taken_bkpt = 0;
     65  1.13        pk #endif
     66  1.13        pk 
     67   1.1       cgd boolean_t
     68   1.6   mycroft db_stop_at_pc(regs, is_breakpoint)
     69   1.6   mycroft 	db_regs_t *regs;
     70   1.1       cgd 	boolean_t	*is_breakpoint;
     71   1.1       cgd {
     72   1.1       cgd 	register db_addr_t	pc;
     73   1.1       cgd 	register db_breakpoint_t bkpt;
     74   1.1       cgd 
     75  1.13        pk 	pc = PC_REGS(regs);
     76  1.13        pk 
     77  1.13        pk #ifdef	SOFTWARE_SSTEP
     78  1.13        pk 	/*
     79  1.13        pk 	 * If we stopped at one of the single-step breakpoints,
     80  1.13        pk 	 * say it's not really a breakpoint so that
     81  1.13        pk 	 * we don't skip over the real instruction.
     82  1.13        pk 	 */
     83  1.13        pk 	if ((db_taken_bkpt != NULL && db_taken_bkpt->address == pc) ||
     84  1.13        pk 	    (db_not_taken_bkpt != NULL && db_not_taken_bkpt->address == pc))
     85  1.13        pk 		*is_breakpoint = FALSE;
     86  1.13        pk #endif
     87  1.13        pk 
     88   1.6   mycroft 	db_clear_single_step(regs);
     89   1.1       cgd 	db_clear_breakpoints();
     90   1.1       cgd 	db_clear_watchpoints();
     91   1.1       cgd 
     92   1.1       cgd #ifdef	FIXUP_PC_AFTER_BREAK
     93   1.1       cgd 	if (*is_breakpoint) {
     94   1.1       cgd 	    /*
     95   1.1       cgd 	     * Breakpoint trap.  Fix up the PC if the
     96   1.1       cgd 	     * machine requires it.
     97   1.1       cgd 	     */
     98  1.10       gwr 	    FIXUP_PC_AFTER_BREAK(regs);
     99   1.6   mycroft 	    pc = PC_REGS(regs);
    100   1.1       cgd 	}
    101   1.1       cgd #endif
    102   1.1       cgd 
    103   1.1       cgd 	/*
    104   1.1       cgd 	 * Now check for a breakpoint at this address.
    105   1.1       cgd 	 */
    106   1.1       cgd 	bkpt = db_find_breakpoint_here(pc);
    107   1.1       cgd 	if (bkpt) {
    108   1.1       cgd 	    if (--bkpt->count == 0) {
    109   1.1       cgd 		bkpt->count = bkpt->init_count;
    110   1.1       cgd 		*is_breakpoint = TRUE;
    111   1.1       cgd 		return (TRUE);	/* stop here */
    112   1.1       cgd 	    }
    113   1.1       cgd 	} else if (*is_breakpoint) {
    114  1.12        pk #ifdef PC_ADVANCE
    115  1.12        pk 		PC_ADVANCE(regs);
    116  1.12        pk #else
    117   1.6   mycroft 		PC_REGS(regs) += BKPT_SIZE;
    118  1.12        pk #endif
    119   1.1       cgd 	}
    120   1.1       cgd 
    121   1.1       cgd 	*is_breakpoint = FALSE;
    122   1.1       cgd 
    123   1.1       cgd 	if (db_run_mode == STEP_INVISIBLE) {
    124   1.1       cgd 	    db_run_mode = STEP_CONTINUE;
    125   1.1       cgd 	    return (FALSE);	/* continue */
    126   1.1       cgd 	}
    127   1.1       cgd 	if (db_run_mode == STEP_COUNT) {
    128   1.1       cgd 	    return (FALSE); /* continue */
    129   1.1       cgd 	}
    130   1.1       cgd 	if (db_run_mode == STEP_ONCE) {
    131   1.1       cgd 	    if (--db_loop_count > 0) {
    132   1.1       cgd 		if (db_sstep_print) {
    133   1.1       cgd 		    db_printf("\t\t");
    134   1.1       cgd 		    db_print_loc_and_inst(pc);
    135   1.1       cgd 		    db_printf("\n");
    136   1.1       cgd 		}
    137   1.1       cgd 		return (FALSE);	/* continue */
    138   1.1       cgd 	    }
    139   1.1       cgd 	}
    140   1.1       cgd 	if (db_run_mode == STEP_RETURN) {
    141   1.1       cgd 	    db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
    142   1.1       cgd 
    143   1.1       cgd 	    /* continue until matching return */
    144   1.1       cgd 
    145   1.1       cgd 	    if (!inst_trap_return(ins) &&
    146   1.1       cgd 		(!inst_return(ins) || --db_call_depth != 0)) {
    147   1.1       cgd 		if (db_sstep_print) {
    148   1.1       cgd 		    if (inst_call(ins) || inst_return(ins)) {
    149   1.1       cgd 			register int i;
    150   1.1       cgd 
    151   1.1       cgd 			db_printf("[after %6d]     ", db_inst_count);
    152   1.1       cgd 			for (i = db_call_depth; --i > 0; )
    153   1.1       cgd 			    db_printf("  ");
    154   1.1       cgd 			db_print_loc_and_inst(pc);
    155   1.1       cgd 			db_printf("\n");
    156   1.1       cgd 		    }
    157   1.1       cgd 		}
    158   1.1       cgd 		if (inst_call(ins))
    159   1.1       cgd 		    db_call_depth++;
    160   1.1       cgd 		return (FALSE);	/* continue */
    161   1.1       cgd 	    }
    162   1.1       cgd 	}
    163   1.1       cgd 	if (db_run_mode == STEP_CALLT) {
    164   1.1       cgd 	    db_expr_t ins = db_get_value(pc, sizeof(int), FALSE);
    165   1.1       cgd 
    166   1.1       cgd 	    /* continue until call or return */
    167   1.1       cgd 
    168   1.1       cgd 	    if (!inst_call(ins) &&
    169   1.1       cgd 		!inst_return(ins) &&
    170   1.1       cgd 		!inst_trap_return(ins)) {
    171   1.1       cgd 		return (FALSE);	/* continue */
    172   1.1       cgd 	    }
    173   1.1       cgd 	}
    174   1.1       cgd 	db_run_mode = STEP_NONE;
    175   1.1       cgd 	return (TRUE);
    176   1.1       cgd }
    177   1.1       cgd 
    178   1.1       cgd void
    179   1.6   mycroft db_restart_at_pc(regs, watchpt)
    180   1.6   mycroft 	db_regs_t *regs;
    181   1.1       cgd 	boolean_t watchpt;
    182   1.1       cgd {
    183   1.6   mycroft 	register db_addr_t pc = PC_REGS(regs);
    184   1.1       cgd 
    185   1.1       cgd 	if ((db_run_mode == STEP_COUNT) ||
    186   1.1       cgd 	    (db_run_mode == STEP_RETURN) ||
    187   1.1       cgd 	    (db_run_mode == STEP_CALLT)) {
    188   1.1       cgd 	    db_expr_t		ins;
    189   1.1       cgd 
    190   1.1       cgd 	    /*
    191   1.1       cgd 	     * We are about to execute this instruction,
    192   1.1       cgd 	     * so count it now.
    193   1.1       cgd 	     */
    194   1.1       cgd 	    ins = db_get_value(pc, sizeof(int), FALSE);
    195   1.1       cgd 	    db_inst_count++;
    196   1.1       cgd 	    db_load_count += inst_load(ins);
    197   1.1       cgd 	    db_store_count += inst_store(ins);
    198  1.11   thorpej 
    199  1.11   thorpej #ifdef SOFTWARE_SSTEP
    200  1.11   thorpej 	    /*
    201  1.11   thorpej 	     * Account for instructions in delay slots.
    202  1.11   thorpej 	     */
    203  1.11   thorpej 	    {
    204  1.11   thorpej 		db_addr_t brpc;
    205  1.11   thorpej 
    206  1.11   thorpej 		brpc = next_instr_address(pc, TRUE);
    207  1.11   thorpej 		if ((brpc != pc) && (inst_branch(ins) || inst_call(ins))) {
    208  1.11   thorpej 		    ins = db_get_value(brpc, sizeof(int), FALSE);
    209  1.11   thorpej 		    db_inst_count++;
    210  1.11   thorpej 		    db_load_count += inst_load(ins);
    211  1.11   thorpej 		    db_store_count += inst_store(ins);
    212  1.11   thorpej 		}
    213   1.1       cgd 	    }
    214   1.9       cgd #endif
    215   1.1       cgd 	}
    216   1.1       cgd 
    217   1.1       cgd 	if (db_run_mode == STEP_CONTINUE) {
    218   1.1       cgd 	    if (watchpt || db_find_breakpoint_here(pc)) {
    219   1.1       cgd 		/*
    220   1.1       cgd 		 * Step over breakpoint/watchpoint.
    221   1.1       cgd 		 */
    222   1.1       cgd 		db_run_mode = STEP_INVISIBLE;
    223   1.6   mycroft 		db_set_single_step(regs);
    224   1.1       cgd 	    } else {
    225   1.1       cgd 		db_set_breakpoints();
    226   1.1       cgd 		db_set_watchpoints();
    227   1.1       cgd 	    }
    228   1.1       cgd 	} else {
    229   1.6   mycroft 	    db_set_single_step(regs);
    230   1.1       cgd 	}
    231   1.1       cgd }
    232   1.1       cgd 
    233   1.1       cgd void
    234   1.1       cgd db_single_step(regs)
    235   1.1       cgd 	db_regs_t *regs;
    236   1.1       cgd {
    237   1.1       cgd 	if (db_run_mode == STEP_CONTINUE) {
    238   1.1       cgd 	    db_run_mode = STEP_INVISIBLE;
    239   1.1       cgd 	    db_set_single_step(regs);
    240   1.1       cgd 	}
    241   1.1       cgd }
    242   1.1       cgd 
    243  1.11   thorpej #ifdef SOFTWARE_SSTEP
    244   1.1       cgd /*
    245   1.1       cgd  *	Software implementation of single-stepping.
    246   1.1       cgd  *	If your machine does not have a trace mode
    247   1.1       cgd  *	similar to the vax or sun ones you can use
    248   1.1       cgd  *	this implementation, done for the mips.
    249   1.1       cgd  *	Just define the above conditional and provide
    250   1.1       cgd  *	the functions/macros defined below.
    251   1.1       cgd  *
    252  1.11   thorpej  * boolean_t inst_branch(int inst)
    253  1.11   thorpej  * boolean_t inst_call(int inst)
    254  1.11   thorpej  *	returns TRUE if the instruction might branch
    255  1.11   thorpej  *
    256  1.11   thorpej  * boolean_t inst_unconditional_flow_transfer(int inst)
    257  1.11   thorpej  *	returns TRUE if the instruction is an unconditional
    258  1.11   thorpej  *	transter of flow (i.e. unconditional branch)
    259  1.11   thorpej  *
    260  1.11   thorpej  * db_addr_t branch_taken(int inst, db_addr_t pc, db_regs_t *regs)
    261  1.11   thorpej  *	returns the target address of the branch
    262  1.11   thorpej  *
    263  1.11   thorpej  * db_addr_t next_instr_address(db_addr_t pc, boolean_t bd)
    264  1.11   thorpej  *	returns the address of the first instruction following the
    265  1.11   thorpej  *	one at "pc", which is either in the taken path of the branch
    266  1.11   thorpej  *	(bd == TRUE) or not.  This is for machines (e.g. mips) with
    267  1.11   thorpej  *	branch delays.
    268   1.1       cgd  *
    269   1.1       cgd  *	A single-step may involve at most 2 breakpoints -
    270   1.1       cgd  *	one for branch-not-taken and one for branch taken.
    271   1.1       cgd  *	If one of these addresses does not already have a breakpoint,
    272   1.1       cgd  *	we allocate a breakpoint and save it here.
    273   1.1       cgd  *	These breakpoints are deleted on return.
    274   1.1       cgd  */
    275   1.1       cgd 
    276   1.1       cgd void
    277   1.1       cgd db_set_single_step(regs)
    278   1.1       cgd 	register db_regs_t *regs;
    279   1.1       cgd {
    280  1.13        pk 	db_addr_t pc = PC_REGS(regs), brpc = pc;
    281  1.11   thorpej 	boolean_t unconditional;
    282  1.11   thorpej 	unsigned int inst;
    283   1.1       cgd 
    284   1.1       cgd 	/*
    285   1.1       cgd 	 *	User was stopped at pc, e.g. the instruction
    286   1.1       cgd 	 *	at pc was not executed.
    287   1.1       cgd 	 */
    288   1.1       cgd 	inst = db_get_value(pc, sizeof(int), FALSE);
    289   1.1       cgd 	if (inst_branch(inst) || inst_call(inst)) {
    290  1.11   thorpej 		brpc = branch_taken(inst, pc, regs);
    291  1.11   thorpej 		if (brpc != pc) {	/* self-branches are hopeless */
    292  1.11   thorpej 			db_taken_bkpt = db_set_temp_breakpoint(brpc);
    293  1.11   thorpej 		} else
    294  1.11   thorpej 			db_taken_bkpt = 0;
    295  1.11   thorpej 		pc = next_instr_address(pc, TRUE);
    296  1.11   thorpej 	}
    297  1.11   thorpej 
    298  1.11   thorpej 	/*
    299  1.11   thorpej 	 *	Check if this control flow instruction is an
    300  1.11   thorpej 	 *	unconditional transfer.
    301  1.11   thorpej 	 */
    302  1.11   thorpej 	unconditional = inst_unconditional_flow_transfer(inst);
    303  1.11   thorpej 
    304  1.11   thorpej 	pc = next_instr_address(pc, FALSE);
    305   1.1       cgd 
    306  1.11   thorpej 	/*
    307  1.11   thorpej 	 *	We only set the sequential breakpoint if previous
    308  1.11   thorpej 	 *	instruction was not an unconditional change of flow
    309  1.11   thorpej 	 *	control.  If the previous instruction is an
    310  1.11   thorpej 	 *	unconditional change of flow control, setting a
    311  1.11   thorpej 	 *	breakpoint in the next sequential location may set
    312  1.11   thorpej 	 *	a breakpoint in data or in another routine, which
    313  1.11   thorpej 	 *	could screw up in either the program or the debugger.
    314  1.11   thorpej 	 *	(Consider, for instance, that the next sequential
    315  1.11   thorpej 	 *	instruction is the start of a routine needed by the
    316  1.11   thorpej 	 *	debugger.)
    317  1.13        pk 	 *
    318  1.13        pk 	 *	Also, don't set both the taken and not-taken breakpoints
    319  1.13        pk 	 *	in the same place even if the MD code would otherwise
    320  1.13        pk 	 *	have us do so.
    321  1.11   thorpej 	 */
    322  1.13        pk 	if (unconditional == FALSE &&
    323  1.13        pk 	    db_find_breakpoint_here(pc) == 0 &&
    324  1.13        pk 	    pc != brpc)
    325  1.11   thorpej 		db_not_taken_bkpt = db_set_temp_breakpoint(pc);
    326  1.11   thorpej 	else
    327  1.11   thorpej 		db_not_taken_bkpt = 0;
    328   1.1       cgd }
    329   1.1       cgd 
    330   1.1       cgd void
    331   1.1       cgd db_clear_single_step(regs)
    332   1.1       cgd 	db_regs_t *regs;
    333   1.1       cgd {
    334   1.1       cgd 
    335   1.1       cgd 	if (db_taken_bkpt != 0) {
    336   1.1       cgd 	    db_delete_temp_breakpoint(db_taken_bkpt);
    337   1.1       cgd 	    db_taken_bkpt = 0;
    338   1.1       cgd 	}
    339   1.1       cgd 	if (db_not_taken_bkpt != 0) {
    340   1.1       cgd 	    db_delete_temp_breakpoint(db_not_taken_bkpt);
    341   1.1       cgd 	    db_not_taken_bkpt = 0;
    342   1.1       cgd 	}
    343   1.1       cgd }
    344   1.1       cgd 
    345  1.11   thorpej #endif /* SOFTWARE_SSTEP */
    346   1.1       cgd 
    347   1.1       cgd extern int	db_cmd_loop_done;
    348   1.1       cgd 
    349   1.1       cgd /* single-step */
    350   1.1       cgd /*ARGSUSED*/
    351   1.1       cgd void
    352   1.1       cgd db_single_step_cmd(addr, have_addr, count, modif)
    353   1.1       cgd 	db_expr_t	addr;
    354   1.1       cgd 	int		have_addr;
    355   1.1       cgd 	db_expr_t	count;
    356   1.1       cgd 	char *		modif;
    357   1.1       cgd {
    358   1.1       cgd 	boolean_t	print = FALSE;
    359   1.1       cgd 
    360   1.1       cgd 	if (count == -1)
    361   1.1       cgd 	    count = 1;
    362   1.1       cgd 
    363   1.1       cgd 	if (modif[0] == 'p')
    364   1.1       cgd 	    print = TRUE;
    365   1.1       cgd 
    366   1.1       cgd 	db_run_mode = STEP_ONCE;
    367   1.1       cgd 	db_loop_count = count;
    368   1.1       cgd 	db_sstep_print = print;
    369   1.1       cgd 	db_inst_count = 0;
    370   1.1       cgd 	db_load_count = 0;
    371   1.1       cgd 	db_store_count = 0;
    372   1.1       cgd 
    373   1.1       cgd 	db_cmd_loop_done = 1;
    374   1.1       cgd }
    375   1.1       cgd 
    376   1.1       cgd /* trace and print until call/return */
    377   1.1       cgd /*ARGSUSED*/
    378   1.1       cgd void
    379   1.1       cgd db_trace_until_call_cmd(addr, have_addr, count, modif)
    380   1.1       cgd 	db_expr_t	addr;
    381   1.1       cgd 	int		have_addr;
    382   1.1       cgd 	db_expr_t	count;
    383   1.1       cgd 	char *		modif;
    384   1.1       cgd {
    385   1.1       cgd 	boolean_t	print = FALSE;
    386   1.1       cgd 
    387   1.1       cgd 	if (modif[0] == 'p')
    388   1.1       cgd 	    print = TRUE;
    389   1.1       cgd 
    390   1.1       cgd 	db_run_mode = STEP_CALLT;
    391   1.1       cgd 	db_sstep_print = print;
    392   1.1       cgd 	db_inst_count = 0;
    393   1.1       cgd 	db_load_count = 0;
    394   1.1       cgd 	db_store_count = 0;
    395   1.1       cgd 
    396   1.1       cgd 	db_cmd_loop_done = 1;
    397   1.1       cgd }
    398   1.1       cgd 
    399   1.1       cgd /*ARGSUSED*/
    400   1.1       cgd void
    401   1.1       cgd db_trace_until_matching_cmd(addr, have_addr, count, modif)
    402   1.1       cgd 	db_expr_t	addr;
    403   1.1       cgd 	int		have_addr;
    404   1.1       cgd 	db_expr_t	count;
    405   1.1       cgd 	char *		modif;
    406   1.1       cgd {
    407   1.1       cgd 	boolean_t	print = FALSE;
    408   1.1       cgd 
    409   1.1       cgd 	if (modif[0] == 'p')
    410   1.1       cgd 	    print = TRUE;
    411   1.1       cgd 
    412   1.1       cgd 	db_run_mode = STEP_RETURN;
    413   1.1       cgd 	db_call_depth = 1;
    414   1.1       cgd 	db_sstep_print = print;
    415   1.1       cgd 	db_inst_count = 0;
    416   1.1       cgd 	db_load_count = 0;
    417   1.1       cgd 	db_store_count = 0;
    418   1.1       cgd 
    419   1.1       cgd 	db_cmd_loop_done = 1;
    420   1.1       cgd }
    421   1.1       cgd 
    422   1.1       cgd /* continue */
    423   1.1       cgd /*ARGSUSED*/
    424   1.1       cgd void
    425   1.1       cgd db_continue_cmd(addr, have_addr, count, modif)
    426   1.1       cgd 	db_expr_t	addr;
    427   1.1       cgd 	int		have_addr;
    428   1.1       cgd 	db_expr_t	count;
    429   1.1       cgd 	char *		modif;
    430   1.1       cgd {
    431   1.1       cgd 	if (modif[0] == 'c')
    432   1.1       cgd 	    db_run_mode = STEP_COUNT;
    433   1.1       cgd 	else
    434   1.1       cgd 	    db_run_mode = STEP_CONTINUE;
    435   1.1       cgd 	db_inst_count = 0;
    436   1.1       cgd 	db_load_count = 0;
    437   1.1       cgd 	db_store_count = 0;
    438   1.1       cgd 
    439   1.1       cgd 	db_cmd_loop_done = 1;
    440   1.1       cgd }
    441