1fa521b03SWarner Losh /*- 2497b6b2aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3497b6b2aSPedro F. Giffuni * 472d44f31SMarcel Moolenaar * Copyright (c) 2004 Marcel Moolenaar 572d44f31SMarcel Moolenaar * All rights reserved. 672d44f31SMarcel Moolenaar * 772d44f31SMarcel Moolenaar * Redistribution and use in source and binary forms, with or without 872d44f31SMarcel Moolenaar * modification, are permitted provided that the following conditions 972d44f31SMarcel Moolenaar * are met: 1072d44f31SMarcel Moolenaar * 1172d44f31SMarcel Moolenaar * 1. Redistributions of source code must retain the above copyright 1272d44f31SMarcel Moolenaar * notice, this list of conditions and the following disclaimer. 1372d44f31SMarcel Moolenaar * 2. Redistributions in binary form must reproduce the above copyright 1472d44f31SMarcel Moolenaar * notice, this list of conditions and the following disclaimer in the 1572d44f31SMarcel Moolenaar * documentation and/or other materials provided with the distribution. 1672d44f31SMarcel Moolenaar * 1772d44f31SMarcel Moolenaar * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 1872d44f31SMarcel Moolenaar * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1972d44f31SMarcel Moolenaar * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2072d44f31SMarcel Moolenaar * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 2172d44f31SMarcel Moolenaar * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2272d44f31SMarcel Moolenaar * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2372d44f31SMarcel Moolenaar * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2472d44f31SMarcel Moolenaar * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2572d44f31SMarcel Moolenaar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2672d44f31SMarcel Moolenaar * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2772d44f31SMarcel Moolenaar */ 2872d44f31SMarcel Moolenaar 2972d44f31SMarcel Moolenaar #include <sys/cdefs.h> 3072d44f31SMarcel Moolenaar __FBSDID("$FreeBSD$"); 3172d44f31SMarcel Moolenaar 3272d44f31SMarcel Moolenaar #include <sys/param.h> 3372d44f31SMarcel Moolenaar #include <sys/systm.h> 3472d44f31SMarcel Moolenaar #include <sys/kdb.h> 3572d44f31SMarcel Moolenaar #include <sys/kernel.h> 3672d44f31SMarcel Moolenaar #include <sys/pcpu.h> 3772d44f31SMarcel Moolenaar #include <sys/proc.h> 3872d44f31SMarcel Moolenaar #include <sys/reboot.h> 395555afa1SConrad Meyer #include <sys/sbuf.h> 4072d44f31SMarcel Moolenaar 4172d44f31SMarcel Moolenaar #include <machine/gdb_machdep.h> 4272d44f31SMarcel Moolenaar #include <machine/kdb.h> 4372d44f31SMarcel Moolenaar 4472d44f31SMarcel Moolenaar #include <gdb/gdb.h> 4572d44f31SMarcel Moolenaar #include <gdb/gdb_int.h> 4672d44f31SMarcel Moolenaar 47*10f6c05cSConrad Meyer SYSCTL_NODE(_debug, OID_AUTO, gdb, CTLFLAG_RW, 0, "GDB settings"); 48*10f6c05cSConrad Meyer 4972d44f31SMarcel Moolenaar static dbbe_init_f gdb_init; 5072d44f31SMarcel Moolenaar static dbbe_trap_f gdb_trap; 5172d44f31SMarcel Moolenaar 5228926c57SJohn Baldwin KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap); 5372d44f31SMarcel Moolenaar 54e8d86c0eSPoul-Henning Kamp static struct gdb_dbgport null_gdb_dbgport; 55e8d86c0eSPoul-Henning Kamp DATA_SET(gdb_dbgport_set, null_gdb_dbgport); 5672d44f31SMarcel Moolenaar SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 5772d44f31SMarcel Moolenaar 5872d44f31SMarcel Moolenaar struct gdb_dbgport *gdb_cur = NULL; 597d0c6c9fSSam Leffler int gdb_listening = 0; 6072d44f31SMarcel Moolenaar 6127ecc2adSBenno Rice static unsigned char gdb_bindata[64]; 6227ecc2adSBenno Rice 6372d44f31SMarcel Moolenaar static int 6472d44f31SMarcel Moolenaar gdb_init(void) 6572d44f31SMarcel Moolenaar { 6672d44f31SMarcel Moolenaar struct gdb_dbgport *dp, **iter; 6772d44f31SMarcel Moolenaar int cur_pri, pri; 6872d44f31SMarcel Moolenaar 6972d44f31SMarcel Moolenaar gdb_cur = NULL; 7072d44f31SMarcel Moolenaar cur_pri = -1; 7172d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 7272d44f31SMarcel Moolenaar dp = *iter; 7372d44f31SMarcel Moolenaar pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 7472d44f31SMarcel Moolenaar dp->gdb_active = (pri >= 0) ? 0 : -1; 7572d44f31SMarcel Moolenaar if (pri > cur_pri) { 7672d44f31SMarcel Moolenaar cur_pri = pri; 7772d44f31SMarcel Moolenaar gdb_cur = dp; 7872d44f31SMarcel Moolenaar } 7972d44f31SMarcel Moolenaar } 8072d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 8172d44f31SMarcel Moolenaar printf("GDB: debug ports:"); 8272d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 8372d44f31SMarcel Moolenaar dp = *iter; 8472d44f31SMarcel Moolenaar if (dp->gdb_active == 0) 8572d44f31SMarcel Moolenaar printf(" %s", dp->gdb_name); 8672d44f31SMarcel Moolenaar } 8772d44f31SMarcel Moolenaar printf("\n"); 8872d44f31SMarcel Moolenaar } else 8972d44f31SMarcel Moolenaar printf("GDB: no debug ports present\n"); 9072d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 9172d44f31SMarcel Moolenaar gdb_cur->gdb_init(); 9272d44f31SMarcel Moolenaar printf("GDB: current port: %s\n", gdb_cur->gdb_name); 9372d44f31SMarcel Moolenaar } 947d0c6c9fSSam Leffler if (gdb_cur != NULL) { 9572d44f31SMarcel Moolenaar cur_pri = (boothowto & RB_GDB) ? 2 : 0; 967d0c6c9fSSam Leffler gdb_consinit(); 977d0c6c9fSSam Leffler } else 9872d44f31SMarcel Moolenaar cur_pri = -1; 9972d44f31SMarcel Moolenaar return (cur_pri); 10072d44f31SMarcel Moolenaar } 10172d44f31SMarcel Moolenaar 1028a7a6571SRyan Libby static void 1038a7a6571SRyan Libby gdb_do_mem_search(void) 1048a7a6571SRyan Libby { 1058a7a6571SRyan Libby size_t patlen; 1068a7a6571SRyan Libby intmax_t addr, size; 1078a7a6571SRyan Libby const unsigned char *found; 1088a7a6571SRyan Libby 1098a7a6571SRyan Libby if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' || 1108a7a6571SRyan Libby gdb_rx_varhex(&size) || gdb_rx_char() != ';' || 1118a7a6571SRyan Libby gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) { 1128a7a6571SRyan Libby gdb_tx_err(EINVAL); 1138a7a6571SRyan Libby return; 1148a7a6571SRyan Libby } 1158a7a6571SRyan Libby if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, 1168a7a6571SRyan Libby patlen, &found)) { 1178a7a6571SRyan Libby if (found == 0ULL) 1188a7a6571SRyan Libby gdb_tx_begin('0'); 1198a7a6571SRyan Libby else { 1208a7a6571SRyan Libby gdb_tx_begin('1'); 1218a7a6571SRyan Libby gdb_tx_char(','); 1228a7a6571SRyan Libby gdb_tx_hex((intmax_t)(uintptr_t)found, 8); 1238a7a6571SRyan Libby } 1248a7a6571SRyan Libby gdb_tx_end(); 1258a7a6571SRyan Libby } else 1268a7a6571SRyan Libby gdb_tx_err(EIO); 1278a7a6571SRyan Libby } 1288a7a6571SRyan Libby 129130ef1adSConrad Meyer static void 130130ef1adSConrad Meyer gdb_do_threadinfo(struct thread **thr_iter) 131130ef1adSConrad Meyer { 132130ef1adSConrad Meyer static struct thread * const done_sentinel = (void *)(uintptr_t)1; 133130ef1adSConrad Meyer static const size_t tidsz_hex = sizeof(lwpid_t) * 2; 134130ef1adSConrad Meyer size_t tds_sent; 135130ef1adSConrad Meyer 136130ef1adSConrad Meyer if (*thr_iter == NULL) { 137130ef1adSConrad Meyer gdb_tx_err(ENXIO); 138130ef1adSConrad Meyer return; 139130ef1adSConrad Meyer } 140130ef1adSConrad Meyer 141130ef1adSConrad Meyer if (*thr_iter == done_sentinel) { 142130ef1adSConrad Meyer gdb_tx_begin('l'); 143130ef1adSConrad Meyer *thr_iter = NULL; 144130ef1adSConrad Meyer goto sendit; 145130ef1adSConrad Meyer } 146130ef1adSConrad Meyer 147130ef1adSConrad Meyer gdb_tx_begin('m'); 148130ef1adSConrad Meyer 149130ef1adSConrad Meyer for (tds_sent = 0; 150130ef1adSConrad Meyer *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1); 151130ef1adSConrad Meyer *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) { 152130ef1adSConrad Meyer if (tds_sent > 0) 153130ef1adSConrad Meyer gdb_tx_char(','); 154130ef1adSConrad Meyer gdb_tx_varhex((*thr_iter)->td_tid); 155130ef1adSConrad Meyer } 156130ef1adSConrad Meyer 157130ef1adSConrad Meyer /* 158130ef1adSConrad Meyer * Can't send EOF and "some" in same packet, so set a sentinel to send 159130ef1adSConrad Meyer * EOF when GDB asks us next. 160130ef1adSConrad Meyer */ 161130ef1adSConrad Meyer if (*thr_iter == NULL && tds_sent > 0) 162130ef1adSConrad Meyer *thr_iter = done_sentinel; 163130ef1adSConrad Meyer 164130ef1adSConrad Meyer sendit: 165130ef1adSConrad Meyer gdb_tx_end(); 166130ef1adSConrad Meyer } 167130ef1adSConrad Meyer 168c4fbbfaaSConrad Meyer #define BIT(n) (1ull << (n)) 169c4fbbfaaSConrad Meyer enum { 170c4fbbfaaSConrad Meyer GDB_MULTIPROCESS, 171c4fbbfaaSConrad Meyer GDB_SWBREAK, 172c4fbbfaaSConrad Meyer GDB_HWBREAK, 173c4fbbfaaSConrad Meyer GDB_QRELOCINSN, 174c4fbbfaaSConrad Meyer GDB_FORK_EVENTS, 175c4fbbfaaSConrad Meyer GDB_VFORK_EVENTS, 176c4fbbfaaSConrad Meyer GDB_EXEC_EVENTS, 177c4fbbfaaSConrad Meyer GDB_VCONT_SUPPORTED, 178c4fbbfaaSConrad Meyer GDB_QTHREADEVENTS, 179c4fbbfaaSConrad Meyer GDB_NO_RESUMED, 180c4fbbfaaSConrad Meyer }; 181c4fbbfaaSConrad Meyer static const char * const gdb_feature_names[] = { 182c4fbbfaaSConrad Meyer [GDB_MULTIPROCESS] = "multiprocess", 183c4fbbfaaSConrad Meyer [GDB_SWBREAK] = "swbreak", 184c4fbbfaaSConrad Meyer [GDB_HWBREAK] = "hwbreak", 185c4fbbfaaSConrad Meyer [GDB_QRELOCINSN] = "qRelocInsn", 186c4fbbfaaSConrad Meyer [GDB_FORK_EVENTS] = "fork-events", 187c4fbbfaaSConrad Meyer [GDB_VFORK_EVENTS] = "vfork-events", 188c4fbbfaaSConrad Meyer [GDB_EXEC_EVENTS] = "exec-events", 189c4fbbfaaSConrad Meyer [GDB_VCONT_SUPPORTED] = "vContSupported", 190c4fbbfaaSConrad Meyer [GDB_QTHREADEVENTS] = "QThreadEvents", 191c4fbbfaaSConrad Meyer [GDB_NO_RESUMED] = "no-resumed", 192c4fbbfaaSConrad Meyer }; 193c4fbbfaaSConrad Meyer static void 194c4fbbfaaSConrad Meyer gdb_do_qsupported(uint32_t *feat) 195c4fbbfaaSConrad Meyer { 196c4fbbfaaSConrad Meyer char *tok, *delim, ok; 197c4fbbfaaSConrad Meyer size_t i, toklen; 198c4fbbfaaSConrad Meyer 199c4fbbfaaSConrad Meyer /* Parse supported host features */ 200c4fbbfaaSConrad Meyer *feat = 0; 201c4fbbfaaSConrad Meyer if (gdb_rx_char() != ':') 202c4fbbfaaSConrad Meyer goto error; 203c4fbbfaaSConrad Meyer 204c4fbbfaaSConrad Meyer while (gdb_rxsz > 0) { 205c4fbbfaaSConrad Meyer tok = gdb_rxp; 206c4fbbfaaSConrad Meyer delim = strchrnul(gdb_rxp, ';'); 207c4fbbfaaSConrad Meyer toklen = (delim - tok); 208c4fbbfaaSConrad Meyer 209c4fbbfaaSConrad Meyer gdb_rxp += toklen; 210c4fbbfaaSConrad Meyer gdb_rxsz -= toklen; 211c4fbbfaaSConrad Meyer if (*delim != '\0') { 212c4fbbfaaSConrad Meyer *delim = '\0'; 213c4fbbfaaSConrad Meyer gdb_rxp += 1; 214c4fbbfaaSConrad Meyer gdb_rxsz -= 1; 215c4fbbfaaSConrad Meyer } 216c4fbbfaaSConrad Meyer 217c4fbbfaaSConrad Meyer if (toklen < 2) 218c4fbbfaaSConrad Meyer goto error; 219c4fbbfaaSConrad Meyer 220c4fbbfaaSConrad Meyer ok = tok[toklen - 1]; 221c4fbbfaaSConrad Meyer if (ok != '-' && ok != '+') { 222c4fbbfaaSConrad Meyer /* 223c4fbbfaaSConrad Meyer * GDB only has one KV-pair feature, and we don't 224c4fbbfaaSConrad Meyer * support it, so ignore and move on. 225c4fbbfaaSConrad Meyer */ 226c4fbbfaaSConrad Meyer if (strchr(tok, '=') != NULL) 227c4fbbfaaSConrad Meyer continue; 228c4fbbfaaSConrad Meyer /* Not a KV-pair, and not a +/- flag? Malformed. */ 229c4fbbfaaSConrad Meyer goto error; 230c4fbbfaaSConrad Meyer } 231c4fbbfaaSConrad Meyer if (ok != '+') 232c4fbbfaaSConrad Meyer continue; 233c4fbbfaaSConrad Meyer tok[toklen - 1] = '\0'; 234c4fbbfaaSConrad Meyer 235c4fbbfaaSConrad Meyer for (i = 0; i < nitems(gdb_feature_names); i++) 236c4fbbfaaSConrad Meyer if (strcmp(gdb_feature_names[i], tok) == 0) 237c4fbbfaaSConrad Meyer break; 238c4fbbfaaSConrad Meyer 239c4fbbfaaSConrad Meyer if (i == nitems(gdb_feature_names)) { 240c4fbbfaaSConrad Meyer /* Unknown GDB feature. */ 241c4fbbfaaSConrad Meyer continue; 242c4fbbfaaSConrad Meyer } 243c4fbbfaaSConrad Meyer 244c4fbbfaaSConrad Meyer *feat |= BIT(i); 245c4fbbfaaSConrad Meyer } 246c4fbbfaaSConrad Meyer 247c4fbbfaaSConrad Meyer /* Send a supported feature list back */ 248c4fbbfaaSConrad Meyer gdb_tx_begin(0); 249c4fbbfaaSConrad Meyer 250c4fbbfaaSConrad Meyer gdb_tx_str("PacketSize"); 251c4fbbfaaSConrad Meyer gdb_tx_char('='); 252c4fbbfaaSConrad Meyer /* 253c4fbbfaaSConrad Meyer * We don't buffer framing bytes, but we do need to retain a byte for a 254c4fbbfaaSConrad Meyer * trailing nul. 255c4fbbfaaSConrad Meyer */ 256c4fbbfaaSConrad Meyer gdb_tx_varhex(GDB_BUFSZ + strlen("$#nn") - 1); 257c4fbbfaaSConrad Meyer 2585555afa1SConrad Meyer gdb_tx_str(";qXfer:threads:read+"); 2595555afa1SConrad Meyer 260c4fbbfaaSConrad Meyer /* 261c4fbbfaaSConrad Meyer * Future consideration: 262c4fbbfaaSConrad Meyer * - vCont 263c4fbbfaaSConrad Meyer * - multiprocess 264c4fbbfaaSConrad Meyer */ 265c4fbbfaaSConrad Meyer gdb_tx_end(); 266c4fbbfaaSConrad Meyer return; 267c4fbbfaaSConrad Meyer 268c4fbbfaaSConrad Meyer error: 269c4fbbfaaSConrad Meyer *feat = 0; 270c4fbbfaaSConrad Meyer gdb_tx_err(EINVAL); 271c4fbbfaaSConrad Meyer } 272c4fbbfaaSConrad Meyer 2735555afa1SConrad Meyer /* 2745555afa1SConrad Meyer * A qXfer_context provides a vaguely generic way to generate a multi-packet 2755555afa1SConrad Meyer * response on the fly, making some assumptions about the size of sbuf writes 2765555afa1SConrad Meyer * vs actual packet length constraints. A non-byzantine gdb host should allow 2775555afa1SConrad Meyer * hundreds of bytes per packet or more. 2785555afa1SConrad Meyer * 2795555afa1SConrad Meyer * Upper layers are considered responsible for escaping the four forbidden 2805555afa1SConrad Meyer * characters '# $ } *'. 2815555afa1SConrad Meyer */ 2825555afa1SConrad Meyer struct qXfer_context { 2835555afa1SConrad Meyer struct sbuf sb; 2845555afa1SConrad Meyer size_t last_offset; 2855555afa1SConrad Meyer bool flushed; 2865555afa1SConrad Meyer bool lastmessage; 2875555afa1SConrad Meyer char xfer_buf[GDB_BUFSZ]; 2885555afa1SConrad Meyer }; 2895555afa1SConrad Meyer 2905555afa1SConrad Meyer static int 2915555afa1SConrad Meyer qXfer_drain(void *v, const char *buf, int len) 2925555afa1SConrad Meyer { 2935555afa1SConrad Meyer struct qXfer_context *qx; 2945555afa1SConrad Meyer 2955555afa1SConrad Meyer if (len < 0) 2965555afa1SConrad Meyer return (-EINVAL); 2975555afa1SConrad Meyer 2985555afa1SConrad Meyer qx = v; 2995555afa1SConrad Meyer if (qx->flushed) { 3005555afa1SConrad Meyer /* 3015555afa1SConrad Meyer * Overflow. We lost some message. Maybe the packet size is 3025555afa1SConrad Meyer * ridiculously small. 3035555afa1SConrad Meyer */ 3045555afa1SConrad Meyer printf("%s: Overflow in qXfer detected.\n", __func__); 3055555afa1SConrad Meyer return (-ENOBUFS); 3065555afa1SConrad Meyer } 3075555afa1SConrad Meyer 3085555afa1SConrad Meyer qx->last_offset += len; 3095555afa1SConrad Meyer qx->flushed = true; 3105555afa1SConrad Meyer 3115555afa1SConrad Meyer if (qx->lastmessage) 3125555afa1SConrad Meyer gdb_tx_begin('l'); 3135555afa1SConrad Meyer else 3145555afa1SConrad Meyer gdb_tx_begin('m'); 3155555afa1SConrad Meyer 3165555afa1SConrad Meyer memcpy(gdb_txp, buf, len); 3175555afa1SConrad Meyer gdb_txp += len; 3185555afa1SConrad Meyer 3195555afa1SConrad Meyer gdb_tx_end(); 3205555afa1SConrad Meyer return (len); 3215555afa1SConrad Meyer } 3225555afa1SConrad Meyer 3235555afa1SConrad Meyer static int 3245555afa1SConrad Meyer init_qXfer_ctx(struct qXfer_context *qx, uintmax_t len) 3255555afa1SConrad Meyer { 3265555afa1SConrad Meyer 3275555afa1SConrad Meyer /* Protocol (max) length field includes framing overhead. */ 3285555afa1SConrad Meyer if (len < sizeof("$m#nn")) 3295555afa1SConrad Meyer return (ENOSPC); 3305555afa1SConrad Meyer 3315555afa1SConrad Meyer len -= 4; 3325555afa1SConrad Meyer len = ummin(len, GDB_BUFSZ - 1); 3335555afa1SConrad Meyer 3345555afa1SConrad Meyer qx->last_offset = 0; 3355555afa1SConrad Meyer qx->flushed = false; 3365555afa1SConrad Meyer qx->lastmessage = false; 3375555afa1SConrad Meyer sbuf_new(&qx->sb, qx->xfer_buf, len, SBUF_FIXEDLEN); 3385555afa1SConrad Meyer sbuf_set_drain(&qx->sb, qXfer_drain, qx); 3395555afa1SConrad Meyer return (0); 3405555afa1SConrad Meyer } 3415555afa1SConrad Meyer 3425555afa1SConrad Meyer /* 3435555afa1SConrad Meyer * dst must be 2x strlen(max_src) + 1. 3445555afa1SConrad Meyer * 3455555afa1SConrad Meyer * Squashes invalid XML characters down to _. Sorry. Then escapes for GDB. 3465555afa1SConrad Meyer */ 3475555afa1SConrad Meyer static void 3485555afa1SConrad Meyer qXfer_escape_xmlattr_str(char *dst, size_t dstlen, const char *src) 3495555afa1SConrad Meyer { 3505555afa1SConrad Meyer static const char *forbidden = "#$}*"; 3515555afa1SConrad Meyer 3525555afa1SConrad Meyer size_t i; 3535555afa1SConrad Meyer char c; 3545555afa1SConrad Meyer 3555555afa1SConrad Meyer for (i = 0; i < dstlen - 1 && *src != 0; src++, i++) { 3565555afa1SConrad Meyer c = *src; 3575555afa1SConrad Meyer /* XML attr filter */ 3585555afa1SConrad Meyer if (c < 32) 3595555afa1SConrad Meyer c = '_'; 3605555afa1SConrad Meyer /* We assume attributes will be "" quoted. */ 3615555afa1SConrad Meyer if (c == '<' || c == '&' || c == '"') 3625555afa1SConrad Meyer c = '_'; 3635555afa1SConrad Meyer 3645555afa1SConrad Meyer /* GDB escape. */ 3655555afa1SConrad Meyer if (strchr(forbidden, c) != NULL) { 3665555afa1SConrad Meyer *dst++ = '}'; 3675555afa1SConrad Meyer c ^= 0x20; 3685555afa1SConrad Meyer } 3695555afa1SConrad Meyer *dst++ = c; 3705555afa1SConrad Meyer } 3715555afa1SConrad Meyer if (*src != 0) 3725555afa1SConrad Meyer printf("XXX%s: overflow; API misuse\n", __func__); 3735555afa1SConrad Meyer 3745555afa1SConrad Meyer *dst = 0; 3755555afa1SConrad Meyer } 3765555afa1SConrad Meyer 3775555afa1SConrad Meyer /* 3785555afa1SConrad Meyer * Dynamically generate qXfer:threads document, one packet at a time. 3795555afa1SConrad Meyer * 3805555afa1SConrad Meyer * The format is loosely described[0], although it does not seem that the 3815555afa1SConrad Meyer * <?xml?> mentioned on that page is required. 3825555afa1SConrad Meyer * 3835555afa1SConrad Meyer * [0]: https://sourceware.org/gdb/current/onlinedocs/gdb/Thread-List-Format.html 3845555afa1SConrad Meyer */ 3855555afa1SConrad Meyer static void 3865555afa1SConrad Meyer do_qXfer_threads_read(void) 3875555afa1SConrad Meyer { 3885555afa1SConrad Meyer /* Kludgy context */ 3895555afa1SConrad Meyer static struct { 3905555afa1SConrad Meyer struct qXfer_context qXfer; 3915555afa1SConrad Meyer /* Kludgy state machine */ 3925555afa1SConrad Meyer struct thread *iter; 3935555afa1SConrad Meyer enum { 3945555afa1SConrad Meyer XML_START_THREAD, /* '<thread' */ 3955555afa1SConrad Meyer XML_THREAD_ID, /* ' id="xxx"' */ 3965555afa1SConrad Meyer XML_THREAD_CORE, /* ' core="yyy"' */ 3975555afa1SConrad Meyer XML_THREAD_NAME, /* ' name="zzz"' */ 3985555afa1SConrad Meyer XML_THREAD_EXTRA, /* '> ...' */ 3995555afa1SConrad Meyer XML_END_THREAD, /* '</thread>' */ 4005555afa1SConrad Meyer XML_SENT_END_THREADS, /* '</threads>' */ 4015555afa1SConrad Meyer } next_step; 4025555afa1SConrad Meyer } ctx; 4035555afa1SConrad Meyer static char td_name_escape[MAXCOMLEN * 2 + 1]; 4045555afa1SConrad Meyer 4055555afa1SConrad Meyer const char *name_src; 4065555afa1SConrad Meyer uintmax_t offset, len; 4075555afa1SConrad Meyer int error; 4085555afa1SConrad Meyer 4095555afa1SConrad Meyer /* Annex part must be empty. */ 4105555afa1SConrad Meyer if (gdb_rx_char() != ':') 4115555afa1SConrad Meyer goto misformed_request; 4125555afa1SConrad Meyer 4135555afa1SConrad Meyer if (gdb_rx_varhex(&offset) != 0 || 4145555afa1SConrad Meyer gdb_rx_char() != ',' || 4155555afa1SConrad Meyer gdb_rx_varhex(&len) != 0) 4165555afa1SConrad Meyer goto misformed_request; 4175555afa1SConrad Meyer 4185555afa1SConrad Meyer /* 4195555afa1SConrad Meyer * Validate resume xfers. 4205555afa1SConrad Meyer */ 4215555afa1SConrad Meyer if (offset != 0) { 4225555afa1SConrad Meyer if (offset != ctx.qXfer.last_offset) { 423acef7371SConrad Meyer printf("%s: Resumed offset %ju != expected %zu\n", 4245555afa1SConrad Meyer __func__, offset, ctx.qXfer.last_offset); 4255555afa1SConrad Meyer error = ESPIPE; 4265555afa1SConrad Meyer goto request_error; 4275555afa1SConrad Meyer } 4285555afa1SConrad Meyer ctx.qXfer.flushed = false; 4295555afa1SConrad Meyer } 4305555afa1SConrad Meyer 4315555afa1SConrad Meyer if (offset == 0) { 4325555afa1SConrad Meyer ctx.iter = kdb_thr_first(); 4335555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 4345555afa1SConrad Meyer error = init_qXfer_ctx(&ctx.qXfer, len); 4355555afa1SConrad Meyer if (error != 0) 4365555afa1SConrad Meyer goto request_error; 4375555afa1SConrad Meyer 4385555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<threads>"); 4395555afa1SConrad Meyer } 4405555afa1SConrad Meyer 4415555afa1SConrad Meyer while (!ctx.qXfer.flushed && ctx.iter != NULL) { 4425555afa1SConrad Meyer switch (ctx.next_step) { 4435555afa1SConrad Meyer case XML_START_THREAD: 4445555afa1SConrad Meyer ctx.next_step = XML_THREAD_ID; 4455555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<thread"); 4465555afa1SConrad Meyer continue; 4475555afa1SConrad Meyer 4485555afa1SConrad Meyer case XML_THREAD_ID: 4495555afa1SConrad Meyer ctx.next_step = XML_THREAD_CORE; 4505555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " id=\"%jx\"", 4515555afa1SConrad Meyer (uintmax_t)ctx.iter->td_tid); 4525555afa1SConrad Meyer continue; 4535555afa1SConrad Meyer 4545555afa1SConrad Meyer case XML_THREAD_CORE: 4555555afa1SConrad Meyer ctx.next_step = XML_THREAD_NAME; 4565555afa1SConrad Meyer if (ctx.iter->td_oncpu != NOCPU) { 4575555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " core=\"%d\"", 4585555afa1SConrad Meyer ctx.iter->td_oncpu); 4595555afa1SConrad Meyer } 4605555afa1SConrad Meyer continue; 4615555afa1SConrad Meyer 4625555afa1SConrad Meyer case XML_THREAD_NAME: 4635555afa1SConrad Meyer ctx.next_step = XML_THREAD_EXTRA; 4645555afa1SConrad Meyer 4655555afa1SConrad Meyer if (ctx.iter->td_name[0] != 0) 4665555afa1SConrad Meyer name_src = ctx.iter->td_name; 4675555afa1SConrad Meyer else if (ctx.iter->td_proc != NULL && 4685555afa1SConrad Meyer ctx.iter->td_proc->p_comm[0] != 0) 4695555afa1SConrad Meyer name_src = ctx.iter->td_proc->p_comm; 4705555afa1SConrad Meyer else 4715555afa1SConrad Meyer continue; 4725555afa1SConrad Meyer 4735555afa1SConrad Meyer qXfer_escape_xmlattr_str(td_name_escape, 4745555afa1SConrad Meyer sizeof(td_name_escape), name_src); 4755555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " name=\"%s\"", 4765555afa1SConrad Meyer td_name_escape); 4775555afa1SConrad Meyer continue; 4785555afa1SConrad Meyer 4795555afa1SConrad Meyer case XML_THREAD_EXTRA: 4805555afa1SConrad Meyer ctx.next_step = XML_END_THREAD; 4815555afa1SConrad Meyer 4825555afa1SConrad Meyer sbuf_putc(&ctx.qXfer.sb, '>'); 4835555afa1SConrad Meyer 4845555afa1SConrad Meyer if (ctx.iter->td_state == TDS_RUNNING) 4855555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Running"); 4865555afa1SConrad Meyer else if (ctx.iter->td_state == TDS_RUNQ) 4875555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "RunQ"); 4885555afa1SConrad Meyer else if (ctx.iter->td_state == TDS_CAN_RUN) 4895555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "CanRun"); 4905555afa1SConrad Meyer else if (TD_ON_LOCK(ctx.iter)) 4915555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Blocked"); 4925555afa1SConrad Meyer else if (TD_IS_SLEEPING(ctx.iter)) 4935555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Sleeping"); 4945555afa1SConrad Meyer else if (TD_IS_SWAPPED(ctx.iter)) 4955555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Swapped"); 4965555afa1SConrad Meyer else if (TD_AWAITING_INTR(ctx.iter)) 4975555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "IthreadWait"); 4985555afa1SConrad Meyer else if (TD_IS_SUSPENDED(ctx.iter)) 4995555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Suspended"); 5005555afa1SConrad Meyer else 5015555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "???"); 5025555afa1SConrad Meyer continue; 5035555afa1SConrad Meyer 5045555afa1SConrad Meyer case XML_END_THREAD: 5055555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 5065555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</thread>"); 5075555afa1SConrad Meyer ctx.iter = kdb_thr_next(ctx.iter); 5085555afa1SConrad Meyer continue; 5095555afa1SConrad Meyer 5105555afa1SConrad Meyer /* 5115555afa1SConrad Meyer * This one isn't part of the looping state machine, 5125555afa1SConrad Meyer * but GCC complains if you leave an enum value out of the 5135555afa1SConrad Meyer * select. 5145555afa1SConrad Meyer */ 5155555afa1SConrad Meyer case XML_SENT_END_THREADS: 5165555afa1SConrad Meyer /* NOTREACHED */ 5175555afa1SConrad Meyer break; 5185555afa1SConrad Meyer } 5195555afa1SConrad Meyer } 5205555afa1SConrad Meyer if (ctx.qXfer.flushed) 5215555afa1SConrad Meyer return; 5225555afa1SConrad Meyer 5235555afa1SConrad Meyer if (ctx.next_step != XML_SENT_END_THREADS) { 5245555afa1SConrad Meyer ctx.next_step = XML_SENT_END_THREADS; 5255555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</threads>"); 5265555afa1SConrad Meyer } 5275555afa1SConrad Meyer if (ctx.qXfer.flushed) 5285555afa1SConrad Meyer return; 5295555afa1SConrad Meyer 5305555afa1SConrad Meyer ctx.qXfer.lastmessage = true; 5315555afa1SConrad Meyer sbuf_finish(&ctx.qXfer.sb); 5325555afa1SConrad Meyer sbuf_delete(&ctx.qXfer.sb); 5335555afa1SConrad Meyer ctx.qXfer.last_offset = 0; 5345555afa1SConrad Meyer return; 5355555afa1SConrad Meyer 5365555afa1SConrad Meyer misformed_request: 5375555afa1SConrad Meyer /* 5385555afa1SConrad Meyer * GDB "General-Query-Packets.html" qXfer-read anchor specifically 5395555afa1SConrad Meyer * documents an E00 code for malformed requests or invalid annex. 5405555afa1SConrad Meyer * Non-zero codes indicate invalid offset or "error reading the data." 5415555afa1SConrad Meyer */ 5425555afa1SConrad Meyer error = 0; 5435555afa1SConrad Meyer request_error: 5445555afa1SConrad Meyer gdb_tx_err(error); 5455555afa1SConrad Meyer return; 5465555afa1SConrad Meyer } 5475555afa1SConrad Meyer 5485555afa1SConrad Meyer /* 5495555afa1SConrad Meyer * A set of standardized transfers from "special data areas." 5505555afa1SConrad Meyer * 5515555afa1SConrad Meyer * We've already matched on "qXfer:" and advanced the rx packet buffer past 5525555afa1SConrad Meyer * that bit. Parse out the rest of the packet and generate an appropriate 5535555afa1SConrad Meyer * response. 5545555afa1SConrad Meyer */ 5555555afa1SConrad Meyer static void 5565555afa1SConrad Meyer do_qXfer(void) 5575555afa1SConrad Meyer { 5585555afa1SConrad Meyer if (!gdb_rx_equal("threads:")) 5595555afa1SConrad Meyer goto unrecognized; 5605555afa1SConrad Meyer 5615555afa1SConrad Meyer if (!gdb_rx_equal("read:")) 5625555afa1SConrad Meyer goto unrecognized; 5635555afa1SConrad Meyer 5645555afa1SConrad Meyer do_qXfer_threads_read(); 5655555afa1SConrad Meyer return; 5665555afa1SConrad Meyer 5675555afa1SConrad Meyer unrecognized: 5685555afa1SConrad Meyer gdb_tx_empty(); 5695555afa1SConrad Meyer return; 5705555afa1SConrad Meyer } 5715555afa1SConrad Meyer 57272d44f31SMarcel Moolenaar static int 57372d44f31SMarcel Moolenaar gdb_trap(int type, int code) 57472d44f31SMarcel Moolenaar { 5753a5d3671SMatthew D Fleming jmp_buf jb; 57672d44f31SMarcel Moolenaar struct thread *thr_iter; 5773a5d3671SMatthew D Fleming void *prev_jb; 578c4fbbfaaSConrad Meyer uint32_t host_features; 5793a5d3671SMatthew D Fleming 5803a5d3671SMatthew D Fleming prev_jb = kdb_jmpbuf(jb); 5813a5d3671SMatthew D Fleming if (setjmp(jb) != 0) { 5823a5d3671SMatthew D Fleming printf("%s bailing, hopefully back to ddb!\n", __func__); 5833a5d3671SMatthew D Fleming gdb_listening = 0; 5843a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 5853a5d3671SMatthew D Fleming return (1); 5863a5d3671SMatthew D Fleming } 58772d44f31SMarcel Moolenaar 5887d0c6c9fSSam Leffler gdb_listening = 0; 58972d44f31SMarcel Moolenaar /* 59072d44f31SMarcel Moolenaar * Send a T packet. We currently do not support watchpoints (the 59172d44f31SMarcel Moolenaar * awatch, rwatch or watch elements). 59272d44f31SMarcel Moolenaar */ 59372d44f31SMarcel Moolenaar gdb_tx_begin('T'); 59472d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 59572d44f31SMarcel Moolenaar gdb_tx_varhex(GDB_REG_PC); 59672d44f31SMarcel Moolenaar gdb_tx_char(':'); 59772d44f31SMarcel Moolenaar gdb_tx_reg(GDB_REG_PC); 59872d44f31SMarcel Moolenaar gdb_tx_char(';'); 59972d44f31SMarcel Moolenaar gdb_tx_str("thread:"); 60072d44f31SMarcel Moolenaar gdb_tx_varhex((long)kdb_thread->td_tid); 60172d44f31SMarcel Moolenaar gdb_tx_char(';'); 60272d44f31SMarcel Moolenaar gdb_tx_end(); /* XXX check error condition. */ 60372d44f31SMarcel Moolenaar 60472d44f31SMarcel Moolenaar thr_iter = NULL; 60572d44f31SMarcel Moolenaar while (gdb_rx_begin() == 0) { 60603e62bf3SMarcel Moolenaar /* printf("GDB: got '%s'\n", gdb_rxp); */ 60772d44f31SMarcel Moolenaar switch (gdb_rx_char()) { 60872d44f31SMarcel Moolenaar case '?': /* Last signal. */ 60976c8c090SConrad Meyer gdb_tx_begin('T'); 61072d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 61176c8c090SConrad Meyer gdb_tx_str("thread:"); 61276c8c090SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 61376c8c090SConrad Meyer gdb_tx_char(';'); 61472d44f31SMarcel Moolenaar gdb_tx_end(); 61572d44f31SMarcel Moolenaar break; 61672d44f31SMarcel Moolenaar case 'c': { /* Continue. */ 61772d44f31SMarcel Moolenaar uintmax_t addr; 618bcc5241cSMarcel Moolenaar register_t pc; 619bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 620bcc5241cSMarcel Moolenaar pc = addr; 621bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 622bcc5241cSMarcel Moolenaar } 62372d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6247d0c6c9fSSam Leffler gdb_listening = 1; 62572d44f31SMarcel Moolenaar return (1); 62672d44f31SMarcel Moolenaar } 62772d44f31SMarcel Moolenaar case 'C': { /* Continue with signal. */ 62872d44f31SMarcel Moolenaar uintmax_t addr, sig; 629bcc5241cSMarcel Moolenaar register_t pc; 63072d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 631bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 632bcc5241cSMarcel Moolenaar pc = addr; 633bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 634bcc5241cSMarcel Moolenaar } 63572d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6367d0c6c9fSSam Leffler gdb_listening = 1; 63772d44f31SMarcel Moolenaar return (1); 63872d44f31SMarcel Moolenaar } 639d412b2deSPeter Grehan case 'D': { /* Detach */ 640d412b2deSPeter Grehan gdb_tx_ok(); 641d412b2deSPeter Grehan kdb_cpu_clear_singlestep(); 642d412b2deSPeter Grehan return (1); 643d412b2deSPeter Grehan } 64472d44f31SMarcel Moolenaar case 'g': { /* Read registers. */ 64572d44f31SMarcel Moolenaar size_t r; 64672d44f31SMarcel Moolenaar gdb_tx_begin(0); 64772d44f31SMarcel Moolenaar for (r = 0; r < GDB_NREGS; r++) 64872d44f31SMarcel Moolenaar gdb_tx_reg(r); 64972d44f31SMarcel Moolenaar gdb_tx_end(); 65072d44f31SMarcel Moolenaar break; 65172d44f31SMarcel Moolenaar } 65272d44f31SMarcel Moolenaar case 'G': /* Write registers. */ 65372d44f31SMarcel Moolenaar gdb_tx_err(0); 65472d44f31SMarcel Moolenaar break; 65572d44f31SMarcel Moolenaar case 'H': { /* Set thread. */ 65672d44f31SMarcel Moolenaar intmax_t tid; 65772d44f31SMarcel Moolenaar struct thread *thr; 6585df6fa43SConrad Meyer 6595df6fa43SConrad Meyer /* Ignore 'g' (general) or 'c' (continue) flag. */ 6605df6fa43SConrad Meyer (void) gdb_rx_char(); 6615df6fa43SConrad Meyer 6624af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 6634af77eceSSam Leffler gdb_tx_err(EINVAL); 6644af77eceSSam Leffler break; 6654af77eceSSam Leffler } 66672d44f31SMarcel Moolenaar if (tid > 0) { 66772d44f31SMarcel Moolenaar thr = kdb_thr_lookup(tid); 66872d44f31SMarcel Moolenaar if (thr == NULL) { 66972d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 67072d44f31SMarcel Moolenaar break; 67172d44f31SMarcel Moolenaar } 67272d44f31SMarcel Moolenaar kdb_thr_select(thr); 67372d44f31SMarcel Moolenaar } 67472d44f31SMarcel Moolenaar gdb_tx_ok(); 67572d44f31SMarcel Moolenaar break; 67672d44f31SMarcel Moolenaar } 67772d44f31SMarcel Moolenaar case 'k': /* Kill request. */ 67872d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6797d0c6c9fSSam Leffler gdb_listening = 1; 68072d44f31SMarcel Moolenaar return (1); 68172d44f31SMarcel Moolenaar case 'm': { /* Read memory. */ 68272d44f31SMarcel Moolenaar uintmax_t addr, size; 68372d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 68472d44f31SMarcel Moolenaar gdb_rx_varhex(&size)) { 68572d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 68672d44f31SMarcel Moolenaar break; 68772d44f31SMarcel Moolenaar } 68872d44f31SMarcel Moolenaar gdb_tx_begin(0); 68972d44f31SMarcel Moolenaar if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 69072d44f31SMarcel Moolenaar gdb_tx_end(); 69172d44f31SMarcel Moolenaar else 69272d44f31SMarcel Moolenaar gdb_tx_err(EIO); 69372d44f31SMarcel Moolenaar break; 69472d44f31SMarcel Moolenaar } 69572d44f31SMarcel Moolenaar case 'M': { /* Write memory. */ 69672d44f31SMarcel Moolenaar uintmax_t addr, size; 69772d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 69872d44f31SMarcel Moolenaar gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 69972d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 70072d44f31SMarcel Moolenaar break; 70172d44f31SMarcel Moolenaar } 70272d44f31SMarcel Moolenaar if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 70372d44f31SMarcel Moolenaar gdb_tx_err(EIO); 70472d44f31SMarcel Moolenaar else 70572d44f31SMarcel Moolenaar gdb_tx_ok(); 70672d44f31SMarcel Moolenaar break; 70772d44f31SMarcel Moolenaar } 70872d44f31SMarcel Moolenaar case 'P': { /* Write register. */ 709bcc5241cSMarcel Moolenaar char *val; 710bcc5241cSMarcel Moolenaar uintmax_t reg; 711bcc5241cSMarcel Moolenaar val = gdb_rxp; 71272d44f31SMarcel Moolenaar if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 713bcc5241cSMarcel Moolenaar !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 71472d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 71572d44f31SMarcel Moolenaar break; 71672d44f31SMarcel Moolenaar } 71772d44f31SMarcel Moolenaar gdb_cpu_setreg(reg, val); 71872d44f31SMarcel Moolenaar gdb_tx_ok(); 71972d44f31SMarcel Moolenaar break; 72072d44f31SMarcel Moolenaar } 72172d44f31SMarcel Moolenaar case 'q': /* General query. */ 7223bbe6e64SConrad Meyer if (gdb_rx_equal("C")) { 7233bbe6e64SConrad Meyer gdb_tx_begin('Q'); 7243bbe6e64SConrad Meyer gdb_tx_char('C'); 7253bbe6e64SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 7263bbe6e64SConrad Meyer gdb_tx_end(); 727c4fbbfaaSConrad Meyer } else if (gdb_rx_equal("Supported")) { 728c4fbbfaaSConrad Meyer gdb_do_qsupported(&host_features); 7293bbe6e64SConrad Meyer } else if (gdb_rx_equal("fThreadInfo")) { 73072d44f31SMarcel Moolenaar thr_iter = kdb_thr_first(); 731130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 73272d44f31SMarcel Moolenaar } else if (gdb_rx_equal("sThreadInfo")) { 733130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 7345555afa1SConrad Meyer } else if (gdb_rx_equal("Xfer:")) { 7355555afa1SConrad Meyer do_qXfer(); 73627ecc2adSBenno Rice } else if (gdb_rx_equal("Search:memory:")) { 7378a7a6571SRyan Libby gdb_do_mem_search(); 73872d44f31SMarcel Moolenaar } else if (!gdb_cpu_query()) 73972d44f31SMarcel Moolenaar gdb_tx_empty(); 74072d44f31SMarcel Moolenaar break; 74172d44f31SMarcel Moolenaar case 's': { /* Step. */ 74272d44f31SMarcel Moolenaar uintmax_t addr; 743bcc5241cSMarcel Moolenaar register_t pc; 744bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 745bcc5241cSMarcel Moolenaar pc = addr; 746bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 747bcc5241cSMarcel Moolenaar } 74872d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 7497d0c6c9fSSam Leffler gdb_listening = 1; 75072d44f31SMarcel Moolenaar return (1); 75172d44f31SMarcel Moolenaar } 75272d44f31SMarcel Moolenaar case 'S': { /* Step with signal. */ 75372d44f31SMarcel Moolenaar uintmax_t addr, sig; 754bcc5241cSMarcel Moolenaar register_t pc; 75572d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 756bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 757bcc5241cSMarcel Moolenaar pc = addr; 758bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 759bcc5241cSMarcel Moolenaar } 76072d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 7617d0c6c9fSSam Leffler gdb_listening = 1; 76272d44f31SMarcel Moolenaar return (1); 76372d44f31SMarcel Moolenaar } 76472d44f31SMarcel Moolenaar case 'T': { /* Thread alive. */ 76572d44f31SMarcel Moolenaar intmax_t tid; 7664af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 7674af77eceSSam Leffler gdb_tx_err(EINVAL); 7684af77eceSSam Leffler break; 7694af77eceSSam Leffler } 77072d44f31SMarcel Moolenaar if (kdb_thr_lookup(tid) != NULL) 77172d44f31SMarcel Moolenaar gdb_tx_ok(); 77272d44f31SMarcel Moolenaar else 77372d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 77472d44f31SMarcel Moolenaar break; 77572d44f31SMarcel Moolenaar } 7765df6fa43SConrad Meyer case EOF: 77772d44f31SMarcel Moolenaar /* Empty command. Treat as unknown command. */ 77872d44f31SMarcel Moolenaar /* FALLTHROUGH */ 77972d44f31SMarcel Moolenaar default: 78072d44f31SMarcel Moolenaar /* Unknown command. Send empty response. */ 78172d44f31SMarcel Moolenaar gdb_tx_empty(); 78272d44f31SMarcel Moolenaar break; 78372d44f31SMarcel Moolenaar } 78472d44f31SMarcel Moolenaar } 7853a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 78672d44f31SMarcel Moolenaar return (0); 78772d44f31SMarcel Moolenaar } 788