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