+/* Handle a GNU/Linux syscall trap wait response. If we see a syscall
+ event, check if the core is interested in it: if not, ignore the
+ event, and keep waiting; otherwise, we need to toggle the LWP's
+ syscall entry/exit status, since the ptrace event itself doesn't
+ indicate it, and report the trap to higher layers. */
+
+static int
+linux_handle_syscall_trap (struct lwp_info *lp, int stopping)
+{
+ struct target_waitstatus *ourstatus = &lp->waitstatus;
+ struct gdbarch *gdbarch = target_thread_architecture (lp->ptid);
+ int syscall_number = (int) gdbarch_get_syscall_number (gdbarch, lp->ptid);
+
+ if (stopping)
+ {
+ /* If we're stopping threads, there's a SIGSTOP pending, which
+ makes it so that the LWP reports an immediate syscall return,
+ followed by the SIGSTOP. Skip seeing that "return" using
+ PTRACE_CONT directly, and let stop_wait_callback collect the
+ SIGSTOP. Later when the thread is resumed, a new syscall
+ entry event. If we didn't do this (and returned 0), we'd
+ leave a syscall entry pending, and our caller, by using
+ PTRACE_CONT to collect the SIGSTOP, skips the syscall return
+ itself. Later, when the user re-resumes this LWP, we'd see
+ another syscall entry event and we'd mistake it for a return.
+
+ If stop_wait_callback didn't force the SIGSTOP out of the LWP
+ (leaving immediately with LWP->signalled set, without issuing
+ a PTRACE_CONT), it would still be problematic to leave this
+ syscall enter pending, as later when the thread is resumed,
+ it would then see the same syscall exit mentioned above,
+ followed by the delayed SIGSTOP, while the syscall didn't
+ actually get to execute. It seems it would be even more
+ confusing to the user. */
+
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LHST: ignoring syscall %d "
+ "for LWP %ld (stopping threads), "
+ "resuming with PTRACE_CONT for SIGSTOP\n",
+ syscall_number,
+ GET_LWP (lp->ptid));
+
+ lp->syscall_state = TARGET_WAITKIND_IGNORE;
+ ptrace (PTRACE_CONT, GET_LWP (lp->ptid), 0, 0);
+ return 1;
+ }
+
+ if (catch_syscall_enabled ())
+ {
+ /* Always update the entry/return state, even if this particular
+ syscall isn't interesting to the core now. In async mode,
+ the user could install a new catchpoint for this syscall
+ between syscall enter/return, and we'll need to know to
+ report a syscall return if that happens. */
+ lp->syscall_state = (lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
+ ? TARGET_WAITKIND_SYSCALL_RETURN
+ : TARGET_WAITKIND_SYSCALL_ENTRY);
+
+ if (catching_syscall_number (syscall_number))
+ {
+ /* Alright, an event to report. */
+ ourstatus->kind = lp->syscall_state;
+ ourstatus->value.syscall_number = syscall_number;
+
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LHST: stopping for %s of syscall %d"
+ " for LWP %ld\n",
+ lp->syscall_state
+ == TARGET_WAITKIND_SYSCALL_ENTRY
+ ? "entry" : "return",
+ syscall_number,
+ GET_LWP (lp->ptid));
+ return 0;
+ }
+
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LHST: ignoring %s of syscall %d "
+ "for LWP %ld\n",
+ lp->syscall_state == TARGET_WAITKIND_SYSCALL_ENTRY
+ ? "entry" : "return",
+ syscall_number,
+ GET_LWP (lp->ptid));
+ }
+ else
+ {
+ /* If we had been syscall tracing, and hence used PT_SYSCALL
+ before on this LWP, it could happen that the user removes all
+ syscall catchpoints before we get to process this event.
+ There are two noteworthy issues here:
+
+ - When stopped at a syscall entry event, resuming with
+ PT_STEP still resumes executing the syscall and reports a
+ syscall return.
+
+ - Only PT_SYSCALL catches syscall enters. If we last
+ single-stepped this thread, then this event can't be a
+ syscall enter. If we last single-stepped this thread, this
+ has to be a syscall exit.
+
+ The points above mean that the next resume, be it PT_STEP or
+ PT_CONTINUE, can not trigger a syscall trace event. */
+ if (debug_linux_nat)
+ fprintf_unfiltered (gdb_stdlog,
+ "LHST: caught syscall event "
+ "with no syscall catchpoints."
+ " %d for LWP %ld, ignoring\n",
+ syscall_number,
+ GET_LWP (lp->ptid));
+ lp->syscall_state = TARGET_WAITKIND_IGNORE;
+ }
+
+ /* The core isn't interested in this event. For efficiency, avoid
+ stopping all threads only to have the core resume them all again.
+ Since we're not stopping threads, if we're still syscall tracing
+ and not stepping, we can't use PTRACE_CONT here, as we'd miss any
+ subsequent syscall. Simply resume using the inf-ptrace layer,
+ which knows when to use PT_SYSCALL or PT_CONTINUE. */
+
+ /* Note that gdbarch_get_syscall_number may access registers, hence
+ fill a regcache. */
+ registers_changed ();
+ linux_ops->to_resume (linux_ops, pid_to_ptid (GET_LWP (lp->ptid)),
+ lp->step, TARGET_SIGNAL_0);
+ return 1;
+}
+