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 /* 622 * Handle a 'Z' packet: set a breakpoint or watchpoint. 623 * 624 * Currently, only watchpoints are supported. 625 */ 626 static void 627 gdb_z_insert(void) 628 { 629 intmax_t addr, length; 630 char ztype; 631 int error; 632 633 ztype = gdb_rx_char(); 634 if (gdb_rx_char() != ',' || gdb_rx_varhex(&addr) || 635 gdb_rx_char() != ',' || gdb_rx_varhex(&length)) { 636 error = EINVAL; 637 goto fail; 638 } 639 640 switch (ztype) { 641 case '2': /* write watchpoint */ 642 error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 643 (vm_size_t)length, KDB_DBG_ACCESS_W); 644 break; 645 case '3': /* read watchpoint */ 646 error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 647 (vm_size_t)length, KDB_DBG_ACCESS_R); 648 break; 649 case '4': /* access (RW) watchpoint */ 650 error = kdb_cpu_set_watchpoint((vm_offset_t)addr, 651 (vm_size_t)length, KDB_DBG_ACCESS_RW); 652 break; 653 case '1': /* hardware breakpoint */ 654 case '0': /* software breakpoint */ 655 /* Not implemented. */ 656 gdb_tx_empty(); 657 return; 658 default: 659 error = EINVAL; 660 break; 661 } 662 if (error != 0) 663 goto fail; 664 gdb_tx_ok(); 665 return; 666 fail: 667 gdb_tx_err(error); 668 return; 669 } 670 671 /* 672 * Handle a 'z' packet; clear a breakpoint or watchpoint. 673 * 674 * Currently, only watchpoints are supported. 675 */ 676 static void 677 gdb_z_remove(void) 678 { 679 intmax_t addr, length; 680 char ztype; 681 int error; 682 683 ztype = gdb_rx_char(); 684 if (gdb_rx_char() != ',' || gdb_rx_varhex(&addr) || 685 gdb_rx_char() != ',' || gdb_rx_varhex(&length)) { 686 error = EINVAL; 687 goto fail; 688 } 689 690 switch (ztype) { 691 case '2': /* write watchpoint */ 692 case '3': /* read watchpoint */ 693 case '4': /* access (RW) watchpoint */ 694 error = kdb_cpu_clr_watchpoint((vm_offset_t)addr, 695 (vm_size_t)length); 696 break; 697 case '1': /* hardware breakpoint */ 698 case '0': /* software breakpoint */ 699 /* Not implemented. */ 700 gdb_tx_empty(); 701 return; 702 default: 703 error = EINVAL; 704 break; 705 } 706 if (error != 0) 707 goto fail; 708 gdb_tx_ok(); 709 return; 710 fail: 711 gdb_tx_err(error); 712 return; 713 } 714 715 static int 716 gdb_trap(int type, int code) 717 { 718 jmp_buf jb; 719 struct thread *thr_iter; 720 void *prev_jb; 721 uint32_t host_features; 722 723 prev_jb = kdb_jmpbuf(jb); 724 if (setjmp(jb) != 0) { 725 printf("%s bailing, hopefully back to ddb!\n", __func__); 726 gdb_listening = 0; 727 (void)kdb_jmpbuf(prev_jb); 728 return (1); 729 } 730 731 gdb_listening = 0; 732 gdb_ackmode = true; 733 734 /* 735 * Send a T packet. We currently do not support watchpoints (the 736 * awatch, rwatch or watch elements). 737 */ 738 gdb_tx_begin('T'); 739 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 740 gdb_tx_varhex(GDB_REG_PC); 741 gdb_tx_char(':'); 742 gdb_tx_reg(GDB_REG_PC); 743 gdb_tx_char(';'); 744 gdb_cpu_stop_reason(type, code); 745 gdb_tx_str("thread:"); 746 gdb_tx_varhex((uintmax_t)kdb_thread->td_tid); 747 gdb_tx_char(';'); 748 gdb_tx_end(); /* XXX check error condition. */ 749 750 thr_iter = NULL; 751 while (gdb_rx_begin() == 0) { 752 /* printf("GDB: got '%s'\n", gdb_rxp); */ 753 switch (gdb_rx_char()) { 754 case '?': /* Last signal. */ 755 gdb_tx_begin('T'); 756 gdb_tx_hex(gdb_cpu_signal(type, code), 2); 757 gdb_tx_str("thread:"); 758 gdb_tx_varhex((long)kdb_thread->td_tid); 759 gdb_tx_char(';'); 760 gdb_tx_end(); 761 break; 762 case 'c': { /* Continue. */ 763 uintmax_t addr; 764 register_t pc; 765 if (!gdb_rx_varhex(&addr)) { 766 pc = addr; 767 gdb_cpu_setreg(GDB_REG_PC, &pc); 768 } 769 kdb_cpu_clear_singlestep(); 770 gdb_listening = 1; 771 return (1); 772 } 773 case 'C': { /* Continue with signal. */ 774 uintmax_t addr, sig; 775 register_t pc; 776 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 777 !gdb_rx_varhex(&addr)) { 778 pc = addr; 779 gdb_cpu_setreg(GDB_REG_PC, &pc); 780 } 781 kdb_cpu_clear_singlestep(); 782 gdb_listening = 1; 783 return (1); 784 } 785 case 'D': { /* Detach */ 786 gdb_tx_ok(); 787 gdb_handle_detach(); 788 return (1); 789 } 790 case 'g': { /* Read registers. */ 791 size_t r; 792 gdb_tx_begin(0); 793 for (r = 0; r < GDB_NREGS; r++) 794 gdb_tx_reg(r); 795 gdb_tx_end(); 796 break; 797 } 798 case 'G': { /* Write registers. */ 799 char *val; 800 bool success; 801 size_t r; 802 for (success = true, r = 0; r < GDB_NREGS; r++) { 803 val = gdb_rxp; 804 if (!gdb_rx_mem(val, gdb_cpu_regsz(r))) { 805 gdb_tx_err(EINVAL); 806 success = false; 807 break; 808 } 809 gdb_cpu_setreg(r, val); 810 } 811 if (success) 812 gdb_tx_ok(); 813 break; 814 } 815 case 'H': { /* Set thread. */ 816 intmax_t tid; 817 struct thread *thr; 818 819 /* Ignore 'g' (general) or 'c' (continue) flag. */ 820 (void) gdb_rx_char(); 821 822 if (gdb_rx_varhex(&tid)) { 823 gdb_tx_err(EINVAL); 824 break; 825 } 826 if (tid > 0) { 827 thr = kdb_thr_lookup(tid); 828 if (thr == NULL) { 829 gdb_tx_err(ENOENT); 830 break; 831 } 832 kdb_thr_select(thr); 833 } 834 gdb_tx_ok(); 835 break; 836 } 837 case 'k': /* Kill request. */ 838 gdb_handle_detach(); 839 return (1); 840 case 'm': { /* Read memory. */ 841 uintmax_t addr, size; 842 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 843 gdb_rx_varhex(&size)) { 844 gdb_tx_err(EINVAL); 845 break; 846 } 847 gdb_tx_begin(0); 848 if (gdb_tx_mem((char *)(uintptr_t)addr, size)) 849 gdb_tx_end(); 850 else 851 gdb_tx_err(EIO); 852 break; 853 } 854 case 'M': { /* Write memory. */ 855 uintmax_t addr, size; 856 if (gdb_rx_varhex(&addr) || gdb_rx_char() != ',' || 857 gdb_rx_varhex(&size) || gdb_rx_char() != ':') { 858 gdb_tx_err(EINVAL); 859 break; 860 } 861 if (gdb_rx_mem((char *)(uintptr_t)addr, size) == 0) 862 gdb_tx_err(EIO); 863 else 864 gdb_tx_ok(); 865 break; 866 } 867 case 'p': { /* Read register. */ 868 uintmax_t reg; 869 if (gdb_rx_varhex(®)) { 870 gdb_tx_err(EINVAL); 871 break; 872 } 873 gdb_tx_begin(0); 874 gdb_tx_reg(reg); 875 gdb_tx_end(); 876 break; 877 } 878 case 'P': { /* Write register. */ 879 char *val; 880 uintmax_t reg; 881 val = gdb_rxp; 882 if (gdb_rx_varhex(®) || gdb_rx_char() != '=' || 883 !gdb_rx_mem(val, gdb_cpu_regsz(reg))) { 884 gdb_tx_err(EINVAL); 885 break; 886 } 887 gdb_cpu_setreg(reg, val); 888 gdb_tx_ok(); 889 break; 890 } 891 case 'q': /* General query. */ 892 if (gdb_rx_equal("C")) { 893 gdb_tx_begin('Q'); 894 gdb_tx_char('C'); 895 gdb_tx_varhex((long)kdb_thread->td_tid); 896 gdb_tx_end(); 897 } else if (gdb_rx_equal("Supported")) { 898 gdb_do_qsupported(&host_features); 899 } else if (gdb_rx_equal("fThreadInfo")) { 900 thr_iter = kdb_thr_first(); 901 gdb_do_threadinfo(&thr_iter); 902 } else if (gdb_rx_equal("sThreadInfo")) { 903 gdb_do_threadinfo(&thr_iter); 904 } else if (gdb_rx_equal("Xfer:")) { 905 do_qXfer(); 906 } else if (gdb_rx_equal("Search:memory:")) { 907 gdb_do_mem_search(); 908 #ifdef __powerpc__ 909 } else if (gdb_rx_equal("Offsets")) { 910 gdb_cpu_do_offsets(); 911 #endif 912 } else if (!gdb_cpu_query()) 913 gdb_tx_empty(); 914 break; 915 case 'Q': 916 if (gdb_rx_equal("StartNoAckMode")) { 917 if ((gdb_cur->gdb_dbfeatures & 918 GDB_DBGP_FEAT_RELIABLE) == 0) { 919 /* 920 * Shouldn't happen if we didn't 921 * advertise support. Reject. 922 */ 923 gdb_tx_empty(); 924 break; 925 } 926 gdb_ackmode = false; 927 gdb_tx_ok(); 928 } else 929 gdb_tx_empty(); 930 break; 931 case 's': { /* Step. */ 932 uintmax_t addr; 933 register_t pc; 934 if (!gdb_rx_varhex(&addr)) { 935 pc = addr; 936 gdb_cpu_setreg(GDB_REG_PC, &pc); 937 } 938 kdb_cpu_set_singlestep(); 939 gdb_listening = 1; 940 return (1); 941 } 942 case 'S': { /* Step with signal. */ 943 uintmax_t addr, sig; 944 register_t pc; 945 if (!gdb_rx_varhex(&sig) && gdb_rx_char() == ';' && 946 !gdb_rx_varhex(&addr)) { 947 pc = addr; 948 gdb_cpu_setreg(GDB_REG_PC, &pc); 949 } 950 kdb_cpu_set_singlestep(); 951 gdb_listening = 1; 952 return (1); 953 } 954 case 'T': { /* Thread alive. */ 955 intmax_t tid; 956 if (gdb_rx_varhex(&tid)) { 957 gdb_tx_err(EINVAL); 958 break; 959 } 960 if (kdb_thr_lookup(tid) != NULL) 961 gdb_tx_ok(); 962 else 963 gdb_tx_err(ENOENT); 964 break; 965 } 966 case 'z': { /* Remove watchpoint. */ 967 gdb_z_remove(); 968 break; 969 } 970 case 'Z': { /* Set watchpoint. */ 971 gdb_z_insert(); 972 break; 973 } 974 case EOF: 975 /* Empty command. Treat as unknown command. */ 976 /* FALLTHROUGH */ 977 default: 978 /* Unknown command. Send empty response. */ 979 gdb_tx_empty(); 980 break; 981 } 982 } 983 (void)kdb_jmpbuf(prev_jb); 984 return (0); 985 } 986