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