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/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/ctype.h>
34 #include <sys/libkern.h>
35 #include <sys/ttydefaults.h>
37 #include <machine/gdb_machdep.h>
38 #include <machine/kdb.h>
41 #include <gdb/gdb_int.h>
43 static char gdb_rxbuf[GDB_BUFSZ];
48 * The goal here is to allow in-place framing without making the math around
49 * 'gdb_txbuf' more complicated. A generous reading of union special rule for
50 * "common initial sequence" suggests this may be valid in standard C99 and
56 char mb_buf[GDB_BUFSZ];
58 } __packed txu_midbuf;
59 /* sizeof includes trailing nul byte and this is intentional. */
60 char txu_fullbuf[GDB_BUFSZ + sizeof("$#..")];
62 #define gdb_txbuf gdb_tx_u.txu_midbuf.mb_buf
63 #define gdb_tx_fullbuf gdb_tx_u.txu_fullbuf
64 _Static_assert(sizeof(gdb_tx_u.txu_midbuf) == sizeof(gdb_tx_u.txu_fullbuf) &&
65 offsetof(struct _midbuf, mb_buf) == 1,
66 "assertions necessary for correctness");
67 char *gdb_txp = NULL; /* Used in inline functions. */
69 #define C2N(c) (((c) < 'A') ? (c) - '0' : \
70 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a'))
71 #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10)
74 * Get a single character
83 c = gdb_cur->gdb_getc();
87 printf("Received ^C; trying to switch back to ddb.\n");
89 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM)
92 if (kdb_dbbe_select("ddb") != 0)
93 printf("The ddb backend could not be selected.\n");
95 printf("using longjmp, hope it works!\n");
103 * Functions to receive and extract from a packet.
114 * Wait for the start character, ignore all others.
115 * XXX needs a timeout.
117 while ((c = gdb_getc()) != '$')
120 /* Read until a # or end of buffer is found. */
123 while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) {
127 gdb_rxbuf[gdb_rxsz++] = c;
130 gdb_rxbuf[gdb_rxsz] = 0;
133 /* Bail out on a buffer overflow. */
140 * In Not-AckMode, we can assume reliable transport and neither
141 * need to verify checksums nor send Ack/Nack.
147 cksum -= (C2N(c) << 4) & 0xf0;
149 cksum -= C2N(c) & 0x0f;
154 printf("GDB: packet `%s' has invalid checksum\n",
157 } while (cksum != 0);
164 gdb_rx_equal(const char *str)
169 if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0)
177 gdb_rx_mem(unsigned char *addr, size_t size)
187 if (size * 2 != gdb_rxsz)
190 wctx = gdb_begin_write();
191 prev = kdb_jmpbuf(jb);
197 c = (C2N(gdb_rxp[0]) << 4) & 0xf0;
198 c |= C2N(gdb_rxp[1]) & 0x0f;
203 kdb_cpu_sync_icache(addr, size);
205 (void)kdb_jmpbuf(prev);
207 return ((ret == 0) ? 1 : 0);
211 gdb_rx_varhex(uintmax_t *vp)
217 neg = (c == '-') ? 1 : 0;
221 gdb_rxp -= ((c == -1) ? 0 : 1) + neg;
222 gdb_rxsz += ((c == -1) ? 0 : 1) + neg;
230 } while (isxdigit(c));
235 *vp = (neg) ? -v : v;
240 * Function to build and send a package.
244 gdb_tx_begin(char tp)
253 * Take raw packet buffer and perform typical GDB packet framing, but not run-
254 * length encoding, before forwarding to driver ::gdb_sendpacket() routine.
257 gdb_tx_sendpacket(void)
262 msglen = gdb_txp - gdb_txbuf;
264 /* Add GDB packet framing */
265 gdb_tx_fullbuf[0] = '$';
268 for (i = 0; i < msglen; i++)
269 csum += (unsigned char)gdb_txbuf[i];
270 snprintf(&gdb_tx_fullbuf[1 + msglen], 4, "#%02x", (unsigned)csum);
272 gdb_cur->gdb_sendpacket(gdb_tx_fullbuf, msglen + 4);
280 unsigned char c, cksum;
283 if (gdb_cur->gdb_sendpacket != NULL) {
288 gdb_cur->gdb_putc('$');
292 while (p < gdb_txp) {
293 /* Send a character and start run-length encoding. */
295 gdb_cur->gdb_putc(c);
298 /* Determine run-length and update checksum. */
299 while (p < gdb_txp && *p == c) {
303 /* Emit the run-length encoded string. */
304 while (runlen >= 97) {
305 gdb_cur->gdb_putc('*');
307 gdb_cur->gdb_putc(97+29);
311 gdb_cur->gdb_putc(c);
316 /* Don't emit '$', '#', '+', '-' or a run length below 3. */
317 while (runlen == 1 || runlen == 2 ||
318 runlen + 29 == '$' || runlen + 29 == '#' ||
319 runlen + 29 == '+' || runlen + 29 == '-') {
320 gdb_cur->gdb_putc(c);
326 gdb_cur->gdb_putc('*');
328 gdb_cur->gdb_putc(runlen+29);
332 gdb_cur->gdb_putc('#');
334 gdb_cur->gdb_putc(N2C(c));
336 gdb_cur->gdb_putc(N2C(c));
340 * In NoAckMode, it is assumed that the underlying transport is
341 * reliable and thus neither conservant sends acknowledgements;
342 * there is nothing to wait for here.
354 gdb_tx_mem(const unsigned char *addr, size_t size)
360 prev = kdb_jmpbuf(jb);
364 *gdb_txp++ = N2C(*addr >> 4);
365 *gdb_txp++ = N2C(*addr & 0x0f);
369 (void)kdb_jmpbuf(prev);
370 return ((ret == 0) ? 1 : 0);
374 gdb_tx_reg(int regnum)
379 regp = gdb_cpu_getreg(regnum, ®sz);
381 /* Register unavailable. */
387 gdb_tx_mem(regp, regsz);
391 gdb_txbuf_has_capacity(size_t req)
393 return (((char *)gdb_txbuf + sizeof(gdb_txbuf) - gdb_txp) >= req);
396 /* Read binary data up until the end of the packet or until we have datalen decoded bytes */
398 gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt)
404 while (*amt < datalen) {
408 /* Escaped character up next */
410 /* Malformed packet. */
411 if ((c = gdb_rx_char()) == EOF)
415 *(data++) = c & 0xff;
423 gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found)
429 prev = kdb_jmpbuf(jb);
432 *found = memmem(addr, size, pat, patlen);
434 (void)kdb_jmpbuf(prev);
435 return ((ret == 0) ? 1 : 0);