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/user.h"
19 #include "hw/core/cpu.h"
21 #include "internals.h"
23 /* User-mode specific state */
30 static GDBUserState gdbserver_user_state
;
32 int gdb_get_char(void)
38 ret
= recv(gdbserver_user_state
.fd
, &ch
, 1, 0);
40 if (errno
== ECONNRESET
) {
41 gdbserver_user_state
.fd
= -1;
46 } else if (ret
== 0) {
47 close(gdbserver_user_state
.fd
);
48 gdbserver_user_state
.fd
= -1;
57 void gdb_put_buffer(const uint8_t *buf
, int len
)
62 ret
= send(gdbserver_user_state
.fd
, buf
, len
, 0);
74 /* Tell the remote gdb that the process has exited. */
75 void gdb_exit(int code
)
79 if (!gdbserver_state
.init
) {
82 if (gdbserver_user_state
.socket_path
) {
83 unlink(gdbserver_user_state
.socket_path
);
85 if (gdbserver_user_state
.fd
< 0) {
89 trace_gdbstub_op_exiting((uint8_t)code
);
91 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
95 int gdb_handlesig(CPUState
*cpu
, int sig
)
100 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
104 /* disable single step if it was enabled */
105 cpu_single_step(cpu
, 0);
109 gdb_set_stop_cpu(cpu
);
110 g_string_printf(gdbserver_state
.str_buf
,
111 "T%02xthread:", gdb_target_signal_to_gdb(sig
));
112 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
113 g_string_append_c(gdbserver_state
.str_buf
, ';');
117 * gdb_put_packet() might have detected that the peer terminated the
120 if (gdbserver_user_state
.fd
< 0) {
125 gdbserver_state
.state
= RS_IDLE
;
126 gdbserver_user_state
.running_state
= 0;
127 while (gdbserver_user_state
.running_state
== 0) {
128 n
= read(gdbserver_user_state
.fd
, buf
, 256);
132 for (i
= 0; i
< n
; i
++) {
133 gdb_read_byte(buf
[i
]);
137 * XXX: Connection closed. Should probably wait for another
138 * connection before continuing.
141 close(gdbserver_user_state
.fd
);
143 gdbserver_user_state
.fd
= -1;
147 sig
= gdbserver_state
.signal
;
148 gdbserver_state
.signal
= 0;
152 /* Tell the remote gdb that the process has exited due to SIG. */
153 void gdb_signalled(CPUArchState
*env
, int sig
)
157 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
161 snprintf(buf
, sizeof(buf
), "X%02x", gdb_target_signal_to_gdb(sig
));
165 static void gdb_accept_init(int fd
)
167 gdb_init_gdbserver_state();
168 gdb_create_default_process(&gdbserver_state
);
169 gdbserver_state
.processes
[0].attached
= true;
170 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
171 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
172 gdbserver_user_state
.fd
= fd
;
176 static bool gdb_accept_socket(int gdb_fd
)
181 fd
= accept(gdb_fd
, NULL
, NULL
);
182 if (fd
< 0 && errno
!= EINTR
) {
183 perror("accept socket");
185 } else if (fd
>= 0) {
186 qemu_set_cloexec(fd
);
195 static int gdbserver_open_socket(const char *path
)
197 struct sockaddr_un sockaddr
= {};
200 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
202 perror("create socket");
206 sockaddr
.sun_family
= AF_UNIX
;
207 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
208 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
210 perror("bind socket");
216 perror("listen socket");
224 static bool gdb_accept_tcp(int gdb_fd
)
226 struct sockaddr_in sockaddr
= {};
231 len
= sizeof(sockaddr
);
232 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
233 if (fd
< 0 && errno
!= EINTR
) {
236 } else if (fd
>= 0) {
237 qemu_set_cloexec(fd
);
242 /* set short latency */
243 if (socket_set_nodelay(fd
)) {
244 perror("setsockopt");
253 static int gdbserver_open_port(int port
)
255 struct sockaddr_in sockaddr
;
258 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
263 qemu_set_cloexec(fd
);
265 socket_set_fast_reuse(fd
);
267 sockaddr
.sin_family
= AF_INET
;
268 sockaddr
.sin_port
= htons(port
);
269 sockaddr
.sin_addr
.s_addr
= 0;
270 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
286 int gdbserver_start(const char *port_or_path
)
288 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
292 gdb_fd
= gdbserver_open_port(port
);
294 gdb_fd
= gdbserver_open_socket(port_or_path
);
301 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
303 } else if (gdb_accept_socket(gdb_fd
)) {
304 gdbserver_user_state
.socket_path
= g_strdup(port_or_path
);
313 /* Disable gdb stub for child processes. */
314 void gdbserver_fork(CPUState
*cpu
)
316 if (!gdbserver_state
.init
|| gdbserver_user_state
.fd
< 0) {
319 close(gdbserver_user_state
.fd
);
320 gdbserver_user_state
.fd
= -1;
321 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
322 /* no cpu_watchpoint_remove_all for user-mode */
326 * Execution state helpers
329 void gdb_continue(void)
331 gdbserver_user_state
.running_state
= 1;
332 trace_gdbstub_op_continue();
336 * Resume execution, for user-mode emulation it's equivalent to
339 int gdb_continue_partial(char *newstates
)
344 * This is not exactly accurate, but it's an improvement compared to the
345 * previous situation, where only one CPU would be single-stepped.
348 if (newstates
[cpu
->cpu_index
] == 's') {
349 trace_gdbstub_op_stepping(cpu
->cpu_index
);
350 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
353 gdbserver_user_state
.running_state
= 1;
358 * Break/Watch point helpers
361 bool gdb_supports_guest_debug(void)
363 /* user-mode == TCG == supported */
367 int gdb_breakpoint_insert(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
373 case GDB_BREAKPOINT_SW
:
374 case GDB_BREAKPOINT_HW
:
376 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
383 /* user-mode doesn't support watchpoints */
388 int gdb_breakpoint_remove(CPUState
*cs
, int type
, vaddr addr
, vaddr len
)
394 case GDB_BREAKPOINT_SW
:
395 case GDB_BREAKPOINT_HW
:
397 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
404 /* user-mode doesn't support watchpoints */
409 void gdb_breakpoint_remove_all(CPUState
*cs
)
411 cpu_breakpoint_remove_all(cs
, BP_GDB
);