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