unwind.c revision 236991
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 236991 2012-06-13 04:59:55Z imp $");
34#include <sys/param.h>
35#include <sys/systm.h>
36
37
38#include <sys/proc.h>
39#include <sys/kdb.h>
40#include <sys/stack.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/pcb.h>
46#include <machine/stack.h>
47#include <machine/vmparam.h>
48#include <ddb/ddb.h>
49#include <ddb/db_access.h>
50#include <ddb/db_sym.h>
51#include <ddb/db_output.h>
52
53/*
54 * APCS stack frames are awkward beasts, so I don't think even trying to use
55 * a structure to represent them is a good idea.
56 *
57 * Here's the diagram from the APCS.  Increasing address is _up_ the page.
58 *
59 *          save code pointer       [fp]        <- fp points to here
60 *          return link value       [fp, #-4]
61 *          return sp value         [fp, #-8]
62 *          return fp value         [fp, #-12]
63 *          [saved v7 value]
64 *          [saved v6 value]
65 *          [saved v5 value]
66 *          [saved v4 value]
67 *          [saved v3 value]
68 *          [saved v2 value]
69 *          [saved v1 value]
70 *          [saved a4 value]
71 *          [saved a3 value]
72 *          [saved a2 value]
73 *          [saved a1 value]
74 *
75 * The save code pointer points twelve bytes beyond the start of the
76 * code sequence (usually a single STM) that created the stack frame.
77 * We have to disassemble it if we want to know which of the optional
78 * fields are actually present.
79 */
80
81static void
82db_stack_trace_cmd(db_expr_t addr, db_expr_t count, boolean_t kernel_only)
83{
84	u_int32_t	*frame, *lastframe;
85	c_db_sym_t sym;
86	const char *name;
87	db_expr_t value;
88	db_expr_t offset;
89	int	scp_offset;
90
91	frame = (u_int32_t *)addr;
92	lastframe = NULL;
93	scp_offset = -(get_pc_str_offset() >> 2);
94
95	while (count-- && frame != NULL && !db_pager_quit) {
96		db_addr_t	scp;
97		u_int32_t	savecode;
98		int		r;
99		u_int32_t	*rp;
100		const char	*sep;
101
102		/*
103		 * In theory, the SCP isn't guaranteed to be in the function
104		 * that generated the stack frame.  We hope for the best.
105		 */
106		scp = frame[FR_SCP];
107
108		sym = db_search_symbol(scp, DB_STGY_ANY, &offset);
109		if (sym == C_DB_SYM_NULL) {
110			value = 0;
111			name = "(null)";
112		} else
113			db_symbol_values(sym, &name, &value);
114		db_printf("%s() at ", name);
115		db_printsym(scp, DB_STGY_PROC);
116		db_printf("\n");
117#ifdef __PROG26
118		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC);
119		db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC);
120		db_printf(")\n");
121#else
122		db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]);
123		db_printsym(frame[FR_RLV], DB_STGY_PROC);
124		db_printf(")\n");
125#endif
126		db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]);
127
128		savecode = ((u_int32_t *)scp)[scp_offset];
129		if ((savecode & 0x0e100000) == 0x08000000) {
130			/* Looks like an STM */
131			rp = frame - 4;
132			sep = "\n\t";
133			for (r = 10; r >= 0; r--) {
134				if (savecode & (1 << r)) {
135					db_printf("%sr%d=0x%08x",
136					    sep, r, *rp--);
137					sep = (frame - rp) % 4 == 2 ?
138					    "\n\t" : " ";
139				}
140			}
141		}
142
143		db_printf("\n");
144
145		/*
146		 * Switch to next frame up
147		 */
148		if (frame[FR_RFP] == 0)
149			break; /* Top of stack */
150
151		lastframe = frame;
152		frame = (u_int32_t *)(frame[FR_RFP]);
153
154		if (INKERNEL((int)frame)) {
155			/* staying in kernel */
156			if (frame <= lastframe) {
157				db_printf("Bad frame pointer: %p\n", frame);
158				break;
159			}
160		} else if (INKERNEL((int)lastframe)) {
161			/* switch from user to kernel */
162			if (kernel_only)
163				break;	/* kernel stack only */
164		} else {
165			/* in user */
166			if (frame <= lastframe) {
167				db_printf("Bad user frame pointer: %p\n",
168					  frame);
169				break;
170			}
171		}
172	}
173}
174
175/* XXX stubs */
176void
177db_md_list_watchpoints()
178{
179}
180
181int
182db_md_clr_watchpoint(db_expr_t addr, db_expr_t size)
183{
184	return (0);
185}
186
187int
188db_md_set_watchpoint(db_expr_t addr, db_expr_t size)
189{
190	return (0);
191}
192
193int
194db_trace_thread(struct thread *thr, int count)
195{
196	struct pcb *ctx;
197
198	if (thr != curthread) {
199		ctx = kdb_thr_ctx(thr);
200		db_stack_trace_cmd(ctx->un_32.pcb32_r11, -1, TRUE);
201	} else
202		db_trace_self();
203	return (0);
204}
205
206void
207db_trace_self(void)
208{
209	db_addr_t addr;
210
211	addr = (db_addr_t)__builtin_frame_address(0);
212	db_stack_trace_cmd(addr, -1, FALSE);
213}
214