2 * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/systm.h>
31 #include <sys/ctype.h>
33 #include <sys/libkern.h>
34 #include <sys/ttydefaults.h>
36 #include <machine/gdb_machdep.h>
37 #include <machine/kdb.h>
40 #include <gdb/gdb_int.h>
42 static char gdb_rxbuf[GDB_BUFSZ];
47 * The goal here is to allow in-place framing without making the math around
48 * 'gdb_txbuf' more complicated. A generous reading of union special rule for
49 * "common initial sequence" suggests this may be valid in standard C99 and
55 char mb_buf[GDB_BUFSZ];
57 } __packed txu_midbuf;
58 /* sizeof includes trailing nul byte and this is intentional. */
59 char txu_fullbuf[GDB_BUFSZ + sizeof("$#..")];
61 #define gdb_txbuf gdb_tx_u.txu_midbuf.mb_buf
62 #define gdb_tx_fullbuf gdb_tx_u.txu_fullbuf
63 _Static_assert(sizeof(gdb_tx_u.txu_midbuf) == sizeof(gdb_tx_u.txu_fullbuf) &&
64 offsetof(struct _midbuf, mb_buf) == 1,
65 "assertions necessary for correctness");
66 char *gdb_txp = NULL; /* Used in inline functions. */
68 #define C2N(c) (((c) < 'A') ? (c) - '0' : \
69 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
70 #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
73 * Get a single character
82 c = gdb_cur->gdb_getc();
86 printf("Received ^C; trying to switch back to ddb.\n");
88 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM)
91 if (kdb_dbbe_select("ddb") != 0)
92 printf("The ddb backend could not be selected.\n");
94 printf("using longjmp, hope it works!\n");
102 * Functions to receive and extract from a packet.
113 * Wait for the start character, ignore all others.
114 * XXX needs a timeout.
116 while ((c = gdb_getc()) != '$')
119 /* Read until a # or end of buffer is found. */
122 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
126 gdb_rxbuf[gdb_rxsz++] = c;
129 gdb_rxbuf[gdb_rxsz] = 0;
132 /* Bail out on a buffer overflow. */
139 * In Not-AckMode, we can assume reliable transport and neither
140 * need to verify checksums nor send Ack/Nack.
146 cksum -= (C2N(c) << 4) & 0xf0;
148 cksum -= C2N(c) & 0x0f;
153 printf("GDB: packet `%s' has invalid checksum\n",
156 } while (cksum != 0);
163 gdb_rx_equal(const char *str)
168 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
176 gdb_rx_mem(unsigned char *addr, size_t size)
186 if (size * 2 != gdb_rxsz)
189 wctx = gdb_begin_write();
190 prev = kdb_jmpbuf(jb);
196 c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
197 c |= C2N(gdb_rxp[1]) & 0x0f;
202 kdb_cpu_sync_icache(addr, size);
204 (void)kdb_jmpbuf(prev);
206 return ((ret == 0) ? 1 : 0);
210 gdb_rx_varhex(uintmax_t *vp)
216 neg = (c == '-') ? 1 : 0;
220 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
221 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
229 } while (isxdigit(c));
234 *vp = (neg) ? -v : v;
239 * Function to build and send a package.
243 gdb_tx_begin(char tp)
252 * Take raw packet buffer and perform typical GDB packet framing, but not run-
253 * length encoding, before forwarding to driver ::gdb_sendpacket() routine.
256 gdb_tx_sendpacket(void)
261 msglen = gdb_txp - gdb_txbuf;
263 /* Add GDB packet framing */
264 gdb_tx_fullbuf[0] = '$';
267 for (i = 0; i < msglen; i++)
268 csum += (unsigned char)gdb_txbuf[i];
269 snprintf(&gdb_tx_fullbuf[1 + msglen], 4, "#%02x", (unsigned)csum);
271 gdb_cur->gdb_sendpacket(gdb_tx_fullbuf, msglen + 4);
279 unsigned char c, cksum;
282 if (gdb_cur->gdb_sendpacket != NULL) {
287 gdb_cur->gdb_putc('$');
291 while (p < gdb_txp) {
292 /* Send a character and start run-length encoding. */
294 gdb_cur->gdb_putc(c);
297 /* Determine run-length and update checksum. */
298 while (p < gdb_txp && *p == c) {
302 /* Emit the run-length encoded string. */
303 while (runlen >= 97) {
304 gdb_cur->gdb_putc('*');
306 gdb_cur->gdb_putc(97+29);
310 gdb_cur->gdb_putc(c);
315 /* Don't emit '$', '#', '+', '-' or a run length below 3. */
316 while (runlen == 1 || runlen == 2 ||
317 runlen + 29 == '$' || runlen + 29 == '#' ||
318 runlen + 29 == '+' || runlen + 29 == '-') {
319 gdb_cur->gdb_putc(c);
325 gdb_cur->gdb_putc('*');
327 gdb_cur->gdb_putc(runlen+29);
331 gdb_cur->gdb_putc('#');
333 gdb_cur->gdb_putc(N2C(c));
335 gdb_cur->gdb_putc(N2C(c));
339 * In NoAckMode, it is assumed that the underlying transport is
340 * reliable and thus neither conservant sends acknowledgements;
341 * there is nothing to wait for here.
353 gdb_tx_mem(const unsigned char *addr, size_t size)
359 prev = kdb_jmpbuf(jb);
363 *gdb_txp++ = N2C(*addr >> 4);
364 *gdb_txp++ = N2C(*addr & 0x0f);
368 (void)kdb_jmpbuf(prev);
369 return ((ret == 0) ? 1 : 0);
373 gdb_tx_reg(int regnum)
378 regp = gdb_cpu_getreg(regnum, ®sz);
380 /* Register unavailable. */
386 gdb_tx_mem(regp, regsz);
390 gdb_txbuf_has_capacity(size_t req)
392 return (((char *)gdb_txbuf + sizeof(gdb_txbuf) - gdb_txp) >= req);
395 /* Read binary data up until the end of the packet or until we have datalen decoded bytes */
397 gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt)
403 while (*amt < datalen) {
407 /* Escaped character up next */
409 /* Malformed packet. */
410 if ((c = gdb_rx_char()) == EOF)
414 *(data++) = c & 0xff;
422 gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found)
428 prev = kdb_jmpbuf(jb);
431 *found = memmem(addr, size, pat, patlen);
433 (void)kdb_jmpbuf(prev);
434 return ((ret == 0) ? 1 : 0);