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 * dst must be 2x strlen(max_src) + 1. 365 * 366 * Squashes invalid XML characters down to _. Sorry. Then escapes for GDB. 367 */ 368 static void 369 qXfer_escape_xmlattr_str(char *dst, size_t dstlen, const char *src) 370 { 371 static const char *forbidden = "#$}*"; 372 373 size_t i; 374 char c; 375 376 for (i = 0; i < dstlen - 1 && *src != 0; src++, i++) { 377 c = *src; 378 /* XML attr filter */ 379 if (c < 32) 380 c = '_'; 381 /* We assume attributes will be "" quoted. */ 382 if (c == '<' || c == '&' || c == '"') 383 c = '_'; 384 385 /* GDB escape. */ 386 if (strchr(forbidden, c) != NULL) { 387 *dst++ = '}'; 388 c ^= 0x20; 389 } 390 *dst++ = c; 391 } 392 if (*src != 0) 393 printf("XXX%s: overflow; API misuse\n", __func__); 394 395 *dst = 0; 396 } 397 398 /* 399 * Dynamically generate qXfer:threads document, one packet at a time. 400 * 401 * The format is loosely described[0], although it does not seem that the 402 * <?xml?> mentioned on that page is required. 403 * 404 * [0]: https://sourceware.org/gdb/current/onlinedocs/gdb/Thread-List-Format.html 405 */ 406 static void 407 do_qXfer_threads_read(void) 408 { 409 /* Kludgy context */ 410 static struct { 411 struct qXfer_context qXfer; 412 /* Kludgy state machine */ 413 struct thread *iter; 414 enum { 415 XML_START_THREAD, /* '<thread' */ 416 XML_THREAD_ID, /* ' id="xxx"' */ 417 XML_THREAD_CORE, /* ' core="yyy"' */ 418 XML_THREAD_NAME, /* ' name="zzz"' */ 419 XML_THREAD_EXTRA, /* '> ...' */ 420 XML_END_THREAD, /* '</thread>' */ 421 XML_SENT_END_THREADS, /* '</threads>' */ 422 } next_step; 423 } ctx; 424 static char td_name_escape[MAXCOMLEN * 2 + 1]; 425 426 const char *name_src; 427 uintmax_t offset, len; 428 int error; 429 430 /* Annex part must be empty. */ 431 if (gdb_rx_char() != ':') 432 goto misformed_request; 433 434 if (gdb_rx_varhex(&offset) != 0 || 435 gdb_rx_char() != ',' || 436 gdb_rx_varhex(&len) != 0) 437 goto misformed_request; 438 439 /* 440 * Validate resume xfers. 441 */ 442 if (offset != 0) { 443 if (offset != ctx.qXfer.last_offset) { 444 printf("%s: Resumed offset %ju != expected %zu\n", 445 __func__, offset, ctx.qXfer.last_offset); 446 error = ESPIPE; 447 goto request_error; 448 } 449 ctx.qXfer.flushed = false; 450 } 451 452 if (offset == 0) { 453 ctx.iter = kdb_thr_first(); 454 ctx.next_step = XML_START_THREAD; 455 error = init_qXfer_ctx(&ctx.qXfer, len); 456 if (error != 0) 457 goto request_error; 458 459 sbuf_cat(&ctx.qXfer.sb, "<threads>"); 460 } 461 462 while (!ctx.qXfer.flushed && ctx.iter != NULL) { 463 switch (ctx.next_step) { 464 case XML_START_THREAD: 465 ctx.next_step = XML_THREAD_ID; 466 sbuf_cat(&ctx.qXfer.sb, "<thread"); 467 continue; 468 469 case XML_THREAD_ID: 470 ctx.next_step = XML_THREAD_CORE; 471 sbuf_printf(&ctx.qXfer.sb, " id=\"%jx\"", 472 (uintmax_t)ctx.iter->td_tid); 473 continue; 474 475 case XML_THREAD_CORE: 476 ctx.next_step = XML_THREAD_NAME; 477 if (ctx.iter->td_oncpu != NOCPU) { 478 sbuf_printf(&ctx.qXfer.sb, " core=\"%d\"", 479 ctx.iter->td_oncpu); 480 } 481 continue; 482 483 case XML_THREAD_NAME: 484 ctx.next_step = XML_THREAD_EXTRA; 485 486 if (ctx.iter->td_name[0] != 0) 487 name_src = ctx.iter->td_name; 488 else if (ctx.iter->td_proc != NULL && 489 ctx.iter->td_proc->p_comm[0] != 0) 490 name_src = ctx.iter->td_proc->p_comm; 491 else 492 continue; 493 494 qXfer_escape_xmlattr_str(td_name_escape, 495 sizeof(td_name_escape), name_src); 496 sbuf_printf(&ctx.qXfer.sb, " name=\"%s\"", 497 td_name_escape); 498 continue; 499 500 case XML_THREAD_EXTRA: 501 ctx.next_step = XML_END_THREAD; 502 503 sbuf_putc(&ctx.qXfer.sb, '>'); 504 505 if (ctx.iter->td_state == TDS_RUNNING) 506 sbuf_cat(&ctx.qXfer.sb, "Running"); 507 else if (ctx.iter->td_state == TDS_RUNQ) 508 sbuf_cat(&ctx.qXfer.sb, "RunQ"); 509 else if (ctx.iter->td_state == TDS_CAN_RUN) 510 sbuf_cat(&ctx.qXfer.sb, "CanRun"); 511 else if (TD_ON_LOCK(ctx.iter)) 512 sbuf_cat(&ctx.qXfer.sb, "Blocked"); 513 else if (TD_IS_SLEEPING(ctx.iter)) 514 sbuf_cat(&ctx.qXfer.sb, "Sleeping"); 515 else if (TD_IS_SWAPPED(ctx.iter)) 516 sbuf_cat(&ctx.qXfer.sb, "Swapped"); 517 else if (TD_AWAITING_INTR(ctx.iter)) 518 sbuf_cat(&ctx.qXfer.sb, "IthreadWait"); 519 else if (TD_IS_SUSPENDED(ctx.iter)) 520 sbuf_cat(&ctx.qXfer.sb, "Suspended"); 521 else 522 sbuf_cat(&ctx.qXfer.sb, "???"); 523 continue; 524 525 case XML_END_THREAD: 526 ctx.next_step = XML_START_THREAD; 527 sbuf_cat(&ctx.qXfer.sb, "</thread>"); 528 ctx.iter = kdb_thr_next(ctx.iter); 529 continue; 530 531 /* 532 * This one isn't part of the looping state machine, 533 * but GCC complains if you leave an enum value out of the 534 * select. 535 */ 536 case XML_SENT_END_THREADS: 537 /* NOTREACHED */ 538 break; 539 } 540 } 541 if (ctx.qXfer.flushed) 542 return; 543 544 if (ctx.next_step != XML_SENT_END_THREADS) { 545 ctx.next_step = XML_SENT_END_THREADS; 546 sbuf_cat(&ctx.qXfer.sb, "</threads>"); 547 } 548 if (ctx.qXfer.flushed) 549 return; 550 551 ctx.qXfer.lastmessage = true; 552 sbuf_finish(&ctx.qXfer.sb); 553 sbuf_delete(&ctx.qXfer.sb); 554 ctx.qXfer.last_offset = 0; 555 return; 556 557 misformed_request: 558 /* 559 * GDB "General-Query-Packets.html" qXfer-read anchor specifically 560 * documents an E00 code for malformed requests or invalid annex. 561 * Non-zero codes indicate invalid offset or "error reading the data." 562 */ 563 error = 0; 564 request_error: 565 gdb_tx_err(error); 566 return; 567 } 568 569 /* 570 * A set of standardized transfers from "special data areas." 571 * 572 * We've already matched on "qXfer:" and advanced the rx packet buffer past 573 * that bit. Parse out the rest of the packet and generate an appropriate 574 * response. 575 */ 576 static void 577 do_qXfer(void) 578 { 579 if (!gdb_rx_equal("threads:")) 580 goto unrecognized; 581 582 if (!gdb_rx_equal("read:")) 583 goto unrecognized; 584 585 do_qXfer_threads_read(); 586 return; 587 588 unrecognized: 589 gdb_tx_empty(); 590 return; 591 } 592 593 static void 594 gdb_handle_detach(void) 595 { 596 kdb_cpu_clear_singlestep(); 597 gdb_listening = 0; 598 599 if (gdb_cur->gdb_dbfeatures & GDB_DBGP_FEAT_WANTTERM) 600 gdb_cur->gdb_term(); 601 602 #ifdef DDB 603 if (!gdb_return_to_ddb) 604 return; 605 606 gdb_return_to_ddb = false; 607 608 if (kdb_dbbe_select("ddb") != 0) 609 printf("The ddb backend could not be selected.\n"); 610 #endif 611 } 612 613 static int 614 gdb_trap(int type, int code) 615 { 616 jmp_buf jb; 617 struct thread *thr_iter; 618 void *prev_jb; 619 uint32_t host_features; 620 621 prev_jb = kdb_jmpbuf(jb); 622 if (setjmp(jb) != 0) { 623 printf("%s bailing, hopefully back to ddb!\n", __func__); 624 gdb_listening = 0; 625 (void)kdb_jmpbuf(prev_jb); 626 return (1); 627 } 628 629 gdb_listening = 0; 630 gdb_ackmode = true; 631 632 /* 633 * Send a T packet. We currently do not support watchpoints (the 634 * awatch, rwatch or watch elements). 635 */ 636 gdb_tx_begin('T'); 637 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 638 gdb_tx_varhex(GDB_REG_PC); 639 gdb_tx_char(':'); 640 gdb_tx_reg(GDB_REG_PC); 641 gdb_tx_char(';'); 642 gdb_tx_str("thread:"); 643 gdb_tx_varhex((long)kdb_thread->td_tid); 644 gdb_tx_char(';'); 645 gdb_tx_end(); /* XXX check error condition. */ 646 647 thr_iter = NULL; 648 while (gdb_rx_begin() == 0) { 649 /* printf("GDB: got '%s'\n", gdb_rxp); */ 650 switch (gdb_rx_char()) { 651 case '?': /* Last signal. */ 652 gdb_tx_begin('T'); 653 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 654 gdb_tx_str("thread:"); 655 gdb_tx_varhex((long)kdb_thread->td_tid); 656 gdb_tx_char(';'); 657 gdb_tx_end(); 658 break; 659 case 'c': { /* Continue. */ 660 uintmax_t addr; 661 register_t pc; 662 if (!gdb_rx_varhex(&addr)) { 663 pc = addr; 664 gdb_cpu_setreg(GDB_REG_PC, &pc); 665 } 666 kdb_cpu_clear_singlestep(); 667 gdb_listening = 1; 668 return (1); 669 } 670 case 'C': { /* Continue with signal. */ 671 uintmax_t addr, sig; 672 register_t pc; 673 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 674 !gdb_rx_varhex(&addr)) { 675 pc = addr; 676 gdb_cpu_setreg(GDB_REG_PC, &pc); 677 } 678 kdb_cpu_clear_singlestep(); 679 gdb_listening = 1; 680 return (1); 681 } 682 case 'D': { /* Detach */ 683 gdb_tx_ok(); 684 gdb_handle_detach(); 685 return (1); 686 } 687 case 'g': { /* Read registers. */ 688 size_t r; 689 gdb_tx_begin(0); 690 for (r = 0; r < GDB_NREGS; r++) 691 gdb_tx_reg(r); 692 gdb_tx_end(); 693 break; 694 } 695 case 'G': /* Write registers. */ 696 gdb_tx_err(0); 697 break; 698 case 'H': { /* Set thread. */ 699 intmax_t tid; 700 struct thread *thr; 701 702 /* Ignore 'g' (general) or 'c' (continue) flag. */ 703 (void) gdb_rx_char(); 704 705 if (gdb_rx_varhex(&tid)) { 706 gdb_tx_err(EINVAL); 707 break; 708 } 709 if (tid > 0) { 710 thr = kdb_thr_lookup(tid); 711 if (thr == NULL) { 712 gdb_tx_err(ENOENT); 713 break; 714 } 715 kdb_thr_select(thr); 716 } 717 gdb_tx_ok(); 718 break; 719 } 720 case 'k': /* Kill request. */ 721 gdb_handle_detach(); 722 return (1); 723 case 'm': { /* Read memory. */ 724 uintmax_t addr, size; 725 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 726 gdb_rx_varhex(&size)) { 727 gdb_tx_err(EINVAL); 728 break; 729 } 730 gdb_tx_begin(0); 731 if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 732 gdb_tx_end(); 733 else 734 gdb_tx_err(EIO); 735 break; 736 } 737 case 'M': { /* Write memory. */ 738 uintmax_t addr, size; 739 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 740 gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 741 gdb_tx_err(EINVAL); 742 break; 743 } 744 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 745 gdb_tx_err(EIO); 746 else 747 gdb_tx_ok(); 748 break; 749 } 750 case 'P': { /* Write register. */ 751 char *val; 752 uintmax_t reg; 753 val = gdb_rxp; 754 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 755 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 756 gdb_tx_err(EINVAL); 757 break; 758 } 759 gdb_cpu_setreg(reg, val); 760 gdb_tx_ok(); 761 break; 762 } 763 case 'q': /* General query. */ 764 if (gdb_rx_equal("C")) { 765 gdb_tx_begin('Q'); 766 gdb_tx_char('C'); 767 gdb_tx_varhex((long)kdb_thread->td_tid); 768 gdb_tx_end(); 769 } else if (gdb_rx_equal("Supported")) { 770 gdb_do_qsupported(&host_features); 771 } else if (gdb_rx_equal("fThreadInfo")) { 772 thr_iter = kdb_thr_first(); 773 gdb_do_threadinfo(&thr_iter); 774 } else if (gdb_rx_equal("sThreadInfo")) { 775 gdb_do_threadinfo(&thr_iter); 776 } else if (gdb_rx_equal("Xfer:")) { 777 do_qXfer(); 778 } else if (gdb_rx_equal("Search:memory:")) { 779 gdb_do_mem_search(); 780 #ifdef __powerpc__ 781 } else if (gdb_rx_equal("Offsets")) { 782 gdb_cpu_do_offsets(); 783 #endif 784 } else if (!gdb_cpu_query()) 785 gdb_tx_empty(); 786 break; 787 case 'Q': 788 if (gdb_rx_equal("StartNoAckMode")) { 789 if ((gdb_cur->gdb_dbfeatures & 790 GDB_DBGP_FEAT_RELIABLE) == 0) { 791 /* 792 * Shouldn't happen if we didn't 793 * advertise support. Reject. 794 */ 795 gdb_tx_empty(); 796 break; 797 } 798 gdb_ackmode = false; 799 gdb_tx_ok(); 800 } else 801 gdb_tx_empty(); 802 break; 803 case 's': { /* Step. */ 804 uintmax_t addr; 805 register_t pc; 806 if (!gdb_rx_varhex(&addr)) { 807 pc = addr; 808 gdb_cpu_setreg(GDB_REG_PC, &pc); 809 } 810 kdb_cpu_set_singlestep(); 811 gdb_listening = 1; 812 return (1); 813 } 814 case 'S': { /* Step with signal. */ 815 uintmax_t addr, sig; 816 register_t pc; 817 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 818 !gdb_rx_varhex(&addr)) { 819 pc = addr; 820 gdb_cpu_setreg(GDB_REG_PC, &pc); 821 } 822 kdb_cpu_set_singlestep(); 823 gdb_listening = 1; 824 return (1); 825 } 826 case 'T': { /* Thread alive. */ 827 intmax_t tid; 828 if (gdb_rx_varhex(&tid)) { 829 gdb_tx_err(EINVAL); 830 break; 831 } 832 if (kdb_thr_lookup(tid) != NULL) 833 gdb_tx_ok(); 834 else 835 gdb_tx_err(ENOENT); 836 break; 837 } 838 case EOF: 839 /* Empty command. Treat as unknown command. */ 840 /* FALLTHROUGH */ 841 default: 842 /* Unknown command. Send empty response. */ 843 gdb_tx_empty(); 844 break; 845 } 846 } 847 (void)kdb_jmpbuf(prev_jb); 848 return (0); 849 } 850