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