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/cutils.h"
14 #include "qemu/sockets.h"
15 #include "exec/hwaddr.h"
16 #include "exec/tb-flush.h"
17 #include "exec/gdbstub.h"
18 #include "gdbstub/syscalls.h"
19 #include "gdbstub/user.h"
20 #include "hw/core/cpu.h"
22 #include "internals.h"
24 /* User-mode specific state */
31 static GDBUserState gdbserver_user_state
;
33 int gdb_get_char(void)
39 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
41 if (errno
== ECONNRESET
) {
42 gdbserver_user_state
.fd
= -1;
47 } else if (ret
== 0) {
48 close(gdbserver_user_state
.fd
);
49 gdbserver_user_state
.fd
= -1;
58 bool gdb_got_immediate_ack(void)
64 /* no response, continue anyway */
69 /* received correctly, continue */
73 /* anything else, including '-' then try again */
77 void gdb_put_buffer(const uint8_t *buf
, int len
)
82 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
94 /* Tell the remote gdb that the process has exited. */
95 void gdb_exit(int code
)
99 if (!gdbserver_state
.init
) {
102 if (gdbserver_user_state
.socket_path
) {
103 unlink(gdbserver_user_state
.socket_path
);
105 if (gdbserver_user_state
.fd
< 0) {
109 trace_gdbstub_op_exiting((uint8_t)code
);
111 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
115 int gdb_handlesig(CPUState
*cpu
, int sig
)
120 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
124 /* disable single step if it was enabled */
125 cpu_single_step(cpu
, 0);
129 gdb_set_stop_cpu(cpu
);
130 g_string_printf(gdbserver_state
.str_buf
,
131 "T%02xthread:", gdb_target_signal_to_gdb(sig
));
132 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
133 g_string_append_c(gdbserver_state
.str_buf
, ';');
137 * gdb_put_packet() might have detected that the peer terminated the
140 if (gdbserver_user_state
.fd
< 0) {
145 gdbserver_state
.state
= RS_IDLE
;
146 gdbserver_user_state
.running_state
= 0;
147 while (gdbserver_user_state
.running_state
== 0) {
148 n
= read(gdbserver_user_state
.fd
, buf
, 256);
152 for (i
= 0; i
< n
; i
++) {
153 gdb_read_byte(buf
[i
]);
157 * XXX: Connection closed. Should probably wait for another
158 * connection before continuing.
161 close(gdbserver_user_state
.fd
);
163 gdbserver_user_state
.fd
= -1;
167 sig
= gdbserver_state
.signal
;
168 gdbserver_state
.signal
= 0;
172 /* Tell the remote gdb that the process has exited due to SIG. */
173 void gdb_signalled(CPUArchState
*env
, int sig
)
177 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
181 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
185 static void gdb_accept_init(int fd
)
187 gdb_init_gdbserver_state();
188 gdb_create_default_process(&gdbserver_state
);
189 gdbserver_state
.processes
[0].attached
= true;
190 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
191 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
192 gdbserver_user_state
.fd
= fd
;
196 static bool gdb_accept_socket(int gdb_fd
)
201 fd
= accept(gdb_fd
, NULL
, NULL
);
202 if (fd
< 0 && errno
!= EINTR
) {
203 perror("accept socket");
205 } else if (fd
>= 0) {
206 qemu_set_cloexec(fd
);
215 static int gdbserver_open_socket(const char *path
)
217 struct sockaddr_un sockaddr
= {};
220 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
222 perror("create socket");
226 sockaddr
.sun_family
= AF_UNIX
;
227 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
228 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
230 perror("bind socket");
236 perror("listen socket");
244 static bool gdb_accept_tcp(int gdb_fd
)
246 struct sockaddr_in sockaddr
= {};
251 len
= sizeof(sockaddr
);
252 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
253 if (fd
< 0 && errno
!= EINTR
) {
256 } else if (fd
>= 0) {
257 qemu_set_cloexec(fd
);
262 /* set short latency */
263 if (socket_set_nodelay(fd
)) {
264 perror("setsockopt");
273 static int gdbserver_open_port(int port
)
275 struct sockaddr_in sockaddr
;
278 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
283 qemu_set_cloexec(fd
);
285 socket_set_fast_reuse(fd
);
287 sockaddr
.sin_family
= AF_INET
;
288 sockaddr
.sin_port
= htons(port
);
289 sockaddr
.sin_addr
.s_addr
= 0;
290 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
306 int gdbserver_start(const char *port_or_path
)
308 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
312 gdb_fd
= gdbserver_open_port(port
);
314 gdb_fd
= gdbserver_open_socket(port_or_path
);
321 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
323 } else if (gdb_accept_socket(gdb_fd
)) {
324 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
333 /* Disable gdb stub for child processes. */
334 void gdbserver_fork(CPUState
*cpu
)
336 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
339 close(gdbserver_user_state
.fd
);
340 gdbserver_user_state
.fd
= -1;
341 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
342 /* no cpu_watchpoint_remove_all for user-mode */
346 * Execution state helpers
349 void gdb_handle_query_attached(GArray
*params
, void *user_ctx
)
354 void gdb_continue(void)
356 gdbserver_user_state
.running_state
= 1;
357 trace_gdbstub_op_continue();
361 * Resume execution, for user-mode emulation it's equivalent to
364 int gdb_continue_partial(char *newstates
)
369 * This is not exactly accurate, but it's an improvement compared to the
370 * previous situation, where only one CPU would be single-stepped.
373 if (newstates
[cpu
->cpu_index
] == 's') {
374 trace_gdbstub_op_stepping(cpu
->cpu_index
);
375 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
378 gdbserver_user_state
.running_state
= 1;
383 * Memory access helpers
385 int gdb_target_memory_rw_debug(CPUState
*cpu
, hwaddr addr
,
386 uint8_t *buf
, int len
, bool is_write
)
390 cc
= CPU_GET_CLASS(cpu
);
391 if (cc
->memory_rw_debug
) {
392 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
394 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
401 unsigned int gdb_get_max_cpus(void)
404 unsigned int max_cpus
= 1;
407 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
413 /* replay not supported for user-mode */
414 bool gdb_can_reverse(void)
420 * Break/Watch point helpers
423 bool gdb_supports_guest_debug(void)
425 /* user-mode == TCG == supported */
429 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
435 case GDB_BREAKPOINT_SW
:
436 case GDB_BREAKPOINT_HW
:
438 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
445 /* user-mode doesn't support watchpoints */
450 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
456 case GDB_BREAKPOINT_SW
:
457 case GDB_BREAKPOINT_HW
:
459 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
466 /* user-mode doesn't support watchpoints */
471 void gdb_breakpoint_remove_all(CPUState
*cs
)
473 cpu_breakpoint_remove_all(cs
, BP_GDB
);
477 * For user-mode syscall support we send the system call immediately
478 * and then return control to gdb for it to process the syscall request.
479 * Since the protocol requires that gdb hands control back to us
480 * using a "here are the results" F packet, we don't need to check
481 * gdb_handlesig's return value (which is the signal to deliver if
482 * execution was resumed via a continue packet).
484 void gdb_syscall_handling(const char *syscall_packet
)
486 gdb_put_packet(syscall_packet
);
487 gdb_handlesig(gdbserver_state
.c_cpu
, 0);