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