2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ctype.h>
36 #include <sys/libkern.h>
37 #include <sys/ttydefaults.h>
39 #include <machine/gdb_machdep.h>
40 #include <machine/kdb.h>
43 #include <gdb/gdb_int.h>
45 static char gdb_rxbuf[GDB_BUFSZ];
50 * The goal here is to allow in-place framing without making the math around
51 * 'gdb_txbuf' more complicated. A generous reading of union special rule for
52 * "common initial sequence" suggests this may be valid in standard C99 and
58 char mb_buf[GDB_BUFSZ];
60 } __packed txu_midbuf;
61 /* sizeof includes trailing nul byte and this is intentional. */
62 char txu_fullbuf[GDB_BUFSZ + sizeof("$#..")];
64 #define gdb_txbuf gdb_tx_u.txu_midbuf.mb_buf
65 #define gdb_tx_fullbuf gdb_tx_u.txu_fullbuf
66 _Static_assert(sizeof(gdb_tx_u.txu_midbuf) == sizeof(gdb_tx_u.txu_fullbuf) &&
67 offsetof(struct _midbuf, mb_buf) == 1,
68 "assertions necessary for correctness");
69 char *gdb_txp = NULL; /* Used in inline functions. */
71 #define C2N(c) (((c) < 'A') ? (c) - '0' : \
72 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
73 #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
76 * Get a single character
85 c = gdb_cur->gdb_getc();
89 printf("Received ^C; trying to switch back to ddb.\n");
91 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM)
94 if (kdb_dbbe_select("ddb") != 0)
95 printf("The ddb backend could not be selected.\n");
97 printf("using longjmp, hope it works!\n");
105 * Functions to receive and extract from a packet.
116 * Wait for the start character, ignore all others.
117 * XXX needs a timeout.
119 while ((c = gdb_getc()) != '$')
122 /* Read until a # or end of buffer is found. */
125 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
129 gdb_rxbuf[gdb_rxsz++] = c;
132 gdb_rxbuf[gdb_rxsz] = 0;
135 /* Bail out on a buffer overflow. */
142 * In Not-AckMode, we can assume reliable transport and neither
143 * need to verify checksums nor send Ack/Nack.
149 cksum -= (C2N(c) << 4) & 0xf0;
151 cksum -= C2N(c) & 0x0f;
156 printf("GDB: packet `%s' has invalid checksum\n",
159 } while (cksum != 0);
166 gdb_rx_equal(const char *str)
171 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
179 gdb_rx_mem(unsigned char *addr, size_t size)
189 if (size * 2 != gdb_rxsz)
192 wctx = gdb_begin_write();
193 prev = kdb_jmpbuf(jb);
199 c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
200 c |= C2N(gdb_rxp[1]) & 0x0f;
205 kdb_cpu_sync_icache(addr, size);
207 (void)kdb_jmpbuf(prev);
209 return ((ret == 0) ? 1 : 0);
213 gdb_rx_varhex(uintmax_t *vp)
219 neg = (c == '-') ? 1 : 0;
223 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
224 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
232 } while (isxdigit(c));
237 *vp = (neg) ? -v : v;
242 * Function to build and send a package.
246 gdb_tx_begin(char tp)
255 * Take raw packet buffer and perform typical GDB packet framing, but not run-
256 * length encoding, before forwarding to driver ::gdb_sendpacket() routine.
259 gdb_tx_sendpacket(void)
264 msglen = gdb_txp - gdb_txbuf;
266 /* Add GDB packet framing */
267 gdb_tx_fullbuf[0] = '$';
270 for (i = 0; i < msglen; i++)
271 csum += (unsigned char)gdb_txbuf[i];
272 snprintf(&gdb_tx_fullbuf[1 + msglen], 4, "#%02x", (unsigned)csum);
274 gdb_cur->gdb_sendpacket(gdb_tx_fullbuf, msglen + 4);
282 unsigned char c, cksum;
285 if (gdb_cur->gdb_sendpacket != NULL) {
290 gdb_cur->gdb_putc('$');
294 while (p < gdb_txp) {
295 /* Send a character and start run-length encoding. */
297 gdb_cur->gdb_putc(c);
300 /* Determine run-length and update checksum. */
301 while (p < gdb_txp && *p == c) {
305 /* Emit the run-length encoded string. */
306 while (runlen >= 97) {
307 gdb_cur->gdb_putc('*');
309 gdb_cur->gdb_putc(97+29);
313 gdb_cur->gdb_putc(c);
319 gdb_cur->gdb_putc(c);
325 /* Don't emit '$', '#', '+' or '-'. */
327 gdb_cur->gdb_putc(c);
331 if (runlen == 6 || runlen == 14 || runlen == 16) {
332 gdb_cur->gdb_putc(c);
336 gdb_cur->gdb_putc('*');
338 gdb_cur->gdb_putc(runlen+29);
342 gdb_cur->gdb_putc('#');
344 gdb_cur->gdb_putc(N2C(c));
346 gdb_cur->gdb_putc(N2C(c));
350 * In NoAckMode, it is assumed that the underlying transport is
351 * reliable and thus neither conservant sends acknowledgements;
352 * there is nothing to wait for here.
364 gdb_tx_mem(const unsigned char *addr, size_t size)
370 prev = kdb_jmpbuf(jb);
374 *gdb_txp++ = N2C(*addr >> 4);
375 *gdb_txp++ = N2C(*addr & 0x0f);
379 (void)kdb_jmpbuf(prev);
380 return ((ret == 0) ? 1 : 0);
384 gdb_tx_reg(int regnum)
389 regp = gdb_cpu_getreg(regnum, ®sz);
391 /* Register unavailable. */
397 gdb_tx_mem(regp, regsz);
401 gdb_txbuf_has_capacity(size_t req)
403 return (((char *)gdb_txbuf + sizeof(gdb_txbuf) - gdb_txp) >= req);
406 /* Read binary data up until the end of the packet or until we have datalen decoded bytes */
408 gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt)
414 while (*amt < datalen) {
418 /* Escaped character up next */
420 /* Malformed packet. */
421 if ((c = gdb_rx_char()) == EOF)
425 *(data++) = c & 0xff;
433 gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found)
439 prev = kdb_jmpbuf(jb);
442 *found = memmem(addr, size, pat, patlen);
444 (void)kdb_jmpbuf(prev);
445 return ((ret == 0) ? 1 : 0);