xref: /netbsd/sys/arch/m68k/m68k/regdump.c (revision bf9ec67e)
1 /*	$NetBSD: regdump.c,v 1.2 1997/10/21 17:30:15 gwr 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. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	from: Utah Hdr: machdep.c 1.74 92/12/20
41  *	from: @(#)machdep.c	8.10 (Berkeley) 4/20/94
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/proc.h>
47 
48 #include <m68k/m68k.h>
49 #include <m68k/frame.h>
50 #include <m68k/reg.h>
51 
52 #include <machine/psl.h>
53 
54 static void dumpmem __P((int *, int, int));
55 static char *hexstr __P((int, int));
56 
57 /*
58  * Print a register and stack dump.
59  */
60 void
61 regdump(tf, sbytes)
62 	struct trapframe *tf; /* must not be register */
63 	int sbytes;
64 {
65 	static int doingdump = 0;
66 	register int i;
67 	int s;
68 
69 	if (doingdump)
70 		return;
71 	s = splhigh();
72 	doingdump = 1;
73 	printf("pid = %d, pc = %s, ",
74 	       curproc ? curproc->p_pid : -1, hexstr(tf->tf_pc, 8));
75 	printf("ps = %s, ", hexstr(tf->tf_sr, 4));
76 	printf("sfc = %d, ", getsfc() & 7);
77 	printf("dfc = %d\n", getdfc() & 7);
78 	printf("Registers:\n     ");
79 	for (i = 0; i < 8; i++)
80 		printf("        %d", i);
81 	printf("\ndreg:");
82 	for (i = 0; i < 8; i++)
83 		printf(" %s", hexstr(tf->tf_regs[i], 8));
84 	printf("\nareg:");
85 	for (i = 0; i < 8; i++)
86 		printf(" %s", hexstr(tf->tf_regs[i+8], 8));
87 	if (sbytes > 0) {
88 		if (tf->tf_sr & PSL_S) {
89 			printf("\n\nKernel stack (%s):",
90 			       hexstr((int)(((int *)&tf)-1), 8));
91 			dumpmem(((int *)&tf)-1, sbytes, 0);
92 		} else {
93 			printf("\n\nUser stack (%s):", hexstr(tf->tf_regs[SP], 8));
94 			dumpmem((int *)tf->tf_regs[SP], sbytes, 1);
95 		}
96 	}
97 	doingdump = 0;
98 	splx(s);
99 }
100 
101 static void
102 dumpmem(ptr, sz, ustack)
103 	register int *ptr;
104 	int sz, ustack;
105 {
106 	register int i, val;
107 	register int limit;
108 
109 	/* Stay in the same page */
110 	limit = ((int)ptr) | (NBPG-3);
111 
112 	for (i = 0; i < sz; i++) {
113 		if ((i & 7) == 0)
114 			printf("\n%s: ", hexstr((int)ptr, 6));
115 		else
116 			printf(" ");
117 		if (ustack == 1) {
118 			if ((val = fuword(ptr++)) == -1)
119 				break;
120 		} else {
121 			if (((int) ptr) >= limit)
122 				break;
123 			val = *ptr++;
124 		}
125 		printf("%s", hexstr(val, 8));
126 	}
127 	printf("\n");
128 }
129 
130 static char *
131 hexstr(val, len)
132 	register int val;
133 	int len;
134 {
135 	static char nbuf[9];
136 	register int x, i;
137 
138 	if (len > 8)
139 		return("");
140 	nbuf[len] = '\0';
141 	for (i = len-1; i >= 0; --i) {
142 		x = val & 0xF;
143 		/* Isn't this a cool trick? */
144 		nbuf[i] = "0123456789ABCDEF"[x];
145 		val >>= 4;
146 	}
147 	return(nbuf);
148 }
149