xref: /netbsd-src/sys/arch/hppa/hppa/db_interface.c (revision 8e6ab8837d8d6b9198e67c1c445300b483e2f304)
1 /*	$NetBSD: db_interface.c,v 1.2 2003/07/15 02:29:39 lukem Exp $	*/
2 
3 /*	$OpenBSD: db_interface.c,v 1.16 2001/03/22 23:31:45 mickey Exp $	*/
4 
5 /*
6  * Copyright (c) 1999-2000 Michael Shalayeff
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Michael Shalayeff.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: db_interface.c,v 1.2 2003/07/15 02:29:39 lukem Exp $");
37 
38 #define DDB_DEBUG
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 
43 #include <machine/db_machdep.h>
44 #include <machine/frame.h>
45 #include <machine/cpufunc.h>
46 
47 #include <ddb/db_access.h>
48 #include <ddb/db_command.h>
49 #include <ddb/db_output.h>
50 #include <ddb/db_run.h>
51 #include <ddb/db_sym.h>
52 #include <ddb/db_variables.h>
53 #include <ddb/db_extern.h>
54 #include <ddb/db_interface.h>
55 
56 #include <dev/cons.h>
57 
58 void kdbprinttrap __P((int, int));
59 
60 extern label_t *db_recover;
61 extern int db_active;
62 extern char *trap_type[];
63 extern int trap_types;
64 
65 db_regs_t	ddb_regs;
66 const struct db_variable db_regs[] = {
67 	{ "flags", (long *)&ddb_regs.tf_flags,  FCN_NULL },
68 	{ "r1",    (long *)&ddb_regs.tf_r1,  FCN_NULL },
69 	{ "rp",    (long *)&ddb_regs.tf_rp,  FCN_NULL },
70 	{ "r3",    (long *)&ddb_regs.tf_r3,  FCN_NULL },
71 	{ "r4",    (long *)&ddb_regs.tf_r4,  FCN_NULL },
72 	{ "r5",    (long *)&ddb_regs.tf_r5,  FCN_NULL },
73 	{ "r6",    (long *)&ddb_regs.tf_r6,  FCN_NULL },
74 	{ "r7",    (long *)&ddb_regs.tf_r7,  FCN_NULL },
75 	{ "r8",    (long *)&ddb_regs.tf_r8,  FCN_NULL },
76 	{ "r9",    (long *)&ddb_regs.tf_r9,  FCN_NULL },
77 	{ "r10",   (long *)&ddb_regs.tf_r10, FCN_NULL },
78 	{ "r11",   (long *)&ddb_regs.tf_r11, FCN_NULL },
79 	{ "r12",   (long *)&ddb_regs.tf_r12, FCN_NULL },
80 	{ "r13",   (long *)&ddb_regs.tf_r13, FCN_NULL },
81 	{ "r14",   (long *)&ddb_regs.tf_r14, FCN_NULL },
82 	{ "r15",   (long *)&ddb_regs.tf_r15, FCN_NULL },
83 	{ "r16",   (long *)&ddb_regs.tf_r16, FCN_NULL },
84 	{ "r17",   (long *)&ddb_regs.tf_r17, FCN_NULL },
85 	{ "r18",   (long *)&ddb_regs.tf_r18, FCN_NULL },
86 	{ "t4",    (long *)&ddb_regs.tf_t4,  FCN_NULL },
87 	{ "t3",    (long *)&ddb_regs.tf_t3,  FCN_NULL },
88 	{ "t2",    (long *)&ddb_regs.tf_t2,  FCN_NULL },
89 	{ "t1",    (long *)&ddb_regs.tf_t1,  FCN_NULL },
90 	{ "arg3",  (long *)&ddb_regs.tf_arg3,  FCN_NULL },
91 	{ "arg2",  (long *)&ddb_regs.tf_arg2,  FCN_NULL },
92 	{ "arg1",  (long *)&ddb_regs.tf_arg1,  FCN_NULL },
93 	{ "arg0",  (long *)&ddb_regs.tf_arg0,  FCN_NULL },
94 	{ "dp",    (long *)&ddb_regs.tf_dp,    FCN_NULL },
95 	{ "ret0",  (long *)&ddb_regs.tf_ret0,  FCN_NULL },
96 	{ "ret1",  (long *)&ddb_regs.tf_ret1,  FCN_NULL },
97 	{ "sp",    (long *)&ddb_regs.tf_sp,    FCN_NULL },
98 	{ "r31",   (long *)&ddb_regs.tf_r31,   FCN_NULL },
99 	{ "sar",   (long *)&ddb_regs.tf_sar,   FCN_NULL },
100 
101 	{ "eirr",  (long *)&ddb_regs.tf_eirr,  FCN_NULL },
102 	{ "eiem",  (long *)&ddb_regs.tf_eiem,  FCN_NULL },
103 	{ "iir",   (long *)&ddb_regs.tf_iir,   FCN_NULL },
104 	{ "isr",   (long *)&ddb_regs.tf_isr,   FCN_NULL },
105 	{ "ior",   (long *)&ddb_regs.tf_ior,   FCN_NULL },
106 	{ "ipsw",  (long *)&ddb_regs.tf_ipsw,  FCN_NULL },
107 	{ "iisqh", (long *)&ddb_regs.tf_iisq_head,  FCN_NULL },
108 	{ "iioqh", (long *)&ddb_regs.tf_iioq_head,  FCN_NULL },
109 	{ "iisqt", (long *)&ddb_regs.tf_iisq_tail,  FCN_NULL },
110 	{ "iioqt", (long *)&ddb_regs.tf_iioq_tail,  FCN_NULL },
111 
112 	{ "sr0",   (long *)&ddb_regs.tf_sr0,   FCN_NULL },
113 	{ "sr1",   (long *)&ddb_regs.tf_sr1,   FCN_NULL },
114 	{ "sr2",   (long *)&ddb_regs.tf_sr2,   FCN_NULL },
115 	{ "sr3",   (long *)&ddb_regs.tf_sr3,   FCN_NULL },
116 	{ "sr4",   (long *)&ddb_regs.tf_sr4,   FCN_NULL },
117 	{ "sr5",   (long *)&ddb_regs.tf_sr5,   FCN_NULL },
118 	{ "sr6",   (long *)&ddb_regs.tf_sr6,   FCN_NULL },
119 	{ "sr7",   (long *)&ddb_regs.tf_sr7,   FCN_NULL },
120 
121 	{ "pidr1", (long *)&ddb_regs.tf_pidr1, FCN_NULL },
122 	{ "pidr2", (long *)&ddb_regs.tf_pidr2, FCN_NULL },
123 #ifdef pbably_not_worth_it
124 	{ "pidr3", (long *)&ddb_regs.tf_pidr3, FCN_NULL },
125 	{ "pidr4", (long *)&ddb_regs.tf_pidr4, FCN_NULL },
126 #endif
127 
128 	{ "hptm",  (long *)&ddb_regs.tf_hptm,  FCN_NULL },
129 	{ "vtop",  (long *)&ddb_regs.tf_vtop,  FCN_NULL },
130 	{ "cr28",  (long *)&ddb_regs.tf_cr28,  FCN_NULL },
131 	{ "cr30",  (long *)&ddb_regs.tf_cr30,  FCN_NULL },
132 };
133 const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
134 int db_active = 0;
135 
136 void
137 Debugger()
138 {
139 	__asm __volatile ("break	%0, %1"
140 			  :: "i" (HPPA_BREAK_KERNEL), "i" (HPPA_BREAK_KGDB));
141 }
142 
143 /*
144  * Print trap reason.
145  */
146 void
147 kdbprinttrap(type, code)
148 	int type, code;
149 {
150 	db_printf("kernel: ");
151 	if (type >= trap_types || type < 0)
152 		db_printf("type %d", type);
153 	else
154 		db_printf("%s", trap_type[type]);
155 	db_printf(" trap, code=%x\n", code);
156 }
157 
158 /*
159  *  kdb_trap - field a BPT trap
160  */
161 int
162 kdb_trap(type, code, regs)
163 	int type, code;
164 	db_regs_t *regs;
165 {
166 	int s;
167 
168 	switch (type) {
169 	case T_RECOVERY:
170 	case T_IBREAK:
171 	case T_DBREAK:
172 	case -1:
173 		break;
174 	default:
175 		kdbprinttrap(type, code);
176 		if (db_recover != 0) {
177 			db_error("Caught exception in DDB; continuing...\n");
178 			/* NOT REACHED */
179 		}
180 		return (0);
181 	}
182 
183 	/* XXX Should switch to kdb`s own stack here. */
184 
185 	ddb_regs = *regs;
186 
187 	s = splhigh();
188 	db_active++;
189 	cnpollc(TRUE);
190 	db_trap(type, code);
191 	cnpollc(FALSE);
192 	db_active--;
193 	splx(s);
194 
195 	*regs = ddb_regs;
196 
197 	return (1);
198 }
199 
200 /*
201  *  Validate an address for use as a breakpoint.
202  *  Any address is allowed for now.
203  */
204 int
205 db_valid_breakpoint(addr)
206 	db_addr_t addr;
207 {
208 	return (1);
209 }
210 
211 void
212 db_stack_trace_print(addr, have_addr, count, modif, pr)
213 	db_expr_t	addr;
214 	int		have_addr;
215 	db_expr_t	count;
216 	char		*modif;
217 	void		(*pr) __P((const char *, ...));
218 {
219 	register_t fp, pc, rp, nargs, *argp;
220 	db_sym_t sym;
221 	db_expr_t off;
222 	char *name;
223 	char **argnp, *argnames[HPPA_FRAME_NARGS];
224 
225 	if (count < 0)
226 		count = 65536;
227 
228 	if (!have_addr) {
229 		fp = ddb_regs.tf_r3;
230 		pc = ddb_regs.tf_iioq_head;
231 		rp = ddb_regs.tf_rp;
232 	} else {
233 		fp = addr;
234 		pc = 0;
235 		rp = ((register_t *)fp)[-5];
236 	}
237 
238 #ifdef DDB_DEBUG
239 	/* db_printf (">> %x, %x, %x\t", fp, pc, rp); */
240 #endif
241 	while (fp && count--) {
242 
243 		if (USERMODE(pc))
244 			return;
245 
246 		sym = db_search_symbol(pc, DB_STGY_ANY, &off);
247 		db_symbol_values (sym, &name, NULL);
248 
249 		pr("%s(", name);
250 
251 		/* args */
252 		nargs = HPPA_FRAME_NARGS;
253 		argnp = NULL;
254 		if (db_sym_numargs(sym, &nargs, argnames))
255 			argnp = argnames;
256 		else
257 			nargs = 4;
258 		/*
259 		 * XXX first four args are passed on registers, and may not
260 		 * be stored on stack, dunno how to recover their values yet
261 		 */
262 		for (argp = &((register_t *)fp)[-9]; nargs--; argp--) {
263 			if (argnp)
264 				pr("%s=", *argnp++);
265 			pr("%lx%s", db_get_value((int)argp, 4, FALSE),
266 				  nargs? ",":"");
267 		}
268 		pr(") at ");
269 		db_printsym(pc, DB_STGY_PROC, pr);
270 		pr("\n");
271 
272 		/* TODO: print locals */
273 
274 		/* next frame */
275 		pc = rp;
276 		rp = ((register_t *)fp)[-5];
277 		fp = ((register_t *)fp)[0];
278 #ifdef DDB_DEBUG
279 		/* db_printf (">> %x, %x, %x\t", fp, pc, rp); */
280 #endif
281 	}
282 
283 	if (count && pc) {
284 		db_printsym(pc, DB_STGY_XTRN, pr);
285 		pr(":\n");
286 	}
287 }
288 
289