1 /* $OpenBSD: debug.c,v 1.16 2010/12/06 22:51:46 jasper 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 <sys/param.h>
31 #include <lib/libsa/stand.h>
32 #include <debug.h>
33 #include <dev/cons.h>
34
35 #define VBASE (0xb8000)
36
37 char *const reg_names[] = { REG_NAMES };
38 const int nreg = nitems(reg_names);
39 struct reg reg;
40 u_int32_t *const reg_values[] = { REG_VALUES(reg) };
41 char *const trap_names[] = { TRAP_NAMES };
42
43 void d_putc(dev_t, int);
44
45 #ifdef DEBUG_DEBUG
46 #define CKPT(c) (*(u_short volatile *)(VBASE+160) = (0x1700 | (c)))
47 #else
48 #define CKPT(c) /* c */
49 #endif
50
51 void
debug_init(void)52 debug_init(void)
53 {
54 }
55
56
57 void
dump_regs(u_int trapno,u_int arg)58 dump_regs(u_int trapno, u_int arg)
59 {
60 register int i;
61 /* make it local, so it won't rely on .data/.bss corruption */
62 struct consdev d_cons, *save_cons;
63
64 /* init cons mod */
65 save_cons = cn_tab;
66 bzero(&d_cons, sizeof(d_cons));
67 d_cons.cn_putc = &d_putc;
68 cn_tab = &d_cons;
69
70 /* Trap info */
71 printf("\ftrap: %u(%x): %s\ncn_tab=%p\n",
72 trapno, arg, trap_names[trapno], save_cons);
73
74 /* Register dump */
75 for (i = 1; i <= nreg; i++)
76 printf("%s\t%x%c", reg_names[i-1], *reg_values[i-1],
77 ((i%4)? ' ': '\n'));
78
79 dump_mem("Code dump", (void *)*reg_values[8], 8);
80 /* %ebx (void *)((*reg_values[3] + 15) & ~0x0F) */
81 dump_mem("Memory dump", (void *)0x1a000, 48);
82 dump_mem("Stack trace", (void *)(*reg_values[4]), 48);
83
84 /* restore the console */
85 cn_tab = save_cons;
86 }
87
88 void
dump_mem(char * l,void * p,size_t n)89 dump_mem(char *l, void *p, size_t n)
90 {
91 register int i;
92
93 printf("%s [%p]:%s", l, p, (n > 6? "\n":" "));
94 for (i = 1; i <= n; i++)
95 printf("%x%c", *(u_int32_t *)p++, ((i%8)? ' ': '\n'));
96 if (n % 8)
97 printf("\n");
98 }
99
100
101 u_int d_pos;
102
103 void
d_putc(dev_t d,int c)104 d_putc(dev_t d, int c)
105 {
106 switch (c) {
107 case '\n': d_pos += 80; break;
108 case '\r': d_pos -= d_pos % 80; break;
109 case '\b': d_pos--; break;
110 case '\f': bzero((void *)VBASE, 80*25*2); d_pos = 0; break;
111 /* print it */
112 default:
113 ((u_int16_t volatile *)VBASE)[d_pos++] = 0x0700 | (u_char)c;
114 break;
115 }
116 }
117