sbsa-ref: use Bochs graphics card instead of VGA
[qemu/armbru.git] / gdbstub / user.c
blob80488b6bb96e64c2f3652684adcc9027966909e6
1 /*
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"
21 #include "trace.h"
22 #include "internals.h"
24 /* User-mode specific state */
25 typedef struct {
26 int fd;
27 char *socket_path;
28 int running_state;
29 } GDBUserState;
31 static GDBUserState gdbserver_user_state;
33 int gdb_get_char(void)
35 uint8_t ch;
36 int ret;
38 for (;;) {
39 ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
40 if (ret < 0) {
41 if (errno == ECONNRESET) {
42 gdbserver_user_state.fd = -1;
44 if (errno != EINTR) {
45 return -1;
47 } else if (ret == 0) {
48 close(gdbserver_user_state.fd);
49 gdbserver_user_state.fd = -1;
50 return -1;
51 } else {
52 break;
55 return ch;
58 bool gdb_got_immediate_ack(void)
60 int i;
62 i = gdb_get_char();
63 if (i < 0) {
64 /* no response, continue anyway */
65 return true;
68 if (i == '+') {
69 /* received correctly, continue */
70 return true;
73 /* anything else, including '-' then try again */
74 return false;
77 void gdb_put_buffer(const uint8_t *buf, int len)
79 int ret;
81 while (len > 0) {
82 ret = send(gdbserver_user_state.fd, buf, len, 0);
83 if (ret < 0) {
84 if (errno != EINTR) {
85 return;
87 } else {
88 buf += ret;
89 len -= ret;
94 /* Tell the remote gdb that the process has exited. */
95 void gdb_exit(int code)
97 char buf[4];
99 if (!gdbserver_state.init) {
100 return;
102 if (gdbserver_user_state.socket_path) {
103 unlink(gdbserver_user_state.socket_path);
105 if (gdbserver_user_state.fd < 0) {
106 return;
109 trace_gdbstub_op_exiting((uint8_t)code);
111 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
112 gdb_put_packet(buf);
115 int gdb_handlesig(CPUState *cpu, int sig)
117 char buf[256];
118 int n;
120 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
121 return sig;
124 /* disable single step if it was enabled */
125 cpu_single_step(cpu, 0);
126 tb_flush(cpu);
128 if (sig != 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, ';');
134 gdb_put_strbuf();
137 * gdb_put_packet() might have detected that the peer terminated the
138 * connection.
140 if (gdbserver_user_state.fd < 0) {
141 return sig;
144 sig = 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);
149 if (n > 0) {
150 int i;
152 for (i = 0; i < n; i++) {
153 gdb_read_byte(buf[i]);
155 } else {
157 * XXX: Connection closed. Should probably wait for another
158 * connection before continuing.
160 if (n == 0) {
161 close(gdbserver_user_state.fd);
163 gdbserver_user_state.fd = -1;
164 return sig;
167 sig = gdbserver_state.signal;
168 gdbserver_state.signal = 0;
169 return sig;
172 /* Tell the remote gdb that the process has exited due to SIG. */
173 void gdb_signalled(CPUArchState *env, int sig)
175 char buf[4];
177 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
178 return;
181 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
182 gdb_put_packet(buf);
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;
193 gdb_has_xml = false;
196 static bool gdb_accept_socket(int gdb_fd)
198 int fd;
200 for (;;) {
201 fd = accept(gdb_fd, NULL, NULL);
202 if (fd < 0 && errno != EINTR) {
203 perror("accept socket");
204 return false;
205 } else if (fd >= 0) {
206 qemu_set_cloexec(fd);
207 break;
211 gdb_accept_init(fd);
212 return true;
215 static int gdbserver_open_socket(const char *path)
217 struct sockaddr_un sockaddr = {};
218 int fd, ret;
220 fd = socket(AF_UNIX, SOCK_STREAM, 0);
221 if (fd < 0) {
222 perror("create socket");
223 return -1;
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));
229 if (ret < 0) {
230 perror("bind socket");
231 close(fd);
232 return -1;
234 ret = listen(fd, 1);
235 if (ret < 0) {
236 perror("listen socket");
237 close(fd);
238 return -1;
241 return fd;
244 static bool gdb_accept_tcp(int gdb_fd)
246 struct sockaddr_in sockaddr = {};
247 socklen_t len;
248 int fd;
250 for (;;) {
251 len = sizeof(sockaddr);
252 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
253 if (fd < 0 && errno != EINTR) {
254 perror("accept");
255 return false;
256 } else if (fd >= 0) {
257 qemu_set_cloexec(fd);
258 break;
262 /* set short latency */
263 if (socket_set_nodelay(fd)) {
264 perror("setsockopt");
265 close(fd);
266 return false;
269 gdb_accept_init(fd);
270 return true;
273 static int gdbserver_open_port(int port)
275 struct sockaddr_in sockaddr;
276 int fd, ret;
278 fd = socket(PF_INET, SOCK_STREAM, 0);
279 if (fd < 0) {
280 perror("socket");
281 return -1;
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));
291 if (ret < 0) {
292 perror("bind");
293 close(fd);
294 return -1;
296 ret = listen(fd, 1);
297 if (ret < 0) {
298 perror("listen");
299 close(fd);
300 return -1;
303 return fd;
306 int gdbserver_start(const char *port_or_path)
308 int port = g_ascii_strtoull(port_or_path, NULL, 10);
309 int gdb_fd;
311 if (port > 0) {
312 gdb_fd = gdbserver_open_port(port);
313 } else {
314 gdb_fd = gdbserver_open_socket(port_or_path);
317 if (gdb_fd < 0) {
318 return -1;
321 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
322 return 0;
323 } else if (gdb_accept_socket(gdb_fd)) {
324 gdbserver_user_state.socket_path = g_strdup(port_or_path);
325 return 0;
328 /* gone wrong */
329 close(gdb_fd);
330 return -1;
333 /* Disable gdb stub for child processes. */
334 void gdbserver_fork(CPUState *cpu)
336 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
337 return;
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)
351 gdb_put_packet("0");
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
362 * gdb_continue.
364 int gdb_continue_partial(char *newstates)
366 CPUState *cpu;
367 int res = 0;
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.
372 CPU_FOREACH(cpu) {
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;
379 return res;
383 * Memory access helpers
385 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
386 uint8_t *buf, int len, bool is_write)
388 CPUClass *cc;
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);
398 * cpu helpers
401 unsigned int gdb_get_max_cpus(void)
403 CPUState *cpu;
404 unsigned int max_cpus = 1;
406 CPU_FOREACH(cpu) {
407 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
410 return max_cpus;
413 /* replay not supported for user-mode */
414 bool gdb_can_reverse(void)
416 return false;
420 * Break/Watch point helpers
423 bool gdb_supports_guest_debug(void)
425 /* user-mode == TCG == supported */
426 return true;
429 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
431 CPUState *cpu;
432 int err = 0;
434 switch (type) {
435 case GDB_BREAKPOINT_SW:
436 case GDB_BREAKPOINT_HW:
437 CPU_FOREACH(cpu) {
438 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
439 if (err) {
440 break;
443 return err;
444 default:
445 /* user-mode doesn't support watchpoints */
446 return -ENOSYS;
450 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
452 CPUState *cpu;
453 int err = 0;
455 switch (type) {
456 case GDB_BREAKPOINT_SW:
457 case GDB_BREAKPOINT_HW:
458 CPU_FOREACH(cpu) {
459 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
460 if (err) {
461 break;
464 return err;
465 default:
466 /* user-mode doesn't support watchpoints */
467 return -ENOSYS;
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);