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"
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.
61 /* Fully owning the GDB socket. */
63 /* Working with the GDB socket; the peer is inactive. */
65 /* Handing off the GDB socket to the peer. */
66 GDB_FORK_DEACTIVATING
,
67 /* The peer is working with the GDB socket. */
69 /* Asking the peer to close its GDB socket fd. */
71 /* Asking the peer to take over, closing our GDB socket fd. */
73 /* The peer has taken over, our GDB socket fd is closed. */
78 GDB_FORK_ACTIVATE
= 'a',
79 GDB_FORK_ENABLE
= 'e',
80 GDB_FORK_DISABLE
= 'd',
83 /* User-mode specific 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
;
95 enum GDBForkState fork_state
;
97 pid_t fork_peer_pid
, fork_peer_tid
;
98 uint8_t siginfo
[MAX_SIGINFO_LENGTH
];
99 unsigned long siginfo_len
;
102 static GDBUserState gdbserver_user_state
;
104 int gdb_get_char(void)
110 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
112 if (errno
== ECONNRESET
) {
113 gdbserver_user_state
.fd
= -1;
115 if (errno
!= EINTR
) {
118 } else if (ret
== 0) {
119 close(gdbserver_user_state
.fd
);
120 gdbserver_user_state
.fd
= -1;
129 bool gdb_got_immediate_ack(void)
135 /* no response, continue anyway */
140 /* received correctly, continue */
144 /* anything else, including '-' then try again */
148 void gdb_put_buffer(const uint8_t *buf
, int len
)
153 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
155 if (errno
!= EINTR
) {
165 /* Tell the remote gdb that the process has exited. */
166 void gdb_exit(int code
)
170 if (!gdbserver_state
.init
) {
173 if (gdbserver_user_state
.socket_path
) {
174 unlink(gdbserver_user_state
.socket_path
);
176 if (gdbserver_user_state
.fd
< 0) {
180 trace_gdbstub_op_exiting((uint8_t)code
);
182 if (gdbserver_state
.allow_stop_reply
) {
183 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
185 gdbserver_state
.allow_stop_reply
= false;
190 void gdb_qemu_exit(int code
)
195 int gdb_handlesig(CPUState
*cpu
, int sig
, const char *reason
, void *siginfo
,
201 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
207 * Save target-specific siginfo.
209 * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
210 * gdbserver_user_state.siginfo, usually in the source file calling
211 * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
213 memcpy(gdbserver_user_state
.siginfo
, siginfo
, siginfo_len
);
214 gdbserver_user_state
.siginfo_len
= siginfo_len
;
217 /* disable single step if it was enabled */
218 cpu_single_step(cpu
, 0);
222 gdb_set_stop_cpu(cpu
);
223 if (gdbserver_state
.allow_stop_reply
) {
224 g_string_printf(gdbserver_state
.str_buf
,
225 "T%02xthread:", gdb_target_signal_to_gdb(sig
));
226 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
227 g_string_append_c(gdbserver_state
.str_buf
, ';');
229 g_string_append(gdbserver_state
.str_buf
, reason
);
232 gdbserver_state
.allow_stop_reply
= false;
236 * gdb_put_packet() might have detected that the peer terminated the
239 if (gdbserver_user_state
.fd
< 0) {
244 gdbserver_state
.state
= RS_IDLE
;
245 gdbserver_user_state
.running_state
= 0;
246 while (gdbserver_user_state
.running_state
== 0) {
247 n
= read(gdbserver_user_state
.fd
, buf
, 256);
251 for (i
= 0; i
< n
; i
++) {
252 gdb_read_byte(buf
[i
]);
256 * XXX: Connection closed. Should probably wait for another
257 * connection before continuing.
260 close(gdbserver_user_state
.fd
);
262 gdbserver_user_state
.fd
= -1;
266 sig
= gdbserver_state
.signal
;
267 gdbserver_state
.signal
= 0;
271 /* Tell the remote gdb that the process has exited due to SIG. */
272 void gdb_signalled(CPUArchState
*env
, int sig
)
276 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0 ||
277 !gdbserver_state
.allow_stop_reply
) {
281 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
283 gdbserver_state
.allow_stop_reply
= false;
286 static void gdb_accept_init(int fd
)
288 gdb_init_gdbserver_state();
289 gdb_create_default_process(&gdbserver_state
);
290 gdbserver_state
.processes
[0].attached
= true;
291 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
292 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
293 gdbserver_user_state
.fd
= fd
;
296 static bool gdb_accept_socket(int gdb_fd
)
301 fd
= accept(gdb_fd
, NULL
, NULL
);
302 if (fd
< 0 && errno
!= EINTR
) {
303 perror("accept socket");
305 } else if (fd
>= 0) {
306 qemu_set_cloexec(fd
);
315 static int gdbserver_open_socket(const char *path
)
317 struct sockaddr_un sockaddr
= {};
320 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
322 perror("create socket");
326 sockaddr
.sun_family
= AF_UNIX
;
327 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
328 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
330 perror("bind socket");
336 perror("listen socket");
344 static bool gdb_accept_tcp(int gdb_fd
)
346 struct sockaddr_in sockaddr
= {};
351 len
= sizeof(sockaddr
);
352 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
353 if (fd
< 0 && errno
!= EINTR
) {
356 } else if (fd
>= 0) {
357 qemu_set_cloexec(fd
);
362 /* set short latency */
363 if (socket_set_nodelay(fd
)) {
364 perror("setsockopt");
373 static int gdbserver_open_port(int port
)
375 struct sockaddr_in sockaddr
;
378 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
383 qemu_set_cloexec(fd
);
385 socket_set_fast_reuse(fd
);
387 sockaddr
.sin_family
= AF_INET
;
388 sockaddr
.sin_port
= htons(port
);
389 sockaddr
.sin_addr
.s_addr
= 0;
390 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
406 int gdbserver_start(const char *port_or_path
)
408 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
412 gdb_fd
= gdbserver_open_port(port
);
414 gdb_fd
= gdbserver_open_socket(port_or_path
);
421 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
423 } else if (gdb_accept_socket(gdb_fd
)) {
424 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
433 void gdbserver_fork_start(void)
435 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
438 if (!gdbserver_user_state
.fork_events
||
439 qemu_socketpair(AF_UNIX
, SOCK_STREAM
, 0,
440 gdbserver_user_state
.fork_sockets
) < 0) {
441 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
444 gdbserver_user_state
.fork_state
= GDB_FORK_INACTIVE
;
445 gdbserver_user_state
.fork_peer_pid
= getpid();
446 gdbserver_user_state
.fork_peer_tid
= qemu_get_thread_id();
449 static void disable_gdbstub(CPUState
*thread_cpu
)
453 close(gdbserver_user_state
.fd
);
454 gdbserver_user_state
.fd
= -1;
456 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
457 /* no cpu_watchpoint_remove_all for user-mode */
458 cpu_single_step(cpu
, 0);
460 tb_flush(thread_cpu
);
463 void gdbserver_fork_end(CPUState
*cpu
, pid_t pid
)
468 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
473 if (gdbserver_user_state
.fork_state
!= GDB_FORK_DISABLED
) {
474 g_assert(gdbserver_user_state
.fork_state
== GDB_FORK_INACTIVE
);
475 close(gdbserver_user_state
.fork_sockets
[0]);
476 close(gdbserver_user_state
.fork_sockets
[1]);
481 if (gdbserver_user_state
.fork_state
== GDB_FORK_DISABLED
) {
483 disable_gdbstub(cpu
);
489 close(gdbserver_user_state
.fork_sockets
[0]);
490 fd
= gdbserver_user_state
.fork_sockets
[1];
491 g_assert(gdbserver_state
.process_num
== 1);
492 g_assert(gdbserver_state
.processes
[0].pid
==
493 gdbserver_user_state
.fork_peer_pid
);
494 g_assert(gdbserver_state
.processes
[0].attached
);
495 gdbserver_state
.processes
[0].pid
= getpid();
497 close(gdbserver_user_state
.fork_sockets
[1]);
498 fd
= gdbserver_user_state
.fork_sockets
[0];
499 gdbserver_user_state
.fork_state
= GDB_FORK_ACTIVE
;
500 gdbserver_user_state
.fork_peer_pid
= pid
;
501 gdbserver_user_state
.fork_peer_tid
= pid
;
503 if (!gdbserver_state
.allow_stop_reply
) {
506 g_string_printf(gdbserver_state
.str_buf
,
507 "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
508 gdb_target_signal_to_gdb(gdb_target_sigtrap()),
509 pid
, pid
, (int)getpid(), qemu_get_thread_id());
513 gdbserver_state
.state
= RS_IDLE
;
514 gdbserver_state
.allow_stop_reply
= false;
515 gdbserver_user_state
.running_state
= 0;
517 switch (gdbserver_user_state
.fork_state
) {
518 case GDB_FORK_ENABLED
:
519 if (gdbserver_user_state
.running_state
) {
524 case GDB_FORK_ACTIVE
:
525 if (read(gdbserver_user_state
.fd
, &b
, 1) != 1) {
530 case GDB_FORK_DEACTIVATING
:
531 b
= GDB_FORK_ACTIVATE
;
532 if (write(fd
, &b
, 1) != 1) {
535 gdbserver_user_state
.fork_state
= GDB_FORK_INACTIVE
;
537 case GDB_FORK_INACTIVE
:
538 if (read(fd
, &b
, 1) != 1) {
542 case GDB_FORK_ACTIVATE
:
543 gdbserver_user_state
.fork_state
= GDB_FORK_ACTIVE
;
545 case GDB_FORK_ENABLE
:
546 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
548 case GDB_FORK_DISABLE
:
549 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
552 g_assert_not_reached();
555 case GDB_FORK_ENABLING
:
556 b
= GDB_FORK_DISABLE
;
557 if (write(fd
, &b
, 1) != 1) {
560 gdbserver_user_state
.fork_state
= GDB_FORK_ENABLED
;
562 case GDB_FORK_DISABLING
:
564 if (write(fd
, &b
, 1) != 1) {
567 gdbserver_user_state
.fork_state
= GDB_FORK_DISABLED
;
569 case GDB_FORK_DISABLED
:
571 disable_gdbstub(cpu
);
574 g_assert_not_reached();
581 disable_gdbstub(cpu
);
585 void gdb_handle_query_supported_user(const char *gdb_supported
)
587 if (strstr(gdb_supported
, "fork-events+")) {
588 gdbserver_user_state
.fork_events
= true;
590 g_string_append(gdbserver_state
.str_buf
, ";fork-events+");
593 bool gdb_handle_set_thread_user(uint32_t pid
, uint32_t tid
)
595 if (gdbserver_user_state
.fork_state
== GDB_FORK_ACTIVE
&&
596 pid
== gdbserver_user_state
.fork_peer_pid
&&
597 tid
== gdbserver_user_state
.fork_peer_tid
) {
598 gdbserver_user_state
.fork_state
= GDB_FORK_DEACTIVATING
;
599 gdb_put_packet("OK");
605 bool gdb_handle_detach_user(uint32_t pid
)
609 if (gdbserver_user_state
.fork_state
== GDB_FORK_ACTIVE
) {
610 enable
= pid
== gdbserver_user_state
.fork_peer_pid
;
611 if (enable
|| pid
== getpid()) {
612 gdbserver_user_state
.fork_state
= enable
? GDB_FORK_ENABLING
:
614 gdb_put_packet("OK");
622 * Execution state helpers
625 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
630 void gdb_continue(void)
632 gdbserver_user_state
.running_state
= 1;
633 trace_gdbstub_op_continue();
637 * Resume execution, for user-mode emulation it's equivalent to
640 int gdb_continue_partial(char *newstates
)
645 * This is not exactly accurate, but it's an improvement compared to the
646 * previous situation, where only one CPU would be single-stepped.
649 if (newstates
[cpu
->cpu_index
] == 's') {
650 trace_gdbstub_op_stepping(cpu
->cpu_index
);
651 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
654 gdbserver_user_state
.running_state
= 1;
659 * Memory access helpers
661 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
662 uint8_t *buf
, int len
, bool is_write
)
666 cc
= CPU_GET_CLASS(cpu
);
667 if (cc
->memory_rw_debug
) {
668 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
670 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
677 unsigned int gdb_get_max_cpus(void)
680 unsigned int max_cpus
= 1;
683 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
689 /* replay not supported for user-mode */
690 bool gdb_can_reverse(void)
696 * Break/Watch point helpers
699 bool gdb_supports_guest_debug(void)
701 /* user-mode == TCG == supported */
705 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
711 case GDB_BREAKPOINT_SW
:
712 case GDB_BREAKPOINT_HW
:
714 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
721 /* user-mode doesn't support watchpoints */
726 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
732 case GDB_BREAKPOINT_SW
:
733 case GDB_BREAKPOINT_HW
:
735 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
742 /* user-mode doesn't support watchpoints */
747 void gdb_breakpoint_remove_all(CPUState
*cs
)
749 cpu_breakpoint_remove_all(cs
, BP_GDB
);
753 * For user-mode syscall support we send the system call immediately
754 * and then return control to gdb for it to process the syscall request.
755 * Since the protocol requires that gdb hands control back to us
756 * using a "here are the results" F packet, we don't need to check
757 * gdb_handlesig's return value (which is the signal to deliver if
758 * execution was resumed via a continue packet).
760 void gdb_syscall_handling(const char *syscall_packet
)
762 gdb_put_packet(syscall_packet
);
763 gdb_handlesig(gdbserver_state
.c_cpu
, 0, NULL
, NULL
, 0);
766 static bool should_catch_syscall(int num
)
768 if (gdbserver_user_state
.catch_all_syscalls
) {
771 if (num
< 0 || num
>= GDB_NR_SYSCALLS
) {
774 return test_bit(num
, gdbserver_user_state
.catch_syscalls_mask
);
777 void gdb_syscall_entry(CPUState
*cs
, int num
)
779 if (should_catch_syscall(num
)) {
780 g_autofree
char *reason
= g_strdup_printf("syscall_entry:%x;", num
);
781 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
785 void gdb_syscall_return(CPUState
*cs
, int num
)
787 if (should_catch_syscall(num
)) {
788 g_autofree
char *reason
= g_strdup_printf("syscall_return:%x;", num
);
789 gdb_handlesig(cs
, gdb_target_sigtrap(), reason
, NULL
, 0);
793 void gdb_handle_set_catch_syscalls(GArray
*params
, void *user_ctx
)
795 const char *param
= get_param(params
, 0)->data
;
796 GDBSyscallsMask catch_syscalls_mask
;
797 bool catch_all_syscalls
;
801 /* "0" means not catching any syscalls. */
802 if (strcmp(param
, "0") == 0) {
803 gdbserver_user_state
.catch_all_syscalls
= false;
804 memset(gdbserver_user_state
.catch_syscalls_mask
, 0,
805 sizeof(gdbserver_user_state
.catch_syscalls_mask
));
806 gdb_put_packet("OK");
810 /* "1" means catching all syscalls. */
811 if (strcmp(param
, "1") == 0) {
812 gdbserver_user_state
.catch_all_syscalls
= true;
813 gdb_put_packet("OK");
818 * "1;..." means catching only the specified syscalls.
819 * The syscall list must not be empty.
821 if (param
[0] == '1' && param
[1] == ';') {
822 catch_all_syscalls
= false;
823 memset(catch_syscalls_mask
, 0, sizeof(catch_syscalls_mask
));
824 for (p
= ¶m
[2];; p
++) {
825 if (qemu_strtoui(p
, &p
, 16, &num
) || (*p
&& *p
!= ';')) {
828 if (num
>= GDB_NR_SYSCALLS
) {
830 * Fall back to reporting all syscalls. Reporting extra
831 * syscalls is inefficient, but the spec explicitly allows it.
832 * Keep parsing in case there is a syntax error ahead.
834 catch_all_syscalls
= true;
836 set_bit(num
, catch_syscalls_mask
);
842 gdbserver_user_state
.catch_all_syscalls
= catch_all_syscalls
;
843 if (!catch_all_syscalls
) {
844 memcpy(gdbserver_user_state
.catch_syscalls_mask
,
845 catch_syscalls_mask
, sizeof(catch_syscalls_mask
));
847 gdb_put_packet("OK");
852 gdb_put_packet("E00");
855 void gdb_handle_query_xfer_siginfo(GArray
*params
, void *user_ctx
)
857 unsigned long offset
, len
;
858 uint8_t *siginfo_offset
;
860 offset
= get_param(params
, 0)->val_ul
;
861 len
= get_param(params
, 1)->val_ul
;
863 if (offset
+ len
> gdbserver_user_state
.siginfo_len
) {
864 /* Invalid offset and/or requested length. */
865 gdb_put_packet("E01");
869 siginfo_offset
= (uint8_t *)gdbserver_user_state
.siginfo
+ offset
;
872 g_string_assign(gdbserver_state
.str_buf
, "l");
873 gdb_memtox(gdbserver_state
.str_buf
, (const char *)siginfo_offset
, len
);
874 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
875 gdbserver_state
.str_buf
->len
, true);