kgdb_stub.c revision 1.7.2.5 1 /* $NetBSD: kgdb_stub.c,v 1.7.2.5 2002/01/08 00:32:36 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratories.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)kgdb_stub.c 8.4 (Berkeley) 1/12/94
45 */
46
47 /*
48 * "Stub" to allow remote cpu to debug over a serial line using gdb.
49 */
50
51 #include <sys/cdefs.h>
52 __KERNEL_RCSID(0, "$NetBSD: kgdb_stub.c,v 1.7.2.5 2002/01/08 00:32:36 nathanw Exp $");
53
54 #include "opt_kgdb.h"
55
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/kgdb.h>
59
60 #undef DEBUG_KGDB
61
62 #ifdef DEBUG_KGDB
63 #define DPRINTF(x) printf x
64 #else
65 #define DPRINTF(x)
66 #endif
67
68 /* XXX: Maybe these should be in the MD files? */
69 #ifndef KGDB_DEV
70 #define KGDB_DEV NODEV
71 #endif
72 #ifndef KGDB_DEVRATE
73 #define KGDB_DEVRATE 19200
74 #endif
75
76 int kgdb_dev = KGDB_DEV; /* remote debugging device (NODEV if none) */
77 int kgdb_rate = KGDB_DEVRATE; /* remote debugging baud rate */
78 int kgdb_active = 0; /* remote debugging active if != 0 */
79 int kgdb_debug_init = 0; /* != 0 waits for remote at system init */
80 int kgdb_debug_panic = 0; /* != 0 waits for remote on panic */
81 label_t *kgdb_recover = 0;
82
83 static void kgdb_copy __P((void *, void *, int));
84 /* static void kgdb_zero __P((void *, int)); */
85 static void kgdb_send __P((u_char *));
86 static int kgdb_recv __P((u_char *, int));
87 static int digit2i __P((u_char));
88 static u_char i2digit __P((int));
89 static void mem2hex __P((void *, void *, int));
90 static u_char *hex2mem __P((void *, u_char *, int));
91 static vaddr_t hex2i __P((u_char **));
92
93 static int (*kgdb_getc) __P((void *));
94 static void (*kgdb_putc) __P((void *, int));
95 static void *kgdb_ioarg;
96
97 static u_char buffer[KGDB_BUFLEN];
98 static kgdb_reg_t gdb_regs[KGDB_NUMREGS];
99
100 #define GETC() ((*kgdb_getc)(kgdb_ioarg))
101 #define PUTC(c) ((*kgdb_putc)(kgdb_ioarg, c))
102
103 /*
104 * db_trap_callback can be hooked by MD port code to handle special
105 * cases such as disabling hardware watchdogs while in kgdb. Name
106 * is shared with DDB.
107 */
108 void (*db_trap_callback)(int);
109
110 /*
111 * This little routine exists simply so that bcopy() can be debugged.
112 */
113 static void
114 kgdb_copy(vsrc, vdst, len)
115 void *vsrc, *vdst;
116 int len;
117 {
118 char *src = vsrc;
119 char *dst = vdst;
120
121 while (--len >= 0)
122 *dst++ = *src++;
123 }
124
125 #if 0
126 /* ditto for bzero */
127 static void
128 kgdb_zero(vptr, len)
129 void *vptr;
130 int len;
131 {
132 char *ptr = vptr;
133
134 while (--len >= 0)
135 *ptr++ = (char) 0;
136 }
137 #endif
138
139 /*
140 * Convert a hex digit into an integer.
141 * This returns -1 if the argument passed is no
142 * valid hex digit.
143 */
144 static int
145 digit2i(c)
146 u_char c;
147 {
148 if (c >= '0' && c <= '9')
149 return (c - '0');
150 else if (c >= 'a' && c <= 'f')
151 return (c - 'a' + 10);
152 else if (c >= 'A' && c <= 'F')
153
154 return (c - 'A' + 10);
155 else
156 return (-1);
157 }
158
159 /*
160 * Convert the low 4 bits of an integer into
161 * an hex digit.
162 */
163 static u_char
164 i2digit(n)
165 int n;
166 {
167 return ("0123456789abcdef"[n & 0x0f]);
168 }
169
170 /*
171 * Convert a byte array into an hex string.
172 */
173 static void
174 mem2hex(vdst, vsrc, len)
175 void *vdst, *vsrc;
176 int len;
177 {
178 u_char *dst = vdst;
179 u_char *src = vsrc;
180
181 while (len--) {
182 *dst++ = i2digit(*src >> 4);
183 *dst++ = i2digit(*src++);
184 }
185 *dst = '\0';
186 }
187
188 /*
189 * Convert an hex string into a byte array.
190 * This returns a pointer to the character following
191 * the last valid hex digit. If the string ends in
192 * the middle of a byte, NULL is returned.
193 */
194 static u_char *
195 hex2mem(vdst, src, maxlen)
196 void *vdst;
197 u_char *src;
198 int maxlen;
199 {
200 u_char *dst = vdst;
201 int msb, lsb;
202
203 while (*src && maxlen--) {
204 msb = digit2i(*src++);
205 if (msb < 0)
206 return (src - 1);
207 lsb = digit2i(*src++);
208 if (lsb < 0)
209 return (NULL);
210 *dst++ = (msb << 4) | lsb;
211 }
212 return (src);
213 }
214
215 /*
216 * Convert an hex string into an integer.
217 * This returns a pointer to the character following
218 * the last valid hex digit.
219 */
220 static vaddr_t
221 hex2i(srcp)
222 u_char **srcp;
223 {
224 char *src = *srcp;
225 vaddr_t r = 0;
226 int nibble;
227
228 while ((nibble = digit2i(*src)) >= 0) {
229 r *= 16;
230 r += nibble;
231 src++;
232 }
233 *srcp = src;
234 return (r);
235 }
236
237 /*
238 * Send a packet.
239 */
240 static void
241 kgdb_send(bp)
242 u_char *bp;
243 {
244 u_char *p;
245 u_char csum, c;
246
247 DPRINTF(("kgdb_send: %s\n", bp));
248 do {
249 p = bp;
250 PUTC(KGDB_START);
251 for (csum = 0; (c = *p); p++) {
252 PUTC(c);
253 csum += c;
254 }
255 PUTC(KGDB_END);
256 PUTC(i2digit(csum >> 4));
257 PUTC(i2digit(csum));
258 } while ((c = GETC() & 0x7f) == KGDB_BADP);
259 }
260
261 /*
262 * Receive a packet.
263 */
264 static int
265 kgdb_recv(bp, maxlen)
266 u_char *bp;
267 int maxlen;
268 {
269 u_char *p;
270 int c, csum, tmpcsum;
271 int len;
272
273 DPRINTF(("kgdb_recv: "));
274 do {
275 p = bp;
276 csum = len = 0;
277 while ((c = GETC()) != KGDB_START)
278 DPRINTF(("%c",c));
279 DPRINTF(("%c Start ",c));
280
281 while ((c = GETC()) != KGDB_END && len < maxlen) {
282 DPRINTF(("%c",c));
283 c &= 0x7f;
284 csum += c;
285 *p++ = c;
286 len++;
287 }
288 csum &= 0xff;
289 *p = '\0';
290 DPRINTF(("%c End ", c));
291
292 if (len >= maxlen) {
293 DPRINTF(("Long- "));
294 PUTC(KGDB_BADP);
295 continue;
296 }
297 tmpcsum = csum;
298
299 c = GETC();
300 DPRINTF(("%c",c));
301 csum -= digit2i(c) * 16;
302 c = GETC();
303 DPRINTF(("%c",c));
304 csum -= digit2i(c);
305
306 if (csum == 0) {
307 DPRINTF(("Good+ "));
308 PUTC(KGDB_GOODP);
309 /* Sequence present? */
310 if (bp[2] == ':') {
311 DPRINTF(("Seq %c%c ", bp[0], bp[1]));
312 PUTC(bp[0]);
313 PUTC(bp[1]);
314 len -= 3;
315 kgdb_copy(bp + 3, bp, len);
316 }
317 break;
318 }
319 DPRINTF((" Bad(wanted %x, off by %d)- ", tmpcsum, csum));
320 PUTC(KGDB_BADP);
321 } while (1);
322 DPRINTF(("kgdb_recv: %s\n", bp));
323 return (len);
324 }
325
326 /*
327 * This is called by the appropriate tty driver.
328 */
329 void
330 kgdb_attach(getfn, putfn, ioarg)
331 int (*getfn) __P((void *));
332 void (*putfn) __P((void *, int));
333 void *ioarg;
334 {
335 kgdb_getc = getfn;
336 kgdb_putc = putfn;
337 kgdb_ioarg = ioarg;
338 }
339
340 /*
341 * This function does all command processing for interfacing to
342 * a remote gdb. Note that the error codes are ignored by gdb
343 * at present, but might eventually become meaningful. (XXX)
344 * It might makes sense to use POSIX errno values, because
345 * that is what the gdb/remote.c functions want to return.
346 */
347 int
348 kgdb_trap(type, regs)
349 int type;
350 db_regs_t *regs;
351 {
352 label_t jmpbuf;
353 vaddr_t addr;
354 size_t len;
355 u_char *p;
356
357 printf("kgdb_trap 1\n");
358
359 if (kgdb_dev < 0 || kgdb_getc == NULL) {
360 /* not debugging */
361 return (0);
362 }
363
364 db_clear_single_step(regs);
365
366 if (db_trap_callback) db_trap_callback(1);
367
368 /* Detect and recover from unexpected traps. */
369 if (kgdb_recover != 0) {
370 printf("kgdb: caught trap 0x%x at %p\n",
371 type, (void*)PC_REGS(regs));
372 kgdb_send("E0E"); /* 14==EFAULT */
373 longjmp(kgdb_recover);
374 }
375
376 /*
377 * The first entry to this function is normally through
378 * a breakpoint trap in kgdb_connect(), in which case we
379 * must advance past the breakpoint because gdb will not.
380 *
381 * Machines vary as to where they leave the PC after a
382 * breakpoint trap. Those that leave the PC set to the
383 * address of the trap instruction (i.e. pc532) will not
384 * define FIXUP_PC_AFTER_BREAK(), and therefore will just
385 * advance the PC. On machines that leave the PC set to
386 * the instruction after the trap, FIXUP_PC_AFTER_BREAK
387 * will be defined to back-up the PC, so that after the
388 * "first-time" part of the if statement below has run,
389 * the PC will be the same as it was on entry.
390 *
391 * On the first entry here, we expect that gdb is not yet
392 * listening to us, so just enter the interaction loop.
393 * After the debugger is "active" (connected) it will be
394 * waiting for a "signaled" message from us.
395 */
396 if (kgdb_active == 0) {
397 if (!IS_BREAKPOINT_TRAP(type, 0)) {
398 /* No debugger active -- let trap handle this. */
399 if (db_trap_callback) db_trap_callback(0);
400 return (0);
401 }
402 /* Make the PC point at the breakpoint... */
403 #ifdef FIXUP_PC_AFTER_BREAK
404 FIXUP_PC_AFTER_BREAK(regs);
405 #endif
406 /* ... and then advance past it. */
407 #ifdef PC_ADVANCE
408 PC_ADVANCE(regs);
409 #else
410 PC_REGS(regs) += BKPT_SIZE;
411 #endif
412 kgdb_active = 1;
413 } else {
414 /* Tell remote host that an exception has occurred. */
415 sprintf(buffer, "S%02x", kgdb_signal(type));
416 kgdb_send(buffer);
417 }
418
419 /* Stick frame regs into our reg cache. */
420 kgdb_getregs(regs, gdb_regs);
421
422 /*
423 * Interact with gdb until it lets us go.
424 * If we cause a trap, resume here.
425 */
426 (void)setjmp((kgdb_recover = &jmpbuf));
427 for (;;) {
428 kgdb_recv(buffer, sizeof(buffer));
429 switch (buffer[0]) {
430
431 default:
432 /* Unknown command. */
433 kgdb_send("");
434 continue;
435
436 case KGDB_SIGNAL:
437 /*
438 * if this command came from a running gdb,
439 * answer it -- the other guy has no way of
440 * knowing if we're in or out of this loop
441 * when he issues a "remote-signal".
442 */
443 sprintf(buffer, "S%02x", kgdb_signal(type));
444 kgdb_send(buffer);
445 continue;
446
447 case KGDB_REG_R:
448 mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
449 kgdb_send(buffer);
450 continue;
451
452 case KGDB_REG_W:
453 p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
454 if (p == NULL || *p != '\0')
455 kgdb_send("E01");
456 else {
457 kgdb_setregs(regs, gdb_regs);
458 kgdb_send("OK");
459 }
460 continue;
461
462 case KGDB_MEM_R:
463 p = buffer + 1;
464 addr = hex2i(&p);
465 if (*p++ != ',') {
466 kgdb_send("E02");
467 continue;
468 }
469 len = hex2i(&p);
470 if (*p != '\0') {
471 kgdb_send("E03");
472 continue;
473 }
474 if (len > sizeof(buffer) / 2) {
475 kgdb_send("E04");
476 continue;
477 }
478 if (kgdb_acc(addr, len) == 0) {
479 kgdb_send("E05");
480 continue;
481 }
482 db_read_bytes(addr, (size_t)len,
483 (char *)buffer + sizeof(buffer) / 2);
484 mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
485 kgdb_send(buffer);
486 continue;
487
488 case KGDB_MEM_W:
489 p = buffer + 1;
490 addr = hex2i(&p);
491 if (*p++ != ',') {
492 kgdb_send("E06");
493 continue;
494 }
495 len = hex2i(&p);
496 if (*p++ != ':') {
497 kgdb_send("E07");
498 continue;
499 }
500 if (len > (sizeof(buffer) - (p - buffer))) {
501 kgdb_send("E08");
502 continue;
503 }
504 p = hex2mem(buffer, p, sizeof(buffer));
505 if (p == NULL) {
506 kgdb_send("E09");
507 continue;
508 }
509 if (kgdb_acc(addr, len) == 0) {
510 kgdb_send("E0A");
511 continue;
512 }
513 db_write_bytes(addr, (size_t)len, (char *)buffer);
514 kgdb_send("OK");
515 continue;
516
517 case KGDB_KILL:
518 kgdb_active = 0;
519 printf("kgdb detached\n");
520 db_clear_single_step(regs);
521 goto out;
522
523 case KGDB_CONT:
524 if (buffer[1]) {
525 p = buffer + 1;
526 addr = hex2i(&p);
527 if (*p) {
528 kgdb_send("E0B");
529 continue;
530 }
531 PC_REGS(regs) = addr;
532 DPRINTF(("kgdb: continuing at %08lx\n", addr))
533
534 } else {
535 DPRINTF((
536 "kgdb: continuing at old address %08lx\n",
537 PC_REGS(regs)));
538 }
539
540 db_clear_single_step(regs);
541 goto out;
542
543 case KGDB_STEP:
544 if (buffer[1]) {
545 p = buffer + 1;
546 addr = hex2i(&p);
547 if (*p) {
548 kgdb_send("E0B");
549 continue;
550 }
551 PC_REGS(regs) = addr;
552 }
553 db_set_single_step(regs);
554 goto out;
555 }
556 }
557 out:
558 if (db_trap_callback) db_trap_callback(0);
559 kgdb_recover = 0;
560 return (1);
561 }
562