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