xref: /openbsd/sys/arch/hppa/hppa/db_interface.c (revision fc61954a)
1 /*	$OpenBSD: db_interface.c,v 1.38 2016/09/19 21:18:35 jasper Exp $	*/
2 
3 /*
4  * Copyright (c) 1999-2003 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 WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #undef DDB_DEBUG
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 
34 #include <machine/db_machdep.h>
35 #include <machine/frame.h>
36 #include <machine/cpufunc.h>
37 
38 #include <ddb/db_access.h>
39 #include <ddb/db_command.h>
40 #include <ddb/db_output.h>
41 #include <ddb/db_run.h>
42 #include <ddb/db_sym.h>
43 #include <ddb/db_var.h>
44 #include <ddb/db_variables.h>
45 #include <ddb/db_extern.h>
46 #include <ddb/db_interface.h>
47 
48 #include <dev/cons.h>
49 
50 void kdbprinttrap(int, int);
51 
52 extern char *trap_type[];
53 extern int trap_types;
54 
55 db_regs_t	ddb_regs;
56 struct db_variable db_regs[] = {
57 	{ "flags", (long *)&ddb_regs.tf_flags,  FCN_NULL },
58 	{ "r1",    (long *)&ddb_regs.tf_r1,  FCN_NULL },
59 	{ "rp",    (long *)&ddb_regs.tf_rp,  FCN_NULL },
60 	{ "r3",    (long *)&ddb_regs.tf_r3,  FCN_NULL },
61 	{ "r4",    (long *)&ddb_regs.tf_r4,  FCN_NULL },
62 	{ "r5",    (long *)&ddb_regs.tf_r5,  FCN_NULL },
63 	{ "r6",    (long *)&ddb_regs.tf_r6,  FCN_NULL },
64 	{ "r7",    (long *)&ddb_regs.tf_r7,  FCN_NULL },
65 	{ "r8",    (long *)&ddb_regs.tf_r8,  FCN_NULL },
66 	{ "r9",    (long *)&ddb_regs.tf_r9,  FCN_NULL },
67 	{ "r10",   (long *)&ddb_regs.tf_r10, FCN_NULL },
68 	{ "r11",   (long *)&ddb_regs.tf_r11, FCN_NULL },
69 	{ "r12",   (long *)&ddb_regs.tf_r12, FCN_NULL },
70 	{ "r13",   (long *)&ddb_regs.tf_r13, FCN_NULL },
71 	{ "r14",   (long *)&ddb_regs.tf_r14, FCN_NULL },
72 	{ "r15",   (long *)&ddb_regs.tf_r15, FCN_NULL },
73 	{ "r16",   (long *)&ddb_regs.tf_r16, FCN_NULL },
74 	{ "r17",   (long *)&ddb_regs.tf_r17, FCN_NULL },
75 	{ "r18",   (long *)&ddb_regs.tf_r18, FCN_NULL },
76 	{ "r19",   (long *)&ddb_regs.tf_t4,  FCN_NULL },
77 	{ "r20",   (long *)&ddb_regs.tf_t3,  FCN_NULL },
78 	{ "r21",   (long *)&ddb_regs.tf_t2,  FCN_NULL },
79 	{ "r22",   (long *)&ddb_regs.tf_t1,  FCN_NULL },
80 	{ "r23",   (long *)&ddb_regs.tf_arg3,  FCN_NULL },
81 	{ "r24",   (long *)&ddb_regs.tf_arg2,  FCN_NULL },
82 	{ "r25",   (long *)&ddb_regs.tf_arg1,  FCN_NULL },
83 	{ "r26",   (long *)&ddb_regs.tf_arg0,  FCN_NULL },
84 	{ "r27",   (long *)&ddb_regs.tf_dp,    FCN_NULL },
85 	{ "r28",   (long *)&ddb_regs.tf_ret0,  FCN_NULL },
86 	{ "r29",   (long *)&ddb_regs.tf_ret1,  FCN_NULL },
87 	{ "r30",   (long *)&ddb_regs.tf_sp,    FCN_NULL },
88 	{ "r31",   (long *)&ddb_regs.tf_r31,   FCN_NULL },
89 	{ "sar",   (long *)&ddb_regs.tf_sar,   FCN_NULL },
90 
91 	{ "rctr",  (long *)&ddb_regs.tf_rctr,  FCN_NULL },
92 	{ "ccr",   (long *)&ddb_regs.tf_ccr,   FCN_NULL },
93 	{ "eirr",  (long *)&ddb_regs.tf_eirr,  FCN_NULL },
94 	{ "eiem",  (long *)&ddb_regs.tf_eiem,  FCN_NULL },
95 	{ "iir",   (long *)&ddb_regs.tf_iir,   FCN_NULL },
96 	{ "isr",   (long *)&ddb_regs.tf_isr,   FCN_NULL },
97 	{ "ior",   (long *)&ddb_regs.tf_ior,   FCN_NULL },
98 	{ "ipsw",  (long *)&ddb_regs.tf_ipsw,  FCN_NULL },
99 	{ "iisqh", (long *)&ddb_regs.tf_iisq_head,  FCN_NULL },
100 	{ "iioqh", (long *)&ddb_regs.tf_iioq_head,  FCN_NULL },
101 	{ "iisqt", (long *)&ddb_regs.tf_iisq_tail,  FCN_NULL },
102 	{ "iioqt", (long *)&ddb_regs.tf_iioq_tail,  FCN_NULL },
103 
104 	{ "sr0",   (long *)&ddb_regs.tf_sr0,   FCN_NULL },
105 	{ "sr1",   (long *)&ddb_regs.tf_sr1,   FCN_NULL },
106 	{ "sr2",   (long *)&ddb_regs.tf_sr2,   FCN_NULL },
107 	{ "sr3",   (long *)&ddb_regs.tf_sr3,   FCN_NULL },
108 	{ "sr4",   (long *)&ddb_regs.tf_sr4,   FCN_NULL },
109 	{ "sr5",   (long *)&ddb_regs.tf_sr5,   FCN_NULL },
110 	{ "sr6",   (long *)&ddb_regs.tf_sr6,   FCN_NULL },
111 	{ "sr7",   (long *)&ddb_regs.tf_sr7,   FCN_NULL },
112 
113 	{ "pidr1", (long *)&ddb_regs.tf_pidr1, FCN_NULL },
114 	{ "pidr2", (long *)&ddb_regs.tf_pidr2, FCN_NULL },
115 #ifdef pbably_not_worth_it
116 	{ "pidr3", (long *)&ddb_regs.tf_pidr3, FCN_NULL },
117 	{ "pidr4", (long *)&ddb_regs.tf_pidr4, FCN_NULL },
118 #endif
119 
120 	{ "vtop",  (long *)&ddb_regs.tf_vtop,  FCN_NULL },
121 	{ "cr28",  (long *)&ddb_regs.tf_cr28,  FCN_NULL },
122 	{ "cr30",  (long *)&ddb_regs.tf_cr30,  FCN_NULL },
123 };
124 struct db_variable *db_eregs = db_regs + nitems(db_regs);
125 int db_active = 0;
126 
127 void
128 Debugger()
129 {
130 	extern int kernelmapped;	/* from locore.S */
131 	if (kernelmapped)
132 		__asm volatile ("break %0, %1"
133 		    :: "i" (HPPA_BREAK_KERNEL), "i" (HPPA_BREAK_KGDB));
134 }
135 
136 void
137 db_read_bytes(vaddr_t addr, size_t size, char *data)
138 {
139 	register char *src = (char *)addr;
140 
141 	while (size--)
142 		*data++ = *src++;
143 }
144 
145 void
146 db_write_bytes(vaddr_t addr, size_t size, char *data)
147 {
148 	register char *dst = (char *)addr;
149 
150 	while (size--)
151 		*dst++ = *data++;
152 
153 	/* unfortunately ddb does not provide any hooks for these */
154 	ficache(HPPA_SID_KERNEL, (vaddr_t)data, size);
155 	fdcache(HPPA_SID_KERNEL, (vaddr_t)data, size);
156 }
157 
158 
159 /*
160  * Print trap reason.
161  */
162 void
163 kdbprinttrap(int type, int code)
164 {
165 	type &= ~T_USER;	/* just in case */
166 	db_printf("kernel: ");
167 	if (type >= trap_types || type < 0)
168 		db_printf("type 0x%x", type);
169 	else
170 		db_printf("%s", trap_type[type]);
171 	db_printf(" trap, code=0x%x\n", code);
172 }
173 
174 /*
175  *  db_ktrap - field a BPT trap
176  */
177 int
178 db_ktrap(int type, int code, db_regs_t *regs)
179 {
180 	extern label_t *db_recover;
181 	int s;
182 
183 	switch (type) {
184 	case T_IBREAK:
185 	case T_DBREAK:
186 	case -1:
187 		break;
188 	default:
189 		if (!db_panic)
190 			return (0);
191 
192 		kdbprinttrap(type, code);
193 		if (db_recover != 0) {
194 			db_error("Caught exception in DDB; continuing...\n");
195 			/* NOT REACHED */
196 		}
197 	}
198 
199 	/* XXX Should switch to kdb`s own stack here. */
200 
201 	s = splhigh();
202 	ddb_regs = *regs;
203 	db_active++;
204 	cnpollc(TRUE);
205 	db_trap(type, code);
206 	cnpollc(FALSE);
207 	db_active--;
208 	splx(s);
209 
210 	*regs = ddb_regs;
211 
212 	return (1);
213 }
214 
215 /*
216  *  Validate an address for use as a breakpoint.
217  *  Any address is allowed for now.
218  */
219 int
220 db_valid_breakpoint(db_addr_t addr)
221 {
222 	return (1);
223 }
224 
225 void
226 db_stack_trace_print(db_expr_t addr, int have_addr, db_expr_t count,
227     char *modif, int (*pr)(const char *, ...))
228 {
229 	register_t *fp, pc, rp, *argp;
230 	db_sym_t sym;
231 	db_expr_t off;
232 	char *name;
233 	int nargs;
234 
235 	if (count < 0)
236 		count = 65536;
237 
238 	if (!have_addr) {
239 		fp = (register_t *)ddb_regs.tf_r3;
240 		pc = ddb_regs.tf_iioq_head;
241 		rp = ddb_regs.tf_rp;
242 	} else {
243 		fp = (register_t *)addr;
244 		pc = 0;
245 		rp = ((register_t *)fp)[-5];
246 	}
247 
248 #ifdef DDB_DEBUG
249 	(*pr) (">> %p, 0x%x, 0x%x\t", fp, pc, rp);
250 #endif
251 	while (fp && count--) {
252 
253 		if (USERMODE(pc))
254 			return;
255 
256 		sym = db_search_symbol(pc, DB_STGY_ANY, &off);
257 		db_symbol_values (sym, &name, NULL);
258 
259 		(*pr)("%s(", name);
260 
261 		/* args */
262 		nargs = 4;
263 		/*
264 		 * XXX first four args are passed on registers, and may not
265 		 * be stored on stack, dunno how to recover their values yet
266 		 */
267 		for (argp = &fp[-9]; nargs--; argp--) {
268 			(*pr)("%x%s", db_get_value((int)argp, 4, FALSE),
269 				  nargs? ",":"");
270 		}
271 		(*pr)(") at ");
272 		db_printsym(pc, DB_STGY_PROC, pr);
273 		(*pr)("\n");
274 
275 		/* TODO: print locals */
276 
277 		/* next frame */
278 		pc = rp;
279 		rp = fp[-5];
280 
281 		/* if a terminal frame and not a start of a page
282 		 * then skip the trapframe and the terminal frame */
283 		if (!fp[0]) {
284 			struct trapframe *tf;
285 
286 			tf = (struct trapframe *)((char *)fp - sizeof(*tf));
287 
288 			if (tf->tf_flags & TFF_SYS)
289 				(*pr)("-- syscall #%d(%x, %x, %x, %x, ...)\n",
290 				    tf->tf_t1, tf->tf_arg0, tf->tf_arg1,
291 				    tf->tf_arg2, tf->tf_arg3);
292 			else
293 				(*pr)("-- trap #%d%s\n", tf->tf_flags & 0x3f,
294 				    (tf->tf_flags & T_USER)? " from user" : "");
295 
296 			if (!(tf->tf_flags & TFF_LAST)) {
297 				fp = (register_t *)tf->tf_r3;
298 				pc = tf->tf_iioq_head;
299 				rp = tf->tf_rp;
300 			} else
301 				fp = 0;
302 		} else
303 			fp = (register_t *)fp[0];
304 #ifdef DDB_DEBUG
305 		(*pr) (">> %x, %x, %x\t", fp, pc, rp);
306 #endif
307 	}
308 
309 	if (count && pc) {
310 		db_printsym(pc, DB_STGY_XTRN, pr);
311 		(*pr)(":\n");
312 	}
313 }
314