xref: /netbsd/sys/arch/hppa/hppa/db_interface.c (revision c4a72b64)
1 /*	$NetBSD: db_interface.c,v 1.1 2002/06/05 01:04:19 fredette 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 #define DDB_DEBUG
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 
40 #include <machine/db_machdep.h>
41 #include <machine/frame.h>
42 #include <machine/cpufunc.h>
43 
44 #include <ddb/db_access.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_output.h>
47 #include <ddb/db_run.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
50 #include <ddb/db_extern.h>
51 #include <ddb/db_interface.h>
52 
53 #include <dev/cons.h>
54 
55 void kdbprinttrap __P((int, int));
56 
57 extern label_t *db_recover;
58 extern int db_active;
59 extern char *trap_type[];
60 extern int trap_types;
61 
62 db_regs_t	ddb_regs;
63 const struct db_variable db_regs[] = {
64 	{ "flags", (long *)&ddb_regs.tf_flags,  FCN_NULL },
65 	{ "r1",    (long *)&ddb_regs.tf_r1,  FCN_NULL },
66 	{ "rp",    (long *)&ddb_regs.tf_rp,  FCN_NULL },
67 	{ "r3",    (long *)&ddb_regs.tf_r3,  FCN_NULL },
68 	{ "r4",    (long *)&ddb_regs.tf_r4,  FCN_NULL },
69 	{ "r5",    (long *)&ddb_regs.tf_r5,  FCN_NULL },
70 	{ "r6",    (long *)&ddb_regs.tf_r6,  FCN_NULL },
71 	{ "r7",    (long *)&ddb_regs.tf_r7,  FCN_NULL },
72 	{ "r8",    (long *)&ddb_regs.tf_r8,  FCN_NULL },
73 	{ "r9",    (long *)&ddb_regs.tf_r9,  FCN_NULL },
74 	{ "r10",   (long *)&ddb_regs.tf_r10, FCN_NULL },
75 	{ "r11",   (long *)&ddb_regs.tf_r11, FCN_NULL },
76 	{ "r12",   (long *)&ddb_regs.tf_r12, FCN_NULL },
77 	{ "r13",   (long *)&ddb_regs.tf_r13, FCN_NULL },
78 	{ "r14",   (long *)&ddb_regs.tf_r14, FCN_NULL },
79 	{ "r15",   (long *)&ddb_regs.tf_r15, FCN_NULL },
80 	{ "r16",   (long *)&ddb_regs.tf_r16, FCN_NULL },
81 	{ "r17",   (long *)&ddb_regs.tf_r17, FCN_NULL },
82 	{ "r18",   (long *)&ddb_regs.tf_r18, FCN_NULL },
83 	{ "t4",    (long *)&ddb_regs.tf_t4,  FCN_NULL },
84 	{ "t3",    (long *)&ddb_regs.tf_t3,  FCN_NULL },
85 	{ "t2",    (long *)&ddb_regs.tf_t2,  FCN_NULL },
86 	{ "t1",    (long *)&ddb_regs.tf_t1,  FCN_NULL },
87 	{ "arg3",  (long *)&ddb_regs.tf_arg3,  FCN_NULL },
88 	{ "arg2",  (long *)&ddb_regs.tf_arg2,  FCN_NULL },
89 	{ "arg1",  (long *)&ddb_regs.tf_arg1,  FCN_NULL },
90 	{ "arg0",  (long *)&ddb_regs.tf_arg0,  FCN_NULL },
91 	{ "dp",    (long *)&ddb_regs.tf_dp,    FCN_NULL },
92 	{ "ret0",  (long *)&ddb_regs.tf_ret0,  FCN_NULL },
93 	{ "ret1",  (long *)&ddb_regs.tf_ret1,  FCN_NULL },
94 	{ "sp",    (long *)&ddb_regs.tf_sp,    FCN_NULL },
95 	{ "r31",   (long *)&ddb_regs.tf_r31,   FCN_NULL },
96 	{ "sar",   (long *)&ddb_regs.tf_sar,   FCN_NULL },
97 
98 	{ "eirr",  (long *)&ddb_regs.tf_eirr,  FCN_NULL },
99 	{ "eiem",  (long *)&ddb_regs.tf_eiem,  FCN_NULL },
100 	{ "iir",   (long *)&ddb_regs.tf_iir,   FCN_NULL },
101 	{ "isr",   (long *)&ddb_regs.tf_isr,   FCN_NULL },
102 	{ "ior",   (long *)&ddb_regs.tf_ior,   FCN_NULL },
103 	{ "ipsw",  (long *)&ddb_regs.tf_ipsw,  FCN_NULL },
104 	{ "iisqh", (long *)&ddb_regs.tf_iisq_head,  FCN_NULL },
105 	{ "iioqh", (long *)&ddb_regs.tf_iioq_head,  FCN_NULL },
106 	{ "iisqt", (long *)&ddb_regs.tf_iisq_tail,  FCN_NULL },
107 	{ "iioqt", (long *)&ddb_regs.tf_iioq_tail,  FCN_NULL },
108 
109 	{ "sr0",   (long *)&ddb_regs.tf_sr0,   FCN_NULL },
110 	{ "sr1",   (long *)&ddb_regs.tf_sr1,   FCN_NULL },
111 	{ "sr2",   (long *)&ddb_regs.tf_sr2,   FCN_NULL },
112 	{ "sr3",   (long *)&ddb_regs.tf_sr3,   FCN_NULL },
113 	{ "sr4",   (long *)&ddb_regs.tf_sr4,   FCN_NULL },
114 	{ "sr5",   (long *)&ddb_regs.tf_sr5,   FCN_NULL },
115 	{ "sr6",   (long *)&ddb_regs.tf_sr6,   FCN_NULL },
116 	{ "sr7",   (long *)&ddb_regs.tf_sr7,   FCN_NULL },
117 
118 	{ "pidr1", (long *)&ddb_regs.tf_pidr1, FCN_NULL },
119 	{ "pidr2", (long *)&ddb_regs.tf_pidr2, FCN_NULL },
120 #ifdef pbably_not_worth_it
121 	{ "pidr3", (long *)&ddb_regs.tf_pidr3, FCN_NULL },
122 	{ "pidr4", (long *)&ddb_regs.tf_pidr4, FCN_NULL },
123 #endif
124 
125 	{ "hptm",  (long *)&ddb_regs.tf_hptm,  FCN_NULL },
126 	{ "vtop",  (long *)&ddb_regs.tf_vtop,  FCN_NULL },
127 	{ "cr28",  (long *)&ddb_regs.tf_cr28,  FCN_NULL },
128 	{ "cr30",  (long *)&ddb_regs.tf_cr30,  FCN_NULL },
129 };
130 const struct db_variable * const db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
131 int db_active = 0;
132 
133 void
134 Debugger()
135 {
136 	__asm __volatile ("break	%0, %1"
137 			  :: "i" (HPPA_BREAK_KERNEL), "i" (HPPA_BREAK_KGDB));
138 }
139 
140 /*
141  * Print trap reason.
142  */
143 void
144 kdbprinttrap(type, code)
145 	int type, code;
146 {
147 	db_printf("kernel: ");
148 	if (type >= trap_types || type < 0)
149 		db_printf("type %d", type);
150 	else
151 		db_printf("%s", trap_type[type]);
152 	db_printf(" trap, code=%x\n", code);
153 }
154 
155 /*
156  *  kdb_trap - field a BPT trap
157  */
158 int
159 kdb_trap(type, code, regs)
160 	int type, code;
161 	db_regs_t *regs;
162 {
163 	int s;
164 
165 	switch (type) {
166 	case T_RECOVERY:
167 	case T_IBREAK:
168 	case T_DBREAK:
169 	case -1:
170 		break;
171 	default:
172 		kdbprinttrap(type, code);
173 		if (db_recover != 0) {
174 			db_error("Caught exception in DDB; continuing...\n");
175 			/* NOT REACHED */
176 		}
177 		return (0);
178 	}
179 
180 	/* XXX Should switch to kdb`s own stack here. */
181 
182 	ddb_regs = *regs;
183 
184 	s = splhigh();
185 	db_active++;
186 	cnpollc(TRUE);
187 	db_trap(type, code);
188 	cnpollc(FALSE);
189 	db_active--;
190 	splx(s);
191 
192 	*regs = ddb_regs;
193 
194 	return (1);
195 }
196 
197 /*
198  *  Validate an address for use as a breakpoint.
199  *  Any address is allowed for now.
200  */
201 int
202 db_valid_breakpoint(addr)
203 	db_addr_t addr;
204 {
205 	return (1);
206 }
207 
208 void
209 db_stack_trace_print(addr, have_addr, count, modif, pr)
210 	db_expr_t	addr;
211 	int		have_addr;
212 	db_expr_t	count;
213 	char		*modif;
214 	void		(*pr) __P((const char *, ...));
215 {
216 	register_t fp, pc, rp, nargs, *argp;
217 	db_sym_t sym;
218 	db_expr_t off;
219 	char *name;
220 	char **argnp, *argnames[HPPA_FRAME_NARGS];
221 
222 	if (count < 0)
223 		count = 65536;
224 
225 	if (!have_addr) {
226 		fp = ddb_regs.tf_r3;
227 		pc = ddb_regs.tf_iioq_head;
228 		rp = ddb_regs.tf_rp;
229 	} else {
230 		fp = addr;
231 		pc = 0;
232 		rp = ((register_t *)fp)[-5];
233 	}
234 
235 #ifdef DDB_DEBUG
236 	/* db_printf (">> %x, %x, %x\t", fp, pc, rp); */
237 #endif
238 	while (fp && count--) {
239 
240 		if (USERMODE(pc))
241 			return;
242 
243 		sym = db_search_symbol(pc, DB_STGY_ANY, &off);
244 		db_symbol_values (sym, &name, NULL);
245 
246 		pr("%s(", name);
247 
248 		/* args */
249 		nargs = HPPA_FRAME_NARGS;
250 		argnp = NULL;
251 		if (db_sym_numargs(sym, &nargs, argnames))
252 			argnp = argnames;
253 		else
254 			nargs = 4;
255 		/*
256 		 * XXX first four args are passed on registers, and may not
257 		 * be stored on stack, dunno how to recover their values yet
258 		 */
259 		for (argp = &((register_t *)fp)[-9]; nargs--; argp--) {
260 			if (argnp)
261 				pr("%s=", *argnp++);
262 			pr("%lx%s", db_get_value((int)argp, 4, FALSE),
263 				  nargs? ",":"");
264 		}
265 		pr(") at ");
266 		db_printsym(pc, DB_STGY_PROC, pr);
267 		pr("\n");
268 
269 		/* TODO: print locals */
270 
271 		/* next frame */
272 		pc = rp;
273 		rp = ((register_t *)fp)[-5];
274 		fp = ((register_t *)fp)[0];
275 #ifdef DDB_DEBUG
276 		/* db_printf (">> %x, %x, %x\t", fp, pc, rp); */
277 #endif
278 	}
279 
280 	if (count && pc) {
281 		db_printsym(pc, DB_STGY_XTRN, pr);
282 		pr(":\n");
283 	}
284 }
285 
286