Home | History | Annotate | Line # | Download | only in mips
db_trace.c revision 1.13
      1  1.13     soren /*	$NetBSD: db_trace.c,v 1.13 2000/05/27 02:13:12 soren Exp $	*/
      2   1.2     perry 
      3   1.8    simonb /*
      4   1.1  jonathan  * Mach Operating System
      5   1.1  jonathan  * Copyright (c) 1993-1987 Carnegie Mellon University
      6   1.1  jonathan  * All Rights Reserved.
      7   1.8    simonb  *
      8   1.1  jonathan  * Permission to use, copy, modify and distribute this software and its
      9   1.1  jonathan  * documentation is hereby granted, provided that both the copyright
     10   1.1  jonathan  * notice and this permission notice appear in all copies of the
     11   1.1  jonathan  * software, derivative works or modified versions, and any portions
     12   1.1  jonathan  * thereof, and that both notices appear in supporting documentation.
     13   1.8    simonb  *
     14   1.1  jonathan  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1  jonathan  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1  jonathan  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17   1.8    simonb  *
     18   1.1  jonathan  * Carnegie Mellon requests users of this software to return to
     19   1.8    simonb  *
     20   1.1  jonathan  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1  jonathan  *  School of Computer Science
     22   1.1  jonathan  *  Carnegie Mellon University
     23   1.1  jonathan  *  Pittsburgh PA 15213-3890
     24   1.8    simonb  *
     25   1.1  jonathan  * any improvements or extensions that they make and grant Carnegie Mellon
     26   1.1  jonathan  * the rights to redistribute these changes.
     27   1.1  jonathan  */
     28   1.1  jonathan 
     29   1.1  jonathan #include <sys/types.h>
     30  1.13     soren #include <sys/param.h>
     31   1.1  jonathan #include <vm/vm_param.h>		/* XXX boolean_t */
     32   1.1  jonathan 
     33   1.5    castor #include <mips/mips_opcode.h>
     34   1.5    castor 
     35   1.1  jonathan #include <machine/param.h>
     36   1.7    simonb #include <machine/db_machdep.h>
     37   1.1  jonathan #include <ddb/db_interface.h>
     38   1.1  jonathan #include <ddb/db_output.h>
     39   1.1  jonathan #include <ddb/db_variables.h>
     40   1.1  jonathan #include <ddb/db_sym.h>
     41   1.1  jonathan 
     42   1.9    simonb int __start __P((void));	/* lowest kernel code address */
     43   1.9    simonb vaddr_t getreg_val __P((db_expr_t regno));
     44   1.1  jonathan 
     45   1.1  jonathan #define REG_ARG(i)	(4+i)
     46   1.1  jonathan #define SAVES_RA(x)	isa_spill((x),31)
     47   1.1  jonathan 
     48   1.1  jonathan #define KERN_SAVE_REG_IDX(vp)	( \
     49   1.1  jonathan 	((vp)->valuep >= (int *)(&((struct mips_saved_state *)0)->s0) &&    \
     50   1.1  jonathan 	 (vp)->valuep <= (int *)(&((struct mips_saved_state *)0)->s7))?	    \
     51   1.1  jonathan 		vp->valuep - (int *)(&((struct mips_saved_state *)0)->s0):  \
     52   1.1  jonathan 	((vp)->valuep >= (int *)(&((struct mips_saved_state *)0)->sp) &&    \
     53   1.1  jonathan 	 (vp)->valuep <= (int *)(&((struct mips_saved_state *)0)->ra))?	    \
     54   1.1  jonathan 		((vp)->valuep-(int *)(&((struct mips_saved_state *)0)->sp)) + \
     55   1.1  jonathan 		 ((int *)(&((struct mips_kernel_state *)0)->sp) - (int *)0):  \
     56   1.1  jonathan 	 -1)
     57   1.1  jonathan 
     58   1.9    simonb db_sym_t localsym __P((db_sym_t sym, boolean_t isreg, int *lex_level));
     59   1.1  jonathan 
     60   1.1  jonathan /*
     61   1.1  jonathan  * Machine register set.
     62   1.1  jonathan  */
     63   1.1  jonathan struct mips_saved_state *db_cur_exc_frame = 0;
     64   1.1  jonathan 
     65   1.8    simonb /*
     66   1.1  jonathan  *  forward declarations
     67   1.1  jonathan  */
     68  1.10    simonb int print_exception_frame __P((struct mips_saved_state *fp,
     69   1.1  jonathan 			       unsigned epc));
     70   1.1  jonathan 
     71   1.1  jonathan /*XXX*/
     72   1.9    simonb void stacktrace_subr __P((int a0, int a1, int a2, int a3,
     73  1.10    simonb 			  u_int pc, u_int sp, u_int fp, u_int ra,
     74  1.10    simonb 			  void (*)(const char*, ...)));
     75   1.1  jonathan 
     76   1.1  jonathan /*
     77   1.1  jonathan  * Stack trace helper.
     78   1.1  jonathan  */
     79   1.4  nisimura void db_mips_stack_trace __P((int count, vaddr_t stackp,
     80  1.10    simonb 			      vaddr_t the_pc, vaddr_t the_ra, int flags,
     81  1.10    simonb 			      vaddr_t kstackp));
     82  1.10    simonb int db_mips_variable_func __P((struct db_variable *vp, db_expr_t *valuep,
     83  1.10    simonb 			       int db_var_fun));
     84   1.1  jonathan 
     85   1.5    castor #define DB_SETF_REGS db_mips_variable_func
     86   1.1  jonathan #define DBREGS_REG()
     87   1.1  jonathan 
     88   1.1  jonathan struct db_variable db_regs[] = {
     89   1.1  jonathan 	{ "at",	(long *)&ddb_regs.f_regs[AST],  DB_SETF_REGS },
     90   1.1  jonathan 	{ "v0",	(long *)&ddb_regs.f_regs[V0],  DB_SETF_REGS },
     91   1.1  jonathan 	{ "v1",	(long *)&ddb_regs.f_regs[V1],  DB_SETF_REGS },
     92   1.1  jonathan 	{ "a0",	(long *)&ddb_regs.f_regs[A0],  DB_SETF_REGS },
     93   1.1  jonathan 	{ "a1",	(long *)&ddb_regs.f_regs[A1],  DB_SETF_REGS },
     94   1.1  jonathan 	{ "a2",	(long *)&ddb_regs.f_regs[A2],  DB_SETF_REGS },
     95   1.1  jonathan 	{ "a3",	(long *)&ddb_regs.f_regs[A3],  DB_SETF_REGS },
     96   1.1  jonathan 	{ "t0",	(long *)&ddb_regs.f_regs[T0],  DB_SETF_REGS },
     97   1.1  jonathan 	{ "t1",	(long *)&ddb_regs.f_regs[T1],  DB_SETF_REGS },
     98   1.1  jonathan 	{ "t2",	(long *)&ddb_regs.f_regs[T2],  DB_SETF_REGS },
     99   1.1  jonathan 	{ "t3",	(long *)&ddb_regs.f_regs[T3],  DB_SETF_REGS },
    100   1.1  jonathan 	{ "t4",	(long *)&ddb_regs.f_regs[T4],  DB_SETF_REGS },
    101   1.1  jonathan 	{ "t5",	(long *)&ddb_regs.f_regs[T5],  DB_SETF_REGS },
    102   1.1  jonathan 	{ "t6",	(long *)&ddb_regs.f_regs[T6],  DB_SETF_REGS },
    103   1.1  jonathan 	{ "t7",	(long *)&ddb_regs.f_regs[T7],  DB_SETF_REGS },
    104   1.1  jonathan 	{ "s0",	(long *)&ddb_regs.f_regs[S0],  DB_SETF_REGS },
    105   1.1  jonathan 	{ "s1",	(long *)&ddb_regs.f_regs[S1],  DB_SETF_REGS },
    106   1.1  jonathan 	{ "s2",	(long *)&ddb_regs.f_regs[S2],  DB_SETF_REGS },
    107   1.1  jonathan 	{ "s3",	(long *)&ddb_regs.f_regs[S3],  DB_SETF_REGS },
    108   1.1  jonathan 	{ "s4",	(long *)&ddb_regs.f_regs[S4],  DB_SETF_REGS },
    109   1.1  jonathan 	{ "s5",	(long *)&ddb_regs.f_regs[S5],  DB_SETF_REGS },
    110   1.1  jonathan 	{ "s6",	(long *)&ddb_regs.f_regs[S6],  DB_SETF_REGS },
    111   1.1  jonathan 	{ "s7",	(long *)&ddb_regs.f_regs[S7],  DB_SETF_REGS },
    112   1.1  jonathan 	{ "t8",	(long *)&ddb_regs.f_regs[T8],  DB_SETF_REGS },
    113   1.1  jonathan 	{ "t9",	(long *)&ddb_regs.f_regs[T9],  DB_SETF_REGS },
    114   1.1  jonathan 	{ "k0",	(long *)&ddb_regs.f_regs[K0],  DB_SETF_REGS },
    115   1.1  jonathan 	{ "k1",	(long *)&ddb_regs.f_regs[K1],  DB_SETF_REGS },
    116   1.1  jonathan 	{ "gp",	(long *)&ddb_regs.f_regs[GP],  DB_SETF_REGS },
    117   1.1  jonathan 	{ "sp",	(long *)&ddb_regs.f_regs[SP],  DB_SETF_REGS },
    118   1.1  jonathan 	{ "fp",	(long *)&ddb_regs.f_regs[S8],  DB_SETF_REGS },	/* frame ptr */
    119   1.1  jonathan 	{ "ra",	(long *)&ddb_regs.f_regs[RA],  DB_SETF_REGS },
    120   1.1  jonathan 	{ "sr",	(long *)&ddb_regs.f_regs[SR],  DB_SETF_REGS },
    121   1.1  jonathan 	{ "mdlo",(long *)&ddb_regs.f_regs[MULLO],  DB_SETF_REGS },
    122   1.1  jonathan 	{ "mdhi",(long *)&ddb_regs.f_regs[MULHI],  DB_SETF_REGS },
    123   1.1  jonathan 	{ "bad", (long *)&ddb_regs.f_regs[BADVADDR], DB_SETF_REGS },
    124   1.1  jonathan 	{ "cs",	(long *)&ddb_regs.f_regs[CAUSE],  DB_SETF_REGS },
    125   1.1  jonathan 	{ "pc",	(long *)&ddb_regs.f_regs[PC],  DB_SETF_REGS },
    126   1.1  jonathan };
    127   1.1  jonathan struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
    128   1.1  jonathan 
    129   1.1  jonathan void
    130  1.11     jhawk db_stack_trace_print(addr, have_addr, count, modif, pr)
    131   1.1  jonathan 	db_expr_t	addr;
    132   1.1  jonathan 	boolean_t	have_addr;
    133   1.1  jonathan 	db_expr_t	count;
    134   1.1  jonathan 	char		*modif;
    135  1.11     jhawk 	void		(*pr) __P((const char *, ...));
    136   1.1  jonathan {
    137   1.5    castor #ifndef DDB_TRACE
    138   1.8    simonb 	stacktrace_subr(ddb_regs.f_regs[A0], ddb_regs.f_regs[A1],
    139   1.1  jonathan 			ddb_regs.f_regs[A2], ddb_regs.f_regs[A3],
    140   1.1  jonathan 			ddb_regs.f_regs[PC],
    141   1.1  jonathan 			ddb_regs.f_regs[SP],
    142   1.5    castor 			ddb_regs.f_regs[S8],	/* non-virtual frame pointer */
    143   1.1  jonathan 			ddb_regs.f_regs[RA],
    144  1.11     jhawk 			pr);
    145   1.5    castor #else
    146   1.5    castor /*
    147  1.13     soren  * Incomplete but practically useful stack backtrace.
    148   1.5    castor  */
    149   1.5    castor #define	MIPS_JR_RA	0x03e00008	/* instruction code for jr ra */
    150   1.5    castor #define	MIPS_JR_K0	0x03400008	/* instruction code for jr k0 */
    151   1.6  nisimura #define	MIPS_ERET	0x42000018	/* instruction code for eret */
    152   1.5    castor 	unsigned va, pc, ra, sp, func;
    153   1.5    castor 	int insn;
    154   1.5    castor 	InstFmt i;
    155   1.5    castor 	int stacksize;
    156   1.5    castor 	db_addr_t offset;
    157   1.5    castor 	char *name;
    158   1.5    castor 	extern char verylocore[];
    159   1.5    castor 
    160   1.5    castor 	pc = ddb_regs.f_regs[PC];
    161   1.5    castor 	sp = ddb_regs.f_regs[SP];
    162   1.6  nisimura 	ra = ddb_regs.f_regs[RA];
    163   1.5    castor 	do {
    164   1.5    castor 		va = pc;
    165   1.5    castor 		do {
    166   1.5    castor 			va -= sizeof(int);
    167   1.5    castor 			insn = *(int *)va;
    168   1.5    castor 			if (insn == MIPS_ERET)
    169   1.5    castor 				goto mips3_eret;
    170   1.5    castor 		} while (insn != MIPS_JR_RA && insn != MIPS_JR_K0);
    171   1.5    castor 		va += sizeof(int);
    172   1.5    castor 	mips3_eret:
    173   1.5    castor 		va += sizeof(int);
    174   1.5    castor 		while (*(int *)va == 0x00000000)
    175   1.5    castor 			va += sizeof(int);
    176   1.5    castor 		func = va;
    177   1.5    castor 		stacksize = 0;
    178   1.5    castor 		do {
    179   1.5    castor 			i.word = *(int *)va;
    180   1.5    castor 			if (i.IType.op == OP_SW
    181   1.5    castor 			    && i.IType.rs == SP
    182   1.5    castor 			    && i.IType.rt == RA)
    183   1.5    castor 				ra = *(int *)(sp + (short)i.IType.imm);
    184   1.5    castor 			if (i.IType.op == OP_ADDIU
    185   1.5    castor 			    && i.IType.rs == SP
    186   1.5    castor 			    && i.IType.rt == SP)
    187   1.5    castor 				stacksize = -(short)i.IType.imm;
    188   1.5    castor 			va += sizeof(int);
    189   1.5    castor 		} while (va < pc);
    190   1.5    castor 
    191   1.5    castor 		db_find_sym_and_offset(func, &name, &offset);
    192   1.5    castor 		if (name == 0)
    193   1.5    castor 			name = "?";
    194  1.11     jhawk 		(*pr)("%s()+0x%x, called by %p, stack size %d\n",
    195   1.5    castor 			name, pc - func, (void *)ra, stacksize);
    196   1.5    castor 
    197   1.5    castor 		if (ra == pc) {
    198  1.12    mhitch 			(*pr)("-- loop? --\n");
    199   1.5    castor 			return;
    200   1.5    castor 		}
    201   1.5    castor 		sp += stacksize;
    202   1.5    castor 		pc = ra;
    203   1.5    castor 	} while (pc > (unsigned)verylocore);
    204   1.5    castor 	if (pc < 0x80000000)
    205  1.11     jhawk 		(*pr)("-- user process --\n");
    206   1.5    castor 	else
    207  1.11     jhawk 		(*pr)("-- kernel entry --\n");
    208   1.5    castor #endif
    209   1.1  jonathan }
    210   1.1  jonathan 
    211   1.1  jonathan void
    212   1.1  jonathan db_mips_stack_trace(count, stackp, the_pc, the_ra, flags, kstackp)
    213   1.1  jonathan 	int count;
    214   1.4  nisimura 	vaddr_t stackp, the_pc, the_ra;
    215   1.1  jonathan 	int flags;
    216   1.4  nisimura 	vaddr_t kstackp;
    217   1.1  jonathan {
    218   1.1  jonathan 	return;
    219   1.1  jonathan }
    220   1.1  jonathan 
    221   1.5    castor 
    222   1.8    simonb int
    223   1.5    castor db_mips_variable_func (struct db_variable *vp,
    224   1.5    castor 	db_expr_t *valuep,
    225   1.5    castor 	int db_var_fcn)
    226   1.5    castor {
    227   1.5    castor 	switch (db_var_fcn) {
    228   1.5    castor 	case DB_VAR_GET:
    229   1.5    castor 		*valuep = *(mips_reg_t *) vp->valuep;
    230   1.5    castor 		break;
    231   1.5    castor 	case DB_VAR_SET:
    232   1.5    castor 		*(mips_reg_t *) vp->valuep = *valuep;
    233   1.5    castor 		break;
    234   1.5    castor 	}
    235   1.5    castor 	return 0;
    236   1.5    castor }
    237