public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: John Baldwin <jhb@FreeBSD.org>
To: gdb-patches@sourceware.org
Subject: [PATCH v3 08/14] inf-ptrace: Support async targets in inf_ptrace_target::wait.
Date: Fri, 26 Nov 2021 17:14:47 -0800	[thread overview]
Message-ID: <20211127011453.74487-9-jhb@FreeBSD.org> (raw)
In-Reply-To: <20211127011453.74487-1-jhb@FreeBSD.org>

- Handle TARGET_WNOHANG by passing WNOHANG to waitpid and returning
  TARGET_WAITKIND_IGNORE if there are no events to report.

- Handle a race in async mode where SIGCHLD might signal the event
  pipe for an event that has already been reported.  If the event was
  the exit of the last child process, waitpid() will fail with ECHILD
  rather than returning a pid of 0.  For this case, return
  TARGET_WAITKIND_NO_RESUMED.
---
 gdb/inf-ptrace.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/gdb/inf-ptrace.c b/gdb/inf-ptrace.c
index d0663cfc6a4..1cec88d97aa 100644
--- a/gdb/inf-ptrace.c
+++ b/gdb/inf-ptrace.c
@@ -299,10 +299,14 @@ inf_ptrace_target::resume (ptid_t ptid, int step, enum gdb_signal signal)
 
 ptid_t
 inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
-			 target_wait_flags options)
+			 target_wait_flags target_options)
 {
   pid_t pid;
-  int status, save_errno;
+  int options, status, save_errno;
+
+  options = 0;
+  if (target_options & TARGET_WNOHANG)
+    options |= WNOHANG;
 
   do
     {
@@ -310,15 +314,32 @@ inf_ptrace_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
 
       do
 	{
-	  pid = waitpid (ptid.pid (), &status, 0);
+	  pid = waitpid (ptid.pid (), &status, options);
 	  save_errno = errno;
 	}
       while (pid == -1 && errno == EINTR);
 
       clear_sigint_trap ();
 
+      if (pid == 0)
+	{
+	  gdb_assert (target_options & TARGET_WNOHANG);
+	  ourstatus->set_ignore ();
+	  return minus_one_ptid;
+	}
+
       if (pid == -1)
 	{
+	  /* In async mode the SIGCHLD might have raced and triggered
+	     a check for an event that had already been reported.  If
+	     the event was the exit of the only remaining child,
+	     waitpid() will fail with ECHILD.  */
+	  if (ptid == minus_one_ptid && save_errno == ECHILD)
+	    {
+	      ourstatus->set_no_resumed ();
+	      return minus_one_ptid;
+	    }
+
 	  errno = save_errno;
 	  perror_with_name (_("Child process unexpectedly missing"));
 	}
-- 
2.33.0


  parent reply	other threads:[~2021-11-27  1:15 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-27  1:14 [PATCH v3 00/14] FreeBSD target async mode and related refactoring John Baldwin
2021-11-27  1:14 ` [PATCH v3 01/14] gdbsupport: Add an event-pipe class John Baldwin
2022-01-14 16:19   ` Tom Tromey
2021-11-27  1:14 ` [PATCH v3 02/14] gdb linux-nat: Convert linux_nat_event_pipe to the event_pipe class John Baldwin
2021-11-27  1:14 ` [PATCH v3 03/14] gdbserver linux-low: Convert linux_event_pipe " John Baldwin
2021-11-27  1:14 ` [PATCH v3 04/14] linux-nat: Don't enable async mode at the end of linux_nat_target::resume John Baldwin
2021-11-27  1:14 ` [PATCH v3 05/14] record: Don't enable async mode at the end of resume target methods John Baldwin
2021-11-27  1:14 ` [PATCH v3 06/14] do_target_wait_1: Clear TARGET_WNOHANG if the target isn't async John Baldwin
2021-11-27  1:14 ` [PATCH v3 07/14] inf-ptrace: Raise an error if waitpid() fails John Baldwin
2021-11-27  1:14 ` John Baldwin [this message]
2021-11-27  1:14 ` [PATCH v3 09/14] fbsd-nat: Implement async target support John Baldwin
2021-11-27  1:14 ` [PATCH v3 10/14] fbsd-nat: Include ptrace operation in error messages John Baldwin
2021-11-27  1:14 ` [PATCH v3 11/14] fbsd-nat: Various cleanups to the ::resume entry debug message John Baldwin
2021-11-27  1:14 ` [PATCH v3 12/14] fbsd-nat: Return nullptr rather than failing ::thread_name John Baldwin
2021-11-27  1:14 ` [PATCH v3 13/14] Enable async mode in the target in attach_cmd John Baldwin
2021-11-27  1:14 ` [PATCH v3 14/14] inf-ptrace: Add an event_pipe to be used for async mode in subclasses John Baldwin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211127011453.74487-9-jhb@FreeBSD.org \
    --to=jhb@freebsd.org \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).