unwind.c revision 135529
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: head/sys/arm/arm/db_trace.c 135529 2004-09-20 19:05:32Z jhb $");
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
53int  db_md_set_watchpoint(db_expr_t addr, db_expr_t size);
54int  db_md_clr_watchpoint(db_expr_t addr, db_expr_t size);
55void 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
89static void
90db_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, quit;
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	quit = 0;
146	db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE);
147	while (count-- && frame != NULL && !quit) {
148		db_addr_t	scp;
149		u_int32_t	savecode;
150		int		r;
151		u_int32_t	*rp;
152		const char	*sep;
153
154		/*
155		 * In theory, the SCP isn't guaranteed to be in the function
156		 * that generated the stack frame.  We hope for the best.
157		 */
158		scp = frame[FR_SCP];
159
160		db_printsym(scp, DB_STGY_PROC);
161		db_printf("\n\t");
162		pc = kdb_frame->tf_pc;
163		sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
164		if (sym == C_DB_SYM_NULL) {
165			value = 0;
166			name = "(null)";
167		} else
168			db_symbol_values(sym, &name, &value);
169		db_printf("%s() at ", name);
170		db_printsym(pc, DB_STGY_PROC);
171		db_printf("\n");
172#ifdef __PROG26
173		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
174		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
175		db_printf(")\n");
176#else
177		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
178		db_printsym(frame[FR_RLV], DB_STGY_PROC);
179		db_printf(")\n");
180#endif
181		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
182
183		savecode = ((u_int32_t *)scp)[scp_offset];
184		if ((savecode & 0x0e100000) == 0x08000000) {
185			/* Looks like an STM */
186			rp = frame - 4;
187			sep = "\n\t";
188			for (r = 10; r >= 0; r--) {
189				if (savecode & (1 << r)) {
190					db_printf("%sr%d=0x%08x",
191					    sep, r, *rp--);
192					sep = (frame - rp) % 4 == 2 ?
193					    "\n\t" : " ";
194				}
195			}
196		}
197
198		db_printf("\n");
199
200		/*
201		 * Switch to next frame up
202		 */
203		if (frame[FR_RFP] == 0)
204			break; /* Top of stack */
205
206		lastframe = frame;
207		frame = (u_int32_t *)(frame[FR_RFP]);
208
209		if (INKERNEL((int)frame)) {
210			/* staying in kernel */
211			if (frame <= lastframe) {
212				db_printf("Bad frame pointer: %p\n", frame);
213				break;
214			}
215		} else if (INKERNEL((int)lastframe)) {
216			/* switch from user to kernel */
217			if (kernel_only)
218				break;	/* kernel stack only */
219		} else {
220			/* in user */
221			if (frame <= lastframe) {
222				db_printf("Bad user frame pointer: %p\n",
223					  frame);
224				break;
225			}
226		}
227	}
228}
229
230/* XXX stubs */
231void
232db_md_list_watchpoints()
233{
234}
235
236int
237db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
238{
239	return (0);
240}
241
242int
243db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
244{
245	return (0);
246}
247
248int
249db_trace_thread(struct thread *thr, int count)
250{
251	uint32_t addr;
252
253	if (thr == curthread)
254		addr = (uint32_t)__builtin_frame_address(0);
255	else
256		addr = thr->td_pcb->un_32.pcb32_r11;
257	db_stack_trace_cmd(addr, 1, -1, NULL);
258	return (0);
259}
260
261void
262db_trace_self(void)
263{
264	db_trace_thread(curthread, -1);
265}
266