1 /* $NetBSD: db_trace.c,v 1.20 2002/05/13 20:30:09 matt Exp $ */ 2 /* $OpenBSD: db_trace.c,v 1.3 1997/03/21 02:10:48 niklas Exp $ */ 3 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. 14 * 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. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie Mellon 27 * the rights to redistribute these changes. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kdb.h> 33 #include <sys/proc.h> 34 #include <sys/stack.h> 35 36 #include <vm/vm.h> 37 #include <vm/pmap.h> 38 #include <vm/vm_extern.h> 39 40 #include <machine/db_machdep.h> 41 #include <machine/pcb.h> 42 #include <machine/spr.h> 43 #include <machine/stack.h> 44 #include <machine/trap.h> 45 46 #include <ddb/ddb.h> 47 #include <ddb/db_access.h> 48 #include <ddb/db_sym.h> 49 #include <ddb/db_variables.h> 50 51 static db_varfcn_t db_frame; 52 53 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x) 54 55 #ifdef __powerpc64__ 56 #define CALLOFFSET 8 /* Include TOC reload slot */ 57 #else 58 #define CALLOFFSET 4 59 #endif 60 61 struct db_variable db_regs[] = { 62 { "r0", DB_OFFSET(fixreg[0]), db_frame }, 63 { "r1", DB_OFFSET(fixreg[1]), db_frame }, 64 { "r2", DB_OFFSET(fixreg[2]), db_frame }, 65 { "r3", DB_OFFSET(fixreg[3]), db_frame }, 66 { "r4", DB_OFFSET(fixreg[4]), db_frame }, 67 { "r5", DB_OFFSET(fixreg[5]), db_frame }, 68 { "r6", DB_OFFSET(fixreg[6]), db_frame }, 69 { "r7", DB_OFFSET(fixreg[7]), db_frame }, 70 { "r8", DB_OFFSET(fixreg[8]), db_frame }, 71 { "r9", DB_OFFSET(fixreg[9]), db_frame }, 72 { "r10", DB_OFFSET(fixreg[10]), db_frame }, 73 { "r11", DB_OFFSET(fixreg[11]), db_frame }, 74 { "r12", DB_OFFSET(fixreg[12]), db_frame }, 75 { "r13", DB_OFFSET(fixreg[13]), db_frame }, 76 { "r14", DB_OFFSET(fixreg[14]), db_frame }, 77 { "r15", DB_OFFSET(fixreg[15]), db_frame }, 78 { "r16", DB_OFFSET(fixreg[16]), db_frame }, 79 { "r17", DB_OFFSET(fixreg[17]), db_frame }, 80 { "r18", DB_OFFSET(fixreg[18]), db_frame }, 81 { "r19", DB_OFFSET(fixreg[19]), db_frame }, 82 { "r20", DB_OFFSET(fixreg[20]), db_frame }, 83 { "r21", DB_OFFSET(fixreg[21]), db_frame }, 84 { "r22", DB_OFFSET(fixreg[22]), db_frame }, 85 { "r23", DB_OFFSET(fixreg[23]), db_frame }, 86 { "r24", DB_OFFSET(fixreg[24]), db_frame }, 87 { "r25", DB_OFFSET(fixreg[25]), db_frame }, 88 { "r26", DB_OFFSET(fixreg[26]), db_frame }, 89 { "r27", DB_OFFSET(fixreg[27]), db_frame }, 90 { "r28", DB_OFFSET(fixreg[28]), db_frame }, 91 { "r29", DB_OFFSET(fixreg[29]), db_frame }, 92 { "r30", DB_OFFSET(fixreg[30]), db_frame }, 93 { "r31", DB_OFFSET(fixreg[31]), db_frame }, 94 { "srr0", DB_OFFSET(srr0), db_frame }, 95 { "srr1", DB_OFFSET(srr1), db_frame }, 96 { "lr", DB_OFFSET(lr), db_frame }, 97 { "ctr", DB_OFFSET(ctr), db_frame }, 98 { "cr", DB_OFFSET(cr), db_frame }, 99 { "xer", DB_OFFSET(xer), db_frame }, 100 { "dar", DB_OFFSET(dar), db_frame }, 101 #ifdef AIM 102 { "dsisr", DB_OFFSET(cpu.aim.dsisr), db_frame }, 103 #endif 104 #if defined(BOOKE) 105 { "esr", DB_OFFSET(cpu.booke.esr), db_frame }, 106 #endif 107 }; 108 struct db_variable *db_eregs = db_regs + nitems(db_regs); 109 110 /* 111 * register variable handling 112 */ 113 static int 114 db_frame(struct db_variable *vp, db_expr_t *valuep, int op) 115 { 116 register_t *reg; 117 118 if (kdb_frame == NULL) 119 return (0); 120 reg = (register_t*)((uintptr_t)kdb_frame + (uintptr_t)vp->valuep); 121 if (op == DB_VAR_GET) 122 *valuep = *reg; 123 else 124 *reg = *valuep; 125 return (1); 126 } 127 128 /* 129 * Frame tracing. 130 */ 131 static int 132 db_backtrace(struct thread *td, db_addr_t fp, int count) 133 { 134 db_addr_t stackframe, lr, *args; 135 bool kernel_only = true; 136 bool full = false; 137 138 #if 0 139 { 140 register char *cp = modif; 141 register char c; 142 143 while ((c = *cp++) != 0) { 144 if (c == 't') 145 trace_thread = true; 146 if (c == 'u') 147 kernel_only = false; 148 if (c == 'f') 149 full = true; 150 } 151 } 152 #endif 153 154 stackframe = fp; 155 156 while (!db_pager_quit) { 157 if (stackframe < PAGE_SIZE) 158 break; 159 160 /* 161 * Locate the next frame by grabbing the backchain ptr 162 * from frame[0] 163 */ 164 stackframe = *(db_addr_t *)stackframe; 165 166 next_frame: 167 #ifdef __powerpc64__ 168 /* The saved arg values start at frame[6] */ 169 args = (db_addr_t *)(stackframe + 48); 170 #else 171 /* The saved arg values start at frame[2] */ 172 args = (db_addr_t *)(stackframe + 8); 173 #endif 174 175 if (stackframe < PAGE_SIZE) 176 break; 177 178 if (count-- == 0) 179 break; 180 181 /* 182 * Extract link register from frame and subtract 183 * 4 to convert into calling address (as opposed to 184 * return address) 185 */ 186 #ifdef __powerpc64__ 187 lr = *(db_addr_t *)(stackframe + 16) - 4; 188 #else 189 lr = *(db_addr_t *)(stackframe + 4) - 4; 190 #endif 191 if ((lr & 3) || (lr < 0x100)) { 192 db_printf("saved LR(0x%zx) is invalid.", lr); 193 break; 194 } 195 196 #ifdef __powerpc64__ 197 db_printf("0x%016lx: ", stackframe); 198 #else 199 db_printf("0x%08x: ", stackframe); 200 #endif 201 202 /* 203 * The trap code labels the return addresses from the 204 * call to C code as 'trapexit' and 'asttrapexit. Use this 205 * to determine if the callframe has to traverse a saved 206 * trap context 207 */ 208 if ((lr + CALLOFFSET == (db_addr_t) &trapexit) || 209 (lr + CALLOFFSET == (db_addr_t) &asttrapexit)) { 210 const char *trapstr; 211 struct trapframe *tf = (struct trapframe *)(args); 212 db_printf("%s ", tf->srr1 & PSL_PR ? "user" : "kernel"); 213 switch (tf->exc) { 214 case EXC_DSI: 215 /* XXX take advantage of the union. */ 216 #ifdef BOOKE 217 db_printf("DSI %s trap @ %#zx by ", 218 (tf->cpu.booke.esr & ESR_ST) ? "write" 219 : "read", tf->dar); 220 #else 221 db_printf("DSI %s trap @ %#zx by ", 222 (tf->cpu.aim.dsisr & DSISR_STORE) ? "write" 223 : "read", tf->dar); 224 #endif 225 goto print_trap; 226 case EXC_ALI: 227 db_printf("ALI trap @ %#zx (xSR %#x) ", 228 tf->dar, (uint32_t)tf->cpu.aim.dsisr); 229 goto print_trap; 230 #ifdef __powerpc64__ 231 case EXC_DSE: 232 db_printf("DSE trap @ %#zx by ", tf->dar); 233 goto print_trap; 234 case EXC_ISE: 235 db_printf("ISE trap @ %#zx by ", tf->srr0); 236 goto print_trap; 237 #endif 238 case EXC_ISI: trapstr = "ISI"; break; 239 case EXC_PGM: trapstr = "PGM"; break; 240 case EXC_SC: trapstr = "SC"; break; 241 case EXC_EXI: trapstr = "EXI"; break; 242 case EXC_MCHK: trapstr = "MCHK"; break; 243 case EXC_VEC: trapstr = "VEC"; break; 244 #if !defined(BOOKE) 245 case EXC_FPA: trapstr = "FPA"; break; 246 case EXC_BPT: trapstr = "BPT"; break; 247 case EXC_TRC: trapstr = "TRC"; break; 248 case EXC_RUNMODETRC: trapstr = "RUNMODETRC"; break; 249 case EXC_SMI: trapstr = "SMI"; break; 250 case EXC_RST: trapstr = "RST"; break; 251 #endif 252 case EXC_FPU: trapstr = "FPU"; break; 253 case EXC_DECR: trapstr = "DECR"; break; 254 case EXC_PERF: trapstr = "PERF"; break; 255 case EXC_VSX: trapstr = "VSX"; break; 256 case EXC_SOFT_PATCH: trapstr = "SOFT_PATCH"; break; 257 default: trapstr = NULL; break; 258 } 259 if (trapstr != NULL) { 260 db_printf("%s trap by ", trapstr); 261 } else { 262 db_printf("trap %#zx by ", tf->exc); 263 } 264 265 print_trap: 266 lr = (db_addr_t) tf->srr0; 267 db_printsym(lr, DB_STGY_ANY); 268 db_printf(": srr1=%#zx\n", tf->srr1); 269 db_printf("%-10s r1=%#zx cr=%#x xer=%#x ctr=%#zx", 270 "", tf->fixreg[1], (uint32_t)tf->cr, 271 (uint32_t)tf->xer, tf->ctr); 272 #ifdef __powerpc64__ 273 db_printf(" r2=%#zx", tf->fixreg[2]); 274 #endif 275 if (tf->exc == EXC_DSI) 276 db_printf(" sr=%#x", 277 (uint32_t)tf->cpu.aim.dsisr); 278 db_printf(" frame=%p\n", tf); 279 stackframe = (db_addr_t) tf->fixreg[1]; 280 if (kernel_only && (tf->srr1 & PSL_PR)) 281 break; 282 goto next_frame; 283 } 284 285 db_printf("at "); 286 db_printsym(lr, DB_STGY_PROC); 287 if (full) 288 /* Print all the args stored in that stackframe. */ 289 db_printf("(%zx, %zx, %zx, %zx, %zx, %zx, %zx, %zx)", 290 args[0], args[1], args[2], args[3], 291 args[4], args[5], args[6], args[7]); 292 db_printf("\n"); 293 } 294 295 return (0); 296 } 297 298 void 299 db_trace_self(void) 300 { 301 db_addr_t addr; 302 303 addr = (db_addr_t)__builtin_frame_address(0); 304 if (addr == 0) { 305 db_printf("Null frame address\n"); 306 return; 307 } 308 db_backtrace(curthread, *(db_addr_t *)addr, -1); 309 } 310 311 int 312 db_trace_thread(struct thread *td, int count) 313 { 314 struct pcb *ctx; 315 316 ctx = kdb_thr_ctx(td); 317 return (db_backtrace(td, (db_addr_t)ctx->pcb_sp, count)); 318 } 319