target/rx: Prefer fast cpu_env() over slower CPU QOM cast macro
[qemu/kevin.git] / gdbstub / user.c
blob7f9f19a1249908d4b8a359afb14aeaa2a0f929ea
1 /*
2 * gdbstub user-mode helper routines.
4 * We know for user-mode we are using TCG so we can call stuff directly.
6 * Copyright (c) 2003-2005 Fabrice Bellard
7 * Copyright (c) 2022 Linaro Ltd
9 * SPDX-License-Identifier: LGPL-2.0+
12 #include "qemu/osdep.h"
13 #include "qemu/bitops.h"
14 #include "qemu/cutils.h"
15 #include "qemu/sockets.h"
16 #include "exec/hwaddr.h"
17 #include "exec/tb-flush.h"
18 #include "exec/gdbstub.h"
19 #include "gdbstub/syscalls.h"
20 #include "gdbstub/user.h"
21 #include "hw/core/cpu.h"
22 #include "trace.h"
23 #include "internals.h"
25 #define GDB_NR_SYSCALLS 1024
26 typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)];
29 * Forked child talks to its parent in order to let GDB enforce the
30 * follow-fork-mode. This happens inside a start_exclusive() section, so that
31 * the other threads, which may be forking too, do not interfere. The
32 * implementation relies on GDB not sending $vCont until it has detached
33 * either from the parent (follow-fork-mode child) or from the child
34 * (follow-fork-mode parent).
36 * The parent and the child share the GDB socket; at any given time only one
37 * of them is allowed to use it, as is reflected in the respective fork_state.
38 * This is negotiated via the fork_sockets pair as a reaction to $Hg.
40 * Below is a short summary of the possible state transitions:
42 * ENABLED : Terminal state.
43 * DISABLED : Terminal state.
44 * ACTIVE : Parent initial state.
45 * INACTIVE : Child initial state.
46 * ACTIVE -> DEACTIVATING: On $Hg.
47 * ACTIVE -> ENABLING : On $D.
48 * ACTIVE -> DISABLING : On $D.
49 * ACTIVE -> DISABLED : On communication error.
50 * DEACTIVATING -> INACTIVE : On gdb_read_byte() return.
51 * DEACTIVATING -> DISABLED : On communication error.
52 * INACTIVE -> ACTIVE : On $Hg in the peer.
53 * INACTIVE -> ENABLE : On $D in the peer.
54 * INACTIVE -> DISABLE : On $D in the peer.
55 * INACTIVE -> DISABLED : On communication error.
56 * ENABLING -> ENABLED : On gdb_read_byte() return.
57 * ENABLING -> DISABLED : On communication error.
58 * DISABLING -> DISABLED : On gdb_read_byte() return.
60 enum GDBForkState {
61 /* Fully owning the GDB socket. */
62 GDB_FORK_ENABLED,
63 /* Working with the GDB socket; the peer is inactive. */
64 GDB_FORK_ACTIVE,
65 /* Handing off the GDB socket to the peer. */
66 GDB_FORK_DEACTIVATING,
67 /* The peer is working with the GDB socket. */
68 GDB_FORK_INACTIVE,
69 /* Asking the peer to close its GDB socket fd. */
70 GDB_FORK_ENABLING,
71 /* Asking the peer to take over, closing our GDB socket fd. */
72 GDB_FORK_DISABLING,
73 /* The peer has taken over, our GDB socket fd is closed. */
74 GDB_FORK_DISABLED,
77 enum GDBForkMessage {
78 GDB_FORK_ACTIVATE = 'a',
79 GDB_FORK_ENABLE = 'e',
80 GDB_FORK_DISABLE = 'd',
83 /* User-mode specific state */
84 typedef struct {
85 int fd;
86 char *socket_path;
87 int running_state;
89 * Store syscalls mask without memory allocation in order to avoid
90 * implementing synchronization.
92 bool catch_all_syscalls;
93 GDBSyscallsMask catch_syscalls_mask;
94 bool fork_events;
95 enum GDBForkState fork_state;
96 int fork_sockets[2];
97 pid_t fork_peer_pid, fork_peer_tid;
98 } GDBUserState;
100 static GDBUserState gdbserver_user_state;
102 int gdb_get_char(void)
104 uint8_t ch;
105 int ret;
107 for (;;) {
108 ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
109 if (ret < 0) {
110 if (errno == ECONNRESET) {
111 gdbserver_user_state.fd = -1;
113 if (errno != EINTR) {
114 return -1;
116 } else if (ret == 0) {
117 close(gdbserver_user_state.fd);
118 gdbserver_user_state.fd = -1;
119 return -1;
120 } else {
121 break;
124 return ch;
127 bool gdb_got_immediate_ack(void)
129 int i;
131 i = gdb_get_char();
132 if (i < 0) {
133 /* no response, continue anyway */
134 return true;
137 if (i == '+') {
138 /* received correctly, continue */
139 return true;
142 /* anything else, including '-' then try again */
143 return false;
146 void gdb_put_buffer(const uint8_t *buf, int len)
148 int ret;
150 while (len > 0) {
151 ret = send(gdbserver_user_state.fd, buf, len, 0);
152 if (ret < 0) {
153 if (errno != EINTR) {
154 return;
156 } else {
157 buf += ret;
158 len -= ret;
163 /* Tell the remote gdb that the process has exited. */
164 void gdb_exit(int code)
166 char buf[4];
168 if (!gdbserver_state.init) {
169 return;
171 if (gdbserver_user_state.socket_path) {
172 unlink(gdbserver_user_state.socket_path);
174 if (gdbserver_user_state.fd < 0) {
175 return;
178 trace_gdbstub_op_exiting((uint8_t)code);
180 if (gdbserver_state.allow_stop_reply) {
181 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
182 gdb_put_packet(buf);
183 gdbserver_state.allow_stop_reply = false;
188 void gdb_qemu_exit(int code)
190 exit(code);
193 int gdb_handlesig_reason(CPUState *cpu, int sig, const char *reason)
195 char buf[256];
196 int n;
198 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
199 return sig;
202 /* disable single step if it was enabled */
203 cpu_single_step(cpu, 0);
204 tb_flush(cpu);
206 if (sig != 0) {
207 gdb_set_stop_cpu(cpu);
208 if (gdbserver_state.allow_stop_reply) {
209 g_string_printf(gdbserver_state.str_buf,
210 "T%02xthread:", gdb_target_signal_to_gdb(sig));
211 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
212 g_string_append_c(gdbserver_state.str_buf, ';');
213 if (reason) {
214 g_string_append(gdbserver_state.str_buf, reason);
216 gdb_put_strbuf();
217 gdbserver_state.allow_stop_reply = false;
221 * gdb_put_packet() might have detected that the peer terminated the
222 * connection.
224 if (gdbserver_user_state.fd < 0) {
225 return sig;
228 sig = 0;
229 gdbserver_state.state = RS_IDLE;
230 gdbserver_user_state.running_state = 0;
231 while (gdbserver_user_state.running_state == 0) {
232 n = read(gdbserver_user_state.fd, buf, 256);
233 if (n > 0) {
234 int i;
236 for (i = 0; i < n; i++) {
237 gdb_read_byte(buf[i]);
239 } else {
241 * XXX: Connection closed. Should probably wait for another
242 * connection before continuing.
244 if (n == 0) {
245 close(gdbserver_user_state.fd);
247 gdbserver_user_state.fd = -1;
248 return sig;
251 sig = gdbserver_state.signal;
252 gdbserver_state.signal = 0;
253 return sig;
256 /* Tell the remote gdb that the process has exited due to SIG. */
257 void gdb_signalled(CPUArchState *env, int sig)
259 char buf[4];
261 if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
262 !gdbserver_state.allow_stop_reply) {
263 return;
266 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
267 gdb_put_packet(buf);
268 gdbserver_state.allow_stop_reply = false;
271 static void gdb_accept_init(int fd)
273 gdb_init_gdbserver_state();
274 gdb_create_default_process(&gdbserver_state);
275 gdbserver_state.processes[0].attached = true;
276 gdbserver_state.c_cpu = gdb_first_attached_cpu();
277 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
278 gdbserver_user_state.fd = fd;
281 static bool gdb_accept_socket(int gdb_fd)
283 int fd;
285 for (;;) {
286 fd = accept(gdb_fd, NULL, NULL);
287 if (fd < 0 && errno != EINTR) {
288 perror("accept socket");
289 return false;
290 } else if (fd >= 0) {
291 qemu_set_cloexec(fd);
292 break;
296 gdb_accept_init(fd);
297 return true;
300 static int gdbserver_open_socket(const char *path)
302 struct sockaddr_un sockaddr = {};
303 int fd, ret;
305 fd = socket(AF_UNIX, SOCK_STREAM, 0);
306 if (fd < 0) {
307 perror("create socket");
308 return -1;
311 sockaddr.sun_family = AF_UNIX;
312 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
313 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
314 if (ret < 0) {
315 perror("bind socket");
316 close(fd);
317 return -1;
319 ret = listen(fd, 1);
320 if (ret < 0) {
321 perror("listen socket");
322 close(fd);
323 return -1;
326 return fd;
329 static bool gdb_accept_tcp(int gdb_fd)
331 struct sockaddr_in sockaddr = {};
332 socklen_t len;
333 int fd;
335 for (;;) {
336 len = sizeof(sockaddr);
337 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
338 if (fd < 0 && errno != EINTR) {
339 perror("accept");
340 return false;
341 } else if (fd >= 0) {
342 qemu_set_cloexec(fd);
343 break;
347 /* set short latency */
348 if (socket_set_nodelay(fd)) {
349 perror("setsockopt");
350 close(fd);
351 return false;
354 gdb_accept_init(fd);
355 return true;
358 static int gdbserver_open_port(int port)
360 struct sockaddr_in sockaddr;
361 int fd, ret;
363 fd = socket(PF_INET, SOCK_STREAM, 0);
364 if (fd < 0) {
365 perror("socket");
366 return -1;
368 qemu_set_cloexec(fd);
370 socket_set_fast_reuse(fd);
372 sockaddr.sin_family = AF_INET;
373 sockaddr.sin_port = htons(port);
374 sockaddr.sin_addr.s_addr = 0;
375 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
376 if (ret < 0) {
377 perror("bind");
378 close(fd);
379 return -1;
381 ret = listen(fd, 1);
382 if (ret < 0) {
383 perror("listen");
384 close(fd);
385 return -1;
388 return fd;
391 int gdbserver_start(const char *port_or_path)
393 int port = g_ascii_strtoull(port_or_path, NULL, 10);
394 int gdb_fd;
396 if (port > 0) {
397 gdb_fd = gdbserver_open_port(port);
398 } else {
399 gdb_fd = gdbserver_open_socket(port_or_path);
402 if (gdb_fd < 0) {
403 return -1;
406 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
407 return 0;
408 } else if (gdb_accept_socket(gdb_fd)) {
409 gdbserver_user_state.socket_path = g_strdup(port_or_path);
410 return 0;
413 /* gone wrong */
414 close(gdb_fd);
415 return -1;
418 void gdbserver_fork_start(void)
420 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
421 return;
423 if (!gdbserver_user_state.fork_events ||
424 qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
425 gdbserver_user_state.fork_sockets) < 0) {
426 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
427 return;
429 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
430 gdbserver_user_state.fork_peer_pid = getpid();
431 gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
434 static void disable_gdbstub(CPUState *thread_cpu)
436 CPUState *cpu;
438 close(gdbserver_user_state.fd);
439 gdbserver_user_state.fd = -1;
440 CPU_FOREACH(cpu) {
441 cpu_breakpoint_remove_all(cpu, BP_GDB);
442 /* no cpu_watchpoint_remove_all for user-mode */
443 cpu_single_step(cpu, 0);
445 tb_flush(thread_cpu);
448 void gdbserver_fork_end(CPUState *cpu, pid_t pid)
450 char b;
451 int fd;
453 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
454 return;
457 if (pid == -1) {
458 if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
459 g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
460 close(gdbserver_user_state.fork_sockets[0]);
461 close(gdbserver_user_state.fork_sockets[1]);
463 return;
466 if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
467 if (pid == 0) {
468 disable_gdbstub(cpu);
470 return;
473 if (pid == 0) {
474 close(gdbserver_user_state.fork_sockets[0]);
475 fd = gdbserver_user_state.fork_sockets[1];
476 g_assert(gdbserver_state.process_num == 1);
477 g_assert(gdbserver_state.processes[0].pid ==
478 gdbserver_user_state.fork_peer_pid);
479 g_assert(gdbserver_state.processes[0].attached);
480 gdbserver_state.processes[0].pid = getpid();
481 } else {
482 close(gdbserver_user_state.fork_sockets[1]);
483 fd = gdbserver_user_state.fork_sockets[0];
484 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
485 gdbserver_user_state.fork_peer_pid = pid;
486 gdbserver_user_state.fork_peer_tid = pid;
488 if (!gdbserver_state.allow_stop_reply) {
489 goto fail;
491 g_string_printf(gdbserver_state.str_buf,
492 "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
493 gdb_target_signal_to_gdb(gdb_target_sigtrap()),
494 pid, pid, (int)getpid(), qemu_get_thread_id());
495 gdb_put_strbuf();
498 gdbserver_state.state = RS_IDLE;
499 gdbserver_state.allow_stop_reply = false;
500 gdbserver_user_state.running_state = 0;
501 for (;;) {
502 switch (gdbserver_user_state.fork_state) {
503 case GDB_FORK_ENABLED:
504 if (gdbserver_user_state.running_state) {
505 return;
507 QEMU_FALLTHROUGH;
508 case GDB_FORK_ACTIVE:
509 if (read(gdbserver_user_state.fd, &b, 1) != 1) {
510 goto fail;
512 gdb_read_byte(b);
513 break;
514 case GDB_FORK_DEACTIVATING:
515 b = GDB_FORK_ACTIVATE;
516 if (write(fd, &b, 1) != 1) {
517 goto fail;
519 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
520 break;
521 case GDB_FORK_INACTIVE:
522 if (read(fd, &b, 1) != 1) {
523 goto fail;
525 switch (b) {
526 case GDB_FORK_ACTIVATE:
527 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
528 break;
529 case GDB_FORK_ENABLE:
530 close(fd);
531 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
532 break;
533 case GDB_FORK_DISABLE:
534 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
535 break;
536 default:
537 g_assert_not_reached();
539 break;
540 case GDB_FORK_ENABLING:
541 b = GDB_FORK_DISABLE;
542 if (write(fd, &b, 1) != 1) {
543 goto fail;
545 close(fd);
546 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
547 break;
548 case GDB_FORK_DISABLING:
549 b = GDB_FORK_ENABLE;
550 if (write(fd, &b, 1) != 1) {
551 goto fail;
553 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
554 break;
555 case GDB_FORK_DISABLED:
556 close(fd);
557 disable_gdbstub(cpu);
558 return;
559 default:
560 g_assert_not_reached();
564 fail:
565 close(fd);
566 if (pid == 0) {
567 disable_gdbstub(cpu);
571 void gdb_handle_query_supported_user(const char *gdb_supported)
573 if (strstr(gdb_supported, "fork-events+")) {
574 gdbserver_user_state.fork_events = true;
576 g_string_append(gdbserver_state.str_buf, ";fork-events+");
579 bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
581 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
582 pid == gdbserver_user_state.fork_peer_pid &&
583 tid == gdbserver_user_state.fork_peer_tid) {
584 gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
585 gdb_put_packet("OK");
586 return true;
588 return false;
591 bool gdb_handle_detach_user(uint32_t pid)
593 bool enable;
595 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
596 enable = pid == gdbserver_user_state.fork_peer_pid;
597 if (enable || pid == getpid()) {
598 gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
599 GDB_FORK_DISABLING;
600 gdb_put_packet("OK");
601 return true;
604 return false;
608 * Execution state helpers
611 void gdb_handle_query_attached(GArray *params, void *user_ctx)
613 gdb_put_packet("0");
616 void gdb_continue(void)
618 gdbserver_user_state.running_state = 1;
619 trace_gdbstub_op_continue();
623 * Resume execution, for user-mode emulation it's equivalent to
624 * gdb_continue.
626 int gdb_continue_partial(char *newstates)
628 CPUState *cpu;
629 int res = 0;
631 * This is not exactly accurate, but it's an improvement compared to the
632 * previous situation, where only one CPU would be single-stepped.
634 CPU_FOREACH(cpu) {
635 if (newstates[cpu->cpu_index] == 's') {
636 trace_gdbstub_op_stepping(cpu->cpu_index);
637 cpu_single_step(cpu, gdbserver_state.sstep_flags);
640 gdbserver_user_state.running_state = 1;
641 return res;
645 * Memory access helpers
647 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
648 uint8_t *buf, int len, bool is_write)
650 CPUClass *cc;
652 cc = CPU_GET_CLASS(cpu);
653 if (cc->memory_rw_debug) {
654 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
656 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
660 * cpu helpers
663 unsigned int gdb_get_max_cpus(void)
665 CPUState *cpu;
666 unsigned int max_cpus = 1;
668 CPU_FOREACH(cpu) {
669 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
672 return max_cpus;
675 /* replay not supported for user-mode */
676 bool gdb_can_reverse(void)
678 return false;
682 * Break/Watch point helpers
685 bool gdb_supports_guest_debug(void)
687 /* user-mode == TCG == supported */
688 return true;
691 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
693 CPUState *cpu;
694 int err = 0;
696 switch (type) {
697 case GDB_BREAKPOINT_SW:
698 case GDB_BREAKPOINT_HW:
699 CPU_FOREACH(cpu) {
700 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
701 if (err) {
702 break;
705 return err;
706 default:
707 /* user-mode doesn't support watchpoints */
708 return -ENOSYS;
712 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
714 CPUState *cpu;
715 int err = 0;
717 switch (type) {
718 case GDB_BREAKPOINT_SW:
719 case GDB_BREAKPOINT_HW:
720 CPU_FOREACH(cpu) {
721 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
722 if (err) {
723 break;
726 return err;
727 default:
728 /* user-mode doesn't support watchpoints */
729 return -ENOSYS;
733 void gdb_breakpoint_remove_all(CPUState *cs)
735 cpu_breakpoint_remove_all(cs, BP_GDB);
739 * For user-mode syscall support we send the system call immediately
740 * and then return control to gdb for it to process the syscall request.
741 * Since the protocol requires that gdb hands control back to us
742 * using a "here are the results" F packet, we don't need to check
743 * gdb_handlesig's return value (which is the signal to deliver if
744 * execution was resumed via a continue packet).
746 void gdb_syscall_handling(const char *syscall_packet)
748 gdb_put_packet(syscall_packet);
749 gdb_handlesig(gdbserver_state.c_cpu, 0);
752 static bool should_catch_syscall(int num)
754 if (gdbserver_user_state.catch_all_syscalls) {
755 return true;
757 if (num < 0 || num >= GDB_NR_SYSCALLS) {
758 return false;
760 return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
763 void gdb_syscall_entry(CPUState *cs, int num)
765 if (should_catch_syscall(num)) {
766 g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
767 gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason);
771 void gdb_syscall_return(CPUState *cs, int num)
773 if (should_catch_syscall(num)) {
774 g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
775 gdb_handlesig_reason(cs, gdb_target_sigtrap(), reason);
779 void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
781 const char *param = get_param(params, 0)->data;
782 GDBSyscallsMask catch_syscalls_mask;
783 bool catch_all_syscalls;
784 unsigned int num;
785 const char *p;
787 /* "0" means not catching any syscalls. */
788 if (strcmp(param, "0") == 0) {
789 gdbserver_user_state.catch_all_syscalls = false;
790 memset(gdbserver_user_state.catch_syscalls_mask, 0,
791 sizeof(gdbserver_user_state.catch_syscalls_mask));
792 gdb_put_packet("OK");
793 return;
796 /* "1" means catching all syscalls. */
797 if (strcmp(param, "1") == 0) {
798 gdbserver_user_state.catch_all_syscalls = true;
799 gdb_put_packet("OK");
800 return;
804 * "1;..." means catching only the specified syscalls.
805 * The syscall list must not be empty.
807 if (param[0] == '1' && param[1] == ';') {
808 catch_all_syscalls = false;
809 memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
810 for (p = &param[2];; p++) {
811 if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
812 goto err;
814 if (num >= GDB_NR_SYSCALLS) {
816 * Fall back to reporting all syscalls. Reporting extra
817 * syscalls is inefficient, but the spec explicitly allows it.
818 * Keep parsing in case there is a syntax error ahead.
820 catch_all_syscalls = true;
821 } else {
822 set_bit(num, catch_syscalls_mask);
824 if (!*p) {
825 break;
828 gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
829 if (!catch_all_syscalls) {
830 memcpy(gdbserver_user_state.catch_syscalls_mask,
831 catch_syscalls_mask, sizeof(catch_syscalls_mask));
833 gdb_put_packet("OK");
834 return;
837 err:
838 gdb_put_packet("E00");