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 "gdbstub/enums.h"
22 #include "hw/core/cpu.h"
24 #include "internals.h"
26 #define GDB_NR_SYSCALLS 1024
27 typedef unsigned long GDBSyscallsMask
[BITS_TO_LONGS(GDB_NR_SYSCALLS
)];
30 * Forked child talks to its parent in order to let GDB enforce the
31 * follow-fork-mode. This happens inside a start_exclusive() section, so that
32 * the other threads, which may be forking too, do not interfere. The
33 * implementation relies on GDB not sending $vCont until it has detached
34 * either from the parent (follow-fork-mode child) or from the child
35 * (follow-fork-mode parent).
37 * The parent and the child share the GDB socket; at any given time only one
38 * of them is allowed to use it, as is reflected in the respective fork_state.
39 * This is negotiated via the fork_sockets pair as a reaction to $Hg.
41 * Below is a short summary of the possible state transitions:
43 * ENABLED : Terminal state.
44 * DISABLED : Terminal state.
45 * ACTIVE : Parent initial state.
46 * INACTIVE : Child initial state.
47 * ACTIVE -> DEACTIVATING: On $Hg.
48 * ACTIVE -> ENABLING : On $D.
49 * ACTIVE -> DISABLING : On $D.
50 * ACTIVE -> DISABLED : On communication error.
51 * DEACTIVATING -> INACTIVE : On gdb_read_byte() return.
52 * DEACTIVATING -> DISABLED : On communication error.
53 * INACTIVE -> ACTIVE : On $Hg in the peer.
54 * INACTIVE -> ENABLE : On $D in the peer.
55 * INACTIVE -> DISABLE : On $D in the peer.
56 * INACTIVE -> DISABLED : On communication error.
57 * ENABLING -> ENABLED : On gdb_read_byte() return.
58 * ENABLING -> DISABLED : On communication error.
59 * DISABLING -> DISABLED : On gdb_read_byte() return.
62 /* Fully owning the GDB socket. */
64 /* Working with the GDB socket; the peer is inactive. */
66 /* Handing off the GDB socket to the peer. */
67 GDB_FORK_DEACTIVATING
,
68 /* The peer is working with the GDB socket. */
70 /* Asking the peer to close its GDB socket fd. */
72 /* Asking the peer to take over, closing our GDB socket fd. */
74 /* The peer has taken over, our GDB socket fd is closed. */
79 GDB_FORK_ACTIVATE
= 'a',
80 GDB_FORK_ENABLE
= 'e',
81 GDB_FORK_DISABLE
= 'd',
84 /* User-mode specific state */
90 * Store syscalls mask without memory allocation in order to avoid
91 * implementing synchronization.
93 bool catch_all_syscalls
;
94 GDBSyscallsMask catch_syscalls_mask
;
96 enum GDBForkState fork_state
;
98 pid_t fork_peer_pid
, fork_peer_tid
;
99 uint8_t siginfo
[MAX_SIGINFO_LENGTH
];
100 unsigned long siginfo_len
;
103 static GDBUserState gdbserver_user_state
;
105 int gdb_get_char(void)
111 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
113 if (errno
== ECONNRESET
) {
114 gdbserver_user_state
.fd
= -1;
116 if (errno
!= EINTR
) {
119 } else if (ret
== 0) {
120 close(gdbserver_user_state
.fd
);
121 gdbserver_user_state
.fd
= -1;
130 bool gdb_got_immediate_ack(void)
136 /* no response, continue anyway */
141 /* received correctly, continue */
145 /* anything else, including '-' then try again */
149 void gdb_put_buffer(const uint8_t *buf
, int len
)
154 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
156 if (errno
!= EINTR
) {
166 /* Tell the remote gdb that the process has exited. */
167 void gdb_exit(int code
)
171 if (!gdbserver_state
.init
) {
174 if (gdbserver_user_state
.socket_path
) {
175 unlink(gdbserver_user_state
.socket_path
);
177 if (gdbserver_user_state
.fd
< 0) {
181 trace_gdbstub_op_exiting((uint8_t)code
);
183 if (gdbserver_state
.allow_stop_reply
) {
184 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
186 gdbserver_state
.allow_stop_reply
= false;
191 void gdb_qemu_exit(int code
)
196 int gdb_handlesig(CPUState
*cpu
, int sig
, const char *reason
, void *siginfo
,
202 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
208 * Save target-specific siginfo.
210 * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
211 * gdbserver_user_state.siginfo, usually in the source file calling
212 * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
214 memcpy(gdbserver_user_state
.siginfo
, siginfo
, siginfo_len
);
215 gdbserver_user_state
.siginfo_len
= siginfo_len
;
218 /* disable single step if it was enabled */
219 cpu_single_step(cpu
, 0);
223 gdb_set_stop_cpu(cpu
);
224 if (gdbserver_state
.allow_stop_reply
) {
225 g_string_printf(gdbserver_state
.str_buf
,
226 "T%02xthread:", gdb_target_signal_to_gdb(sig
));
227 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
228 g_string_append_c(gdbserver_state
.str_buf
, ';');
230 g_string_append(gdbserver_state
.str_buf
, reason
);
233 gdbserver_state
.allow_stop_reply
= false;
237 * gdb_put_packet() might have detected that the peer terminated the
240 if (gdbserver_user_state
.fd
< 0) {
245 gdbserver_state
.state
= RS_IDLE
;
246 gdbserver_user_state
.running_state
= 0;
247 while (gdbserver_user_state
.running_state
== 0) {
248 n
= read(gdbserver_user_state
.fd
, buf
, 256);
252 for (i
= 0; i
< n
; i
++) {
253 gdb_read_byte(buf
[i
]);
257 * XXX: Connection closed. Should probably wait for another
258 * connection before continuing.
261 close(gdbserver_user_state
.fd
);
263 gdbserver_user_state
.fd
= -1;
267 sig
= gdbserver_state
.signal
;
268 gdbserver_state
.signal
= 0;
272 /* Tell the remote gdb that the process has exited due to SIG. */
273 void gdb_signalled(CPUArchState
*env
, int sig
)
277 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0 ||
278 !gdbserver_state
.allow_stop_reply
) {
282 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
284 gdbserver_state
.allow_stop_reply
= false;
287 static void gdb_accept_init(int fd
)
289 gdb_init_gdbserver_state();
290 gdb_create_default_process(&gdbserver_state
);
291 gdbserver_state
.processes
[0].attached
= true;
292 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
293 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
294 gdbserver_user_state
.fd
= fd
;
297 static bool gdb_accept_socket(int gdb_fd
)
302 fd
= accept(gdb_fd
, NULL
, NULL
);
303 if (fd
< 0 && errno
!= EINTR
) {
304 perror("accept socket");
306 } else if (fd
>= 0) {
307 qemu_set_cloexec(fd
);
316 static int gdbserver_open_socket(const char *path
)
318 struct sockaddr_un sockaddr
= {};
321 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
323 perror("create socket");
327 sockaddr
.sun_family
= AF_UNIX
;
328 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
329 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
331 perror("bind socket");
337 perror("listen socket");
345 static bool gdb_accept_tcp(int gdb_fd
)
347 struct sockaddr_in sockaddr
= {};
352 len
= sizeof(sockaddr
);
353 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
354 if (fd
< 0 && errno
!= EINTR
) {
357 } else if (fd
>= 0) {
358 qemu_set_cloexec(fd
);
363 /* set short latency */
364 if (socket_set_nodelay(fd
)) {
365 perror("setsockopt");
374 static int gdbserver_open_port(int port
)
376 struct sockaddr_in sockaddr
;
379 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
384 qemu_set_cloexec(fd
);
386 socket_set_fast_reuse(fd
);
388 sockaddr
.sin_family
= AF_INET
;
389 sockaddr
.sin_port
= htons(port
);
390 sockaddr
.sin_addr
.s_addr
= 0;
391 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
407 int gdbserver_start(const char *port_or_path
)
409 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
413 gdb_fd
= gdbserver_open_port(port
);
415 gdb_fd
= gdbserver_open_socket(port_or_path
);
422 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
424 } else if (gdb_accept_socket(gdb_fd
)) {
425 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
434 void gdbserver_fork_start(void)
436 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
439 if (!gdbserver_user_state
.fork_events
||
440 qemu_socketpair(AF_UNIX
, SOCK_STREAM
, 0,
441 gdbserver_user_state
.fork_sockets
) < 0) {
442 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
445 gdbserver_user_state
.fork_state
= GDB_FORK_INACTIVE
;
446 gdbserver_user_state
.fork_peer_pid
= getpid();
447 gdbserver_user_state
.fork_peer_tid
= qemu_get_thread_id();
450 static void disable_gdbstub(CPUState
*thread_cpu
)
454 close(gdbserver_user_state
.fd
);
455 gdbserver_user_state
.fd
= -1;
457 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
458 /* no cpu_watchpoint_remove_all for user-mode */
459 cpu_single_step(cpu
, 0);
461 tb_flush(thread_cpu
);
464 void gdbserver_fork_end(CPUState
*cpu
, pid_t pid
)
469 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
474 if (gdbserver_user_state
.fork_state
!= GDB_FORK_DISABLED
) {
475 g_assert(gdbserver_user_state
.fork_state
== GDB_FORK_INACTIVE
);
476 close(gdbserver_user_state
.fork_sockets
[0]);
477 close(gdbserver_user_state
.fork_sockets
[1]);
482 if (gdbserver_user_state
.fork_state
== GDB_FORK_DISABLED
) {
484 disable_gdbstub(cpu
);
490 close(gdbserver_user_state
.fork_sockets
[0]);
491 fd
= gdbserver_user_state
.fork_sockets
[1];
492 g_assert(gdbserver_state
.process_num
== 1);
493 g_assert(gdbserver_state
.processes
[0].pid
==
494 gdbserver_user_state
.fork_peer_pid
);
495 g_assert(gdbserver_state
.processes
[0].attached
);
496 gdbserver_state
.processes
[0].pid
= getpid();
498 close(gdbserver_user_state
.fork_sockets
[1]);
499 fd
= gdbserver_user_state
.fork_sockets
[0];
500 gdbserver_user_state
.fork_state
= GDB_FORK_ACTIVE
;
501 gdbserver_user_state
.fork_peer_pid
= pid
;
502 gdbserver_user_state
.fork_peer_tid
= pid
;
504 if (!gdbserver_state
.allow_stop_reply
) {
507 g_string_printf(gdbserver_state
.str_buf
,
508 "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
509 gdb_target_signal_to_gdb(gdb_target_sigtrap()),
510 pid
, pid
, (int)getpid(), qemu_get_thread_id());
514 gdbserver_state
.state
= RS_IDLE
;
515 gdbserver_state
.allow_stop_reply
= false;
516 gdbserver_user_state
.running_state
= 0;
518 switch (gdbserver_user_state
.fork_state
) {
519 case GDB_FORK_ENABLED
:
520 if (gdbserver_user_state
.running_state
) {
525 case GDB_FORK_ACTIVE
:
526 if (read(gdbserver_user_state
.fd
, &b
, 1) != 1) {
531 case GDB_FORK_DEACTIVATING
:
532 b
= GDB_FORK_ACTIVATE
;
533 if (write(fd
, &b
, 1) != 1) {
536 gdbserver_user_state
.fork_state
= GDB_FORK_INACTIVE
;
538 case GDB_FORK_INACTIVE
:
539 if (read(fd
, &b
, 1) != 1) {
543 case GDB_FORK_ACTIVATE
:
544 gdbserver_user_state
.fork_state
= GDB_FORK_ACTIVE
;
546 case GDB_FORK_ENABLE
:
547 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
549 case GDB_FORK_DISABLE
:
550 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
553 g_assert_not_reached();
556 case GDB_FORK_ENABLING
:
557 b
= GDB_FORK_DISABLE
;
558 if (write(fd
, &b
, 1) != 1) {
561 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
563 case GDB_FORK_DISABLING
:
565 if (write(fd
, &b
, 1) != 1) {
568 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
570 case GDB_FORK_DISABLED
:
572 disable_gdbstub(cpu
);
575 g_assert_not_reached();
582 disable_gdbstub(cpu
);
586 void gdb_handle_query_supported_user(const char *gdb_supported
)
588 if (strstr(gdb_supported
, "fork-events+")) {
589 gdbserver_user_state
.fork_events
= true;
591 g_string_append(gdbserver_state
.str_buf
, ";fork-events+");
594 bool gdb_handle_set_thread_user(uint32_t pid
, uint32_t tid
)
596 if (gdbserver_user_state
.fork_state
== GDB_FORK_ACTIVE
&&
597 pid
== gdbserver_user_state
.fork_peer_pid
&&
598 tid
== gdbserver_user_state
.fork_peer_tid
) {
599 gdbserver_user_state
.fork_state
= GDB_FORK_DEACTIVATING
;
600 gdb_put_packet("OK");
606 bool gdb_handle_detach_user(uint32_t pid
)
610 if (gdbserver_user_state
.fork_state
== GDB_FORK_ACTIVE
) {
611 enable
= pid
== gdbserver_user_state
.fork_peer_pid
;
612 if (enable
|| pid
== getpid()) {
613 gdbserver_user_state
.fork_state
= enable
? GDB_FORK_ENABLING
:
615 gdb_put_packet("OK");
623 * Execution state helpers
626 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
631 void gdb_continue(void)
633 gdbserver_user_state
.running_state
= 1;
634 trace_gdbstub_op_continue();
638 * Resume execution, for user-mode emulation it's equivalent to
641 int gdb_continue_partial(char *newstates
)
646 * This is not exactly accurate, but it's an improvement compared to the
647 * previous situation, where only one CPU would be single-stepped.
650 if (newstates
[cpu
->cpu_index
] == 's') {
651 trace_gdbstub_op_stepping(cpu
->cpu_index
);
652 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
655 gdbserver_user_state
.running_state
= 1;
660 * Memory access helpers
662 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
663 uint8_t *buf
, int len
, bool is_write
)
667 cc
= CPU_GET_CLASS(cpu
);
668 if (cc
->memory_rw_debug
) {
669 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
671 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
678 unsigned int gdb_get_max_cpus(void)
681 unsigned int max_cpus
= 1;
684 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
690 /* replay not supported for user-mode */
691 bool gdb_can_reverse(void)
697 * Break/Watch point helpers
700 bool gdb_supports_guest_debug(void)
702 /* user-mode == TCG == supported */
706 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
712 case GDB_BREAKPOINT_SW
:
713 case GDB_BREAKPOINT_HW
:
715 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
722 /* user-mode doesn't support watchpoints */
727 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
733 case GDB_BREAKPOINT_SW
:
734 case GDB_BREAKPOINT_HW
:
736 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
743 /* user-mode doesn't support watchpoints */
748 void gdb_breakpoint_remove_all(CPUState
*cs
)
750 cpu_breakpoint_remove_all(cs
, BP_GDB
);
754 * For user-mode syscall support we send the system call immediately
755 * and then return control to gdb for it to process the syscall request.
756 * Since the protocol requires that gdb hands control back to us
757 * using a "here are the results" F packet, we don't need to check
758 * gdb_handlesig's return value (which is the signal to deliver if
759 * execution was resumed via a continue packet).
761 void gdb_syscall_handling(const char *syscall_packet
)
763 gdb_put_packet(syscall_packet
);
764 gdb_handlesig(gdbserver_state
.c_cpu
, 0, NULL
, NULL
, 0);
767 static bool should_catch_syscall(int num
)
769 if (gdbserver_user_state
.catch_all_syscalls
) {
772 if (num
< 0 || num
>= GDB_NR_SYSCALLS
) {
775 return test_bit(num
, gdbserver_user_state
.catch_syscalls_mask
);
778 void gdb_syscall_entry(CPUState
*cs
, int num
)
780 if (should_catch_syscall(num
)) {
781 g_autofree
char *reason
= g_strdup_printf("syscall_entry:%x;", num
);
782 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
786 void gdb_syscall_return(CPUState
*cs
, int num
)
788 if (should_catch_syscall(num
)) {
789 g_autofree
char *reason
= g_strdup_printf("syscall_return:%x;", num
);
790 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
794 void gdb_handle_set_catch_syscalls(GArray
*params
, void *user_ctx
)
796 const char *param
= get_param(params
, 0)->data
;
797 GDBSyscallsMask catch_syscalls_mask
;
798 bool catch_all_syscalls
;
802 /* "0" means not catching any syscalls. */
803 if (strcmp(param
, "0") == 0) {
804 gdbserver_user_state
.catch_all_syscalls
= false;
805 memset(gdbserver_user_state
.catch_syscalls_mask
, 0,
806 sizeof(gdbserver_user_state
.catch_syscalls_mask
));
807 gdb_put_packet("OK");
811 /* "1" means catching all syscalls. */
812 if (strcmp(param
, "1") == 0) {
813 gdbserver_user_state
.catch_all_syscalls
= true;
814 gdb_put_packet("OK");
819 * "1;..." means catching only the specified syscalls.
820 * The syscall list must not be empty.
822 if (param
[0] == '1' && param
[1] == ';') {
823 catch_all_syscalls
= false;
824 memset(catch_syscalls_mask
, 0, sizeof(catch_syscalls_mask
));
825 for (p
= ¶m
[2];; p
++) {
826 if (qemu_strtoui(p
, &p
, 16, &num
) || (*p
&& *p
!= ';')) {
829 if (num
>= GDB_NR_SYSCALLS
) {
831 * Fall back to reporting all syscalls. Reporting extra
832 * syscalls is inefficient, but the spec explicitly allows it.
833 * Keep parsing in case there is a syntax error ahead.
835 catch_all_syscalls
= true;
837 set_bit(num
, catch_syscalls_mask
);
843 gdbserver_user_state
.catch_all_syscalls
= catch_all_syscalls
;
844 if (!catch_all_syscalls
) {
845 memcpy(gdbserver_user_state
.catch_syscalls_mask
,
846 catch_syscalls_mask
, sizeof(catch_syscalls_mask
));
848 gdb_put_packet("OK");
853 gdb_put_packet("E00");
856 void gdb_handle_query_xfer_siginfo(GArray
*params
, void *user_ctx
)
858 unsigned long offset
, len
;
859 uint8_t *siginfo_offset
;
861 offset
= get_param(params
, 0)->val_ul
;
862 len
= get_param(params
, 1)->val_ul
;
864 if (offset
+ len
> gdbserver_user_state
.siginfo_len
) {
865 /* Invalid offset and/or requested length. */
866 gdb_put_packet("E01");
870 siginfo_offset
= (uint8_t *)gdbserver_user_state
.siginfo
+ offset
;
873 g_string_assign(gdbserver_state
.str_buf
, "l");
874 gdb_memtox(gdbserver_state
.str_buf
, (const char *)siginfo_offset
, len
);
875 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
876 gdbserver_state
.str_buf
->len
, true);