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