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