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