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-or-later
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/commands.h"
20 #include "gdbstub/syscalls.h"
21 #include "gdbstub/user.h"
22 #include "gdbstub/enums.h"
23 #include "hw/core/cpu.h"
25 #include "internals.h"
27 #define GDB_NR_SYSCALLS 1024
28 typedef unsigned long GDBSyscallsMask
[BITS_TO_LONGS(GDB_NR_SYSCALLS
)];
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).
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.
42 * Below is a short summary of the possible state transitions:
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.
63 /* Fully owning the GDB socket. */
65 /* Working with the GDB socket; the peer is inactive. */
67 /* Handing off the GDB socket to the peer. */
68 GDB_FORK_DEACTIVATING
,
69 /* The peer is working with the GDB socket. */
71 /* Asking the peer to close its GDB socket fd. */
73 /* Asking the peer to take over, closing our GDB socket fd. */
75 /* The peer has taken over, our GDB socket fd is closed. */
80 GDB_FORK_ACTIVATE
= 'a',
81 GDB_FORK_ENABLE
= 'e',
82 GDB_FORK_DISABLE
= 'd',
85 /* User-mode specific state */
91 * Store syscalls mask without memory allocation in order to avoid
92 * implementing synchronization.
94 bool catch_all_syscalls
;
95 GDBSyscallsMask catch_syscalls_mask
;
97 enum GDBForkState fork_state
;
99 pid_t fork_peer_pid
, fork_peer_tid
;
100 uint8_t siginfo
[MAX_SIGINFO_LENGTH
];
101 unsigned long siginfo_len
;
104 static GDBUserState gdbserver_user_state
;
106 int gdb_get_char(void)
112 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
114 if (errno
== ECONNRESET
) {
115 gdbserver_user_state
.fd
= -1;
117 if (errno
!= EINTR
) {
120 } else if (ret
== 0) {
121 close(gdbserver_user_state
.fd
);
122 gdbserver_user_state
.fd
= -1;
131 bool gdb_got_immediate_ack(void)
137 /* no response, continue anyway */
142 /* received correctly, continue */
146 /* anything else, including '-' then try again */
150 void gdb_put_buffer(const uint8_t *buf
, int len
)
155 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
157 if (errno
!= EINTR
) {
167 /* Tell the remote gdb that the process has exited. */
168 void gdb_exit(int code
)
172 if (!gdbserver_state
.init
) {
175 if (gdbserver_user_state
.socket_path
) {
176 unlink(gdbserver_user_state
.socket_path
);
178 if (gdbserver_user_state
.fd
< 0) {
182 trace_gdbstub_op_exiting((uint8_t)code
);
184 if (gdbserver_state
.allow_stop_reply
) {
185 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
187 gdbserver_state
.allow_stop_reply
= false;
192 void gdb_qemu_exit(int code
)
197 int gdb_handlesig(CPUState
*cpu
, int sig
, const char *reason
, void *siginfo
,
203 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
209 * Save target-specific siginfo.
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.
215 memcpy(gdbserver_user_state
.siginfo
, siginfo
, siginfo_len
);
216 gdbserver_user_state
.siginfo_len
= siginfo_len
;
219 /* disable single step if it was enabled */
220 cpu_single_step(cpu
, 0);
224 gdb_set_stop_cpu(cpu
);
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
, ';');
231 g_string_append(gdbserver_state
.str_buf
, reason
);
234 gdbserver_state
.allow_stop_reply
= false;
238 * gdb_put_packet() might have detected that the peer terminated the
241 if (gdbserver_user_state
.fd
< 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);
253 for (i
= 0; i
< n
; i
++) {
254 gdb_read_byte(buf
[i
]);
258 * XXX: Connection closed. Should probably wait for another
259 * connection before continuing.
262 close(gdbserver_user_state
.fd
);
264 gdbserver_user_state
.fd
= -1;
268 sig
= gdbserver_state
.signal
;
269 gdbserver_state
.signal
= 0;
273 /* Tell the remote gdb that the process has exited due to SIG. */
274 void gdb_signalled(CPUArchState
*env
, int sig
)
278 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0 ||
279 !gdbserver_state
.allow_stop_reply
) {
283 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
285 gdbserver_state
.allow_stop_reply
= false;
288 static void gdb_accept_init(int fd
)
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
;
298 static bool gdb_accept_socket(int gdb_fd
)
303 fd
= accept(gdb_fd
, NULL
, NULL
);
304 if (fd
< 0 && errno
!= EINTR
) {
305 perror("accept socket");
307 } else if (fd
>= 0) {
308 qemu_set_cloexec(fd
);
317 static int gdbserver_open_socket(const char *path
)
319 struct sockaddr_un sockaddr
= {};
322 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
324 perror("create socket");
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
));
332 perror("bind socket");
338 perror("listen socket");
346 static bool gdb_accept_tcp(int gdb_fd
)
348 struct sockaddr_in sockaddr
= {};
353 len
= sizeof(sockaddr
);
354 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
355 if (fd
< 0 && errno
!= EINTR
) {
358 } else if (fd
>= 0) {
359 qemu_set_cloexec(fd
);
364 /* set short latency */
365 if (socket_set_nodelay(fd
)) {
366 perror("setsockopt");
375 static int gdbserver_open_port(int port
)
377 struct sockaddr_in sockaddr
;
380 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
385 qemu_set_cloexec(fd
);
387 socket_set_fast_reuse(fd
);
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
));
408 int gdbserver_start(const char *port_or_path
)
410 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
414 gdb_fd
= gdbserver_open_port(port
);
416 gdb_fd
= gdbserver_open_socket(port_or_path
);
423 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
425 } else if (gdb_accept_socket(gdb_fd
)) {
426 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
435 void gdbserver_fork_start(void)
437 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
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
;
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();
451 static void disable_gdbstub(CPUState
*thread_cpu
)
455 close(gdbserver_user_state
.fd
);
456 gdbserver_user_state
.fd
= -1;
458 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
459 /* no cpu_watchpoint_remove_all for user-mode */
460 cpu_single_step(cpu
, 0);
462 tb_flush(thread_cpu
);
465 void gdbserver_fork_end(CPUState
*cpu
, pid_t pid
)
470 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
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]);
483 if (gdbserver_user_state
.fork_state
== GDB_FORK_DISABLED
) {
485 disable_gdbstub(cpu
);
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();
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
;
505 if (!gdbserver_state
.allow_stop_reply
) {
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());
515 gdbserver_state
.state
= RS_IDLE
;
516 gdbserver_state
.allow_stop_reply
= false;
517 gdbserver_user_state
.running_state
= 0;
519 switch (gdbserver_user_state
.fork_state
) {
520 case GDB_FORK_ENABLED
:
521 if (gdbserver_user_state
.running_state
) {
526 case GDB_FORK_ACTIVE
:
527 if (read(gdbserver_user_state
.fd
, &b
, 1) != 1) {
532 case GDB_FORK_DEACTIVATING
:
533 b
= GDB_FORK_ACTIVATE
;
534 if (write(fd
, &b
, 1) != 1) {
537 gdbserver_user_state
.fork_state
= GDB_FORK_INACTIVE
;
539 case GDB_FORK_INACTIVE
:
540 if (read(fd
, &b
, 1) != 1) {
544 case GDB_FORK_ACTIVATE
:
545 gdbserver_user_state
.fork_state
= GDB_FORK_ACTIVE
;
547 case GDB_FORK_ENABLE
:
548 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
550 case GDB_FORK_DISABLE
:
551 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
554 g_assert_not_reached();
557 case GDB_FORK_ENABLING
:
558 b
= GDB_FORK_DISABLE
;
559 if (write(fd
, &b
, 1) != 1) {
562 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
564 case GDB_FORK_DISABLING
:
566 if (write(fd
, &b
, 1) != 1) {
569 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
571 case GDB_FORK_DISABLED
:
573 disable_gdbstub(cpu
);
576 g_assert_not_reached();
583 disable_gdbstub(cpu
);
587 void gdb_handle_query_supported_user(const char *gdb_supported
)
589 if (strstr(gdb_supported
, "fork-events+")) {
590 gdbserver_user_state
.fork_events
= true;
592 g_string_append(gdbserver_state
.str_buf
, ";fork-events+");
595 bool gdb_handle_set_thread_user(uint32_t pid
, uint32_t tid
)
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");
607 bool gdb_handle_detach_user(uint32_t pid
)
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
:
616 gdb_put_packet("OK");
624 * Execution state helpers
627 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
632 void gdb_continue(void)
634 gdbserver_user_state
.running_state
= 1;
635 trace_gdbstub_op_continue();
639 * Resume execution, for user-mode emulation it's equivalent to
642 int gdb_continue_partial(char *newstates
)
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.
651 if (newstates
[cpu
->cpu_index
] == 's') {
652 trace_gdbstub_op_stepping(cpu
->cpu_index
);
653 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
656 gdbserver_user_state
.running_state
= 1;
661 * Memory access helpers
663 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
664 uint8_t *buf
, int len
, bool is_write
)
668 cc
= CPU_GET_CLASS(cpu
);
669 if (cc
->memory_rw_debug
) {
670 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
672 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
679 unsigned int gdb_get_max_cpus(void)
682 unsigned int max_cpus
= 1;
685 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
691 /* replay not supported for user-mode */
692 bool gdb_can_reverse(void)
698 * Break/Watch point helpers
701 bool gdb_supports_guest_debug(void)
703 /* user-mode == TCG == supported */
707 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
713 case GDB_BREAKPOINT_SW
:
714 case GDB_BREAKPOINT_HW
:
716 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
723 /* user-mode doesn't support watchpoints */
728 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
734 case GDB_BREAKPOINT_SW
:
735 case GDB_BREAKPOINT_HW
:
737 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
744 /* user-mode doesn't support watchpoints */
749 void gdb_breakpoint_remove_all(CPUState
*cs
)
751 cpu_breakpoint_remove_all(cs
, BP_GDB
);
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).
762 void gdb_syscall_handling(const char *syscall_packet
)
764 gdb_put_packet(syscall_packet
);
765 gdb_handlesig(gdbserver_state
.c_cpu
, 0, NULL
, NULL
, 0);
768 static bool should_catch_syscall(int num
)
770 if (gdbserver_user_state
.catch_all_syscalls
) {
773 if (num
< 0 || num
>= GDB_NR_SYSCALLS
) {
776 return test_bit(num
, gdbserver_user_state
.catch_syscalls_mask
);
779 void gdb_syscall_entry(CPUState
*cs
, int num
)
781 if (should_catch_syscall(num
)) {
782 g_autofree
char *reason
= g_strdup_printf("syscall_entry:%x;", num
);
783 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
787 void gdb_syscall_return(CPUState
*cs
, int num
)
789 if (should_catch_syscall(num
)) {
790 g_autofree
char *reason
= g_strdup_printf("syscall_return:%x;", num
);
791 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
795 void gdb_handle_set_catch_syscalls(GArray
*params
, void *user_ctx
)
797 const char *param
= gdb_get_cmd_param(params
, 0)->data
;
798 GDBSyscallsMask catch_syscalls_mask
;
799 bool catch_all_syscalls
;
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");
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");
820 * "1;..." means catching only the specified syscalls.
821 * The syscall list must not be empty.
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
!= ';')) {
830 if (num
>= GDB_NR_SYSCALLS
) {
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.
836 catch_all_syscalls
= true;
838 set_bit(num
, catch_syscalls_mask
);
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
));
849 gdb_put_packet("OK");
854 gdb_put_packet("E00");
857 void gdb_handle_query_xfer_siginfo(GArray
*params
, void *user_ctx
)
859 unsigned long offset
, len
;
860 uint8_t *siginfo_offset
;
862 offset
= gdb_get_cmd_param(params
, 0)->val_ul
;
863 len
= gdb_get_cmd_param(params
, 1)->val_ul
;
865 if (offset
+ len
> gdbserver_user_state
.siginfo_len
) {
866 /* Invalid offset and/or requested length. */
867 gdb_put_packet("E01");
871 siginfo_offset
= (uint8_t *)gdbserver_user_state
.siginfo
+ offset
;
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);