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/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/pcb.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, db_expr_t count) 91 { 92 u_int32_t *frame, *lastframe; 93 c_db_sym_t sym; 94 const char *name; 95 db_expr_t value; 96 db_expr_t offset; 97 boolean_t kernel_only = TRUE; 98 int scp_offset, quit; 99 100 frame = (u_int32_t *)addr; 101 lastframe = NULL; 102 scp_offset = -(get_pc_str_offset() >> 2); 103 104 quit = 0; 105 db_setup_paging(db_simple_pager, &quit, db_lines_per_page); 106 while (count-- && frame != NULL && !quit) { 107 db_addr_t scp; 108 u_int32_t savecode; 109 int r; 110 u_int32_t *rp; 111 const char *sep; 112 113 /* 114 * In theory, the SCP isn't guaranteed to be in the function 115 * that generated the stack frame. We hope for the best. 116 */ 117 scp = frame[FR_SCP]; 118 119 sym = db_search_symbol(scp, DB_STGY_ANY, &offset); 120 if (sym == C_DB_SYM_NULL) { 121 value = 0; 122 name = "(null)"; 123 } else 124 db_symbol_values(sym, &name, &value); 125 db_printf("%s() at ", name); 126 db_printsym(scp, DB_STGY_PROC); 127 db_printf("\n"); 128 #ifdef __PROG26 129 db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV] & R15_PC); 130 db_printsym(frame[FR_RLV] & R15_PC, DB_STGY_PROC); 131 db_printf(")\n"); 132 #else 133 db_printf("scp=0x%08x rlv=0x%08x (", scp, frame[FR_RLV]); 134 db_printsym(frame[FR_RLV], DB_STGY_PROC); 135 db_printf(")\n"); 136 #endif 137 db_printf("\trsp=0x%08x rfp=0x%08x", frame[FR_RSP], frame[FR_RFP]); 138 139 savecode = ((u_int32_t *)scp)[scp_offset]; 140 if ((savecode & 0x0e100000) == 0x08000000) { 141 /* Looks like an STM */ 142 rp = frame - 4; 143 sep = "\n\t"; 144 for (r = 10; r >= 0; r--) { 145 if (savecode & (1 << r)) { 146 db_printf("%sr%d=0x%08x", 147 sep, r, *rp--); 148 sep = (frame - rp) % 4 == 2 ? 149 "\n\t" : " "; 150 } 151 } 152 } 153 154 db_printf("\n"); 155 156 /* 157 * Switch to next frame up 158 */ 159 if (frame[FR_RFP] == 0) 160 break; /* Top of stack */ 161 162 lastframe = frame; 163 frame = (u_int32_t *)(frame[FR_RFP]); 164 165 if (INKERNEL((int)frame)) { 166 /* staying in kernel */ 167 if (frame <= lastframe) { 168 db_printf("Bad frame pointer: %p\n", frame); 169 break; 170 } 171 } else if (INKERNEL((int)lastframe)) { 172 /* switch from user to kernel */ 173 if (kernel_only) 174 break; /* kernel stack only */ 175 } else { 176 /* in user */ 177 if (frame <= lastframe) { 178 db_printf("Bad user frame pointer: %p\n", 179 frame); 180 break; 181 } 182 } 183 } 184 } 185 186 /* XXX stubs */ 187 void 188 db_md_list_watchpoints() 189 { 190 } 191 192 int 193 db_md_clr_watchpoint(db_expr_t addr, db_expr_t size) 194 { 195 return (0); 196 } 197 198 int 199 db_md_set_watchpoint(db_expr_t addr, db_expr_t size) 200 { 201 return (0); 202 } 203 204 int 205 db_trace_thread(struct thread *thr, int count) 206 { 207 uint32_t addr; 208 209 if (thr == curthread) 210 addr = (uint32_t)__builtin_frame_address(0); 211 else 212 addr = thr->td_pcb->un_32.pcb32_r11; 213 db_stack_trace_cmd(addr, -1); 214 return (0); 215 } 216 217 void 218 db_trace_self(void) 219 { 220 db_trace_thread(curthread, -1); 221 } 222