xref: /openbmc/qemu/gdbstub/user.c (revision fccb744f41c69fec6fd92225fe907c6e69de5d44)
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, Error **errp)
319 {
320     g_autoptr(GString) buf = g_string_new("");
321     char *pid_placeholder;
322 
323     pid_placeholder = strstr(path, "%d");
324     if (pid_placeholder != NULL) {
325         g_string_append_len(buf, path, pid_placeholder - path);
326         g_string_append_printf(buf, "%d", qemu_get_thread_id());
327         g_string_append(buf, pid_placeholder + 2);
328         path = buf->str;
329     }
330 
331     return unix_listen(path, errp);
332 }
333 
334 static bool gdb_accept_tcp(int gdb_fd)
335 {
336     struct sockaddr_in sockaddr = {};
337     socklen_t len;
338     int fd;
339 
340     for (;;) {
341         len = sizeof(sockaddr);
342         fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
343         if (fd < 0 && errno != EINTR) {
344             perror("accept");
345             return false;
346         } else if (fd >= 0) {
347             qemu_set_cloexec(fd);
348             break;
349         }
350     }
351 
352     /* set short latency */
353     if (socket_set_nodelay(fd)) {
354         perror("setsockopt");
355         close(fd);
356         return false;
357     }
358 
359     gdb_accept_init(fd);
360     return true;
361 }
362 
363 static int gdbserver_open_port(int port, Error **errp)
364 {
365     struct sockaddr_in sockaddr;
366     int fd, ret;
367 
368     fd = socket(PF_INET, SOCK_STREAM, 0);
369     if (fd < 0) {
370         error_setg_errno(errp, errno, "Failed to create socket");
371         return -1;
372     }
373     qemu_set_cloexec(fd);
374 
375     socket_set_fast_reuse(fd);
376 
377     sockaddr.sin_family = AF_INET;
378     sockaddr.sin_port = htons(port);
379     sockaddr.sin_addr.s_addr = 0;
380     ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
381     if (ret < 0) {
382         error_setg_errno(errp, errno, "Failed to bind socket");
383         close(fd);
384         return -1;
385     }
386     ret = listen(fd, 1);
387     if (ret < 0) {
388         error_setg_errno(errp, errno, "Failed to listen to socket");
389         close(fd);
390         return -1;
391     }
392 
393     return fd;
394 }
395 
396 bool gdbserver_start(const char *port_or_path, Error **errp)
397 {
398     int port = g_ascii_strtoull(port_or_path, NULL, 10);
399     int gdb_fd;
400 
401     if (port > 0) {
402         gdb_fd = gdbserver_open_port(port, errp);
403     } else {
404         gdb_fd = gdbserver_open_socket(port_or_path, errp);
405     }
406 
407     if (gdb_fd < 0) {
408         return false;
409     }
410 
411     if (port > 0 && gdb_accept_tcp(gdb_fd)) {
412         return true;
413     } else if (gdb_accept_socket(gdb_fd)) {
414         gdbserver_user_state.socket_path = g_strdup(port_or_path);
415         return true;
416     }
417 
418     /* gone wrong */
419     close(gdb_fd);
420     error_setg(errp, "gdbstub: failed to accept connection");
421     return false;
422 }
423 
424 void gdbserver_fork_start(void)
425 {
426     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
427         return;
428     }
429     if (!gdbserver_user_state.fork_events ||
430             qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
431                             gdbserver_user_state.fork_sockets) < 0) {
432         gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
433         return;
434     }
435     gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
436     gdbserver_user_state.fork_peer_pid = getpid();
437     gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
438 }
439 
440 static void disable_gdbstub(CPUState *thread_cpu)
441 {
442     CPUState *cpu;
443 
444     close(gdbserver_user_state.fd);
445     gdbserver_user_state.fd = -1;
446     CPU_FOREACH(cpu) {
447         cpu_breakpoint_remove_all(cpu, BP_GDB);
448         /* no cpu_watchpoint_remove_all for user-mode */
449         cpu_single_step(cpu, 0);
450     }
451     tb_flush(thread_cpu);
452 }
453 
454 void gdbserver_fork_end(CPUState *cpu, pid_t pid)
455 {
456     char b;
457     int fd;
458 
459     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
460         return;
461     }
462 
463     if (pid == -1) {
464         if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
465             g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
466             close(gdbserver_user_state.fork_sockets[0]);
467             close(gdbserver_user_state.fork_sockets[1]);
468         }
469         return;
470     }
471 
472     if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
473         if (pid == 0) {
474             disable_gdbstub(cpu);
475         }
476         return;
477     }
478 
479     if (pid == 0) {
480         close(gdbserver_user_state.fork_sockets[0]);
481         fd = gdbserver_user_state.fork_sockets[1];
482         g_assert(gdbserver_state.process_num == 1);
483         g_assert(gdbserver_state.processes[0].pid ==
484                      gdbserver_user_state.fork_peer_pid);
485         g_assert(gdbserver_state.processes[0].attached);
486         gdbserver_state.processes[0].pid = getpid();
487     } else {
488         close(gdbserver_user_state.fork_sockets[1]);
489         fd = gdbserver_user_state.fork_sockets[0];
490         gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
491         gdbserver_user_state.fork_peer_pid = pid;
492         gdbserver_user_state.fork_peer_tid = pid;
493 
494         if (!gdbserver_state.allow_stop_reply) {
495             goto fail;
496         }
497         g_string_printf(gdbserver_state.str_buf,
498                         "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
499                         gdb_target_signal_to_gdb(gdb_target_sigtrap()),
500                         pid, pid, (int)getpid(), qemu_get_thread_id());
501         gdb_put_strbuf();
502     }
503 
504     gdbserver_state.state = RS_IDLE;
505     gdbserver_state.allow_stop_reply = false;
506     gdbserver_user_state.running_state = 0;
507     for (;;) {
508         switch (gdbserver_user_state.fork_state) {
509         case GDB_FORK_ENABLED:
510             if (gdbserver_user_state.running_state) {
511                 close(fd);
512                 return;
513             }
514             QEMU_FALLTHROUGH;
515         case GDB_FORK_ACTIVE:
516             if (read(gdbserver_user_state.fd, &b, 1) != 1) {
517                 goto fail;
518             }
519             gdb_read_byte(b);
520             break;
521         case GDB_FORK_DEACTIVATING:
522             b = GDB_FORK_ACTIVATE;
523             if (write(fd, &b, 1) != 1) {
524                 goto fail;
525             }
526             gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
527             break;
528         case GDB_FORK_INACTIVE:
529             if (read(fd, &b, 1) != 1) {
530                 goto fail;
531             }
532             switch (b) {
533             case GDB_FORK_ACTIVATE:
534                 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
535                 break;
536             case GDB_FORK_ENABLE:
537                 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
538                 break;
539             case GDB_FORK_DISABLE:
540                 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
541                 break;
542             default:
543                 g_assert_not_reached();
544             }
545             break;
546         case GDB_FORK_ENABLING:
547             b = GDB_FORK_DISABLE;
548             if (write(fd, &b, 1) != 1) {
549                 goto fail;
550             }
551             gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
552             break;
553         case GDB_FORK_DISABLING:
554             b = GDB_FORK_ENABLE;
555             if (write(fd, &b, 1) != 1) {
556                 goto fail;
557             }
558             gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
559             break;
560         case GDB_FORK_DISABLED:
561             close(fd);
562             disable_gdbstub(cpu);
563             return;
564         default:
565             g_assert_not_reached();
566         }
567     }
568 
569 fail:
570     close(fd);
571     if (pid == 0) {
572         disable_gdbstub(cpu);
573     }
574 }
575 
576 void gdb_handle_query_supported_user(const char *gdb_supported)
577 {
578     if (strstr(gdb_supported, "fork-events+")) {
579         gdbserver_user_state.fork_events = true;
580     }
581     g_string_append(gdbserver_state.str_buf, ";fork-events+");
582 }
583 
584 bool gdb_handle_set_thread_user(uint32_t pid, uint32_t tid)
585 {
586     if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE &&
587             pid == gdbserver_user_state.fork_peer_pid &&
588             tid == gdbserver_user_state.fork_peer_tid) {
589         gdbserver_user_state.fork_state = GDB_FORK_DEACTIVATING;
590         gdb_put_packet("OK");
591         return true;
592     }
593     return false;
594 }
595 
596 bool gdb_handle_detach_user(uint32_t pid)
597 {
598     bool enable;
599 
600     if (gdbserver_user_state.fork_state == GDB_FORK_ACTIVE) {
601         enable = pid == gdbserver_user_state.fork_peer_pid;
602         if (enable || pid == getpid()) {
603             gdbserver_user_state.fork_state = enable ? GDB_FORK_ENABLING :
604                                                        GDB_FORK_DISABLING;
605             gdb_put_packet("OK");
606             return true;
607         }
608     }
609     return false;
610 }
611 
612 /*
613  * Execution state helpers
614  */
615 
616 void gdb_handle_query_attached(GArray *params, void *user_ctx)
617 {
618     gdb_put_packet("0");
619 }
620 
621 void gdb_continue(void)
622 {
623     gdbserver_user_state.running_state = 1;
624     trace_gdbstub_op_continue();
625 }
626 
627 /*
628  * Resume execution, for user-mode emulation it's equivalent to
629  * gdb_continue.
630  */
631 int gdb_continue_partial(char *newstates)
632 {
633     CPUState *cpu;
634     int res = 0;
635     /*
636      * This is not exactly accurate, but it's an improvement compared to the
637      * previous situation, where only one CPU would be single-stepped.
638      */
639     CPU_FOREACH(cpu) {
640         if (newstates[cpu->cpu_index] == 's') {
641             trace_gdbstub_op_stepping(cpu->cpu_index);
642             cpu_single_step(cpu, gdbserver_state.sstep_flags);
643         }
644     }
645     gdbserver_user_state.running_state = 1;
646     return res;
647 }
648 
649 /*
650  * Memory access helpers
651  */
652 int gdb_target_memory_rw_debug(CPUState *cpu, hwaddr addr,
653                                uint8_t *buf, int len, bool is_write)
654 {
655     CPUClass *cc;
656 
657     cc = CPU_GET_CLASS(cpu);
658     if (cc->memory_rw_debug) {
659         return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
660     }
661     return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
662 }
663 
664 /*
665  * cpu helpers
666  */
667 
668 unsigned int gdb_get_max_cpus(void)
669 {
670     CPUState *cpu;
671     unsigned int max_cpus = 1;
672 
673     CPU_FOREACH(cpu) {
674         max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
675     }
676 
677     return max_cpus;
678 }
679 
680 /* replay not supported for user-mode */
681 bool gdb_can_reverse(void)
682 {
683     return false;
684 }
685 
686 /*
687  * Break/Watch point helpers
688  */
689 
690 bool gdb_supports_guest_debug(void)
691 {
692     /* user-mode == TCG == supported */
693     return true;
694 }
695 
696 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len)
697 {
698     CPUState *cpu;
699     int err = 0;
700 
701     switch (type) {
702     case GDB_BREAKPOINT_SW:
703     case GDB_BREAKPOINT_HW:
704         CPU_FOREACH(cpu) {
705             err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
706             if (err) {
707                 break;
708             }
709         }
710         return err;
711     default:
712         /* user-mode doesn't support watchpoints */
713         return -ENOSYS;
714     }
715 }
716 
717 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len)
718 {
719     CPUState *cpu;
720     int err = 0;
721 
722     switch (type) {
723     case GDB_BREAKPOINT_SW:
724     case GDB_BREAKPOINT_HW:
725         CPU_FOREACH(cpu) {
726             err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
727             if (err) {
728                 break;
729             }
730         }
731         return err;
732     default:
733         /* user-mode doesn't support watchpoints */
734         return -ENOSYS;
735     }
736 }
737 
738 void gdb_breakpoint_remove_all(CPUState *cs)
739 {
740     cpu_breakpoint_remove_all(cs, BP_GDB);
741 }
742 
743 /*
744  * For user-mode syscall support we send the system call immediately
745  * and then return control to gdb for it to process the syscall request.
746  * Since the protocol requires that gdb hands control back to us
747  * using a "here are the results" F packet, we don't need to check
748  * gdb_handlesig's return value (which is the signal to deliver if
749  * execution was resumed via a continue packet).
750  */
751 void gdb_syscall_handling(const char *syscall_packet)
752 {
753     gdb_put_packet(syscall_packet);
754     gdb_handlesig(gdbserver_state.c_cpu, 0, NULL, NULL, 0);
755 }
756 
757 static bool should_catch_syscall(int num)
758 {
759     if (gdbserver_user_state.catch_all_syscalls) {
760         return true;
761     }
762     if (num < 0 || num >= GDB_NR_SYSCALLS) {
763         return false;
764     }
765     return test_bit(num, gdbserver_user_state.catch_syscalls_mask);
766 }
767 
768 void gdb_syscall_entry(CPUState *cs, int num)
769 {
770     if (should_catch_syscall(num)) {
771         g_autofree char *reason = g_strdup_printf("syscall_entry:%x;", num);
772         gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
773     }
774 }
775 
776 void gdb_syscall_return(CPUState *cs, int num)
777 {
778     if (should_catch_syscall(num)) {
779         g_autofree char *reason = g_strdup_printf("syscall_return:%x;", num);
780         gdb_handlesig(cs, gdb_target_sigtrap(), reason, NULL, 0);
781     }
782 }
783 
784 void gdb_handle_set_catch_syscalls(GArray *params, void *user_ctx)
785 {
786     const char *param = gdb_get_cmd_param(params, 0)->data;
787     GDBSyscallsMask catch_syscalls_mask;
788     bool catch_all_syscalls;
789     unsigned int num;
790     const char *p;
791 
792     /* "0" means not catching any syscalls. */
793     if (strcmp(param, "0") == 0) {
794         gdbserver_user_state.catch_all_syscalls = false;
795         memset(gdbserver_user_state.catch_syscalls_mask, 0,
796                sizeof(gdbserver_user_state.catch_syscalls_mask));
797         gdb_put_packet("OK");
798         return;
799     }
800 
801     /* "1" means catching all syscalls. */
802     if (strcmp(param, "1") == 0) {
803         gdbserver_user_state.catch_all_syscalls = true;
804         gdb_put_packet("OK");
805         return;
806     }
807 
808     /*
809      * "1;..." means catching only the specified syscalls.
810      * The syscall list must not be empty.
811      */
812     if (param[0] == '1' && param[1] == ';') {
813         catch_all_syscalls = false;
814         memset(catch_syscalls_mask, 0, sizeof(catch_syscalls_mask));
815         for (p = &param[2];; p++) {
816             if (qemu_strtoui(p, &p, 16, &num) || (*p && *p != ';')) {
817                 goto err;
818             }
819             if (num >= GDB_NR_SYSCALLS) {
820                 /*
821                  * Fall back to reporting all syscalls. Reporting extra
822                  * syscalls is inefficient, but the spec explicitly allows it.
823                  * Keep parsing in case there is a syntax error ahead.
824                  */
825                 catch_all_syscalls = true;
826             } else {
827                 set_bit(num, catch_syscalls_mask);
828             }
829             if (!*p) {
830                 break;
831             }
832         }
833         gdbserver_user_state.catch_all_syscalls = catch_all_syscalls;
834         if (!catch_all_syscalls) {
835             memcpy(gdbserver_user_state.catch_syscalls_mask,
836                    catch_syscalls_mask, sizeof(catch_syscalls_mask));
837         }
838         gdb_put_packet("OK");
839         return;
840     }
841 
842 err:
843     gdb_put_packet("E00");
844 }
845 
846 void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
847 {
848     unsigned long offset, len;
849     uint8_t *siginfo_offset;
850 
851     offset = gdb_get_cmd_param(params, 0)->val_ul;
852     len = gdb_get_cmd_param(params, 1)->val_ul;
853 
854     if (offset + len > gdbserver_user_state.siginfo_len) {
855         /* Invalid offset and/or requested length. */
856         gdb_put_packet("E01");
857         return;
858     }
859 
860     siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset;
861 
862     /* Reply */
863     g_string_assign(gdbserver_state.str_buf, "l");
864     gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
865     gdb_put_packet_binary(gdbserver_state.str_buf->str,
866                           gdbserver_state.str_buf->len, true);
867 }
868