Home | History | Annotate | Line # | Download | only in kern
kgdb_stub.c revision 1.26
      1 /*	$NetBSD: kgdb_stub.c,v 1.26 2013/05/11 15:44:46 skrll 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.26 2013/05/11 15:44:46 skrll 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)
    349 		db_trap_callback(1);
    350 
    351 	/* Detect and recover from unexpected traps. */
    352 	if (kgdb_recover != 0) {
    353 		printf("kgdb: caught trap 0x%x at %p\n",
    354 			   type, (void*)PC_REGS(regs));
    355 		kgdb_send("E0E"); /* 14==EFAULT */
    356 		longjmp(kgdb_recover);
    357 	}
    358 
    359 	/*
    360 	 * The first entry to this function is normally through
    361 	 * a breakpoint trap in kgdb_connect(), in which case we
    362 	 * must advance past the breakpoint because gdb will not.
    363 	 *
    364 	 * Machines vary as to where they leave the PC after a
    365 	 * breakpoint trap.  Those that leave the PC set to the
    366 	 * address of the trap instruction (i.e. pc532) will not
    367 	 * define FIXUP_PC_AFTER_BREAK(), and therefore will just
    368 	 * advance the PC.  On machines that leave the PC set to
    369 	 * the instruction after the trap, FIXUP_PC_AFTER_BREAK
    370 	 * will be defined to back-up the PC, so that after the
    371 	 * "first-time" part of the if statement below has run,
    372 	 * the PC will be the same as it was on entry.
    373 	 *
    374 	 * On the first entry here, we expect that gdb is not yet
    375 	 * listening to us, so just enter the interaction loop.
    376 	 * After the debugger is "active" (connected) it will be
    377 	 * waiting for a "signaled" message from us.
    378 	 */
    379 	if (kgdb_active == 0) {
    380 		if (!IS_BREAKPOINT_TRAP(type, 0)) {
    381 			/* No debugger active -- let trap handle this. */
    382 			if (db_trap_callback)
    383 				db_trap_callback(0);
    384 			return (0);
    385 		}
    386 		/* Make the PC point at the breakpoint... */
    387 #ifdef	FIXUP_PC_AFTER_BREAK
    388 		FIXUP_PC_AFTER_BREAK(regs);
    389 #endif
    390 		/* ... and then advance past it. */
    391 #ifdef	PC_ADVANCE
    392 		PC_ADVANCE(regs);
    393 #else
    394 		PC_REGS(regs) += BKPT_SIZE;
    395 #endif
    396 		kgdb_active = 1;
    397 	} else {
    398 		/* Tell remote host that an exception has occurred. */
    399 		snprintf(buffer, sizeof(buffer), "S%02x", kgdb_signal(type));
    400 		kgdb_send(buffer);
    401 	}
    402 
    403 	/* Stick frame regs into our reg cache. */
    404 	kgdb_getregs(regs, gdb_regs);
    405 
    406 	/*
    407 	 * Interact with gdb until it lets us go.
    408 	 * If we cause a trap, resume here.
    409 	 */
    410 	(void)setjmp((kgdb_recover = &jmpbuf));
    411 	for (;;) {
    412 		kgdb_recv(buffer, sizeof(buffer));
    413 		switch (buffer[0]) {
    414 
    415 		default:
    416 			/* Unknown command. */
    417 			kgdb_send("");
    418 			continue;
    419 
    420 		case KGDB_SIGNAL:
    421 			/*
    422 			 * if this command came from a running gdb,
    423 			 * answer it -- the other guy has no way of
    424 			 * knowing if we're in or out of this loop
    425 			 * when he issues a "remote-signal".
    426 			 */
    427 			snprintf(buffer, sizeof(buffer), "S%02x",
    428 			    kgdb_signal(type));
    429 			kgdb_send(buffer);
    430 			continue;
    431 
    432 		case KGDB_REG_R:
    433 			mem2hex(buffer, gdb_regs, sizeof(gdb_regs));
    434 			kgdb_send(buffer);
    435 			continue;
    436 
    437 		case KGDB_REG_W:
    438 			p = hex2mem(gdb_regs, buffer + 1, sizeof(gdb_regs));
    439 			if (p == NULL || *p != '\0')
    440 				kgdb_send("E01");
    441 			else {
    442 				kgdb_setregs(regs, gdb_regs);
    443 				kgdb_send("OK");
    444 			}
    445 			continue;
    446 
    447 		case KGDB_MEM_R:
    448 			p = buffer + 1;
    449 			addr = hex2i(&p);
    450 			if (*p++ != ',') {
    451 				kgdb_send("E02");
    452 				continue;
    453 			}
    454 			len = hex2i(&p);
    455 			if (*p != '\0') {
    456 				kgdb_send("E03");
    457 				continue;
    458 			}
    459 			if (len > sizeof(buffer) / 2) {
    460 				kgdb_send("E04");
    461 				continue;
    462 			}
    463 			if (kgdb_acc(addr, len) == 0) {
    464 				kgdb_send("E05");
    465 				continue;
    466 			}
    467 			db_read_bytes(addr, (size_t)len,
    468 					(char *)buffer + sizeof(buffer) / 2);
    469 			mem2hex(buffer, buffer + sizeof(buffer) / 2, len);
    470 			kgdb_send(buffer);
    471 			continue;
    472 
    473 		case KGDB_MEM_W:
    474 			p = buffer + 1;
    475 			addr = hex2i(&p);
    476 			if (*p++ != ',') {
    477 				kgdb_send("E06");
    478 				continue;
    479 			}
    480 			len = hex2i(&p);
    481 			if (*p++ != ':') {
    482 				kgdb_send("E07");
    483 				continue;
    484 			}
    485 			if (len > (sizeof(buffer) - (p - buffer))) {
    486 				kgdb_send("E08");
    487 				continue;
    488 			}
    489 			p = hex2mem(buffer, p, sizeof(buffer));
    490 			if (p == NULL) {
    491 				kgdb_send("E09");
    492 				continue;
    493 			}
    494 			if (kgdb_acc(addr, len) == 0) {
    495 				kgdb_send("E0A");
    496 				continue;
    497 			}
    498 			db_write_bytes(addr, (size_t)len, (char *)buffer);
    499 			kgdb_send("OK");
    500 			continue;
    501 
    502 		case KGDB_DETACH:
    503 		case KGDB_KILL:
    504 			kgdb_active = 0;
    505 			printf("kgdb detached\n");
    506 			db_clear_single_step(regs);
    507 			kgdb_send("OK");
    508 			goto out;
    509 
    510 		case KGDB_CONT:
    511 			if (buffer[1]) {
    512 				p = buffer + 1;
    513 				addr = hex2i(&p);
    514 				if (*p) {
    515 					kgdb_send("E0B");
    516 					continue;
    517 				}
    518 				PC_REGS(regs) = addr;
    519 				DPRINTF(("kgdb: continuing at %08lx\n", addr));
    520 
    521 			} else {
    522 				DPRINTF((
    523 				  "kgdb: continuing at old address %08lx\n",
    524 				  (vaddr_t)PC_REGS(regs)));
    525 			}
    526 
    527 			db_clear_single_step(regs);
    528 			goto out;
    529 
    530 		case KGDB_STEP:
    531 			if (buffer[1]) {
    532 				p = buffer + 1;
    533 				addr = hex2i(&p);
    534 				if (*p) {
    535 					kgdb_send("E0B");
    536 					continue;
    537 				}
    538 				PC_REGS(regs) = addr;
    539 			}
    540 			db_set_single_step(regs);
    541 			goto out;
    542 		}
    543 	}
    544  out:
    545 	if (db_trap_callback)
    546 		db_trap_callback(0);
    547 	kgdb_recover = 0;
    548 	return (1);
    549 }
    550 
    551 int
    552 kgdb_disconnected(void)
    553 {
    554 	return 1;
    555 }
    556