1 /* 2 * gdb server stub - system specific bits 3 * 4 * Debug integration depends on support from the individual 5 * accelerators so most of this involves calling the ops helpers. 6 * 7 * Copyright (c) 2003-2005 Fabrice Bellard 8 * Copyright (c) 2022 Linaro Ltd 9 * 10 * SPDX-License-Identifier: LGPL-2.0-or-later 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qapi/error.h" 15 #include "qemu/error-report.h" 16 #include "qemu/cutils.h" 17 #include "exec/gdbstub.h" 18 #include "gdbstub/syscalls.h" 19 #include "gdbstub/commands.h" 20 #include "exec/hwaddr.h" 21 #include "exec/tb-flush.h" 22 #include "system/accel-ops.h" 23 #include "system/cpus.h" 24 #include "system/runstate.h" 25 #include "system/replay.h" 26 #include "system/tcg.h" 27 #include "hw/core/cpu.h" 28 #include "hw/cpu/cluster.h" 29 #include "hw/boards.h" 30 #include "chardev/char.h" 31 #include "chardev/char-fe.h" 32 #include "monitor/monitor.h" 33 #include "trace.h" 34 #include "internals.h" 35 36 /* System emulation specific state */ 37 typedef struct { 38 CharBackend chr; 39 Chardev *mon_chr; 40 } GDBSystemState; 41 42 GDBSystemState gdbserver_system_state; 43 44 static void reset_gdbserver_state(void) 45 { 46 g_free(gdbserver_state.processes); 47 gdbserver_state.processes = NULL; 48 gdbserver_state.process_num = 0; 49 gdbserver_state.allow_stop_reply = false; 50 } 51 52 /* 53 * Return the GDB index for a given vCPU state. 54 * 55 * In system mode GDB numbers CPUs from 1 as 0 is reserved as an "any 56 * cpu" index. 57 */ 58 int gdb_get_cpu_index(CPUState *cpu) 59 { 60 return cpu->cpu_index + 1; 61 } 62 63 /* 64 * We check the status of the last message in the chardev receive code 65 */ 66 bool gdb_got_immediate_ack(void) 67 { 68 return true; 69 } 70 71 /* 72 * GDB Connection management. For system emulation we do all of this 73 * via our existing Chardev infrastructure which allows us to support 74 * network and unix sockets. 75 */ 76 77 void gdb_put_buffer(const uint8_t *buf, int len) 78 { 79 /* 80 * XXX this blocks entire thread. Rewrite to use 81 * qemu_chr_fe_write and background I/O callbacks 82 */ 83 qemu_chr_fe_write_all(&gdbserver_system_state.chr, buf, len); 84 } 85 86 static void gdb_chr_event(void *opaque, QEMUChrEvent event) 87 { 88 int i; 89 GDBState *s = (GDBState *) opaque; 90 91 switch (event) { 92 case CHR_EVENT_OPENED: 93 /* Start with first process attached, others detached */ 94 for (i = 0; i < s->process_num; i++) { 95 s->processes[i].attached = !i; 96 } 97 98 s->c_cpu = gdb_first_attached_cpu(); 99 s->g_cpu = s->c_cpu; 100 101 vm_stop(RUN_STATE_PAUSED); 102 replay_gdb_attached(); 103 break; 104 default: 105 break; 106 } 107 } 108 109 /* 110 * In system-mode we stop the VM and wait to send the syscall packet 111 * until notification that the CPU has stopped. This must be done 112 * because if the packet is sent now the reply from the syscall 113 * request could be received while the CPU is still in the running 114 * state, which can cause packets to be dropped and state transition 115 * 'T' packets to be sent while the syscall is still being processed. 116 */ 117 void gdb_syscall_handling(const char *syscall_packet) 118 { 119 vm_stop(RUN_STATE_DEBUG); 120 qemu_cpu_kick(gdbserver_state.c_cpu); 121 } 122 123 static void gdb_vm_state_change(void *opaque, bool running, RunState state) 124 { 125 CPUState *cpu = gdbserver_state.c_cpu; 126 g_autoptr(GString) buf = g_string_new(NULL); 127 g_autoptr(GString) tid = g_string_new(NULL); 128 const char *type; 129 int ret; 130 131 if (running || gdbserver_state.state == RS_INACTIVE) { 132 return; 133 } 134 135 /* Is there a GDB syscall waiting to be sent? */ 136 if (gdb_handled_syscall()) { 137 return; 138 } 139 140 if (cpu == NULL) { 141 /* No process attached */ 142 return; 143 } 144 145 if (!gdbserver_state.allow_stop_reply) { 146 return; 147 } 148 149 gdb_append_thread_id(cpu, tid); 150 151 switch (state) { 152 case RUN_STATE_DEBUG: 153 if (cpu->watchpoint_hit) { 154 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) { 155 case BP_MEM_READ: 156 type = "r"; 157 break; 158 case BP_MEM_ACCESS: 159 type = "a"; 160 break; 161 default: 162 type = ""; 163 break; 164 } 165 trace_gdbstub_hit_watchpoint(type, 166 gdb_get_cpu_index(cpu), 167 cpu->watchpoint_hit->vaddr); 168 g_string_printf(buf, "T%02xthread:%s;%swatch:%" VADDR_PRIx ";", 169 GDB_SIGNAL_TRAP, tid->str, type, 170 cpu->watchpoint_hit->vaddr); 171 cpu->watchpoint_hit = NULL; 172 goto send_packet; 173 } else { 174 trace_gdbstub_hit_break(); 175 } 176 if (tcg_enabled()) { 177 tb_flush(cpu); 178 } 179 ret = GDB_SIGNAL_TRAP; 180 break; 181 case RUN_STATE_PAUSED: 182 trace_gdbstub_hit_paused(); 183 ret = GDB_SIGNAL_INT; 184 break; 185 case RUN_STATE_SHUTDOWN: 186 trace_gdbstub_hit_shutdown(); 187 ret = GDB_SIGNAL_QUIT; 188 break; 189 case RUN_STATE_IO_ERROR: 190 trace_gdbstub_hit_io_error(); 191 ret = GDB_SIGNAL_STOP; 192 break; 193 case RUN_STATE_WATCHDOG: 194 trace_gdbstub_hit_watchdog(); 195 ret = GDB_SIGNAL_ALRM; 196 break; 197 case RUN_STATE_INTERNAL_ERROR: 198 trace_gdbstub_hit_internal_error(); 199 ret = GDB_SIGNAL_ABRT; 200 break; 201 case RUN_STATE_SAVE_VM: 202 case RUN_STATE_RESTORE_VM: 203 return; 204 case RUN_STATE_FINISH_MIGRATE: 205 ret = GDB_SIGNAL_XCPU; 206 break; 207 default: 208 trace_gdbstub_hit_unknown(state); 209 ret = GDB_SIGNAL_UNKNOWN; 210 break; 211 } 212 gdb_set_stop_cpu(cpu); 213 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str); 214 215 send_packet: 216 gdb_put_packet(buf->str); 217 gdbserver_state.allow_stop_reply = false; 218 219 /* disable single step if it was enabled */ 220 cpu_single_step(cpu, 0); 221 } 222 223 #ifndef _WIN32 224 static void gdb_sigterm_handler(int signal) 225 { 226 if (runstate_is_running()) { 227 vm_stop(RUN_STATE_PAUSED); 228 } 229 } 230 #endif 231 232 static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len) 233 { 234 g_autoptr(GString) hex_buf = g_string_new("O"); 235 gdb_memtohex(hex_buf, buf, len); 236 gdb_put_packet(hex_buf->str); 237 return len; 238 } 239 240 static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend, 241 bool *be_opened, Error **errp) 242 { 243 *be_opened = false; 244 } 245 246 static void char_gdb_class_init(ObjectClass *oc, void *data) 247 { 248 ChardevClass *cc = CHARDEV_CLASS(oc); 249 250 cc->internal = true; 251 cc->open = gdb_monitor_open; 252 cc->chr_write = gdb_monitor_write; 253 } 254 255 #define TYPE_CHARDEV_GDB "chardev-gdb" 256 257 static const TypeInfo char_gdb_type_info = { 258 .name = TYPE_CHARDEV_GDB, 259 .parent = TYPE_CHARDEV, 260 .class_init = char_gdb_class_init, 261 }; 262 263 static int gdb_chr_can_receive(void *opaque) 264 { 265 /* 266 * We can handle an arbitrarily large amount of data. 267 * Pick the maximum packet size, which is as good as anything. 268 */ 269 return MAX_PACKET_LENGTH; 270 } 271 272 static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size) 273 { 274 int i; 275 276 for (i = 0; i < size; i++) { 277 gdb_read_byte(buf[i]); 278 } 279 } 280 281 static int find_cpu_clusters(Object *child, void *opaque) 282 { 283 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) { 284 GDBState *s = (GDBState *) opaque; 285 CPUClusterState *cluster = CPU_CLUSTER(child); 286 GDBProcess *process; 287 288 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); 289 290 process = &s->processes[s->process_num - 1]; 291 292 /* 293 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at 294 * runtime, we enforce here that the machine does not use a cluster ID 295 * that would lead to PID 0. 296 */ 297 assert(cluster->cluster_id != UINT32_MAX); 298 process->pid = cluster->cluster_id + 1; 299 process->attached = false; 300 process->target_xml = NULL; 301 302 return 0; 303 } 304 305 return object_child_foreach(child, find_cpu_clusters, opaque); 306 } 307 308 static int pid_order(const void *a, const void *b) 309 { 310 GDBProcess *pa = (GDBProcess *) a; 311 GDBProcess *pb = (GDBProcess *) b; 312 313 if (pa->pid < pb->pid) { 314 return -1; 315 } else if (pa->pid > pb->pid) { 316 return 1; 317 } else { 318 return 0; 319 } 320 } 321 322 static void create_processes(GDBState *s) 323 { 324 object_child_foreach(object_get_root(), find_cpu_clusters, s); 325 326 if (gdbserver_state.processes) { 327 /* Sort by PID */ 328 qsort(gdbserver_state.processes, 329 gdbserver_state.process_num, 330 sizeof(gdbserver_state.processes[0]), 331 pid_order); 332 } 333 334 gdb_create_default_process(s); 335 } 336 337 bool gdbserver_start(const char *device, Error **errp) 338 { 339 Chardev *chr = NULL; 340 Chardev *mon_chr; 341 g_autoptr(GString) cs = g_string_new(device); 342 343 if (!first_cpu) { 344 error_setg(errp, "gdbstub: meaningless to attach gdb to a " 345 "machine without any CPU."); 346 return false; 347 } 348 349 if (!gdb_supports_guest_debug()) { 350 error_setg(errp, "gdbstub: current accelerator doesn't " 351 "support guest debugging"); 352 return false; 353 } 354 355 if (cs->len == 0) { 356 error_setg(errp, "gdbstub: missing connection string"); 357 return false; 358 } 359 360 trace_gdbstub_op_start(cs->str); 361 362 if (g_strcmp0(cs->str, "none") != 0) { 363 if (g_str_has_prefix(cs->str, "tcp:")) { 364 /* enforce required TCP attributes */ 365 g_string_append_printf(cs, ",wait=off,nodelay=on,server=on"); 366 } 367 #ifndef _WIN32 368 else if (strcmp(device, "stdio") == 0) { 369 struct sigaction act; 370 371 memset(&act, 0, sizeof(act)); 372 act.sa_handler = gdb_sigterm_handler; 373 sigaction(SIGINT, &act, NULL); 374 } 375 #endif 376 /* 377 * FIXME: it's a bit weird to allow using a mux chardev here 378 * and implicitly setup a monitor. We may want to break this. 379 */ 380 chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL); 381 if (!chr) { 382 error_setg(errp, "gdbstub: couldn't create chardev"); 383 return false; 384 } 385 } 386 387 if (!gdbserver_state.init) { 388 gdb_init_gdbserver_state(); 389 390 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL); 391 392 /* Initialize a monitor terminal for gdb */ 393 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB, 394 NULL, NULL, &error_abort); 395 monitor_init_hmp(mon_chr, false, &error_abort); 396 } else { 397 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true); 398 mon_chr = gdbserver_system_state.mon_chr; 399 reset_gdbserver_state(); 400 } 401 402 create_processes(&gdbserver_state); 403 404 if (chr) { 405 qemu_chr_fe_init(&gdbserver_system_state.chr, chr, &error_abort); 406 qemu_chr_fe_set_handlers(&gdbserver_system_state.chr, 407 gdb_chr_can_receive, 408 gdb_chr_receive, gdb_chr_event, 409 NULL, &gdbserver_state, NULL, true); 410 } 411 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE; 412 gdbserver_system_state.mon_chr = mon_chr; 413 gdb_syscall_reset(); 414 415 return true; 416 } 417 418 static void register_types(void) 419 { 420 type_register_static(&char_gdb_type_info); 421 } 422 423 type_init(register_types); 424 425 /* Tell the remote gdb that the process has exited. */ 426 void gdb_exit(int code) 427 { 428 char buf[4]; 429 430 if (!gdbserver_state.init) { 431 return; 432 } 433 434 trace_gdbstub_op_exiting((uint8_t)code); 435 436 if (gdbserver_state.allow_stop_reply) { 437 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); 438 gdb_put_packet(buf); 439 gdbserver_state.allow_stop_reply = false; 440 } 441 442 qemu_chr_fe_deinit(&gdbserver_system_state.chr, true); 443 } 444 445 void gdb_qemu_exit(int code) 446 { 447 qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN, 448 code); 449 } 450 451 /* 452 * Memory access 453 */ 454 static int phy_memory_mode; 455 456 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, 457 uint8_t *buf, int len, bool is_write) 458 { 459 CPUClass *cc; 460 461 if (phy_memory_mode) { 462 if (is_write) { 463 cpu_physical_memory_write(addr, buf, len); 464 } else { 465 cpu_physical_memory_read(addr, buf, len); 466 } 467 return 0; 468 } 469 470 cc = CPU_GET_CLASS(cpu); 471 if (cc->memory_rw_debug) { 472 return cc->memory_rw_debug(cpu, addr, buf, len, is_write); 473 } 474 475 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); 476 } 477 478 /* 479 * cpu helpers 480 */ 481 482 unsigned int gdb_get_max_cpus(void) 483 { 484 MachineState *ms = MACHINE(qdev_get_machine()); 485 return ms->smp.max_cpus; 486 } 487 488 bool gdb_can_reverse(void) 489 { 490 return replay_mode == REPLAY_MODE_PLAY; 491 } 492 493 /* 494 * Softmmu specific command helpers 495 */ 496 497 void gdb_handle_query_qemu_phy_mem_mode(GArray *params, 498 void *ctx) 499 { 500 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode); 501 gdb_put_strbuf(); 502 } 503 504 void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx) 505 { 506 if (!params->len) { 507 gdb_put_packet("E22"); 508 return; 509 } 510 511 if (!gdb_get_cmd_param(params, 0)->val_ul) { 512 phy_memory_mode = 0; 513 } else { 514 phy_memory_mode = 1; 515 } 516 gdb_put_packet("OK"); 517 } 518 519 void gdb_handle_query_rcmd(GArray *params, void *ctx) 520 { 521 const guint8 zero = 0; 522 int len; 523 524 if (!params->len) { 525 gdb_put_packet("E22"); 526 return; 527 } 528 529 len = strlen(gdb_get_cmd_param(params, 0)->data); 530 if (len % 2) { 531 gdb_put_packet("E01"); 532 return; 533 } 534 535 g_assert(gdbserver_state.mem_buf->len == 0); 536 len = len / 2; 537 gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len); 538 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1); 539 qemu_chr_be_write(gdbserver_system_state.mon_chr, 540 gdbserver_state.mem_buf->data, 541 gdbserver_state.mem_buf->len); 542 gdb_put_packet("OK"); 543 } 544 545 /* 546 * Execution state helpers 547 */ 548 549 void gdb_handle_query_attached(GArray *params, void *ctx) 550 { 551 gdb_put_packet("1"); 552 } 553 554 void gdb_continue(void) 555 { 556 if (!runstate_needs_reset()) { 557 trace_gdbstub_op_continue(); 558 vm_start(); 559 } 560 } 561 562 /* 563 * Resume execution, per CPU actions. 564 */ 565 int gdb_continue_partial(char *newstates) 566 { 567 CPUState *cpu; 568 int res = 0; 569 int flag = 0; 570 571 if (!runstate_needs_reset()) { 572 bool step_requested = false; 573 CPU_FOREACH(cpu) { 574 if (newstates[cpu->cpu_index] == 's') { 575 step_requested = true; 576 break; 577 } 578 } 579 580 if (vm_prepare_start(step_requested)) { 581 return 0; 582 } 583 584 CPU_FOREACH(cpu) { 585 switch (newstates[cpu->cpu_index]) { 586 case 0: 587 case 1: 588 break; /* nothing to do here */ 589 case 's': 590 trace_gdbstub_op_stepping(cpu->cpu_index); 591 cpu_single_step(cpu, gdbserver_state.sstep_flags); 592 cpu_resume(cpu); 593 flag = 1; 594 break; 595 case 'c': 596 trace_gdbstub_op_continue_cpu(cpu->cpu_index); 597 cpu_resume(cpu); 598 flag = 1; 599 break; 600 default: 601 res = -1; 602 break; 603 } 604 } 605 } 606 if (flag) { 607 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); 608 } 609 return res; 610 } 611 612 /* 613 * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other 614 * signals are not yet supported. 615 */ 616 617 enum { 618 TARGET_SIGINT = 2, 619 TARGET_SIGTRAP = 5 620 }; 621 622 int gdb_signal_to_target(int sig) 623 { 624 switch (sig) { 625 case 2: 626 return TARGET_SIGINT; 627 case 5: 628 return TARGET_SIGTRAP; 629 default: 630 return -1; 631 } 632 } 633 634 /* 635 * Break/Watch point helpers 636 */ 637 638 bool gdb_supports_guest_debug(void) 639 { 640 const AccelOpsClass *ops = cpus_get_accel(); 641 if (ops->supports_guest_debug) { 642 return ops->supports_guest_debug(); 643 } 644 return false; 645 } 646 647 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len) 648 { 649 const AccelOpsClass *ops = cpus_get_accel(); 650 if (ops->insert_breakpoint) { 651 return ops->insert_breakpoint(cs, type, addr, len); 652 } 653 return -ENOSYS; 654 } 655 656 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len) 657 { 658 const AccelOpsClass *ops = cpus_get_accel(); 659 if (ops->remove_breakpoint) { 660 return ops->remove_breakpoint(cs, type, addr, len); 661 } 662 return -ENOSYS; 663 } 664 665 void gdb_breakpoint_remove_all(CPUState *cs) 666 { 667 const AccelOpsClass *ops = cpus_get_accel(); 668 if (ops->remove_all_breakpoints) { 669 ops->remove_all_breakpoints(cs); 670 } 671 } 672