kgdb_stub.c revision 1.7.2.6 1 /* $NetBSD: kgdb_stub.c,v 1.7.2.6 2002/01/11 23:39:39 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.6 2002/01/11 23:39:39 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 /* KGDB_BUFLEN must be at least (2*KGDB_NUMREGS*sizeof(kgdb_reg_t)+1) */
98 static u_char buffer[KGDB_BUFLEN];
99 static kgdb_reg_t gdb_regs[KGDB_NUMREGS];
100
101 #define GETC() ((*kgdb_getc)(kgdb_ioarg))
102 #define PUTC(c) ((*kgdb_putc)(kgdb_ioarg, c))
103
104 /*
105 * db_trap_callback can be hooked by MD port code to handle special
106 * cases such as disabling hardware watchdogs while in kgdb. Name
107 * is shared with DDB.
108 */
109 void (*db_trap_callback)(int);
110
111 /*
112 * This little routine exists simply so that bcopy() can be debugged.
113 */
114 static void
115 kgdb_copy(vsrc, vdst, len)
116 void *vsrc, *vdst;
117 int len;
118 {
119 char *src = vsrc;
120 char *dst = vdst;
121
122 while (--len >= 0)
123 *dst++ = *src++;
124 }
125
126 #if 0
127 /* ditto for bzero */
128 static void
129 kgdb_zero(vptr, len)
130 void *vptr;
131 int len;
132 {
133 char *ptr = vptr;
134
135 while (--len >= 0)
136 *ptr++ = (char) 0;
137 }
138 #endif
139
140 /*
141 * Convert a hex digit into an integer.
142 * This returns -1 if the argument passed is no
143 * valid hex digit.
144 */
145 static int
146 digit2i(c)
147 u_char c;
148 {
149 if (c >= '0' && c <= '9')
150 return (c - '0');
151 else if (c >= 'a' && c <= 'f')
152 return (c - 'a' + 10);
153 else if (c >= 'A' && c <= 'F')
154
155 return (c - 'A' + 10);
156 else
157 return (-1);
158 }
159
160 /*
161 * Convert the low 4 bits of an integer into
162 * an hex digit.
163 */
164 static u_char
165 i2digit(n)
166 int n;
167 {
168 return ("0123456789abcdef"[n & 0x0f]);
169 }
170
171 /*
172 * Convert a byte array into an hex string.
173 */
174 static void
175 mem2hex(vdst, vsrc, len)
176 void *vdst, *vsrc;
177 int len;
178 {
179 u_char *dst = vdst;
180 u_char *src = vsrc;
181
182 while (len--) {
183 *dst++ = i2digit(*src >> 4);
184 *dst++ = i2digit(*src++);
185 }
186 *dst = '\0';
187 }
188
189 /*
190 * Convert an hex string into a byte array.
191 * This returns a pointer to the character following
192 * the last valid hex digit. If the string ends in
193 * the middle of a byte, NULL is returned.
194 */
195 static u_char *
196 hex2mem(vdst, src, maxlen)
197 void *vdst;
198 u_char *src;
199 int maxlen;
200 {
201 u_char *dst = vdst;
202 int msb, lsb;
203
204 while (*src && maxlen--) {
205 msb = digit2i(*src++);
206 if (msb < 0)
207 return (src - 1);
208 lsb = digit2i(*src++);
209 if (lsb < 0)
210 return (NULL);
211 *dst++ = (msb << 4) | lsb;
212 }
213 return (src);
214 }
215
216 /*
217 * Convert an hex string into an integer.
218 * This returns a pointer to the character following
219 * the last valid hex digit.
220 */
221 static vaddr_t
222 hex2i(srcp)
223 u_char **srcp;
224 {
225 char *src = *srcp;
226 vaddr_t r = 0;
227 int nibble;
228
229 while ((nibble = digit2i(*src)) >= 0) {
230 r *= 16;
231 r += nibble;
232 src++;
233 }
234 *srcp = src;
235 return (r);
236 }
237
238 /*
239 * Send a packet.
240 */
241 static void
242 kgdb_send(bp)
243 u_char *bp;
244 {
245 u_char *p;
246 u_char csum, c;
247
248 DPRINTF(("kgdb_send: %s\n", bp));
249 do {
250 p = bp;
251 PUTC(KGDB_START);
252 for (csum = 0; (c = *p); p++) {
253 PUTC(c);
254 csum += c;
255 }
256 PUTC(KGDB_END);
257 PUTC(i2digit(csum >> 4));
258 PUTC(i2digit(csum));
259 } while ((c = GETC() & 0x7f) == KGDB_BADP);
260 }
261
262 /*
263 * Receive a packet.
264 */
265 static int
266 kgdb_recv(bp, maxlen)
267 u_char *bp;
268 int maxlen;
269 {
270 u_char *p;
271 int c, csum, tmpcsum;
272 int len;
273
274 DPRINTF(("kgdb_recv: "));
275 do {
276 p = bp;
277 csum = len = 0;
278 while ((c = GETC()) != KGDB_START)
279 DPRINTF(("%c",c));
280 DPRINTF(("%c Start ",c));
281
282 while ((c = GETC()) != KGDB_END && len < maxlen) {
283 DPRINTF(("%c",c));
284 c &= 0x7f;
285 csum += c;
286 *p++ = c;
287 len++;
288 }
289 csum &= 0xff;
290 *p = '\0';
291 DPRINTF(("%c End ", c));
292
293 if (len >= maxlen) {
294 DPRINTF(("Long- "));
295 PUTC(KGDB_BADP);
296 continue;
297 }
298 tmpcsum = csum;
299
300 c = GETC();
301 DPRINTF(("%c",c));
302 csum -= digit2i(c) * 16;
303 c = GETC();
304 DPRINTF(("%c",c));
305 csum -= digit2i(c);
306
307 if (csum == 0) {
308 DPRINTF(("Good+ "));
309 PUTC(KGDB_GOODP);
310 /* Sequence present? */
311 if (bp[2] == ':') {
312 DPRINTF(("Seq %c%c ", bp[0], bp[1]));
313 PUTC(bp[0]);
314 PUTC(bp[1]);
315 len -= 3;
316 kgdb_copy(bp + 3, bp, len);
317 }
318 break;
319 }
320 DPRINTF((" Bad(wanted %x, off by %d)- ", tmpcsum, csum));
321 PUTC(KGDB_BADP);
322 } while (1);
323 DPRINTF(("kgdb_recv: %s\n", bp));
324 return (len);
325 }
326
327 /*
328 * This is called by the appropriate tty driver.
329 */
330 void
331 kgdb_attach(getfn, putfn, ioarg)
332 int (*getfn) __P((void *));
333 void (*putfn) __P((void *, int));
334 void *ioarg;
335 {
336 kgdb_getc = getfn;
337 kgdb_putc = putfn;
338 kgdb_ioarg = ioarg;
339 }
340
341 /*
342 * This function does all command processing for interfacing to
343 * a remote gdb. Note that the error codes are ignored by gdb
344 * at present, but might eventually become meaningful. (XXX)
345 * It might makes sense to use POSIX errno values, because
346 * that is what the gdb/remote.c functions want to return.
347 */
348 int
349 kgdb_trap(type, regs)
350 int type;
351 db_regs_t *regs;
352 {
353 label_t jmpbuf;
354 vaddr_t addr;
355 size_t len;
356 u_char *p;
357
358 printf("kgdb_trap 1\n");
359
360 if (kgdb_dev < 0 || kgdb_getc == NULL) {
361 /* not debugging */
362 return (0);
363 }
364
365 db_clear_single_step(regs);
366
367 if (db_trap_callback) db_trap_callback(1);
368
369 /* Detect and recover from unexpected traps. */
370 if (kgdb_recover != 0) {
371 printf("kgdb: caught trap 0x%x at %p\n",
372 type, (void*)PC_REGS(regs));
373 kgdb_send("E0E"); /* 14==EFAULT */
374 longjmp(kgdb_recover);
375 }
376
377 /*
378 * The first entry to this function is normally through
379 * a breakpoint trap in kgdb_connect(), in which case we
380 * must advance past the breakpoint because gdb will not.
381 *
382 * Machines vary as to where they leave the PC after a
383 * breakpoint trap. Those that leave the PC set to the
384 * address of the trap instruction (i.e. pc532) will not
385 * define FIXUP_PC_AFTER_BREAK(), and therefore will just
386 * advance the PC. On machines that leave the PC set to
387 * the instruction after the trap, FIXUP_PC_AFTER_BREAK
388 * will be defined to back-up the PC, so that after the
389 * "first-time" part of the if statement below has run,
390 * the PC will be the same as it was on entry.
391 *
392 * On the first entry here, we expect that gdb is not yet
393 * listening to us, so just enter the interaction loop.
394 * After the debugger is "active" (connected) it will be
395 * waiting for a "signaled" message from us.
396 */
397 if (kgdb_active == 0) {
398 if (!IS_BREAKPOINT_TRAP(type, 0)) {
399 /* No debugger active -- let trap handle this. */
400 if (db_trap_callback) db_trap_callback(0);
401 return (0);
402 }
403 /* Make the PC point at the breakpoint... */
404 #ifdef FIXUP_PC_AFTER_BREAK
405 FIXUP_PC_AFTER_BREAK(regs);
406 #endif
407 /* ... and then advance past it. */
408 #ifdef PC_ADVANCE
409 PC_ADVANCE(regs);
410 #else
411 PC_REGS(regs) += BKPT_SIZE;
412 #endif
413 kgdb_active = 1;
414 } else {
415 /* Tell remote host that an exception has occurred. */
416 sprintf(buffer, "S%02x", kgdb_signal(type));
417 kgdb_send(buffer);
418 }
419
420 /* Stick frame regs into our reg cache. */
421 kgdb_getregs(regs, gdb_regs);
422
423 /*
424 * Interact with gdb until it lets us go.
425 * If we cause a trap, resume here.
426 */
427 (void)setjmp((kgdb_recover = &jmpbuf));
428 for (;;) {
429 kgdb_recv(buffer, sizeof(buffer));
430 switch (buffer[0]) {
431
432 default:
433 /* Unknown command. */
434 kgdb_send("");
435 continue;
436
437 case KGDB_SIGNAL:
438 /*
439 * if this command came from a running gdb,
440 * answer it -- the other guy has no way of
441 * knowing if we're in or out of this loop
442 * when he issues a "remote-signal".
443 */
444 sprintf(buffer, "S%02x", kgdb_signal(type));
445 kgdb_send(buffer);
446 continue;
447
448 case KGDB_REG_R:
449 mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
450 kgdb_send(buffer);
451 continue;
452
453 case KGDB_REG_W:
454 p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
455 if (p == NULL || *p != '\0')
456 kgdb_send("E01");
457 else {
458 kgdb_setregs(regs, gdb_regs);
459 kgdb_send("OK");
460 }
461 continue;
462
463 case KGDB_MEM_R:
464 p = buffer + 1;
465 addr = hex2i(&p);
466 if (*p++ != ',') {
467 kgdb_send("E02");
468 continue;
469 }
470 len = hex2i(&p);
471 if (*p != '\0') {
472 kgdb_send("E03");
473 continue;
474 }
475 if (len > sizeof(buffer) / 2) {
476 kgdb_send("E04");
477 continue;
478 }
479 if (kgdb_acc(addr, len) == 0) {
480 kgdb_send("E05");
481 continue;
482 }
483 db_read_bytes(addr, (size_t)len,
484 (char *)buffer + sizeof(buffer) / 2);
485 mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
486 kgdb_send(buffer);
487 continue;
488
489 case KGDB_MEM_W:
490 p = buffer + 1;
491 addr = hex2i(&p);
492 if (*p++ != ',') {
493 kgdb_send("E06");
494 continue;
495 }
496 len = hex2i(&p);
497 if (*p++ != ':') {
498 kgdb_send("E07");
499 continue;
500 }
501 if (len > (sizeof(buffer) - (p - buffer))) {
502 kgdb_send("E08");
503 continue;
504 }
505 p = hex2mem(buffer, p, sizeof(buffer));
506 if (p == NULL) {
507 kgdb_send("E09");
508 continue;
509 }
510 if (kgdb_acc(addr, len) == 0) {
511 kgdb_send("E0A");
512 continue;
513 }
514 db_write_bytes(addr, (size_t)len, (char *)buffer);
515 kgdb_send("OK");
516 continue;
517
518 case KGDB_DETACH:
519 case KGDB_KILL:
520 kgdb_active = 0;
521 printf("kgdb detached\n");
522 db_clear_single_step(regs);
523 kgdb_send("OK");
524 goto out;
525
526 case KGDB_CONT:
527 if (buffer[1]) {
528 p = buffer + 1;
529 addr = hex2i(&p);
530 if (*p) {
531 kgdb_send("E0B");
532 continue;
533 }
534 PC_REGS(regs) = addr;
535 DPRINTF(("kgdb: continuing at %08lx\n", addr))
536
537 } else {
538 DPRINTF((
539 "kgdb: continuing at old address %08lx\n",
540 PC_REGS(regs)));
541 }
542
543 db_clear_single_step(regs);
544 goto out;
545
546 case KGDB_STEP:
547 if (buffer[1]) {
548 p = buffer + 1;
549 addr = hex2i(&p);
550 if (*p) {
551 kgdb_send("E0B");
552 continue;
553 }
554 PC_REGS(regs) = addr;
555 }
556 db_set_single_step(regs);
557 goto out;
558 }
559 }
560 out:
561 if (db_trap_callback) db_trap_callback(0);
562 kgdb_recover = 0;
563 return (1);
564 }
565