1 /* $NetBSD: regdump.c,v 1.16 2024/01/13 00:44:42 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 1988 University of Utah. 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * the Systems Programming Group of the University of Utah Computer 10 * Science Department. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: Utah Hdr: machdep.c 1.74 92/12/20 37 * from: @(#)machdep.c 8.10 (Berkeley) 4/20/94 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: regdump.c,v 1.16 2024/01/13 00:44:42 thorpej Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/proc.h> 46 47 #include <uvm/uvm_extern.h> 48 49 #include <m68k/m68k.h> 50 #include <m68k/fcode.h> 51 #include <m68k/frame.h> 52 #include <m68k/reg.h> 53 54 #include <machine/psl.h> 55 56 static void dumpmem(int *, int, int); 57 static char *hexstr(int, int); 58 59 /* 60 * Print a register and stack dump. 61 */ 62 void 63 regdump(struct trapframe *tf, int sbytes) 64 { 65 static int doingdump = 0; 66 int i; 67 int s; 68 69 if (doingdump) 70 return; 71 s = splhigh(); 72 doingdump = 1; 73 printf("pid = %d, lid = %d, pc = %s, ", 74 curproc ? curproc->p_pid : -1, 75 curlwp ? curlwp->l_lid : -1, hexstr(tf->tf_pc, 8)); 76 printf("ps = %s, ", hexstr(tf->tf_sr, 4)); 77 printf("sfc = %d, ", getsfc() & 7); 78 printf("dfc = %d\n", getdfc() & 7); 79 printf("Registers:\n "); 80 for (i = 0; i < 8; i++) 81 printf(" %d", i); 82 printf("\ndreg:"); 83 for (i = 0; i < 8; i++) 84 printf(" %s", hexstr(tf->tf_regs[i], 8)); 85 printf("\nareg:"); 86 for (i = 0; i < 8; i++) 87 printf(" %s", hexstr(tf->tf_regs[i+8], 8)); 88 if (sbytes > 0) { 89 /* 90 * XXXGCC12. 91 * This accesses beyond what "trapframe *tf" technically supplies. 92 */ 93 #pragma GCC push_options 94 #pragma GCC diagnostic ignored "-Warray-bounds" 95 if (tf->tf_sr & PSL_S) { 96 printf("\n\nKernel stack (%s):", 97 hexstr((int)(((int *)(void *)&tf)-1), 8)); 98 dumpmem(((int *)(void *)&tf)-1, sbytes, 0); 99 } else { 100 printf("\n\nUser stack (%s):", 101 hexstr(tf->tf_regs[SP], 8)); 102 dumpmem((int *)tf->tf_regs[SP], sbytes, 1); 103 } 104 #pragma GCC pop_options 105 } 106 doingdump = 0; 107 splx(s); 108 } 109 110 static void 111 dumpmem(int *ptr, int sz, int ustack) 112 { 113 int i, val; 114 int limit; 115 116 /* Stay in the same page */ 117 limit = ((int)ptr) | (PAGE_SIZE - 3); 118 119 for (i = 0; i < sz; i++) { 120 if ((i & 7) == 0) 121 printf("\n%s: ", hexstr((int)ptr, 6)); 122 else 123 printf(" "); 124 if (ustack == 1) { 125 if (ufetch_int((void *)(ptr++), (u_int *)&val) != 0) 126 break; 127 } else { 128 if (((int) ptr) >= limit) 129 break; 130 val = *ptr++; 131 } 132 printf("%s", hexstr(val, 8)); 133 } 134 printf("\n"); 135 } 136 137 static char * 138 hexstr(int val, int len) 139 { 140 static char nbuf[9]; 141 int x, i; 142 143 if (len > 8) { 144 nbuf[0] = '\0'; 145 return(nbuf); 146 } 147 nbuf[len] = '\0'; 148 for (i = len-1; i >= 0; --i) { 149 x = val & 0xF; 150 /* Isn't this a cool trick? */ 151 nbuf[i] = HEXDIGITS[x]; 152 val >>= 4; 153 } 154 return nbuf; 155 } 156