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