2 * Copyright (c) 2004 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/ctype.h>
35 #include <machine/gdb_machdep.h>
38 #include <gdb/gdb_int.h>
40 static char gdb_rxbuf[GDB_BUFSZ];
43 static char gdb_txbuf[GDB_BUFSZ];
44 char *gdb_txp = NULL; /* Used in inline functions. */
46 #define C2N(c) (((c) < 'A') ? (c) - '0' : \
47 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
48 #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
51 * Get a single character
60 c = gdb_cur->gdb_getc();
66 * Functions to receive and extract from a packet.
77 * Wait for the start character, ignore all others.
78 * XXX needs a timeout.
80 while ((c = gdb_getc()) != '$')
83 /* Read until a # or end of buffer is found. */
86 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
90 gdb_rxbuf[gdb_rxsz++] = c;
93 gdb_rxbuf[gdb_rxsz] = 0;
96 /* Bail out on a buffer overflow. */
98 gdb_cur->gdb_putc('-');
103 cksum -= (C2N(c) << 4) & 0xf0;
105 cksum -= C2N(c) & 0x0f;
106 gdb_cur->gdb_putc((cksum == 0) ? '+' : '-');
108 printf("GDB: packet `%s' has invalid checksum\n",
110 } while (cksum != 0);
117 gdb_rx_equal(const char *str)
122 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
130 gdb_rx_mem(unsigned char *addr, size_t size)
137 if (size * 2 != gdb_rxsz)
140 prev = kdb_jmpbuf(jb);
144 c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
145 c |= C2N(gdb_rxp[1]) & 0x0f;
151 (void)kdb_jmpbuf(prev);
152 return ((ret == 0) ? 1 : 0);
156 gdb_rx_varhex(uintmax_t *vp)
162 neg = (c == '-') ? 1 : 0;
166 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
167 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
175 } while (isxdigit(c));
180 *vp = (neg) ? -v : v;
185 * Function to build and send a package.
189 gdb_tx_begin(char tp)
202 unsigned char c, cksum;
205 gdb_cur->gdb_putc('$');
209 while (p < gdb_txp) {
210 /* Send a character and start run-length encoding. */
212 gdb_cur->gdb_putc(c);
215 /* Determine run-length and update checksum. */
216 while (p < gdb_txp && *p == c) {
220 /* Emit the run-length encoded string. */
221 while (runlen >= 97) {
222 gdb_cur->gdb_putc('*');
224 gdb_cur->gdb_putc(97+29);
228 gdb_cur->gdb_putc(c);
234 gdb_cur->gdb_putc(c);
240 /* Don't emit '$', '#', '+' or '-'. */
242 gdb_cur->gdb_putc(c);
246 if (runlen == 6 || runlen == 14 || runlen == 16) {
247 gdb_cur->gdb_putc(c);
251 gdb_cur->gdb_putc('*');
253 gdb_cur->gdb_putc(runlen+29);
257 gdb_cur->gdb_putc('#');
259 gdb_cur->gdb_putc(N2C(c));
261 gdb_cur->gdb_putc(N2C(c));
270 gdb_tx_mem(const unsigned char *addr, size_t size)
276 prev = kdb_jmpbuf(jb);
280 *gdb_txp++ = N2C(*addr >> 4);
281 *gdb_txp++ = N2C(*addr & 0x0f);
285 (void)kdb_jmpbuf(prev);
286 return ((ret == 0) ? 1 : 0);
290 gdb_tx_reg(int regnum)
295 regp = gdb_cpu_getreg(regnum, ®sz);
297 /* Register unavailable. */
303 gdb_tx_mem(regp, regsz);