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