xref: /openbmc/qemu/gdbstub/user.c (revision f84e313e)
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                 return;
521             }
522             QEMU_FALLTHROUGH;
523         case GDB_FORK_ACTIVE:
524             if (read(gdbserver_user_state.fd, &b, 1) != 1) {
525                 goto fail;
526             }
527             gdb_read_byte(b);
528             break;
529         case GDB_FORK_DEACTIVATING:
530             b = GDB_FORK_ACTIVATE;
531             if (write(fd, &b, 1) != 1) {
532                 goto fail;
533             }
534             gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
535             break;
536         case GDB_FORK_INACTIVE:
537             if (read(fd, &b, 1) != 1) {
538                 goto fail;
539             }
540             switch (b) {
541             case GDB_FORK_ACTIVATE:
542                 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
543                 break;
544             case GDB_FORK_ENABLE:
545                 close(fd);
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             close(fd);
561             gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
562             break;
563         case GDB_FORK_DISABLING:
564             b = GDB_FORK_ENABLE;
565             if (write(fd, &b, 1) != 1) {
566                 goto fail;
567             }
568             gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
569             break;
570         case GDB_FORK_DISABLED:
571             close(fd);
572             disable_gdbstub(cpu);
573             return;
574         default:
575             g_assert_not_reached();
576         }
577     }
578 
579 fail:
580     close(fd);
581     if (pid == 0) {
582         disable_gdbstub(cpu);
583     }
584 }
585 
586 void gdb_handle_query_supported_user(const char *gdb_supported)
587 {
588     if (strstr(gdb_supported, "fork-events+")) {
589         gdbserver_user_state.fork_events = true;
590     }
591     g_string_append(gdbserver_state.str_buf, ";fork-events+");
592 }
593 
594 bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
595 {
596     if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
597             pid == gdbserver_user_state.fork_peer_pid &&
598             tid == gdbserver_user_state.fork_peer_tid) {
599         gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
600         gdb_put_packet("OK");
601         return true;
602     }
603     return false;
604 }
605 
606 bool gdb_handle_detach_user(uint32_t pid)
607 {
608     bool enable;
609 
610     if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
611         enable = pid == gdbserver_user_state.fork_peer_pid;
612         if (enable || pid == getpid()) {
613             gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
614                                                        GDB_FORK_DISABLING;
615             gdb_put_packet("OK");
616             return true;
617         }
618     }
619     return false;
620 }
621 
622 /*
623  * Execution state helpers
624  */
625 
626 void gdb_handle_query_attached(GArray *params, void *user_ctx)
627 {
628     gdb_put_packet("0");
629 }
630 
631 void gdb_continue(void)
632 {
633     gdbserver_user_state.running_state = 1;
634     trace_gdbstub_op_continue();
635 }
636 
637 /*
638  * Resume execution, for user-mode emulation it's equivalent to
639  * gdb_continue.
640  */
641 int gdb_continue_partial(char *newstates)
642 {
643     CPUState *cpu;
644     int res = 0;
645     /*
646      * This is not exactly accurate, but it's an improvement compared to the
647      * previous situation, where only one CPU would be single-stepped.
648      */
649     CPU_FOREACH(cpu) {
650         if (newstates[cpu->cpu_index] == 's') {
651             trace_gdbstub_op_stepping(cpu->cpu_index);
652             cpu_single_step(cpu, gdbserver_state.sstep_flags);
653         }
654     }
655     gdbserver_user_state.running_state = 1;
656     return res;
657 }
658 
659 /*
660  * Memory access helpers
661  */
662 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
663                                uint8_t *buf, int len, bool is_write)
664 {
665     CPUClass *cc;
666 
667     cc = CPU_GET_CLASS(cpu);
668     if (cc->memory_rw_debug) {
669         return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
670     }
671     return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
672 }
673 
674 /*
675  * cpu helpers
676  */
677 
678 unsigned int gdb_get_max_cpus(void)
679 {
680     CPUState *cpu;
681     unsigned int max_cpus = 1;
682 
683     CPU_FOREACH(cpu) {
684         max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
685     }
686 
687     return max_cpus;
688 }
689 
690 /* replay not supported for user-mode */
691 bool gdb_can_reverse(void)
692 {
693     return false;
694 }
695 
696 /*
697  * Break/Watch point helpers
698  */
699 
700 bool gdb_supports_guest_debug(void)
701 {
702     /* user-mode == TCG == supported */
703     return true;
704 }
705 
706 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
707 {
708     CPUState *cpu;
709     int err = 0;
710 
711     switch (type) {
712     case GDB_BREAKPOINT_SW:
713     case GDB_BREAKPOINT_HW:
714         CPU_FOREACH(cpu) {
715             err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
716             if (err) {
717                 break;
718             }
719         }
720         return err;
721     default:
722         /* user-mode doesn't support watchpoints */
723         return -ENOSYS;
724     }
725 }
726 
727 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
728 {
729     CPUState *cpu;
730     int err = 0;
731 
732     switch (type) {
733     case GDB_BREAKPOINT_SW:
734     case GDB_BREAKPOINT_HW:
735         CPU_FOREACH(cpu) {
736             err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
737             if (err) {
738                 break;
739             }
740         }
741         return err;
742     default:
743         /* user-mode doesn't support watchpoints */
744         return -ENOSYS;
745     }
746 }
747 
748 void gdb_breakpoint_remove_all(CPUState *cs)
749 {
750     cpu_breakpoint_remove_all(cs, BP_GDB);
751 }
752 
753 /*
754  * For user-mode syscall support we send the system call immediately
755  * and then return control to gdb for it to process the syscall request.
756  * Since the protocol requires that gdb hands control back to us
757  * using a "here are the results" F packet, we don't need to check
758  * gdb_handlesig's return value (which is the signal to deliver if
759  * execution was resumed via a continue packet).
760  */
761 void gdb_syscall_handling(const char *syscall_packet)
762 {
763     gdb_put_packet(syscall_packet);
764     gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0);
765 }
766 
767 static bool should_catch_syscall(int num)
768 {
769     if (gdbserver_user_state.catch_all_syscalls) {
770         return true;
771     }
772     if (num < 0 || num >= GDB_NR_SYSCALLS) {
773         return false;
774     }
775     return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
776 }
777 
778 void gdb_syscall_entry(CPUState *cs, int num)
779 {
780     if (should_catch_syscall(num)) {
781         g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
782         gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
783     }
784 }
785 
786 void gdb_syscall_return(CPUState *cs, int num)
787 {
788     if (should_catch_syscall(num)) {
789         g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
790         gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
791     }
792 }
793 
794 void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
795 {
796     const char *param = get_param(params, 0)->data;
797     GDBSyscallsMask catch_syscalls_mask;
798     bool catch_all_syscalls;
799     unsigned int num;
800     const char *p;
801 
802     /* "0" means not catching any syscalls. */
803     if (strcmp(param, "0") == 0) {
804         gdbserver_user_state.catch_all_syscalls = false;
805         memset(gdbserver_user_state.catch_syscalls_mask, 0,
806                sizeof(gdbserver_user_state.catch_syscalls_mask));
807         gdb_put_packet("OK");
808         return;
809     }
810 
811     /* "1" means catching all syscalls. */
812     if (strcmp(param, "1") == 0) {
813         gdbserver_user_state.catch_all_syscalls = true;
814         gdb_put_packet("OK");
815         return;
816     }
817 
818     /*
819      * "1;..." means catching only the specified syscalls.
820      * The syscall list must not be empty.
821      */
822     if (param[0] == '1' && param[1] == ';') {
823         catch_all_syscalls = false;
824         memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
825         for (p = &param[2];; p++) {
826             if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
827                 goto err;
828             }
829             if (num >= GDB_NR_SYSCALLS) {
830                 /*
831                  * Fall back to reporting all syscalls. Reporting extra
832                  * syscalls is inefficient, but the spec explicitly allows it.
833                  * Keep parsing in case there is a syntax error ahead.
834                  */
835                 catch_all_syscalls = true;
836             } else {
837                 set_bit(num, catch_syscalls_mask);
838             }
839             if (!*p) {
840                 break;
841             }
842         }
843         gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
844         if (!catch_all_syscalls) {
845             memcpy(gdbserver_user_state.catch_syscalls_mask,
846                    catch_syscalls_mask, sizeof(catch_syscalls_mask));
847         }
848         gdb_put_packet("OK");
849         return;
850     }
851 
852 err:
853     gdb_put_packet("E00");
854 }
855