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