xref: /freebsd/sys/arm/arm/db_trace.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
1 /*	$NetBSD: db_trace.c,v 1.8 2003/01/17 22:28:48 thorpej Exp $	*/
2 
3 /*
4  * Copyright (c) 2000, 2001 Ben Harris
5  * Copyright (c) 1996 Scott K. Stevens
6  *
7  * Mach Operating System
8  * Copyright (c) 1991,1990 Carnegie Mellon University
9  * All Rights Reserved.
10  *
11  * Permission to use, copy, modify and distribute this software and its
12  * documentation is hereby granted, provided that both the copyright
13  * notice and this permission notice appear in all copies of the
14  * software, derivative works or modified versions, and any portions
15  * thereof, and that both notices appear in supporting documentation.
16  *
17  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
18  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
19  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
20  *
21  * Carnegie Mellon requests users of this software to return to
22  *
23  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
24  *  School of Computer Science
25  *  Carnegie Mellon University
26  *  Pittsburgh PA 15213-3890
27  *
28  * any improvements or extensions that they make and grant Carnegie the
29  * rights to redistribute these changes.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 
37 
38 #include <sys/proc.h>
39 #include <sys/user.h>
40 #include <sys/kdb.h>
41 #include <machine/armreg.h>
42 #include <machine/asm.h>
43 #include <machine/cpufunc.h>
44 #include <machine/db_machdep.h>
45 #include <machine/vmparam.h>
46 #include <ddb/ddb.h>
47 #include <ddb/db_access.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_output.h>
50 
51 #define INKERNEL(va)	(((vm_offset_t)(va)) >= VM_MIN_KERNEL_ADDRESS)
52 
53 int  db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
54 int  db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
55 void db_md_list_watchpoints(void);
56 /*
57  * APCS stack frames are awkward beasts, so I don't think even trying to use
58  * a structure to represent them is a good idea.
59  *
60  * Here's the diagram from the APCS.  Increasing address is _up_ the page.
61  *
62  *          save code pointer       [fp]        <- fp points to here
63  *          return link value       [fp, #-4]
64  *          return sp value         [fp, #-8]
65  *          return fp value         [fp, #-12]
66  *          [saved v7 value]
67  *          [saved v6 value]
68  *          [saved v5 value]
69  *          [saved v4 value]
70  *          [saved v3 value]
71  *          [saved v2 value]
72  *          [saved v1 value]
73  *          [saved a4 value]
74  *          [saved a3 value]
75  *          [saved a2 value]
76  *          [saved a1 value]
77  *
78  * The save code pointer points twelve bytes beyond the start of the
79  * code sequence (usually a single STM) that created the stack frame.
80  * We have to disassemble it if we want to know which of the optional
81  * fields are actually present.
82  */
83 
84 #define FR_SCP	(0)
85 #define FR_RLV	(-1)
86 #define FR_RSP	(-2)
87 #define FR_RFP	(-3)
88 
89 static void
90 db_stack_trace_cmd(addr, have_addr, count, modif)
91 	db_expr_t       addr;
92 	int             have_addr;
93 	db_expr_t       count;
94 	char            *modif;
95 {
96 	u_int32_t	*frame, *lastframe;
97 	c_db_sym_t sym;
98 	db_expr_t pc;
99 	char c, *cp = modif;
100 	const char *name;
101 	db_expr_t value;
102 	db_expr_t offset;
103 	boolean_t	kernel_only = TRUE;
104 	boolean_t	trace_thread = FALSE;
105 	int	scp_offset;
106 
107 	if (kdb_frame == NULL && !have_addr)
108 		return;
109 	while ((c = *cp++) != 0) {
110 		if (c == 'u')
111 			kernel_only = FALSE;
112 		if (c == 't')
113 			trace_thread = TRUE;
114 	}
115 
116 	if (!have_addr)
117 		frame = (u_int32_t *)(kdb_frame->tf_r11);
118 	else {
119 		if (trace_thread) {
120 			struct proc *p;
121 			struct thread *td;
122 			pid_t pid = (pid_t)addr;
123 			LIST_FOREACH(p, &allproc, p_list) {
124 				if (p->p_pid == pid)
125 					break;
126 			}
127 
128 			if (p == NULL) {
129 				db_printf("not found\n");
130 				return;
131 			}
132 			if (!(p->p_sflag & PS_INMEM)) {
133 				db_printf("swapped out\n");
134 				return;
135 			}
136 			td = FIRST_THREAD_IN_PROC(p);
137 			frame = (u_int32_t *)(td->td_pcb->un_32.pcb32_r11);
138 			db_printf("at %p\n", frame);
139 		} else
140 			frame = (u_int32_t *)(addr);
141 	}
142 	lastframe = NULL;
143 	scp_offset = -(get_pc_str_offset() >> 2);
144 
145 	while (count-- && frame != NULL) {
146 		db_addr_t	scp;
147 		u_int32_t	savecode;
148 		int		r;
149 		u_int32_t	*rp;
150 		const char	*sep;
151 
152 		/*
153 		 * In theory, the SCP isn't guaranteed to be in the function
154 		 * that generated the stack frame.  We hope for the best.
155 		 */
156 		scp = frame[FR_SCP];
157 
158 		db_printsym(scp, DB_STGY_PROC);
159 		db_printf("\n\t");
160 		pc = kdb_frame->tf_pc;
161 		sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
162 		if (sym == C_DB_SYM_NULL) {
163 			value = 0;
164 			name = "(null)";
165 		} else
166 			db_symbol_values(sym, &name, &value);
167 		db_printf("%s() at ", name);
168 		db_printsym(pc, DB_STGY_PROC);
169 		db_printf("\n");
170 #ifdef __PROG26
171 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
172 		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
173 		db_printf(")\n");
174 #else
175 		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
176 		db_printsym(frame[FR_RLV], DB_STGY_PROC);
177 		db_printf(")\n");
178 #endif
179 		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
180 
181 		savecode = ((u_int32_t *)scp)[scp_offset];
182 		if ((savecode & 0x0e100000) == 0x08000000) {
183 			/* Looks like an STM */
184 			rp = frame - 4;
185 			sep = "\n\t";
186 			for (r = 10; r >= 0; r--) {
187 				if (savecode & (1 << r)) {
188 					db_printf("%sr%d=0x%08x",
189 					    sep, r, *rp--);
190 					sep = (frame - rp) % 4 == 2 ?
191 					    "\n\t" : " ";
192 				}
193 			}
194 		}
195 
196 		db_printf("\n");
197 
198 		/*
199 		 * Switch to next frame up
200 		 */
201 		if (frame[FR_RFP] == 0)
202 			break; /* Top of stack */
203 
204 		lastframe = frame;
205 		frame = (u_int32_t *)(frame[FR_RFP]);
206 
207 		if (INKERNEL((int)frame)) {
208 			/* staying in kernel */
209 			if (frame <= lastframe) {
210 				db_printf("Bad frame pointer: %p\n", frame);
211 				break;
212 			}
213 		} else if (INKERNEL((int)lastframe)) {
214 			/* switch from user to kernel */
215 			if (kernel_only)
216 				break;	/* kernel stack only */
217 		} else {
218 			/* in user */
219 			if (frame <= lastframe) {
220 				db_printf("Bad user frame pointer: %p\n",
221 					  frame);
222 				break;
223 			}
224 		}
225 	}
226 }
227 
228 /* XXX stubs */
229 void
230 db_md_list_watchpoints()
231 {
232 }
233 
234 int
235 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
236 {
237 	return (0);
238 }
239 
240 int
241 db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
242 {
243 	return (0);
244 }
245 
246 int
247 db_trace_thread(struct thread *thr, int count)
248 {
249 	uint32_t addr;
250 
251 	if (thr == curthread)
252 		addr = (uint32_t)__builtin_frame_address(0);
253 	else
254 		addr = thr->td_pcb->un_32.pcb32_r11;
255 	db_stack_trace_cmd(addr, 1, -1, NULL);
256 	return (0);
257 }
258 
259 void
260 db_trace_self(void)
261 {
262 	db_trace_thread(curthread, -1);
263 }
264