No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / powerpc / powerpc / db_trace.c
blob6edc7f1364a98303746e6c675fb09d2de206fbf6
1 /* $NetBSD: db_trace.c,v 1.49 2009/10/21 21:12:02 rmind Exp $ */
2 /* $OpenBSD: db_trace.c,v 1.3 1997/03/21 02:10:48 niklas Exp $ */
4 /*
5 * Mach Operating System
6 * Copyright (c) 1992 Carnegie Mellon University
7 * All Rights Reserved.
8 *
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19 * Carnegie Mellon requests users of this software to return to
21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
26 * any improvements or extensions that they make and grant Carnegie Mellon
27 * the rights to redistribute these changes.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.49 2009/10/21 21:12:02 rmind Exp $");
33 #include "opt_ppcarch.h"
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/kernel.h>
39 #include <uvm/uvm_extern.h>
41 #include <machine/db_machdep.h>
42 #include <machine/pmap.h>
43 #include <powerpc/spr.h>
45 #include <ddb/db_access.h>
46 #include <ddb/db_interface.h>
47 #include <ddb/db_sym.h>
48 #include <ddb/db_variables.h>
50 const struct db_variable db_regs[] = {
51 { "r0", (long *)&ddb_regs.r[0], FCN_NULL, NULL },
52 { "r1", (long *)&ddb_regs.r[1], FCN_NULL, NULL },
53 { "r2", (long *)&ddb_regs.r[2], FCN_NULL, NULL },
54 { "r3", (long *)&ddb_regs.r[3], FCN_NULL, NULL },
55 { "r4", (long *)&ddb_regs.r[4], FCN_NULL, NULL },
56 { "r5", (long *)&ddb_regs.r[5], FCN_NULL, NULL },
57 { "r6", (long *)&ddb_regs.r[6], FCN_NULL, NULL },
58 { "r7", (long *)&ddb_regs.r[7], FCN_NULL, NULL },
59 { "r8", (long *)&ddb_regs.r[8], FCN_NULL, NULL },
60 { "r9", (long *)&ddb_regs.r[9], FCN_NULL, NULL },
61 { "r10", (long *)&ddb_regs.r[10], FCN_NULL, NULL },
62 { "r11", (long *)&ddb_regs.r[11], FCN_NULL, NULL },
63 { "r12", (long *)&ddb_regs.r[12], FCN_NULL, NULL },
64 { "r13", (long *)&ddb_regs.r[13], FCN_NULL, NULL },
65 { "r14", (long *)&ddb_regs.r[14], FCN_NULL, NULL },
66 { "r15", (long *)&ddb_regs.r[15], FCN_NULL, NULL },
67 { "r16", (long *)&ddb_regs.r[16], FCN_NULL, NULL },
68 { "r17", (long *)&ddb_regs.r[17], FCN_NULL, NULL },
69 { "r18", (long *)&ddb_regs.r[18], FCN_NULL, NULL },
70 { "r19", (long *)&ddb_regs.r[19], FCN_NULL, NULL },
71 { "r20", (long *)&ddb_regs.r[20], FCN_NULL, NULL },
72 { "r21", (long *)&ddb_regs.r[21], FCN_NULL, NULL },
73 { "r22", (long *)&ddb_regs.r[22], FCN_NULL, NULL },
74 { "r23", (long *)&ddb_regs.r[23], FCN_NULL, NULL },
75 { "r24", (long *)&ddb_regs.r[24], FCN_NULL, NULL },
76 { "r25", (long *)&ddb_regs.r[25], FCN_NULL, NULL },
77 { "r26", (long *)&ddb_regs.r[26], FCN_NULL, NULL },
78 { "r27", (long *)&ddb_regs.r[27], FCN_NULL, NULL },
79 { "r28", (long *)&ddb_regs.r[28], FCN_NULL, NULL },
80 { "r29", (long *)&ddb_regs.r[29], FCN_NULL, NULL },
81 { "r30", (long *)&ddb_regs.r[30], FCN_NULL, NULL },
82 { "r31", (long *)&ddb_regs.r[31], FCN_NULL, NULL },
83 { "iar", (long *)&ddb_regs.iar, FCN_NULL, NULL },
84 { "msr", (long *)&ddb_regs.msr, FCN_NULL, NULL },
85 { "lr", (long *)&ddb_regs.lr, FCN_NULL, NULL },
86 { "ctr", (long *)&ddb_regs.ctr, FCN_NULL, NULL },
87 { "cr", (long *)&ddb_regs.cr, FCN_NULL, NULL },
88 { "xer", (long *)&ddb_regs.xer, FCN_NULL, NULL },
89 { "mq", (long *)&ddb_regs.mq, FCN_NULL, NULL },
90 #ifdef PPC_IBM4XX
91 { "dear", (long *)&ddb_regs.dear, FCN_NULL, NULL },
92 { "esr", (long *)&ddb_regs.esr, FCN_NULL, NULL },
93 { "pid", (long *)&ddb_regs.pid, FCN_NULL, NULL },
94 #endif
96 const struct db_variable * const db_eregs = db_regs + sizeof (db_regs)/sizeof (db_regs[0]);
99 * Frame tracing.
101 void
102 db_stack_trace_print(db_expr_t addr, bool have_addr, db_expr_t count,
103 const char *modif, void (*pr)(const char *, ...))
105 db_addr_t frame, lr, *args;
106 db_expr_t diff;
107 db_sym_t sym;
108 const char *symname;
109 const char *cp = modif;
110 char c;
111 bool kernel_only = true;
112 bool trace_thread = false;
113 bool lwpaddr = false;
114 extern int trapexit[], sctrapexit[];
115 bool full = false;
116 bool in_kernel = true;
118 while ((c = *cp++) != 0) {
119 if (c == 'a') {
120 lwpaddr = true;
121 trace_thread = true;
123 if (c == 't')
124 trace_thread = true;
125 if (c == 'u')
126 kernel_only = false;
127 if (c == 'f')
128 full = true;
131 if (have_addr) {
132 if (trace_thread) {
133 struct proc *p;
134 struct lwp *l;
135 struct pcb *pcb;
137 if (lwpaddr) {
138 l = (struct lwp *)addr;
139 p = l->l_proc;
140 (*pr)("trace: pid %d ", p->p_pid);
141 } else {
142 (*pr)("trace: pid %d ", (int)addr);
143 p = p_find(addr, PFIND_LOCKED);
144 if (p == NULL) {
145 (*pr)("not found\n");
146 return;
148 l = LIST_FIRST(&p->p_lwps);
149 KASSERT(l != NULL);
151 (*pr)("lid %d ", l->l_lid);
152 pcb = lwp_getpcb(l);
153 frame = (db_addr_t)pcb->pcb_sp;
154 (*pr)("at %p\n", frame);
155 } else
156 frame = (db_addr_t)addr;
157 } else {
158 frame = (db_addr_t)ddb_regs.r[1];
160 for (;;) {
161 if (frame < PAGE_SIZE)
162 break;
163 frame = *(db_addr_t *)frame;
164 next_frame:
165 args = (db_addr_t *)(frame + 8);
166 if (frame < PAGE_SIZE)
167 break;
168 if (count-- == 0)
169 break;
171 lr = *(db_addr_t *)(frame + 4) - 4;
172 if ((lr & 3) || (lr < 0x100)) {
173 (*pr)("saved LR(0x%x) is invalid.", lr);
174 break;
177 (*pr)("0x%08lx: ", frame);
178 if (lr + 4 == (db_addr_t) trapexit ||
179 lr + 4 == (db_addr_t) sctrapexit) {
180 const char *trapstr;
181 struct trapframe *tf = (struct trapframe *) (frame+8);
182 (*pr)("%s ", tf->srr1 & PSL_PR ? "user" : "kernel");
183 if (lr + 4 == (db_addr_t) sctrapexit) {
184 (*pr)("SC trap #%d by ", tf->fixreg[0]);
185 goto print_trap;
187 switch (tf->exc) {
188 case EXC_DSI:
189 #ifdef PPC_OEA
190 (*pr)("DSI %s trap @ %#x by ",
191 tf->dsisr & DSISR_STORE ? "write" : "read",
192 tf->dar);
193 #endif
194 #ifdef PPC_IBM4XX
195 (*pr)("DSI %s trap @ %#x by ",
196 tf->tf_xtra[TF_ESR] & ESR_DST ? "write" : "read",
197 tf->dar);
198 #endif
199 goto print_trap;
200 case EXC_ALI:
201 #ifdef PPC_OEA
202 (*pr)("ALI trap @ %#x (DSISR %#x) ",
203 tf->dar, tf->dsisr);
204 goto print_trap;
205 #else
206 trapstr = "ALI"; break;
207 #endif
208 case EXC_ISI: trapstr = "ISI"; break;
209 case EXC_PGM: trapstr = "PGM"; break;
210 case EXC_SC: trapstr = "SC"; break;
211 case EXC_EXI: trapstr = "EXI"; break;
212 case EXC_MCHK: trapstr = "MCHK"; break;
213 case EXC_VEC: trapstr = "VEC"; break;
214 case EXC_FPU: trapstr = "FPU"; break;
215 case EXC_FPA: trapstr = "FPA"; break;
216 case EXC_DECR: trapstr = "DECR"; break;
217 case EXC_BPT: trapstr = "BPT"; break;
218 case EXC_TRC: trapstr = "TRC"; break;
219 case EXC_RUNMODETRC: trapstr = "RUNMODETRC"; break;
220 case EXC_PERF: trapstr = "PERF"; break;
221 case EXC_SMI: trapstr = "SMI"; break;
222 case EXC_RST: trapstr = "RST"; break;
223 default: trapstr = NULL; break;
225 if (trapstr != NULL) {
226 (*pr)("%s trap by ", trapstr);
227 } else {
228 (*pr)("trap %#x by ", tf->exc);
230 print_trap:
231 lr = (db_addr_t) tf->srr0;
232 diff = 0;
233 symname = NULL;
234 if (in_kernel && (tf->srr1 & PSL_PR) == 0) {
235 sym = db_search_symbol(lr, DB_STGY_ANY, &diff);
236 db_symbol_values(sym, &symname, 0);
238 if (symname == NULL || !strcmp(symname, "end")) {
239 (*pr)("%p: srr1=%#x\n", lr, tf->srr1);
240 } else {
241 (*pr)("%s+%#x: srr1=%#x\n", symname,
242 diff, tf->srr1);
244 (*pr)("%-10s r1=%#x cr=%#x xer=%#x ctr=%#x",
245 "", tf->fixreg[1], tf->cr, tf->xer, tf->ctr);
246 #ifdef PPC_OEA
247 if (tf->exc == EXC_DSI)
248 (*pr)(" dsisr=%#x", tf->dsisr);
249 #ifdef PPC_OEA601
250 if ((mfpvr() >> 16) == MPC601)
251 (*pr)(" mq=%#x", tf->tf_xtra[TF_MQ]);
252 #endif /* PPC_OEA601 */
253 #endif /* PPC_OEA */
254 #ifdef PPC_IBM4XX
255 if (tf->exc == EXC_DSI)
256 (*pr)(" dear=%#x", tf->dar);
257 (*pr)(" esr=%#x pid=%#x", tf->tf_xtra[TF_ESR],
258 tf->tf_xtra[TF_PID]);
259 #endif
260 (*pr)("\n");
261 frame = (db_addr_t) tf->fixreg[1];
262 in_kernel = !(tf->srr1 & PSL_PR);
263 if (kernel_only && !in_kernel)
264 break;
265 goto next_frame;
268 diff = 0;
269 symname = NULL;
270 if (in_kernel) {
271 sym = db_search_symbol(lr, DB_STGY_ANY, &diff);
272 db_symbol_values(sym, &symname, 0);
274 if (symname == NULL || !strcmp(symname, "end"))
275 (*pr)("at %p", lr);
276 else
277 (*pr)("at %s+%#x", symname, diff);
278 if (full)
279 /* Print all the args stored in that stackframe. */
280 (*pr)("(%lx, %lx, %lx, %lx, %lx, %lx, %lx, %lx)",
281 args[0], args[1], args[2], args[3],
282 args[4], args[5], args[6], args[7]);
283 (*pr)("\n");