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