master
c 978 lines 26.3 KB
Raw
1 /*
2 * gdbstub user-mode helper routines.
3 *
4 * We know for user-mode we are using TCG so we can call stuff directly.
5 *
6 * Copyright (c) 2003-2005 Fabrice Bellard
7 * Copyright (c) 2022 Linaro Ltd
8 *
9 * SPDX-License-Identifier: LGPL-2.0-or-later
10 */
11
12 #include "qemu/osdep.h"
13 #include "qemu/bitops.h"
14 #include "qemu/cutils.h"
15 #include "qemu/sockets.h"
16 #include "qapi/error.h"
17 #include "exec/hwaddr.h"
18 #include "exec/gdbstub.h"
19 #include "gdbstub/commands.h"
20 #include "gdbstub/syscalls.h"
21 #include "gdbstub/user.h"
22 #include "gdbstub/enums.h"
23 #include "hw/core/cpu.h"
24 #include "user/signal.h"
25 #include "trace.h"
26 #include "internals.h"
27
28 #define GDB_NR_SYSCALLS 1024
29 typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)];
30
31 /*
32 * Forked child talks to its parent in order to let GDB enforce the
33 * follow-fork-mode. This happens inside a start_exclusive() section, so that
34 * the other threads, which may be forking too, do not interfere. The
35 * implementation relies on GDB not sending $vCont until it has detached
36 * either from the parent (follow-fork-mode child) or from the child
37 * (follow-fork-mode parent).
38 *
39 * The parent and the child share the GDB socket; at any given time only one
40 * of them is allowed to use it, as is reflected in the respective fork_state.
41 * This is negotiated via the fork_sockets pair as a reaction to $Hg.
42 *
43 * Below is a short summary of the possible state transitions:
44 *
45 * ENABLED : Terminal state.
46 * DISABLED : Terminal state.
47 * ACTIVE : Parent initial state.
48 * INACTIVE : Child initial state.
49 * ACTIVE -> DEACTIVATING: On $Hg.
50 * ACTIVE -> ENABLING : On $D.
51 * ACTIVE -> DISABLING : On $D.
52 * ACTIVE -> DISABLED : On communication error.
53 * DEACTIVATING -> INACTIVE : On gdb_read_byte() return.
54 * DEACTIVATING -> DISABLED : On communication error.
55 * INACTIVE -> ACTIVE : On $Hg in the peer.
56 * INACTIVE -> ENABLE : On $D in the peer.
57 * INACTIVE -> DISABLE : On $D in the peer.
58 * INACTIVE -> DISABLED : On communication error.
59 * ENABLING -> ENABLED : On gdb_read_byte() return.
60 * ENABLING -> DISABLED : On communication error.
61 * DISABLING -> DISABLED : On gdb_read_byte() return.
62 */
63 enum GDBForkState {
64 /* Fully owning the GDB socket. */
65 GDB_FORK_ENABLED,
66 /* Working with the GDB socket; the peer is inactive. */
67 GDB_FORK_ACTIVE,
68 /* Handing off the GDB socket to the peer. */
69 GDB_FORK_DEACTIVATING,
70 /* The peer is working with the GDB socket. */
71 GDB_FORK_INACTIVE,
72 /* Asking the peer to close its GDB socket fd. */
73 GDB_FORK_ENABLING,
74 /* Asking the peer to take over, closing our GDB socket fd. */
75 GDB_FORK_DISABLING,
76 /* The peer has taken over, our GDB socket fd is closed. */
77 GDB_FORK_DISABLED,
78 };
79
80 enum GDBForkMessage {
81 GDB_FORK_ACTIVATE = 'a',
82 GDB_FORK_ENABLE = 'e',
83 GDB_FORK_DISABLE = 'd',
84 };
85
86 /* User-mode specific state */
87 typedef struct {
88 int fd;
89 char *socket_path;
90 /*
91 * running state of the guest, when we process a packet that restarts
92 * the guest we set this to true.
93 */
94 bool running;
95 /*
96 * Store syscalls mask without memory allocation in order to avoid
97 * implementing synchronization.
98 */
99 bool catch_all_syscalls;
100 GDBSyscallsMask catch_syscalls_mask;
101 bool fork_events;
102 enum GDBForkState fork_state;
103 int fork_sockets[2];
104 pid_t fork_peer_pid, fork_peer_tid;
105 uint8_t siginfo[MAX_SIGINFO_LENGTH];
106 unsigned long siginfo_len;
107 } GDBUserState;
108
109 static GDBUserState gdbserver_user_state;
110
111 int gdb_get_char(void)
112 {
113 uint8_t ch;
114 int ret;
115
116 for (;;) {
117 ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
118 if (ret < 0) {
119 if (errno == ECONNRESET) {
120 gdbserver_user_state.fd = -1;
121 }
122 if (errno != EINTR) {
123 return -1;
124 }
125 } else if (ret == 0) {
126 close(gdbserver_user_state.fd);
127 gdbserver_user_state.fd = -1;
128 return -1;
129 } else {
130 break;
131 }
132 }
133 return ch;
134 }
135
136 bool gdb_got_immediate_ack(void)
137 {
138 int i;
139
140 i = gdb_get_char();
141 if (i < 0) {
142 /* no response, continue anyway */
143 return true;
144 }
145
146 if (i == '+') {
147 /* received correctly, continue */
148 return true;
149 }
150
151 /* anything else, including '-' then try again */
152 return false;
153 }
154
155 void gdb_put_buffer(const uint8_t *buf, int len)
156 {
157 int ret;
158
159 while (len > 0) {
160 ret = send(gdbserver_user_state.fd, buf, len, 0);
161 if (ret < 0) {
162 if (errno != EINTR) {
163 return;
164 }
165 } else {
166 buf += ret;
167 len -= ret;
168 }
169 }
170 }
171
172 /* Tell the remote gdb that the process has exited. */
173 void gdb_exit(int code)
174 {
175 char buf[4];
176
177 if (!gdbserver_state.init) {
178 return;
179 }
180 if (gdbserver_user_state.socket_path) {
181 unlink(gdbserver_user_state.socket_path);
182 }
183 if (gdbserver_user_state.fd < 0) {
184 return;
185 }
186
187 trace_gdbstub_op_exiting((uint8_t)code);
188
189 if (gdbserver_state.allow_stop_reply) {
190 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
191 gdb_put_packet(buf);
192 gdbserver_state.allow_stop_reply = false;
193 }
194
195 }
196
197 void gdb_qemu_exit(int code)
198 {
199 exit(code);
200 }
201
202 int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
203 int siginfo_len)
204 {
205 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
206 return sig;
207 }
208
209 if (siginfo) {
210 /*
211 * Save target-specific siginfo.
212 *
213 * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
214 * gdbserver_user_state.siginfo, usually in the source file calling
215 * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
216 */
217 memcpy(gdbserver_user_state.siginfo, siginfo, siginfo_len);
218 gdbserver_user_state.siginfo_len = siginfo_len;
219 }
220
221 /* disable single step if it was enabled */
222 cpu_single_step(cpu, 0);
223
224 if (sig != 0) {
225 gdb_set_stop_cpu(cpu);
226 if (gdbserver_state.allow_stop_reply) {
227 g_string_printf(gdbserver_state.str_buf,
228 "T%02xthread:", gdb_target_signal_to_gdb(sig));
229 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
230 g_string_append_c(gdbserver_state.str_buf, ';');
231 if (reason) {
232 g_string_append(gdbserver_state.str_buf, reason);
233 }
234 gdb_put_strbuf();
235 gdbserver_state.allow_stop_reply = false;
236 }
237 }
238 /*
239 * gdb_put_packet() might have detected that the peer terminated the
240 * connection.
241 */
242 if (gdbserver_user_state.fd < 0) {
243 return sig;
244 }
245
246 sig = 0;
247 gdbserver_state.state = RS_IDLE;
248 gdbserver_user_state.running = false;
249 while (!gdbserver_user_state.running) {
250 char buf[256];
251 int n = read(gdbserver_user_state.fd, buf, 256);
252 if (n > 0) {
253 for (int i = 0; i < n; i++) {
254 gdb_read_byte(buf[i]);
255 }
256 } else {
257 /*
258 * XXX: Connection closed. Should probably wait for another
259 * connection before continuing.
260 */
261 if (n == 0) {
262 close(gdbserver_user_state.fd);
263 }
264 gdbserver_user_state.fd = -1;
265 return sig;
266 }
267 }
268 sig = gdbserver_state.signal;
269 gdbserver_state.signal = 0;
270 return sig;
271 }
272
273 /* Tell the remote gdb that the process has exited due to SIG. */
274 void gdb_signalled(CPUArchState *env, int sig)
275 {
276 char buf[4];
277
278 if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
279 !gdbserver_state.allow_stop_reply) {
280 return;
281 }
282
283 snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
284 gdb_put_packet(buf);
285 gdbserver_state.allow_stop_reply = false;
286 }
287
288 static void gdb_accept_init(int fd)
289 {
290 gdb_init_gdbserver_state();
291 gdb_create_default_process(&gdbserver_state);
292 gdbserver_state.processes[0].attached = true;
293 gdbserver_state.c_cpu = gdb_first_attached_cpu();
294 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
295 gdbserver_user_state.fd = fd;
296 }
297
298 static bool gdb_accept_socket(int gdb_fd)
299 {
300 int fd;
301
302 for (;;) {
303 fd = accept(gdb_fd, NULL, NULL);
304 if (fd < 0 && errno != EINTR) {
305 perror("accept socket");
306 return false;
307 } else if (fd >= 0) {
308 qemu_set_cloexec(fd);
309 break;
310 }
311 }
312
313 gdb_accept_init(fd);
314 return true;
315 }
316
317 static int gdbserver_open_socket(const char *path)
318 {
319 g_autoptr(GString) buf = g_string_new("");
320 struct sockaddr_un sockaddr = {};
321 const char *pid_placeholder;
322 int fd, ret;
323
324 pid_placeholder = strstr(path, "%d");
325 if (pid_placeholder != NULL) {
326 g_string_append_len(buf, path, pid_placeholder - path);
327 g_string_append_printf(buf, "%d", qemu_get_thread_id());
328 g_string_append(buf, pid_placeholder + 2);
329 path = buf->str;
330 }
331
332 fd = socket(AF_UNIX, SOCK_STREAM, 0);
333 if (fd < 0) {
334 perror("create socket");
335 return -1;
336 }
337
338 sockaddr.sun_family = AF_UNIX;
339 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
340 unlink(sockaddr.sun_path);
341 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
342 if (ret < 0) {
343 perror("bind socket");
344 close(fd);
345 return -1;
346 }
347 ret = listen(fd, 1);
348 if (ret < 0) {
349 perror("listen socket");
350 close(fd);
351 return -1;
352 }
353
354 return fd;
355 }
356
357 static bool gdb_accept_tcp(int gdb_fd)
358 {
359 struct sockaddr_in sockaddr = {};
360 socklen_t len;
361 int fd;
362
363 for (;;) {
364 len = sizeof(sockaddr);
365 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
366 if (fd < 0 && errno != EINTR) {
367 perror("accept");
368 return false;
369 } else if (fd >= 0) {
370 qemu_set_cloexec(fd);
371 break;
372 }
373 }
374
375 /* set short latency */
376 if (socket_set_nodelay(fd)) {
377 perror("setsockopt");
378 close(fd);
379 return false;
380 }
381
382 gdb_accept_init(fd);
383 return true;
384 }
385
386 static int gdbserver_open_port(int port, Error **errp)
387 {
388 struct sockaddr_in sockaddr;
389 int fd, ret;
390
391 fd = socket(PF_INET, SOCK_STREAM, 0);
392 if (fd < 0) {
393 error_setg_errno(errp, errno, "Failed to create socket");
394 return -1;
395 }
396 qemu_set_cloexec(fd);
397
398 socket_set_fast_reuse(fd);
399
400 sockaddr.sin_family = AF_INET;
401 sockaddr.sin_port = htons(port);
402 sockaddr.sin_addr.s_addr = 0;
403 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
404 if (ret < 0) {
405 error_setg_errno(errp, errno, "Failed to bind socket");
406 close(fd);
407 return -1;
408 }
409 ret = listen(fd, 1);
410 if (ret < 0) {
411 error_setg_errno(errp, errno, "Failed to listen to socket");
412 close(fd);
413 return -1;
414 }
415
416 return fd;
417 }
418
419 static bool gdbserver_accept(int port, int gdb_fd, const char *path)
420 {
421 bool ret;
422
423 if (port > 0) {
424 ret = gdb_accept_tcp(gdb_fd);
425 } else {
426 ret = gdb_accept_socket(gdb_fd);
427 if (ret) {
428 gdbserver_user_state.socket_path = g_strdup(path);
429 }
430 }
431
432 if (!ret) {
433 close(gdb_fd);
434 }
435
436 return ret;
437 }
438
439 struct {
440 int port;
441 int gdb_fd;
442 char *path;
443 } gdbserver_args;
444
445 static void do_gdb_handlesig(CPUState *cs, run_on_cpu_data arg)
446 {
447 int sig;
448
449 sig = target_to_host_signal(gdb_handlesig(cs, 0, NULL, NULL, 0));
450 if (sig >= 1 && sig < NSIG) {
451 qemu_kill_thread(gdb_get_cpu_index(cs), sig);
452 }
453 }
454
455 static void *gdbserver_accept_thread(void *arg)
456 {
457 if (gdbserver_accept(gdbserver_args.port, gdbserver_args.gdb_fd,
458 gdbserver_args.path)) {
459 CPUState *cs = first_cpu;
460
461 async_safe_run_on_cpu(cs, do_gdb_handlesig, RUN_ON_CPU_NULL);
462 qemu_kill_thread(gdb_get_cpu_index(cs), host_interrupt_signal);
463 }
464
465 g_free(gdbserver_args.path);
466 gdbserver_args.path = NULL;
467
468 return NULL;
469 }
470
471 #define USAGE "\nUsage: -g {port|path}[,suspend={y|n}]"
472
473 bool gdbserver_start(const char *args, Error **errp)
474 {
475 g_auto(GStrv) argv = g_strsplit(args, ",", 0);
476 const char *port_or_path = NULL;
477 bool suspend = true;
478 int gdb_fd, port;
479 GStrv arg;
480
481 for (arg = argv; *arg; arg++) {
482 g_auto(GStrv) tokens = g_strsplit(*arg, "=", 2);
483
484 if (g_strcmp0(tokens[0], "suspend") == 0) {
485 if (tokens[1] == NULL) {
486 error_setg(errp,
487 "gdbstub: missing \"suspend\" option value" USAGE);
488 return false;
489 } else if (!qapi_bool_parse(tokens[0], tokens[1],
490 &suspend, errp)) {
491 return false;
492 }
493 } else {
494 if (port_or_path) {
495 error_setg(errp, "gdbstub: unknown option \"%s\"" USAGE, *arg);
496 return false;
497 }
498 port_or_path = *arg;
499 }
500 }
501 if (!port_or_path) {
502 error_setg(errp, "gdbstub: port or path not specified" USAGE);
503 return false;
504 }
505
506 port = g_ascii_strtoull(port_or_path, NULL, 10);
507 if (port > 0) {
508 gdb_fd = gdbserver_open_port(port, errp);
509 } else {
510 gdb_fd = gdbserver_open_socket(port_or_path);
511 }
512 if (gdb_fd < 0) {
513 return false;
514 }
515
516 if (suspend) {
517 if (gdbserver_accept(port, gdb_fd, port_or_path)) {
518 gdb_handlesig(first_cpu, 0, NULL, NULL, 0);
519 return true;
520 } else {
521 error_setg(errp, "gdbstub: failed to accept connection");
522 return false;
523 }
524 } else {
525 QemuThread thread;
526
527 gdbserver_args.port = port;
528 gdbserver_args.gdb_fd = gdb_fd;
529 gdbserver_args.path = g_strdup(port_or_path);
530 qemu_thread_create(&thread, "gdb-accept",
531 &gdbserver_accept_thread, NULL,
532 QEMU_THREAD_DETACHED);
533 return true;
534 }
535 }
536
537 void gdbserver_fork_start(void)
538 {
539 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
540 return;
541 }
542 if (!gdbserver_user_state.fork_events ||
543 qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
544 gdbserver_user_state.fork_sockets) < 0) {
545 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
546 return;
547 }
548 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
549 gdbserver_user_state.fork_peer_pid = getpid();
550 gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
551 }
552
553 static void disable_gdbstub(CPUState *thread_cpu)
554 {
555 CPUState *cpu;
556
557 close(gdbserver_user_state.fd);
558 gdbserver_user_state.fd = -1;
559 CPU_FOREACH(cpu) {
560 gdb_breakpoint_remove_all(cpu);
561 /* no cpu_watchpoint_remove_all for user-mode */
562 cpu_single_step(cpu, 0);
563 }
564 }
565
566 void gdbserver_fork_end(CPUState *cpu, pid_t pid)
567 {
568 char b;
569 int fd;
570
571 if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
572 return;
573 }
574
575 if (pid == -1) {
576 if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
577 g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
578 close(gdbserver_user_state.fork_sockets[0]);
579 close(gdbserver_user_state.fork_sockets[1]);
580 }
581 return;
582 }
583
584 if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
585 if (pid == 0) {
586 disable_gdbstub(cpu);
587 }
588 return;
589 }
590
591 if (pid == 0) {
592 close(gdbserver_user_state.fork_sockets[0]);
593 fd = gdbserver_user_state.fork_sockets[1];
594 g_assert(gdbserver_state.process_num == 1);
595 g_assert(gdbserver_state.processes[0].pid ==
596 gdbserver_user_state.fork_peer_pid);
597 g_assert(gdbserver_state.processes[0].attached);
598 gdbserver_state.processes[0].pid = getpid();
599 } else {
600 close(gdbserver_user_state.fork_sockets[1]);
601 fd = gdbserver_user_state.fork_sockets[0];
602 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
603 gdbserver_user_state.fork_peer_pid = pid;
604 gdbserver_user_state.fork_peer_tid = pid;
605
606 if (!gdbserver_state.allow_stop_reply) {
607 goto fail;
608 }
609 g_string_printf(gdbserver_state.str_buf,
610 "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
611 gdb_target_signal_to_gdb(gdb_target_sigtrap()),
612 pid, pid, (int)getpid(), qemu_get_thread_id());
613 gdb_put_strbuf();
614 }
615
616 gdbserver_state.state = RS_IDLE;
617 gdbserver_state.allow_stop_reply = false;
618 gdbserver_user_state.running = false;
619 for (;;) {
620 switch (gdbserver_user_state.fork_state) {
621 case GDB_FORK_ENABLED:
622 if (gdbserver_user_state.running) {
623 close(fd);
624 return;
625 }
626 QEMU_FALLTHROUGH;
627 case GDB_FORK_ACTIVE:
628 if (read(gdbserver_user_state.fd, &b, 1) != 1) {
629 goto fail;
630 }
631 gdb_read_byte(b);
632 break;
633 case GDB_FORK_DEACTIVATING:
634 b = GDB_FORK_ACTIVATE;
635 if (write(fd, &b, 1) != 1) {
636 goto fail;
637 }
638 gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
639 break;
640 case GDB_FORK_INACTIVE:
641 if (read(fd, &b, 1) != 1) {
642 goto fail;
643 }
644 switch (b) {
645 case GDB_FORK_ACTIVATE:
646 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
647 break;
648 case GDB_FORK_ENABLE:
649 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
650 break;
651 case GDB_FORK_DISABLE:
652 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
653 break;
654 default:
655 g_assert_not_reached();
656 }
657 break;
658 case GDB_FORK_ENABLING:
659 b = GDB_FORK_DISABLE;
660 if (write(fd, &b, 1) != 1) {
661 goto fail;
662 }
663 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
664 break;
665 case GDB_FORK_DISABLING:
666 b = GDB_FORK_ENABLE;
667 if (write(fd, &b, 1) != 1) {
668 goto fail;
669 }
670 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
671 break;
672 case GDB_FORK_DISABLED:
673 close(fd);
674 disable_gdbstub(cpu);
675 return;
676 default:
677 g_assert_not_reached();
678 }
679 }
680
681 fail:
682 close(fd);
683 if (pid == 0) {
684 disable_gdbstub(cpu);
685 }
686 }
687
688 void gdb_handle_query_supported_user(const char *gdb_supported)
689 {
690 if (strstr(gdb_supported, "fork-events+")) {
691 gdbserver_user_state.fork_events = true;
692 }
693 g_string_append(gdbserver_state.str_buf, ";fork-events+");
694 }
695
696 bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
697 {
698 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
699 pid == gdbserver_user_state.fork_peer_pid &&
700 tid == gdbserver_user_state.fork_peer_tid) {
701 gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
702 gdb_put_packet("OK");
703 return true;
704 }
705 return false;
706 }
707
708 bool gdb_handle_detach_user(uint32_t pid)
709 {
710 bool enable;
711
712 if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
713 enable = pid == gdbserver_user_state.fork_peer_pid;
714 if (enable || pid == getpid()) {
715 gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
716 GDB_FORK_DISABLING;
717 gdb_put_packet("OK");
718 return true;
719 }
720 }
721 return false;
722 }
723
724 /*
725 * Execution state helpers
726 */
727
728 void gdb_handle_query_attached(GArray *params, void *user_ctx)
729 {
730 gdb_put_packet("0");
731 }
732
733 void gdb_continue(void)
734 {
735 gdbserver_user_state.running = true;
736 trace_gdbstub_op_continue();
737 }
738
739 /*
740 * Resume execution, for user-mode emulation it's equivalent to
741 * gdb_continue.
742 */
743 int gdb_continue_partial(char *newstates)
744 {
745 CPUState *cpu;
746 int res = 0;
747 /*
748 * This is not exactly accurate, but it's an improvement compared to the
749 * previous situation, where only one CPU would be single-stepped.
750 */
751 CPU_FOREACH(cpu) {
752 if (newstates[cpu->cpu_index] == 's') {
753 trace_gdbstub_op_stepping(cpu->cpu_index);
754 cpu_single_step(cpu, gdbserver_state.sstep_flags);
755 }
756 }
757 gdbserver_user_state.running = true;
758 return res;
759 }
760
761 /*
762 * Memory access helpers
763 */
764 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
765 uint8_t *buf, int len, bool is_write)
766 {
767 if (cpu->cc->memory_rw_debug) {
768 return cpu->cc->memory_rw_debug(cpu, addr, buf, len, is_write);
769 }
770 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
771 }
772
773 /*
774 * cpu helpers
775 */
776
777 unsigned int gdb_get_max_cpus(void)
778 {
779 CPUState *cpu;
780 unsigned int max_cpus = 1;
781
782 CPU_FOREACH(cpu) {
783 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
784 }
785
786 return max_cpus;
787 }
788
789 /*
790 * Break/Watch point helpers
791 */
792
793 int gdb_breakpoint_insert(CPUState *cs, GdbBreakpointType type,
794 vaddr addr, vaddr len)
795 {
796 CPUState *cpu;
797 int err = 0;
798
799 switch (type) {
800 case GDB_BREAKPOINT_SW:
801 case GDB_BREAKPOINT_HW:
802 CPU_FOREACH(cpu) {
803 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
804 if (err) {
805 break;
806 }
807 }
808 return err;
809 default:
810 /* user-mode doesn't support watchpoints */
811 return -ENOSYS;
812 }
813 }
814
815 int gdb_breakpoint_remove(CPUState *cs, GdbBreakpointType type,
816 vaddr addr, vaddr len)
817 {
818 CPUState *cpu;
819 int err = 0;
820
821 switch (type) {
822 case GDB_BREAKPOINT_SW:
823 case GDB_BREAKPOINT_HW:
824 CPU_FOREACH(cpu) {
825 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
826 if (err) {
827 break;
828 }
829 }
830 return err;
831 default:
832 /* user-mode doesn't support watchpoints */
833 return -ENOSYS;
834 }
835 }
836
837 void gdb_breakpoint_remove_all(CPUState *cs)
838 {
839 cpu_breakpoint_remove_all(cs, BP_GDB);
840 }
841
842 /*
843 * For user-mode syscall support we send the system call immediately
844 * and then return control to gdb for it to process the syscall request.
845 * Since the protocol requires that gdb hands control back to us
846 * using a "here are the results" F packet, we don't need to check
847 * gdb_handlesig's return value (which is the signal to deliver if
848 * execution was resumed via a continue packet).
849 */
850 void gdb_syscall_handling(const char *syscall_packet)
851 {
852 gdb_put_packet(syscall_packet);
853 gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0);
854 }
855
856 static bool should_catch_syscall(int num)
857 {
858 if (gdbserver_user_state.catch_all_syscalls) {
859 return true;
860 }
861 if (num < 0 || num >= GDB_NR_SYSCALLS) {
862 return false;
863 }
864 return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
865 }
866
867 void gdb_syscall_entry(CPUState *cs, int num)
868 {
869 if (should_catch_syscall(num)) {
870 g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
871 gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
872 }
873 }
874
875 void gdb_syscall_return(CPUState *cs, int num)
876 {
877 if (should_catch_syscall(num)) {
878 g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
879 gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
880 }
881 }
882
883 void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
884 {
885 const char *param = gdb_get_cmd_param(params, 0)->data;
886 GDBSyscallsMask catch_syscalls_mask;
887 bool catch_all_syscalls;
888 unsigned int num;
889 const char *p;
890
891 /* "0" means not catching any syscalls. */
892 if (strcmp(param, "0") == 0) {
893 gdbserver_user_state.catch_all_syscalls = false;
894 memset(gdbserver_user_state.catch_syscalls_mask, 0,
895 sizeof(gdbserver_user_state.catch_syscalls_mask));
896 gdb_put_packet("OK");
897 return;
898 }
899
900 /* "1" means catching all syscalls. */
901 if (strcmp(param, "1") == 0) {
902 gdbserver_user_state.catch_all_syscalls = true;
903 gdb_put_packet("OK");
904 return;
905 }
906
907 /*
908 * "1;..." means catching only the specified syscalls.
909 * The syscall list must not be empty.
910 */
911 if (param[0] == '1' && param[1] == ';') {
912 catch_all_syscalls = false;
913 memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
914 for (p = &param[2];; p++) {
915 if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
916 goto err;
917 }
918 if (num >= GDB_NR_SYSCALLS) {
919 /*
920 * Fall back to reporting all syscalls. Reporting extra
921 * syscalls is inefficient, but the spec explicitly allows it.
922 * Keep parsing in case there is a syntax error ahead.
923 */
924 catch_all_syscalls = true;
925 } else {
926 set_bit(num, catch_syscalls_mask);
927 }
928 if (!*p) {
929 break;
930 }
931 }
932 gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
933 if (!catch_all_syscalls) {
934 memcpy(gdbserver_user_state.catch_syscalls_mask,
935 catch_syscalls_mask, sizeof(catch_syscalls_mask));
936 }
937 gdb_put_packet("OK");
938 return;
939 }
940
941 err:
942 gdb_put_packet("E00");
943 }
944
945 void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
946 {
947 unsigned long offset, len;
948 uint8_t *siginfo_offset;
949
950 offset = gdb_get_cmd_param(params, 0)->val_ul;
951 len = gdb_get_cmd_param(params, 1)->val_ul;
952
953 if (offset + len > gdbserver_user_state.siginfo_len) {
954 /* Invalid offset and/or requested length. */
955 gdb_put_packet("E01");
956 return;
957 }
958
959 siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset;
960
961 /* Reply */
962 g_string_assign(gdbserver_state.str_buf, "l");
963 gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
964 gdb_put_packet_binary(gdbserver_state.str_buf->str,
965 gdbserver_state.str_buf->len, true);
966 }
967
968 /*
969 * The minimal user-mode stop reply packet is:
970 * T05thread:{id};
971 */
972
973 void gdb_build_stop_packet(GString *buf, CPUState *cs)
974 {
975 g_string_printf(buf, "T%02xthread:", GDB_SIGNAL_TRAP);
976 gdb_append_thread_id(cs, buf);
977 g_string_append_c(buf, ';');
978 }