xref: /openbsd/sys/arch/i386/stand/libsa/debug.c (revision 78b63d65)
1 /*	$OpenBSD: debug.c,v 1.7 1998/06/09 13:45:07 mickey Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Michael Shalayeff.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  */
34 
35 #include <lib/libsa/stand.h>
36 #include <debug.h>
37 #include <dev/cons.h>
38 
39 #define	VBASE	(0xb8000)
40 
41 char *const reg_names[] = { REG_NAMES };
42 const int nreg = NENTS(reg_names);
43 struct reg reg;
44 u_int32_t *const reg_values[] = { REG_VALUES(reg) };
45 char *const trap_names[] = { TRAP_NAMES };
46 
47 void d_putc __P((dev_t, int));
48 
49 #ifdef DEBUG_DEBUG
50 #define	CKPT(c)	(*(u_short volatile *)(VBASE+160) = (0x1700 | (c)))
51 #else
52 #define	CKPT(c)	/* c */
53 #endif
54 
55 int
56 debug_init()
57 {
58 	return 0;
59 }
60 
61 
62 void
63 dump_regs(trapno, arg)
64 	u_int trapno, arg;
65 {
66 	register int i;
67 	/* make it local, so it won't rely on .data/.bss corruption */
68 	struct consdev d_cons, *save_cons;
69 
70 	/* init cons mod */
71 	save_cons = cn_tab;
72 	bzero(&d_cons, sizeof(d_cons));
73 	d_cons.cn_putc = &d_putc;
74 	cn_tab = &d_cons;
75 
76 	/* Trap info */
77 	printf("\ftrap: %u(%x): %s\ncn_tab=%p\n",
78 		trapno, arg, trap_names[trapno], save_cons);
79 
80 	/* Register dump */
81 	for(i = 1; i <= nreg; i++)
82 		printf("%s\t%x%c", reg_names[i-1], *reg_values[i-1],
83 			((i%4)? ' ': '\n'));
84 
85 	dump_mem("Code dump", (void *)*reg_values[8], 8);
86 	/* %ebx (void*)((*reg_values[3] + 15) & ~0x0F) */
87 	dump_mem("Memory dump", (void *)0x1a000, 48);
88 	dump_mem("Stack trace", (void *)(*reg_values[4]), 48);
89 
90 	/* restore the console */
91 	cn_tab = save_cons;
92 }
93 
94 void
95 dump_mem(l, p, n)
96 	char *l;
97 	void *p;
98 	size_t n;
99 {
100 	register int i;
101 	printf("%s [%p]:%s", l, p, (n > 6? "\n":" "));
102 	for(i = 1; i <= n; i++)
103 		printf("%x%c", *(u_int32_t *)p++, ((i%8)? ' ': '\n'));
104 	if (n % 8)
105 		printf ("\n");
106 }
107 
108 
109 u_int d_pos;
110 
111 void
112 d_putc(d, c)
113 	dev_t d;
114 	int c;
115 {
116 	switch (c) {
117 	case '\n':	d_pos += 80;					break;
118 	case '\r':	d_pos -= d_pos % 80;				break;
119 	case '\b':	d_pos--;					break;
120 	case '\f':	bzero((void *)VBASE, 80*25*2); d_pos = 0;	break;
121 		/* print it */
122 	default:
123 		((u_int16_t volatile *)VBASE)[d_pos++] = 0x0700 | (u_char)c;
124 		break;
125 	}
126 }
127