1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kdb.h> 35 #include <sys/kernel.h> 36 #include <sys/pcpu.h> 37 #include <sys/proc.h> 38 #include <sys/reboot.h> 39 #include <sys/sbuf.h> 40 41 #include <machine/gdb_machdep.h> 42 #include <machine/kdb.h> 43 44 #include <gdb/gdb.h> 45 #include <gdb/gdb_int.h> 46 47 SYSCTL_NODE(_debug, OID_AUTO, gdb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 48 "GDB settings"); 49 50 static dbbe_init_f gdb_init; 51 static dbbe_trap_f gdb_trap; 52 53 KDB_BACKEND(gdb, gdb_init, NULL, NULL, gdb_trap); 54 55 static struct gdb_dbgport null_gdb_dbgport; 56 DATA_SET(gdb_dbgport_set, null_gdb_dbgport); 57 SET_DECLARE(gdb_dbgport_set, struct gdb_dbgport); 58 59 struct gdb_dbgport *gdb_cur = NULL; 60 int gdb_listening = 0; 61 bool gdb_ackmode = true; 62 63 static unsigned char gdb_bindata[64]; 64 65 #ifdef DDB 66 bool gdb_return_to_ddb = false; 67 #endif 68 69 static int 70 gdb_init(void) 71 { 72 struct gdb_dbgport *dp, **iter; 73 int cur_pri, pri; 74 75 gdb_cur = NULL; 76 cur_pri = -1; 77 SET_FOREACH(iter, gdb_dbgport_set) { 78 dp = *iter; 79 pri = (dp->gdb_probe != NULL) ? dp->gdb_probe() : -1; 80 dp->gdb_active = (pri >= 0) ? 0 : -1; 81 if (pri > cur_pri) { 82 cur_pri = pri; 83 gdb_cur = dp; 84 } 85 } 86 if (gdb_cur != NULL) { 87 printf("GDB: debug ports:"); 88 SET_FOREACH(iter, gdb_dbgport_set) { 89 dp = *iter; 90 if (dp->gdb_active == 0) 91 printf(" %s", dp->gdb_name); 92 } 93 printf("\n"); 94 } else 95 printf("GDB: no debug ports present\n"); 96 if (gdb_cur != NULL) { 97 gdb_cur->gdb_init(); 98 printf("GDB: current port: %s\n", gdb_cur->gdb_name); 99 } 100 if (gdb_cur != NULL) { 101 cur_pri = (boothowto & RB_GDB) ? 2 : 0; 102 gdb_consinit(); 103 } else 104 cur_pri = -1; 105 return (cur_pri); 106 } 107 108 static void 109 gdb_do_mem_search(void) 110 { 111 size_t patlen; 112 intmax_t addr, size; 113 const unsigned char *found; 114 115 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ';' || 116 gdb_rx_varhex(&size) || gdb_rx_char() != ';' || 117 gdb_rx_bindata(gdb_bindata, sizeof(gdb_bindata), &patlen)) { 118 gdb_tx_err(EINVAL); 119 return; 120 } 121 if (gdb_search_mem((char *)(uintptr_t)addr, size, gdb_bindata, 122 patlen, &found)) { 123 if (found == 0ULL) 124 gdb_tx_begin('0'); 125 else { 126 gdb_tx_begin('1'); 127 gdb_tx_char(','); 128 gdb_tx_hex((intmax_t)(uintptr_t)found, 8); 129 } 130 gdb_tx_end(); 131 } else 132 gdb_tx_err(EIO); 133 } 134 135 static void 136 gdb_do_threadinfo(struct thread **thr_iter) 137 { 138 static struct thread * const done_sentinel = (void *)(uintptr_t)1; 139 static const size_t tidsz_hex = sizeof(lwpid_t) * 2; 140 size_t tds_sent; 141 142 if (*thr_iter == NULL) { 143 gdb_tx_err(ENXIO); 144 return; 145 } 146 147 if (*thr_iter == done_sentinel) { 148 gdb_tx_begin('l'); 149 *thr_iter = NULL; 150 goto sendit; 151 } 152 153 gdb_tx_begin('m'); 154 155 for (tds_sent = 0; 156 *thr_iter != NULL && gdb_txbuf_has_capacity(tidsz_hex + 1); 157 *thr_iter = kdb_thr_next(*thr_iter), tds_sent++) { 158 if (tds_sent > 0) 159 gdb_tx_char(','); 160 gdb_tx_varhex((*thr_iter)->td_tid); 161 } 162 163 /* 164 * Can't send EOF and "some" in same packet, so set a sentinel to send 165 * EOF when GDB asks us next. 166 */ 167 if (*thr_iter == NULL && tds_sent > 0) 168 *thr_iter = done_sentinel; 169 170 sendit: 171 gdb_tx_end(); 172 } 173 174 #define BIT(n) (1ull << (n)) 175 enum { 176 GDB_MULTIPROCESS, 177 GDB_SWBREAK, 178 GDB_HWBREAK, 179 GDB_QRELOCINSN, 180 GDB_FORK_EVENTS, 181 GDB_VFORK_EVENTS, 182 GDB_EXEC_EVENTS, 183 GDB_VCONT_SUPPORTED, 184 GDB_QTHREADEVENTS, 185 GDB_NO_RESUMED, 186 }; 187 static const char * const gdb_feature_names[] = { 188 [GDB_MULTIPROCESS] = "multiprocess", 189 [GDB_SWBREAK] = "swbreak", 190 [GDB_HWBREAK] = "hwbreak", 191 [GDB_QRELOCINSN] = "qRelocInsn", 192 [GDB_FORK_EVENTS] = "fork-events", 193 [GDB_VFORK_EVENTS] = "vfork-events", 194 [GDB_EXEC_EVENTS] = "exec-events", 195 [GDB_VCONT_SUPPORTED] = "vContSupported", 196 [GDB_QTHREADEVENTS] = "QThreadEvents", 197 [GDB_NO_RESUMED] = "no-resumed", 198 }; 199 static void 200 gdb_do_qsupported(uint32_t *feat) 201 { 202 char *tok, *delim, ok; 203 size_t i, toklen; 204 205 /* Parse supported host features */ 206 *feat = 0; 207 switch (gdb_rx_char()) { 208 case ':': 209 break; 210 case EOF: 211 goto nofeatures; 212 default: 213 goto error; 214 } 215 216 while (gdb_rxsz > 0) { 217 tok = gdb_rxp; 218 delim = strchrnul(gdb_rxp, ';'); 219 toklen = (delim - tok); 220 221 gdb_rxp += toklen; 222 gdb_rxsz -= toklen; 223 if (*delim != '\0') { 224 *delim = '\0'; 225 gdb_rxp += 1; 226 gdb_rxsz -= 1; 227 } 228 229 if (toklen < 2) 230 goto error; 231 232 ok = tok[toklen - 1]; 233 if (ok != '-' && ok != '+') { 234 /* 235 * GDB only has one KV-pair feature, and we don't 236 * support it, so ignore and move on. 237 */ 238 if (strchr(tok, '=') != NULL) 239 continue; 240 /* Not a KV-pair, and not a +/- flag? Malformed. */ 241 goto error; 242 } 243 if (ok != '+') 244 continue; 245 tok[toklen - 1] = '\0'; 246 247 for (i = 0; i < nitems(gdb_feature_names); i++) 248 if (strcmp(gdb_feature_names[i], tok) == 0) 249 break; 250 251 if (i == nitems(gdb_feature_names)) { 252 /* Unknown GDB feature. */ 253 continue; 254 } 255 256 *feat |= BIT(i); 257 } 258 259 nofeatures: 260 /* Send a supported feature list back */ 261 gdb_tx_begin(0); 262 263 gdb_tx_str("PacketSize"); 264 gdb_tx_char('='); 265 /* 266 * We don't buffer framing bytes, but we do need to retain a byte for a 267 * trailing nul. 268 */ 269 gdb_tx_varhex(GDB_BUFSZ + strlen("$#nn") - 1); 270 271 gdb_tx_str(";qXfer:threads:read+"); 272 273 /* 274 * If the debugport is a reliable transport, request No Ack mode from 275 * the server. The server may or may not choose to enter No Ack mode. 276 * https://sourceware.org/gdb/onlinedocs/gdb/Packet-Acknowledgment.html 277 */ 278 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_RELIABLE) 279 gdb_tx_str(";QStartNoAckMode+"); 280 281 /* 282 * Future consideration: 283 * - vCont 284 * - multiprocess 285 */ 286 gdb_tx_end(); 287 return; 288 289 error: 290 *feat = 0; 291 gdb_tx_err(EINVAL); 292 } 293 294 /* 295 * A qXfer_context provides a vaguely generic way to generate a multi-packet 296 * response on the fly, making some assumptions about the size of sbuf writes 297 * vs actual packet length constraints. A non-byzantine gdb host should allow 298 * hundreds of bytes per packet or more. 299 * 300 * Upper layers are considered responsible for escaping the four forbidden 301 * characters '# $ } *'. 302 */ 303 struct qXfer_context { 304 struct sbuf sb; 305 size_t last_offset; 306 bool flushed; 307 bool lastmessage; 308 char xfer_buf[GDB_BUFSZ]; 309 }; 310 311 static int 312 qXfer_drain(void *v, const char *buf, int len) 313 { 314 struct qXfer_context *qx; 315 316 if (len < 0) 317 return (-EINVAL); 318 319 qx = v; 320 if (qx->flushed) { 321 /* 322 * Overflow. We lost some message. Maybe the packet size is 323 * ridiculously small. 324 */ 325 printf("%s: Overflow in qXfer detected.\n", __func__); 326 return (-ENOBUFS); 327 } 328 329 qx->last_offset += len; 330 qx->flushed = true; 331 332 if (qx->lastmessage) 333 gdb_tx_begin('l'); 334 else 335 gdb_tx_begin('m'); 336 337 memcpy(gdb_txp, buf, len); 338 gdb_txp += len; 339 340 gdb_tx_end(); 341 return (len); 342 } 343 344 static int 345 init_qXfer_ctx(struct qXfer_context *qx, uintmax_t len) 346 { 347 348 /* Protocol (max) length field includes framing overhead. */ 349 if (len < sizeof("$m#nn")) 350 return (ENOSPC); 351 352 len -= 4; 353 len = ummin(len, GDB_BUFSZ - 1); 354 355 qx->last_offset = 0; 356 qx->flushed = false; 357 qx->lastmessage = false; 358 sbuf_new(&qx->sb, qx->xfer_buf, len, SBUF_FIXEDLEN); 359 sbuf_set_drain(&qx->sb, qXfer_drain, qx); 360 return (0); 361 } 362 363 /* 364 * Squashes special XML and GDB characters down to _. Sorry. 365 */ 366 static void 367 qXfer_escape_xmlattr_str(char *dst, size_t dstlen, const char *src) 368 { 369 static const char *forbidden = "#$}*"; 370 371 size_t i; 372 char c; 373 374 for (i = 0; i < dstlen - 1 && *src != 0; src++, i++) { 375 c = *src; 376 /* XML attr filter */ 377 if (c < 32) 378 c = '_'; 379 /* We assume attributes will be "" quoted. */ 380 if (c == '<' || c == '&' || c == '"') 381 c = '_'; 382 383 /* GDB escape. */ 384 if (strchr(forbidden, c) != NULL) { 385 /* 386 * It would be nice to escape these properly, but to do 387 * it correctly we need to escape them in the transmit 388 * layer, potentially doubling our buffer requirements. 389 * For now, avoid breaking the protocol by squashing 390 * them to underscore. 391 */ 392 #if 0 393 *dst++ = '}'; 394 c ^= 0x20; 395 #endif 396 c = '_'; 397 } 398 *dst++ = c; 399 } 400 if (*src != 0) 401 printf("XXX%s: overflow; API misuse\n", __func__); 402 403 *dst = 0; 404 } 405 406 /* 407 * Dynamically generate qXfer:threads document, one packet at a time. 408 * 409 * The format is loosely described[0], although it does not seem that the 410 * <?xml?> mentioned on that page is required. 411 * 412 * [0]: https://sourceware.org/gdb/current/onlinedocs/gdb/Thread-List-Format.html 413 */ 414 static void 415 do_qXfer_threads_read(void) 416 { 417 /* Kludgy context */ 418 static struct { 419 struct qXfer_context qXfer; 420 /* Kludgy state machine */ 421 struct thread *iter; 422 enum { 423 XML_START_THREAD, /* '<thread' */ 424 XML_THREAD_ID, /* ' id="xxx"' */ 425 XML_THREAD_CORE, /* ' core="yyy"' */ 426 XML_THREAD_NAME, /* ' name="zzz"' */ 427 XML_THREAD_EXTRA, /* '> ...' */ 428 XML_END_THREAD, /* '</thread>' */ 429 XML_SENT_END_THREADS, /* '</threads>' */ 430 } next_step; 431 } ctx; 432 static char td_name_escape[MAXCOMLEN * 2 + 1]; 433 434 const char *name_src; 435 uintmax_t offset, len; 436 int error; 437 438 /* Annex part must be empty. */ 439 if (gdb_rx_char() != ':') 440 goto misformed_request; 441 442 if (gdb_rx_varhex(&offset) != 0 || 443 gdb_rx_char() != ',' || 444 gdb_rx_varhex(&len) != 0) 445 goto misformed_request; 446 447 /* 448 * Validate resume xfers. 449 */ 450 if (offset != 0) { 451 if (offset != ctx.qXfer.last_offset) { 452 printf("%s: Resumed offset %ju != expected %zu\n", 453 __func__, offset, ctx.qXfer.last_offset); 454 error = ESPIPE; 455 goto request_error; 456 } 457 ctx.qXfer.flushed = false; 458 } 459 460 if (offset == 0) { 461 ctx.iter = kdb_thr_first(); 462 ctx.next_step = XML_START_THREAD; 463 error = init_qXfer_ctx(&ctx.qXfer, len); 464 if (error != 0) 465 goto request_error; 466 467 sbuf_cat(&ctx.qXfer.sb, "<threads>"); 468 } 469 470 while (!ctx.qXfer.flushed && ctx.iter != NULL) { 471 switch (ctx.next_step) { 472 case XML_START_THREAD: 473 ctx.next_step = XML_THREAD_ID; 474 sbuf_cat(&ctx.qXfer.sb, "<thread"); 475 continue; 476 477 case XML_THREAD_ID: 478 ctx.next_step = XML_THREAD_CORE; 479 sbuf_printf(&ctx.qXfer.sb, " id=\"%jx\"", 480 (uintmax_t)ctx.iter->td_tid); 481 continue; 482 483 case XML_THREAD_CORE: 484 ctx.next_step = XML_THREAD_NAME; 485 if (ctx.iter->td_oncpu != NOCPU) { 486 sbuf_printf(&ctx.qXfer.sb, " core=\"%d\"", 487 ctx.iter->td_oncpu); 488 } 489 continue; 490 491 case XML_THREAD_NAME: 492 ctx.next_step = XML_THREAD_EXTRA; 493 494 if (ctx.iter->td_name[0] != 0) 495 name_src = ctx.iter->td_name; 496 else if (ctx.iter->td_proc != NULL && 497 ctx.iter->td_proc->p_comm[0] != 0) 498 name_src = ctx.iter->td_proc->p_comm; 499 else 500 continue; 501 502 qXfer_escape_xmlattr_str(td_name_escape, 503 sizeof(td_name_escape), name_src); 504 sbuf_printf(&ctx.qXfer.sb, " name=\"%s\"", 505 td_name_escape); 506 continue; 507 508 case XML_THREAD_EXTRA: 509 ctx.next_step = XML_END_THREAD; 510 511 sbuf_putc(&ctx.qXfer.sb, '>'); 512 513 if (TD_GET_STATE(ctx.iter) == TDS_RUNNING) 514 sbuf_cat(&ctx.qXfer.sb, "Running"); 515 else if (TD_GET_STATE(ctx.iter) == TDS_RUNQ) 516 sbuf_cat(&ctx.qXfer.sb, "RunQ"); 517 else if (TD_GET_STATE(ctx.iter) == TDS_CAN_RUN) 518 sbuf_cat(&ctx.qXfer.sb, "CanRun"); 519 else if (TD_ON_LOCK(ctx.iter)) 520 sbuf_cat(&ctx.qXfer.sb, "Blocked"); 521 else if (TD_IS_SLEEPING(ctx.iter)) 522 sbuf_cat(&ctx.qXfer.sb, "Sleeping"); 523 else if (TD_IS_SWAPPED(ctx.iter)) 524 sbuf_cat(&ctx.qXfer.sb, "Swapped"); 525 else if (TD_AWAITING_INTR(ctx.iter)) 526 sbuf_cat(&ctx.qXfer.sb, "IthreadWait"); 527 else if (TD_IS_SUSPENDED(ctx.iter)) 528 sbuf_cat(&ctx.qXfer.sb, "Suspended"); 529 else 530 sbuf_cat(&ctx.qXfer.sb, "???"); 531 continue; 532 533 case XML_END_THREAD: 534 ctx.next_step = XML_START_THREAD; 535 sbuf_cat(&ctx.qXfer.sb, "</thread>"); 536 ctx.iter = kdb_thr_next(ctx.iter); 537 continue; 538 539 /* 540 * This one isn't part of the looping state machine, 541 * but GCC complains if you leave an enum value out of the 542 * select. 543 */ 544 case XML_SENT_END_THREADS: 545 /* NOTREACHED */ 546 break; 547 } 548 } 549 if (ctx.qXfer.flushed) 550 return; 551 552 if (ctx.next_step != XML_SENT_END_THREADS) { 553 ctx.next_step = XML_SENT_END_THREADS; 554 sbuf_cat(&ctx.qXfer.sb, "</threads>"); 555 } 556 if (ctx.qXfer.flushed) 557 return; 558 559 ctx.qXfer.lastmessage = true; 560 sbuf_finish(&ctx.qXfer.sb); 561 sbuf_delete(&ctx.qXfer.sb); 562 ctx.qXfer.last_offset = 0; 563 return; 564 565 misformed_request: 566 /* 567 * GDB "General-Query-Packets.html" qXfer-read anchor specifically 568 * documents an E00 code for malformed requests or invalid annex. 569 * Non-zero codes indicate invalid offset or "error reading the data." 570 */ 571 error = 0; 572 request_error: 573 gdb_tx_err(error); 574 return; 575 } 576 577 /* 578 * A set of standardized transfers from "special data areas." 579 * 580 * We've already matched on "qXfer:" and advanced the rx packet buffer past 581 * that bit. Parse out the rest of the packet and generate an appropriate 582 * response. 583 */ 584 static void 585 do_qXfer(void) 586 { 587 if (!gdb_rx_equal("threads:")) 588 goto unrecognized; 589 590 if (!gdb_rx_equal("read:")) 591 goto unrecognized; 592 593 do_qXfer_threads_read(); 594 return; 595 596 unrecognized: 597 gdb_tx_empty(); 598 return; 599 } 600 601 static void 602 gdb_handle_detach(void) 603 { 604 kdb_cpu_clear_singlestep(); 605 gdb_listening = 0; 606 607 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM) 608 gdb_cur->gdb_term(); 609 610 #ifdef DDB 611 if (!gdb_return_to_ddb) 612 return; 613 614 gdb_return_to_ddb = false; 615 616 if (kdb_dbbe_select("ddb") != 0) 617 printf("The ddb backend could not be selected.\n"); 618 #endif 619 } 620 621 static int 622 gdb_trap(int type, int code) 623 { 624 jmp_buf jb; 625 struct thread *thr_iter; 626 void *prev_jb; 627 uint32_t host_features; 628 629 prev_jb = kdb_jmpbuf(jb); 630 if (setjmp(jb) != 0) { 631 printf("%s bailing, hopefully back to ddb!\n", __func__); 632 gdb_listening = 0; 633 (void)kdb_jmpbuf(prev_jb); 634 return (1); 635 } 636 637 gdb_listening = 0; 638 gdb_ackmode = true; 639 640 /* 641 * Send a T packet. We currently do not support watchpoints (the 642 * awatch, rwatch or watch elements). 643 */ 644 gdb_tx_begin('T'); 645 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 646 gdb_tx_varhex(GDB_REG_PC); 647 gdb_tx_char(':'); 648 gdb_tx_reg(GDB_REG_PC); 649 gdb_tx_char(';'); 650 gdb_tx_str("thread:"); 651 gdb_tx_varhex((long)kdb_thread->td_tid); 652 gdb_tx_char(';'); 653 gdb_tx_end(); /* XXX check error condition. */ 654 655 thr_iter = NULL; 656 while (gdb_rx_begin() == 0) { 657 /* printf("GDB: got '%s'\n", gdb_rxp); */ 658 switch (gdb_rx_char()) { 659 case '?': /* Last signal. */ 660 gdb_tx_begin('T'); 661 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 662 gdb_tx_str("thread:"); 663 gdb_tx_varhex((long)kdb_thread->td_tid); 664 gdb_tx_char(';'); 665 gdb_tx_end(); 666 break; 667 case 'c': { /* Continue. */ 668 uintmax_t addr; 669 register_t pc; 670 if (!gdb_rx_varhex(&addr)) { 671 pc = addr; 672 gdb_cpu_setreg(GDB_REG_PC, &pc); 673 } 674 kdb_cpu_clear_singlestep(); 675 gdb_listening = 1; 676 return (1); 677 } 678 case 'C': { /* Continue with signal. */ 679 uintmax_t addr, sig; 680 register_t pc; 681 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 682 !gdb_rx_varhex(&addr)) { 683 pc = addr; 684 gdb_cpu_setreg(GDB_REG_PC, &pc); 685 } 686 kdb_cpu_clear_singlestep(); 687 gdb_listening = 1; 688 return (1); 689 } 690 case 'D': { /* Detach */ 691 gdb_tx_ok(); 692 gdb_handle_detach(); 693 return (1); 694 } 695 case 'g': { /* Read registers. */ 696 size_t r; 697 gdb_tx_begin(0); 698 for (r = 0; r < GDB_NREGS; r++) 699 gdb_tx_reg(r); 700 gdb_tx_end(); 701 break; 702 } 703 case 'G': { /* Write registers. */ 704 char *val; 705 bool success; 706 size_t r; 707 for (success = true, r = 0; r < GDB_NREGS; r++) { 708 val = gdb_rxp; 709 if (!gdb_rx_mem(val, gdb_cpu_regsz(r))) { 710 gdb_tx_err(EINVAL); 711 success = false; 712 break; 713 } 714 gdb_cpu_setreg(r, val); 715 } 716 if (success) 717 gdb_tx_ok(); 718 break; 719 } 720 case 'H': { /* Set thread. */ 721 intmax_t tid; 722 struct thread *thr; 723 724 /* Ignore 'g' (general) or 'c' (continue) flag. */ 725 (void) gdb_rx_char(); 726 727 if (gdb_rx_varhex(&tid)) { 728 gdb_tx_err(EINVAL); 729 break; 730 } 731 if (tid > 0) { 732 thr = kdb_thr_lookup(tid); 733 if (thr == NULL) { 734 gdb_tx_err(ENOENT); 735 break; 736 } 737 kdb_thr_select(thr); 738 } 739 gdb_tx_ok(); 740 break; 741 } 742 case 'k': /* Kill request. */ 743 gdb_handle_detach(); 744 return (1); 745 case 'm': { /* Read memory. */ 746 uintmax_t addr, size; 747 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 748 gdb_rx_varhex(&size)) { 749 gdb_tx_err(EINVAL); 750 break; 751 } 752 gdb_tx_begin(0); 753 if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 754 gdb_tx_end(); 755 else 756 gdb_tx_err(EIO); 757 break; 758 } 759 case 'M': { /* Write memory. */ 760 uintmax_t addr, size; 761 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 762 gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 763 gdb_tx_err(EINVAL); 764 break; 765 } 766 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 767 gdb_tx_err(EIO); 768 else 769 gdb_tx_ok(); 770 break; 771 } 772 case 'p': { /* Read register. */ 773 uintmax_t reg; 774 if (gdb_rx_varhex(®)) { 775 gdb_tx_err(EINVAL); 776 break; 777 } 778 gdb_tx_begin(0); 779 gdb_tx_reg(reg); 780 gdb_tx_end(); 781 break; 782 } 783 case 'P': { /* Write register. */ 784 char *val; 785 uintmax_t reg; 786 val = gdb_rxp; 787 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 788 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 789 gdb_tx_err(EINVAL); 790 break; 791 } 792 gdb_cpu_setreg(reg, val); 793 gdb_tx_ok(); 794 break; 795 } 796 case 'q': /* General query. */ 797 if (gdb_rx_equal("C")) { 798 gdb_tx_begin('Q'); 799 gdb_tx_char('C'); 800 gdb_tx_varhex((long)kdb_thread->td_tid); 801 gdb_tx_end(); 802 } else if (gdb_rx_equal("Supported")) { 803 gdb_do_qsupported(&host_features); 804 } else if (gdb_rx_equal("fThreadInfo")) { 805 thr_iter = kdb_thr_first(); 806 gdb_do_threadinfo(&thr_iter); 807 } else if (gdb_rx_equal("sThreadInfo")) { 808 gdb_do_threadinfo(&thr_iter); 809 } else if (gdb_rx_equal("Xfer:")) { 810 do_qXfer(); 811 } else if (gdb_rx_equal("Search:memory:")) { 812 gdb_do_mem_search(); 813 #ifdef __powerpc__ 814 } else if (gdb_rx_equal("Offsets")) { 815 gdb_cpu_do_offsets(); 816 #endif 817 } else if (!gdb_cpu_query()) 818 gdb_tx_empty(); 819 break; 820 case 'Q': 821 if (gdb_rx_equal("StartNoAckMode")) { 822 if ((gdb_cur->gdb_dbfeatures & 823 GDB_DBGP_FEAT_RELIABLE) == 0) { 824 /* 825 * Shouldn't happen if we didn't 826 * advertise support. Reject. 827 */ 828 gdb_tx_empty(); 829 break; 830 } 831 gdb_ackmode = false; 832 gdb_tx_ok(); 833 } else 834 gdb_tx_empty(); 835 break; 836 case 's': { /* Step. */ 837 uintmax_t addr; 838 register_t pc; 839 if (!gdb_rx_varhex(&addr)) { 840 pc = addr; 841 gdb_cpu_setreg(GDB_REG_PC, &pc); 842 } 843 kdb_cpu_set_singlestep(); 844 gdb_listening = 1; 845 return (1); 846 } 847 case 'S': { /* Step with signal. */ 848 uintmax_t addr, sig; 849 register_t pc; 850 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 851 !gdb_rx_varhex(&addr)) { 852 pc = addr; 853 gdb_cpu_setreg(GDB_REG_PC, &pc); 854 } 855 kdb_cpu_set_singlestep(); 856 gdb_listening = 1; 857 return (1); 858 } 859 case 'T': { /* Thread alive. */ 860 intmax_t tid; 861 if (gdb_rx_varhex(&tid)) { 862 gdb_tx_err(EINVAL); 863 break; 864 } 865 if (kdb_thr_lookup(tid) != NULL) 866 gdb_tx_ok(); 867 else 868 gdb_tx_err(ENOENT); 869 break; 870 } 871 case EOF: 872 /* Empty command. Treat as unknown command. */ 873 /* FALLTHROUGH */ 874 default: 875 /* Unknown command. Send empty response. */ 876 gdb_tx_empty(); 877 break; 878 } 879 } 880 (void)kdb_jmpbuf(prev_jb); 881 return (0); 882 } 883