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 477029da5cSPawel Biernacki SYSCTL_NODE(_debug, OID_AUTO, gdb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 487029da5cSPawel Biernacki "GDB settings"); 4910f6c05cSConrad Meyer 5072d44f31SMarcel Moolenaar static dbbe_init_f gdb_init; 5172d44f31SMarcel Moolenaar static dbbe_trap_f gdb_trap; 5272d44f31SMarcel Moolenaar 5328926c57SJohn Baldwin KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap); 5472d44f31SMarcel Moolenaar 55e8d86c0eSPoul-Henning Kamp static struct gdb_dbgport null_gdb_dbgport; 56e8d86c0eSPoul-Henning Kamp DATA_SET(gdb_dbgport_set, null_gdb_dbgport); 5772d44f31SMarcel Moolenaar SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 5872d44f31SMarcel Moolenaar 5972d44f31SMarcel Moolenaar struct gdb_dbgport *gdb_cur = NULL; 607d0c6c9fSSam Leffler int gdb_listening = 0; 616310546dSConrad Meyer bool gdb_ackmode = true; 6272d44f31SMarcel Moolenaar 6327ecc2adSBenno Rice static unsigned char gdb_bindata[64]; 6427ecc2adSBenno Rice 65dda17b36SConrad Meyer #ifdef DDB 66dda17b36SConrad Meyer bool gdb_return_to_ddb = false; 67dda17b36SConrad Meyer #endif 68dda17b36SConrad Meyer 6972d44f31SMarcel Moolenaar static int 7072d44f31SMarcel Moolenaar gdb_init(void) 7172d44f31SMarcel Moolenaar { 7272d44f31SMarcel Moolenaar struct gdb_dbgport *dp, **iter; 7372d44f31SMarcel Moolenaar int cur_pri, pri; 7472d44f31SMarcel Moolenaar 7572d44f31SMarcel Moolenaar gdb_cur = NULL; 7672d44f31SMarcel Moolenaar cur_pri = -1; 7772d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 7872d44f31SMarcel Moolenaar dp = *iter; 7972d44f31SMarcel Moolenaar pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 8072d44f31SMarcel Moolenaar dp->gdb_active = (pri >= 0) ? 0 : -1; 8172d44f31SMarcel Moolenaar if (pri > cur_pri) { 8272d44f31SMarcel Moolenaar cur_pri = pri; 8372d44f31SMarcel Moolenaar gdb_cur = dp; 8472d44f31SMarcel Moolenaar } 8572d44f31SMarcel Moolenaar } 8672d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 8772d44f31SMarcel Moolenaar printf("GDB: debug ports:"); 8872d44f31SMarcel Moolenaar SET_FOREACH(iter, gdb_dbgport_set) { 8972d44f31SMarcel Moolenaar dp = *iter; 9072d44f31SMarcel Moolenaar if (dp->gdb_active == 0) 9172d44f31SMarcel Moolenaar printf(" %s", dp->gdb_name); 9272d44f31SMarcel Moolenaar } 9372d44f31SMarcel Moolenaar printf("\n"); 9472d44f31SMarcel Moolenaar } else 9572d44f31SMarcel Moolenaar printf("GDB: no debug ports present\n"); 9672d44f31SMarcel Moolenaar if (gdb_cur != NULL) { 9772d44f31SMarcel Moolenaar gdb_cur->gdb_init(); 9872d44f31SMarcel Moolenaar printf("GDB: current port: %s\n", gdb_cur->gdb_name); 9972d44f31SMarcel Moolenaar } 1007d0c6c9fSSam Leffler if (gdb_cur != NULL) { 10172d44f31SMarcel Moolenaar cur_pri = (boothowto & RB_GDB) ? 2 : 0; 1027d0c6c9fSSam Leffler gdb_consinit(); 1037d0c6c9fSSam Leffler } else 10472d44f31SMarcel Moolenaar cur_pri = -1; 10572d44f31SMarcel Moolenaar return (cur_pri); 10672d44f31SMarcel Moolenaar } 10772d44f31SMarcel Moolenaar 1088a7a6571SRyan Libby static void 1098a7a6571SRyan Libby gdb_do_mem_search(void) 1108a7a6571SRyan Libby { 1118a7a6571SRyan Libby size_t patlen; 1128a7a6571SRyan Libby intmax_t addr, size; 1138a7a6571SRyan Libby const unsigned char *found; 1148a7a6571SRyan Libby 1158a7a6571SRyan Libby if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' || 1168a7a6571SRyan Libby gdb_rx_varhex(&size) || gdb_rx_char() != ';' || 1178a7a6571SRyan Libby gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) { 1188a7a6571SRyan Libby gdb_tx_err(EINVAL); 1198a7a6571SRyan Libby return; 1208a7a6571SRyan Libby } 1218a7a6571SRyan Libby if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, 1228a7a6571SRyan Libby patlen, &found)) { 1238a7a6571SRyan Libby if (found == 0ULL) 1248a7a6571SRyan Libby gdb_tx_begin('0'); 1258a7a6571SRyan Libby else { 1268a7a6571SRyan Libby gdb_tx_begin('1'); 1278a7a6571SRyan Libby gdb_tx_char(','); 1288a7a6571SRyan Libby gdb_tx_hex((intmax_t)(uintptr_t)found, 8); 1298a7a6571SRyan Libby } 1308a7a6571SRyan Libby gdb_tx_end(); 1318a7a6571SRyan Libby } else 1328a7a6571SRyan Libby gdb_tx_err(EIO); 1338a7a6571SRyan Libby } 1348a7a6571SRyan Libby 135130ef1adSConrad Meyer static void 136130ef1adSConrad Meyer gdb_do_threadinfo(struct thread **thr_iter) 137130ef1adSConrad Meyer { 138130ef1adSConrad Meyer static struct thread * const done_sentinel = (void *)(uintptr_t)1; 139130ef1adSConrad Meyer static const size_t tidsz_hex = sizeof(lwpid_t) * 2; 140130ef1adSConrad Meyer size_t tds_sent; 141130ef1adSConrad Meyer 142130ef1adSConrad Meyer if (*thr_iter == NULL) { 143130ef1adSConrad Meyer gdb_tx_err(ENXIO); 144130ef1adSConrad Meyer return; 145130ef1adSConrad Meyer } 146130ef1adSConrad Meyer 147130ef1adSConrad Meyer if (*thr_iter == done_sentinel) { 148130ef1adSConrad Meyer gdb_tx_begin('l'); 149130ef1adSConrad Meyer *thr_iter = NULL; 150130ef1adSConrad Meyer goto sendit; 151130ef1adSConrad Meyer } 152130ef1adSConrad Meyer 153130ef1adSConrad Meyer gdb_tx_begin('m'); 154130ef1adSConrad Meyer 155130ef1adSConrad Meyer for (tds_sent = 0; 156130ef1adSConrad Meyer *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1); 157130ef1adSConrad Meyer *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) { 158130ef1adSConrad Meyer if (tds_sent > 0) 159130ef1adSConrad Meyer gdb_tx_char(','); 160130ef1adSConrad Meyer gdb_tx_varhex((*thr_iter)->td_tid); 161130ef1adSConrad Meyer } 162130ef1adSConrad Meyer 163130ef1adSConrad Meyer /* 164130ef1adSConrad Meyer * Can't send EOF and "some" in same packet, so set a sentinel to send 165130ef1adSConrad Meyer * EOF when GDB asks us next. 166130ef1adSConrad Meyer */ 167130ef1adSConrad Meyer if (*thr_iter == NULL && tds_sent > 0) 168130ef1adSConrad Meyer *thr_iter = done_sentinel; 169130ef1adSConrad Meyer 170130ef1adSConrad Meyer sendit: 171130ef1adSConrad Meyer gdb_tx_end(); 172130ef1adSConrad Meyer } 173130ef1adSConrad Meyer 174c4fbbfaaSConrad Meyer #define BIT(n) (1ull << (n)) 175c4fbbfaaSConrad Meyer enum { 176c4fbbfaaSConrad Meyer GDB_MULTIPROCESS, 177c4fbbfaaSConrad Meyer GDB_SWBREAK, 178c4fbbfaaSConrad Meyer GDB_HWBREAK, 179c4fbbfaaSConrad Meyer GDB_QRELOCINSN, 180c4fbbfaaSConrad Meyer GDB_FORK_EVENTS, 181c4fbbfaaSConrad Meyer GDB_VFORK_EVENTS, 182c4fbbfaaSConrad Meyer GDB_EXEC_EVENTS, 183c4fbbfaaSConrad Meyer GDB_VCONT_SUPPORTED, 184c4fbbfaaSConrad Meyer GDB_QTHREADEVENTS, 185c4fbbfaaSConrad Meyer GDB_NO_RESUMED, 186c4fbbfaaSConrad Meyer }; 187c4fbbfaaSConrad Meyer static const char * const gdb_feature_names[] = { 188c4fbbfaaSConrad Meyer [GDB_MULTIPROCESS] = "multiprocess", 189c4fbbfaaSConrad Meyer [GDB_SWBREAK] = "swbreak", 190c4fbbfaaSConrad Meyer [GDB_HWBREAK] = "hwbreak", 191c4fbbfaaSConrad Meyer [GDB_QRELOCINSN] = "qRelocInsn", 192c4fbbfaaSConrad Meyer [GDB_FORK_EVENTS] = "fork-events", 193c4fbbfaaSConrad Meyer [GDB_VFORK_EVENTS] = "vfork-events", 194c4fbbfaaSConrad Meyer [GDB_EXEC_EVENTS] = "exec-events", 195c4fbbfaaSConrad Meyer [GDB_VCONT_SUPPORTED] = "vContSupported", 196c4fbbfaaSConrad Meyer [GDB_QTHREADEVENTS] = "QThreadEvents", 197c4fbbfaaSConrad Meyer [GDB_NO_RESUMED] = "no-resumed", 198c4fbbfaaSConrad Meyer }; 199c4fbbfaaSConrad Meyer static void 200c4fbbfaaSConrad Meyer gdb_do_qsupported(uint32_t *feat) 201c4fbbfaaSConrad Meyer { 202c4fbbfaaSConrad Meyer char *tok, *delim, ok; 203c4fbbfaaSConrad Meyer size_t i, toklen; 204c4fbbfaaSConrad Meyer 205c4fbbfaaSConrad Meyer /* Parse supported host features */ 206c4fbbfaaSConrad Meyer *feat = 0; 2075af88677SConrad Meyer switch (gdb_rx_char()) { 2085af88677SConrad Meyer case ':': 2095af88677SConrad Meyer break; 2105af88677SConrad Meyer case EOF: 2115af88677SConrad Meyer goto nofeatures; 2125af88677SConrad Meyer default: 213c4fbbfaaSConrad Meyer goto error; 2145af88677SConrad Meyer } 215c4fbbfaaSConrad Meyer 216c4fbbfaaSConrad Meyer while (gdb_rxsz > 0) { 217c4fbbfaaSConrad Meyer tok = gdb_rxp; 218c4fbbfaaSConrad Meyer delim = strchrnul(gdb_rxp, ';'); 219c4fbbfaaSConrad Meyer toklen = (delim - tok); 220c4fbbfaaSConrad Meyer 221c4fbbfaaSConrad Meyer gdb_rxp += toklen; 222c4fbbfaaSConrad Meyer gdb_rxsz -= toklen; 223c4fbbfaaSConrad Meyer if (*delim != '\0') { 224c4fbbfaaSConrad Meyer *delim = '\0'; 225c4fbbfaaSConrad Meyer gdb_rxp += 1; 226c4fbbfaaSConrad Meyer gdb_rxsz -= 1; 227c4fbbfaaSConrad Meyer } 228c4fbbfaaSConrad Meyer 229c4fbbfaaSConrad Meyer if (toklen < 2) 230c4fbbfaaSConrad Meyer goto error; 231c4fbbfaaSConrad Meyer 232c4fbbfaaSConrad Meyer ok = tok[toklen - 1]; 233c4fbbfaaSConrad Meyer if (ok != '-' && ok != '+') { 234c4fbbfaaSConrad Meyer /* 235c4fbbfaaSConrad Meyer * GDB only has one KV-pair feature, and we don't 236c4fbbfaaSConrad Meyer * support it, so ignore and move on. 237c4fbbfaaSConrad Meyer */ 238c4fbbfaaSConrad Meyer if (strchr(tok, '=') != NULL) 239c4fbbfaaSConrad Meyer continue; 240c4fbbfaaSConrad Meyer /* Not a KV-pair, and not a +/- flag? Malformed. */ 241c4fbbfaaSConrad Meyer goto error; 242c4fbbfaaSConrad Meyer } 243c4fbbfaaSConrad Meyer if (ok != '+') 244c4fbbfaaSConrad Meyer continue; 245c4fbbfaaSConrad Meyer tok[toklen - 1] = '\0'; 246c4fbbfaaSConrad Meyer 247c4fbbfaaSConrad Meyer for (i = 0; i < nitems(gdb_feature_names); i++) 248c4fbbfaaSConrad Meyer if (strcmp(gdb_feature_names[i], tok) == 0) 249c4fbbfaaSConrad Meyer break; 250c4fbbfaaSConrad Meyer 251c4fbbfaaSConrad Meyer if (i == nitems(gdb_feature_names)) { 252c4fbbfaaSConrad Meyer /* Unknown GDB feature. */ 253c4fbbfaaSConrad Meyer continue; 254c4fbbfaaSConrad Meyer } 255c4fbbfaaSConrad Meyer 256c4fbbfaaSConrad Meyer *feat |= BIT(i); 257c4fbbfaaSConrad Meyer } 258c4fbbfaaSConrad Meyer 2595af88677SConrad Meyer nofeatures: 260c4fbbfaaSConrad Meyer /* Send a supported feature list back */ 261c4fbbfaaSConrad Meyer gdb_tx_begin(0); 262c4fbbfaaSConrad Meyer 263c4fbbfaaSConrad Meyer gdb_tx_str("PacketSize"); 264c4fbbfaaSConrad Meyer gdb_tx_char('='); 265c4fbbfaaSConrad Meyer /* 266c4fbbfaaSConrad Meyer * We don't buffer framing bytes, but we do need to retain a byte for a 267c4fbbfaaSConrad Meyer * trailing nul. 268c4fbbfaaSConrad Meyer */ 269c4fbbfaaSConrad Meyer gdb_tx_varhex(GDB_BUFSZ + strlen("$#nn") - 1); 270c4fbbfaaSConrad Meyer 2715555afa1SConrad Meyer gdb_tx_str(";qXfer:threads:read+"); 2725555afa1SConrad Meyer 273c4fbbfaaSConrad Meyer /* 2746310546dSConrad Meyer * If the debugport is a reliable transport, request No Ack mode from 2756310546dSConrad Meyer * the server. The server may or may not choose to enter No Ack mode. 2766310546dSConrad Meyer * https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html 2776310546dSConrad Meyer */ 2786310546dSConrad Meyer if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_RELIABLE) 2796310546dSConrad Meyer gdb_tx_str(";QStartNoAckMode+"); 2806310546dSConrad Meyer 2816310546dSConrad Meyer /* 282c4fbbfaaSConrad Meyer * Future consideration: 283c4fbbfaaSConrad Meyer * - vCont 284c4fbbfaaSConrad Meyer * - multiprocess 285c4fbbfaaSConrad Meyer */ 286c4fbbfaaSConrad Meyer gdb_tx_end(); 287c4fbbfaaSConrad Meyer return; 288c4fbbfaaSConrad Meyer 289c4fbbfaaSConrad Meyer error: 290c4fbbfaaSConrad Meyer *feat = 0; 291c4fbbfaaSConrad Meyer gdb_tx_err(EINVAL); 292c4fbbfaaSConrad Meyer } 293c4fbbfaaSConrad Meyer 2945555afa1SConrad Meyer /* 2955555afa1SConrad Meyer * A qXfer_context provides a vaguely generic way to generate a multi-packet 2965555afa1SConrad Meyer * response on the fly, making some assumptions about the size of sbuf writes 2975555afa1SConrad Meyer * vs actual packet length constraints. A non-byzantine gdb host should allow 2985555afa1SConrad Meyer * hundreds of bytes per packet or more. 2995555afa1SConrad Meyer * 3005555afa1SConrad Meyer * Upper layers are considered responsible for escaping the four forbidden 3015555afa1SConrad Meyer * characters '# $ } *'. 3025555afa1SConrad Meyer */ 3035555afa1SConrad Meyer struct qXfer_context { 3045555afa1SConrad Meyer struct sbuf sb; 3055555afa1SConrad Meyer size_t last_offset; 3065555afa1SConrad Meyer bool flushed; 3075555afa1SConrad Meyer bool lastmessage; 3085555afa1SConrad Meyer char xfer_buf[GDB_BUFSZ]; 3095555afa1SConrad Meyer }; 3105555afa1SConrad Meyer 3115555afa1SConrad Meyer static int 3125555afa1SConrad Meyer qXfer_drain(void *v, const char *buf, int len) 3135555afa1SConrad Meyer { 3145555afa1SConrad Meyer struct qXfer_context *qx; 3155555afa1SConrad Meyer 3165555afa1SConrad Meyer if (len < 0) 3175555afa1SConrad Meyer return (-EINVAL); 3185555afa1SConrad Meyer 3195555afa1SConrad Meyer qx = v; 3205555afa1SConrad Meyer if (qx->flushed) { 3215555afa1SConrad Meyer /* 3225555afa1SConrad Meyer * Overflow. We lost some message. Maybe the packet size is 3235555afa1SConrad Meyer * ridiculously small. 3245555afa1SConrad Meyer */ 3255555afa1SConrad Meyer printf("%s: Overflow in qXfer detected.\n", __func__); 3265555afa1SConrad Meyer return (-ENOBUFS); 3275555afa1SConrad Meyer } 3285555afa1SConrad Meyer 3295555afa1SConrad Meyer qx->last_offset += len; 3305555afa1SConrad Meyer qx->flushed = true; 3315555afa1SConrad Meyer 3325555afa1SConrad Meyer if (qx->lastmessage) 3335555afa1SConrad Meyer gdb_tx_begin('l'); 3345555afa1SConrad Meyer else 3355555afa1SConrad Meyer gdb_tx_begin('m'); 3365555afa1SConrad Meyer 3375555afa1SConrad Meyer memcpy(gdb_txp, buf, len); 3385555afa1SConrad Meyer gdb_txp += len; 3395555afa1SConrad Meyer 3405555afa1SConrad Meyer gdb_tx_end(); 3415555afa1SConrad Meyer return (len); 3425555afa1SConrad Meyer } 3435555afa1SConrad Meyer 3445555afa1SConrad Meyer static int 3455555afa1SConrad Meyer init_qXfer_ctx(struct qXfer_context *qx, uintmax_t len) 3465555afa1SConrad Meyer { 3475555afa1SConrad Meyer 3485555afa1SConrad Meyer /* Protocol (max) length field includes framing overhead. */ 3495555afa1SConrad Meyer if (len < sizeof("$m#nn")) 3505555afa1SConrad Meyer return (ENOSPC); 3515555afa1SConrad Meyer 3525555afa1SConrad Meyer len -= 4; 3535555afa1SConrad Meyer len = ummin(len, GDB_BUFSZ - 1); 3545555afa1SConrad Meyer 3555555afa1SConrad Meyer qx->last_offset = 0; 3565555afa1SConrad Meyer qx->flushed = false; 3575555afa1SConrad Meyer qx->lastmessage = false; 3585555afa1SConrad Meyer sbuf_new(&qx->sb, qx->xfer_buf, len, SBUF_FIXEDLEN); 3595555afa1SConrad Meyer sbuf_set_drain(&qx->sb, qXfer_drain, qx); 3605555afa1SConrad Meyer return (0); 3615555afa1SConrad Meyer } 3625555afa1SConrad Meyer 3635555afa1SConrad Meyer /* 364a91812f6SConrad Meyer * Squashes special XML and GDB characters down to _. Sorry. 3655555afa1SConrad Meyer */ 3665555afa1SConrad Meyer static void 3675555afa1SConrad Meyer qXfer_escape_xmlattr_str(char *dst, size_t dstlen, const char *src) 3685555afa1SConrad Meyer { 3695555afa1SConrad Meyer static const char *forbidden = "#$}*"; 3705555afa1SConrad Meyer 3715555afa1SConrad Meyer size_t i; 3725555afa1SConrad Meyer char c; 3735555afa1SConrad Meyer 3745555afa1SConrad Meyer for (i = 0; i < dstlen - 1 && *src != 0; src++, i++) { 3755555afa1SConrad Meyer c = *src; 3765555afa1SConrad Meyer /* XML attr filter */ 3775555afa1SConrad Meyer if (c < 32) 3785555afa1SConrad Meyer c = '_'; 3795555afa1SConrad Meyer /* We assume attributes will be "" quoted. */ 3805555afa1SConrad Meyer if (c == '<' || c == '&' || c == '"') 3815555afa1SConrad Meyer c = '_'; 3825555afa1SConrad Meyer 3835555afa1SConrad Meyer /* GDB escape. */ 3845555afa1SConrad Meyer if (strchr(forbidden, c) != NULL) { 385a91812f6SConrad Meyer /* 386a91812f6SConrad Meyer * It would be nice to escape these properly, but to do 387a91812f6SConrad Meyer * it correctly we need to escape them in the transmit 388a91812f6SConrad Meyer * layer, potentially doubling our buffer requirements. 389a91812f6SConrad Meyer * For now, avoid breaking the protocol by squashing 390a91812f6SConrad Meyer * them to underscore. 391a91812f6SConrad Meyer */ 392a91812f6SConrad Meyer #if 0 3935555afa1SConrad Meyer *dst++ = '}'; 3945555afa1SConrad Meyer c ^= 0x20; 395a91812f6SConrad Meyer #endif 396a91812f6SConrad Meyer c = '_'; 3975555afa1SConrad Meyer } 3985555afa1SConrad Meyer *dst++ = c; 3995555afa1SConrad Meyer } 4005555afa1SConrad Meyer if (*src != 0) 4015555afa1SConrad Meyer printf("XXX%s: overflow; API misuse\n", __func__); 4025555afa1SConrad Meyer 4035555afa1SConrad Meyer *dst = 0; 4045555afa1SConrad Meyer } 4055555afa1SConrad Meyer 4065555afa1SConrad Meyer /* 4075555afa1SConrad Meyer * Dynamically generate qXfer:threads document, one packet at a time. 4085555afa1SConrad Meyer * 4095555afa1SConrad Meyer * The format is loosely described[0], although it does not seem that the 4105555afa1SConrad Meyer * <?xml?> mentioned on that page is required. 4115555afa1SConrad Meyer * 4125555afa1SConrad Meyer * [0]: https://sourceware.org/gdb/current/onlinedocs/gdb/Thread-List-Format.html 4135555afa1SConrad Meyer */ 4145555afa1SConrad Meyer static void 4155555afa1SConrad Meyer do_qXfer_threads_read(void) 4165555afa1SConrad Meyer { 4175555afa1SConrad Meyer /* Kludgy context */ 4185555afa1SConrad Meyer static struct { 4195555afa1SConrad Meyer struct qXfer_context qXfer; 4205555afa1SConrad Meyer /* Kludgy state machine */ 4215555afa1SConrad Meyer struct thread *iter; 4225555afa1SConrad Meyer enum { 4235555afa1SConrad Meyer XML_START_THREAD, /* '<thread' */ 4245555afa1SConrad Meyer XML_THREAD_ID, /* ' id="xxx"' */ 4255555afa1SConrad Meyer XML_THREAD_CORE, /* ' core="yyy"' */ 4265555afa1SConrad Meyer XML_THREAD_NAME, /* ' name="zzz"' */ 4275555afa1SConrad Meyer XML_THREAD_EXTRA, /* '> ...' */ 4285555afa1SConrad Meyer XML_END_THREAD, /* '</thread>' */ 4295555afa1SConrad Meyer XML_SENT_END_THREADS, /* '</threads>' */ 4305555afa1SConrad Meyer } next_step; 4315555afa1SConrad Meyer } ctx; 4325555afa1SConrad Meyer static char td_name_escape[MAXCOMLEN * 2 + 1]; 4335555afa1SConrad Meyer 4345555afa1SConrad Meyer const char *name_src; 4355555afa1SConrad Meyer uintmax_t offset, len; 4365555afa1SConrad Meyer int error; 4375555afa1SConrad Meyer 4385555afa1SConrad Meyer /* Annex part must be empty. */ 4395555afa1SConrad Meyer if (gdb_rx_char() != ':') 4405555afa1SConrad Meyer goto misformed_request; 4415555afa1SConrad Meyer 4425555afa1SConrad Meyer if (gdb_rx_varhex(&offset) != 0 || 4435555afa1SConrad Meyer gdb_rx_char() != ',' || 4445555afa1SConrad Meyer gdb_rx_varhex(&len) != 0) 4455555afa1SConrad Meyer goto misformed_request; 4465555afa1SConrad Meyer 4475555afa1SConrad Meyer /* 4485555afa1SConrad Meyer * Validate resume xfers. 4495555afa1SConrad Meyer */ 4505555afa1SConrad Meyer if (offset != 0) { 4515555afa1SConrad Meyer if (offset != ctx.qXfer.last_offset) { 452acef7371SConrad Meyer printf("%s: Resumed offset %ju != expected %zu\n", 4535555afa1SConrad Meyer __func__, offset, ctx.qXfer.last_offset); 4545555afa1SConrad Meyer error = ESPIPE; 4555555afa1SConrad Meyer goto request_error; 4565555afa1SConrad Meyer } 4575555afa1SConrad Meyer ctx.qXfer.flushed = false; 4585555afa1SConrad Meyer } 4595555afa1SConrad Meyer 4605555afa1SConrad Meyer if (offset == 0) { 4615555afa1SConrad Meyer ctx.iter = kdb_thr_first(); 4625555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 4635555afa1SConrad Meyer error = init_qXfer_ctx(&ctx.qXfer, len); 4645555afa1SConrad Meyer if (error != 0) 4655555afa1SConrad Meyer goto request_error; 4665555afa1SConrad Meyer 4675555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<threads>"); 4685555afa1SConrad Meyer } 4695555afa1SConrad Meyer 4705555afa1SConrad Meyer while (!ctx.qXfer.flushed && ctx.iter != NULL) { 4715555afa1SConrad Meyer switch (ctx.next_step) { 4725555afa1SConrad Meyer case XML_START_THREAD: 4735555afa1SConrad Meyer ctx.next_step = XML_THREAD_ID; 4745555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "<thread"); 4755555afa1SConrad Meyer continue; 4765555afa1SConrad Meyer 4775555afa1SConrad Meyer case XML_THREAD_ID: 4785555afa1SConrad Meyer ctx.next_step = XML_THREAD_CORE; 4795555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " id=\"%jx\"", 4805555afa1SConrad Meyer (uintmax_t)ctx.iter->td_tid); 4815555afa1SConrad Meyer continue; 4825555afa1SConrad Meyer 4835555afa1SConrad Meyer case XML_THREAD_CORE: 4845555afa1SConrad Meyer ctx.next_step = XML_THREAD_NAME; 4855555afa1SConrad Meyer if (ctx.iter->td_oncpu != NOCPU) { 4865555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " core=\"%d\"", 4875555afa1SConrad Meyer ctx.iter->td_oncpu); 4885555afa1SConrad Meyer } 4895555afa1SConrad Meyer continue; 4905555afa1SConrad Meyer 4915555afa1SConrad Meyer case XML_THREAD_NAME: 4925555afa1SConrad Meyer ctx.next_step = XML_THREAD_EXTRA; 4935555afa1SConrad Meyer 4945555afa1SConrad Meyer if (ctx.iter->td_name[0] != 0) 4955555afa1SConrad Meyer name_src = ctx.iter->td_name; 4965555afa1SConrad Meyer else if (ctx.iter->td_proc != NULL && 4975555afa1SConrad Meyer ctx.iter->td_proc->p_comm[0] != 0) 4985555afa1SConrad Meyer name_src = ctx.iter->td_proc->p_comm; 4995555afa1SConrad Meyer else 5005555afa1SConrad Meyer continue; 5015555afa1SConrad Meyer 5025555afa1SConrad Meyer qXfer_escape_xmlattr_str(td_name_escape, 5035555afa1SConrad Meyer sizeof(td_name_escape), name_src); 5045555afa1SConrad Meyer sbuf_printf(&ctx.qXfer.sb, " name=\"%s\"", 5055555afa1SConrad Meyer td_name_escape); 5065555afa1SConrad Meyer continue; 5075555afa1SConrad Meyer 5085555afa1SConrad Meyer case XML_THREAD_EXTRA: 5095555afa1SConrad Meyer ctx.next_step = XML_END_THREAD; 5105555afa1SConrad Meyer 5115555afa1SConrad Meyer sbuf_putc(&ctx.qXfer.sb, '>'); 5125555afa1SConrad Meyer 513fa2528acSAlex Richardson if (TD_GET_STATE(ctx.iter) == TDS_RUNNING) 5145555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Running"); 515fa2528acSAlex Richardson else if (TD_GET_STATE(ctx.iter) == TDS_RUNQ) 5165555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "RunQ"); 517fa2528acSAlex Richardson else if (TD_GET_STATE(ctx.iter) == TDS_CAN_RUN) 5185555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "CanRun"); 5195555afa1SConrad Meyer else if (TD_ON_LOCK(ctx.iter)) 5205555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Blocked"); 5215555afa1SConrad Meyer else if (TD_IS_SLEEPING(ctx.iter)) 5225555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Sleeping"); 5235555afa1SConrad Meyer else if (TD_IS_SWAPPED(ctx.iter)) 5245555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Swapped"); 5255555afa1SConrad Meyer else if (TD_AWAITING_INTR(ctx.iter)) 5265555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "IthreadWait"); 5275555afa1SConrad Meyer else if (TD_IS_SUSPENDED(ctx.iter)) 5285555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "Suspended"); 5295555afa1SConrad Meyer else 5305555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "???"); 5315555afa1SConrad Meyer continue; 5325555afa1SConrad Meyer 5335555afa1SConrad Meyer case XML_END_THREAD: 5345555afa1SConrad Meyer ctx.next_step = XML_START_THREAD; 5355555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</thread>"); 5365555afa1SConrad Meyer ctx.iter = kdb_thr_next(ctx.iter); 5375555afa1SConrad Meyer continue; 5385555afa1SConrad Meyer 5395555afa1SConrad Meyer /* 5405555afa1SConrad Meyer * This one isn't part of the looping state machine, 5415555afa1SConrad Meyer * but GCC complains if you leave an enum value out of the 5425555afa1SConrad Meyer * select. 5435555afa1SConrad Meyer */ 5445555afa1SConrad Meyer case XML_SENT_END_THREADS: 5455555afa1SConrad Meyer /* NOTREACHED */ 5465555afa1SConrad Meyer break; 5475555afa1SConrad Meyer } 5485555afa1SConrad Meyer } 5495555afa1SConrad Meyer if (ctx.qXfer.flushed) 5505555afa1SConrad Meyer return; 5515555afa1SConrad Meyer 5525555afa1SConrad Meyer if (ctx.next_step != XML_SENT_END_THREADS) { 5535555afa1SConrad Meyer ctx.next_step = XML_SENT_END_THREADS; 5545555afa1SConrad Meyer sbuf_cat(&ctx.qXfer.sb, "</threads>"); 5555555afa1SConrad Meyer } 5565555afa1SConrad Meyer if (ctx.qXfer.flushed) 5575555afa1SConrad Meyer return; 5585555afa1SConrad Meyer 5595555afa1SConrad Meyer ctx.qXfer.lastmessage = true; 5605555afa1SConrad Meyer sbuf_finish(&ctx.qXfer.sb); 5615555afa1SConrad Meyer sbuf_delete(&ctx.qXfer.sb); 5625555afa1SConrad Meyer ctx.qXfer.last_offset = 0; 5635555afa1SConrad Meyer return; 5645555afa1SConrad Meyer 5655555afa1SConrad Meyer misformed_request: 5665555afa1SConrad Meyer /* 5675555afa1SConrad Meyer * GDB "General-Query-Packets.html" qXfer-read anchor specifically 5685555afa1SConrad Meyer * documents an E00 code for malformed requests or invalid annex. 5695555afa1SConrad Meyer * Non-zero codes indicate invalid offset or "error reading the data." 5705555afa1SConrad Meyer */ 5715555afa1SConrad Meyer error = 0; 5725555afa1SConrad Meyer request_error: 5735555afa1SConrad Meyer gdb_tx_err(error); 5745555afa1SConrad Meyer return; 5755555afa1SConrad Meyer } 5765555afa1SConrad Meyer 5775555afa1SConrad Meyer /* 5785555afa1SConrad Meyer * A set of standardized transfers from "special data areas." 5795555afa1SConrad Meyer * 5805555afa1SConrad Meyer * We've already matched on "qXfer:" and advanced the rx packet buffer past 5815555afa1SConrad Meyer * that bit. Parse out the rest of the packet and generate an appropriate 5825555afa1SConrad Meyer * response. 5835555afa1SConrad Meyer */ 5845555afa1SConrad Meyer static void 5855555afa1SConrad Meyer do_qXfer(void) 5865555afa1SConrad Meyer { 5875555afa1SConrad Meyer if (!gdb_rx_equal("threads:")) 5885555afa1SConrad Meyer goto unrecognized; 5895555afa1SConrad Meyer 5905555afa1SConrad Meyer if (!gdb_rx_equal("read:")) 5915555afa1SConrad Meyer goto unrecognized; 5925555afa1SConrad Meyer 5935555afa1SConrad Meyer do_qXfer_threads_read(); 5945555afa1SConrad Meyer return; 5955555afa1SConrad Meyer 5965555afa1SConrad Meyer unrecognized: 5975555afa1SConrad Meyer gdb_tx_empty(); 5985555afa1SConrad Meyer return; 5995555afa1SConrad Meyer } 6005555afa1SConrad Meyer 601dda17b36SConrad Meyer static void 602dda17b36SConrad Meyer gdb_handle_detach(void) 603dda17b36SConrad Meyer { 604dda17b36SConrad Meyer kdb_cpu_clear_singlestep(); 605dda17b36SConrad Meyer gdb_listening = 0; 606dda17b36SConrad Meyer 607dda17b36SConrad Meyer if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM) 608dda17b36SConrad Meyer gdb_cur->gdb_term(); 609dda17b36SConrad Meyer 610dda17b36SConrad Meyer #ifdef DDB 611dda17b36SConrad Meyer if (!gdb_return_to_ddb) 612dda17b36SConrad Meyer return; 613dda17b36SConrad Meyer 614dda17b36SConrad Meyer gdb_return_to_ddb = false; 615dda17b36SConrad Meyer 616dda17b36SConrad Meyer if (kdb_dbbe_select("ddb") != 0) 617dda17b36SConrad Meyer printf("The ddb backend could not be selected.\n"); 618dda17b36SConrad Meyer #endif 619dda17b36SConrad Meyer } 620dda17b36SConrad Meyer 6214beb3858SMitchell Horne /* 6224beb3858SMitchell Horne * Handle a 'Z' packet: set a breakpoint or watchpoint. 6234beb3858SMitchell Horne * 6244beb3858SMitchell Horne * Currently, only watchpoints are supported. 6254beb3858SMitchell Horne */ 6264beb3858SMitchell Horne static void 6274beb3858SMitchell Horne gdb_z_insert(void) 6284beb3858SMitchell Horne { 6294beb3858SMitchell Horne intmax_t addr, length; 6304beb3858SMitchell Horne char ztype; 6314beb3858SMitchell Horne int error; 6324beb3858SMitchell Horne 6334beb3858SMitchell Horne ztype = gdb_rx_char(); 6344beb3858SMitchell Horne if (gdb_rx_char() != ',' || gdb_rx_varhex(&addr) || 6354beb3858SMitchell Horne gdb_rx_char() != ',' || gdb_rx_varhex(&length)) { 6364beb3858SMitchell Horne error = EINVAL; 6374beb3858SMitchell Horne goto fail; 6384beb3858SMitchell Horne } 6394beb3858SMitchell Horne 6404beb3858SMitchell Horne switch (ztype) { 6414beb3858SMitchell Horne case '2': /* write watchpoint */ 6424beb3858SMitchell Horne error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 6434beb3858SMitchell Horne (vm_size_t)length, KDB_DBG_ACCESS_W); 6444beb3858SMitchell Horne break; 6454beb3858SMitchell Horne case '3': /* read watchpoint */ 6464beb3858SMitchell Horne error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 6474beb3858SMitchell Horne (vm_size_t)length, KDB_DBG_ACCESS_R); 6484beb3858SMitchell Horne break; 6494beb3858SMitchell Horne case '4': /* access (RW) watchpoint */ 6504beb3858SMitchell Horne error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 6514beb3858SMitchell Horne (vm_size_t)length, KDB_DBG_ACCESS_RW); 6524beb3858SMitchell Horne break; 6534beb3858SMitchell Horne case '1': /* hardware breakpoint */ 6544beb3858SMitchell Horne case '0': /* software breakpoint */ 6554beb3858SMitchell Horne /* Not implemented. */ 6564beb3858SMitchell Horne gdb_tx_empty(); 6574beb3858SMitchell Horne return; 6584beb3858SMitchell Horne default: 6594beb3858SMitchell Horne error = EINVAL; 6604beb3858SMitchell Horne break; 6614beb3858SMitchell Horne } 6624beb3858SMitchell Horne if (error != 0) 6634beb3858SMitchell Horne goto fail; 6644beb3858SMitchell Horne gdb_tx_ok(); 6654beb3858SMitchell Horne return; 6664beb3858SMitchell Horne fail: 6674beb3858SMitchell Horne gdb_tx_err(error); 6684beb3858SMitchell Horne return; 6694beb3858SMitchell Horne } 6704beb3858SMitchell Horne 6714beb3858SMitchell Horne /* 6724beb3858SMitchell Horne * Handle a 'z' packet; clear a breakpoint or watchpoint. 6734beb3858SMitchell Horne * 6744beb3858SMitchell Horne * Currently, only watchpoints are supported. 6754beb3858SMitchell Horne */ 6764beb3858SMitchell Horne static void 6774beb3858SMitchell Horne gdb_z_remove(void) 6784beb3858SMitchell Horne { 6794beb3858SMitchell Horne intmax_t addr, length; 6804beb3858SMitchell Horne char ztype; 6814beb3858SMitchell Horne int error; 6824beb3858SMitchell Horne 6834beb3858SMitchell Horne ztype = gdb_rx_char(); 6844beb3858SMitchell Horne if (gdb_rx_char() != ',' || gdb_rx_varhex(&addr) || 6854beb3858SMitchell Horne gdb_rx_char() != ',' || gdb_rx_varhex(&length)) { 6864beb3858SMitchell Horne error = EINVAL; 6874beb3858SMitchell Horne goto fail; 6884beb3858SMitchell Horne } 6894beb3858SMitchell Horne 6904beb3858SMitchell Horne switch (ztype) { 6914beb3858SMitchell Horne case '2': /* write watchpoint */ 6924beb3858SMitchell Horne case '3': /* read watchpoint */ 6934beb3858SMitchell Horne case '4': /* access (RW) watchpoint */ 6944beb3858SMitchell Horne error = kdb_cpu_clr_watchpoint((vm_offset_t)addr, 6954beb3858SMitchell Horne (vm_size_t)length); 6964beb3858SMitchell Horne break; 6974beb3858SMitchell Horne case '1': /* hardware breakpoint */ 6984beb3858SMitchell Horne case '0': /* software breakpoint */ 6994beb3858SMitchell Horne /* Not implemented. */ 7004beb3858SMitchell Horne gdb_tx_empty(); 7014beb3858SMitchell Horne return; 7024beb3858SMitchell Horne default: 7034beb3858SMitchell Horne error = EINVAL; 7044beb3858SMitchell Horne break; 7054beb3858SMitchell Horne } 7064beb3858SMitchell Horne if (error != 0) 7074beb3858SMitchell Horne goto fail; 7084beb3858SMitchell Horne gdb_tx_ok(); 7094beb3858SMitchell Horne return; 7104beb3858SMitchell Horne fail: 7114beb3858SMitchell Horne gdb_tx_err(error); 7124beb3858SMitchell Horne return; 7134beb3858SMitchell Horne } 7144beb3858SMitchell Horne 71572d44f31SMarcel Moolenaar static int 71672d44f31SMarcel Moolenaar gdb_trap(int type, int code) 71772d44f31SMarcel Moolenaar { 7183a5d3671SMatthew D Fleming jmp_buf jb; 71972d44f31SMarcel Moolenaar struct thread *thr_iter; 7203a5d3671SMatthew D Fleming void *prev_jb; 721c4fbbfaaSConrad Meyer uint32_t host_features; 7223a5d3671SMatthew D Fleming 7233a5d3671SMatthew D Fleming prev_jb = kdb_jmpbuf(jb); 7243a5d3671SMatthew D Fleming if (setjmp(jb) != 0) { 7253a5d3671SMatthew D Fleming printf("%s bailing, hopefully back to ddb!\n", __func__); 7263a5d3671SMatthew D Fleming gdb_listening = 0; 7273a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 7283a5d3671SMatthew D Fleming return (1); 7293a5d3671SMatthew D Fleming } 73072d44f31SMarcel Moolenaar 7317d0c6c9fSSam Leffler gdb_listening = 0; 7326310546dSConrad Meyer gdb_ackmode = true; 7336310546dSConrad Meyer 73472d44f31SMarcel Moolenaar /* 73572d44f31SMarcel Moolenaar * Send a T packet. We currently do not support watchpoints (the 73672d44f31SMarcel Moolenaar * awatch, rwatch or watch elements). 73772d44f31SMarcel Moolenaar */ 73872d44f31SMarcel Moolenaar gdb_tx_begin('T'); 73972d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 74072d44f31SMarcel Moolenaar gdb_tx_varhex(GDB_REG_PC); 74172d44f31SMarcel Moolenaar gdb_tx_char(':'); 74272d44f31SMarcel Moolenaar gdb_tx_reg(GDB_REG_PC); 74372d44f31SMarcel Moolenaar gdb_tx_char(';'); 744*7446b088SMitchell Horne gdb_cpu_stop_reason(type, code); 74572d44f31SMarcel Moolenaar gdb_tx_str("thread:"); 746*7446b088SMitchell Horne gdb_tx_varhex((uintmax_t)kdb_thread->td_tid); 74772d44f31SMarcel Moolenaar gdb_tx_char(';'); 74872d44f31SMarcel Moolenaar gdb_tx_end(); /* XXX check error condition. */ 74972d44f31SMarcel Moolenaar 75072d44f31SMarcel Moolenaar thr_iter = NULL; 75172d44f31SMarcel Moolenaar while (gdb_rx_begin() == 0) { 75203e62bf3SMarcel Moolenaar /* printf("GDB: got '%s'\n", gdb_rxp); */ 75372d44f31SMarcel Moolenaar switch (gdb_rx_char()) { 75472d44f31SMarcel Moolenaar case '?': /* Last signal. */ 75576c8c090SConrad Meyer gdb_tx_begin('T'); 75672d44f31SMarcel Moolenaar gdb_tx_hex(gdb_cpu_signal(type, code), 2); 75776c8c090SConrad Meyer gdb_tx_str("thread:"); 75876c8c090SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 75976c8c090SConrad Meyer gdb_tx_char(';'); 76072d44f31SMarcel Moolenaar gdb_tx_end(); 76172d44f31SMarcel Moolenaar break; 76272d44f31SMarcel Moolenaar case 'c': { /* Continue. */ 76372d44f31SMarcel Moolenaar uintmax_t addr; 764bcc5241cSMarcel Moolenaar register_t pc; 765bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 766bcc5241cSMarcel Moolenaar pc = addr; 767bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 768bcc5241cSMarcel Moolenaar } 76972d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 7707d0c6c9fSSam Leffler gdb_listening = 1; 77172d44f31SMarcel Moolenaar return (1); 77272d44f31SMarcel Moolenaar } 77372d44f31SMarcel Moolenaar case 'C': { /* Continue with signal. */ 77472d44f31SMarcel Moolenaar uintmax_t addr, sig; 775bcc5241cSMarcel Moolenaar register_t pc; 77672d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 777bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 778bcc5241cSMarcel Moolenaar pc = addr; 779bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 780bcc5241cSMarcel Moolenaar } 78172d44f31SMarcel Moolenaar kdb_cpu_clear_singlestep(); 7827d0c6c9fSSam Leffler gdb_listening = 1; 78372d44f31SMarcel Moolenaar return (1); 78472d44f31SMarcel Moolenaar } 785d412b2deSPeter Grehan case 'D': { /* Detach */ 786d412b2deSPeter Grehan gdb_tx_ok(); 787dda17b36SConrad Meyer gdb_handle_detach(); 788d412b2deSPeter Grehan return (1); 789d412b2deSPeter Grehan } 79072d44f31SMarcel Moolenaar case 'g': { /* Read registers. */ 79172d44f31SMarcel Moolenaar size_t r; 79272d44f31SMarcel Moolenaar gdb_tx_begin(0); 79372d44f31SMarcel Moolenaar for (r = 0; r < GDB_NREGS; r++) 79472d44f31SMarcel Moolenaar gdb_tx_reg(r); 79572d44f31SMarcel Moolenaar gdb_tx_end(); 79672d44f31SMarcel Moolenaar break; 79772d44f31SMarcel Moolenaar } 7983f3cc995SMitchell Horne case 'G': { /* Write registers. */ 7993f3cc995SMitchell Horne char *val; 8003f3cc995SMitchell Horne bool success; 8013f3cc995SMitchell Horne size_t r; 8023f3cc995SMitchell Horne for (success = true, r = 0; r < GDB_NREGS; r++) { 8033f3cc995SMitchell Horne val = gdb_rxp; 8043f3cc995SMitchell Horne if (!gdb_rx_mem(val, gdb_cpu_regsz(r))) { 8053f3cc995SMitchell Horne gdb_tx_err(EINVAL); 8063f3cc995SMitchell Horne success = false; 80772d44f31SMarcel Moolenaar break; 8083f3cc995SMitchell Horne } 8093f3cc995SMitchell Horne gdb_cpu_setreg(r, val); 8103f3cc995SMitchell Horne } 8113f3cc995SMitchell Horne if (success) 8123f3cc995SMitchell Horne gdb_tx_ok(); 8133f3cc995SMitchell Horne break; 8143f3cc995SMitchell Horne } 81572d44f31SMarcel Moolenaar case 'H': { /* Set thread. */ 81672d44f31SMarcel Moolenaar intmax_t tid; 81772d44f31SMarcel Moolenaar struct thread *thr; 8185df6fa43SConrad Meyer 8195df6fa43SConrad Meyer /* Ignore 'g' (general) or 'c' (continue) flag. */ 8205df6fa43SConrad Meyer (void) gdb_rx_char(); 8215df6fa43SConrad Meyer 8224af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 8234af77eceSSam Leffler gdb_tx_err(EINVAL); 8244af77eceSSam Leffler break; 8254af77eceSSam Leffler } 82672d44f31SMarcel Moolenaar if (tid > 0) { 82772d44f31SMarcel Moolenaar thr = kdb_thr_lookup(tid); 82872d44f31SMarcel Moolenaar if (thr == NULL) { 82972d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 83072d44f31SMarcel Moolenaar break; 83172d44f31SMarcel Moolenaar } 83272d44f31SMarcel Moolenaar kdb_thr_select(thr); 83372d44f31SMarcel Moolenaar } 83472d44f31SMarcel Moolenaar gdb_tx_ok(); 83572d44f31SMarcel Moolenaar break; 83672d44f31SMarcel Moolenaar } 83772d44f31SMarcel Moolenaar case 'k': /* Kill request. */ 838dda17b36SConrad Meyer gdb_handle_detach(); 83972d44f31SMarcel Moolenaar return (1); 84072d44f31SMarcel Moolenaar case 'm': { /* Read memory. */ 84172d44f31SMarcel Moolenaar uintmax_t addr, size; 84272d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 84372d44f31SMarcel Moolenaar gdb_rx_varhex(&size)) { 84472d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 84572d44f31SMarcel Moolenaar break; 84672d44f31SMarcel Moolenaar } 84772d44f31SMarcel Moolenaar gdb_tx_begin(0); 84872d44f31SMarcel Moolenaar if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 84972d44f31SMarcel Moolenaar gdb_tx_end(); 85072d44f31SMarcel Moolenaar else 85172d44f31SMarcel Moolenaar gdb_tx_err(EIO); 85272d44f31SMarcel Moolenaar break; 85372d44f31SMarcel Moolenaar } 85472d44f31SMarcel Moolenaar case 'M': { /* Write memory. */ 85572d44f31SMarcel Moolenaar uintmax_t addr, size; 85672d44f31SMarcel Moolenaar if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 85772d44f31SMarcel Moolenaar gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 85872d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 85972d44f31SMarcel Moolenaar break; 86072d44f31SMarcel Moolenaar } 86172d44f31SMarcel Moolenaar if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 86272d44f31SMarcel Moolenaar gdb_tx_err(EIO); 86372d44f31SMarcel Moolenaar else 86472d44f31SMarcel Moolenaar gdb_tx_ok(); 86572d44f31SMarcel Moolenaar break; 86672d44f31SMarcel Moolenaar } 867fd29833dSMitchell Horne case 'p': { /* Read register. */ 868fd29833dSMitchell Horne uintmax_t reg; 869fd29833dSMitchell Horne if (gdb_rx_varhex(®)) { 870fd29833dSMitchell Horne gdb_tx_err(EINVAL); 871fd29833dSMitchell Horne break; 872fd29833dSMitchell Horne } 873fd29833dSMitchell Horne gdb_tx_begin(0); 874fd29833dSMitchell Horne gdb_tx_reg(reg); 875fd29833dSMitchell Horne gdb_tx_end(); 876fd29833dSMitchell Horne break; 877fd29833dSMitchell Horne } 87872d44f31SMarcel Moolenaar case 'P': { /* Write register. */ 879bcc5241cSMarcel Moolenaar char *val; 880bcc5241cSMarcel Moolenaar uintmax_t reg; 881bcc5241cSMarcel Moolenaar val = gdb_rxp; 88272d44f31SMarcel Moolenaar if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 883bcc5241cSMarcel Moolenaar !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 88472d44f31SMarcel Moolenaar gdb_tx_err(EINVAL); 88572d44f31SMarcel Moolenaar break; 88672d44f31SMarcel Moolenaar } 88772d44f31SMarcel Moolenaar gdb_cpu_setreg(reg, val); 88872d44f31SMarcel Moolenaar gdb_tx_ok(); 88972d44f31SMarcel Moolenaar break; 89072d44f31SMarcel Moolenaar } 89172d44f31SMarcel Moolenaar case 'q': /* General query. */ 8923bbe6e64SConrad Meyer if (gdb_rx_equal("C")) { 8933bbe6e64SConrad Meyer gdb_tx_begin('Q'); 8943bbe6e64SConrad Meyer gdb_tx_char('C'); 8953bbe6e64SConrad Meyer gdb_tx_varhex((long)kdb_thread->td_tid); 8963bbe6e64SConrad Meyer gdb_tx_end(); 897c4fbbfaaSConrad Meyer } else if (gdb_rx_equal("Supported")) { 898c4fbbfaaSConrad Meyer gdb_do_qsupported(&host_features); 8993bbe6e64SConrad Meyer } else if (gdb_rx_equal("fThreadInfo")) { 90072d44f31SMarcel Moolenaar thr_iter = kdb_thr_first(); 901130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 90272d44f31SMarcel Moolenaar } else if (gdb_rx_equal("sThreadInfo")) { 903130ef1adSConrad Meyer gdb_do_threadinfo(&thr_iter); 9045555afa1SConrad Meyer } else if (gdb_rx_equal("Xfer:")) { 9055555afa1SConrad Meyer do_qXfer(); 90627ecc2adSBenno Rice } else if (gdb_rx_equal("Search:memory:")) { 9078a7a6571SRyan Libby gdb_do_mem_search(); 908fa76c6f9SLeandro Lupori #ifdef __powerpc__ 909fa76c6f9SLeandro Lupori } else if (gdb_rx_equal("Offsets")) { 910fa76c6f9SLeandro Lupori gdb_cpu_do_offsets(); 911fa76c6f9SLeandro Lupori #endif 91272d44f31SMarcel Moolenaar } else if (!gdb_cpu_query()) 91372d44f31SMarcel Moolenaar gdb_tx_empty(); 91472d44f31SMarcel Moolenaar break; 9156310546dSConrad Meyer case 'Q': 9166310546dSConrad Meyer if (gdb_rx_equal("StartNoAckMode")) { 9176310546dSConrad Meyer if ((gdb_cur->gdb_dbfeatures & 9186310546dSConrad Meyer GDB_DBGP_FEAT_RELIABLE) == 0) { 9196310546dSConrad Meyer /* 9206310546dSConrad Meyer * Shouldn't happen if we didn't 9216310546dSConrad Meyer * advertise support. Reject. 9226310546dSConrad Meyer */ 9236310546dSConrad Meyer gdb_tx_empty(); 9246310546dSConrad Meyer break; 9256310546dSConrad Meyer } 9266310546dSConrad Meyer gdb_ackmode = false; 9276310546dSConrad Meyer gdb_tx_ok(); 9286310546dSConrad Meyer } else 9296310546dSConrad Meyer gdb_tx_empty(); 9306310546dSConrad Meyer break; 93172d44f31SMarcel Moolenaar case 's': { /* Step. */ 93272d44f31SMarcel Moolenaar uintmax_t addr; 933bcc5241cSMarcel Moolenaar register_t pc; 934bcc5241cSMarcel Moolenaar if (!gdb_rx_varhex(&addr)) { 935bcc5241cSMarcel Moolenaar pc = addr; 936bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 937bcc5241cSMarcel Moolenaar } 93872d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 9397d0c6c9fSSam Leffler gdb_listening = 1; 94072d44f31SMarcel Moolenaar return (1); 94172d44f31SMarcel Moolenaar } 94272d44f31SMarcel Moolenaar case 'S': { /* Step with signal. */ 94372d44f31SMarcel Moolenaar uintmax_t addr, sig; 944bcc5241cSMarcel Moolenaar register_t pc; 94572d44f31SMarcel Moolenaar if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 946bcc5241cSMarcel Moolenaar !gdb_rx_varhex(&addr)) { 947bcc5241cSMarcel Moolenaar pc = addr; 948bcc5241cSMarcel Moolenaar gdb_cpu_setreg(GDB_REG_PC, &pc); 949bcc5241cSMarcel Moolenaar } 95072d44f31SMarcel Moolenaar kdb_cpu_set_singlestep(); 9517d0c6c9fSSam Leffler gdb_listening = 1; 95272d44f31SMarcel Moolenaar return (1); 95372d44f31SMarcel Moolenaar } 95472d44f31SMarcel Moolenaar case 'T': { /* Thread alive. */ 95572d44f31SMarcel Moolenaar intmax_t tid; 9564af77eceSSam Leffler if (gdb_rx_varhex(&tid)) { 9574af77eceSSam Leffler gdb_tx_err(EINVAL); 9584af77eceSSam Leffler break; 9594af77eceSSam Leffler } 96072d44f31SMarcel Moolenaar if (kdb_thr_lookup(tid) != NULL) 96172d44f31SMarcel Moolenaar gdb_tx_ok(); 96272d44f31SMarcel Moolenaar else 96372d44f31SMarcel Moolenaar gdb_tx_err(ENOENT); 96472d44f31SMarcel Moolenaar break; 96572d44f31SMarcel Moolenaar } 9664beb3858SMitchell Horne case 'z': { /* Remove watchpoint. */ 9674beb3858SMitchell Horne gdb_z_remove(); 9684beb3858SMitchell Horne break; 9694beb3858SMitchell Horne } 9704beb3858SMitchell Horne case 'Z': { /* Set watchpoint. */ 9714beb3858SMitchell Horne gdb_z_insert(); 9724beb3858SMitchell Horne break; 9734beb3858SMitchell Horne } 9745df6fa43SConrad Meyer case EOF: 97572d44f31SMarcel Moolenaar /* Empty command. Treat as unknown command. */ 97672d44f31SMarcel Moolenaar /* FALLTHROUGH */ 97772d44f31SMarcel Moolenaar default: 97872d44f31SMarcel Moolenaar /* Unknown command. Send empty response. */ 97972d44f31SMarcel Moolenaar gdb_tx_empty(); 98072d44f31SMarcel Moolenaar break; 98172d44f31SMarcel Moolenaar } 98272d44f31SMarcel Moolenaar } 9833a5d3671SMatthew D Fleming (void)kdb_jmpbuf(prev_jb); 98472d44f31SMarcel Moolenaar return (0); 98572d44f31SMarcel Moolenaar } 986