Index: head/sys/gdb/gdb_int.h =================================================================== --- head/sys/gdb/gdb_int.h (revision 351237) +++ head/sys/gdb/gdb_int.h (revision 351238) @@ -1,117 +1,118 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _GDB_GDB_INT_H_ #define _GDB_GDB_INT_H_ extern struct gdb_dbgport *gdb_cur; extern int gdb_listening; void gdb_consinit(void); extern char *gdb_rxp; extern size_t gdb_rxsz; extern char *gdb_txp; int gdb_rx_begin(void); int gdb_rx_equal(const char *); int gdb_rx_mem(unsigned char *, size_t); int gdb_rx_varhex(uintmax_t *); static __inline int gdb_rx_char(void) { int c; if (gdb_rxsz > 0) { c = *gdb_rxp++; gdb_rxsz--; } else c = -1; return (c); } void gdb_tx_begin(char); int gdb_tx_end(void); int gdb_tx_mem(const unsigned char *, size_t); void gdb_tx_reg(int); +bool gdb_txbuf_has_capacity(size_t); int gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt); int gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found); static __inline void gdb_tx_char(char c) { *gdb_txp++ = c; } static __inline int gdb_tx_empty(void) { gdb_tx_begin('\0'); return (gdb_tx_end()); } static __inline void gdb_tx_hex(uintmax_t n, int sz) { gdb_txp += sprintf(gdb_txp, "%0*jx", sz, n); } static __inline int gdb_tx_err(int err) { gdb_tx_begin('E'); gdb_tx_hex(err, 2); return (gdb_tx_end()); } static __inline int gdb_tx_ok(void) { gdb_tx_begin('O'); gdb_tx_char('K'); return (gdb_tx_end()); } static __inline void gdb_tx_str(const char *s) { while (*s) *gdb_txp++ = *s++; } static __inline void gdb_tx_varhex(uintmax_t n) { gdb_txp += sprintf(gdb_txp, "%jx", n); } #endif /* !_GDB_GDB_INT_H_ */ Index: head/sys/gdb/gdb_main.c =================================================================== --- head/sys/gdb/gdb_main.c (revision 351237) +++ head/sys/gdb/gdb_main.c (revision 351238) @@ -1,339 +1,364 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include static dbbe_init_f gdb_init; static dbbe_trap_f gdb_trap; KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap); static struct gdb_dbgport null_gdb_dbgport; DATA_SET(gdb_dbgport_set, null_gdb_dbgport); SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); struct gdb_dbgport *gdb_cur = NULL; int gdb_listening = 0; static unsigned char gdb_bindata[64]; static int gdb_init(void) { struct gdb_dbgport *dp, **iter; int cur_pri, pri; gdb_cur = NULL; cur_pri = -1; SET_FOREACH(iter, gdb_dbgport_set) { dp = *iter; pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; dp->gdb_active = (pri >= 0) ? 0 : -1; if (pri > cur_pri) { cur_pri = pri; gdb_cur = dp; } } if (gdb_cur != NULL) { printf("GDB: debug ports:"); SET_FOREACH(iter, gdb_dbgport_set) { dp = *iter; if (dp->gdb_active == 0) printf(" %s", dp->gdb_name); } printf("\n"); } else printf("GDB: no debug ports present\n"); if (gdb_cur != NULL) { gdb_cur->gdb_init(); printf("GDB: current port: %s\n", gdb_cur->gdb_name); } if (gdb_cur != NULL) { cur_pri = (boothowto & RB_GDB) ? 2 : 0; gdb_consinit(); } else cur_pri = -1; return (cur_pri); } static void gdb_do_mem_search(void) { size_t patlen; intmax_t addr, size; const unsigned char *found; if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' || gdb_rx_varhex(&size) || gdb_rx_char() != ';' || gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) { gdb_tx_err(EINVAL); return; } if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, patlen, &found)) { if (found == 0ULL) gdb_tx_begin('0'); else { gdb_tx_begin('1'); gdb_tx_char(','); gdb_tx_hex((intmax_t)(uintptr_t)found, 8); } gdb_tx_end(); } else gdb_tx_err(EIO); } +static void +gdb_do_threadinfo(struct thread **thr_iter) +{ + static struct thread * const done_sentinel = (void *)(uintptr_t)1; + static const size_t tidsz_hex = sizeof(lwpid_t) * 2; + size_t tds_sent; + + if (*thr_iter == NULL) { + gdb_tx_err(ENXIO); + return; + } + + if (*thr_iter == done_sentinel) { + gdb_tx_begin('l'); + *thr_iter = NULL; + goto sendit; + } + + gdb_tx_begin('m'); + + for (tds_sent = 0; + *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1); + *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) { + if (tds_sent > 0) + gdb_tx_char(','); + gdb_tx_varhex((*thr_iter)->td_tid); + } + + /* + * Can't send EOF and "some" in same packet, so set a sentinel to send + * EOF when GDB asks us next. + */ + if (*thr_iter == NULL && tds_sent > 0) + *thr_iter = done_sentinel; + +sendit: + gdb_tx_end(); +} + static int gdb_trap(int type, int code) { jmp_buf jb; struct thread *thr_iter; void *prev_jb; prev_jb = kdb_jmpbuf(jb); if (setjmp(jb) != 0) { printf("%s bailing, hopefully back to ddb!\n", __func__); gdb_listening = 0; (void)kdb_jmpbuf(prev_jb); return (1); } gdb_listening = 0; /* * Send a T packet. We currently do not support watchpoints (the * awatch, rwatch or watch elements). */ gdb_tx_begin('T'); gdb_tx_hex(gdb_cpu_signal(type, code), 2); gdb_tx_varhex(GDB_REG_PC); gdb_tx_char(':'); gdb_tx_reg(GDB_REG_PC); gdb_tx_char(';'); gdb_tx_str("thread:"); gdb_tx_varhex((long)kdb_thread->td_tid); gdb_tx_char(';'); gdb_tx_end(); /* XXX check error condition. */ thr_iter = NULL; while (gdb_rx_begin() == 0) { /* printf("GDB: got '%s'\n", gdb_rxp); */ switch (gdb_rx_char()) { case '?': /* Last signal. */ gdb_tx_begin('S'); gdb_tx_hex(gdb_cpu_signal(type, code), 2); gdb_tx_end(); break; case 'c': { /* Continue. */ uintmax_t addr; register_t pc; if (!gdb_rx_varhex(&addr)) { pc = addr; gdb_cpu_setreg(GDB_REG_PC, &pc); } kdb_cpu_clear_singlestep(); gdb_listening = 1; return (1); } case 'C': { /* Continue with signal. */ uintmax_t addr, sig; register_t pc; if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && !gdb_rx_varhex(&addr)) { pc = addr; gdb_cpu_setreg(GDB_REG_PC, &pc); } kdb_cpu_clear_singlestep(); gdb_listening = 1; return (1); } case 'D': { /* Detach */ gdb_tx_ok(); kdb_cpu_clear_singlestep(); return (1); } case 'g': { /* Read registers. */ size_t r; gdb_tx_begin(0); for (r = 0; r < GDB_NREGS; r++) gdb_tx_reg(r); gdb_tx_end(); break; } case 'G': /* Write registers. */ gdb_tx_err(0); break; case 'H': { /* Set thread. */ intmax_t tid; struct thread *thr; gdb_rx_char(); if (gdb_rx_varhex(&tid)) { gdb_tx_err(EINVAL); break; } if (tid > 0) { thr = kdb_thr_lookup(tid); if (thr == NULL) { gdb_tx_err(ENOENT); break; } kdb_thr_select(thr); } gdb_tx_ok(); break; } case 'k': /* Kill request. */ kdb_cpu_clear_singlestep(); gdb_listening = 1; return (1); case 'm': { /* Read memory. */ uintmax_t addr, size; if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || gdb_rx_varhex(&size)) { gdb_tx_err(EINVAL); break; } gdb_tx_begin(0); if (gdb_tx_mem((char *)(uintptr_t)addr, size)) gdb_tx_end(); else gdb_tx_err(EIO); break; } case 'M': { /* Write memory. */ uintmax_t addr, size; if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || gdb_rx_varhex(&size) || gdb_rx_char() != ':') { gdb_tx_err(EINVAL); break; } if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) gdb_tx_err(EIO); else gdb_tx_ok(); break; } case 'P': { /* Write register. */ char *val; uintmax_t reg; val = gdb_rxp; if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { gdb_tx_err(EINVAL); break; } gdb_cpu_setreg(reg, val); gdb_tx_ok(); break; } case 'q': /* General query. */ if (gdb_rx_equal("fThreadInfo")) { thr_iter = kdb_thr_first(); - gdb_tx_begin('m'); - gdb_tx_hex((long)thr_iter->td_tid, 8); - gdb_tx_end(); + gdb_do_threadinfo(&thr_iter); } else if (gdb_rx_equal("sThreadInfo")) { - if (thr_iter == NULL) { - gdb_tx_err(ENXIO); - break; - } - thr_iter = kdb_thr_next(thr_iter); - if (thr_iter != NULL) { - gdb_tx_begin('m'); - gdb_tx_hex((long)thr_iter->td_tid, 8); - gdb_tx_end(); - } else { - gdb_tx_begin('l'); - gdb_tx_end(); - } + gdb_do_threadinfo(&thr_iter); } else if (gdb_rx_equal("Search:memory:")) { gdb_do_mem_search(); } else if (!gdb_cpu_query()) gdb_tx_empty(); break; case 's': { /* Step. */ uintmax_t addr; register_t pc; if (!gdb_rx_varhex(&addr)) { pc = addr; gdb_cpu_setreg(GDB_REG_PC, &pc); } kdb_cpu_set_singlestep(); gdb_listening = 1; return (1); } case 'S': { /* Step with signal. */ uintmax_t addr, sig; register_t pc; if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && !gdb_rx_varhex(&addr)) { pc = addr; gdb_cpu_setreg(GDB_REG_PC, &pc); } kdb_cpu_set_singlestep(); gdb_listening = 1; return (1); } case 'T': { /* Thread alive. */ intmax_t tid; if (gdb_rx_varhex(&tid)) { gdb_tx_err(EINVAL); break; } if (kdb_thr_lookup(tid) != NULL) gdb_tx_ok(); else gdb_tx_err(ENOENT); break; } case -1: /* Empty command. Treat as unknown command. */ /* FALLTHROUGH */ default: /* Unknown command. Send empty response. */ gdb_tx_empty(); break; } } (void)kdb_jmpbuf(prev_jb); return (0); } Index: head/sys/gdb/gdb_packet.c =================================================================== --- head/sys/gdb/gdb_packet.c (revision 351237) +++ head/sys/gdb/gdb_packet.c (revision 351238) @@ -1,371 +1,377 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include static char gdb_rxbuf[GDB_BUFSZ]; char *gdb_rxp = NULL; size_t gdb_rxsz = 0; static char gdb_txbuf[GDB_BUFSZ]; char *gdb_txp = NULL; /* Used in inline functions. */ #define C2N(c) (((c) < 'A') ? (c) - '0' : \ 10 + (((c) < 'a') ? (c) - 'A' : (c) - 'a')) #define N2C(n) (((n) < 10) ? (n) + '0' : (n) + 'a' - 10) /* * Get a single character */ static int gdb_getc(void) { int c; do c = gdb_cur->gdb_getc(); while (c == -1); if (c == CTRL('C')) { printf("Received ^C; trying to switch back to ddb.\n"); if (kdb_dbbe_select("ddb") != 0) printf("The ddb backend could not be selected.\n"); else { printf("using longjmp, hope it works!\n"); kdb_reenter(); } } return (c); } /* * Functions to receive and extract from a packet. */ int gdb_rx_begin(void) { int c, cksum; gdb_rxp = NULL; do { /* * Wait for the start character, ignore all others. * XXX needs a timeout. */ while ((c = gdb_getc()) != '$') ; /* Read until a # or end of buffer is found. */ cksum = 0; gdb_rxsz = 0; while (gdb_rxsz < sizeof(gdb_rxbuf) - 1) { c = gdb_getc(); if (c == '#') break; gdb_rxbuf[gdb_rxsz++] = c; cksum += c; } gdb_rxbuf[gdb_rxsz] = 0; cksum &= 0xff; /* Bail out on a buffer overflow. */ if (c != '#') { gdb_cur->gdb_putc('-'); return (ENOSPC); } c = gdb_getc(); cksum -= (C2N(c) << 4) & 0xf0; c = gdb_getc(); cksum -= C2N(c) & 0x0f; gdb_cur->gdb_putc((cksum == 0) ? '+' : '-'); if (cksum != 0) printf("GDB: packet `%s' has invalid checksum\n", gdb_rxbuf); } while (cksum != 0); gdb_rxp = gdb_rxbuf; return (0); } int gdb_rx_equal(const char *str) { int len; len = strlen(str); if (len > gdb_rxsz || strncmp(str, gdb_rxp, len) != 0) return (0); gdb_rxp += len; gdb_rxsz -= len; return (1); } int gdb_rx_mem(unsigned char *addr, size_t size) { unsigned char *p; void *prev; void *wctx; jmp_buf jb; size_t cnt; int ret; unsigned char c; if (size * 2 != gdb_rxsz) return (-1); wctx = gdb_begin_write(); prev = kdb_jmpbuf(jb); ret = setjmp(jb); if (ret == 0) { p = addr; cnt = size; while (cnt-- > 0) { c = (C2N(gdb_rxp[0]) << 4) & 0xf0; c |= C2N(gdb_rxp[1]) & 0x0f; *p++ = c; gdb_rxsz -= 2; gdb_rxp += 2; } kdb_cpu_sync_icache(addr, size); } (void)kdb_jmpbuf(prev); gdb_end_write(wctx); return ((ret == 0) ? 1 : 0); } int gdb_rx_varhex(uintmax_t *vp) { uintmax_t v; int c, neg; c = gdb_rx_char(); neg = (c == '-') ? 1 : 0; if (neg == 1) c = gdb_rx_char(); if (!isxdigit(c)) { gdb_rxp -= ((c == -1) ? 0 : 1) + neg; gdb_rxsz += ((c == -1) ? 0 : 1) + neg; return (-1); } v = 0; do { v <<= 4; v += C2N(c); c = gdb_rx_char(); } while (isxdigit(c)); if (c != -1) { gdb_rxp--; gdb_rxsz++; } *vp = (neg) ? -v : v; return (0); } /* * Function to build and send a package. */ void gdb_tx_begin(char tp) { gdb_txp = gdb_txbuf; if (tp != '\0') gdb_tx_char(tp); } int gdb_tx_end(void) { const char *p; int runlen; unsigned char c, cksum; do { gdb_cur->gdb_putc('$'); cksum = 0; p = gdb_txbuf; while (p < gdb_txp) { /* Send a character and start run-length encoding. */ c = *p++; gdb_cur->gdb_putc(c); cksum += c; runlen = 0; /* Determine run-length and update checksum. */ while (p < gdb_txp && *p == c) { runlen++; p++; } /* Emit the run-length encoded string. */ while (runlen >= 97) { gdb_cur->gdb_putc('*'); cksum += '*'; gdb_cur->gdb_putc(97+29); cksum += 97+29; runlen -= 97; if (runlen > 0) { gdb_cur->gdb_putc(c); cksum += c; runlen--; } } if (runlen == 1) { gdb_cur->gdb_putc(c); cksum += c; runlen--; } if (runlen == 0) continue; /* Don't emit '$', '#', '+' or '-'. */ if (runlen == 7) { gdb_cur->gdb_putc(c); cksum += c; runlen--; } if (runlen == 6 || runlen == 14 || runlen == 16) { gdb_cur->gdb_putc(c); cksum += c; runlen--; } gdb_cur->gdb_putc('*'); cksum += '*'; gdb_cur->gdb_putc(runlen+29); cksum += runlen+29; } gdb_cur->gdb_putc('#'); c = cksum >> 4; gdb_cur->gdb_putc(N2C(c)); c = cksum & 0x0f; gdb_cur->gdb_putc(N2C(c)); c = gdb_getc(); } while (c != '+'); return (0); } int gdb_tx_mem(const unsigned char *addr, size_t size) { void *prev; jmp_buf jb; int ret; prev = kdb_jmpbuf(jb); ret = setjmp(jb); if (ret == 0) { while (size-- > 0) { *gdb_txp++ = N2C(*addr >> 4); *gdb_txp++ = N2C(*addr & 0x0f); addr++; } } (void)kdb_jmpbuf(prev); return ((ret == 0) ? 1 : 0); } void gdb_tx_reg(int regnum) { unsigned char *regp; size_t regsz; regp = gdb_cpu_getreg(regnum, ®sz); if (regp == NULL) { /* Register unavailable. */ while (regsz--) { gdb_tx_char('x'); gdb_tx_char('x'); } } else gdb_tx_mem(regp, regsz); } +bool +gdb_txbuf_has_capacity(size_t req) +{ + return (((char *)gdb_txbuf + sizeof(gdb_txbuf) - gdb_txp) >= req); +} + /* Read binary data up until the end of the packet or until we have datalen decoded bytes */ int gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt) { int c; *amt = 0; while (*amt < datalen) { c = gdb_rx_char(); /* End of packet? */ if (c == -1) break; /* Escaped character up next */ if (c == '}') { /* Truncated packet? Bail out */ if ((c = gdb_rx_char()) == -1) return (1); c ^= 0x20; } *(data++) = c & 0xff; (*amt)++; } return (0); } int gdb_search_mem(const unsigned char *addr, size_t size, const unsigned char *pat, size_t patlen, const unsigned char **found) { void *prev; jmp_buf jb; int ret; prev = kdb_jmpbuf(jb); ret = setjmp(jb); if (ret == 0) *found = memmem(addr, size, pat, patlen); (void)kdb_jmpbuf(prev); return ((ret == 0) ? 1 : 0); }