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