Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * gdbstub user-mode helper routines. |
| 3 | * |
| 4 | * We know for user-mode we are using TCG so we can call stuff directly. |
| 5 | * |
Alex Bennée | 9455762 | 2023-03-02 18:57:38 -0800 | [diff] [blame] | 6 | * Copyright (c) 2003-2005 Fabrice Bellard |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 7 | * Copyright (c) 2022 Linaro Ltd |
| 8 | * |
Alex Bennée | 9455762 | 2023-03-02 18:57:38 -0800 | [diff] [blame] | 9 | * SPDX-License-Identifier: LGPL-2.0+ |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "qemu/osdep.h" |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 13 | #include "qemu/bitops.h" |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 14 | #include "qemu/cutils.h" |
| 15 | #include "qemu/sockets.h" |
| 16 | #include "exec/hwaddr.h" |
| 17 | #include "exec/tb-flush.h" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 18 | #include "exec/gdbstub.h" |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 19 | #include "gdbstub/commands.h" |
Alex Bennée | c566080 | 2023-03-02 18:57:57 -0800 | [diff] [blame] | 20 | #include "gdbstub/syscalls.h" |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 21 | #include "gdbstub/user.h" |
Alex Bennée | 5b7d54d | 2024-06-20 16:22:10 +0100 | [diff] [blame] | 22 | #include "gdbstub/enums.h" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 23 | #include "hw/core/cpu.h" |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 24 | #include "trace.h" |
Alex Bennée | ae7467b | 2022-09-29 12:42:24 +0100 | [diff] [blame] | 25 | #include "internals.h" |
| 26 | |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 27 | #define GDB_NR_SYSCALLS 1024 |
| 28 | typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)]; |
| 29 | |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 30 | /* |
| 31 | * Forked child talks to its parent in order to let GDB enforce the |
| 32 | * follow-fork-mode. This happens inside a start_exclusive() section, so that |
| 33 | * the other threads, which may be forking too, do not interfere. The |
| 34 | * implementation relies on GDB not sending $vCont until it has detached |
| 35 | * either from the parent (follow-fork-mode child) or from the child |
| 36 | * (follow-fork-mode parent). |
| 37 | * |
| 38 | * The parent and the child share the GDB socket; at any given time only one |
| 39 | * of them is allowed to use it, as is reflected in the respective fork_state. |
| 40 | * This is negotiated via the fork_sockets pair as a reaction to $Hg. |
| 41 | * |
| 42 | * Below is a short summary of the possible state transitions: |
| 43 | * |
| 44 | * ENABLED : Terminal state. |
| 45 | * DISABLED : Terminal state. |
| 46 | * ACTIVE : Parent initial state. |
| 47 | * INACTIVE : Child initial state. |
| 48 | * ACTIVE -> DEACTIVATING: On $Hg. |
| 49 | * ACTIVE -> ENABLING : On $D. |
| 50 | * ACTIVE -> DISABLING : On $D. |
| 51 | * ACTIVE -> DISABLED : On communication error. |
| 52 | * DEACTIVATING -> INACTIVE : On gdb_read_byte() return. |
| 53 | * DEACTIVATING -> DISABLED : On communication error. |
| 54 | * INACTIVE -> ACTIVE : On $Hg in the peer. |
| 55 | * INACTIVE -> ENABLE : On $D in the peer. |
| 56 | * INACTIVE -> DISABLE : On $D in the peer. |
| 57 | * INACTIVE -> DISABLED : On communication error. |
| 58 | * ENABLING -> ENABLED : On gdb_read_byte() return. |
| 59 | * ENABLING -> DISABLED : On communication error. |
| 60 | * DISABLING -> DISABLED : On gdb_read_byte() return. |
| 61 | */ |
| 62 | enum GDBForkState { |
| 63 | /* Fully owning the GDB socket. */ |
| 64 | GDB_FORK_ENABLED, |
| 65 | /* Working with the GDB socket; the peer is inactive. */ |
| 66 | GDB_FORK_ACTIVE, |
| 67 | /* Handing off the GDB socket to the peer. */ |
| 68 | GDB_FORK_DEACTIVATING, |
| 69 | /* The peer is working with the GDB socket. */ |
| 70 | GDB_FORK_INACTIVE, |
| 71 | /* Asking the peer to close its GDB socket fd. */ |
| 72 | GDB_FORK_ENABLING, |
| 73 | /* Asking the peer to take over, closing our GDB socket fd. */ |
| 74 | GDB_FORK_DISABLING, |
| 75 | /* The peer has taken over, our GDB socket fd is closed. */ |
| 76 | GDB_FORK_DISABLED, |
| 77 | }; |
| 78 | |
| 79 | enum GDBForkMessage { |
| 80 | GDB_FORK_ACTIVATE = 'a', |
| 81 | GDB_FORK_ENABLE = 'e', |
| 82 | GDB_FORK_DISABLE = 'd', |
| 83 | }; |
| 84 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 85 | /* User-mode specific state */ |
| 86 | typedef struct { |
| 87 | int fd; |
| 88 | char *socket_path; |
| 89 | int running_state; |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 90 | /* |
| 91 | * Store syscalls mask without memory allocation in order to avoid |
| 92 | * implementing synchronization. |
| 93 | */ |
| 94 | bool catch_all_syscalls; |
| 95 | GDBSyscallsMask catch_syscalls_mask; |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 96 | bool fork_events; |
| 97 | enum GDBForkState fork_state; |
| 98 | int fork_sockets[2]; |
| 99 | pid_t fork_peer_pid, fork_peer_tid; |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 100 | uint8_t siginfo[MAX_SIGINFO_LENGTH]; |
| 101 | unsigned long siginfo_len; |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 102 | } GDBUserState; |
| 103 | |
| 104 | static GDBUserState gdbserver_user_state; |
| 105 | |
| 106 | int gdb_get_char(void) |
| 107 | { |
| 108 | uint8_t ch; |
| 109 | int ret; |
| 110 | |
| 111 | for (;;) { |
| 112 | ret = recv(gdbserver_user_state.fd, &ch, 1, 0); |
| 113 | if (ret < 0) { |
| 114 | if (errno == ECONNRESET) { |
| 115 | gdbserver_user_state.fd = -1; |
| 116 | } |
| 117 | if (errno != EINTR) { |
| 118 | return -1; |
| 119 | } |
| 120 | } else if (ret == 0) { |
| 121 | close(gdbserver_user_state.fd); |
| 122 | gdbserver_user_state.fd = -1; |
| 123 | return -1; |
| 124 | } else { |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | return ch; |
| 129 | } |
| 130 | |
Alex Bennée | a7e0f9b | 2023-03-02 18:57:49 -0800 | [diff] [blame] | 131 | bool gdb_got_immediate_ack(void) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | i = gdb_get_char(); |
| 136 | if (i < 0) { |
| 137 | /* no response, continue anyway */ |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | if (i == '+') { |
| 142 | /* received correctly, continue */ |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | /* anything else, including '-' then try again */ |
| 147 | return false; |
| 148 | } |
| 149 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 150 | void gdb_put_buffer(const uint8_t *buf, int len) |
| 151 | { |
| 152 | int ret; |
| 153 | |
| 154 | while (len > 0) { |
| 155 | ret = send(gdbserver_user_state.fd, buf, len, 0); |
| 156 | if (ret < 0) { |
| 157 | if (errno != EINTR) { |
| 158 | return; |
| 159 | } |
| 160 | } else { |
| 161 | buf += ret; |
| 162 | len -= ret; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* Tell the remote gdb that the process has exited. */ |
| 168 | void gdb_exit(int code) |
| 169 | { |
| 170 | char buf[4]; |
| 171 | |
| 172 | if (!gdbserver_state.init) { |
| 173 | return; |
| 174 | } |
| 175 | if (gdbserver_user_state.socket_path) { |
| 176 | unlink(gdbserver_user_state.socket_path); |
| 177 | } |
| 178 | if (gdbserver_user_state.fd < 0) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | trace_gdbstub_op_exiting((uint8_t)code); |
| 183 | |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 184 | if (gdbserver_state.allow_stop_reply) { |
| 185 | snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code); |
| 186 | gdb_put_packet(buf); |
| 187 | gdbserver_state.allow_stop_reply = false; |
| 188 | } |
Clément Chigot | e216256 | 2023-10-03 09:14:27 +0200 | [diff] [blame] | 189 | |
| 190 | } |
| 191 | |
| 192 | void gdb_qemu_exit(int code) |
| 193 | { |
| 194 | exit(code); |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 197 | int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo, |
| 198 | int siginfo_len) |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 199 | { |
| 200 | char buf[256]; |
| 201 | int n; |
| 202 | |
| 203 | if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { |
| 204 | return sig; |
| 205 | } |
| 206 | |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 207 | if (siginfo) { |
| 208 | /* |
| 209 | * Save target-specific siginfo. |
| 210 | * |
| 211 | * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in |
| 212 | * gdbserver_user_state.siginfo, usually in the source file calling |
| 213 | * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c. |
| 214 | */ |
| 215 | memcpy(gdbserver_user_state.siginfo, siginfo, siginfo_len); |
| 216 | gdbserver_user_state.siginfo_len = siginfo_len; |
| 217 | } |
| 218 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 219 | /* disable single step if it was enabled */ |
| 220 | cpu_single_step(cpu, 0); |
| 221 | tb_flush(cpu); |
| 222 | |
| 223 | if (sig != 0) { |
| 224 | gdb_set_stop_cpu(cpu); |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 225 | if (gdbserver_state.allow_stop_reply) { |
| 226 | g_string_printf(gdbserver_state.str_buf, |
| 227 | "T%02xthread:", gdb_target_signal_to_gdb(sig)); |
| 228 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); |
| 229 | g_string_append_c(gdbserver_state.str_buf, ';'); |
Ilya Leoshkevich | 8b7fcb8 | 2024-02-07 16:38:09 +0000 | [diff] [blame] | 230 | if (reason) { |
| 231 | g_string_append(gdbserver_state.str_buf, reason); |
| 232 | } |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 233 | gdb_put_strbuf(); |
| 234 | gdbserver_state.allow_stop_reply = false; |
| 235 | } |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 236 | } |
| 237 | /* |
| 238 | * gdb_put_packet() might have detected that the peer terminated the |
| 239 | * connection. |
| 240 | */ |
| 241 | if (gdbserver_user_state.fd < 0) { |
| 242 | return sig; |
| 243 | } |
| 244 | |
| 245 | sig = 0; |
| 246 | gdbserver_state.state = RS_IDLE; |
| 247 | gdbserver_user_state.running_state = 0; |
| 248 | while (gdbserver_user_state.running_state == 0) { |
| 249 | n = read(gdbserver_user_state.fd, buf, 256); |
| 250 | if (n > 0) { |
| 251 | int i; |
| 252 | |
| 253 | for (i = 0; i < n; i++) { |
| 254 | gdb_read_byte(buf[i]); |
| 255 | } |
| 256 | } else { |
| 257 | /* |
| 258 | * XXX: Connection closed. Should probably wait for another |
| 259 | * connection before continuing. |
| 260 | */ |
| 261 | if (n == 0) { |
| 262 | close(gdbserver_user_state.fd); |
| 263 | } |
| 264 | gdbserver_user_state.fd = -1; |
| 265 | return sig; |
| 266 | } |
| 267 | } |
| 268 | sig = gdbserver_state.signal; |
| 269 | gdbserver_state.signal = 0; |
| 270 | return sig; |
| 271 | } |
| 272 | |
| 273 | /* Tell the remote gdb that the process has exited due to SIG. */ |
| 274 | void gdb_signalled(CPUArchState *env, int sig) |
| 275 | { |
| 276 | char buf[4]; |
| 277 | |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 278 | if (!gdbserver_state.init || gdbserver_user_state.fd < 0 || |
| 279 | !gdbserver_state.allow_stop_reply) { |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 280 | return; |
| 281 | } |
| 282 | |
| 283 | snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig)); |
| 284 | gdb_put_packet(buf); |
Matheus Tavares Bernardino | 7583700 | 2023-05-04 12:37:31 -0300 | [diff] [blame] | 285 | gdbserver_state.allow_stop_reply = false; |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void gdb_accept_init(int fd) |
| 289 | { |
| 290 | gdb_init_gdbserver_state(); |
| 291 | gdb_create_default_process(&gdbserver_state); |
| 292 | gdbserver_state.processes[0].attached = true; |
| 293 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); |
| 294 | gdbserver_state.g_cpu = gdbserver_state.c_cpu; |
| 295 | gdbserver_user_state.fd = fd; |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | static bool gdb_accept_socket(int gdb_fd) |
| 299 | { |
| 300 | int fd; |
| 301 | |
| 302 | for (;;) { |
| 303 | fd = accept(gdb_fd, NULL, NULL); |
| 304 | if (fd < 0 && errno != EINTR) { |
| 305 | perror("accept socket"); |
| 306 | return false; |
| 307 | } else if (fd >= 0) { |
| 308 | qemu_set_cloexec(fd); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | gdb_accept_init(fd); |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | static int gdbserver_open_socket(const char *path) |
| 318 | { |
| 319 | struct sockaddr_un sockaddr = {}; |
| 320 | int fd, ret; |
| 321 | |
| 322 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 323 | if (fd < 0) { |
| 324 | perror("create socket"); |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | sockaddr.sun_family = AF_UNIX; |
| 329 | pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path); |
| 330 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 331 | if (ret < 0) { |
| 332 | perror("bind socket"); |
| 333 | close(fd); |
| 334 | return -1; |
| 335 | } |
| 336 | ret = listen(fd, 1); |
| 337 | if (ret < 0) { |
| 338 | perror("listen socket"); |
| 339 | close(fd); |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | return fd; |
| 344 | } |
| 345 | |
| 346 | static bool gdb_accept_tcp(int gdb_fd) |
| 347 | { |
| 348 | struct sockaddr_in sockaddr = {}; |
| 349 | socklen_t len; |
| 350 | int fd; |
| 351 | |
| 352 | for (;;) { |
| 353 | len = sizeof(sockaddr); |
| 354 | fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len); |
| 355 | if (fd < 0 && errno != EINTR) { |
| 356 | perror("accept"); |
| 357 | return false; |
| 358 | } else if (fd >= 0) { |
| 359 | qemu_set_cloexec(fd); |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* set short latency */ |
| 365 | if (socket_set_nodelay(fd)) { |
| 366 | perror("setsockopt"); |
| 367 | close(fd); |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | gdb_accept_init(fd); |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | static int gdbserver_open_port(int port) |
| 376 | { |
| 377 | struct sockaddr_in sockaddr; |
| 378 | int fd, ret; |
| 379 | |
| 380 | fd = socket(PF_INET, SOCK_STREAM, 0); |
| 381 | if (fd < 0) { |
| 382 | perror("socket"); |
| 383 | return -1; |
| 384 | } |
| 385 | qemu_set_cloexec(fd); |
| 386 | |
| 387 | socket_set_fast_reuse(fd); |
| 388 | |
| 389 | sockaddr.sin_family = AF_INET; |
| 390 | sockaddr.sin_port = htons(port); |
| 391 | sockaddr.sin_addr.s_addr = 0; |
| 392 | ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); |
| 393 | if (ret < 0) { |
| 394 | perror("bind"); |
| 395 | close(fd); |
| 396 | return -1; |
| 397 | } |
| 398 | ret = listen(fd, 1); |
| 399 | if (ret < 0) { |
| 400 | perror("listen"); |
| 401 | close(fd); |
| 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | return fd; |
| 406 | } |
| 407 | |
| 408 | int gdbserver_start(const char *port_or_path) |
| 409 | { |
| 410 | int port = g_ascii_strtoull(port_or_path, NULL, 10); |
| 411 | int gdb_fd; |
| 412 | |
| 413 | if (port > 0) { |
| 414 | gdb_fd = gdbserver_open_port(port); |
| 415 | } else { |
| 416 | gdb_fd = gdbserver_open_socket(port_or_path); |
| 417 | } |
| 418 | |
| 419 | if (gdb_fd < 0) { |
| 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | if (port > 0 && gdb_accept_tcp(gdb_fd)) { |
| 424 | return 0; |
| 425 | } else if (gdb_accept_socket(gdb_fd)) { |
| 426 | gdbserver_user_state.socket_path = g_strdup(port_or_path); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* gone wrong */ |
| 431 | close(gdb_fd); |
| 432 | return -1; |
| 433 | } |
| 434 | |
Ilya Leoshkevich | 3d6ed98 | 2024-03-05 12:09:41 +0000 | [diff] [blame] | 435 | void gdbserver_fork_start(void) |
| 436 | { |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 437 | if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { |
| 438 | return; |
| 439 | } |
| 440 | if (!gdbserver_user_state.fork_events || |
| 441 | qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, |
| 442 | gdbserver_user_state.fork_sockets) < 0) { |
| 443 | gdbserver_user_state.fork_state = GDB_FORK_DISABLED; |
| 444 | return; |
| 445 | } |
| 446 | gdbserver_user_state.fork_state = GDB_FORK_INACTIVE; |
| 447 | gdbserver_user_state.fork_peer_pid = getpid(); |
| 448 | gdbserver_user_state.fork_peer_tid = qemu_get_thread_id(); |
Ilya Leoshkevich | 3d6ed98 | 2024-03-05 12:09:41 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Ilya Leoshkevich | 1ea96f1 | 2024-03-05 12:09:38 +0000 | [diff] [blame] | 451 | static void disable_gdbstub(CPUState *thread_cpu) |
| 452 | { |
| 453 | CPUState *cpu; |
| 454 | |
| 455 | close(gdbserver_user_state.fd); |
| 456 | gdbserver_user_state.fd = -1; |
| 457 | CPU_FOREACH(cpu) { |
| 458 | cpu_breakpoint_remove_all(cpu, BP_GDB); |
| 459 | /* no cpu_watchpoint_remove_all for user-mode */ |
| 460 | cpu_single_step(cpu, 0); |
| 461 | } |
| 462 | tb_flush(thread_cpu); |
| 463 | } |
| 464 | |
Ilya Leoshkevich | 6604b05 | 2024-03-05 12:09:44 +0000 | [diff] [blame] | 465 | void gdbserver_fork_end(CPUState *cpu, pid_t pid) |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 466 | { |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 467 | char b; |
| 468 | int fd; |
| 469 | |
| 470 | if (!gdbserver_state.init || gdbserver_user_state.fd < 0) { |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 471 | return; |
| 472 | } |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 473 | |
| 474 | if (pid == -1) { |
| 475 | if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) { |
| 476 | g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE); |
| 477 | close(gdbserver_user_state.fork_sockets[0]); |
| 478 | close(gdbserver_user_state.fork_sockets[1]); |
| 479 | } |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) { |
| 484 | if (pid == 0) { |
| 485 | disable_gdbstub(cpu); |
| 486 | } |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | if (pid == 0) { |
| 491 | close(gdbserver_user_state.fork_sockets[0]); |
| 492 | fd = gdbserver_user_state.fork_sockets[1]; |
| 493 | g_assert(gdbserver_state.process_num == 1); |
| 494 | g_assert(gdbserver_state.processes[0].pid == |
| 495 | gdbserver_user_state.fork_peer_pid); |
| 496 | g_assert(gdbserver_state.processes[0].attached); |
| 497 | gdbserver_state.processes[0].pid = getpid(); |
| 498 | } else { |
| 499 | close(gdbserver_user_state.fork_sockets[1]); |
| 500 | fd = gdbserver_user_state.fork_sockets[0]; |
| 501 | gdbserver_user_state.fork_state = GDB_FORK_ACTIVE; |
| 502 | gdbserver_user_state.fork_peer_pid = pid; |
| 503 | gdbserver_user_state.fork_peer_tid = pid; |
| 504 | |
| 505 | if (!gdbserver_state.allow_stop_reply) { |
| 506 | goto fail; |
| 507 | } |
| 508 | g_string_printf(gdbserver_state.str_buf, |
| 509 | "T%02xfork:p%02x.%02x;thread:p%02x.%02x;", |
| 510 | gdb_target_signal_to_gdb(gdb_target_sigtrap()), |
| 511 | pid, pid, (int)getpid(), qemu_get_thread_id()); |
| 512 | gdb_put_strbuf(); |
| 513 | } |
| 514 | |
| 515 | gdbserver_state.state = RS_IDLE; |
| 516 | gdbserver_state.allow_stop_reply = false; |
| 517 | gdbserver_user_state.running_state = 0; |
| 518 | for (;;) { |
| 519 | switch (gdbserver_user_state.fork_state) { |
| 520 | case GDB_FORK_ENABLED: |
| 521 | if (gdbserver_user_state.running_state) { |
Ilya Leoshkevich | 6971998 | 2024-03-12 01:07:01 +0100 | [diff] [blame] | 522 | close(fd); |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | QEMU_FALLTHROUGH; |
| 526 | case GDB_FORK_ACTIVE: |
| 527 | if (read(gdbserver_user_state.fd, &b, 1) != 1) { |
| 528 | goto fail; |
| 529 | } |
| 530 | gdb_read_byte(b); |
| 531 | break; |
| 532 | case GDB_FORK_DEACTIVATING: |
| 533 | b = GDB_FORK_ACTIVATE; |
| 534 | if (write(fd, &b, 1) != 1) { |
| 535 | goto fail; |
| 536 | } |
| 537 | gdbserver_user_state.fork_state = GDB_FORK_INACTIVE; |
| 538 | break; |
| 539 | case GDB_FORK_INACTIVE: |
| 540 | if (read(fd, &b, 1) != 1) { |
| 541 | goto fail; |
| 542 | } |
| 543 | switch (b) { |
| 544 | case GDB_FORK_ACTIVATE: |
| 545 | gdbserver_user_state.fork_state = GDB_FORK_ACTIVE; |
| 546 | break; |
| 547 | case GDB_FORK_ENABLE: |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 548 | gdbserver_user_state.fork_state = GDB_FORK_ENABLED; |
| 549 | break; |
| 550 | case GDB_FORK_DISABLE: |
| 551 | gdbserver_user_state.fork_state = GDB_FORK_DISABLED; |
| 552 | break; |
| 553 | default: |
| 554 | g_assert_not_reached(); |
| 555 | } |
| 556 | break; |
| 557 | case GDB_FORK_ENABLING: |
| 558 | b = GDB_FORK_DISABLE; |
| 559 | if (write(fd, &b, 1) != 1) { |
| 560 | goto fail; |
| 561 | } |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 562 | gdbserver_user_state.fork_state = GDB_FORK_ENABLED; |
| 563 | break; |
| 564 | case GDB_FORK_DISABLING: |
| 565 | b = GDB_FORK_ENABLE; |
| 566 | if (write(fd, &b, 1) != 1) { |
| 567 | goto fail; |
| 568 | } |
| 569 | gdbserver_user_state.fork_state = GDB_FORK_DISABLED; |
| 570 | break; |
| 571 | case GDB_FORK_DISABLED: |
| 572 | close(fd); |
| 573 | disable_gdbstub(cpu); |
| 574 | return; |
| 575 | default: |
| 576 | g_assert_not_reached(); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | fail: |
| 581 | close(fd); |
| 582 | if (pid == 0) { |
| 583 | disable_gdbstub(cpu); |
| 584 | } |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Ilya Leoshkevich | 6d92311 | 2024-03-05 12:09:45 +0000 | [diff] [blame] | 587 | void gdb_handle_query_supported_user(const char *gdb_supported) |
| 588 | { |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 589 | if (strstr(gdb_supported, "fork-events+")) { |
| 590 | gdbserver_user_state.fork_events = true; |
| 591 | } |
| 592 | g_string_append(gdbserver_state.str_buf, ";fork-events+"); |
Ilya Leoshkevich | 6d92311 | 2024-03-05 12:09:45 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Ilya Leoshkevich | e454f2f | 2024-03-05 12:09:46 +0000 | [diff] [blame] | 595 | bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid) |
| 596 | { |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 597 | if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE && |
| 598 | pid == gdbserver_user_state.fork_peer_pid && |
| 599 | tid == gdbserver_user_state.fork_peer_tid) { |
| 600 | gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING; |
| 601 | gdb_put_packet("OK"); |
| 602 | return true; |
| 603 | } |
Ilya Leoshkevich | e454f2f | 2024-03-05 12:09:46 +0000 | [diff] [blame] | 604 | return false; |
| 605 | } |
| 606 | |
Ilya Leoshkevich | 539cb4e | 2024-03-05 12:09:47 +0000 | [diff] [blame] | 607 | bool gdb_handle_detach_user(uint32_t pid) |
| 608 | { |
Ilya Leoshkevich | d547e71 | 2024-03-05 12:09:48 +0000 | [diff] [blame] | 609 | bool enable; |
| 610 | |
| 611 | if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) { |
| 612 | enable = pid == gdbserver_user_state.fork_peer_pid; |
| 613 | if (enable || pid == getpid()) { |
| 614 | gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING : |
| 615 | GDB_FORK_DISABLING; |
| 616 | gdb_put_packet("OK"); |
| 617 | return true; |
| 618 | } |
| 619 | } |
Ilya Leoshkevich | 539cb4e | 2024-03-05 12:09:47 +0000 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 623 | /* |
| 624 | * Execution state helpers |
| 625 | */ |
| 626 | |
Alex Bennée | 8a2025b | 2023-03-02 18:57:50 -0800 | [diff] [blame] | 627 | void gdb_handle_query_attached(GArray *params, void *user_ctx) |
| 628 | { |
| 629 | gdb_put_packet("0"); |
| 630 | } |
| 631 | |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 632 | void gdb_continue(void) |
| 633 | { |
| 634 | gdbserver_user_state.running_state = 1; |
| 635 | trace_gdbstub_op_continue(); |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Resume execution, for user-mode emulation it's equivalent to |
| 640 | * gdb_continue. |
| 641 | */ |
| 642 | int gdb_continue_partial(char *newstates) |
| 643 | { |
| 644 | CPUState *cpu; |
| 645 | int res = 0; |
| 646 | /* |
| 647 | * This is not exactly accurate, but it's an improvement compared to the |
| 648 | * previous situation, where only one CPU would be single-stepped. |
| 649 | */ |
| 650 | CPU_FOREACH(cpu) { |
| 651 | if (newstates[cpu->cpu_index] == 's') { |
| 652 | trace_gdbstub_op_stepping(cpu->cpu_index); |
| 653 | cpu_single_step(cpu, gdbserver_state.sstep_flags); |
| 654 | } |
| 655 | } |
| 656 | gdbserver_user_state.running_state = 1; |
| 657 | return res; |
| 658 | } |
| 659 | |
| 660 | /* |
Alex Bennée | 589a586 | 2023-03-02 18:57:51 -0800 | [diff] [blame] | 661 | * Memory access helpers |
| 662 | */ |
| 663 | int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr, |
| 664 | uint8_t *buf, int len, bool is_write) |
| 665 | { |
| 666 | CPUClass *cc; |
| 667 | |
| 668 | cc = CPU_GET_CLASS(cpu); |
| 669 | if (cc->memory_rw_debug) { |
| 670 | return cc->memory_rw_debug(cpu, addr, buf, len, is_write); |
| 671 | } |
| 672 | return cpu_memory_rw_debug(cpu, addr, buf, len, is_write); |
| 673 | } |
| 674 | |
| 675 | /* |
Alex Bennée | 7ea0c33 | 2023-03-02 18:57:52 -0800 | [diff] [blame] | 676 | * cpu helpers |
| 677 | */ |
| 678 | |
| 679 | unsigned int gdb_get_max_cpus(void) |
| 680 | { |
| 681 | CPUState *cpu; |
| 682 | unsigned int max_cpus = 1; |
| 683 | |
| 684 | CPU_FOREACH(cpu) { |
| 685 | max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus; |
| 686 | } |
| 687 | |
| 688 | return max_cpus; |
| 689 | } |
| 690 | |
Alex Bennée | 505601d | 2023-03-02 18:57:53 -0800 | [diff] [blame] | 691 | /* replay not supported for user-mode */ |
| 692 | bool gdb_can_reverse(void) |
| 693 | { |
| 694 | return false; |
| 695 | } |
Alex Bennée | 7ea0c33 | 2023-03-02 18:57:52 -0800 | [diff] [blame] | 696 | |
| 697 | /* |
Alex Bennée | d96bf49 | 2023-03-02 18:57:47 -0800 | [diff] [blame] | 698 | * Break/Watch point helpers |
| 699 | */ |
| 700 | |
Alex Bennée | a48e7d9 | 2022-09-29 12:42:25 +0100 | [diff] [blame] | 701 | bool gdb_supports_guest_debug(void) |
| 702 | { |
| 703 | /* user-mode == TCG == supported */ |
| 704 | return true; |
| 705 | } |
| 706 | |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 707 | 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] | 708 | { |
| 709 | CPUState *cpu; |
| 710 | int err = 0; |
| 711 | |
| 712 | switch (type) { |
| 713 | case GDB_BREAKPOINT_SW: |
| 714 | case GDB_BREAKPOINT_HW: |
| 715 | CPU_FOREACH(cpu) { |
| 716 | err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL); |
| 717 | if (err) { |
| 718 | break; |
| 719 | } |
| 720 | } |
| 721 | return err; |
| 722 | default: |
| 723 | /* user-mode doesn't support watchpoints */ |
| 724 | return -ENOSYS; |
| 725 | } |
| 726 | } |
| 727 | |
Philippe Mathieu-Daudé | 55b5b8e | 2022-12-06 16:20:27 +0100 | [diff] [blame] | 728 | 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] | 729 | { |
| 730 | CPUState *cpu; |
| 731 | int err = 0; |
| 732 | |
| 733 | switch (type) { |
| 734 | case GDB_BREAKPOINT_SW: |
| 735 | case GDB_BREAKPOINT_HW: |
| 736 | CPU_FOREACH(cpu) { |
| 737 | err = cpu_breakpoint_remove(cpu, addr, BP_GDB); |
| 738 | if (err) { |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | return err; |
| 743 | default: |
| 744 | /* user-mode doesn't support watchpoints */ |
| 745 | return -ENOSYS; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void gdb_breakpoint_remove_all(CPUState *cs) |
| 750 | { |
| 751 | cpu_breakpoint_remove_all(cs, BP_GDB); |
| 752 | } |
Alex Bennée | 131f387 | 2023-03-02 18:58:01 -0800 | [diff] [blame] | 753 | |
| 754 | /* |
| 755 | * For user-mode syscall support we send the system call immediately |
| 756 | * and then return control to gdb for it to process the syscall request. |
| 757 | * Since the protocol requires that gdb hands control back to us |
| 758 | * using a "here are the results" F packet, we don't need to check |
| 759 | * gdb_handlesig's return value (which is the signal to deliver if |
| 760 | * execution was resumed via a continue packet). |
| 761 | */ |
| 762 | void gdb_syscall_handling(const char *syscall_packet) |
| 763 | { |
| 764 | gdb_put_packet(syscall_packet); |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 765 | gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0); |
Alex Bennée | 131f387 | 2023-03-02 18:58:01 -0800 | [diff] [blame] | 766 | } |
Ilya Leoshkevich | 0a0d87c | 2024-02-07 16:38:10 +0000 | [diff] [blame] | 767 | |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 768 | static bool should_catch_syscall(int num) |
| 769 | { |
| 770 | if (gdbserver_user_state.catch_all_syscalls) { |
| 771 | return true; |
| 772 | } |
| 773 | if (num < 0 || num >= GDB_NR_SYSCALLS) { |
| 774 | return false; |
| 775 | } |
| 776 | return test_bit(num, gdbserver_user_state.catch_syscalls_mask); |
| 777 | } |
| 778 | |
Ilya Leoshkevich | 0a0d87c | 2024-02-07 16:38:10 +0000 | [diff] [blame] | 779 | void gdb_syscall_entry(CPUState *cs, int num) |
| 780 | { |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 781 | if (should_catch_syscall(num)) { |
| 782 | g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num); |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 783 | gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0); |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 784 | } |
Ilya Leoshkevich | 0a0d87c | 2024-02-07 16:38:10 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void gdb_syscall_return(CPUState *cs, int num) |
| 788 | { |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 789 | if (should_catch_syscall(num)) { |
| 790 | g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num); |
Gustavo Romero | f84e313 | 2024-03-09 03:08:59 +0000 | [diff] [blame] | 791 | gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0); |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx) |
| 796 | { |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 797 | const char *param = gdb_get_cmd_param(params, 0)->data; |
Ilya Leoshkevich | 046f143 | 2024-02-07 16:38:11 +0000 | [diff] [blame] | 798 | GDBSyscallsMask catch_syscalls_mask; |
| 799 | bool catch_all_syscalls; |
| 800 | unsigned int num; |
| 801 | const char *p; |
| 802 | |
| 803 | /* "0" means not catching any syscalls. */ |
| 804 | if (strcmp(param, "0") == 0) { |
| 805 | gdbserver_user_state.catch_all_syscalls = false; |
| 806 | memset(gdbserver_user_state.catch_syscalls_mask, 0, |
| 807 | sizeof(gdbserver_user_state.catch_syscalls_mask)); |
| 808 | gdb_put_packet("OK"); |
| 809 | return; |
| 810 | } |
| 811 | |
| 812 | /* "1" means catching all syscalls. */ |
| 813 | if (strcmp(param, "1") == 0) { |
| 814 | gdbserver_user_state.catch_all_syscalls = true; |
| 815 | gdb_put_packet("OK"); |
| 816 | return; |
| 817 | } |
| 818 | |
| 819 | /* |
| 820 | * "1;..." means catching only the specified syscalls. |
| 821 | * The syscall list must not be empty. |
| 822 | */ |
| 823 | if (param[0] == '1' && param[1] == ';') { |
| 824 | catch_all_syscalls = false; |
| 825 | memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask)); |
| 826 | for (p = ¶m[2];; p++) { |
| 827 | if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) { |
| 828 | goto err; |
| 829 | } |
| 830 | if (num >= GDB_NR_SYSCALLS) { |
| 831 | /* |
| 832 | * Fall back to reporting all syscalls. Reporting extra |
| 833 | * syscalls is inefficient, but the spec explicitly allows it. |
| 834 | * Keep parsing in case there is a syntax error ahead. |
| 835 | */ |
| 836 | catch_all_syscalls = true; |
| 837 | } else { |
| 838 | set_bit(num, catch_syscalls_mask); |
| 839 | } |
| 840 | if (!*p) { |
| 841 | break; |
| 842 | } |
| 843 | } |
| 844 | gdbserver_user_state.catch_all_syscalls = catch_all_syscalls; |
| 845 | if (!catch_all_syscalls) { |
| 846 | memcpy(gdbserver_user_state.catch_syscalls_mask, |
| 847 | catch_syscalls_mask, sizeof(catch_syscalls_mask)); |
| 848 | } |
| 849 | gdb_put_packet("OK"); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | err: |
| 854 | gdb_put_packet("E00"); |
Ilya Leoshkevich | 0a0d87c | 2024-02-07 16:38:10 +0000 | [diff] [blame] | 855 | } |
Gustavo Romero | 9ae5801 | 2024-03-09 03:09:00 +0000 | [diff] [blame] | 856 | |
| 857 | void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx) |
| 858 | { |
| 859 | unsigned long offset, len; |
| 860 | uint8_t *siginfo_offset; |
| 861 | |
Gustavo Romero | 133f202 | 2024-07-05 09:40:38 +0100 | [diff] [blame] | 862 | offset = gdb_get_cmd_param(params, 0)->val_ul; |
| 863 | len = gdb_get_cmd_param(params, 1)->val_ul; |
Gustavo Romero | 9ae5801 | 2024-03-09 03:09:00 +0000 | [diff] [blame] | 864 | |
| 865 | if (offset + len > gdbserver_user_state.siginfo_len) { |
| 866 | /* Invalid offset and/or requested length. */ |
| 867 | gdb_put_packet("E01"); |
| 868 | return; |
| 869 | } |
| 870 | |
| 871 | siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset; |
| 872 | |
| 873 | /* Reply */ |
| 874 | g_string_assign(gdbserver_state.str_buf, "l"); |
| 875 | gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len); |
| 876 | gdb_put_packet_binary(gdbserver_state.str_buf->str, |
| 877 | gdbserver_state.str_buf->len, true); |
| 878 | } |