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 4772d44f31SMarcel Moolenaar static dbbe_init_f gdb_init; 4872d44f31SMarcel Moolenaar static dbbe_trap_f gdb_trap; 4972d44f31SMarcel Moolenaar 5028926c57SJohn Baldwin KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap); 5172d44f31SMarcel Moolenaar 52e8d86c0eSPoul-Henning Kamp static struct gdb_dbgport null_gdb_dbgport; 53e8d86c0eSPoul-Henning Kamp DATA_SET(gdb_dbgport_set, null_gdb_dbgport); 5472d44f31SMarcel Moolenaar SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 5572d44f31SMarcel Moolenaar 5672d44f31SMarcel Moolenaar struct gdb_dbgport *gdb_cur = NULL; 577d0c6c9fSSam Leffler int gdb_listening = 0; 5872d44f31SMarcel Moolenaar 5927ecc2adSBenno Rice static unsigned char gdb_bindata[64]; 6027ecc2adSBenno Rice 6172d44f31SMarcel Moolenaar static int 6272d44f31SMarcel Moolenaar gdb_init(void) 6372d44f31SMarcel Moolenaar { 6472d44f31SMarcel Moolenaar struct gdb_dbgport *dp, **iter; 6572d44f31SMarcel Moolenaar int cur_pri, pri; 6672d44f31SMarcel Moolenaar 6772d44f31SMarcel Moolenaar gdb_cur = NULL; 6872d44f31SMarcel Moolenaar cur_pri = -1; 6972d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 7072d44f31SMarcel Moolenaar dp = *iter; 7172d44f31SMarcel Moolenaar pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 7272d44f31SMarcel Moolenaar dp->gdb_active = (pri >= 0) ? 0 : -1; 7372d44f31SMarcel Moolenaar if (pri > cur_pri) { 7472d44f31SMarcel Moolenaar cur_pri = pri; 7572d44f31SMarcel Moolenaar gdb_cur = dp; 7672d44f31SMarcel Moolenaar } 7772d44f31SMarcel Moolenaar } 7872d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 7972d44f31SMarcel Moolenaar printf("GDB: debug ports:"); 8072d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 8172d44f31SMarcel Moolenaar dp = *iter; 8272d44f31SMarcel Moolenaar if (dp->gdb_active == 0) 8372d44f31SMarcel Moolenaar printf(" %s", dp->gdb_name); 8472d44f31SMarcel Moolenaar } 8572d44f31SMarcel Moolenaar printf("\n"); 8672d44f31SMarcel Moolenaar } else 8772d44f31SMarcel Moolenaar printf("GDB: no debug ports present\n"); 8872d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 8972d44f31SMarcel Moolenaar gdb_cur->gdb_init(); 9072d44f31SMarcel Moolenaar printf("GDB: current port: %s\n", gdb_cur->gdb_name); 9172d44f31SMarcel Moolenaar } 927d0c6c9fSSam Leffler if (gdb_cur != NULL) { 9372d44f31SMarcel Moolenaar cur_pri = (boothowto & RB_GDB) ? 2 : 0; 947d0c6c9fSSam Leffler gdb_consinit(); 957d0c6c9fSSam Leffler } else 9672d44f31SMarcel Moolenaar cur_pri = -1; 9772d44f31SMarcel Moolenaar return (cur_pri); 9872d44f31SMarcel Moolenaar } 9972d44f31SMarcel Moolenaar 1008a7a6571SRyan Libby static void 1018a7a6571SRyan Libby gdb_do_mem_search(void) 1028a7a6571SRyan Libby { 1038a7a6571SRyan Libby size_t patlen; 1048a7a6571SRyan Libby intmax_t addr, size; 1058a7a6571SRyan Libby const unsigned char *found; 1068a7a6571SRyan Libby 1078a7a6571SRyan Libby if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' || 1088a7a6571SRyan Libby gdb_rx_varhex(&size) || gdb_rx_char() != ';' || 1098a7a6571SRyan Libby gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) { 1108a7a6571SRyan Libby gdb_tx_err(EINVAL); 1118a7a6571SRyan Libby return; 1128a7a6571SRyan Libby } 1138a7a6571SRyan Libby if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, 1148a7a6571SRyan Libby patlen, &found)) { 1158a7a6571SRyan Libby if (found == 0ULL) 1168a7a6571SRyan Libby gdb_tx_begin('0'); 1178a7a6571SRyan Libby else { 1188a7a6571SRyan Libby gdb_tx_begin('1'); 1198a7a6571SRyan Libby gdb_tx_char(','); 1208a7a6571SRyan Libby gdb_tx_hex((intmax_t)(uintptr_t)found, 8); 1218a7a6571SRyan Libby } 1228a7a6571SRyan Libby gdb_tx_end(); 1238a7a6571SRyan Libby } else 1248a7a6571SRyan Libby gdb_tx_err(EIO); 1258a7a6571SRyan Libby } 1268a7a6571SRyan Libby 127130ef1adSConrad Meyer static void 128130ef1adSConrad Meyer gdb_do_threadinfo(struct thread **thr_iter) 129130ef1adSConrad Meyer { 130130ef1adSConrad Meyer static struct thread * const done_sentinel = (void *)(uintptr_t)1; 131130ef1adSConrad Meyer static const size_t tidsz_hex = sizeof(lwpid_t) * 2; 132130ef1adSConrad Meyer size_t tds_sent; 133130ef1adSConrad Meyer 134130ef1adSConrad Meyer if (*thr_iter == NULL) { 135130ef1adSConrad Meyer gdb_tx_err(ENXIO); 136130ef1adSConrad Meyer return; 137130ef1adSConrad Meyer } 138130ef1adSConrad Meyer 139130ef1adSConrad Meyer if (*thr_iter == done_sentinel) { 140130ef1adSConrad Meyer gdb_tx_begin('l'); 141130ef1adSConrad Meyer *thr_iter = NULL; 142130ef1adSConrad Meyer goto sendit; 143130ef1adSConrad Meyer } 144130ef1adSConrad Meyer 145130ef1adSConrad Meyer gdb_tx_begin('m'); 146130ef1adSConrad Meyer 147130ef1adSConrad Meyer for (tds_sent = 0; 148130ef1adSConrad Meyer *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1); 149130ef1adSConrad Meyer *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) { 150130ef1adSConrad Meyer if (tds_sent > 0) 151130ef1adSConrad Meyer gdb_tx_char(','); 152130ef1adSConrad Meyer gdb_tx_varhex((*thr_iter)->td_tid); 153130ef1adSConrad Meyer } 154130ef1adSConrad Meyer 155130ef1adSConrad Meyer /* 156130ef1adSConrad Meyer * Can't send EOF and "some" in same packet, so set a sentinel to send 157130ef1adSConrad Meyer * EOF when GDB asks us next. 158130ef1adSConrad Meyer */ 159130ef1adSConrad Meyer if (*thr_iter == NULL && tds_sent > 0) 160130ef1adSConrad Meyer *thr_iter = done_sentinel; 161130ef1adSConrad Meyer 162130ef1adSConrad Meyer sendit: 163130ef1adSConrad Meyer gdb_tx_end(); 164130ef1adSConrad Meyer } 165130ef1adSConrad Meyer 166c4fbbfaaSConrad Meyer #define BIT(n) (1ull << (n)) 167c4fbbfaaSConrad Meyer enum { 168c4fbbfaaSConrad Meyer GDB_MULTIPROCESS, 169c4fbbfaaSConrad Meyer GDB_SWBREAK, 170c4fbbfaaSConrad Meyer GDB_HWBREAK, 171c4fbbfaaSConrad Meyer GDB_QRELOCINSN, 172c4fbbfaaSConrad Meyer GDB_FORK_EVENTS, 173c4fbbfaaSConrad Meyer GDB_VFORK_EVENTS, 174c4fbbfaaSConrad Meyer GDB_EXEC_EVENTS, 175c4fbbfaaSConrad Meyer GDB_VCONT_SUPPORTED, 176c4fbbfaaSConrad Meyer GDB_QTHREADEVENTS, 177c4fbbfaaSConrad Meyer GDB_NO_RESUMED, 178c4fbbfaaSConrad Meyer }; 179c4fbbfaaSConrad Meyer static const char * const gdb_feature_names[] = { 180c4fbbfaaSConrad Meyer [GDB_MULTIPROCESS] = "multiprocess", 181c4fbbfaaSConrad Meyer [GDB_SWBREAK] = "swbreak", 182c4fbbfaaSConrad Meyer [GDB_HWBREAK] = "hwbreak", 183c4fbbfaaSConrad Meyer [GDB_QRELOCINSN] = "qRelocInsn", 184c4fbbfaaSConrad Meyer [GDB_FORK_EVENTS] = "fork-events", 185c4fbbfaaSConrad Meyer [GDB_VFORK_EVENTS] = "vfork-events", 186c4fbbfaaSConrad Meyer [GDB_EXEC_EVENTS] = "exec-events", 187c4fbbfaaSConrad Meyer [GDB_VCONT_SUPPORTED] = "vContSupported", 188c4fbbfaaSConrad Meyer [GDB_QTHREADEVENTS] = "QThreadEvents", 189c4fbbfaaSConrad Meyer [GDB_NO_RESUMED] = "no-resumed", 190c4fbbfaaSConrad Meyer }; 191c4fbbfaaSConrad Meyer static void 192c4fbbfaaSConrad Meyer gdb_do_qsupported(uint32_t *feat) 193c4fbbfaaSConrad Meyer { 194c4fbbfaaSConrad Meyer char *tok, *delim, ok; 195c4fbbfaaSConrad Meyer size_t i, toklen; 196c4fbbfaaSConrad Meyer 197c4fbbfaaSConrad Meyer /* Parse supported host features */ 198c4fbbfaaSConrad Meyer *feat = 0; 199c4fbbfaaSConrad Meyer if (gdb_rx_char() != ':') 200c4fbbfaaSConrad Meyer goto error; 201c4fbbfaaSConrad Meyer 202c4fbbfaaSConrad Meyer while (gdb_rxsz > 0) { 203c4fbbfaaSConrad Meyer tok = gdb_rxp; 204c4fbbfaaSConrad Meyer delim = strchrnul(gdb_rxp, ';'); 205c4fbbfaaSConrad Meyer toklen = (delim - tok); 206c4fbbfaaSConrad Meyer 207c4fbbfaaSConrad Meyer gdb_rxp += toklen; 208c4fbbfaaSConrad Meyer gdb_rxsz -= toklen; 209c4fbbfaaSConrad Meyer if (*delim != '\0') { 210c4fbbfaaSConrad Meyer *delim = '\0'; 211c4fbbfaaSConrad Meyer gdb_rxp += 1; 212c4fbbfaaSConrad Meyer gdb_rxsz -= 1; 213c4fbbfaaSConrad Meyer } 214c4fbbfaaSConrad Meyer 215c4fbbfaaSConrad Meyer if (toklen < 2) 216c4fbbfaaSConrad Meyer goto error; 217c4fbbfaaSConrad Meyer 218c4fbbfaaSConrad Meyer ok = tok[toklen - 1]; 219c4fbbfaaSConrad Meyer if (ok != '-' && ok != '+') { 220c4fbbfaaSConrad Meyer /* 221c4fbbfaaSConrad Meyer * GDB only has one KV-pair feature, and we don't 222c4fbbfaaSConrad Meyer * support it, so ignore and move on. 223c4fbbfaaSConrad Meyer */ 224c4fbbfaaSConrad Meyer if (strchr(tok, '=') != NULL) 225c4fbbfaaSConrad Meyer continue; 226c4fbbfaaSConrad Meyer /* Not a KV-pair, and not a +/- flag? Malformed. */ 227c4fbbfaaSConrad Meyer goto error; 228c4fbbfaaSConrad Meyer } 229c4fbbfaaSConrad Meyer if (ok != '+') 230c4fbbfaaSConrad Meyer continue; 231c4fbbfaaSConrad Meyer tok[toklen - 1] = '\0'; 232c4fbbfaaSConrad Meyer 233c4fbbfaaSConrad Meyer for (i = 0; i < nitems(gdb_feature_names); i++) 234c4fbbfaaSConrad Meyer if (strcmp(gdb_feature_names[i], tok) == 0) 235c4fbbfaaSConrad Meyer break; 236c4fbbfaaSConrad Meyer 237c4fbbfaaSConrad Meyer if (i == nitems(gdb_feature_names)) { 238c4fbbfaaSConrad Meyer /* Unknown GDB feature. */ 239c4fbbfaaSConrad Meyer continue; 240c4fbbfaaSConrad Meyer } 241c4fbbfaaSConrad Meyer 242c4fbbfaaSConrad Meyer *feat |= BIT(i); 243c4fbbfaaSConrad Meyer } 244c4fbbfaaSConrad Meyer 245c4fbbfaaSConrad Meyer /* Send a supported feature list back */ 246c4fbbfaaSConrad Meyer gdb_tx_begin(0); 247c4fbbfaaSConrad Meyer 248c4fbbfaaSConrad Meyer gdb_tx_str("PacketSize"); 249c4fbbfaaSConrad Meyer gdb_tx_char('='); 250c4fbbfaaSConrad Meyer /* 251c4fbbfaaSConrad Meyer * We don't buffer framing bytes, but we do need to retain a byte for a 252c4fbbfaaSConrad Meyer * trailing nul. 253c4fbbfaaSConrad Meyer */ 254c4fbbfaaSConrad Meyer gdb_tx_varhex(GDB_BUFSZ + strlen("$#nn") - 1); 255c4fbbfaaSConrad Meyer 2565555afa1SConrad Meyer gdb_tx_str(";qXfer:threads:read+"); 2575555afa1SConrad Meyer 258c4fbbfaaSConrad Meyer /* 259c4fbbfaaSConrad Meyer * Future consideration: 260c4fbbfaaSConrad Meyer * - vCont 261c4fbbfaaSConrad Meyer * - multiprocess 262c4fbbfaaSConrad Meyer */ 263c4fbbfaaSConrad Meyer gdb_tx_end(); 264c4fbbfaaSConrad Meyer return; 265c4fbbfaaSConrad Meyer 266c4fbbfaaSConrad Meyer error: 267c4fbbfaaSConrad Meyer *feat = 0; 268c4fbbfaaSConrad Meyer gdb_tx_err(EINVAL); 269c4fbbfaaSConrad Meyer } 270c4fbbfaaSConrad Meyer 2715555afa1SConrad Meyer /* 2725555afa1SConrad Meyer * A qXfer_context provides a vaguely generic way to generate a multi-packet 2735555afa1SConrad Meyer * response on the fly, making some assumptions about the size of sbuf writes 2745555afa1SConrad Meyer * vs actual packet length constraints. A non-byzantine gdb host should allow 2755555afa1SConrad Meyer * hundreds of bytes per packet or more. 2765555afa1SConrad Meyer * 2775555afa1SConrad Meyer * Upper layers are considered responsible for escaping the four forbidden 2785555afa1SConrad Meyer * characters '# $ } *'. 2795555afa1SConrad Meyer */ 2805555afa1SConrad Meyer struct qXfer_context { 2815555afa1SConrad Meyer struct sbuf sb; 2825555afa1SConrad Meyer size_t last_offset; 2835555afa1SConrad Meyer bool flushed; 2845555afa1SConrad Meyer bool lastmessage; 2855555afa1SConrad Meyer char xfer_buf[GDB_BUFSZ]; 2865555afa1SConrad Meyer }; 2875555afa1SConrad Meyer 2885555afa1SConrad Meyer static int 2895555afa1SConrad Meyer qXfer_drain(void *v, const char *buf, int len) 2905555afa1SConrad Meyer { 2915555afa1SConrad Meyer struct qXfer_context *qx; 2925555afa1SConrad Meyer 2935555afa1SConrad Meyer if (len < 0) 2945555afa1SConrad Meyer return (-EINVAL); 2955555afa1SConrad Meyer 2965555afa1SConrad Meyer qx = v; 2975555afa1SConrad Meyer if (qx->flushed) { 2985555afa1SConrad Meyer /* 2995555afa1SConrad Meyer * Overflow. We lost some message. Maybe the packet size is 3005555afa1SConrad Meyer * ridiculously small. 3015555afa1SConrad Meyer */ 3025555afa1SConrad Meyer printf("%s: Overflow in qXfer detected.\n", __func__); 3035555afa1SConrad Meyer return (-ENOBUFS); 3045555afa1SConrad Meyer } 3055555afa1SConrad Meyer 3065555afa1SConrad Meyer qx->last_offset += len; 3075555afa1SConrad Meyer qx->flushed = true; 3085555afa1SConrad Meyer 3095555afa1SConrad Meyer if (qx->lastmessage) 3105555afa1SConrad Meyer gdb_tx_begin('l'); 3115555afa1SConrad Meyer else 3125555afa1SConrad Meyer gdb_tx_begin('m'); 3135555afa1SConrad Meyer 3145555afa1SConrad Meyer memcpy(gdb_txp, buf, len); 3155555afa1SConrad Meyer gdb_txp += len; 3165555afa1SConrad Meyer 3175555afa1SConrad Meyer gdb_tx_end(); 3185555afa1SConrad Meyer return (len); 3195555afa1SConrad Meyer } 3205555afa1SConrad Meyer 3215555afa1SConrad Meyer static int 3225555afa1SConrad Meyer init_qXfer_ctx(struct qXfer_context *qx, uintmax_t len) 3235555afa1SConrad Meyer { 3245555afa1SConrad Meyer 3255555afa1SConrad Meyer /* Protocol (max) length field includes framing overhead. */ 3265555afa1SConrad Meyer if (len < sizeof("$m#nn")) 3275555afa1SConrad Meyer return (ENOSPC); 3285555afa1SConrad Meyer 3295555afa1SConrad Meyer len -= 4; 3305555afa1SConrad Meyer len = ummin(len, GDB_BUFSZ - 1); 3315555afa1SConrad Meyer 3325555afa1SConrad Meyer qx->last_offset = 0; 3335555afa1SConrad Meyer qx->flushed = false; 3345555afa1SConrad Meyer qx->lastmessage = false; 3355555afa1SConrad Meyer sbuf_new(&qx->sb, qx->xfer_buf, len, SBUF_FIXEDLEN); 3365555afa1SConrad Meyer sbuf_set_drain(&qx->sb, qXfer_drain, qx); 3375555afa1SConrad Meyer return (0); 3385555afa1SConrad Meyer } 3395555afa1SConrad Meyer 3405555afa1SConrad Meyer /* 3415555afa1SConrad Meyer * dst must be 2x strlen(max_src) + 1. 3425555afa1SConrad Meyer * 3435555afa1SConrad Meyer * Squashes invalid XML characters down to _. Sorry. Then escapes for GDB. 3445555afa1SConrad Meyer */ 3455555afa1SConrad Meyer static void 3465555afa1SConrad Meyer qXfer_escape_xmlattr_str(char *dst, size_t dstlen, const char *src) 3475555afa1SConrad Meyer { 3485555afa1SConrad Meyer static const char *forbidden = "#$}*"; 3495555afa1SConrad Meyer 3505555afa1SConrad Meyer size_t i; 3515555afa1SConrad Meyer char c; 3525555afa1SConrad Meyer 3535555afa1SConrad Meyer for (i = 0; i < dstlen - 1 && *src != 0; src++, i++) { 3545555afa1SConrad Meyer c = *src; 3555555afa1SConrad Meyer /* XML attr filter */ 3565555afa1SConrad Meyer if (c < 32) 3575555afa1SConrad Meyer c = '_'; 3585555afa1SConrad Meyer /* We assume attributes will be "" quoted. */ 3595555afa1SConrad Meyer if (c == '<' || c == '&' || c == '"') 3605555afa1SConrad Meyer c = '_'; 3615555afa1SConrad Meyer 3625555afa1SConrad Meyer /* GDB escape. */ 3635555afa1SConrad Meyer if (strchr(forbidden, c) != NULL) { 3645555afa1SConrad Meyer *dst++ = '}'; 3655555afa1SConrad Meyer c ^= 0x20; 3665555afa1SConrad Meyer } 3675555afa1SConrad Meyer *dst++ = c; 3685555afa1SConrad Meyer } 3695555afa1SConrad Meyer if (*src != 0) 3705555afa1SConrad Meyer printf("XXX%s: overflow; API misuse\n", __func__); 3715555afa1SConrad Meyer 3725555afa1SConrad Meyer *dst = 0; 3735555afa1SConrad Meyer } 3745555afa1SConrad Meyer 3755555afa1SConrad Meyer /* 3765555afa1SConrad Meyer * Dynamically generate qXfer:threads document, one packet at a time. 3775555afa1SConrad Meyer * 3785555afa1SConrad Meyer * The format is loosely described[0], although it does not seem that the 3795555afa1SConrad Meyer * <?xml?> mentioned on that page is required. 3805555afa1SConrad Meyer * 3815555afa1SConrad Meyer * [0]: https://sourceware.org/gdb/current/onlinedocs/gdb/Thread-List-Format.html 3825555afa1SConrad Meyer */ 3835555afa1SConrad Meyer static void 3845555afa1SConrad Meyer do_qXfer_threads_read(void) 3855555afa1SConrad Meyer { 3865555afa1SConrad Meyer /* Kludgy context */ 3875555afa1SConrad Meyer static struct { 3885555afa1SConrad Meyer struct qXfer_context qXfer; 3895555afa1SConrad Meyer /* Kludgy state machine */ 3905555afa1SConrad Meyer struct thread *iter; 3915555afa1SConrad Meyer enum { 3925555afa1SConrad Meyer XML_START_THREAD, /* '<thread' */ 3935555afa1SConrad Meyer XML_THREAD_ID, /* ' id="xxx"' */ 3945555afa1SConrad Meyer XML_THREAD_CORE, /* ' core="yyy"' */ 3955555afa1SConrad Meyer XML_THREAD_NAME, /* ' name="zzz"' */ 3965555afa1SConrad Meyer XML_THREAD_EXTRA, /* '> ...' */ 3975555afa1SConrad Meyer XML_END_THREAD, /* '</thread>' */ 3985555afa1SConrad Meyer XML_SENT_END_THREADS, /* '</threads>' */ 3995555afa1SConrad Meyer } next_step; 4005555afa1SConrad Meyer } ctx; 4015555afa1SConrad Meyer static char td_name_escape[MAXCOMLEN * 2 + 1]; 4025555afa1SConrad Meyer 4035555afa1SConrad Meyer const char *name_src; 4045555afa1SConrad Meyer uintmax_t offset, len; 4055555afa1SConrad Meyer int error; 4065555afa1SConrad Meyer 4075555afa1SConrad Meyer /* Annex part must be empty. */ 4085555afa1SConrad Meyer if (gdb_rx_char() != ':') 4095555afa1SConrad Meyer goto misformed_request; 4105555afa1SConrad Meyer 4115555afa1SConrad Meyer if (gdb_rx_varhex(&offset) != 0 || 4125555afa1SConrad Meyer gdb_rx_char() != ',' || 4135555afa1SConrad Meyer gdb_rx_varhex(&len) != 0) 4145555afa1SConrad Meyer goto misformed_request; 4155555afa1SConrad Meyer 4165555afa1SConrad Meyer /* 4175555afa1SConrad Meyer * Validate resume xfers. 4185555afa1SConrad Meyer */ 4195555afa1SConrad Meyer if (offset != 0) { 4205555afa1SConrad Meyer if (offset != ctx.qXfer.last_offset) { 4215555afa1SConrad Meyer printf("%s: Resumed offset %ju != expected %ju\n", 4225555afa1SConrad Meyer __func__, offset, ctx.qXfer.last_offset); 4235555afa1SConrad Meyer error = ESPIPE; 4245555afa1SConrad Meyer goto request_error; 4255555afa1SConrad Meyer } 4265555afa1SConrad Meyer ctx.qXfer.flushed = false; 4275555afa1SConrad Meyer } 4285555afa1SConrad Meyer 4295555afa1SConrad Meyer if (offset == 0) { 4305555afa1SConrad Meyer ctx.iter = kdb_thr_first(); 4315555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 4325555afa1SConrad Meyer error = init_qXfer_ctx(&ctx.qXfer, len); 4335555afa1SConrad Meyer if (error != 0) 4345555afa1SConrad Meyer goto request_error; 4355555afa1SConrad Meyer 4365555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<threads>"); 4375555afa1SConrad Meyer } 4385555afa1SConrad Meyer 4395555afa1SConrad Meyer while (!ctx.qXfer.flushed && ctx.iter != NULL) { 4405555afa1SConrad Meyer switch (ctx.next_step) { 4415555afa1SConrad Meyer case XML_START_THREAD: 4425555afa1SConrad Meyer ctx.next_step = XML_THREAD_ID; 4435555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<thread"); 4445555afa1SConrad Meyer continue; 4455555afa1SConrad Meyer 4465555afa1SConrad Meyer case XML_THREAD_ID: 4475555afa1SConrad Meyer ctx.next_step = XML_THREAD_CORE; 4485555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " id=\"%jx\"", 4495555afa1SConrad Meyer (uintmax_t)ctx.iter->td_tid); 4505555afa1SConrad Meyer continue; 4515555afa1SConrad Meyer 4525555afa1SConrad Meyer case XML_THREAD_CORE: 4535555afa1SConrad Meyer ctx.next_step = XML_THREAD_NAME; 4545555afa1SConrad Meyer if (ctx.iter->td_oncpu != NOCPU) { 4555555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " core=\"%d\"", 4565555afa1SConrad Meyer ctx.iter->td_oncpu); 4575555afa1SConrad Meyer } 4585555afa1SConrad Meyer continue; 4595555afa1SConrad Meyer 4605555afa1SConrad Meyer case XML_THREAD_NAME: 4615555afa1SConrad Meyer ctx.next_step = XML_THREAD_EXTRA; 4625555afa1SConrad Meyer 4635555afa1SConrad Meyer if (ctx.iter->td_name[0] != 0) 4645555afa1SConrad Meyer name_src = ctx.iter->td_name; 4655555afa1SConrad Meyer else if (ctx.iter->td_proc != NULL && 4665555afa1SConrad Meyer ctx.iter->td_proc->p_comm[0] != 0) 4675555afa1SConrad Meyer name_src = ctx.iter->td_proc->p_comm; 4685555afa1SConrad Meyer else 4695555afa1SConrad Meyer continue; 4705555afa1SConrad Meyer 4715555afa1SConrad Meyer qXfer_escape_xmlattr_str(td_name_escape, 4725555afa1SConrad Meyer sizeof(td_name_escape), name_src); 4735555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " name=\"%s\"", 4745555afa1SConrad Meyer td_name_escape); 4755555afa1SConrad Meyer continue; 4765555afa1SConrad Meyer 4775555afa1SConrad Meyer case XML_THREAD_EXTRA: 4785555afa1SConrad Meyer ctx.next_step = XML_END_THREAD; 4795555afa1SConrad Meyer 4805555afa1SConrad Meyer sbuf_putc(&ctx.qXfer.sb, '>'); 4815555afa1SConrad Meyer 4825555afa1SConrad Meyer if (ctx.iter->td_state == TDS_RUNNING) 4835555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Running"); 4845555afa1SConrad Meyer else if (ctx.iter->td_state == TDS_RUNQ) 4855555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "RunQ"); 4865555afa1SConrad Meyer else if (ctx.iter->td_state == TDS_CAN_RUN) 4875555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "CanRun"); 4885555afa1SConrad Meyer else if (TD_ON_LOCK(ctx.iter)) 4895555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Blocked"); 4905555afa1SConrad Meyer else if (TD_IS_SLEEPING(ctx.iter)) 4915555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Sleeping"); 4925555afa1SConrad Meyer else if (TD_IS_SWAPPED(ctx.iter)) 4935555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Swapped"); 4945555afa1SConrad Meyer else if (TD_AWAITING_INTR(ctx.iter)) 4955555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "IthreadWait"); 4965555afa1SConrad Meyer else if (TD_IS_SUSPENDED(ctx.iter)) 4975555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Suspended"); 4985555afa1SConrad Meyer else 4995555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "???"); 5005555afa1SConrad Meyer continue; 5015555afa1SConrad Meyer 5025555afa1SConrad Meyer case XML_END_THREAD: 5035555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 5045555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</thread>"); 5055555afa1SConrad Meyer ctx.iter = kdb_thr_next(ctx.iter); 5065555afa1SConrad Meyer continue; 5075555afa1SConrad Meyer 5085555afa1SConrad Meyer /* 5095555afa1SConrad Meyer * This one isn't part of the looping state machine, 5105555afa1SConrad Meyer * but GCC complains if you leave an enum value out of the 5115555afa1SConrad Meyer * select. 5125555afa1SConrad Meyer */ 5135555afa1SConrad Meyer case XML_SENT_END_THREADS: 5145555afa1SConrad Meyer /* NOTREACHED */ 5155555afa1SConrad Meyer break; 5165555afa1SConrad Meyer } 5175555afa1SConrad Meyer } 5185555afa1SConrad Meyer if (ctx.qXfer.flushed) 5195555afa1SConrad Meyer return; 5205555afa1SConrad Meyer 5215555afa1SConrad Meyer if (ctx.next_step != XML_SENT_END_THREADS) { 5225555afa1SConrad Meyer ctx.next_step = XML_SENT_END_THREADS; 5235555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</threads>"); 5245555afa1SConrad Meyer } 5255555afa1SConrad Meyer if (ctx.qXfer.flushed) 5265555afa1SConrad Meyer return; 5275555afa1SConrad Meyer 5285555afa1SConrad Meyer ctx.qXfer.lastmessage = true; 5295555afa1SConrad Meyer sbuf_finish(&ctx.qXfer.sb); 5305555afa1SConrad Meyer sbuf_delete(&ctx.qXfer.sb); 5315555afa1SConrad Meyer ctx.qXfer.last_offset = 0; 5325555afa1SConrad Meyer return; 5335555afa1SConrad Meyer 5345555afa1SConrad Meyer misformed_request: 5355555afa1SConrad Meyer /* 5365555afa1SConrad Meyer * GDB "General-Query-Packets.html" qXfer-read anchor specifically 5375555afa1SConrad Meyer * documents an E00 code for malformed requests or invalid annex. 5385555afa1SConrad Meyer * Non-zero codes indicate invalid offset or "error reading the data." 5395555afa1SConrad Meyer */ 5405555afa1SConrad Meyer error = 0; 5415555afa1SConrad Meyer request_error: 5425555afa1SConrad Meyer gdb_tx_err(error); 5435555afa1SConrad Meyer return; 5445555afa1SConrad Meyer } 5455555afa1SConrad Meyer 5465555afa1SConrad Meyer /* 5475555afa1SConrad Meyer * A set of standardized transfers from "special data areas." 5485555afa1SConrad Meyer * 5495555afa1SConrad Meyer * We've already matched on "qXfer:" and advanced the rx packet buffer past 5505555afa1SConrad Meyer * that bit. Parse out the rest of the packet and generate an appropriate 5515555afa1SConrad Meyer * response. 5525555afa1SConrad Meyer */ 5535555afa1SConrad Meyer static void 5545555afa1SConrad Meyer do_qXfer(void) 5555555afa1SConrad Meyer { 5565555afa1SConrad Meyer if (!gdb_rx_equal("threads:")) 5575555afa1SConrad Meyer goto unrecognized; 5585555afa1SConrad Meyer 5595555afa1SConrad Meyer if (!gdb_rx_equal("read:")) 5605555afa1SConrad Meyer goto unrecognized; 5615555afa1SConrad Meyer 5625555afa1SConrad Meyer do_qXfer_threads_read(); 5635555afa1SConrad Meyer return; 5645555afa1SConrad Meyer 5655555afa1SConrad Meyer unrecognized: 5665555afa1SConrad Meyer gdb_tx_empty(); 5675555afa1SConrad Meyer return; 5685555afa1SConrad Meyer } 5695555afa1SConrad Meyer 57072d44f31SMarcel Moolenaar static int 57172d44f31SMarcel Moolenaar gdb_trap(int type, int code) 57272d44f31SMarcel Moolenaar { 5733a5d3671SMatthew D Fleming jmp_buf jb; 57472d44f31SMarcel Moolenaar struct thread *thr_iter; 5753a5d3671SMatthew D Fleming void *prev_jb; 576c4fbbfaaSConrad Meyer uint32_t host_features; 5773a5d3671SMatthew D Fleming 5783a5d3671SMatthew D Fleming prev_jb = kdb_jmpbuf(jb); 5793a5d3671SMatthew D Fleming if (setjmp(jb) != 0) { 5803a5d3671SMatthew D Fleming printf("%s bailing, hopefully back to ddb!\n", __func__); 5813a5d3671SMatthew D Fleming gdb_listening = 0; 5823a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 5833a5d3671SMatthew D Fleming return (1); 5843a5d3671SMatthew D Fleming } 58572d44f31SMarcel Moolenaar 5867d0c6c9fSSam Leffler gdb_listening = 0; 58772d44f31SMarcel Moolenaar /* 58872d44f31SMarcel Moolenaar * Send a T packet. We currently do not support watchpoints (the 58972d44f31SMarcel Moolenaar * awatch, rwatch or watch elements). 59072d44f31SMarcel Moolenaar */ 59172d44f31SMarcel Moolenaar gdb_tx_begin('T'); 59272d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 59372d44f31SMarcel Moolenaar gdb_tx_varhex(GDB_REG_PC); 59472d44f31SMarcel Moolenaar gdb_tx_char(':'); 59572d44f31SMarcel Moolenaar gdb_tx_reg(GDB_REG_PC); 59672d44f31SMarcel Moolenaar gdb_tx_char(';'); 59772d44f31SMarcel Moolenaar gdb_tx_str("thread:"); 59872d44f31SMarcel Moolenaar gdb_tx_varhex((long)kdb_thread->td_tid); 59972d44f31SMarcel Moolenaar gdb_tx_char(';'); 60072d44f31SMarcel Moolenaar gdb_tx_end(); /* XXX check error condition. */ 60172d44f31SMarcel Moolenaar 60272d44f31SMarcel Moolenaar thr_iter = NULL; 60372d44f31SMarcel Moolenaar while (gdb_rx_begin() == 0) { 60403e62bf3SMarcel Moolenaar /* printf("GDB: got '%s'\n", gdb_rxp); */ 60572d44f31SMarcel Moolenaar switch (gdb_rx_char()) { 60672d44f31SMarcel Moolenaar case '?': /* Last signal. */ 60776c8c090SConrad Meyer gdb_tx_begin('T'); 60872d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 60976c8c090SConrad Meyer gdb_tx_str("thread:"); 61076c8c090SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 61176c8c090SConrad Meyer gdb_tx_char(';'); 61272d44f31SMarcel Moolenaar gdb_tx_end(); 61372d44f31SMarcel Moolenaar break; 61472d44f31SMarcel Moolenaar case 'c': { /* Continue. */ 61572d44f31SMarcel Moolenaar uintmax_t addr; 616bcc5241cSMarcel Moolenaar register_t pc; 617bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 618bcc5241cSMarcel Moolenaar pc = addr; 619bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 620bcc5241cSMarcel Moolenaar } 62172d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6227d0c6c9fSSam Leffler gdb_listening = 1; 62372d44f31SMarcel Moolenaar return (1); 62472d44f31SMarcel Moolenaar } 62572d44f31SMarcel Moolenaar case 'C': { /* Continue with signal. */ 62672d44f31SMarcel Moolenaar uintmax_t addr, sig; 627bcc5241cSMarcel Moolenaar register_t pc; 62872d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 629bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 630bcc5241cSMarcel Moolenaar pc = addr; 631bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 632bcc5241cSMarcel Moolenaar } 63372d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6347d0c6c9fSSam Leffler gdb_listening = 1; 63572d44f31SMarcel Moolenaar return (1); 63672d44f31SMarcel Moolenaar } 637d412b2deSPeter Grehan case 'D': { /* Detach */ 638d412b2deSPeter Grehan gdb_tx_ok(); 639d412b2deSPeter Grehan kdb_cpu_clear_singlestep(); 640d412b2deSPeter Grehan return (1); 641d412b2deSPeter Grehan } 64272d44f31SMarcel Moolenaar case 'g': { /* Read registers. */ 64372d44f31SMarcel Moolenaar size_t r; 64472d44f31SMarcel Moolenaar gdb_tx_begin(0); 64572d44f31SMarcel Moolenaar for (r = 0; r < GDB_NREGS; r++) 64672d44f31SMarcel Moolenaar gdb_tx_reg(r); 64772d44f31SMarcel Moolenaar gdb_tx_end(); 64872d44f31SMarcel Moolenaar break; 64972d44f31SMarcel Moolenaar } 65072d44f31SMarcel Moolenaar case 'G': /* Write registers. */ 65172d44f31SMarcel Moolenaar gdb_tx_err(0); 65272d44f31SMarcel Moolenaar break; 65372d44f31SMarcel Moolenaar case 'H': { /* Set thread. */ 65472d44f31SMarcel Moolenaar intmax_t tid; 65572d44f31SMarcel Moolenaar struct thread *thr; 656*5df6fa43SConrad Meyer 657*5df6fa43SConrad Meyer /* Ignore 'g' (general) or 'c' (continue) flag. */ 658*5df6fa43SConrad Meyer (void) gdb_rx_char(); 659*5df6fa43SConrad Meyer 6604af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 6614af77eceSSam Leffler gdb_tx_err(EINVAL); 6624af77eceSSam Leffler break; 6634af77eceSSam Leffler } 66472d44f31SMarcel Moolenaar if (tid > 0) { 66572d44f31SMarcel Moolenaar thr = kdb_thr_lookup(tid); 66672d44f31SMarcel Moolenaar if (thr == NULL) { 66772d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 66872d44f31SMarcel Moolenaar break; 66972d44f31SMarcel Moolenaar } 67072d44f31SMarcel Moolenaar kdb_thr_select(thr); 67172d44f31SMarcel Moolenaar } 67272d44f31SMarcel Moolenaar gdb_tx_ok(); 67372d44f31SMarcel Moolenaar break; 67472d44f31SMarcel Moolenaar } 67572d44f31SMarcel Moolenaar case 'k': /* Kill request. */ 67672d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 6777d0c6c9fSSam Leffler gdb_listening = 1; 67872d44f31SMarcel Moolenaar return (1); 67972d44f31SMarcel Moolenaar case 'm': { /* Read memory. */ 68072d44f31SMarcel Moolenaar uintmax_t addr, size; 68172d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 68272d44f31SMarcel Moolenaar gdb_rx_varhex(&size)) { 68372d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 68472d44f31SMarcel Moolenaar break; 68572d44f31SMarcel Moolenaar } 68672d44f31SMarcel Moolenaar gdb_tx_begin(0); 68772d44f31SMarcel Moolenaar if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 68872d44f31SMarcel Moolenaar gdb_tx_end(); 68972d44f31SMarcel Moolenaar else 69072d44f31SMarcel Moolenaar gdb_tx_err(EIO); 69172d44f31SMarcel Moolenaar break; 69272d44f31SMarcel Moolenaar } 69372d44f31SMarcel Moolenaar case 'M': { /* Write memory. */ 69472d44f31SMarcel Moolenaar uintmax_t addr, size; 69572d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 69672d44f31SMarcel Moolenaar gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 69772d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 69872d44f31SMarcel Moolenaar break; 69972d44f31SMarcel Moolenaar } 70072d44f31SMarcel Moolenaar if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 70172d44f31SMarcel Moolenaar gdb_tx_err(EIO); 70272d44f31SMarcel Moolenaar else 70372d44f31SMarcel Moolenaar gdb_tx_ok(); 70472d44f31SMarcel Moolenaar break; 70572d44f31SMarcel Moolenaar } 70672d44f31SMarcel Moolenaar case 'P': { /* Write register. */ 707bcc5241cSMarcel Moolenaar char *val; 708bcc5241cSMarcel Moolenaar uintmax_t reg; 709bcc5241cSMarcel Moolenaar val = gdb_rxp; 71072d44f31SMarcel Moolenaar if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 711bcc5241cSMarcel Moolenaar !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 71272d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 71372d44f31SMarcel Moolenaar break; 71472d44f31SMarcel Moolenaar } 71572d44f31SMarcel Moolenaar gdb_cpu_setreg(reg, val); 71672d44f31SMarcel Moolenaar gdb_tx_ok(); 71772d44f31SMarcel Moolenaar break; 71872d44f31SMarcel Moolenaar } 71972d44f31SMarcel Moolenaar case 'q': /* General query. */ 7203bbe6e64SConrad Meyer if (gdb_rx_equal("C")) { 7213bbe6e64SConrad Meyer gdb_tx_begin('Q'); 7223bbe6e64SConrad Meyer gdb_tx_char('C'); 7233bbe6e64SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 7243bbe6e64SConrad Meyer gdb_tx_end(); 725c4fbbfaaSConrad Meyer } else if (gdb_rx_equal("Supported")) { 726c4fbbfaaSConrad Meyer gdb_do_qsupported(&host_features); 7273bbe6e64SConrad Meyer } else if (gdb_rx_equal("fThreadInfo")) { 72872d44f31SMarcel Moolenaar thr_iter = kdb_thr_first(); 729130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 73072d44f31SMarcel Moolenaar } else if (gdb_rx_equal("sThreadInfo")) { 731130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 7325555afa1SConrad Meyer } else if (gdb_rx_equal("Xfer:")) { 7335555afa1SConrad Meyer do_qXfer(); 73427ecc2adSBenno Rice } else if (gdb_rx_equal("Search:memory:")) { 7358a7a6571SRyan Libby gdb_do_mem_search(); 73672d44f31SMarcel Moolenaar } else if (!gdb_cpu_query()) 73772d44f31SMarcel Moolenaar gdb_tx_empty(); 73872d44f31SMarcel Moolenaar break; 73972d44f31SMarcel Moolenaar case 's': { /* Step. */ 74072d44f31SMarcel Moolenaar uintmax_t addr; 741bcc5241cSMarcel Moolenaar register_t pc; 742bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 743bcc5241cSMarcel Moolenaar pc = addr; 744bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 745bcc5241cSMarcel Moolenaar } 74672d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 7477d0c6c9fSSam Leffler gdb_listening = 1; 74872d44f31SMarcel Moolenaar return (1); 74972d44f31SMarcel Moolenaar } 75072d44f31SMarcel Moolenaar case 'S': { /* Step with signal. */ 75172d44f31SMarcel Moolenaar uintmax_t addr, sig; 752bcc5241cSMarcel Moolenaar register_t pc; 75372d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 754bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 755bcc5241cSMarcel Moolenaar pc = addr; 756bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 757bcc5241cSMarcel Moolenaar } 75872d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 7597d0c6c9fSSam Leffler gdb_listening = 1; 76072d44f31SMarcel Moolenaar return (1); 76172d44f31SMarcel Moolenaar } 76272d44f31SMarcel Moolenaar case 'T': { /* Thread alive. */ 76372d44f31SMarcel Moolenaar intmax_t tid; 7644af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 7654af77eceSSam Leffler gdb_tx_err(EINVAL); 7664af77eceSSam Leffler break; 7674af77eceSSam Leffler } 76872d44f31SMarcel Moolenaar if (kdb_thr_lookup(tid) != NULL) 76972d44f31SMarcel Moolenaar gdb_tx_ok(); 77072d44f31SMarcel Moolenaar else 77172d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 77272d44f31SMarcel Moolenaar break; 77372d44f31SMarcel Moolenaar } 774*5df6fa43SConrad Meyer case EOF: 77572d44f31SMarcel Moolenaar /* Empty command. Treat as unknown command. */ 77672d44f31SMarcel Moolenaar /* FALLTHROUGH */ 77772d44f31SMarcel Moolenaar default: 77872d44f31SMarcel Moolenaar /* Unknown command. Send empty response. */ 77972d44f31SMarcel Moolenaar gdb_tx_empty(); 78072d44f31SMarcel Moolenaar break; 78172d44f31SMarcel Moolenaar } 78272d44f31SMarcel Moolenaar } 7833a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 78472d44f31SMarcel Moolenaar return (0); 78572d44f31SMarcel Moolenaar } 786