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