1 /* $OpenBSD: db_interface.c,v 1.49 2023/04/26 16:53:58 claudio Exp $ */ 2 3 /* 4 * Copyright (c) 1999-2003 Michael Shalayeff 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 * THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #undef DDB_DEBUG 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/stacktrace.h> 34 35 #include <machine/db_machdep.h> 36 #include <machine/frame.h> 37 #include <machine/cpufunc.h> 38 39 #include <ddb/db_access.h> 40 #include <ddb/db_command.h> 41 #include <ddb/db_output.h> 42 #include <ddb/db_run.h> 43 #include <ddb/db_sym.h> 44 #include <ddb/db_var.h> 45 #include <ddb/db_variables.h> 46 #include <ddb/db_extern.h> 47 #include <ddb/db_interface.h> 48 49 #include <dev/cons.h> 50 51 void kdbprinttrap(int, int); 52 53 extern char *trap_type[]; 54 extern int trap_types; 55 56 db_regs_t ddb_regs; 57 struct db_variable db_regs[] = { 58 { "flags", (long *)&ddb_regs.tf_flags, FCN_NULL }, 59 { "r1", (long *)&ddb_regs.tf_r1, FCN_NULL }, 60 { "rp", (long *)&ddb_regs.tf_rp, FCN_NULL }, 61 { "r3", (long *)&ddb_regs.tf_r3, FCN_NULL }, 62 { "r4", (long *)&ddb_regs.tf_r4, FCN_NULL }, 63 { "r5", (long *)&ddb_regs.tf_r5, FCN_NULL }, 64 { "r6", (long *)&ddb_regs.tf_r6, FCN_NULL }, 65 { "r7", (long *)&ddb_regs.tf_r7, FCN_NULL }, 66 { "r8", (long *)&ddb_regs.tf_r8, FCN_NULL }, 67 { "r9", (long *)&ddb_regs.tf_r9, FCN_NULL }, 68 { "r10", (long *)&ddb_regs.tf_r10, FCN_NULL }, 69 { "r11", (long *)&ddb_regs.tf_r11, FCN_NULL }, 70 { "r12", (long *)&ddb_regs.tf_r12, FCN_NULL }, 71 { "r13", (long *)&ddb_regs.tf_r13, FCN_NULL }, 72 { "r14", (long *)&ddb_regs.tf_r14, FCN_NULL }, 73 { "r15", (long *)&ddb_regs.tf_r15, FCN_NULL }, 74 { "r16", (long *)&ddb_regs.tf_r16, FCN_NULL }, 75 { "r17", (long *)&ddb_regs.tf_r17, FCN_NULL }, 76 { "r18", (long *)&ddb_regs.tf_r18, FCN_NULL }, 77 { "r19", (long *)&ddb_regs.tf_t4, FCN_NULL }, 78 { "r20", (long *)&ddb_regs.tf_t3, FCN_NULL }, 79 { "r21", (long *)&ddb_regs.tf_t2, FCN_NULL }, 80 { "r22", (long *)&ddb_regs.tf_t1, FCN_NULL }, 81 { "r23", (long *)&ddb_regs.tf_arg3, FCN_NULL }, 82 { "r24", (long *)&ddb_regs.tf_arg2, FCN_NULL }, 83 { "r25", (long *)&ddb_regs.tf_arg1, FCN_NULL }, 84 { "r26", (long *)&ddb_regs.tf_arg0, FCN_NULL }, 85 { "r27", (long *)&ddb_regs.tf_dp, FCN_NULL }, 86 { "r28", (long *)&ddb_regs.tf_ret0, FCN_NULL }, 87 { "r29", (long *)&ddb_regs.tf_ret1, FCN_NULL }, 88 { "r30", (long *)&ddb_regs.tf_sp, FCN_NULL }, 89 { "r31", (long *)&ddb_regs.tf_r31, FCN_NULL }, 90 { "sar", (long *)&ddb_regs.tf_sar, FCN_NULL }, 91 92 { "rctr", (long *)&ddb_regs.tf_rctr, FCN_NULL }, 93 { "ccr", (long *)&ddb_regs.tf_ccr, FCN_NULL }, 94 { "eirr", (long *)&ddb_regs.tf_eirr, FCN_NULL }, 95 { "eiem", (long *)&ddb_regs.tf_eiem, FCN_NULL }, 96 { "iir", (long *)&ddb_regs.tf_iir, FCN_NULL }, 97 { "isr", (long *)&ddb_regs.tf_isr, FCN_NULL }, 98 { "ior", (long *)&ddb_regs.tf_ior, FCN_NULL }, 99 { "ipsw", (long *)&ddb_regs.tf_ipsw, FCN_NULL }, 100 { "iisqh", (long *)&ddb_regs.tf_iisq_head, FCN_NULL }, 101 { "iioqh", (long *)&ddb_regs.tf_iioq_head, FCN_NULL }, 102 { "iisqt", (long *)&ddb_regs.tf_iisq_tail, FCN_NULL }, 103 { "iioqt", (long *)&ddb_regs.tf_iioq_tail, FCN_NULL }, 104 105 { "sr0", (long *)&ddb_regs.tf_sr0, FCN_NULL }, 106 { "sr1", (long *)&ddb_regs.tf_sr1, FCN_NULL }, 107 { "sr2", (long *)&ddb_regs.tf_sr2, FCN_NULL }, 108 { "sr3", (long *)&ddb_regs.tf_sr3, FCN_NULL }, 109 { "sr4", (long *)&ddb_regs.tf_sr4, FCN_NULL }, 110 { "sr5", (long *)&ddb_regs.tf_sr5, FCN_NULL }, 111 { "sr6", (long *)&ddb_regs.tf_sr6, FCN_NULL }, 112 { "sr7", (long *)&ddb_regs.tf_sr7, FCN_NULL }, 113 114 { "pidr1", (long *)&ddb_regs.tf_pidr1, FCN_NULL }, 115 { "pidr2", (long *)&ddb_regs.tf_pidr2, FCN_NULL }, 116 #ifdef pbably_not_worth_it 117 { "pidr3", (long *)&ddb_regs.tf_pidr3, FCN_NULL }, 118 { "pidr4", (long *)&ddb_regs.tf_pidr4, FCN_NULL }, 119 #endif 120 121 { "vtop", (long *)&ddb_regs.tf_vtop, FCN_NULL }, 122 { "cr28", (long *)&ddb_regs.tf_cr28, FCN_NULL }, 123 { "cr30", (long *)&ddb_regs.tf_cr30, FCN_NULL }, 124 }; 125 struct db_variable *db_eregs = db_regs + nitems(db_regs); 126 127 void 128 db_enter(void) 129 { 130 extern int kernelmapped; /* from locore.S */ 131 if (kernelmapped) 132 __asm volatile ("break %0, %1" 133 :: "i" (HPPA_BREAK_KERNEL), "i" (HPPA_BREAK_KGDB)); 134 } 135 136 void 137 db_read_bytes(vaddr_t addr, size_t size, char *data) 138 { 139 register char *src = (char *)addr; 140 141 while (size--) 142 *data++ = *src++; 143 } 144 145 void 146 db_write_bytes(vaddr_t addr, size_t size, char *data) 147 { 148 register char *dst = (char *)addr; 149 150 while (size--) 151 *dst++ = *data++; 152 153 /* unfortunately ddb does not provide any hooks for these */ 154 ficache(HPPA_SID_KERNEL, (vaddr_t)data, size); 155 fdcache(HPPA_SID_KERNEL, (vaddr_t)data, size); 156 } 157 158 159 /* 160 * Print trap reason. 161 */ 162 void 163 kdbprinttrap(int type, int code) 164 { 165 type &= ~T_USER; /* just in case */ 166 db_printf("kernel: "); 167 if (type >= trap_types || type < 0) 168 db_printf("type 0x%x", type); 169 else 170 db_printf("%s", trap_type[type]); 171 db_printf(" trap, code=0x%x\n", code); 172 } 173 174 /* 175 * db_ktrap - field a BPT trap 176 */ 177 int 178 db_ktrap(int type, int code, db_regs_t *regs) 179 { 180 extern label_t *db_recover; 181 int s; 182 183 switch (type) { 184 case T_IBREAK: 185 case T_DBREAK: 186 case -1: 187 break; 188 default: 189 if (!db_panic) 190 return (0); 191 192 kdbprinttrap(type, code); 193 if (db_recover != 0) { 194 db_error("Caught exception in DDB; continuing...\n"); 195 /* NOT REACHED */ 196 } 197 } 198 199 /* XXX Should switch to kdb`s own stack here. */ 200 201 s = splhigh(); 202 ddb_regs = *regs; 203 db_active++; 204 cnpollc(1); 205 db_trap(type, code); 206 cnpollc(0); 207 db_active--; 208 splx(s); 209 210 *regs = ddb_regs; 211 212 return (1); 213 } 214 215 /* 216 * Validate an address for use as a breakpoint. 217 * Any address is allowed for now. 218 */ 219 int 220 db_valid_breakpoint(vaddr_t addr) 221 { 222 return (1); 223 } 224 225 void 226 db_stack_trace_print(db_expr_t addr, int have_addr, db_expr_t count, 227 char *modif, int (*pr)(const char *, ...)) 228 { 229 register_t *fp, pc, rp, *argp; 230 Elf_Sym *sym; 231 db_expr_t off; 232 char *name; 233 int nargs; 234 235 if (count < 0) 236 count = 65536; 237 238 if (!have_addr) { 239 fp = (register_t *)ddb_regs.tf_r3; 240 pc = ddb_regs.tf_iioq_head; 241 rp = ddb_regs.tf_rp; 242 } else { 243 fp = (register_t *)addr; 244 pc = 0; 245 rp = ((register_t *)fp)[-5]; 246 } 247 248 #ifdef DDB_DEBUG 249 (*pr) (">> %p, 0x%x, 0x%x\t", fp, pc, rp); 250 #endif 251 while (fp && count--) { 252 253 if (USERMODE(pc)) 254 return; 255 256 sym = db_search_symbol(pc, DB_STGY_ANY, &off); 257 db_symbol_values (sym, &name, NULL); 258 259 if (name == NULL) 260 (*pr)("%lx(", pc); 261 else 262 (*pr)("%s(", name); 263 264 /* args */ 265 nargs = 4; 266 /* 267 * XXX first four args are passed on registers, and may not 268 * be stored on stack, dunno how to recover their values yet 269 */ 270 for (argp = &fp[-9]; nargs--; argp--) { 271 (*pr)("%x%s", db_get_value((int)argp, 4, 0), 272 nargs? ",":""); 273 } 274 (*pr)(") at "); 275 db_printsym(pc, DB_STGY_PROC, pr); 276 (*pr)("\n"); 277 278 /* TODO: print locals */ 279 280 /* next frame */ 281 pc = rp; 282 rp = fp[-5]; 283 284 /* if a terminal frame and not a start of a page 285 * then skip the trapframe and the terminal frame */ 286 if (!fp[0]) { 287 struct trapframe *tf; 288 289 tf = (struct trapframe *)((char *)fp - sizeof(*tf)); 290 291 if (tf->tf_flags & TFF_SYS) 292 (*pr)("-- syscall #%d(%x, %x, %x, %x, ...)\n", 293 tf->tf_t1, tf->tf_arg0, tf->tf_arg1, 294 tf->tf_arg2, tf->tf_arg3); 295 else 296 (*pr)("-- trap #%d%s\n", tf->tf_flags & 0x3f, 297 (tf->tf_flags & T_USER)? " from user" : ""); 298 299 if (!(tf->tf_flags & TFF_LAST)) { 300 fp = (register_t *)tf->tf_r3; 301 pc = tf->tf_iioq_head; 302 rp = tf->tf_rp; 303 } else 304 fp = 0; 305 } else 306 fp = (register_t *)fp[0]; 307 #ifdef DDB_DEBUG 308 (*pr) (">> %x, %x, %x\t", fp, pc, rp); 309 #endif 310 } 311 312 if (count && pc) { 313 db_printsym(pc, DB_STGY_XTRN, pr); 314 (*pr)(":\n"); 315 } 316 } 317 318 void 319 stacktrace_save_at(struct stacktrace *st, unsigned int skip) 320 { 321 register_t *fp, pc, rp; 322 int i; 323 324 fp = (register_t *)__builtin_frame_address(0); 325 pc = 0; 326 rp = fp[-5]; 327 328 st->st_count = 0; 329 for (i = 0; i < STACKTRACE_MAX; i++) { 330 if (skip == 0) 331 st->st_pc[st->st_count++] = rp; 332 else 333 skip--; 334 335 /* next frame */ 336 pc = rp; 337 if (!fp[0] || USERMODE(pc)) 338 break; 339 340 rp = fp[-5]; 341 fp = (register_t *)fp[0]; 342 } 343 } 344 345 void 346 stacktrace_save_utrace(struct stacktrace *st) 347 { 348 st->st_count = 0; 349 } 350