1 /* 2 * gdb server stub 3 * 4 * This implements a subset of the remote protocol as described in: 5 * 6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html 7 * 8 * Copyright (c) 2003-2005 Fabrice Bellard 9 * 10 * This library is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU Lesser General Public 12 * License as published by the Free Software Foundation; either 13 * version 2 of the License, or (at your option) any later version. 14 * 15 * This library is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 * Lesser General Public License for more details. 19 * 20 * You should have received a copy of the GNU Lesser General Public 21 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 22 * 23 * SPDX-License-Identifier: LGPL-2.0+ 24 */ 25 26 #include "qemu/osdep.h" 27 #include "qemu/ctype.h" 28 #include "qemu/cutils.h" 29 #include "qemu/module.h" 30 #include "qemu/error-report.h" 31 #include "trace.h" 32 #include "exec/gdbstub.h" 33 #include "gdbstub/syscalls.h" 34 #ifdef CONFIG_USER_ONLY 35 #include "gdbstub/user.h" 36 #else 37 #include "hw/cpu/cluster.h" 38 #include "hw/boards.h" 39 #endif 40 41 #include "sysemu/hw_accel.h" 42 #include "sysemu/runstate.h" 43 #include "exec/replay-core.h" 44 #include "exec/hwaddr.h" 45 46 #include "internals.h" 47 48 typedef struct GDBRegisterState { 49 int base_reg; 50 gdb_get_reg_cb get_reg; 51 gdb_set_reg_cb set_reg; 52 const GDBFeature *feature; 53 } GDBRegisterState; 54 55 GDBState gdbserver_state; 56 57 void gdb_init_gdbserver_state(void) 58 { 59 g_assert(!gdbserver_state.init); 60 memset(&gdbserver_state, 0, sizeof(GDBState)); 61 gdbserver_state.init = true; 62 gdbserver_state.str_buf = g_string_new(NULL); 63 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH); 64 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4); 65 66 /* 67 * What single-step modes are supported is accelerator dependent. 68 * By default try to use no IRQs and no timers while single 69 * stepping so as to make single stepping like a typical ICE HW step. 70 */ 71 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags(); 72 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; 73 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags; 74 } 75 76 /* writes 2*len+1 bytes in buf */ 77 void gdb_memtohex(GString *buf, const uint8_t *mem, int len) 78 { 79 int i, c; 80 for(i = 0; i < len; i++) { 81 c = mem[i]; 82 g_string_append_c(buf, tohex(c >> 4)); 83 g_string_append_c(buf, tohex(c & 0xf)); 84 } 85 g_string_append_c(buf, '\0'); 86 } 87 88 void gdb_hextomem(GByteArray *mem, const char *buf, int len) 89 { 90 int i; 91 92 for(i = 0; i < len; i++) { 93 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]); 94 g_byte_array_append(mem, &byte, 1); 95 buf += 2; 96 } 97 } 98 99 static void hexdump(const char *buf, int len, 100 void (*trace_fn)(size_t ofs, char const *text)) 101 { 102 char line_buffer[3 * 16 + 4 + 16 + 1]; 103 104 size_t i; 105 for (i = 0; i < len || (i & 0xF); ++i) { 106 size_t byte_ofs = i & 15; 107 108 if (byte_ofs == 0) { 109 memset(line_buffer, ' ', 3 * 16 + 4 + 16); 110 line_buffer[3 * 16 + 4 + 16] = 0; 111 } 112 113 size_t col_group = (i >> 2) & 3; 114 size_t hex_col = byte_ofs * 3 + col_group; 115 size_t txt_col = 3 * 16 + 4 + byte_ofs; 116 117 if (i < len) { 118 char value = buf[i]; 119 120 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); 121 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); 122 line_buffer[txt_col + 0] = (value >= ' ' && value < 127) 123 ? value 124 : '.'; 125 } 126 127 if (byte_ofs == 0xF) 128 trace_fn(i & -16, line_buffer); 129 } 130 } 131 132 /* return -1 if error, 0 if OK */ 133 int gdb_put_packet_binary(const char *buf, int len, bool dump) 134 { 135 int csum, i; 136 uint8_t footer[3]; 137 138 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { 139 hexdump(buf, len, trace_gdbstub_io_binaryreply); 140 } 141 142 for(;;) { 143 g_byte_array_set_size(gdbserver_state.last_packet, 0); 144 g_byte_array_append(gdbserver_state.last_packet, 145 (const uint8_t *) "$", 1); 146 g_byte_array_append(gdbserver_state.last_packet, 147 (const uint8_t *) buf, len); 148 csum = 0; 149 for(i = 0; i < len; i++) { 150 csum += buf[i]; 151 } 152 footer[0] = '#'; 153 footer[1] = tohex((csum >> 4) & 0xf); 154 footer[2] = tohex((csum) & 0xf); 155 g_byte_array_append(gdbserver_state.last_packet, footer, 3); 156 157 gdb_put_buffer(gdbserver_state.last_packet->data, 158 gdbserver_state.last_packet->len); 159 160 if (gdb_got_immediate_ack()) { 161 break; 162 } 163 } 164 return 0; 165 } 166 167 /* return -1 if error, 0 if OK */ 168 int gdb_put_packet(const char *buf) 169 { 170 trace_gdbstub_io_reply(buf); 171 172 return gdb_put_packet_binary(buf, strlen(buf), false); 173 } 174 175 void gdb_put_strbuf(void) 176 { 177 gdb_put_packet(gdbserver_state.str_buf->str); 178 } 179 180 /* Encode data using the encoding for 'x' packets. */ 181 void gdb_memtox(GString *buf, const char *mem, int len) 182 { 183 char c; 184 185 while (len--) { 186 c = *(mem++); 187 switch (c) { 188 case '#': case '$': case '*': case '}': 189 g_string_append_c(buf, '}'); 190 g_string_append_c(buf, c ^ 0x20); 191 break; 192 default: 193 g_string_append_c(buf, c); 194 break; 195 } 196 } 197 } 198 199 static uint32_t gdb_get_cpu_pid(CPUState *cpu) 200 { 201 #ifdef CONFIG_USER_ONLY 202 return getpid(); 203 #else 204 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { 205 /* Return the default process' PID */ 206 int index = gdbserver_state.process_num - 1; 207 return gdbserver_state.processes[index].pid; 208 } 209 return cpu->cluster_index + 1; 210 #endif 211 } 212 213 GDBProcess *gdb_get_process(uint32_t pid) 214 { 215 int i; 216 217 if (!pid) { 218 /* 0 means any process, we take the first one */ 219 return &gdbserver_state.processes[0]; 220 } 221 222 for (i = 0; i < gdbserver_state.process_num; i++) { 223 if (gdbserver_state.processes[i].pid == pid) { 224 return &gdbserver_state.processes[i]; 225 } 226 } 227 228 return NULL; 229 } 230 231 static GDBProcess *gdb_get_cpu_process(CPUState *cpu) 232 { 233 return gdb_get_process(gdb_get_cpu_pid(cpu)); 234 } 235 236 static CPUState *find_cpu(uint32_t thread_id) 237 { 238 CPUState *cpu; 239 240 CPU_FOREACH(cpu) { 241 if (gdb_get_cpu_index(cpu) == thread_id) { 242 return cpu; 243 } 244 } 245 246 return NULL; 247 } 248 249 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process) 250 { 251 CPUState *cpu; 252 253 CPU_FOREACH(cpu) { 254 if (gdb_get_cpu_pid(cpu) == process->pid) { 255 return cpu; 256 } 257 } 258 259 return NULL; 260 } 261 262 static CPUState *gdb_next_cpu_in_process(CPUState *cpu) 263 { 264 uint32_t pid = gdb_get_cpu_pid(cpu); 265 cpu = CPU_NEXT(cpu); 266 267 while (cpu) { 268 if (gdb_get_cpu_pid(cpu) == pid) { 269 break; 270 } 271 272 cpu = CPU_NEXT(cpu); 273 } 274 275 return cpu; 276 } 277 278 /* Return the cpu following @cpu, while ignoring unattached processes. */ 279 static CPUState *gdb_next_attached_cpu(CPUState *cpu) 280 { 281 cpu = CPU_NEXT(cpu); 282 283 while (cpu) { 284 if (gdb_get_cpu_process(cpu)->attached) { 285 break; 286 } 287 288 cpu = CPU_NEXT(cpu); 289 } 290 291 return cpu; 292 } 293 294 /* Return the first attached cpu */ 295 CPUState *gdb_first_attached_cpu(void) 296 { 297 CPUState *cpu = first_cpu; 298 GDBProcess *process = gdb_get_cpu_process(cpu); 299 300 if (!process->attached) { 301 return gdb_next_attached_cpu(cpu); 302 } 303 304 return cpu; 305 } 306 307 static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) 308 { 309 GDBProcess *process; 310 CPUState *cpu; 311 312 if (!pid && !tid) { 313 /* 0 means any process/thread, we take the first attached one */ 314 return gdb_first_attached_cpu(); 315 } else if (pid && !tid) { 316 /* any thread in a specific process */ 317 process = gdb_get_process(pid); 318 319 if (process == NULL) { 320 return NULL; 321 } 322 323 if (!process->attached) { 324 return NULL; 325 } 326 327 return gdb_get_first_cpu_in_process(process); 328 } else { 329 /* a specific thread */ 330 cpu = find_cpu(tid); 331 332 if (cpu == NULL) { 333 return NULL; 334 } 335 336 process = gdb_get_cpu_process(cpu); 337 338 if (pid && process->pid != pid) { 339 return NULL; 340 } 341 342 if (!process->attached) { 343 return NULL; 344 } 345 346 return cpu; 347 } 348 } 349 350 static const char *get_feature_xml(const char *p, const char **newp, 351 GDBProcess *process) 352 { 353 CPUState *cpu = gdb_get_first_cpu_in_process(process); 354 CPUClass *cc = CPU_GET_CLASS(cpu); 355 GDBRegisterState *r; 356 size_t len; 357 358 /* 359 * qXfer:features:read:ANNEX:OFFSET,LENGTH' 360 * ^p ^newp 361 */ 362 char *term = strchr(p, ':'); 363 *newp = term + 1; 364 len = term - p; 365 366 /* Is it the main target xml? */ 367 if (strncmp(p, "target.xml", len) == 0) { 368 if (!process->target_xml) { 369 g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free); 370 371 g_ptr_array_add( 372 xml, 373 g_strdup("<?xml version=\"1.0\"?>" 374 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" 375 "<target>")); 376 377 if (cc->gdb_arch_name) { 378 g_ptr_array_add( 379 xml, 380 g_markup_printf_escaped("<architecture>%s</architecture>", 381 cc->gdb_arch_name(cpu))); 382 } 383 for (guint i = 0; i < cpu->gdb_regs->len; i++) { 384 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); 385 g_ptr_array_add( 386 xml, 387 g_markup_printf_escaped("<xi:include href=\"%s\"/>", 388 r->feature->xmlname)); 389 } 390 g_ptr_array_add(xml, g_strdup("</target>")); 391 g_ptr_array_add(xml, NULL); 392 393 process->target_xml = g_strjoinv(NULL, (void *)xml->pdata); 394 } 395 return process->target_xml; 396 } 397 /* Is it one of the features? */ 398 for (guint i = 0; i < cpu->gdb_regs->len; i++) { 399 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); 400 if (strncmp(p, r->feature->xmlname, len) == 0) { 401 return r->feature->xml; 402 } 403 } 404 405 /* failed */ 406 return NULL; 407 } 408 409 void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature, 410 const char *name, const char *xmlname, 411 int base_reg) 412 { 413 char *header = g_markup_printf_escaped( 414 "<?xml version=\"1.0\"?>" 415 "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">" 416 "<feature name=\"%s\">", 417 name); 418 419 builder->feature = feature; 420 builder->xml = g_ptr_array_new(); 421 g_ptr_array_add(builder->xml, header); 422 builder->base_reg = base_reg; 423 feature->xmlname = xmlname; 424 feature->num_regs = 0; 425 } 426 427 void gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder, 428 const char *format, ...) 429 { 430 va_list ap; 431 va_start(ap, format); 432 g_ptr_array_add(builder->xml, g_markup_vprintf_escaped(format, ap)); 433 va_end(ap); 434 } 435 436 void gdb_feature_builder_append_reg(const GDBFeatureBuilder *builder, 437 const char *name, 438 int bitsize, 439 int regnum, 440 const char *type, 441 const char *group) 442 { 443 if (builder->feature->num_regs < regnum) { 444 builder->feature->num_regs = regnum; 445 } 446 447 if (group) { 448 gdb_feature_builder_append_tag( 449 builder, 450 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>", 451 name, bitsize, builder->base_reg + regnum, type, group); 452 } else { 453 gdb_feature_builder_append_tag( 454 builder, 455 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>", 456 name, bitsize, builder->base_reg + regnum, type); 457 } 458 } 459 460 void gdb_feature_builder_end(const GDBFeatureBuilder *builder) 461 { 462 g_ptr_array_add(builder->xml, (void *)"</feature>"); 463 g_ptr_array_add(builder->xml, NULL); 464 465 builder->feature->xml = g_strjoinv(NULL, (void *)builder->xml->pdata); 466 467 for (guint i = 0; i < builder->xml->len - 2; i++) { 468 g_free(g_ptr_array_index(builder->xml, i)); 469 } 470 471 g_ptr_array_free(builder->xml, TRUE); 472 } 473 474 const GDBFeature *gdb_find_static_feature(const char *xmlname) 475 { 476 const GDBFeature *feature; 477 478 for (feature = gdb_static_features; feature->xmlname; feature++) { 479 if (!strcmp(feature->xmlname, xmlname)) { 480 return feature; 481 } 482 } 483 484 g_assert_not_reached(); 485 } 486 487 static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) 488 { 489 CPUClass *cc = CPU_GET_CLASS(cpu); 490 GDBRegisterState *r; 491 492 if (reg < cc->gdb_num_core_regs) { 493 return cc->gdb_read_register(cpu, buf, reg); 494 } 495 496 for (guint i = 0; i < cpu->gdb_regs->len; i++) { 497 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); 498 if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { 499 return r->get_reg(cpu, buf, reg - r->base_reg); 500 } 501 } 502 return 0; 503 } 504 505 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) 506 { 507 CPUClass *cc = CPU_GET_CLASS(cpu); 508 GDBRegisterState *r; 509 510 if (reg < cc->gdb_num_core_regs) { 511 return cc->gdb_write_register(cpu, mem_buf, reg); 512 } 513 514 for (guint i = 0; i < cpu->gdb_regs->len; i++) { 515 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); 516 if (r->base_reg <= reg && reg < r->base_reg + r->feature->num_regs) { 517 return r->set_reg(cpu, mem_buf, reg - r->base_reg); 518 } 519 } 520 return 0; 521 } 522 523 static void gdb_register_feature(CPUState *cpu, int base_reg, 524 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, 525 const GDBFeature *feature) 526 { 527 GDBRegisterState s = { 528 .base_reg = base_reg, 529 .get_reg = get_reg, 530 .set_reg = set_reg, 531 .feature = feature 532 }; 533 534 g_array_append_val(cpu->gdb_regs, s); 535 } 536 537 void gdb_init_cpu(CPUState *cpu) 538 { 539 CPUClass *cc = CPU_GET_CLASS(cpu); 540 const GDBFeature *feature; 541 542 cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState)); 543 544 if (cc->gdb_core_xml_file) { 545 feature = gdb_find_static_feature(cc->gdb_core_xml_file); 546 gdb_register_feature(cpu, 0, 547 cc->gdb_read_register, cc->gdb_write_register, 548 feature); 549 cpu->gdb_num_regs = cpu->gdb_num_g_regs = feature->num_regs; 550 } 551 552 if (cc->gdb_num_core_regs) { 553 cpu->gdb_num_regs = cpu->gdb_num_g_regs = cc->gdb_num_core_regs; 554 } 555 } 556 557 void gdb_register_coprocessor(CPUState *cpu, 558 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, 559 const GDBFeature *feature, int g_pos) 560 { 561 GDBRegisterState *s; 562 guint i; 563 int base_reg = cpu->gdb_num_regs; 564 565 for (i = 0; i < cpu->gdb_regs->len; i++) { 566 /* Check for duplicates. */ 567 s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); 568 if (s->feature == feature) { 569 return; 570 } 571 } 572 573 gdb_register_feature(cpu, base_reg, get_reg, set_reg, feature); 574 575 /* Add to end of list. */ 576 cpu->gdb_num_regs += feature->num_regs; 577 if (g_pos) { 578 if (g_pos != base_reg) { 579 error_report("Error: Bad gdb register numbering for '%s', " 580 "expected %d got %d", feature->xml, g_pos, base_reg); 581 } else { 582 cpu->gdb_num_g_regs = cpu->gdb_num_regs; 583 } 584 } 585 } 586 587 static void gdb_process_breakpoint_remove_all(GDBProcess *p) 588 { 589 CPUState *cpu = gdb_get_first_cpu_in_process(p); 590 591 while (cpu) { 592 gdb_breakpoint_remove_all(cpu); 593 cpu = gdb_next_cpu_in_process(cpu); 594 } 595 } 596 597 598 static void gdb_set_cpu_pc(vaddr pc) 599 { 600 CPUState *cpu = gdbserver_state.c_cpu; 601 602 cpu_synchronize_state(cpu); 603 cpu_set_pc(cpu, pc); 604 } 605 606 void gdb_append_thread_id(CPUState *cpu, GString *buf) 607 { 608 if (gdbserver_state.multiprocess) { 609 g_string_append_printf(buf, "p%02x.%02x", 610 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu)); 611 } else { 612 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu)); 613 } 614 } 615 616 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, 617 uint32_t *pid, uint32_t *tid) 618 { 619 unsigned long p, t; 620 int ret; 621 622 if (*buf == 'p') { 623 buf++; 624 ret = qemu_strtoul(buf, &buf, 16, &p); 625 626 if (ret) { 627 return GDB_READ_THREAD_ERR; 628 } 629 630 /* Skip '.' */ 631 buf++; 632 } else { 633 p = 0; 634 } 635 636 ret = qemu_strtoul(buf, &buf, 16, &t); 637 638 if (ret) { 639 return GDB_READ_THREAD_ERR; 640 } 641 642 *end_buf = buf; 643 644 if (p == -1) { 645 return GDB_ALL_PROCESSES; 646 } 647 648 if (pid) { 649 *pid = p; 650 } 651 652 if (t == -1) { 653 return GDB_ALL_THREADS; 654 } 655 656 if (tid) { 657 *tid = t; 658 } 659 660 return GDB_ONE_THREAD; 661 } 662 663 /** 664 * gdb_handle_vcont - Parses and handles a vCont packet. 665 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is 666 * a format error, 0 on success. 667 */ 668 static int gdb_handle_vcont(const char *p) 669 { 670 int res, signal = 0; 671 char cur_action; 672 unsigned long tmp; 673 uint32_t pid, tid; 674 GDBProcess *process; 675 CPUState *cpu; 676 GDBThreadIdKind kind; 677 unsigned int max_cpus = gdb_get_max_cpus(); 678 /* uninitialised CPUs stay 0 */ 679 g_autofree char *newstates = g_new0(char, max_cpus); 680 681 /* mark valid CPUs with 1 */ 682 CPU_FOREACH(cpu) { 683 newstates[cpu->cpu_index] = 1; 684 } 685 686 /* 687 * res keeps track of what error we are returning, with -ENOTSUP meaning 688 * that the command is unknown or unsupported, thus returning an empty 689 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, 690 * or incorrect parameters passed. 691 */ 692 res = 0; 693 694 /* 695 * target_count and last_target keep track of how many CPUs we are going to 696 * step or resume, and a pointer to the state structure of one of them, 697 * respectively 698 */ 699 int target_count = 0; 700 CPUState *last_target = NULL; 701 702 while (*p) { 703 if (*p++ != ';') { 704 return -ENOTSUP; 705 } 706 707 cur_action = *p++; 708 if (cur_action == 'C' || cur_action == 'S') { 709 cur_action = qemu_tolower(cur_action); 710 res = qemu_strtoul(p, &p, 16, &tmp); 711 if (res) { 712 return res; 713 } 714 signal = gdb_signal_to_target(tmp); 715 } else if (cur_action != 'c' && cur_action != 's') { 716 /* unknown/invalid/unsupported command */ 717 return -ENOTSUP; 718 } 719 720 if (*p == '\0' || *p == ';') { 721 /* 722 * No thread specifier, action is on "all threads". The 723 * specification is unclear regarding the process to act on. We 724 * choose all processes. 725 */ 726 kind = GDB_ALL_PROCESSES; 727 } else if (*p++ == ':') { 728 kind = read_thread_id(p, &p, &pid, &tid); 729 } else { 730 return -ENOTSUP; 731 } 732 733 switch (kind) { 734 case GDB_READ_THREAD_ERR: 735 return -EINVAL; 736 737 case GDB_ALL_PROCESSES: 738 cpu = gdb_first_attached_cpu(); 739 while (cpu) { 740 if (newstates[cpu->cpu_index] == 1) { 741 newstates[cpu->cpu_index] = cur_action; 742 743 target_count++; 744 last_target = cpu; 745 } 746 747 cpu = gdb_next_attached_cpu(cpu); 748 } 749 break; 750 751 case GDB_ALL_THREADS: 752 process = gdb_get_process(pid); 753 754 if (!process->attached) { 755 return -EINVAL; 756 } 757 758 cpu = gdb_get_first_cpu_in_process(process); 759 while (cpu) { 760 if (newstates[cpu->cpu_index] == 1) { 761 newstates[cpu->cpu_index] = cur_action; 762 763 target_count++; 764 last_target = cpu; 765 } 766 767 cpu = gdb_next_cpu_in_process(cpu); 768 } 769 break; 770 771 case GDB_ONE_THREAD: 772 cpu = gdb_get_cpu(pid, tid); 773 774 /* invalid CPU/thread specified */ 775 if (!cpu) { 776 return -EINVAL; 777 } 778 779 /* only use if no previous match occourred */ 780 if (newstates[cpu->cpu_index] == 1) { 781 newstates[cpu->cpu_index] = cur_action; 782 783 target_count++; 784 last_target = cpu; 785 } 786 break; 787 } 788 } 789 790 /* 791 * if we're about to resume a specific set of CPUs/threads, make it so that 792 * in case execution gets interrupted, we can send GDB a stop reply with a 793 * correct value. it doesn't really matter which CPU we tell GDB the signal 794 * happened in (VM pauses stop all of them anyway), so long as it is one of 795 * the ones we resumed/single stepped here. 796 */ 797 if (target_count > 0) { 798 gdbserver_state.c_cpu = last_target; 799 } 800 801 gdbserver_state.signal = signal; 802 gdb_continue_partial(newstates); 803 return res; 804 } 805 806 static const char *cmd_next_param(const char *param, const char delimiter) 807 { 808 static const char all_delimiters[] = ",;:="; 809 char curr_delimiters[2] = {0}; 810 const char *delimiters; 811 812 if (delimiter == '?') { 813 delimiters = all_delimiters; 814 } else if (delimiter == '0') { 815 return strchr(param, '\0'); 816 } else if (delimiter == '.' && *param) { 817 return param + 1; 818 } else { 819 curr_delimiters[0] = delimiter; 820 delimiters = curr_delimiters; 821 } 822 823 param += strcspn(param, delimiters); 824 if (*param) { 825 param++; 826 } 827 return param; 828 } 829 830 static int cmd_parse_params(const char *data, const char *schema, 831 GArray *params) 832 { 833 const char *curr_schema, *curr_data; 834 835 g_assert(schema); 836 g_assert(params->len == 0); 837 838 curr_schema = schema; 839 curr_data = data; 840 while (curr_schema[0] && curr_schema[1] && *curr_data) { 841 GdbCmdVariant this_param; 842 843 switch (curr_schema[0]) { 844 case 'l': 845 if (qemu_strtoul(curr_data, &curr_data, 16, 846 &this_param.val_ul)) { 847 return -EINVAL; 848 } 849 curr_data = cmd_next_param(curr_data, curr_schema[1]); 850 g_array_append_val(params, this_param); 851 break; 852 case 'L': 853 if (qemu_strtou64(curr_data, &curr_data, 16, 854 (uint64_t *)&this_param.val_ull)) { 855 return -EINVAL; 856 } 857 curr_data = cmd_next_param(curr_data, curr_schema[1]); 858 g_array_append_val(params, this_param); 859 break; 860 case 's': 861 this_param.data = curr_data; 862 curr_data = cmd_next_param(curr_data, curr_schema[1]); 863 g_array_append_val(params, this_param); 864 break; 865 case 'o': 866 this_param.opcode = *(uint8_t *)curr_data; 867 curr_data = cmd_next_param(curr_data, curr_schema[1]); 868 g_array_append_val(params, this_param); 869 break; 870 case 't': 871 this_param.thread_id.kind = 872 read_thread_id(curr_data, &curr_data, 873 &this_param.thread_id.pid, 874 &this_param.thread_id.tid); 875 curr_data = cmd_next_param(curr_data, curr_schema[1]); 876 g_array_append_val(params, this_param); 877 break; 878 case '?': 879 curr_data = cmd_next_param(curr_data, curr_schema[1]); 880 break; 881 default: 882 return -EINVAL; 883 } 884 curr_schema += 2; 885 } 886 887 return 0; 888 } 889 890 typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); 891 892 /* 893 * cmd_startswith -> cmd is compared using startswith 894 * 895 * allow_stop_reply -> true iff the gdbstub can respond to this command with a 896 * "stop reply" packet. The list of commands that accept such response is 897 * defined at the GDB Remote Serial Protocol documentation. see: 898 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets. 899 * 900 * schema definitions: 901 * Each schema parameter entry consists of 2 chars, 902 * the first char represents the parameter type handling 903 * the second char represents the delimiter for the next parameter 904 * 905 * Currently supported schema types: 906 * 'l' -> unsigned long (stored in .val_ul) 907 * 'L' -> unsigned long long (stored in .val_ull) 908 * 's' -> string (stored in .data) 909 * 'o' -> single char (stored in .opcode) 910 * 't' -> thread id (stored in .thread_id) 911 * '?' -> skip according to delimiter 912 * 913 * Currently supported delimiters: 914 * '?' -> Stop at any delimiter (",;:=\0") 915 * '0' -> Stop at "\0" 916 * '.' -> Skip 1 char unless reached "\0" 917 * Any other value is treated as the delimiter value itself 918 */ 919 typedef struct GdbCmdParseEntry { 920 GdbCmdHandler handler; 921 const char *cmd; 922 bool cmd_startswith; 923 const char *schema; 924 bool allow_stop_reply; 925 } GdbCmdParseEntry; 926 927 static inline int startswith(const char *string, const char *pattern) 928 { 929 return !strncmp(string, pattern, strlen(pattern)); 930 } 931 932 static int process_string_cmd(const char *data, 933 const GdbCmdParseEntry *cmds, int num_cmds) 934 { 935 int i; 936 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); 937 938 if (!cmds) { 939 return -1; 940 } 941 942 for (i = 0; i < num_cmds; i++) { 943 const GdbCmdParseEntry *cmd = &cmds[i]; 944 g_assert(cmd->handler && cmd->cmd); 945 946 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || 947 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { 948 continue; 949 } 950 951 if (cmd->schema) { 952 if (cmd_parse_params(&data[strlen(cmd->cmd)], 953 cmd->schema, params)) { 954 return -1; 955 } 956 } 957 958 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply; 959 cmd->handler(params, NULL); 960 return 0; 961 } 962 963 return -1; 964 } 965 966 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) 967 { 968 if (!data) { 969 return; 970 } 971 972 g_string_set_size(gdbserver_state.str_buf, 0); 973 g_byte_array_set_size(gdbserver_state.mem_buf, 0); 974 975 /* In case there was an error during the command parsing we must 976 * send a NULL packet to indicate the command is not supported */ 977 if (process_string_cmd(data, cmd, 1)) { 978 gdb_put_packet(""); 979 } 980 } 981 982 static void handle_detach(GArray *params, void *user_ctx) 983 { 984 GDBProcess *process; 985 uint32_t pid = 1; 986 987 if (gdbserver_state.multiprocess) { 988 if (!params->len) { 989 gdb_put_packet("E22"); 990 return; 991 } 992 993 pid = get_param(params, 0)->val_ul; 994 } 995 996 process = gdb_get_process(pid); 997 gdb_process_breakpoint_remove_all(process); 998 process->attached = false; 999 1000 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) { 1001 gdbserver_state.c_cpu = gdb_first_attached_cpu(); 1002 } 1003 1004 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) { 1005 gdbserver_state.g_cpu = gdb_first_attached_cpu(); 1006 } 1007 1008 if (!gdbserver_state.c_cpu) { 1009 /* No more process attached */ 1010 gdb_disable_syscalls(); 1011 gdb_continue(); 1012 } 1013 gdb_put_packet("OK"); 1014 } 1015 1016 static void handle_thread_alive(GArray *params, void *user_ctx) 1017 { 1018 CPUState *cpu; 1019 1020 if (!params->len) { 1021 gdb_put_packet("E22"); 1022 return; 1023 } 1024 1025 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { 1026 gdb_put_packet("E22"); 1027 return; 1028 } 1029 1030 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, 1031 get_param(params, 0)->thread_id.tid); 1032 if (!cpu) { 1033 gdb_put_packet("E22"); 1034 return; 1035 } 1036 1037 gdb_put_packet("OK"); 1038 } 1039 1040 static void handle_continue(GArray *params, void *user_ctx) 1041 { 1042 if (params->len) { 1043 gdb_set_cpu_pc(get_param(params, 0)->val_ull); 1044 } 1045 1046 gdbserver_state.signal = 0; 1047 gdb_continue(); 1048 } 1049 1050 static void handle_cont_with_sig(GArray *params, void *user_ctx) 1051 { 1052 unsigned long signal = 0; 1053 1054 /* 1055 * Note: C sig;[addr] is currently unsupported and we simply 1056 * omit the addr parameter 1057 */ 1058 if (params->len) { 1059 signal = get_param(params, 0)->val_ul; 1060 } 1061 1062 gdbserver_state.signal = gdb_signal_to_target(signal); 1063 if (gdbserver_state.signal == -1) { 1064 gdbserver_state.signal = 0; 1065 } 1066 gdb_continue(); 1067 } 1068 1069 static void handle_set_thread(GArray *params, void *user_ctx) 1070 { 1071 CPUState *cpu; 1072 1073 if (params->len != 2) { 1074 gdb_put_packet("E22"); 1075 return; 1076 } 1077 1078 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { 1079 gdb_put_packet("E22"); 1080 return; 1081 } 1082 1083 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { 1084 gdb_put_packet("OK"); 1085 return; 1086 } 1087 1088 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid, 1089 get_param(params, 1)->thread_id.tid); 1090 if (!cpu) { 1091 gdb_put_packet("E22"); 1092 return; 1093 } 1094 1095 /* 1096 * Note: This command is deprecated and modern gdb's will be using the 1097 * vCont command instead. 1098 */ 1099 switch (get_param(params, 0)->opcode) { 1100 case 'c': 1101 gdbserver_state.c_cpu = cpu; 1102 gdb_put_packet("OK"); 1103 break; 1104 case 'g': 1105 gdbserver_state.g_cpu = cpu; 1106 gdb_put_packet("OK"); 1107 break; 1108 default: 1109 gdb_put_packet("E22"); 1110 break; 1111 } 1112 } 1113 1114 static void handle_insert_bp(GArray *params, void *user_ctx) 1115 { 1116 int res; 1117 1118 if (params->len != 3) { 1119 gdb_put_packet("E22"); 1120 return; 1121 } 1122 1123 res = gdb_breakpoint_insert(gdbserver_state.c_cpu, 1124 get_param(params, 0)->val_ul, 1125 get_param(params, 1)->val_ull, 1126 get_param(params, 2)->val_ull); 1127 if (res >= 0) { 1128 gdb_put_packet("OK"); 1129 return; 1130 } else if (res == -ENOSYS) { 1131 gdb_put_packet(""); 1132 return; 1133 } 1134 1135 gdb_put_packet("E22"); 1136 } 1137 1138 static void handle_remove_bp(GArray *params, void *user_ctx) 1139 { 1140 int res; 1141 1142 if (params->len != 3) { 1143 gdb_put_packet("E22"); 1144 return; 1145 } 1146 1147 res = gdb_breakpoint_remove(gdbserver_state.c_cpu, 1148 get_param(params, 0)->val_ul, 1149 get_param(params, 1)->val_ull, 1150 get_param(params, 2)->val_ull); 1151 if (res >= 0) { 1152 gdb_put_packet("OK"); 1153 return; 1154 } else if (res == -ENOSYS) { 1155 gdb_put_packet(""); 1156 return; 1157 } 1158 1159 gdb_put_packet("E22"); 1160 } 1161 1162 /* 1163 * handle_set/get_reg 1164 * 1165 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available. 1166 * This works, but can be very slow. Anything new enough to understand 1167 * XML also knows how to use this properly. However to use this we 1168 * need to define a local XML file as well as be talking to a 1169 * reasonably modern gdb. Responding with an empty packet will cause 1170 * the remote gdb to fallback to older methods. 1171 */ 1172 1173 static void handle_set_reg(GArray *params, void *user_ctx) 1174 { 1175 int reg_size; 1176 1177 if (params->len != 2) { 1178 gdb_put_packet("E22"); 1179 return; 1180 } 1181 1182 reg_size = strlen(get_param(params, 1)->data) / 2; 1183 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); 1184 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, 1185 get_param(params, 0)->val_ull); 1186 gdb_put_packet("OK"); 1187 } 1188 1189 static void handle_get_reg(GArray *params, void *user_ctx) 1190 { 1191 int reg_size; 1192 1193 if (!params->len) { 1194 gdb_put_packet("E14"); 1195 return; 1196 } 1197 1198 reg_size = gdb_read_register(gdbserver_state.g_cpu, 1199 gdbserver_state.mem_buf, 1200 get_param(params, 0)->val_ull); 1201 if (!reg_size) { 1202 gdb_put_packet("E14"); 1203 return; 1204 } else { 1205 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); 1206 } 1207 1208 gdb_memtohex(gdbserver_state.str_buf, 1209 gdbserver_state.mem_buf->data, reg_size); 1210 gdb_put_strbuf(); 1211 } 1212 1213 static void handle_write_mem(GArray *params, void *user_ctx) 1214 { 1215 if (params->len != 3) { 1216 gdb_put_packet("E22"); 1217 return; 1218 } 1219 1220 /* gdb_hextomem() reads 2*len bytes */ 1221 if (get_param(params, 1)->val_ull > 1222 strlen(get_param(params, 2)->data) / 2) { 1223 gdb_put_packet("E22"); 1224 return; 1225 } 1226 1227 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, 1228 get_param(params, 1)->val_ull); 1229 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, 1230 get_param(params, 0)->val_ull, 1231 gdbserver_state.mem_buf->data, 1232 gdbserver_state.mem_buf->len, true)) { 1233 gdb_put_packet("E14"); 1234 return; 1235 } 1236 1237 gdb_put_packet("OK"); 1238 } 1239 1240 static void handle_read_mem(GArray *params, void *user_ctx) 1241 { 1242 if (params->len != 2) { 1243 gdb_put_packet("E22"); 1244 return; 1245 } 1246 1247 /* gdb_memtohex() doubles the required space */ 1248 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { 1249 gdb_put_packet("E22"); 1250 return; 1251 } 1252 1253 g_byte_array_set_size(gdbserver_state.mem_buf, 1254 get_param(params, 1)->val_ull); 1255 1256 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, 1257 get_param(params, 0)->val_ull, 1258 gdbserver_state.mem_buf->data, 1259 gdbserver_state.mem_buf->len, false)) { 1260 gdb_put_packet("E14"); 1261 return; 1262 } 1263 1264 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, 1265 gdbserver_state.mem_buf->len); 1266 gdb_put_strbuf(); 1267 } 1268 1269 static void handle_write_all_regs(GArray *params, void *user_ctx) 1270 { 1271 int reg_id; 1272 size_t len; 1273 uint8_t *registers; 1274 int reg_size; 1275 1276 if (!params->len) { 1277 return; 1278 } 1279 1280 cpu_synchronize_state(gdbserver_state.g_cpu); 1281 len = strlen(get_param(params, 0)->data) / 2; 1282 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); 1283 registers = gdbserver_state.mem_buf->data; 1284 for (reg_id = 0; 1285 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; 1286 reg_id++) { 1287 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id); 1288 len -= reg_size; 1289 registers += reg_size; 1290 } 1291 gdb_put_packet("OK"); 1292 } 1293 1294 static void handle_read_all_regs(GArray *params, void *user_ctx) 1295 { 1296 int reg_id; 1297 size_t len; 1298 1299 cpu_synchronize_state(gdbserver_state.g_cpu); 1300 g_byte_array_set_size(gdbserver_state.mem_buf, 0); 1301 len = 0; 1302 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) { 1303 len += gdb_read_register(gdbserver_state.g_cpu, 1304 gdbserver_state.mem_buf, 1305 reg_id); 1306 } 1307 g_assert(len == gdbserver_state.mem_buf->len); 1308 1309 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); 1310 gdb_put_strbuf(); 1311 } 1312 1313 1314 static void handle_step(GArray *params, void *user_ctx) 1315 { 1316 if (params->len) { 1317 gdb_set_cpu_pc(get_param(params, 0)->val_ull); 1318 } 1319 1320 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); 1321 gdb_continue(); 1322 } 1323 1324 static void handle_backward(GArray *params, void *user_ctx) 1325 { 1326 if (!gdb_can_reverse()) { 1327 gdb_put_packet("E22"); 1328 } 1329 if (params->len == 1) { 1330 switch (get_param(params, 0)->opcode) { 1331 case 's': 1332 if (replay_reverse_step()) { 1333 gdb_continue(); 1334 } else { 1335 gdb_put_packet("E14"); 1336 } 1337 return; 1338 case 'c': 1339 if (replay_reverse_continue()) { 1340 gdb_continue(); 1341 } else { 1342 gdb_put_packet("E14"); 1343 } 1344 return; 1345 } 1346 } 1347 1348 /* Default invalid command */ 1349 gdb_put_packet(""); 1350 } 1351 1352 static void handle_v_cont_query(GArray *params, void *user_ctx) 1353 { 1354 gdb_put_packet("vCont;c;C;s;S"); 1355 } 1356 1357 static void handle_v_cont(GArray *params, void *user_ctx) 1358 { 1359 int res; 1360 1361 if (!params->len) { 1362 return; 1363 } 1364 1365 res = gdb_handle_vcont(get_param(params, 0)->data); 1366 if ((res == -EINVAL) || (res == -ERANGE)) { 1367 gdb_put_packet("E22"); 1368 } else if (res) { 1369 gdb_put_packet(""); 1370 } 1371 } 1372 1373 static void handle_v_attach(GArray *params, void *user_ctx) 1374 { 1375 GDBProcess *process; 1376 CPUState *cpu; 1377 1378 g_string_assign(gdbserver_state.str_buf, "E22"); 1379 if (!params->len) { 1380 goto cleanup; 1381 } 1382 1383 process = gdb_get_process(get_param(params, 0)->val_ul); 1384 if (!process) { 1385 goto cleanup; 1386 } 1387 1388 cpu = gdb_get_first_cpu_in_process(process); 1389 if (!cpu) { 1390 goto cleanup; 1391 } 1392 1393 process->attached = true; 1394 gdbserver_state.g_cpu = cpu; 1395 gdbserver_state.c_cpu = cpu; 1396 1397 if (gdbserver_state.allow_stop_reply) { 1398 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); 1399 gdb_append_thread_id(cpu, gdbserver_state.str_buf); 1400 g_string_append_c(gdbserver_state.str_buf, ';'); 1401 gdbserver_state.allow_stop_reply = false; 1402 cleanup: 1403 gdb_put_strbuf(); 1404 } 1405 } 1406 1407 static void handle_v_kill(GArray *params, void *user_ctx) 1408 { 1409 /* Kill the target */ 1410 gdb_put_packet("OK"); 1411 error_report("QEMU: Terminated via GDBstub"); 1412 gdb_exit(0); 1413 gdb_qemu_exit(0); 1414 } 1415 1416 static const GdbCmdParseEntry gdb_v_commands_table[] = { 1417 /* Order is important if has same prefix */ 1418 { 1419 .handler = handle_v_cont_query, 1420 .cmd = "Cont?", 1421 .cmd_startswith = 1 1422 }, 1423 { 1424 .handler = handle_v_cont, 1425 .cmd = "Cont", 1426 .cmd_startswith = 1, 1427 .allow_stop_reply = true, 1428 .schema = "s0" 1429 }, 1430 { 1431 .handler = handle_v_attach, 1432 .cmd = "Attach;", 1433 .cmd_startswith = 1, 1434 .allow_stop_reply = true, 1435 .schema = "l0" 1436 }, 1437 { 1438 .handler = handle_v_kill, 1439 .cmd = "Kill;", 1440 .cmd_startswith = 1 1441 }, 1442 #ifdef CONFIG_USER_ONLY 1443 /* 1444 * Host I/O Packets. See [1] for details. 1445 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html 1446 */ 1447 { 1448 .handler = gdb_handle_v_file_open, 1449 .cmd = "File:open:", 1450 .cmd_startswith = 1, 1451 .schema = "s,L,L0" 1452 }, 1453 { 1454 .handler = gdb_handle_v_file_close, 1455 .cmd = "File:close:", 1456 .cmd_startswith = 1, 1457 .schema = "l0" 1458 }, 1459 { 1460 .handler = gdb_handle_v_file_pread, 1461 .cmd = "File:pread:", 1462 .cmd_startswith = 1, 1463 .schema = "l,L,L0" 1464 }, 1465 { 1466 .handler = gdb_handle_v_file_readlink, 1467 .cmd = "File:readlink:", 1468 .cmd_startswith = 1, 1469 .schema = "s0" 1470 }, 1471 #endif 1472 }; 1473 1474 static void handle_v_commands(GArray *params, void *user_ctx) 1475 { 1476 if (!params->len) { 1477 return; 1478 } 1479 1480 if (process_string_cmd(get_param(params, 0)->data, 1481 gdb_v_commands_table, 1482 ARRAY_SIZE(gdb_v_commands_table))) { 1483 gdb_put_packet(""); 1484 } 1485 } 1486 1487 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx) 1488 { 1489 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE); 1490 1491 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) { 1492 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x", 1493 SSTEP_NOIRQ); 1494 } 1495 1496 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) { 1497 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x", 1498 SSTEP_NOTIMER); 1499 } 1500 1501 gdb_put_strbuf(); 1502 } 1503 1504 static void handle_set_qemu_sstep(GArray *params, void *user_ctx) 1505 { 1506 int new_sstep_flags; 1507 1508 if (!params->len) { 1509 return; 1510 } 1511 1512 new_sstep_flags = get_param(params, 0)->val_ul; 1513 1514 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { 1515 gdb_put_packet("E22"); 1516 return; 1517 } 1518 1519 gdbserver_state.sstep_flags = new_sstep_flags; 1520 gdb_put_packet("OK"); 1521 } 1522 1523 static void handle_query_qemu_sstep(GArray *params, void *user_ctx) 1524 { 1525 g_string_printf(gdbserver_state.str_buf, "0x%x", 1526 gdbserver_state.sstep_flags); 1527 gdb_put_strbuf(); 1528 } 1529 1530 static void handle_query_curr_tid(GArray *params, void *user_ctx) 1531 { 1532 CPUState *cpu; 1533 GDBProcess *process; 1534 1535 /* 1536 * "Current thread" remains vague in the spec, so always return 1537 * the first thread of the current process (gdb returns the 1538 * first thread). 1539 */ 1540 process = gdb_get_cpu_process(gdbserver_state.g_cpu); 1541 cpu = gdb_get_first_cpu_in_process(process); 1542 g_string_assign(gdbserver_state.str_buf, "QC"); 1543 gdb_append_thread_id(cpu, gdbserver_state.str_buf); 1544 gdb_put_strbuf(); 1545 } 1546 1547 static void handle_query_threads(GArray *params, void *user_ctx) 1548 { 1549 if (!gdbserver_state.query_cpu) { 1550 gdb_put_packet("l"); 1551 return; 1552 } 1553 1554 g_string_assign(gdbserver_state.str_buf, "m"); 1555 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf); 1556 gdb_put_strbuf(); 1557 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu); 1558 } 1559 1560 static void handle_query_first_threads(GArray *params, void *user_ctx) 1561 { 1562 gdbserver_state.query_cpu = gdb_first_attached_cpu(); 1563 handle_query_threads(params, user_ctx); 1564 } 1565 1566 static void handle_query_thread_extra(GArray *params, void *user_ctx) 1567 { 1568 g_autoptr(GString) rs = g_string_new(NULL); 1569 CPUState *cpu; 1570 1571 if (!params->len || 1572 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { 1573 gdb_put_packet("E22"); 1574 return; 1575 } 1576 1577 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, 1578 get_param(params, 0)->thread_id.tid); 1579 if (!cpu) { 1580 return; 1581 } 1582 1583 cpu_synchronize_state(cpu); 1584 1585 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) { 1586 /* Print the CPU model and name in multiprocess mode */ 1587 ObjectClass *oc = object_get_class(OBJECT(cpu)); 1588 const char *cpu_model = object_class_get_name(oc); 1589 const char *cpu_name = 1590 object_get_canonical_path_component(OBJECT(cpu)); 1591 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name, 1592 cpu->halted ? "halted " : "running"); 1593 } else { 1594 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, 1595 cpu->halted ? "halted " : "running"); 1596 } 1597 trace_gdbstub_op_extra_info(rs->str); 1598 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len); 1599 gdb_put_strbuf(); 1600 } 1601 1602 static void handle_query_supported(GArray *params, void *user_ctx) 1603 { 1604 CPUClass *cc; 1605 1606 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH); 1607 cc = CPU_GET_CLASS(first_cpu); 1608 if (cc->gdb_core_xml_file) { 1609 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+"); 1610 } 1611 1612 if (gdb_can_reverse()) { 1613 g_string_append(gdbserver_state.str_buf, 1614 ";ReverseStep+;ReverseContinue+"); 1615 } 1616 1617 #if defined(CONFIG_USER_ONLY) 1618 #if defined(CONFIG_LINUX) 1619 if (gdbserver_state.c_cpu->opaque) { 1620 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); 1621 } 1622 g_string_append(gdbserver_state.str_buf, ";QCatchSyscalls+"); 1623 #endif 1624 g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+"); 1625 #endif 1626 1627 if (params->len && 1628 strstr(get_param(params, 0)->data, "multiprocess+")) { 1629 gdbserver_state.multiprocess = true; 1630 } 1631 1632 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); 1633 gdb_put_strbuf(); 1634 } 1635 1636 static void handle_query_xfer_features(GArray *params, void *user_ctx) 1637 { 1638 GDBProcess *process; 1639 CPUClass *cc; 1640 unsigned long len, total_len, addr; 1641 const char *xml; 1642 const char *p; 1643 1644 if (params->len < 3) { 1645 gdb_put_packet("E22"); 1646 return; 1647 } 1648 1649 process = gdb_get_cpu_process(gdbserver_state.g_cpu); 1650 cc = CPU_GET_CLASS(gdbserver_state.g_cpu); 1651 if (!cc->gdb_core_xml_file) { 1652 gdb_put_packet(""); 1653 return; 1654 } 1655 1656 p = get_param(params, 0)->data; 1657 xml = get_feature_xml(p, &p, process); 1658 if (!xml) { 1659 gdb_put_packet("E00"); 1660 return; 1661 } 1662 1663 addr = get_param(params, 1)->val_ul; 1664 len = get_param(params, 2)->val_ul; 1665 total_len = strlen(xml); 1666 if (addr > total_len) { 1667 gdb_put_packet("E00"); 1668 return; 1669 } 1670 1671 if (len > (MAX_PACKET_LENGTH - 5) / 2) { 1672 len = (MAX_PACKET_LENGTH - 5) / 2; 1673 } 1674 1675 if (len < total_len - addr) { 1676 g_string_assign(gdbserver_state.str_buf, "m"); 1677 gdb_memtox(gdbserver_state.str_buf, xml + addr, len); 1678 } else { 1679 g_string_assign(gdbserver_state.str_buf, "l"); 1680 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr); 1681 } 1682 1683 gdb_put_packet_binary(gdbserver_state.str_buf->str, 1684 gdbserver_state.str_buf->len, true); 1685 } 1686 1687 static void handle_query_qemu_supported(GArray *params, void *user_ctx) 1688 { 1689 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep"); 1690 #ifndef CONFIG_USER_ONLY 1691 g_string_append(gdbserver_state.str_buf, ";PhyMemMode"); 1692 #endif 1693 gdb_put_strbuf(); 1694 } 1695 1696 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { 1697 /* Order is important if has same prefix */ 1698 { 1699 .handler = handle_query_qemu_sstepbits, 1700 .cmd = "qemu.sstepbits", 1701 }, 1702 { 1703 .handler = handle_query_qemu_sstep, 1704 .cmd = "qemu.sstep", 1705 }, 1706 { 1707 .handler = handle_set_qemu_sstep, 1708 .cmd = "qemu.sstep=", 1709 .cmd_startswith = 1, 1710 .schema = "l0" 1711 }, 1712 }; 1713 1714 static const GdbCmdParseEntry gdb_gen_query_table[] = { 1715 { 1716 .handler = handle_query_curr_tid, 1717 .cmd = "C", 1718 }, 1719 { 1720 .handler = handle_query_threads, 1721 .cmd = "sThreadInfo", 1722 }, 1723 { 1724 .handler = handle_query_first_threads, 1725 .cmd = "fThreadInfo", 1726 }, 1727 { 1728 .handler = handle_query_thread_extra, 1729 .cmd = "ThreadExtraInfo,", 1730 .cmd_startswith = 1, 1731 .schema = "t0" 1732 }, 1733 #ifdef CONFIG_USER_ONLY 1734 { 1735 .handler = gdb_handle_query_offsets, 1736 .cmd = "Offsets", 1737 }, 1738 #else 1739 { 1740 .handler = gdb_handle_query_rcmd, 1741 .cmd = "Rcmd,", 1742 .cmd_startswith = 1, 1743 .schema = "s0" 1744 }, 1745 #endif 1746 { 1747 .handler = handle_query_supported, 1748 .cmd = "Supported:", 1749 .cmd_startswith = 1, 1750 .schema = "s0" 1751 }, 1752 { 1753 .handler = handle_query_supported, 1754 .cmd = "Supported", 1755 .schema = "s0" 1756 }, 1757 { 1758 .handler = handle_query_xfer_features, 1759 .cmd = "Xfer:features:read:", 1760 .cmd_startswith = 1, 1761 .schema = "s:l,l0" 1762 }, 1763 #if defined(CONFIG_USER_ONLY) 1764 #if defined(CONFIG_LINUX) 1765 { 1766 .handler = gdb_handle_query_xfer_auxv, 1767 .cmd = "Xfer:auxv:read::", 1768 .cmd_startswith = 1, 1769 .schema = "l,l0" 1770 }, 1771 #endif 1772 { 1773 .handler = gdb_handle_query_xfer_exec_file, 1774 .cmd = "Xfer:exec-file:read:", 1775 .cmd_startswith = 1, 1776 .schema = "l:l,l0" 1777 }, 1778 #endif 1779 { 1780 .handler = gdb_handle_query_attached, 1781 .cmd = "Attached:", 1782 .cmd_startswith = 1 1783 }, 1784 { 1785 .handler = gdb_handle_query_attached, 1786 .cmd = "Attached", 1787 }, 1788 { 1789 .handler = handle_query_qemu_supported, 1790 .cmd = "qemu.Supported", 1791 }, 1792 #ifndef CONFIG_USER_ONLY 1793 { 1794 .handler = gdb_handle_query_qemu_phy_mem_mode, 1795 .cmd = "qemu.PhyMemMode", 1796 }, 1797 #endif 1798 }; 1799 1800 static const GdbCmdParseEntry gdb_gen_set_table[] = { 1801 /* Order is important if has same prefix */ 1802 { 1803 .handler = handle_set_qemu_sstep, 1804 .cmd = "qemu.sstep:", 1805 .cmd_startswith = 1, 1806 .schema = "l0" 1807 }, 1808 #ifndef CONFIG_USER_ONLY 1809 { 1810 .handler = gdb_handle_set_qemu_phy_mem_mode, 1811 .cmd = "qemu.PhyMemMode:", 1812 .cmd_startswith = 1, 1813 .schema = "l0" 1814 }, 1815 #endif 1816 #if defined(CONFIG_USER_ONLY) 1817 { 1818 .handler = gdb_handle_set_catch_syscalls, 1819 .cmd = "CatchSyscalls:", 1820 .cmd_startswith = 1, 1821 .schema = "s0", 1822 }, 1823 #endif 1824 }; 1825 1826 static void handle_gen_query(GArray *params, void *user_ctx) 1827 { 1828 if (!params->len) { 1829 return; 1830 } 1831 1832 if (!process_string_cmd(get_param(params, 0)->data, 1833 gdb_gen_query_set_common_table, 1834 ARRAY_SIZE(gdb_gen_query_set_common_table))) { 1835 return; 1836 } 1837 1838 if (process_string_cmd(get_param(params, 0)->data, 1839 gdb_gen_query_table, 1840 ARRAY_SIZE(gdb_gen_query_table))) { 1841 gdb_put_packet(""); 1842 } 1843 } 1844 1845 static void handle_gen_set(GArray *params, void *user_ctx) 1846 { 1847 if (!params->len) { 1848 return; 1849 } 1850 1851 if (!process_string_cmd(get_param(params, 0)->data, 1852 gdb_gen_query_set_common_table, 1853 ARRAY_SIZE(gdb_gen_query_set_common_table))) { 1854 return; 1855 } 1856 1857 if (process_string_cmd(get_param(params, 0)->data, 1858 gdb_gen_set_table, 1859 ARRAY_SIZE(gdb_gen_set_table))) { 1860 gdb_put_packet(""); 1861 } 1862 } 1863 1864 static void handle_target_halt(GArray *params, void *user_ctx) 1865 { 1866 if (gdbserver_state.allow_stop_reply) { 1867 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); 1868 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf); 1869 g_string_append_c(gdbserver_state.str_buf, ';'); 1870 gdb_put_strbuf(); 1871 gdbserver_state.allow_stop_reply = false; 1872 } 1873 /* 1874 * Remove all the breakpoints when this query is issued, 1875 * because gdb is doing an initial connect and the state 1876 * should be cleaned up. 1877 */ 1878 gdb_breakpoint_remove_all(gdbserver_state.c_cpu); 1879 } 1880 1881 static int gdb_handle_packet(const char *line_buf) 1882 { 1883 const GdbCmdParseEntry *cmd_parser = NULL; 1884 1885 trace_gdbstub_io_command(line_buf); 1886 1887 switch (line_buf[0]) { 1888 case '!': 1889 gdb_put_packet("OK"); 1890 break; 1891 case '?': 1892 { 1893 static const GdbCmdParseEntry target_halted_cmd_desc = { 1894 .handler = handle_target_halt, 1895 .cmd = "?", 1896 .cmd_startswith = 1, 1897 .allow_stop_reply = true, 1898 }; 1899 cmd_parser = &target_halted_cmd_desc; 1900 } 1901 break; 1902 case 'c': 1903 { 1904 static const GdbCmdParseEntry continue_cmd_desc = { 1905 .handler = handle_continue, 1906 .cmd = "c", 1907 .cmd_startswith = 1, 1908 .allow_stop_reply = true, 1909 .schema = "L0" 1910 }; 1911 cmd_parser = &continue_cmd_desc; 1912 } 1913 break; 1914 case 'C': 1915 { 1916 static const GdbCmdParseEntry cont_with_sig_cmd_desc = { 1917 .handler = handle_cont_with_sig, 1918 .cmd = "C", 1919 .cmd_startswith = 1, 1920 .allow_stop_reply = true, 1921 .schema = "l0" 1922 }; 1923 cmd_parser = &cont_with_sig_cmd_desc; 1924 } 1925 break; 1926 case 'v': 1927 { 1928 static const GdbCmdParseEntry v_cmd_desc = { 1929 .handler = handle_v_commands, 1930 .cmd = "v", 1931 .cmd_startswith = 1, 1932 .schema = "s0" 1933 }; 1934 cmd_parser = &v_cmd_desc; 1935 } 1936 break; 1937 case 'k': 1938 /* Kill the target */ 1939 error_report("QEMU: Terminated via GDBstub"); 1940 gdb_exit(0); 1941 gdb_qemu_exit(0); 1942 break; 1943 case 'D': 1944 { 1945 static const GdbCmdParseEntry detach_cmd_desc = { 1946 .handler = handle_detach, 1947 .cmd = "D", 1948 .cmd_startswith = 1, 1949 .schema = "?.l0" 1950 }; 1951 cmd_parser = &detach_cmd_desc; 1952 } 1953 break; 1954 case 's': 1955 { 1956 static const GdbCmdParseEntry step_cmd_desc = { 1957 .handler = handle_step, 1958 .cmd = "s", 1959 .cmd_startswith = 1, 1960 .allow_stop_reply = true, 1961 .schema = "L0" 1962 }; 1963 cmd_parser = &step_cmd_desc; 1964 } 1965 break; 1966 case 'b': 1967 { 1968 static const GdbCmdParseEntry backward_cmd_desc = { 1969 .handler = handle_backward, 1970 .cmd = "b", 1971 .cmd_startswith = 1, 1972 .allow_stop_reply = true, 1973 .schema = "o0" 1974 }; 1975 cmd_parser = &backward_cmd_desc; 1976 } 1977 break; 1978 case 'F': 1979 { 1980 static const GdbCmdParseEntry file_io_cmd_desc = { 1981 .handler = gdb_handle_file_io, 1982 .cmd = "F", 1983 .cmd_startswith = 1, 1984 .schema = "L,L,o0" 1985 }; 1986 cmd_parser = &file_io_cmd_desc; 1987 } 1988 break; 1989 case 'g': 1990 { 1991 static const GdbCmdParseEntry read_all_regs_cmd_desc = { 1992 .handler = handle_read_all_regs, 1993 .cmd = "g", 1994 .cmd_startswith = 1 1995 }; 1996 cmd_parser = &read_all_regs_cmd_desc; 1997 } 1998 break; 1999 case 'G': 2000 { 2001 static const GdbCmdParseEntry write_all_regs_cmd_desc = { 2002 .handler = handle_write_all_regs, 2003 .cmd = "G", 2004 .cmd_startswith = 1, 2005 .schema = "s0" 2006 }; 2007 cmd_parser = &write_all_regs_cmd_desc; 2008 } 2009 break; 2010 case 'm': 2011 { 2012 static const GdbCmdParseEntry read_mem_cmd_desc = { 2013 .handler = handle_read_mem, 2014 .cmd = "m", 2015 .cmd_startswith = 1, 2016 .schema = "L,L0" 2017 }; 2018 cmd_parser = &read_mem_cmd_desc; 2019 } 2020 break; 2021 case 'M': 2022 { 2023 static const GdbCmdParseEntry write_mem_cmd_desc = { 2024 .handler = handle_write_mem, 2025 .cmd = "M", 2026 .cmd_startswith = 1, 2027 .schema = "L,L:s0" 2028 }; 2029 cmd_parser = &write_mem_cmd_desc; 2030 } 2031 break; 2032 case 'p': 2033 { 2034 static const GdbCmdParseEntry get_reg_cmd_desc = { 2035 .handler = handle_get_reg, 2036 .cmd = "p", 2037 .cmd_startswith = 1, 2038 .schema = "L0" 2039 }; 2040 cmd_parser = &get_reg_cmd_desc; 2041 } 2042 break; 2043 case 'P': 2044 { 2045 static const GdbCmdParseEntry set_reg_cmd_desc = { 2046 .handler = handle_set_reg, 2047 .cmd = "P", 2048 .cmd_startswith = 1, 2049 .schema = "L?s0" 2050 }; 2051 cmd_parser = &set_reg_cmd_desc; 2052 } 2053 break; 2054 case 'Z': 2055 { 2056 static const GdbCmdParseEntry insert_bp_cmd_desc = { 2057 .handler = handle_insert_bp, 2058 .cmd = "Z", 2059 .cmd_startswith = 1, 2060 .schema = "l?L?L0" 2061 }; 2062 cmd_parser = &insert_bp_cmd_desc; 2063 } 2064 break; 2065 case 'z': 2066 { 2067 static const GdbCmdParseEntry remove_bp_cmd_desc = { 2068 .handler = handle_remove_bp, 2069 .cmd = "z", 2070 .cmd_startswith = 1, 2071 .schema = "l?L?L0" 2072 }; 2073 cmd_parser = &remove_bp_cmd_desc; 2074 } 2075 break; 2076 case 'H': 2077 { 2078 static const GdbCmdParseEntry set_thread_cmd_desc = { 2079 .handler = handle_set_thread, 2080 .cmd = "H", 2081 .cmd_startswith = 1, 2082 .schema = "o.t0" 2083 }; 2084 cmd_parser = &set_thread_cmd_desc; 2085 } 2086 break; 2087 case 'T': 2088 { 2089 static const GdbCmdParseEntry thread_alive_cmd_desc = { 2090 .handler = handle_thread_alive, 2091 .cmd = "T", 2092 .cmd_startswith = 1, 2093 .schema = "t0" 2094 }; 2095 cmd_parser = &thread_alive_cmd_desc; 2096 } 2097 break; 2098 case 'q': 2099 { 2100 static const GdbCmdParseEntry gen_query_cmd_desc = { 2101 .handler = handle_gen_query, 2102 .cmd = "q", 2103 .cmd_startswith = 1, 2104 .schema = "s0" 2105 }; 2106 cmd_parser = &gen_query_cmd_desc; 2107 } 2108 break; 2109 case 'Q': 2110 { 2111 static const GdbCmdParseEntry gen_set_cmd_desc = { 2112 .handler = handle_gen_set, 2113 .cmd = "Q", 2114 .cmd_startswith = 1, 2115 .schema = "s0" 2116 }; 2117 cmd_parser = &gen_set_cmd_desc; 2118 } 2119 break; 2120 default: 2121 /* put empty packet */ 2122 gdb_put_packet(""); 2123 break; 2124 } 2125 2126 if (cmd_parser) { 2127 run_cmd_parser(line_buf, cmd_parser); 2128 } 2129 2130 return RS_IDLE; 2131 } 2132 2133 void gdb_set_stop_cpu(CPUState *cpu) 2134 { 2135 GDBProcess *p = gdb_get_cpu_process(cpu); 2136 2137 if (!p->attached) { 2138 /* 2139 * Having a stop CPU corresponding to a process that is not attached 2140 * confuses GDB. So we ignore the request. 2141 */ 2142 return; 2143 } 2144 2145 gdbserver_state.c_cpu = cpu; 2146 gdbserver_state.g_cpu = cpu; 2147 } 2148 2149 void gdb_read_byte(uint8_t ch) 2150 { 2151 uint8_t reply; 2152 2153 gdbserver_state.allow_stop_reply = false; 2154 #ifndef CONFIG_USER_ONLY 2155 if (gdbserver_state.last_packet->len) { 2156 /* Waiting for a response to the last packet. If we see the start 2157 of a new command then abandon the previous response. */ 2158 if (ch == '-') { 2159 trace_gdbstub_err_got_nack(); 2160 gdb_put_buffer(gdbserver_state.last_packet->data, 2161 gdbserver_state.last_packet->len); 2162 } else if (ch == '+') { 2163 trace_gdbstub_io_got_ack(); 2164 } else { 2165 trace_gdbstub_io_got_unexpected(ch); 2166 } 2167 2168 if (ch == '+' || ch == '$') { 2169 g_byte_array_set_size(gdbserver_state.last_packet, 0); 2170 } 2171 if (ch != '$') 2172 return; 2173 } 2174 if (runstate_is_running()) { 2175 /* 2176 * When the CPU is running, we cannot do anything except stop 2177 * it when receiving a char. This is expected on a Ctrl-C in the 2178 * gdb client. Because we are in all-stop mode, gdb sends a 2179 * 0x03 byte which is not a usual packet, so we handle it specially 2180 * here, but it does expect a stop reply. 2181 */ 2182 if (ch != 0x03) { 2183 trace_gdbstub_err_unexpected_runpkt(ch); 2184 } else { 2185 gdbserver_state.allow_stop_reply = true; 2186 } 2187 vm_stop(RUN_STATE_PAUSED); 2188 } else 2189 #endif 2190 { 2191 switch(gdbserver_state.state) { 2192 case RS_IDLE: 2193 if (ch == '$') { 2194 /* start of command packet */ 2195 gdbserver_state.line_buf_index = 0; 2196 gdbserver_state.line_sum = 0; 2197 gdbserver_state.state = RS_GETLINE; 2198 } else if (ch == '+') { 2199 /* 2200 * do nothing, gdb may preemptively send out ACKs on 2201 * initial connection 2202 */ 2203 } else { 2204 trace_gdbstub_err_garbage(ch); 2205 } 2206 break; 2207 case RS_GETLINE: 2208 if (ch == '}') { 2209 /* start escape sequence */ 2210 gdbserver_state.state = RS_GETLINE_ESC; 2211 gdbserver_state.line_sum += ch; 2212 } else if (ch == '*') { 2213 /* start run length encoding sequence */ 2214 gdbserver_state.state = RS_GETLINE_RLE; 2215 gdbserver_state.line_sum += ch; 2216 } else if (ch == '#') { 2217 /* end of command, start of checksum*/ 2218 gdbserver_state.state = RS_CHKSUM1; 2219 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { 2220 trace_gdbstub_err_overrun(); 2221 gdbserver_state.state = RS_IDLE; 2222 } else { 2223 /* unescaped command character */ 2224 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch; 2225 gdbserver_state.line_sum += ch; 2226 } 2227 break; 2228 case RS_GETLINE_ESC: 2229 if (ch == '#') { 2230 /* unexpected end of command in escape sequence */ 2231 gdbserver_state.state = RS_CHKSUM1; 2232 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { 2233 /* command buffer overrun */ 2234 trace_gdbstub_err_overrun(); 2235 gdbserver_state.state = RS_IDLE; 2236 } else { 2237 /* parse escaped character and leave escape state */ 2238 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20; 2239 gdbserver_state.line_sum += ch; 2240 gdbserver_state.state = RS_GETLINE; 2241 } 2242 break; 2243 case RS_GETLINE_RLE: 2244 /* 2245 * Run-length encoding is explained in "Debugging with GDB / 2246 * Appendix E GDB Remote Serial Protocol / Overview". 2247 */ 2248 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { 2249 /* invalid RLE count encoding */ 2250 trace_gdbstub_err_invalid_repeat(ch); 2251 gdbserver_state.state = RS_GETLINE; 2252 } else { 2253 /* decode repeat length */ 2254 int repeat = ch - ' ' + 3; 2255 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) { 2256 /* that many repeats would overrun the command buffer */ 2257 trace_gdbstub_err_overrun(); 2258 gdbserver_state.state = RS_IDLE; 2259 } else if (gdbserver_state.line_buf_index < 1) { 2260 /* got a repeat but we have nothing to repeat */ 2261 trace_gdbstub_err_invalid_rle(); 2262 gdbserver_state.state = RS_GETLINE; 2263 } else { 2264 /* repeat the last character */ 2265 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index, 2266 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat); 2267 gdbserver_state.line_buf_index += repeat; 2268 gdbserver_state.line_sum += ch; 2269 gdbserver_state.state = RS_GETLINE; 2270 } 2271 } 2272 break; 2273 case RS_CHKSUM1: 2274 /* get high hex digit of checksum */ 2275 if (!isxdigit(ch)) { 2276 trace_gdbstub_err_checksum_invalid(ch); 2277 gdbserver_state.state = RS_GETLINE; 2278 break; 2279 } 2280 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0'; 2281 gdbserver_state.line_csum = fromhex(ch) << 4; 2282 gdbserver_state.state = RS_CHKSUM2; 2283 break; 2284 case RS_CHKSUM2: 2285 /* get low hex digit of checksum */ 2286 if (!isxdigit(ch)) { 2287 trace_gdbstub_err_checksum_invalid(ch); 2288 gdbserver_state.state = RS_GETLINE; 2289 break; 2290 } 2291 gdbserver_state.line_csum |= fromhex(ch); 2292 2293 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) { 2294 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum); 2295 /* send NAK reply */ 2296 reply = '-'; 2297 gdb_put_buffer(&reply, 1); 2298 gdbserver_state.state = RS_IDLE; 2299 } else { 2300 /* send ACK reply */ 2301 reply = '+'; 2302 gdb_put_buffer(&reply, 1); 2303 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf); 2304 } 2305 break; 2306 default: 2307 abort(); 2308 } 2309 } 2310 } 2311 2312 /* 2313 * Create the process that will contain all the "orphan" CPUs (that are not 2314 * part of a CPU cluster). Note that if this process contains no CPUs, it won't 2315 * be attachable and thus will be invisible to the user. 2316 */ 2317 void gdb_create_default_process(GDBState *s) 2318 { 2319 GDBProcess *process; 2320 int pid; 2321 2322 #ifdef CONFIG_USER_ONLY 2323 assert(gdbserver_state.process_num == 0); 2324 pid = getpid(); 2325 #else 2326 if (gdbserver_state.process_num) { 2327 pid = s->processes[s->process_num - 1].pid; 2328 } else { 2329 pid = 0; 2330 } 2331 /* We need an available PID slot for this process */ 2332 assert(pid < UINT32_MAX); 2333 pid++; 2334 #endif 2335 2336 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); 2337 process = &s->processes[s->process_num - 1]; 2338 process->pid = pid; 2339 process->attached = false; 2340 process->target_xml = NULL; 2341 } 2342 2343