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