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