xref: /freebsd/sys/arm/arm/db_trace.c (revision 9cdb2bfc90369fb013fb0f2c10e375cf93a6940d)
16fc729afSOlivier Houchard /*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
26fc729afSOlivier Houchard 
36fc729afSOlivier Houchard /*
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>
396fc729afSOlivier Houchard #include <sys/user.h>
402f6d0d8fSOlivier Houchard #include <sys/kdb.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>
456fc729afSOlivier Houchard #include <machine/vmparam.h>
466fc729afSOlivier Houchard #include <ddb/ddb.h>
476fc729afSOlivier Houchard #include <ddb/db_access.h>
486fc729afSOlivier Houchard #include <ddb/db_sym.h>
496fc729afSOlivier Houchard #include <ddb/db_output.h>
506fc729afSOlivier Houchard 
516fc729afSOlivier Houchard #define INKERNEL(va)	(((vm_offset_t)(va)) >= VM_MIN_KERNEL_ADDRESS)
526fc729afSOlivier Houchard 
536fc729afSOlivier Houchard int  db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
546fc729afSOlivier Houchard int  db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
556fc729afSOlivier Houchard void db_md_list_watchpoints(void);
566fc729afSOlivier Houchard /*
576fc729afSOlivier Houchard  * APCS stack frames are awkward beasts, so I don't think even trying to use
586fc729afSOlivier Houchard  * a structure to represent them is a good idea.
596fc729afSOlivier Houchard  *
606fc729afSOlivier Houchard  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
616fc729afSOlivier Houchard  *
626fc729afSOlivier Houchard  *          save code pointer       [fp]        <- fp points to here
636fc729afSOlivier Houchard  *          return link value       [fp, #-4]
646fc729afSOlivier Houchard  *          return sp value         [fp, #-8]
656fc729afSOlivier Houchard  *          return fp value         [fp, #-12]
666fc729afSOlivier Houchard  *          [saved v7 value]
676fc729afSOlivier Houchard  *          [saved v6 value]
686fc729afSOlivier Houchard  *          [saved v5 value]
696fc729afSOlivier Houchard  *          [saved v4 value]
706fc729afSOlivier Houchard  *          [saved v3 value]
716fc729afSOlivier Houchard  *          [saved v2 value]
726fc729afSOlivier Houchard  *          [saved v1 value]
736fc729afSOlivier Houchard  *          [saved a4 value]
746fc729afSOlivier Houchard  *          [saved a3 value]
756fc729afSOlivier Houchard  *          [saved a2 value]
766fc729afSOlivier Houchard  *          [saved a1 value]
776fc729afSOlivier Houchard  *
786fc729afSOlivier Houchard  * The save code pointer points twelve bytes beyond the start of the
796fc729afSOlivier Houchard  * code sequence (usually a single STM) that created the stack frame.
806fc729afSOlivier Houchard  * We have to disassemble it if we want to know which of the optional
816fc729afSOlivier Houchard  * fields are actually present.
826fc729afSOlivier Houchard  */
836fc729afSOlivier Houchard 
846fc729afSOlivier Houchard #define FR_SCP	(0)
856fc729afSOlivier Houchard #define FR_RLV	(-1)
866fc729afSOlivier Houchard #define FR_RSP	(-2)
876fc729afSOlivier Houchard #define FR_RFP	(-3)
886fc729afSOlivier Houchard 
896fc729afSOlivier Houchard void
906fc729afSOlivier Houchard db_stack_trace_cmd(addr, have_addr, count, modif)
916fc729afSOlivier Houchard 	db_expr_t       addr;
926fc729afSOlivier Houchard 	int             have_addr;
936fc729afSOlivier Houchard 	db_expr_t       count;
946fc729afSOlivier Houchard 	char            *modif;
956fc729afSOlivier Houchard {
966fc729afSOlivier Houchard 	u_int32_t	*frame, *lastframe;
976fc729afSOlivier Houchard 	c_db_sym_t sym;
986fc729afSOlivier Houchard 	db_expr_t pc;
996fc729afSOlivier Houchard 	char c, *cp = modif;
1006fc729afSOlivier Houchard 	const char *name;
1016fc729afSOlivier Houchard 	db_expr_t value;
1026fc729afSOlivier Houchard 	db_expr_t offset;
1036fc729afSOlivier Houchard 	boolean_t	kernel_only = TRUE;
1046fc729afSOlivier Houchard 	boolean_t	trace_thread = FALSE;
1056fc729afSOlivier Houchard 	int	scp_offset;
1066fc729afSOlivier Houchard 
1079cdb2bfcSOlivier Houchard 	if (kdb_frame == NULL && !have_addr)
1082f6d0d8fSOlivier Houchard 		return;
1096fc729afSOlivier Houchard 	while ((c = *cp++) != 0) {
1106fc729afSOlivier Houchard 		if (c == 'u')
1116fc729afSOlivier Houchard 			kernel_only = FALSE;
1126fc729afSOlivier Houchard 		if (c == 't')
1136fc729afSOlivier Houchard 			trace_thread = TRUE;
1146fc729afSOlivier Houchard 	}
1156fc729afSOlivier Houchard 
1166fc729afSOlivier Houchard 	if (!have_addr)
1172f6d0d8fSOlivier Houchard 		frame = (u_int32_t *)(kdb_frame->tf_r11);
1186fc729afSOlivier Houchard 	else {
1196fc729afSOlivier Houchard 		if (trace_thread) {
1206fc729afSOlivier Houchard 			struct proc *p;
1216fc729afSOlivier Houchard 			struct thread *td;
1226fc729afSOlivier Houchard 			pid_t pid = (pid_t)addr;
1236fc729afSOlivier Houchard 			LIST_FOREACH(p, &allproc, p_list) {
1246fc729afSOlivier Houchard 				if (p->p_pid == pid)
1256fc729afSOlivier Houchard 					break;
1266fc729afSOlivier Houchard 			}
1276fc729afSOlivier Houchard 
1286fc729afSOlivier Houchard 			if (p == NULL) {
1296fc729afSOlivier Houchard 				db_printf("not found\n");
1306fc729afSOlivier Houchard 				return;
1316fc729afSOlivier Houchard 			}
1326fc729afSOlivier Houchard 			if (!(p->p_sflag & PS_INMEM)) {
1336fc729afSOlivier Houchard 				db_printf("swapped out\n");
1346fc729afSOlivier Houchard 				return;
1356fc729afSOlivier Houchard 			}
1366fc729afSOlivier Houchard 			td = FIRST_THREAD_IN_PROC(p);
1376fc729afSOlivier Houchard 			frame = (u_int32_t *)(td->td_pcb->un_32.pcb32_r11);
1386fc729afSOlivier Houchard 			db_printf("at %p\n", frame);
1396fc729afSOlivier Houchard 		} else
1406fc729afSOlivier Houchard 			frame = (u_int32_t *)(addr);
1416fc729afSOlivier Houchard 	}
1426fc729afSOlivier Houchard 	lastframe = NULL;
1436fc729afSOlivier Houchard 	scp_offset = -(get_pc_str_offset() >> 2);
1446fc729afSOlivier Houchard 
1456fc729afSOlivier Houchard 	while (count-- && frame != NULL) {
1466fc729afSOlivier Houchard 		db_addr_t	scp;
1476fc729afSOlivier Houchard 		u_int32_t	savecode;
1486fc729afSOlivier Houchard 		int		r;
1496fc729afSOlivier Houchard 		u_int32_t	*rp;
1506fc729afSOlivier Houchard 		const char	*sep;
1516fc729afSOlivier Houchard 
1526fc729afSOlivier Houchard 		/*
1536fc729afSOlivier Houchard 		 * In theory, the SCP isn't guaranteed to be in the function
1546fc729afSOlivier Houchard 		 * that generated the stack frame.  We hope for the best.
1556fc729afSOlivier Houchard 		 */
1566fc729afSOlivier Houchard 		scp = frame[FR_SCP];
1576fc729afSOlivier Houchard 
1586fc729afSOlivier Houchard 		db_printsym(scp, DB_STGY_PROC);
1596fc729afSOlivier Houchard 		db_printf("\n\t");
1602f6d0d8fSOlivier Houchard 		pc = kdb_frame->tf_pc;
1616fc729afSOlivier Houchard 		sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
1626fc729afSOlivier Houchard 		if (sym == C_DB_SYM_NULL) {
1636fc729afSOlivier Houchard 			value = 0;
1646fc729afSOlivier Houchard 			name = "(null)";
1656fc729afSOlivier Houchard 		} else
1666fc729afSOlivier Houchard 			db_symbol_values(sym, &name, &value);
1676fc729afSOlivier Houchard 		db_printf("%s() at ", name);
1686fc729afSOlivier Houchard 		db_printsym(pc, DB_STGY_PROC);
1696fc729afSOlivier Houchard 		db_printf("\n");
1706fc729afSOlivier Houchard #ifdef __PROG26
1716fc729afSOlivier Houchard 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
1726fc729afSOlivier Houchard 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
1736fc729afSOlivier Houchard 		db_printf(")\n");
1746fc729afSOlivier Houchard #else
1756fc729afSOlivier Houchard 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
1766fc729afSOlivier Houchard 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
1776fc729afSOlivier Houchard 		db_printf(")\n");
1786fc729afSOlivier Houchard #endif
1796fc729afSOlivier Houchard 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
1806fc729afSOlivier Houchard 
1816fc729afSOlivier Houchard 		savecode = ((u_int32_t *)scp)[scp_offset];
1826fc729afSOlivier Houchard 		if ((savecode & 0x0e100000) == 0x08000000) {
1836fc729afSOlivier Houchard 			/* Looks like an STM */
1846fc729afSOlivier Houchard 			rp = frame - 4;
1856fc729afSOlivier Houchard 			sep = "\n\t";
1866fc729afSOlivier Houchard 			for (r = 10; r >= 0; r--) {
1876fc729afSOlivier Houchard 				if (savecode & (1 << r)) {
1886fc729afSOlivier Houchard 					db_printf("%sr%d=0x%08x",
1896fc729afSOlivier Houchard 					    sep, r, *rp--);
1906fc729afSOlivier Houchard 					sep = (frame - rp) % 4 == 2 ?
1916fc729afSOlivier Houchard 					    "\n\t" : " ";
1926fc729afSOlivier Houchard 				}
1936fc729afSOlivier Houchard 			}
1946fc729afSOlivier Houchard 		}
1956fc729afSOlivier Houchard 
1966fc729afSOlivier Houchard 		db_printf("\n");
1976fc729afSOlivier Houchard 
1986fc729afSOlivier Houchard 		/*
1996fc729afSOlivier Houchard 		 * Switch to next frame up
2006fc729afSOlivier Houchard 		 */
2016fc729afSOlivier Houchard 		if (frame[FR_RFP] == 0)
2026fc729afSOlivier Houchard 			break; /* Top of stack */
2036fc729afSOlivier Houchard 
2046fc729afSOlivier Houchard 		lastframe = frame;
2056fc729afSOlivier Houchard 		frame = (u_int32_t *)(frame[FR_RFP]);
2066fc729afSOlivier Houchard 
2076fc729afSOlivier Houchard 		if (INKERNEL((int)frame)) {
2086fc729afSOlivier Houchard 			/* staying in kernel */
2096fc729afSOlivier Houchard 			if (frame <= lastframe) {
2106fc729afSOlivier Houchard 				db_printf("Bad frame pointer: %p\n", frame);
2116fc729afSOlivier Houchard 				break;
2126fc729afSOlivier Houchard 			}
2136fc729afSOlivier Houchard 		} else if (INKERNEL((int)lastframe)) {
2146fc729afSOlivier Houchard 			/* switch from user to kernel */
2156fc729afSOlivier Houchard 			if (kernel_only)
2166fc729afSOlivier Houchard 				break;	/* kernel stack only */
2176fc729afSOlivier Houchard 		} else {
2186fc729afSOlivier Houchard 			/* in user */
2196fc729afSOlivier Houchard 			if (frame <= lastframe) {
2206fc729afSOlivier Houchard 				db_printf("Bad user frame pointer: %p\n",
2216fc729afSOlivier Houchard 					  frame);
2226fc729afSOlivier Houchard 				break;
2236fc729afSOlivier Houchard 			}
2246fc729afSOlivier Houchard 		}
2256fc729afSOlivier Houchard 	}
2266fc729afSOlivier Houchard }
2276fc729afSOlivier Houchard 
2286fc729afSOlivier Houchard /* XXX stubs */
2296fc729afSOlivier Houchard void
2306fc729afSOlivier Houchard db_md_list_watchpoints()
2316fc729afSOlivier Houchard {
2326fc729afSOlivier Houchard }
2336fc729afSOlivier Houchard 
2346fc729afSOlivier Houchard int
2356fc729afSOlivier Houchard db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
2366fc729afSOlivier Houchard {
2376fc729afSOlivier Houchard 	return (0);
2386fc729afSOlivier Houchard }
2396fc729afSOlivier Houchard 
2406fc729afSOlivier Houchard int
2416fc729afSOlivier Houchard db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
2426fc729afSOlivier Houchard {
2436fc729afSOlivier Houchard 	return (0);
2446fc729afSOlivier Houchard }
2452f6d0d8fSOlivier Houchard int
2462f6d0d8fSOlivier Houchard db_trace_thread(struct thread *thr, int count)
2476fc729afSOlivier Houchard {
2489cdb2bfcSOlivier Houchard 	uint32_t addr;
2496fc729afSOlivier Houchard 
2509cdb2bfcSOlivier Houchard 	if (thr == curthread)
2519cdb2bfcSOlivier Houchard 		addr = (uint32_t)__builtin_frame_address(0);
2529cdb2bfcSOlivier Houchard 	else
2539cdb2bfcSOlivier Houchard 		addr = thr->td_pcb->un_32.pcb32_r11;
2549cdb2bfcSOlivier Houchard 	db_stack_trace_cmd(addr, 1, -1, NULL);
2552f6d0d8fSOlivier Houchard 	return (0);
2562f6d0d8fSOlivier Houchard }
2572f6d0d8fSOlivier Houchard 
2582f6d0d8fSOlivier Houchard void
2592f6d0d8fSOlivier Houchard db_trace_self(void)
2602f6d0d8fSOlivier Houchard {
2619cdb2bfcSOlivier Houchard 	db_trace_thread(curthread, -1);
2626fc729afSOlivier Houchard }
263