xref: /freebsd/sys/arm/arm/db_trace.c (revision 0cdf461105452fa9e8868858f56cac7eca9405ab)
16fc729afSOlivier Houchard /*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
26fc729afSOlivier Houchard 
3d8315c79SWarner Losh /*-
46fc729afSOlivier Houchard  * Copyright (c) 2000, 2001 Ben Harris
56fc729afSOlivier Houchard  * Copyright (c) 1996 Scott K. Stevens
66fc729afSOlivier Houchard  *
76fc729afSOlivier Houchard  * Mach Operating System
86fc729afSOlivier Houchard  * Copyright (c) 1991,1990 Carnegie Mellon University
96fc729afSOlivier Houchard  * All Rights Reserved.
106fc729afSOlivier Houchard  *
116fc729afSOlivier Houchard  * Permission to use, copy, modify and distribute this software and its
126fc729afSOlivier Houchard  * documentation is hereby granted, provided that both the copyright
136fc729afSOlivier Houchard  * notice and this permission notice appear in all copies of the
146fc729afSOlivier Houchard  * software, derivative works or modified versions, and any portions
156fc729afSOlivier Houchard  * thereof, and that both notices appear in supporting documentation.
166fc729afSOlivier Houchard  *
176fc729afSOlivier Houchard  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
186fc729afSOlivier Houchard  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
196fc729afSOlivier Houchard  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
206fc729afSOlivier Houchard  *
216fc729afSOlivier Houchard  * Carnegie Mellon requests users of this software to return to
226fc729afSOlivier Houchard  *
236fc729afSOlivier Houchard  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
246fc729afSOlivier Houchard  *  School of Computer Science
256fc729afSOlivier Houchard  *  Carnegie Mellon University
266fc729afSOlivier Houchard  *  Pittsburgh PA 15213-3890
276fc729afSOlivier Houchard  *
286fc729afSOlivier Houchard  * any improvements or extensions that they make and grant Carnegie the
296fc729afSOlivier Houchard  * rights to redistribute these changes.
306fc729afSOlivier Houchard  */
316fc729afSOlivier Houchard 
326fc729afSOlivier Houchard #include <sys/cdefs.h>
336fc729afSOlivier Houchard __FBSDID("$FreeBSD$");
346fc729afSOlivier Houchard #include <sys/param.h>
359cdb2bfcSOlivier Houchard #include <sys/systm.h>
366fc729afSOlivier Houchard 
376fc729afSOlivier Houchard 
386fc729afSOlivier Houchard #include <sys/proc.h>
392f6d0d8fSOlivier Houchard #include <sys/kdb.h>
408d511e2aSJeff Roberson #include <sys/stack.h>
416fc729afSOlivier Houchard #include <machine/armreg.h>
426fc729afSOlivier Houchard #include <machine/asm.h>
436fc729afSOlivier Houchard #include <machine/cpufunc.h>
446fc729afSOlivier Houchard #include <machine/db_machdep.h>
456004362eSDavid Schultz #include <machine/pcb.h>
463c90d1eaSRobert Watson #include <machine/stack.h>
476fc729afSOlivier Houchard #include <machine/vmparam.h>
486fc729afSOlivier Houchard #include <ddb/ddb.h>
496fc729afSOlivier Houchard #include <ddb/db_access.h>
506fc729afSOlivier Houchard #include <ddb/db_sym.h>
516fc729afSOlivier Houchard #include <ddb/db_output.h>
526fc729afSOlivier Houchard 
536fc729afSOlivier Houchard /*
546fc729afSOlivier Houchard  * APCS stack frames are awkward beasts, so I don't think even trying to use
556fc729afSOlivier Houchard  * a structure to represent them is a good idea.
566fc729afSOlivier Houchard  *
576fc729afSOlivier Houchard  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
586fc729afSOlivier Houchard  *
596fc729afSOlivier Houchard  *          save code pointer       [fp]        <- fp points to here
606fc729afSOlivier Houchard  *          return link value       [fp, #-4]
616fc729afSOlivier Houchard  *          return sp value         [fp, #-8]
626fc729afSOlivier Houchard  *          return fp value         [fp, #-12]
636fc729afSOlivier Houchard  *          [saved v7 value]
646fc729afSOlivier Houchard  *          [saved v6 value]
656fc729afSOlivier Houchard  *          [saved v5 value]
666fc729afSOlivier Houchard  *          [saved v4 value]
676fc729afSOlivier Houchard  *          [saved v3 value]
686fc729afSOlivier Houchard  *          [saved v2 value]
696fc729afSOlivier Houchard  *          [saved v1 value]
706fc729afSOlivier Houchard  *          [saved a4 value]
716fc729afSOlivier Houchard  *          [saved a3 value]
726fc729afSOlivier Houchard  *          [saved a2 value]
736fc729afSOlivier Houchard  *          [saved a1 value]
746fc729afSOlivier Houchard  *
756fc729afSOlivier Houchard  * The save code pointer points twelve bytes beyond the start of the
766fc729afSOlivier Houchard  * code sequence (usually a single STM) that created the stack frame.
776fc729afSOlivier Houchard  * We have to disassemble it if we want to know which of the optional
786fc729afSOlivier Houchard  * fields are actually present.
796fc729afSOlivier Houchard  */
806fc729afSOlivier Houchard 
81fd32d93bSMarcel Moolenaar static void
82*0cdf4611SGrzegorz Bernacki db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
836fc729afSOlivier Houchard {
846fc729afSOlivier Houchard 	u_int32_t	*frame, *lastframe;
856fc729afSOlivier Houchard 	c_db_sym_t sym;
866fc729afSOlivier Houchard 	const char *name;
876fc729afSOlivier Houchard 	db_expr_t value;
886fc729afSOlivier Houchard 	db_expr_t offset;
8919e9205aSJohn Baldwin 	int	scp_offset;
906fc729afSOlivier Houchard 
91b1ff74ebSOlivier Houchard 	frame = (u_int32_t *)addr;
926fc729afSOlivier Houchard 	lastframe = NULL;
936fc729afSOlivier Houchard 	scp_offset = -(get_pc_str_offset() >> 2);
946fc729afSOlivier Houchard 
9519e9205aSJohn Baldwin 	while (count-- && frame != NULL && !db_pager_quit) {
966fc729afSOlivier Houchard 		db_addr_t	scp;
976fc729afSOlivier Houchard 		u_int32_t	savecode;
986fc729afSOlivier Houchard 		int		r;
996fc729afSOlivier Houchard 		u_int32_t	*rp;
1006fc729afSOlivier Houchard 		const char	*sep;
1016fc729afSOlivier Houchard 
1026fc729afSOlivier Houchard 		/*
1036fc729afSOlivier Houchard 		 * In theory, the SCP isn't guaranteed to be in the function
1046fc729afSOlivier Houchard 		 * that generated the stack frame.  We hope for the best.
1056fc729afSOlivier Houchard 		 */
1066fc729afSOlivier Houchard 		scp = frame[FR_SCP];
1076fc729afSOlivier Houchard 
108282c3a65SOlivier Houchard 		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
1096fc729afSOlivier Houchard 		if (sym == C_DB_SYM_NULL) {
1106fc729afSOlivier Houchard 			value = 0;
1116fc729afSOlivier Houchard 			name = "(null)";
1126fc729afSOlivier Houchard 		} else
1136fc729afSOlivier Houchard 			db_symbol_values(sym, &name, &value);
1146fc729afSOlivier Houchard 		db_printf("%s() at ", name);
115282c3a65SOlivier Houchard 		db_printsym(scp, DB_STGY_PROC);
1166fc729afSOlivier Houchard 		db_printf("\n");
1176fc729afSOlivier Houchard #ifdef __PROG26
1186fc729afSOlivier Houchard 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
1196fc729afSOlivier Houchard 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
1206fc729afSOlivier Houchard 		db_printf(")\n");
1216fc729afSOlivier Houchard #else
1226fc729afSOlivier Houchard 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
1236fc729afSOlivier Houchard 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
1246fc729afSOlivier Houchard 		db_printf(")\n");
1256fc729afSOlivier Houchard #endif
1266fc729afSOlivier Houchard 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
1276fc729afSOlivier Houchard 
1286fc729afSOlivier Houchard 		savecode = ((u_int32_t *)scp)[scp_offset];
1296fc729afSOlivier Houchard 		if ((savecode & 0x0e100000) == 0x08000000) {
1306fc729afSOlivier Houchard 			/* Looks like an STM */
1316fc729afSOlivier Houchard 			rp = frame - 4;
1326fc729afSOlivier Houchard 			sep = "\n\t";
1336fc729afSOlivier Houchard 			for (r = 10; r >= 0; r--) {
1346fc729afSOlivier Houchard 				if (savecode & (1 << r)) {
1356fc729afSOlivier Houchard 					db_printf("%sr%d=0x%08x",
1366fc729afSOlivier Houchard 					    sep, r, *rp--);
1376fc729afSOlivier Houchard 					sep = (frame - rp) % 4 == 2 ?
1386fc729afSOlivier Houchard 					    "\n\t" : " ";
1396fc729afSOlivier Houchard 				}
1406fc729afSOlivier Houchard 			}
1416fc729afSOlivier Houchard 		}
1426fc729afSOlivier Houchard 
1436fc729afSOlivier Houchard 		db_printf("\n");
1446fc729afSOlivier Houchard 
1456fc729afSOlivier Houchard 		/*
1466fc729afSOlivier Houchard 		 * Switch to next frame up
1476fc729afSOlivier Houchard 		 */
1486fc729afSOlivier Houchard 		if (frame[FR_RFP] == 0)
1496fc729afSOlivier Houchard 			break; /* Top of stack */
1506fc729afSOlivier Houchard 
1516fc729afSOlivier Houchard 		lastframe = frame;
1526fc729afSOlivier Houchard 		frame = (u_int32_t *)(frame[FR_RFP]);
1536fc729afSOlivier Houchard 
1546fc729afSOlivier Houchard 		if (INKERNEL((int)frame)) {
1556fc729afSOlivier Houchard 			/* staying in kernel */
1566fc729afSOlivier Houchard 			if (frame <= lastframe) {
1576fc729afSOlivier Houchard 				db_printf("Bad frame pointer: %p\n", frame);
1586fc729afSOlivier Houchard 				break;
1596fc729afSOlivier Houchard 			}
1606fc729afSOlivier Houchard 		} else if (INKERNEL((int)lastframe)) {
1616fc729afSOlivier Houchard 			/* switch from user to kernel */
1626fc729afSOlivier Houchard 			if (kernel_only)
1636fc729afSOlivier Houchard 				break;	/* kernel stack only */
1646fc729afSOlivier Houchard 		} else {
1656fc729afSOlivier Houchard 			/* in user */
1666fc729afSOlivier Houchard 			if (frame <= lastframe) {
1676fc729afSOlivier Houchard 				db_printf("Bad user frame pointer: %p\n",
1686fc729afSOlivier Houchard 					  frame);
1696fc729afSOlivier Houchard 				break;
1706fc729afSOlivier Houchard 			}
1716fc729afSOlivier Houchard 		}
1726fc729afSOlivier Houchard 	}
1736fc729afSOlivier Houchard }
1746fc729afSOlivier Houchard 
1756fc729afSOlivier Houchard /* XXX stubs */
1766fc729afSOlivier Houchard void
1776fc729afSOlivier Houchard db_md_list_watchpoints()
1786fc729afSOlivier Houchard {
1796fc729afSOlivier Houchard }
1806fc729afSOlivier Houchard 
1816fc729afSOlivier Houchard int
1826fc729afSOlivier Houchard db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
1836fc729afSOlivier Houchard {
1846fc729afSOlivier Houchard 	return (0);
1856fc729afSOlivier Houchard }
1866fc729afSOlivier Houchard 
1876fc729afSOlivier Houchard int
1886fc729afSOlivier Houchard db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
1896fc729afSOlivier Houchard {
1906fc729afSOlivier Houchard 	return (0);
1916fc729afSOlivier Houchard }
192fd32d93bSMarcel Moolenaar 
1932f6d0d8fSOlivier Houchard int
1942f6d0d8fSOlivier Houchard db_trace_thread(struct thread *thr, int count)
1956fc729afSOlivier Houchard {
1962ffa4420SMarcel Moolenaar 	struct pcb *ctx;
1976fc729afSOlivier Houchard 
198*0cdf4611SGrzegorz Bernacki 	if (thr != curthread) {
1992ffa4420SMarcel Moolenaar 		ctx = kdb_thr_ctx(thr);
200*0cdf4611SGrzegorz Bernacki 		db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
201*0cdf4611SGrzegorz Bernacki 	} else
202*0cdf4611SGrzegorz Bernacki 		db_trace_self();
2032f6d0d8fSOlivier Houchard 	return (0);
2042f6d0d8fSOlivier Houchard }
2052f6d0d8fSOlivier Houchard 
2062f6d0d8fSOlivier Houchard void
2072f6d0d8fSOlivier Houchard db_trace_self(void)
2082f6d0d8fSOlivier Houchard {
209faa7ba7aSMarcel Moolenaar 	db_addr_t addr;
210faa7ba7aSMarcel Moolenaar 
21188256118SMarcel Moolenaar 	addr = (db_addr_t)__builtin_frame_address(0);
212*0cdf4611SGrzegorz Bernacki 	db_stack_trace_cmd(addr, -1, FALSE);
2136fc729afSOlivier Houchard }
214