blob: 8225b7028021e09d409882dfa9783c92d69697fe [file] [log] [blame]
Alex Bennéeae7467b2022-09-29 12:42:24 +01001/*
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ée94557622023-03-02 18:57:38 -08006 * Copyright (c) 2003-2005 Fabrice Bellard
Alex Bennéeae7467b2022-09-29 12:42:24 +01007 * Copyright (c) 2022 Linaro Ltd
8 *
Philippe Mathieu-Daudéb14d0642024-09-11 17:12:04 +02009 * SPDX-License-Identifier: LGPL-2.0-or-later
Alex Bennéeae7467b2022-09-29 12:42:24 +010010 */
11
12#include "qemu/osdep.h"
Ilya Leoshkevich046f1432024-02-07 16:38:11 +000013#include "qemu/bitops.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080014#include "qemu/cutils.h"
15#include "qemu/sockets.h"
Alex Bennéec0e6b8b2025-01-16 16:02:39 +000016#include "qapi/error.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080017#include "exec/hwaddr.h"
18#include "exec/tb-flush.h"
Alex Bennéeae7467b2022-09-29 12:42:24 +010019#include "exec/gdbstub.h"
Gustavo Romero133f2022024-07-05 09:40:38 +010020#include "gdbstub/commands.h"
Alex Bennéec5660802023-03-02 18:57:57 -080021#include "gdbstub/syscalls.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080022#include "gdbstub/user.h"
Alex Bennée5b7d54d2024-06-20 16:22:10 +010023#include "gdbstub/enums.h"
Alex Bennéeae7467b2022-09-29 12:42:24 +010024#include "hw/core/cpu.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080025#include "trace.h"
Alex Bennéeae7467b2022-09-29 12:42:24 +010026#include "internals.h"
27
Ilya Leoshkevich046f1432024-02-07 16:38:11 +000028#define GDB_NR_SYSCALLS 1024
29typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)];
30
Ilya Leoshkevichd547e712024-03-05 12:09:48 +000031/*
32 * Forked child talks to its parent in order to let GDB enforce the
33 * follow-fork-mode. This happens inside a start_exclusive() section, so that
34 * the other threads, which may be forking too, do not interfere. The
35 * implementation relies on GDB not sending $vCont until it has detached
36 * either from the parent (follow-fork-mode child) or from the child
37 * (follow-fork-mode parent).
38 *
39 * The parent and the child share the GDB socket; at any given time only one
40 * of them is allowed to use it, as is reflected in the respective fork_state.
41 * This is negotiated via the fork_sockets pair as a reaction to $Hg.
42 *
43 * Below is a short summary of the possible state transitions:
44 *
45 * ENABLED : Terminal state.
46 * DISABLED : Terminal state.
47 * ACTIVE : Parent initial state.
48 * INACTIVE : Child initial state.
49 * ACTIVE -> DEACTIVATING: On $Hg.
50 * ACTIVE -> ENABLING : On $D.
51 * ACTIVE -> DISABLING : On $D.
52 * ACTIVE -> DISABLED : On communication error.
53 * DEACTIVATING -> INACTIVE : On gdb_read_byte() return.
54 * DEACTIVATING -> DISABLED : On communication error.
55 * INACTIVE -> ACTIVE : On $Hg in the peer.
56 * INACTIVE -> ENABLE : On $D in the peer.
57 * INACTIVE -> DISABLE : On $D in the peer.
58 * INACTIVE -> DISABLED : On communication error.
59 * ENABLING -> ENABLED : On gdb_read_byte() return.
60 * ENABLING -> DISABLED : On communication error.
61 * DISABLING -> DISABLED : On gdb_read_byte() return.
62 */
63enum GDBForkState {
64 /* Fully owning the GDB socket. */
65 GDB_FORK_ENABLED,
66 /* Working with the GDB socket; the peer is inactive. */
67 GDB_FORK_ACTIVE,
68 /* Handing off the GDB socket to the peer. */
69 GDB_FORK_DEACTIVATING,
70 /* The peer is working with the GDB socket. */
71 GDB_FORK_INACTIVE,
72 /* Asking the peer to close its GDB socket fd. */
73 GDB_FORK_ENABLING,
74 /* Asking the peer to take over, closing our GDB socket fd. */
75 GDB_FORK_DISABLING,
76 /* The peer has taken over, our GDB socket fd is closed. */
77 GDB_FORK_DISABLED,
78};
79
80enum GDBForkMessage {
81 GDB_FORK_ACTIVATE = 'a',
82 GDB_FORK_ENABLE = 'e',
83 GDB_FORK_DISABLE = 'd',
84};
85
Alex Bennéed96bf492023-03-02 18:57:47 -080086/* User-mode specific state */
87typedef struct {
88 int fd;
89 char *socket_path;
90 int running_state;
Ilya Leoshkevich046f1432024-02-07 16:38:11 +000091 /*
92 * Store syscalls mask without memory allocation in order to avoid
93 * implementing synchronization.
94 */
95 bool catch_all_syscalls;
96 GDBSyscallsMask catch_syscalls_mask;
Ilya Leoshkevichd547e712024-03-05 12:09:48 +000097 bool fork_events;
98 enum GDBForkState fork_state;
99 int fork_sockets[2];
100 pid_t fork_peer_pid, fork_peer_tid;
Gustavo Romerof84e3132024-03-09 03:08:59 +0000101 uint8_t siginfo[MAX_SIGINFO_LENGTH];
102 unsigned long siginfo_len;
Alex Bennéed96bf492023-03-02 18:57:47 -0800103} GDBUserState;
104
105static GDBUserState gdbserver_user_state;
106
107int gdb_get_char(void)
108{
109 uint8_t ch;
110 int ret;
111
112 for (;;) {
113 ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
114 if (ret < 0) {
115 if (errno == ECONNRESET) {
116 gdbserver_user_state.fd = -1;
117 }
118 if (errno != EINTR) {
119 return -1;
120 }
121 } else if (ret == 0) {
122 close(gdbserver_user_state.fd);
123 gdbserver_user_state.fd = -1;
124 return -1;
125 } else {
126 break;
127 }
128 }
129 return ch;
130}
131
Alex Bennéea7e0f9b2023-03-02 18:57:49 -0800132bool gdb_got_immediate_ack(void)
133{
134 int i;
135
136 i = gdb_get_char();
137 if (i < 0) {
138 /* no response, continue anyway */
139 return true;
140 }
141
142 if (i == '+') {
143 /* received correctly, continue */
144 return true;
145 }
146
147 /* anything else, including '-' then try again */
148 return false;
149}
150
Alex Bennéed96bf492023-03-02 18:57:47 -0800151void gdb_put_buffer(const uint8_t *buf, int len)
152{
153 int ret;
154
155 while (len > 0) {
156 ret = send(gdbserver_user_state.fd, buf, len, 0);
157 if (ret < 0) {
158 if (errno != EINTR) {
159 return;
160 }
161 } else {
162 buf += ret;
163 len -= ret;
164 }
165 }
166}
167
168/* Tell the remote gdb that the process has exited. */
169void gdb_exit(int code)
170{
171 char buf[4];
172
173 if (!gdbserver_state.init) {
174 return;
175 }
176 if (gdbserver_user_state.socket_path) {
177 unlink(gdbserver_user_state.socket_path);
178 }
179 if (gdbserver_user_state.fd < 0) {
180 return;
181 }
182
183 trace_gdbstub_op_exiting((uint8_t)code);
184
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300185 if (gdbserver_state.allow_stop_reply) {
186 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
187 gdb_put_packet(buf);
188 gdbserver_state.allow_stop_reply = false;
189 }
Clément Chigote2162562023-10-03 09:14:27 +0200190
191}
192
193void gdb_qemu_exit(int code)
194{
195 exit(code);
Alex Bennéed96bf492023-03-02 18:57:47 -0800196}
197
Gustavo Romerof84e3132024-03-09 03:08:59 +0000198int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
199 int siginfo_len)
Alex Bennéed96bf492023-03-02 18:57:47 -0800200{
201 char buf[256];
202 int n;
203
204 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
205 return sig;
206 }
207
Gustavo Romerof84e3132024-03-09 03:08:59 +0000208 if (siginfo) {
209 /*
210 * Save target-specific siginfo.
211 *
212 * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
213 * gdbserver_user_state.siginfo, usually in the source file calling
214 * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
215 */
216 memcpy(gdbserver_user_state.siginfo, siginfo, siginfo_len);
217 gdbserver_user_state.siginfo_len = siginfo_len;
218 }
219
Alex Bennéed96bf492023-03-02 18:57:47 -0800220 /* disable single step if it was enabled */
221 cpu_single_step(cpu, 0);
222 tb_flush(cpu);
223
224 if (sig != 0) {
225 gdb_set_stop_cpu(cpu);
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300226 if (gdbserver_state.allow_stop_reply) {
227 g_string_printf(gdbserver_state.str_buf,
228 "T%02xthread:", gdb_target_signal_to_gdb(sig));
229 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
230 g_string_append_c(gdbserver_state.str_buf, ';');
Ilya Leoshkevich8b7fcb82024-02-07 16:38:09 +0000231 if (reason) {
232 g_string_append(gdbserver_state.str_buf, reason);
233 }
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300234 gdb_put_strbuf();
235 gdbserver_state.allow_stop_reply = false;
236 }
Alex Bennéed96bf492023-03-02 18:57:47 -0800237 }
238 /*
239 * gdb_put_packet() might have detected that the peer terminated the
240 * connection.
241 */
242 if (gdbserver_user_state.fd < 0) {
243 return sig;
244 }
245
246 sig = 0;
247 gdbserver_state.state = RS_IDLE;
248 gdbserver_user_state.running_state = 0;
249 while (gdbserver_user_state.running_state == 0) {
250 n = read(gdbserver_user_state.fd, buf, 256);
251 if (n > 0) {
252 int i;
253
254 for (i = 0; i < n; i++) {
255 gdb_read_byte(buf[i]);
256 }
257 } else {
258 /*
259 * XXX: Connection closed. Should probably wait for another
260 * connection before continuing.
261 */
262 if (n == 0) {
263 close(gdbserver_user_state.fd);
264 }
265 gdbserver_user_state.fd = -1;
266 return sig;
267 }
268 }
269 sig = gdbserver_state.signal;
270 gdbserver_state.signal = 0;
271 return sig;
272}
273
274/* Tell the remote gdb that the process has exited due to SIG. */
275void gdb_signalled(CPUArchState *env, int sig)
276{
277 char buf[4];
278
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300279 if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
280 !gdbserver_state.allow_stop_reply) {
Alex Bennéed96bf492023-03-02 18:57:47 -0800281 return;
282 }
283
284 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
285 gdb_put_packet(buf);
Matheus Tavares Bernardino75837002023-05-04 12:37:31 -0300286 gdbserver_state.allow_stop_reply = false;
Alex Bennéed96bf492023-03-02 18:57:47 -0800287}
288
289static void gdb_accept_init(int fd)
290{
291 gdb_init_gdbserver_state();
292 gdb_create_default_process(&gdbserver_state);
293 gdbserver_state.processes[0].attached = true;
294 gdbserver_state.c_cpu = gdb_first_attached_cpu();
295 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
296 gdbserver_user_state.fd = fd;
Alex Bennéed96bf492023-03-02 18:57:47 -0800297}
298
299static bool gdb_accept_socket(int gdb_fd)
300{
301 int fd;
302
303 for (;;) {
304 fd = accept(gdb_fd, NULL, NULL);
305 if (fd < 0 && errno != EINTR) {
306 perror("accept socket");
307 return false;
308 } else if (fd >= 0) {
309 qemu_set_cloexec(fd);
310 break;
311 }
312 }
313
314 gdb_accept_init(fd);
315 return true;
316}
317
Ilya Leoshkevichfccb7442025-02-07 15:31:06 +0000318static int gdbserver_open_socket(const char *path, Error **errp)
Alex Bennéed96bf492023-03-02 18:57:47 -0800319{
Ilya Leoshkevich98534852025-02-07 15:31:05 +0000320 g_autoptr(GString) buf = g_string_new("");
Ilya Leoshkevich98534852025-02-07 15:31:05 +0000321 char *pid_placeholder;
Alex Bennéed96bf492023-03-02 18:57:47 -0800322
Ilya Leoshkevich98534852025-02-07 15:31:05 +0000323 pid_placeholder = strstr(path, "%d");
324 if (pid_placeholder != NULL) {
325 g_string_append_len(buf, path, pid_placeholder - path);
326 g_string_append_printf(buf, "%d", qemu_get_thread_id());
327 g_string_append(buf, pid_placeholder + 2);
328 path = buf->str;
329 }
330
Ilya Leoshkevichfccb7442025-02-07 15:31:06 +0000331 return unix_listen(path, errp);
Alex Bennéed96bf492023-03-02 18:57:47 -0800332}
333
334static bool gdb_accept_tcp(int gdb_fd)
335{
336 struct sockaddr_in sockaddr = {};
337 socklen_t len;
338 int fd;
339
340 for (;;) {
341 len = sizeof(sockaddr);
342 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
343 if (fd < 0 && errno != EINTR) {
344 perror("accept");
345 return false;
346 } else if (fd >= 0) {
347 qemu_set_cloexec(fd);
348 break;
349 }
350 }
351
352 /* set short latency */
353 if (socket_set_nodelay(fd)) {
354 perror("setsockopt");
355 close(fd);
356 return false;
357 }
358
359 gdb_accept_init(fd);
360 return true;
361}
362
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000363static int gdbserver_open_port(int port, Error **errp)
Alex Bennéed96bf492023-03-02 18:57:47 -0800364{
365 struct sockaddr_in sockaddr;
366 int fd, ret;
367
368 fd = socket(PF_INET, SOCK_STREAM, 0);
369 if (fd < 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000370 error_setg_errno(errp, errno, "Failed to create socket");
Alex Bennéed96bf492023-03-02 18:57:47 -0800371 return -1;
372 }
373 qemu_set_cloexec(fd);
374
375 socket_set_fast_reuse(fd);
376
377 sockaddr.sin_family = AF_INET;
378 sockaddr.sin_port = htons(port);
379 sockaddr.sin_addr.s_addr = 0;
380 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
381 if (ret < 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000382 error_setg_errno(errp, errno, "Failed to bind socket");
Alex Bennéed96bf492023-03-02 18:57:47 -0800383 close(fd);
384 return -1;
385 }
386 ret = listen(fd, 1);
387 if (ret < 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000388 error_setg_errno(errp, errno, "Failed to listen to socket");
Alex Bennéed96bf492023-03-02 18:57:47 -0800389 close(fd);
390 return -1;
391 }
392
393 return fd;
394}
395
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000396bool gdbserver_start(const char *port_or_path, Error **errp)
Alex Bennéed96bf492023-03-02 18:57:47 -0800397{
398 int port = g_ascii_strtoull(port_or_path, NULL, 10);
399 int gdb_fd;
400
401 if (port > 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000402 gdb_fd = gdbserver_open_port(port, errp);
Alex Bennéed96bf492023-03-02 18:57:47 -0800403 } else {
Ilya Leoshkevichfccb7442025-02-07 15:31:06 +0000404 gdb_fd = gdbserver_open_socket(port_or_path, errp);
Alex Bennéed96bf492023-03-02 18:57:47 -0800405 }
406
407 if (gdb_fd < 0) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000408 return false;
Alex Bennéed96bf492023-03-02 18:57:47 -0800409 }
410
411 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000412 return true;
Alex Bennéed96bf492023-03-02 18:57:47 -0800413 } else if (gdb_accept_socket(gdb_fd)) {
414 gdbserver_user_state.socket_path = g_strdup(port_or_path);
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000415 return true;
Alex Bennéed96bf492023-03-02 18:57:47 -0800416 }
417
418 /* gone wrong */
419 close(gdb_fd);
Alex Bennéec0e6b8b2025-01-16 16:02:39 +0000420 error_setg(errp, "gdbstub: failed to accept connection");
421 return false;
Alex Bennéed96bf492023-03-02 18:57:47 -0800422}
423
Ilya Leoshkevich3d6ed982024-03-05 12:09:41 +0000424void gdbserver_fork_start(void)
425{
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000426 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
427 return;
428 }
429 if (!gdbserver_user_state.fork_events ||
430 qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
431 gdbserver_user_state.fork_sockets) < 0) {
432 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
433 return;
434 }
435 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
436 gdbserver_user_state.fork_peer_pid = getpid();
437 gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
Ilya Leoshkevich3d6ed982024-03-05 12:09:41 +0000438}
439
Ilya Leoshkevich1ea96f12024-03-05 12:09:38 +0000440static void disable_gdbstub(CPUState *thread_cpu)
441{
442 CPUState *cpu;
443
444 close(gdbserver_user_state.fd);
445 gdbserver_user_state.fd = -1;
446 CPU_FOREACH(cpu) {
447 cpu_breakpoint_remove_all(cpu, BP_GDB);
448 /* no cpu_watchpoint_remove_all for user-mode */
449 cpu_single_step(cpu, 0);
450 }
451 tb_flush(thread_cpu);
452}
453
Ilya Leoshkevich6604b052024-03-05 12:09:44 +0000454void gdbserver_fork_end(CPUState *cpu, pid_t pid)
Alex Bennéed96bf492023-03-02 18:57:47 -0800455{
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000456 char b;
457 int fd;
458
459 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
Alex Bennéed96bf492023-03-02 18:57:47 -0800460 return;
461 }
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000462
463 if (pid == -1) {
464 if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
465 g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
466 close(gdbserver_user_state.fork_sockets[0]);
467 close(gdbserver_user_state.fork_sockets[1]);
468 }
469 return;
470 }
471
472 if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
473 if (pid == 0) {
474 disable_gdbstub(cpu);
475 }
476 return;
477 }
478
479 if (pid == 0) {
480 close(gdbserver_user_state.fork_sockets[0]);
481 fd = gdbserver_user_state.fork_sockets[1];
482 g_assert(gdbserver_state.process_num == 1);
483 g_assert(gdbserver_state.processes[0].pid ==
484 gdbserver_user_state.fork_peer_pid);
485 g_assert(gdbserver_state.processes[0].attached);
486 gdbserver_state.processes[0].pid = getpid();
487 } else {
488 close(gdbserver_user_state.fork_sockets[1]);
489 fd = gdbserver_user_state.fork_sockets[0];
490 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
491 gdbserver_user_state.fork_peer_pid = pid;
492 gdbserver_user_state.fork_peer_tid = pid;
493
494 if (!gdbserver_state.allow_stop_reply) {
495 goto fail;
496 }
497 g_string_printf(gdbserver_state.str_buf,
498 "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
499 gdb_target_signal_to_gdb(gdb_target_sigtrap()),
500 pid, pid, (int)getpid(), qemu_get_thread_id());
501 gdb_put_strbuf();
502 }
503
504 gdbserver_state.state = RS_IDLE;
505 gdbserver_state.allow_stop_reply = false;
506 gdbserver_user_state.running_state = 0;
507 for (;;) {
508 switch (gdbserver_user_state.fork_state) {
509 case GDB_FORK_ENABLED:
510 if (gdbserver_user_state.running_state) {
Ilya Leoshkevich69719982024-03-12 01:07:01 +0100511 close(fd);
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000512 return;
513 }
514 QEMU_FALLTHROUGH;
515 case GDB_FORK_ACTIVE:
516 if (read(gdbserver_user_state.fd, &b, 1) != 1) {
517 goto fail;
518 }
519 gdb_read_byte(b);
520 break;
521 case GDB_FORK_DEACTIVATING:
522 b = GDB_FORK_ACTIVATE;
523 if (write(fd, &b, 1) != 1) {
524 goto fail;
525 }
526 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
527 break;
528 case GDB_FORK_INACTIVE:
529 if (read(fd, &b, 1) != 1) {
530 goto fail;
531 }
532 switch (b) {
533 case GDB_FORK_ACTIVATE:
534 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
535 break;
536 case GDB_FORK_ENABLE:
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000537 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
538 break;
539 case GDB_FORK_DISABLE:
540 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
541 break;
542 default:
543 g_assert_not_reached();
544 }
545 break;
546 case GDB_FORK_ENABLING:
547 b = GDB_FORK_DISABLE;
548 if (write(fd, &b, 1) != 1) {
549 goto fail;
550 }
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000551 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
552 break;
553 case GDB_FORK_DISABLING:
554 b = GDB_FORK_ENABLE;
555 if (write(fd, &b, 1) != 1) {
556 goto fail;
557 }
558 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
559 break;
560 case GDB_FORK_DISABLED:
561 close(fd);
562 disable_gdbstub(cpu);
563 return;
564 default:
565 g_assert_not_reached();
566 }
567 }
568
569fail:
570 close(fd);
571 if (pid == 0) {
572 disable_gdbstub(cpu);
573 }
Alex Bennéed96bf492023-03-02 18:57:47 -0800574}
575
Ilya Leoshkevich6d923112024-03-05 12:09:45 +0000576void gdb_handle_query_supported_user(const char *gdb_supported)
577{
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000578 if (strstr(gdb_supported, "fork-events+")) {
579 gdbserver_user_state.fork_events = true;
580 }
581 g_string_append(gdbserver_state.str_buf, ";fork-events+");
Ilya Leoshkevich6d923112024-03-05 12:09:45 +0000582}
583
Ilya Leoshkeviche454f2f2024-03-05 12:09:46 +0000584bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
585{
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000586 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
587 pid == gdbserver_user_state.fork_peer_pid &&
588 tid == gdbserver_user_state.fork_peer_tid) {
589 gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
590 gdb_put_packet("OK");
591 return true;
592 }
Ilya Leoshkeviche454f2f2024-03-05 12:09:46 +0000593 return false;
594}
595
Ilya Leoshkevich539cb4e2024-03-05 12:09:47 +0000596bool gdb_handle_detach_user(uint32_t pid)
597{
Ilya Leoshkevichd547e712024-03-05 12:09:48 +0000598 bool enable;
599
600 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
601 enable = pid == gdbserver_user_state.fork_peer_pid;
602 if (enable || pid == getpid()) {
603 gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
604 GDB_FORK_DISABLING;
605 gdb_put_packet("OK");
606 return true;
607 }
608 }
Ilya Leoshkevich539cb4e2024-03-05 12:09:47 +0000609 return false;
610}
611
Alex Bennéed96bf492023-03-02 18:57:47 -0800612/*
613 * Execution state helpers
614 */
615
Alex Bennée8a2025b2023-03-02 18:57:50 -0800616void gdb_handle_query_attached(GArray *params, void *user_ctx)
617{
618 gdb_put_packet("0");
619}
620
Alex Bennéed96bf492023-03-02 18:57:47 -0800621void gdb_continue(void)
622{
623 gdbserver_user_state.running_state = 1;
624 trace_gdbstub_op_continue();
625}
626
627/*
628 * Resume execution, for user-mode emulation it's equivalent to
629 * gdb_continue.
630 */
631int gdb_continue_partial(char *newstates)
632{
633 CPUState *cpu;
634 int res = 0;
635 /*
636 * This is not exactly accurate, but it's an improvement compared to the
637 * previous situation, where only one CPU would be single-stepped.
638 */
639 CPU_FOREACH(cpu) {
640 if (newstates[cpu->cpu_index] == 's') {
641 trace_gdbstub_op_stepping(cpu->cpu_index);
642 cpu_single_step(cpu, gdbserver_state.sstep_flags);
643 }
644 }
645 gdbserver_user_state.running_state = 1;
646 return res;
647}
648
649/*
Alex Bennée589a5862023-03-02 18:57:51 -0800650 * Memory access helpers
651 */
652int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
653 uint8_t *buf, int len, bool is_write)
654{
655 CPUClass *cc;
656
657 cc = CPU_GET_CLASS(cpu);
658 if (cc->memory_rw_debug) {
659 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
660 }
661 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
662}
663
664/*
Alex Bennée7ea0c332023-03-02 18:57:52 -0800665 * cpu helpers
666 */
667
668unsigned int gdb_get_max_cpus(void)
669{
670 CPUState *cpu;
671 unsigned int max_cpus = 1;
672
673 CPU_FOREACH(cpu) {
674 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
675 }
676
677 return max_cpus;
678}
679
Alex Bennée505601d2023-03-02 18:57:53 -0800680/* replay not supported for user-mode */
681bool gdb_can_reverse(void)
682{
683 return false;
684}
Alex Bennée7ea0c332023-03-02 18:57:52 -0800685
686/*
Alex Bennéed96bf492023-03-02 18:57:47 -0800687 * Break/Watch point helpers
688 */
689
Alex Bennéea48e7d92022-09-29 12:42:25 +0100690bool gdb_supports_guest_debug(void)
691{
692 /* user-mode == TCG == supported */
693 return true;
694}
695
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +0100696int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
Alex Bennéeae7467b2022-09-29 12:42:24 +0100697{
698 CPUState *cpu;
699 int err = 0;
700
701 switch (type) {
702 case GDB_BREAKPOINT_SW:
703 case GDB_BREAKPOINT_HW:
704 CPU_FOREACH(cpu) {
705 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
706 if (err) {
707 break;
708 }
709 }
710 return err;
711 default:
712 /* user-mode doesn't support watchpoints */
713 return -ENOSYS;
714 }
715}
716
Philippe Mathieu-Daudé55b5b8e2022-12-06 16:20:27 +0100717int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
Alex Bennéeae7467b2022-09-29 12:42:24 +0100718{
719 CPUState *cpu;
720 int err = 0;
721
722 switch (type) {
723 case GDB_BREAKPOINT_SW:
724 case GDB_BREAKPOINT_HW:
725 CPU_FOREACH(cpu) {
726 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
727 if (err) {
728 break;
729 }
730 }
731 return err;
732 default:
733 /* user-mode doesn't support watchpoints */
734 return -ENOSYS;
735 }
736}
737
738void gdb_breakpoint_remove_all(CPUState *cs)
739{
740 cpu_breakpoint_remove_all(cs, BP_GDB);
741}
Alex Bennée131f3872023-03-02 18:58:01 -0800742
743/*
744 * For user-mode syscall support we send the system call immediately
745 * and then return control to gdb for it to process the syscall request.
746 * Since the protocol requires that gdb hands control back to us
747 * using a "here are the results" F packet, we don't need to check
748 * gdb_handlesig's return value (which is the signal to deliver if
749 * execution was resumed via a continue packet).
750 */
751void gdb_syscall_handling(const char *syscall_packet)
752{
753 gdb_put_packet(syscall_packet);
Gustavo Romerof84e3132024-03-09 03:08:59 +0000754 gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0);
Alex Bennée131f3872023-03-02 18:58:01 -0800755}
Ilya Leoshkevich0a0d87c2024-02-07 16:38:10 +0000756
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000757static bool should_catch_syscall(int num)
758{
759 if (gdbserver_user_state.catch_all_syscalls) {
760 return true;
761 }
762 if (num < 0 || num >= GDB_NR_SYSCALLS) {
763 return false;
764 }
765 return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
766}
767
Ilya Leoshkevich0a0d87c2024-02-07 16:38:10 +0000768void gdb_syscall_entry(CPUState *cs, int num)
769{
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000770 if (should_catch_syscall(num)) {
771 g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
Gustavo Romerof84e3132024-03-09 03:08:59 +0000772 gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000773 }
Ilya Leoshkevich0a0d87c2024-02-07 16:38:10 +0000774}
775
776void gdb_syscall_return(CPUState *cs, int num)
777{
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000778 if (should_catch_syscall(num)) {
779 g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
Gustavo Romerof84e3132024-03-09 03:08:59 +0000780 gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000781 }
782}
783
784void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
785{
Gustavo Romero133f2022024-07-05 09:40:38 +0100786 const char *param = gdb_get_cmd_param(params, 0)->data;
Ilya Leoshkevich046f1432024-02-07 16:38:11 +0000787 GDBSyscallsMask catch_syscalls_mask;
788 bool catch_all_syscalls;
789 unsigned int num;
790 const char *p;
791
792 /* "0" means not catching any syscalls. */
793 if (strcmp(param, "0") == 0) {
794 gdbserver_user_state.catch_all_syscalls = false;
795 memset(gdbserver_user_state.catch_syscalls_mask, 0,
796 sizeof(gdbserver_user_state.catch_syscalls_mask));
797 gdb_put_packet("OK");
798 return;
799 }
800
801 /* "1" means catching all syscalls. */
802 if (strcmp(param, "1") == 0) {
803 gdbserver_user_state.catch_all_syscalls = true;
804 gdb_put_packet("OK");
805 return;
806 }
807
808 /*
809 * "1;..." means catching only the specified syscalls.
810 * The syscall list must not be empty.
811 */
812 if (param[0] == '1' && param[1] == ';') {
813 catch_all_syscalls = false;
814 memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
815 for (p = &param[2];; p++) {
816 if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
817 goto err;
818 }
819 if (num >= GDB_NR_SYSCALLS) {
820 /*
821 * Fall back to reporting all syscalls. Reporting extra
822 * syscalls is inefficient, but the spec explicitly allows it.
823 * Keep parsing in case there is a syntax error ahead.
824 */
825 catch_all_syscalls = true;
826 } else {
827 set_bit(num, catch_syscalls_mask);
828 }
829 if (!*p) {
830 break;
831 }
832 }
833 gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
834 if (!catch_all_syscalls) {
835 memcpy(gdbserver_user_state.catch_syscalls_mask,
836 catch_syscalls_mask, sizeof(catch_syscalls_mask));
837 }
838 gdb_put_packet("OK");
839 return;
840 }
841
842err:
843 gdb_put_packet("E00");
Ilya Leoshkevich0a0d87c2024-02-07 16:38:10 +0000844}
Gustavo Romero9ae58012024-03-09 03:09:00 +0000845
846void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
847{
848 unsigned long offset, len;
849 uint8_t *siginfo_offset;
850
Gustavo Romero133f2022024-07-05 09:40:38 +0100851 offset = gdb_get_cmd_param(params, 0)->val_ul;
852 len = gdb_get_cmd_param(params, 1)->val_ul;
Gustavo Romero9ae58012024-03-09 03:09:00 +0000853
854 if (offset + len > gdbserver_user_state.siginfo_len) {
855 /* Invalid offset and/or requested length. */
856 gdb_put_packet("E01");
857 return;
858 }
859
860 siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset;
861
862 /* Reply */
863 g_string_assign(gdbserver_state.str_buf, "l");
864 gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
865 gdb_put_packet_binary(gdbserver_state.str_buf->str,
866 gdbserver_state.str_buf->len, true);
867}