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
)];
28 /* User-mode specific state */
34 * Store syscalls mask without memory allocation in order to avoid
35 * implementing synchronization.
37 bool catch_all_syscalls
;
38 GDBSyscallsMask catch_syscalls_mask
;
41 static GDBUserState gdbserver_user_state
;
43 int gdb_get_char(void)
49 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
51 if (errno
== ECONNRESET
) {
52 gdbserver_user_state
.fd
= -1;
57 } else if (ret
== 0) {
58 close(gdbserver_user_state
.fd
);
59 gdbserver_user_state
.fd
= -1;
68 bool gdb_got_immediate_ack(void)
74 /* no response, continue anyway */
79 /* received correctly, continue */
83 /* anything else, including '-' then try again */
87 void gdb_put_buffer(const uint8_t *buf
, int len
)
92 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
104 /* Tell the remote gdb that the process has exited. */
105 void gdb_exit(int code
)
109 if (!gdbserver_state
.init
) {
112 if (gdbserver_user_state
.socket_path
) {
113 unlink(gdbserver_user_state
.socket_path
);
115 if (gdbserver_user_state
.fd
< 0) {
119 trace_gdbstub_op_exiting((uint8_t)code
);
121 if (gdbserver_state
.allow_stop_reply
) {
122 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
124 gdbserver_state
.allow_stop_reply
= false;
129 void gdb_qemu_exit(int code
)
134 int gdb_handlesig_reason(CPUState
*cpu
, int sig
, const char *reason
)
139 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
143 /* disable single step if it was enabled */
144 cpu_single_step(cpu
, 0);
148 gdb_set_stop_cpu(cpu
);
149 if (gdbserver_state
.allow_stop_reply
) {
150 g_string_printf(gdbserver_state
.str_buf
,
151 "T%02xthread:", gdb_target_signal_to_gdb(sig
));
152 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
153 g_string_append_c(gdbserver_state
.str_buf
, ';');
155 g_string_append(gdbserver_state
.str_buf
, reason
);
158 gdbserver_state
.allow_stop_reply
= false;
162 * gdb_put_packet() might have detected that the peer terminated the
165 if (gdbserver_user_state
.fd
< 0) {
170 gdbserver_state
.state
= RS_IDLE
;
171 gdbserver_user_state
.running_state
= 0;
172 while (gdbserver_user_state
.running_state
== 0) {
173 n
= read(gdbserver_user_state
.fd
, buf
, 256);
177 for (i
= 0; i
< n
; i
++) {
178 gdb_read_byte(buf
[i
]);
182 * XXX: Connection closed. Should probably wait for another
183 * connection before continuing.
186 close(gdbserver_user_state
.fd
);
188 gdbserver_user_state
.fd
= -1;
192 sig
= gdbserver_state
.signal
;
193 gdbserver_state
.signal
= 0;
197 /* Tell the remote gdb that the process has exited due to SIG. */
198 void gdb_signalled(CPUArchState
*env
, int sig
)
202 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0 ||
203 !gdbserver_state
.allow_stop_reply
) {
207 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
209 gdbserver_state
.allow_stop_reply
= false;
212 static void gdb_accept_init(int fd
)
214 gdb_init_gdbserver_state();
215 gdb_create_default_process(&gdbserver_state
);
216 gdbserver_state
.processes
[0].attached
= true;
217 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
218 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
219 gdbserver_user_state
.fd
= fd
;
222 static bool gdb_accept_socket(int gdb_fd
)
227 fd
= accept(gdb_fd
, NULL
, NULL
);
228 if (fd
< 0 && errno
!= EINTR
) {
229 perror("accept socket");
231 } else if (fd
>= 0) {
232 qemu_set_cloexec(fd
);
241 static int gdbserver_open_socket(const char *path
)
243 struct sockaddr_un sockaddr
= {};
246 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
248 perror("create socket");
252 sockaddr
.sun_family
= AF_UNIX
;
253 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
254 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
256 perror("bind socket");
262 perror("listen socket");
270 static bool gdb_accept_tcp(int gdb_fd
)
272 struct sockaddr_in sockaddr
= {};
277 len
= sizeof(sockaddr
);
278 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
279 if (fd
< 0 && errno
!= EINTR
) {
282 } else if (fd
>= 0) {
283 qemu_set_cloexec(fd
);
288 /* set short latency */
289 if (socket_set_nodelay(fd
)) {
290 perror("setsockopt");
299 static int gdbserver_open_port(int port
)
301 struct sockaddr_in sockaddr
;
304 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
309 qemu_set_cloexec(fd
);
311 socket_set_fast_reuse(fd
);
313 sockaddr
.sin_family
= AF_INET
;
314 sockaddr
.sin_port
= htons(port
);
315 sockaddr
.sin_addr
.s_addr
= 0;
316 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
332 int gdbserver_start(const char *port_or_path
)
334 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
338 gdb_fd
= gdbserver_open_port(port
);
340 gdb_fd
= gdbserver_open_socket(port_or_path
);
347 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
349 } else if (gdb_accept_socket(gdb_fd
)) {
350 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
359 /* Disable gdb stub for child processes. */
360 void gdbserver_fork(CPUState
*cpu
)
362 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
365 close(gdbserver_user_state
.fd
);
366 gdbserver_user_state
.fd
= -1;
367 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
368 /* no cpu_watchpoint_remove_all for user-mode */
372 * Execution state helpers
375 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
380 void gdb_continue(void)
382 gdbserver_user_state
.running_state
= 1;
383 trace_gdbstub_op_continue();
387 * Resume execution, for user-mode emulation it's equivalent to
390 int gdb_continue_partial(char *newstates
)
395 * This is not exactly accurate, but it's an improvement compared to the
396 * previous situation, where only one CPU would be single-stepped.
399 if (newstates
[cpu
->cpu_index
] == 's') {
400 trace_gdbstub_op_stepping(cpu
->cpu_index
);
401 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
404 gdbserver_user_state
.running_state
= 1;
409 * Memory access helpers
411 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
412 uint8_t *buf
, int len
, bool is_write
)
416 cc
= CPU_GET_CLASS(cpu
);
417 if (cc
->memory_rw_debug
) {
418 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
420 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
427 unsigned int gdb_get_max_cpus(void)
430 unsigned int max_cpus
= 1;
433 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
439 /* replay not supported for user-mode */
440 bool gdb_can_reverse(void)
446 * Break/Watch point helpers
449 bool gdb_supports_guest_debug(void)
451 /* user-mode == TCG == supported */
455 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
461 case GDB_BREAKPOINT_SW
:
462 case GDB_BREAKPOINT_HW
:
464 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
471 /* user-mode doesn't support watchpoints */
476 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
482 case GDB_BREAKPOINT_SW
:
483 case GDB_BREAKPOINT_HW
:
485 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
492 /* user-mode doesn't support watchpoints */
497 void gdb_breakpoint_remove_all(CPUState
*cs
)
499 cpu_breakpoint_remove_all(cs
, BP_GDB
);
503 * For user-mode syscall support we send the system call immediately
504 * and then return control to gdb for it to process the syscall request.
505 * Since the protocol requires that gdb hands control back to us
506 * using a "here are the results" F packet, we don't need to check
507 * gdb_handlesig's return value (which is the signal to deliver if
508 * execution was resumed via a continue packet).
510 void gdb_syscall_handling(const char *syscall_packet
)
512 gdb_put_packet(syscall_packet
);
513 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
516 static bool should_catch_syscall(int num
)
518 if (gdbserver_user_state
.catch_all_syscalls
) {
521 if (num
< 0 || num
>= GDB_NR_SYSCALLS
) {
524 return test_bit(num
, gdbserver_user_state
.catch_syscalls_mask
);
527 void gdb_syscall_entry(CPUState
*cs
, int num
)
529 if (should_catch_syscall(num
)) {
530 g_autofree
char *reason
= g_strdup_printf("syscall_entry:%x;", num
);
531 gdb_handlesig_reason(cs
, gdb_target_sigtrap(), reason
);
535 void gdb_syscall_return(CPUState
*cs
, int num
)
537 if (should_catch_syscall(num
)) {
538 g_autofree
char *reason
= g_strdup_printf("syscall_return:%x;", num
);
539 gdb_handlesig_reason(cs
, gdb_target_sigtrap(), reason
);
543 void gdb_handle_set_catch_syscalls(GArray
*params
, void *user_ctx
)
545 const char *param
= get_param(params
, 0)->data
;
546 GDBSyscallsMask catch_syscalls_mask
;
547 bool catch_all_syscalls
;
551 /* "0" means not catching any syscalls. */
552 if (strcmp(param
, "0") == 0) {
553 gdbserver_user_state
.catch_all_syscalls
= false;
554 memset(gdbserver_user_state
.catch_syscalls_mask
, 0,
555 sizeof(gdbserver_user_state
.catch_syscalls_mask
));
556 gdb_put_packet("OK");
560 /* "1" means catching all syscalls. */
561 if (strcmp(param
, "1") == 0) {
562 gdbserver_user_state
.catch_all_syscalls
= true;
563 gdb_put_packet("OK");
568 * "1;..." means catching only the specified syscalls.
569 * The syscall list must not be empty.
571 if (param
[0] == '1' && param
[1] == ';') {
572 catch_all_syscalls
= false;
573 memset(catch_syscalls_mask
, 0, sizeof(catch_syscalls_mask
));
574 for (p
= ¶m
[2];; p
++) {
575 if (qemu_strtoui(p
, &p
, 16, &num
) || (*p
&& *p
!= ';')) {
578 if (num
>= GDB_NR_SYSCALLS
) {
580 * Fall back to reporting all syscalls. Reporting extra
581 * syscalls is inefficient, but the spec explicitly allows it.
582 * Keep parsing in case there is a syntax error ahead.
584 catch_all_syscalls
= true;
586 set_bit(num
, catch_syscalls_mask
);
592 gdbserver_user_state
.catch_all_syscalls
= catch_all_syscalls
;
593 if (!catch_all_syscalls
) {
594 memcpy(gdbserver_user_state
.catch_syscalls_mask
,
595 catch_syscalls_mask
, sizeof(catch_syscalls_mask
));
597 gdb_put_packet("OK");
602 gdb_put_packet("E00");