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