Home | History | Annotate | Line # | Download | only in mips
db_trace.c revision 1.26
      1  1.26    simonb /*	$NetBSD: db_trace.c,v 1.26 2005/05/30 02:26:17 simonb 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.24     lukem 
     29  1.24     lukem #include <sys/cdefs.h>
     30  1.26    simonb __KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.26 2005/05/30 02:26:17 simonb Exp $");
     31   1.1  jonathan 
     32  1.16     jeffs #include <sys/types.h>
     33  1.13     soren #include <sys/param.h>
     34  1.16     jeffs #include <sys/systm.h>
     35  1.16     jeffs #include <sys/proc.h>
     36  1.16     jeffs #include <sys/user.h>
     37   1.1  jonathan 
     38   1.5    castor #include <mips/mips_opcode.h>
     39   1.5    castor 
     40   1.1  jonathan #include <machine/param.h>
     41   1.7    simonb #include <machine/db_machdep.h>
     42   1.1  jonathan #include <ddb/db_interface.h>
     43   1.1  jonathan #include <ddb/db_output.h>
     44   1.1  jonathan #include <ddb/db_variables.h>
     45   1.1  jonathan #include <ddb/db_sym.h>
     46   1.1  jonathan 
     47  1.21    simonb int __start(void);	/* lowest kernel code address */
     48  1.21    simonb vaddr_t getreg_val(db_expr_t regno);
     49   1.1  jonathan 
     50   1.1  jonathan #define REG_ARG(i)	(4+i)
     51   1.1  jonathan #define SAVES_RA(x)	isa_spill((x),31)
     52   1.1  jonathan 
     53   1.1  jonathan #define KERN_SAVE_REG_IDX(vp)	( \
     54   1.1  jonathan 	((vp)->valuep >= (int *)(&((struct mips_saved_state *)0)->s0) &&    \
     55   1.1  jonathan 	 (vp)->valuep <= (int *)(&((struct mips_saved_state *)0)->s7))?	    \
     56   1.1  jonathan 		vp->valuep - (int *)(&((struct mips_saved_state *)0)->s0):  \
     57   1.1  jonathan 	((vp)->valuep >= (int *)(&((struct mips_saved_state *)0)->sp) &&    \
     58   1.1  jonathan 	 (vp)->valuep <= (int *)(&((struct mips_saved_state *)0)->ra))?	    \
     59   1.1  jonathan 		((vp)->valuep-(int *)(&((struct mips_saved_state *)0)->sp)) + \
     60   1.1  jonathan 		 ((int *)(&((struct mips_kernel_state *)0)->sp) - (int *)0):  \
     61   1.1  jonathan 	 -1)
     62   1.1  jonathan 
     63  1.21    simonb db_sym_t localsym(db_sym_t sym, boolean_t isreg, int *lex_level);
     64   1.1  jonathan 
     65   1.1  jonathan /*
     66   1.1  jonathan  * Machine register set.
     67   1.1  jonathan  */
     68   1.1  jonathan struct mips_saved_state *db_cur_exc_frame = 0;
     69   1.1  jonathan 
     70   1.1  jonathan /*XXX*/
     71  1.21    simonb void	stacktrace_subr(int, int, int, int, u_int, u_int, u_int, u_int,
     72  1.21    simonb 	    void (*)(const char*, ...));
     73   1.1  jonathan 
     74   1.1  jonathan /*
     75   1.1  jonathan  * Stack trace helper.
     76   1.1  jonathan  */
     77  1.21    simonb void db_mips_stack_trace(int, vaddr_t, vaddr_t, vaddr_t, int, vaddr_t);
     78  1.21    simonb int db_mips_variable_func(const struct db_variable *, db_expr_t *, int);
     79   1.1  jonathan 
     80   1.5    castor #define DB_SETF_REGS db_mips_variable_func
     81   1.1  jonathan #define DBREGS_REG()
     82   1.1  jonathan 
     83  1.17  jdolecek const struct db_variable db_regs[] = {
     84  1.25        he 	{ "at",	(long *)&ddb_regs.f_regs[_R_AST],  DB_SETF_REGS },
     85  1.25        he 	{ "v0",	(long *)&ddb_regs.f_regs[_R_V0],  DB_SETF_REGS },
     86  1.25        he 	{ "v1",	(long *)&ddb_regs.f_regs[_R_V1],  DB_SETF_REGS },
     87  1.25        he 	{ "a0",	(long *)&ddb_regs.f_regs[_R_A0],  DB_SETF_REGS },
     88  1.25        he 	{ "a1",	(long *)&ddb_regs.f_regs[_R_A1],  DB_SETF_REGS },
     89  1.25        he 	{ "a2",	(long *)&ddb_regs.f_regs[_R_A2],  DB_SETF_REGS },
     90  1.25        he 	{ "a3",	(long *)&ddb_regs.f_regs[_R_A3],  DB_SETF_REGS },
     91  1.22   thorpej #if defined(__mips_n32) || defined(__mips_n64)
     92  1.25        he 	{ "a4",	(long *)&ddb_regs.f_regs[_R_A4],  DB_SETF_REGS },
     93  1.25        he 	{ "a5",	(long *)&ddb_regs.f_regs[_R_A5],  DB_SETF_REGS },
     94  1.25        he 	{ "a6",	(long *)&ddb_regs.f_regs[_R_A6],  DB_SETF_REGS },
     95  1.25        he 	{ "a7",	(long *)&ddb_regs.f_regs[_R_A7],  DB_SETF_REGS },
     96  1.25        he 	{ "t0",	(long *)&ddb_regs.f_regs[_R_T0],  DB_SETF_REGS },
     97  1.25        he 	{ "t1",	(long *)&ddb_regs.f_regs[_R_T1],  DB_SETF_REGS },
     98  1.25        he 	{ "t2",	(long *)&ddb_regs.f_regs[_R_T2],  DB_SETF_REGS },
     99  1.25        he 	{ "t3",	(long *)&ddb_regs.f_regs[_R_T3],  DB_SETF_REGS },
    100  1.22   thorpej #else
    101  1.25        he 	{ "t0",	(long *)&ddb_regs.f_regs[_R_T0],  DB_SETF_REGS },
    102  1.25        he 	{ "t1",	(long *)&ddb_regs.f_regs[_R_T1],  DB_SETF_REGS },
    103  1.25        he 	{ "t2",	(long *)&ddb_regs.f_regs[_R_T2],  DB_SETF_REGS },
    104  1.25        he 	{ "t3",	(long *)&ddb_regs.f_regs[_R_T3],  DB_SETF_REGS },
    105  1.25        he 	{ "t4",	(long *)&ddb_regs.f_regs[_R_T4],  DB_SETF_REGS },
    106  1.25        he 	{ "t5",	(long *)&ddb_regs.f_regs[_R_T5],  DB_SETF_REGS },
    107  1.25        he 	{ "t6",	(long *)&ddb_regs.f_regs[_R_T6],  DB_SETF_REGS },
    108  1.25        he 	{ "t7",	(long *)&ddb_regs.f_regs[_R_T7],  DB_SETF_REGS },
    109  1.22   thorpej #endif /* __mips_n32 || __mips_n64 */
    110  1.25        he 	{ "s0",	(long *)&ddb_regs.f_regs[_R_S0],  DB_SETF_REGS },
    111  1.25        he 	{ "s1",	(long *)&ddb_regs.f_regs[_R_S1],  DB_SETF_REGS },
    112  1.25        he 	{ "s2",	(long *)&ddb_regs.f_regs[_R_S2],  DB_SETF_REGS },
    113  1.25        he 	{ "s3",	(long *)&ddb_regs.f_regs[_R_S3],  DB_SETF_REGS },
    114  1.25        he 	{ "s4",	(long *)&ddb_regs.f_regs[_R_S4],  DB_SETF_REGS },
    115  1.25        he 	{ "s5",	(long *)&ddb_regs.f_regs[_R_S5],  DB_SETF_REGS },
    116  1.25        he 	{ "s6",	(long *)&ddb_regs.f_regs[_R_S6],  DB_SETF_REGS },
    117  1.25        he 	{ "s7",	(long *)&ddb_regs.f_regs[_R_S7],  DB_SETF_REGS },
    118  1.25        he 	{ "t8",	(long *)&ddb_regs.f_regs[_R_T8],  DB_SETF_REGS },
    119  1.25        he 	{ "t9",	(long *)&ddb_regs.f_regs[_R_T9],  DB_SETF_REGS },
    120  1.25        he 	{ "k0",	(long *)&ddb_regs.f_regs[_R_K0],  DB_SETF_REGS },
    121  1.25        he 	{ "k1",	(long *)&ddb_regs.f_regs[_R_K1],  DB_SETF_REGS },
    122  1.25        he 	{ "gp",	(long *)&ddb_regs.f_regs[_R_GP],  DB_SETF_REGS },
    123  1.25        he 	{ "sp",	(long *)&ddb_regs.f_regs[_R_SP],  DB_SETF_REGS },
    124  1.25        he 	{ "fp",	(long *)&ddb_regs.f_regs[_R_S8],  DB_SETF_REGS },/* frame ptr */
    125  1.25        he 	{ "ra",	(long *)&ddb_regs.f_regs[_R_RA],  DB_SETF_REGS },
    126  1.25        he 	{ "sr",	(long *)&ddb_regs.f_regs[_R_SR],  DB_SETF_REGS },
    127  1.25        he 	{ "mdlo",(long *)&ddb_regs.f_regs[_R_MULLO],  DB_SETF_REGS },
    128  1.25        he 	{ "mdhi",(long *)&ddb_regs.f_regs[_R_MULHI],  DB_SETF_REGS },
    129  1.25        he 	{ "bad", (long *)&ddb_regs.f_regs[_R_BADVADDR], DB_SETF_REGS },
    130  1.25        he 	{ "cs",	(long *)&ddb_regs.f_regs[_R_CAUSE],  DB_SETF_REGS },
    131  1.25        he 	{ "pc",	(long *)&ddb_regs.f_regs[_R_PC],  DB_SETF_REGS },
    132   1.1  jonathan };
    133  1.17  jdolecek const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
    134   1.1  jonathan 
    135   1.1  jonathan void
    136  1.26    simonb db_stack_trace_print(db_expr_t addr, int have_addr, db_expr_t count,
    137  1.26    simonb     const char *modif, void (*pr)(const char *, ...))
    138   1.1  jonathan {
    139   1.5    castor #ifndef DDB_TRACE
    140  1.16     jeffs 	struct pcb *pcb;
    141  1.16     jeffs 	struct proc *p;
    142  1.23   thorpej 	struct lwp *l;
    143  1.16     jeffs 
    144  1.16     jeffs 	if (!have_addr) {
    145  1.25        he 		stacktrace_subr(ddb_regs.f_regs[_R_A0],
    146  1.25        he 				ddb_regs.f_regs[_R_A1],
    147  1.25        he 				ddb_regs.f_regs[_R_A2],
    148  1.25        he 				ddb_regs.f_regs[_R_A3],
    149  1.25        he 				ddb_regs.f_regs[_R_PC],
    150  1.25        he 				ddb_regs.f_regs[_R_SP],
    151  1.16     jeffs 				/* non-virtual frame pointer */
    152  1.25        he 				ddb_regs.f_regs[_R_S8],
    153  1.25        he 				ddb_regs.f_regs[_R_RA],
    154  1.16     jeffs 				pr);
    155  1.16     jeffs 		return;
    156  1.16     jeffs 	}
    157  1.16     jeffs 
    158  1.16     jeffs 	/* "trace/t" */
    159  1.16     jeffs 	(*pr)("pid %d ", (int)addr);
    160  1.16     jeffs 	p = pfind(addr);
    161  1.16     jeffs 	if (p == NULL) {
    162  1.16     jeffs 		(*pr)("not found\n");
    163  1.16     jeffs 		return;
    164  1.16     jeffs 	}
    165  1.23   thorpej 	l = LIST_FIRST(&p->p_lwps); /* XXX NJWLWP */
    166  1.23   thorpej 	if (!(l->l_flag & L_INMEM)) {
    167  1.16     jeffs 		(*pr)("swapped out\n");
    168  1.16     jeffs 		return;
    169  1.16     jeffs 	}
    170  1.16     jeffs 
    171  1.23   thorpej 	pcb = &(l->l_addr->u_pcb);
    172  1.16     jeffs 	(*pr)("at %p\n", pcb);
    173  1.16     jeffs 
    174  1.16     jeffs 	stacktrace_subr(0,0,0,0,	/* no args known */
    175  1.16     jeffs 			(int)cpu_switch,
    176  1.16     jeffs 			pcb->pcb_context[8],
    177  1.16     jeffs 			pcb->pcb_context[9],
    178  1.16     jeffs 			pcb->pcb_context[10],
    179  1.11     jhawk 			pr);
    180   1.5    castor #else
    181   1.5    castor /*
    182  1.13     soren  * Incomplete but practically useful stack backtrace.
    183   1.5    castor  */
    184   1.5    castor #define	MIPS_JR_RA	0x03e00008	/* instruction code for jr ra */
    185   1.5    castor #define	MIPS_JR_K0	0x03400008	/* instruction code for jr k0 */
    186   1.6  nisimura #define	MIPS_ERET	0x42000018	/* instruction code for eret */
    187   1.5    castor 	unsigned va, pc, ra, sp, func;
    188   1.5    castor 	int insn;
    189   1.5    castor 	InstFmt i;
    190   1.5    castor 	int stacksize;
    191   1.5    castor 	db_addr_t offset;
    192   1.5    castor 	char *name;
    193   1.5    castor 	extern char verylocore[];
    194   1.5    castor 
    195  1.25        he 	pc = ddb_regs.f_regs[_R_PC];
    196  1.25        he 	sp = ddb_regs.f_regs[_R_SP];
    197  1.25        he 	ra = ddb_regs.f_regs[_R_RA];
    198   1.5    castor 	do {
    199   1.5    castor 		va = pc;
    200   1.5    castor 		do {
    201   1.5    castor 			va -= sizeof(int);
    202   1.5    castor 			insn = *(int *)va;
    203   1.5    castor 			if (insn == MIPS_ERET)
    204   1.5    castor 				goto mips3_eret;
    205   1.5    castor 		} while (insn != MIPS_JR_RA && insn != MIPS_JR_K0);
    206   1.5    castor 		va += sizeof(int);
    207   1.5    castor 	mips3_eret:
    208   1.5    castor 		va += sizeof(int);
    209   1.5    castor 		while (*(int *)va == 0x00000000)
    210   1.5    castor 			va += sizeof(int);
    211   1.5    castor 		func = va;
    212   1.5    castor 		stacksize = 0;
    213   1.5    castor 		do {
    214   1.5    castor 			i.word = *(int *)va;
    215   1.5    castor 			if (i.IType.op == OP_SW
    216  1.25        he 			    && i.IType.rs == _R_SP
    217  1.25        he 			    && i.IType.rt == _R_RA)
    218   1.5    castor 				ra = *(int *)(sp + (short)i.IType.imm);
    219   1.5    castor 			if (i.IType.op == OP_ADDIU
    220  1.25        he 			    && i.IType.rs == _R_SP
    221  1.25        he 			    && i.IType.rt == _R_SP)
    222   1.5    castor 				stacksize = -(short)i.IType.imm;
    223   1.5    castor 			va += sizeof(int);
    224   1.5    castor 		} while (va < pc);
    225   1.5    castor 
    226   1.5    castor 		db_find_sym_and_offset(func, &name, &offset);
    227   1.5    castor 		if (name == 0)
    228   1.5    castor 			name = "?";
    229  1.11     jhawk 		(*pr)("%s()+0x%x, called by %p, stack size %d\n",
    230   1.5    castor 			name, pc - func, (void *)ra, stacksize);
    231   1.5    castor 
    232   1.5    castor 		if (ra == pc) {
    233  1.12    mhitch 			(*pr)("-- loop? --\n");
    234   1.5    castor 			return;
    235   1.5    castor 		}
    236   1.5    castor 		sp += stacksize;
    237   1.5    castor 		pc = ra;
    238   1.5    castor 	} while (pc > (unsigned)verylocore);
    239   1.5    castor 	if (pc < 0x80000000)
    240  1.11     jhawk 		(*pr)("-- user process --\n");
    241   1.5    castor 	else
    242  1.11     jhawk 		(*pr)("-- kernel entry --\n");
    243   1.5    castor #endif
    244   1.1  jonathan }
    245   1.1  jonathan 
    246   1.1  jonathan void
    247  1.20    simonb db_mips_stack_trace(int count, vaddr_t stackp, vaddr_t the_pc, vaddr_t the_ra,
    248  1.20    simonb     int flags, vaddr_t kstackp)
    249   1.1  jonathan {
    250  1.20    simonb 
    251  1.20    simonb 	/* nothing... */
    252   1.1  jonathan }
    253   1.1  jonathan 
    254   1.5    castor 
    255   1.8    simonb int
    256  1.20    simonb db_mips_variable_func (const struct db_variable *vp, db_expr_t *valuep,
    257  1.20    simonb     int db_var_fcn)
    258   1.5    castor {
    259  1.20    simonb 
    260   1.5    castor 	switch (db_var_fcn) {
    261   1.5    castor 	case DB_VAR_GET:
    262   1.5    castor 		*valuep = *(mips_reg_t *) vp->valuep;
    263   1.5    castor 		break;
    264   1.5    castor 	case DB_VAR_SET:
    265   1.5    castor 		*(mips_reg_t *) vp->valuep = *valuep;
    266   1.5    castor 		break;
    267   1.5    castor 	}
    268   1.5    castor 	return 0;
    269   1.5    castor }
    270