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