Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1 | /* |
Philippe Mathieu-Daudé | 25f34eb | 2024-03-13 21:09:38 +0100 | [diff] [blame] | 2 | * gdb server stub - system specific bits |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 3 | * |
| 4 | * Debug integration depends on support from the individual |
| 5 | * accelerators so most of this involves calling the ops helpers. |
| 6 | * |
Alex Bennée | 9455762 | 2023-03-02 18:57:38 -0800 | [diff] [blame] | 7 | * Copyright (c) 2003-2005 Fabrice Bellard |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 8 | * Copyright (c) 2022 Linaro Ltd |
| 9 | * |
Philippe Mathieu-Daudé | b14d064 | 2024-09-11 17:12:04 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: LGPL-2.0-or-later |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 14 | #include "qapi/error.h" |
| 15 | #include "qemu/error-report.h" |
| 16 | #include "qemu/cutils.h" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 17 | #include "exec/gdbstub.h" |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 18 | #include "gdbstub/syscalls.h" |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 19 | #include "gdbstub/commands.h" |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 20 | #include "exec/hwaddr.h" |
| 21 | #include "exec/tb-flush.h" |
Philippe Mathieu-Daudé | 0f66536 | 2025-01-23 13:39:05 +0100 | [diff] [blame] | 22 | #include "system/accel-ops.h" |
Philippe Mathieu-Daudé | 32cad1f | 2024-12-03 15:20:13 +0100 | [diff] [blame] | 23 | #include "system/cpus.h" |
| 24 | #include "system/runstate.h" |
| 25 | #include "system/replay.h" |
Philippe Mathieu-Daudé | 270dbee | 2025-01-23 14:09:19 +0100 | [diff] [blame] | 26 | #include "system/tcg.h" |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 34 | #include "internals.h" |
| 35 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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; |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 49 | gdbserver_state.allow_stop_reply = false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | /* |
Alex Bennée | a7e0f9b | 2023-03-02 18:57:49 -0800 | [diff] [blame] | 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 | /* |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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(); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 103 | break; |
| 104 | default: |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | |
Alex Bennée | 131f387 | 2023-03-02 18:58:01 -0800 | [diff] [blame] | 109 | /* |
Philippe Mathieu-Daudé | 3f7d1bd | 2023-10-04 11:06:22 +0200 | [diff] [blame] | 110 | * In system-mode we stop the VM and wait to send the syscall packet |
Alex Bennée | 131f387 | 2023-03-02 18:58:01 -0800 | [diff] [blame] | 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 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | } |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 134 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 135 | /* Is there a GDB syscall waiting to be sent? */ |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 136 | if (gdb_handled_syscall()) { |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (cpu == NULL) { |
| 141 | /* No process attached */ |
| 142 | return; |
| 143 | } |
| 144 | |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 145 | if (!gdbserver_state.allow_stop_reply) { |
| 146 | return; |
| 147 | } |
| 148 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | } |
Philippe Mathieu-Daudé | 270dbee | 2025-01-23 14:09:19 +0100 | [diff] [blame] | 176 | if (tcg_enabled()) { |
| 177 | tb_flush(cpu); |
| 178 | } |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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(); |
Alex Bennée | 5dcf633 | 2023-12-01 09:36:27 +0000 | [diff] [blame] | 191 | ret = GDB_SIGNAL_STOP; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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); |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 217 | gdbserver_state.allow_stop_reply = false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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; |
Akihiko Odaki | af2e06c | 2023-10-09 17:40:47 +0100 | [diff] [blame] | 300 | process->target_xml = NULL; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 337 | bool gdbserver_start(const char *device, Error **errp) |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 338 | { |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 339 | Chardev *chr = NULL; |
| 340 | Chardev *mon_chr; |
Alex Bennée | d2fe226 | 2023-06-30 19:04:13 +0100 | [diff] [blame] | 341 | g_autoptr(GString) cs = g_string_new(device); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 342 | |
| 343 | if (!first_cpu) { |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 344 | error_setg(errp, "gdbstub: meaningless to attach gdb to a " |
| 345 | "machine without any CPU."); |
| 346 | return false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (!gdb_supports_guest_debug()) { |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 350 | error_setg(errp, "gdbstub: current accelerator doesn't " |
| 351 | "support guest debugging"); |
| 352 | return false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Alex Bennée | d2fe226 | 2023-06-30 19:04:13 +0100 | [diff] [blame] | 355 | if (cs->len == 0) { |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 356 | error_setg(errp, "gdbstub: missing connection string"); |
| 357 | return false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 358 | } |
Alex Bennée | d2fe226 | 2023-06-30 19:04:13 +0100 | [diff] [blame] | 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:")) { |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 364 | /* enforce required TCP attributes */ |
Alex Bennée | d2fe226 | 2023-06-30 19:04:13 +0100 | [diff] [blame] | 365 | g_string_append_printf(cs, ",wait=off,nodelay=on,server=on"); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | */ |
Alex Bennée | d2fe226 | 2023-06-30 19:04:13 +0100 | [diff] [blame] | 380 | chr = qemu_chr_new_noreplay("gdb", cs->str, true, NULL); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 381 | if (!chr) { |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 382 | error_setg(errp, "gdbstub: couldn't create chardev"); |
| 383 | return false; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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; |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 413 | gdb_syscall_reset(); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 414 | |
Alex Bennée | c0e6b8b | 2025-01-16 16:02:39 +0000 | [diff] [blame] | 415 | return true; |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 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 | |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 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 | } |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 441 | |
| 442 | qemu_chr_fe_deinit(&gdbserver_system_state.chr, true); |
| 443 | } |
| 444 | |
Clément Chigot | e216256 | 2023-10-03 09:14:27 +0200 | [diff] [blame] | 445 | void gdb_qemu_exit(int code) |
| 446 | { |
| 447 | qemu_system_shutdown_request_with_code(SHUTDOWN_CAUSE_GUEST_SHUTDOWN, |
| 448 | code); |
| 449 | } |
| 450 | |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 451 | /* |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 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 | { |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 459 | if (phy_memory_mode) { |
| 460 | if (is_write) { |
| 461 | cpu_physical_memory_write(addr, buf, len); |
| 462 | } else { |
| 463 | cpu_physical_memory_read(addr, buf, len); |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
Philippe Mathieu-Daudé | 0368d8d | 2025-01-21 12:11:35 +0100 | [diff] [blame] | 468 | if (cpu->cc->memory_rw_debug) { |
| 469 | return cpu->cc->memory_rw_debug(cpu, addr, buf, len, is_write); |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); |
| 473 | } |
| 474 | |
Alex Bennée | 7ea0c33 | 2023-03-02 18:57:52 -0800 | [diff] [blame] | 475 | /* |
| 476 | * cpu helpers |
| 477 | */ |
| 478 | |
| 479 | unsigned int gdb_get_max_cpus(void) |
| 480 | { |
| 481 | MachineState *ms = MACHINE(qdev_get_machine()); |
| 482 | return ms->smp.max_cpus; |
| 483 | } |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 484 | |
Alex Bennée | 505601d | 2023-03-02 18:57:53 -0800 | [diff] [blame] | 485 | bool gdb_can_reverse(void) |
| 486 | { |
| 487 | return replay_mode == REPLAY_MODE_PLAY; |
| 488 | } |
| 489 | |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 490 | /* |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 491 | * Softmmu specific command helpers |
| 492 | */ |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 493 | |
| 494 | void gdb_handle_query_qemu_phy_mem_mode(GArray *params, |
Philippe Mathieu-Daudé | 0eaf7fb | 2024-03-13 21:12:36 +0100 | [diff] [blame] | 495 | void *ctx) |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 496 | { |
| 497 | g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode); |
| 498 | gdb_put_strbuf(); |
| 499 | } |
| 500 | |
Philippe Mathieu-Daudé | 0eaf7fb | 2024-03-13 21:12:36 +0100 | [diff] [blame] | 501 | void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *ctx) |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 502 | { |
| 503 | if (!params->len) { |
| 504 | gdb_put_packet("E22"); |
| 505 | return; |
| 506 | } |
| 507 | |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 508 | if (!gdb_get_cmd_param(params, 0)->val_ul) { |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 509 | phy_memory_mode = 0; |
| 510 | } else { |
| 511 | phy_memory_mode = 1; |
| 512 | } |
| 513 | gdb_put_packet("OK"); |
| 514 | } |
| 515 | |
Philippe Mathieu-Daudé | 0eaf7fb | 2024-03-13 21:12:36 +0100 | [diff] [blame] | 516 | void gdb_handle_query_rcmd(GArray *params, void *ctx) |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 517 | { |
| 518 | const guint8 zero = 0; |
| 519 | int len; |
| 520 | |
| 521 | if (!params->len) { |
| 522 | gdb_put_packet("E22"); |
| 523 | return; |
| 524 | } |
| 525 | |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 526 | len = strlen(gdb_get_cmd_param(params, 0)->data); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 527 | if (len % 2) { |
| 528 | gdb_put_packet("E01"); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | g_assert(gdbserver_state.mem_buf->len == 0); |
| 533 | len = len / 2; |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 534 | gdb_hextomem(gdbserver_state.mem_buf, gdb_get_cmd_param(params, 0)->data, len); |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 535 | g_byte_array_append(gdbserver_state.mem_buf, &zero, 1); |
| 536 | qemu_chr_be_write(gdbserver_system_state.mon_chr, |
| 537 | gdbserver_state.mem_buf->data, |
| 538 | gdbserver_state.mem_buf->len); |
| 539 | gdb_put_packet("OK"); |
| 540 | } |
| 541 | |
| 542 | /* |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 543 | * Execution state helpers |
| 544 | */ |
| 545 | |
Philippe Mathieu-Daudé | 0eaf7fb | 2024-03-13 21:12:36 +0100 | [diff] [blame] | 546 | void gdb_handle_query_attached(GArray *params, void *ctx) |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 547 | { |
| 548 | gdb_put_packet("1"); |
| 549 | } |
| 550 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 551 | void gdb_continue(void) |
| 552 | { |
| 553 | if (!runstate_needs_reset()) { |
| 554 | trace_gdbstub_op_continue(); |
| 555 | vm_start(); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Resume execution, per CPU actions. |
| 561 | */ |
| 562 | int gdb_continue_partial(char *newstates) |
| 563 | { |
| 564 | CPUState *cpu; |
| 565 | int res = 0; |
| 566 | int flag = 0; |
| 567 | |
| 568 | if (!runstate_needs_reset()) { |
| 569 | bool step_requested = false; |
| 570 | CPU_FOREACH(cpu) { |
| 571 | if (newstates[cpu->cpu_index] == 's') { |
| 572 | step_requested = true; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | if (vm_prepare_start(step_requested)) { |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | CPU_FOREACH(cpu) { |
| 582 | switch (newstates[cpu->cpu_index]) { |
| 583 | case 0: |
| 584 | case 1: |
| 585 | break; /* nothing to do here */ |
| 586 | case 's': |
| 587 | trace_gdbstub_op_stepping(cpu->cpu_index); |
| 588 | cpu_single_step(cpu, gdbserver_state.sstep_flags); |
| 589 | cpu_resume(cpu); |
| 590 | flag = 1; |
| 591 | break; |
| 592 | case 'c': |
| 593 | trace_gdbstub_op_continue_cpu(cpu->cpu_index); |
| 594 | cpu_resume(cpu); |
| 595 | flag = 1; |
| 596 | break; |
| 597 | default: |
| 598 | res = -1; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | if (flag) { |
| 604 | qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true); |
| 605 | } |
| 606 | return res; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Signal Handling - in system mode we only need SIGINT and SIGTRAP; other |
| 611 | * signals are not yet supported. |
| 612 | */ |
| 613 | |
| 614 | enum { |
| 615 | TARGET_SIGINT = 2, |
| 616 | TARGET_SIGTRAP = 5 |
| 617 | }; |
| 618 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 619 | int gdb_signal_to_target(int sig) |
| 620 | { |
Alex Bennée | ccd4c7c | 2023-03-02 18:57:48 -0800 | [diff] [blame] | 621 | switch (sig) { |
| 622 | case 2: |
| 623 | return TARGET_SIGINT; |
| 624 | case 5: |
| 625 | return TARGET_SIGTRAP; |
| 626 | default: |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 627 | return -1; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* |
Alex Bennée | b6fa2ec | 2023-03-02 18:57:46 -0800 | [diff] [blame] | 632 | * Break/Watch point helpers |
| 633 | */ |
| 634 | |
Alex Bennée | a48e7d9 | 2022-09-29 12:42:25 +0100 | [diff] [blame] | 635 | bool gdb_supports_guest_debug(void) |
| 636 | { |
| 637 | const AccelOpsClass *ops = cpus_get_accel(); |
| 638 | if (ops->supports_guest_debug) { |
| 639 | return ops->supports_guest_debug(); |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 644 | int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len) |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 645 | { |
| 646 | const AccelOpsClass *ops = cpus_get_accel(); |
| 647 | if (ops->insert_breakpoint) { |
| 648 | return ops->insert_breakpoint(cs, type, addr, len); |
| 649 | } |
| 650 | return -ENOSYS; |
| 651 | } |
| 652 | |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 653 | int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len) |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 654 | { |
| 655 | const AccelOpsClass *ops = cpus_get_accel(); |
| 656 | if (ops->remove_breakpoint) { |
| 657 | return ops->remove_breakpoint(cs, type, addr, len); |
| 658 | } |
| 659 | return -ENOSYS; |
| 660 | } |
| 661 | |
| 662 | void gdb_breakpoint_remove_all(CPUState *cs) |
| 663 | { |
| 664 | const AccelOpsClass *ops = cpus_get_accel(); |
| 665 | if (ops->remove_all_breakpoints) { |
| 666 | ops->remove_all_breakpoints(cs); |
| 667 | } |
| 668 | } |