xref: /openbmc/qemu/gdbstub/user.c (revision 76bccf3c)
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 "gdbstub/enums.h"
22 #include "hw/core/cpu.h"
23 #include "trace.h"
24 #include "internals.h"
25 
26 #define GDB_NR_SYSCALLS 1024
27 typedef unsigned long GDBSyscallsMask[BITS_TO_LONGS(GDB_NR_SYSCALLS)];
28 
29 /*
30  * Forked child talks to its parent in order to let GDB enforce the
31  * follow-fork-mode. This happens inside a start_exclusive() section, so that
32  * the other threads, which may be forking too, do not interfere. The
33  * implementation relies on GDB not sending $vCont until it has detached
34  * either from the parent (follow-fork-mode child) or from the child
35  * (follow-fork-mode parent).
36  *
37  * The parent and the child share the GDB socket; at any given time only one
38  * of them is allowed to use it, as is reflected in the respective fork_state.
39  * This is negotiated via the fork_sockets pair as a reaction to $Hg.
40  *
41  * Below is a short summary of the possible state transitions:
42  *
43  *     ENABLED                     : Terminal state.
44  *     DISABLED                    : Terminal state.
45  *     ACTIVE                      : Parent initial state.
46  *     INACTIVE                    : Child initial state.
47  *     ACTIVE       -> DEACTIVATING: On $Hg.
48  *     ACTIVE       -> ENABLING    : On $D.
49  *     ACTIVE       -> DISABLING   : On $D.
50  *     ACTIVE       -> DISABLED    : On communication error.
51  *     DEACTIVATING -> INACTIVE    : On gdb_read_byte() return.
52  *     DEACTIVATING -> DISABLED    : On communication error.
53  *     INACTIVE     -> ACTIVE      : On $Hg in the peer.
54  *     INACTIVE     -> ENABLE      : On $D in the peer.
55  *     INACTIVE     -> DISABLE     : On $D in the peer.
56  *     INACTIVE     -> DISABLED    : On communication error.
57  *     ENABLING     -> ENABLED     : On gdb_read_byte() return.
58  *     ENABLING     -> DISABLED    : On communication error.
59  *     DISABLING    -> DISABLED    : On gdb_read_byte() return.
60  */
61 enum GDBForkState {
62     /* Fully owning the GDB socket. */
63     GDB_FORK_ENABLED,
64     /* Working with the GDB socket; the peer is inactive. */
65     GDB_FORK_ACTIVE,
66     /* Handing off the GDB socket to the peer. */
67     GDB_FORK_DEACTIVATING,
68     /* The peer is working with the GDB socket. */
69     GDB_FORK_INACTIVE,
70     /* Asking the peer to close its GDB socket fd. */
71     GDB_FORK_ENABLING,
72     /* Asking the peer to take over, closing our GDB socket fd. */
73     GDB_FORK_DISABLING,
74     /* The peer has taken over, our GDB socket fd is closed. */
75     GDB_FORK_DISABLED,
76 };
77 
78 enum GDBForkMessage {
79     GDB_FORK_ACTIVATE = 'a',
80     GDB_FORK_ENABLE = 'e',
81     GDB_FORK_DISABLE = 'd',
82 };
83 
84 /* User-mode specific state */
85 typedef struct {
86     int fd;
87     char *socket_path;
88     int running_state;
89     /*
90      * Store syscalls mask without memory allocation in order to avoid
91      * implementing synchronization.
92      */
93     bool catch_all_syscalls;
94     GDBSyscallsMask catch_syscalls_mask;
95     bool fork_events;
96     enum GDBForkState fork_state;
97     int fork_sockets[2];
98     pid_t fork_peer_pid, fork_peer_tid;
99     uint8_t siginfo[MAX_SIGINFO_LENGTH];
100     unsigned long siginfo_len;
101 } GDBUserState;
102 
103 static GDBUserState gdbserver_user_state;
104 
105 int gdb_get_char(void)
106 {
107     uint8_t ch;
108     int ret;
109 
110     for (;;) {
111         ret = recv(gdbserver_user_state.fd, &ch, 1, 0);
112         if (ret < 0) {
113             if (errno == ECONNRESET) {
114                 gdbserver_user_state.fd = -1;
115             }
116             if (errno != EINTR) {
117                 return -1;
118             }
119         } else if (ret == 0) {
120             close(gdbserver_user_state.fd);
121             gdbserver_user_state.fd = -1;
122             return -1;
123         } else {
124             break;
125         }
126     }
127     return ch;
128 }
129 
130 bool gdb_got_immediate_ack(void)
131 {
132     int i;
133 
134     i = gdb_get_char();
135     if (i < 0) {
136         /* no response, continue anyway */
137         return true;
138     }
139 
140     if (i == '+') {
141         /* received correctly, continue */
142         return true;
143     }
144 
145     /* anything else, including '-' then try again */
146     return false;
147 }
148 
149 void gdb_put_buffer(const uint8_t *buf, int len)
150 {
151     int ret;
152 
153     while (len > 0) {
154         ret = send(gdbserver_user_state.fd, buf, len, 0);
155         if (ret < 0) {
156             if (errno != EINTR) {
157                 return;
158             }
159         } else {
160             buf += ret;
161             len -= ret;
162         }
163     }
164 }
165 
166 /* Tell the remote gdb that the process has exited.  */
167 void gdb_exit(int code)
168 {
169     char buf[4];
170 
171     if (!gdbserver_state.init) {
172         return;
173     }
174     if (gdbserver_user_state.socket_path) {
175         unlink(gdbserver_user_state.socket_path);
176     }
177     if (gdbserver_user_state.fd < 0) {
178         return;
179     }
180 
181     trace_gdbstub_op_exiting((uint8_t)code);
182 
183     if (gdbserver_state.allow_stop_reply) {
184         snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
185         gdb_put_packet(buf);
186         gdbserver_state.allow_stop_reply = false;
187     }
188 
189 }
190 
191 void gdb_qemu_exit(int code)
192 {
193     exit(code);
194 }
195 
196 int gdb_handlesig(CPUState *cpu, int sig, const char *reason, void *siginfo,
197                   int siginfo_len)
198 {
199     char buf[256];
200     int n;
201 
202     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
203         return sig;
204     }
205 
206     if (siginfo) {
207         /*
208          * Save target-specific siginfo.
209          *
210          * siginfo size, i.e. siginfo_len, is asserted at compile-time to fit in
211          * gdbserver_user_state.siginfo, usually in the source file calling
212          * gdb_handlesig. See, for instance, {linux,bsd}-user/signal.c.
213          */
214         memcpy(gdbserver_user_state.siginfo, siginfo, siginfo_len);
215         gdbserver_user_state.siginfo_len = siginfo_len;
216     }
217 
218     /* disable single step if it was enabled */
219     cpu_single_step(cpu, 0);
220     tb_flush(cpu);
221 
222     if (sig != 0) {
223         gdb_set_stop_cpu(cpu);
224         if (gdbserver_state.allow_stop_reply) {
225             g_string_printf(gdbserver_state.str_buf,
226                             "T%02xthread:", gdb_target_signal_to_gdb(sig));
227             gdb_append_thread_id(cpu, gdbserver_state.str_buf);
228             g_string_append_c(gdbserver_state.str_buf, ';');
229             if (reason) {
230                 g_string_append(gdbserver_state.str_buf, reason);
231             }
232             gdb_put_strbuf();
233             gdbserver_state.allow_stop_reply = false;
234         }
235     }
236     /*
237      * gdb_put_packet() might have detected that the peer terminated the
238      * connection.
239      */
240     if (gdbserver_user_state.fd < 0) {
241         return sig;
242     }
243 
244     sig = 0;
245     gdbserver_state.state = RS_IDLE;
246     gdbserver_user_state.running_state = 0;
247     while (gdbserver_user_state.running_state == 0) {
248         n = read(gdbserver_user_state.fd, buf, 256);
249         if (n > 0) {
250             int i;
251 
252             for (i = 0; i < n; i++) {
253                 gdb_read_byte(buf[i]);
254             }
255         } else {
256             /*
257              * XXX: Connection closed.  Should probably wait for another
258              * connection before continuing.
259              */
260             if (n == 0) {
261                 close(gdbserver_user_state.fd);
262             }
263             gdbserver_user_state.fd = -1;
264             return sig;
265         }
266     }
267     sig = gdbserver_state.signal;
268     gdbserver_state.signal = 0;
269     return sig;
270 }
271 
272 /* Tell the remote gdb that the process has exited due to SIG.  */
273 void gdb_signalled(CPUArchState *env, int sig)
274 {
275     char buf[4];
276 
277     if (!gdbserver_state.init || gdbserver_user_state.fd < 0 ||
278         !gdbserver_state.allow_stop_reply) {
279         return;
280     }
281 
282     snprintf(buf, sizeof(buf), "X%02x", gdb_target_signal_to_gdb(sig));
283     gdb_put_packet(buf);
284     gdbserver_state.allow_stop_reply = false;
285 }
286 
287 static void gdb_accept_init(int fd)
288 {
289     gdb_init_gdbserver_state();
290     gdb_create_default_process(&gdbserver_state);
291     gdbserver_state.processes[0].attached = true;
292     gdbserver_state.c_cpu = gdb_first_attached_cpu();
293     gdbserver_state.g_cpu = gdbserver_state.c_cpu;
294     gdbserver_user_state.fd = fd;
295 }
296 
297 static bool gdb_accept_socket(int gdb_fd)
298 {
299     int fd;
300 
301     for (;;) {
302         fd = accept(gdb_fd, NULL, NULL);
303         if (fd < 0 && errno != EINTR) {
304             perror("accept socket");
305             return false;
306         } else if (fd >= 0) {
307             qemu_set_cloexec(fd);
308             break;
309         }
310     }
311 
312     gdb_accept_init(fd);
313     return true;
314 }
315 
316 static int gdbserver_open_socket(const char *path)
317 {
318     struct sockaddr_un sockaddr = {};
319     int fd, ret;
320 
321     fd = socket(AF_UNIX, SOCK_STREAM, 0);
322     if (fd < 0) {
323         perror("create socket");
324         return -1;
325     }
326 
327     sockaddr.sun_family = AF_UNIX;
328     pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
329     ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
330     if (ret < 0) {
331         perror("bind socket");
332         close(fd);
333         return -1;
334     }
335     ret = listen(fd, 1);
336     if (ret < 0) {
337         perror("listen socket");
338         close(fd);
339         return -1;
340     }
341 
342     return fd;
343 }
344 
345 static bool gdb_accept_tcp(int gdb_fd)
346 {
347     struct sockaddr_in sockaddr = {};
348     socklen_t len;
349     int fd;
350 
351     for (;;) {
352         len = sizeof(sockaddr);
353         fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
354         if (fd < 0 && errno != EINTR) {
355             perror("accept");
356             return false;
357         } else if (fd >= 0) {
358             qemu_set_cloexec(fd);
359             break;
360         }
361     }
362 
363     /* set short latency */
364     if (socket_set_nodelay(fd)) {
365         perror("setsockopt");
366         close(fd);
367         return false;
368     }
369 
370     gdb_accept_init(fd);
371     return true;
372 }
373 
374 static int gdbserver_open_port(int port)
375 {
376     struct sockaddr_in sockaddr;
377     int fd, ret;
378 
379     fd = socket(PF_INET, SOCK_STREAM, 0);
380     if (fd < 0) {
381         perror("socket");
382         return -1;
383     }
384     qemu_set_cloexec(fd);
385 
386     socket_set_fast_reuse(fd);
387 
388     sockaddr.sin_family = AF_INET;
389     sockaddr.sin_port = htons(port);
390     sockaddr.sin_addr.s_addr = 0;
391     ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
392     if (ret < 0) {
393         perror("bind");
394         close(fd);
395         return -1;
396     }
397     ret = listen(fd, 1);
398     if (ret < 0) {
399         perror("listen");
400         close(fd);
401         return -1;
402     }
403 
404     return fd;
405 }
406 
407 int gdbserver_start(const char *port_or_path)
408 {
409     int port = g_ascii_strtoull(port_or_path, NULL, 10);
410     int gdb_fd;
411 
412     if (port > 0) {
413         gdb_fd = gdbserver_open_port(port);
414     } else {
415         gdb_fd = gdbserver_open_socket(port_or_path);
416     }
417 
418     if (gdb_fd < 0) {
419         return -1;
420     }
421 
422     if (port > 0 && gdb_accept_tcp(gdb_fd)) {
423         return 0;
424     } else if (gdb_accept_socket(gdb_fd)) {
425         gdbserver_user_state.socket_path = g_strdup(port_or_path);
426         return 0;
427     }
428 
429     /* gone wrong */
430     close(gdb_fd);
431     return -1;
432 }
433 
434 void gdbserver_fork_start(void)
435 {
436     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
437         return;
438     }
439     if (!gdbserver_user_state.fork_events ||
440             qemu_socketpair(AF_UNIX, SOCK_STREAM, 0,
441                             gdbserver_user_state.fork_sockets) < 0) {
442         gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
443         return;
444     }
445     gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
446     gdbserver_user_state.fork_peer_pid = getpid();
447     gdbserver_user_state.fork_peer_tid = qemu_get_thread_id();
448 }
449 
450 static void disable_gdbstub(CPUState *thread_cpu)
451 {
452     CPUState *cpu;
453 
454     close(gdbserver_user_state.fd);
455     gdbserver_user_state.fd = -1;
456     CPU_FOREACH(cpu) {
457         cpu_breakpoint_remove_all(cpu, BP_GDB);
458         /* no cpu_watchpoint_remove_all for user-mode */
459         cpu_single_step(cpu, 0);
460     }
461     tb_flush(thread_cpu);
462 }
463 
464 void gdbserver_fork_end(CPUState *cpu, pid_t pid)
465 {
466     char b;
467     int fd;
468 
469     if (!gdbserver_state.init || gdbserver_user_state.fd < 0) {
470         return;
471     }
472 
473     if (pid == -1) {
474         if (gdbserver_user_state.fork_state != GDB_FORK_DISABLED) {
475             g_assert(gdbserver_user_state.fork_state == GDB_FORK_INACTIVE);
476             close(gdbserver_user_state.fork_sockets[0]);
477             close(gdbserver_user_state.fork_sockets[1]);
478         }
479         return;
480     }
481 
482     if (gdbserver_user_state.fork_state == GDB_FORK_DISABLED) {
483         if (pid == 0) {
484             disable_gdbstub(cpu);
485         }
486         return;
487     }
488 
489     if (pid == 0) {
490         close(gdbserver_user_state.fork_sockets[0]);
491         fd = gdbserver_user_state.fork_sockets[1];
492         g_assert(gdbserver_state.process_num == 1);
493         g_assert(gdbserver_state.processes[0].pid ==
494                      gdbserver_user_state.fork_peer_pid);
495         g_assert(gdbserver_state.processes[0].attached);
496         gdbserver_state.processes[0].pid = getpid();
497     } else {
498         close(gdbserver_user_state.fork_sockets[1]);
499         fd = gdbserver_user_state.fork_sockets[0];
500         gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
501         gdbserver_user_state.fork_peer_pid = pid;
502         gdbserver_user_state.fork_peer_tid = pid;
503 
504         if (!gdbserver_state.allow_stop_reply) {
505             goto fail;
506         }
507         g_string_printf(gdbserver_state.str_buf,
508                         "T%02xfork:p%02x.%02x;thread:p%02x.%02x;",
509                         gdb_target_signal_to_gdb(gdb_target_sigtrap()),
510                         pid, pid, (int)getpid(), qemu_get_thread_id());
511         gdb_put_strbuf();
512     }
513 
514     gdbserver_state.state = RS_IDLE;
515     gdbserver_state.allow_stop_reply = false;
516     gdbserver_user_state.running_state = 0;
517     for (;;) {
518         switch (gdbserver_user_state.fork_state) {
519         case GDB_FORK_ENABLED:
520             if (gdbserver_user_state.running_state) {
521                 close(fd);
522                 return;
523             }
524             QEMU_FALLTHROUGH;
525         case GDB_FORK_ACTIVE:
526             if (read(gdbserver_user_state.fd, &b, 1) != 1) {
527                 goto fail;
528             }
529             gdb_read_byte(b);
530             break;
531         case GDB_FORK_DEACTIVATING:
532             b = GDB_FORK_ACTIVATE;
533             if (write(fd, &b, 1) != 1) {
534                 goto fail;
535             }
536             gdbserver_user_state.fork_state = GDB_FORK_INACTIVE;
537             break;
538         case GDB_FORK_INACTIVE:
539             if (read(fd, &b, 1) != 1) {
540                 goto fail;
541             }
542             switch (b) {
543             case GDB_FORK_ACTIVATE:
544                 gdbserver_user_state.fork_state = GDB_FORK_ACTIVE;
545                 break;
546             case GDB_FORK_ENABLE:
547                 gdbserver_user_state.fork_state = GDB_FORK_ENABLED;
548                 break;
549             case GDB_FORK_DISABLE:
550                 gdbserver_user_state.fork_state = GDB_FORK_DISABLED;
551                 break;
552             default:
553                 g_assert_not_reached();
554             }
555             break;
556         case GDB_FORK_ENABLING:
557             b = GDB_FORK_DISABLE;
558             if (write(fd, &b, 1) != 1) {
559                 goto fail;
560             }
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 
856 void gdb_handle_query_xfer_siginfo(GArray *params, void *user_ctx)
857 {
858     unsigned long offset, len;
859     uint8_t *siginfo_offset;
860 
861     offset = get_param(params, 0)->val_ul;
862     len = get_param(params, 1)->val_ul;
863 
864     if (offset + len > gdbserver_user_state.siginfo_len) {
865         /* Invalid offset and/or requested length. */
866         gdb_put_packet("E01");
867         return;
868     }
869 
870     siginfo_offset = (uint8_t *)gdbserver_user_state.siginfo + offset;
871 
872     /* Reply */
873     g_string_assign(gdbserver_state.str_buf, "l");
874     gdb_memtox(gdbserver_state.str_buf, (const char *)siginfo_offset, len);
875     gdb_put_packet_binary(gdbserver_state.str_buf->str,
876                           gdbserver_state.str_buf->len, true);
877 }
878