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 v5 02/15] gdb linux-nat: Convert linux_nat_event_pipe to the event_pipe class.
Date: Fri, 21 Jan 2022 12:16:18 -0800	[thread overview]
Message-ID: <20220121201631.63530-3-jhb@FreeBSD.org> (raw)
In-Reply-To: <20220121201631.63530-1-jhb@FreeBSD.org>

Use event_pipe from gdbsupport in place of the existing file
descriptor array.
---
 gdb/linux-nat.c | 59 ++++++++++++++-----------------------------------
 1 file changed, 16 insertions(+), 43 deletions(-)

diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index 0cc6923ad02..db3585dfa23 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -48,6 +48,7 @@
 #include <fcntl.h>		/* for O_RDONLY */
 #include "inf-loop.h"
 #include "gdbsupport/event-loop.h"
+#include "gdbsupport/event-pipe.h"
 #include "event-top.h"
 #include <pwd.h>
 #include <sys/types.h>
@@ -111,11 +112,11 @@ and target events, so neither blocking waitpid nor sigsuspend are
 viable options.  Instead, we should asynchronously notify the GDB main
 event loop whenever there's an unprocessed event from the target.  We
 detect asynchronous target events by handling SIGCHLD signals.  To
-notify the event loop about target events, the self-pipe trick is used
---- a pipe is registered as waitable event source in the event loop,
+notify the event loop about target events, an event pipe is used
+--- the pipe is registered as waitable event source in the event loop,
 the event loop select/poll's on the read end of this pipe (as well on
-other event sources, e.g., stdin), and the SIGCHLD handler writes a
-byte to this pipe.  This is more portable than relying on
+other event sources, e.g., stdin), and the SIGCHLD handler marks the
+event pipe to raise an event.  This is more portable than relying on
 pselect/ppoll, since on kernels that lack those syscalls, libc
 emulates them with select/poll+sigprocmask, and that is racy
 (a.k.a. plain broken).
@@ -228,26 +229,18 @@ static int report_thread_events;
 
 /* Async mode support.  */
 
-/* The read/write ends of the pipe registered as waitable file in the
-   event loop.  */
-static int linux_nat_event_pipe[2] = { -1, -1 };
+/* The event pipe registered as a waitable file in the event loop.  */
+static event_pipe linux_nat_event_pipe;
 
 /* True if we're currently in async mode.  */
-#define linux_is_async_p() (linux_nat_event_pipe[0] != -1)
+#define linux_is_async_p() (linux_nat_event_pipe.is_open ())
 
 /* Flush the event pipe.  */
 
 static void
 async_file_flush (void)
 {
-  int ret;
-  char buf;
-
-  do
-    {
-      ret = read (linux_nat_event_pipe[0], &buf, 1);
-    }
-  while (ret >= 0 || (ret == -1 && errno == EINTR));
+  linux_nat_event_pipe.flush ();
 }
 
 /* Put something (anything, doesn't matter what, or how much) in event
@@ -257,21 +250,7 @@ async_file_flush (void)
 static void
 async_file_mark (void)
 {
-  int ret;
-
-  /* It doesn't really matter what the pipe contains, as long we end
-     up with something in it.  Might as well flush the previous
-     left-overs.  */
-  async_file_flush ();
-
-  do
-    {
-      ret = write (linux_nat_event_pipe[1], "+", 1);
-    }
-  while (ret == -1 && errno == EINTR);
-
-  /* Ignore EAGAIN.  If the pipe is full, the event loop will already
-     be awakened anyway.  */
+  linux_nat_event_pipe.mark ();
 }
 
 static int kill_lwp (int lwpid, int signo);
@@ -4202,7 +4181,7 @@ sigchld_handler (int signo)
     gdb_stdlog->write_async_safe ("sigchld\n", sizeof ("sigchld\n") - 1);
 
   if (signo == SIGCHLD
-      && linux_nat_event_pipe[0] != -1)
+      && linux_nat_event_pipe.is_open ())
     async_file_mark (); /* Let the event loop know that there are
 			   events to handle.  */
 
@@ -4234,19 +4213,13 @@ linux_async_pipe (int enable)
 
       if (enable)
 	{
-	  if (gdb_pipe_cloexec (linux_nat_event_pipe) == -1)
+	  if (!linux_nat_event_pipe.open ())
 	    internal_error (__FILE__, __LINE__,
 			    "creating event pipe failed.");
-
-	  fcntl (linux_nat_event_pipe[0], F_SETFL, O_NONBLOCK);
-	  fcntl (linux_nat_event_pipe[1], F_SETFL, O_NONBLOCK);
 	}
       else
 	{
-	  close (linux_nat_event_pipe[0]);
-	  close (linux_nat_event_pipe[1]);
-	  linux_nat_event_pipe[0] = -1;
-	  linux_nat_event_pipe[1] = -1;
+	  linux_nat_event_pipe.close ();
 	}
 
       restore_child_signals_mask (&prev_mask);
@@ -4258,7 +4231,7 @@ linux_async_pipe (int enable)
 int
 linux_nat_target::async_wait_fd ()
 {
-  return linux_nat_event_pipe[0];
+  return linux_nat_event_pipe.event_fd ();
 }
 
 /* target_async implementation.  */
@@ -4270,7 +4243,7 @@ linux_nat_target::async (int enable)
     {
       if (!linux_async_pipe (1))
 	{
-	  add_file_handler (linux_nat_event_pipe[0],
+	  add_file_handler (linux_nat_event_pipe.event_fd (),
 			    handle_target_event, NULL,
 			    "linux-nat");
 	  /* There may be pending events to handle.  Tell the event loop
@@ -4280,7 +4253,7 @@ linux_nat_target::async (int enable)
     }
   else
     {
-      delete_file_handler (linux_nat_event_pipe[0]);
+      delete_file_handler (linux_nat_event_pipe.event_fd ());
       linux_async_pipe (0);
     }
   return;
-- 
2.34.1


  parent reply	other threads:[~2022-01-21 20:16 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-21 20:16 [PATCH v5 00/15] FreeBSD target async mode and related refactoring John Baldwin
2022-01-21 20:16 ` [PATCH v5 01/15] gdbsupport: Add an event-pipe class John Baldwin
2022-01-21 20:16 ` John Baldwin [this message]
2022-01-21 20:16 ` [PATCH v5 03/15] gdbserver linux-low: Convert linux_event_pipe to the event_pipe class John Baldwin
2022-01-21 20:16 ` [PATCH v5 04/15] Enable async mode on supported targets in target_resume John Baldwin
2022-02-20 10:47   ` Joel Brobecker
2022-01-21 20:16 ` [PATCH v5 05/15] Don't enable async mode at the end of target ::resume methods John Baldwin
2022-02-20 10:49   ` Joel Brobecker
2022-01-21 20:16 ` [PATCH v5 06/15] do_target_wait_1: Clear TARGET_WNOHANG if the target isn't async John Baldwin
2022-02-22 22:38   ` Simon Marchi
2022-02-23  0:07     ` John Baldwin
2022-02-23  1:40       ` Simon Marchi
2022-02-23 15:51         ` Simon Marchi
2022-02-23 16:12           ` John Baldwin
2022-02-24  2:46             ` Simon Marchi
2022-02-24 19:02               ` John Baldwin
2022-02-24 19:16                 ` Simon Marchi
2022-01-21 20:16 ` [PATCH v5 07/15] inf-ptrace: Return an IGNORE event if waitpid() fails John Baldwin
2022-02-20 11:38   ` Joel Brobecker
2022-01-21 20:16 ` [PATCH v5 08/15] inf-ptrace: Support async targets in inf_ptrace_target::wait John Baldwin
2022-01-21 20:16 ` [PATCH v5 09/15] fbsd-nat: Implement async target support John Baldwin
2022-01-21 20:16 ` [PATCH v5 10/15] fbsd-nat: Include ptrace operation in error messages John Baldwin
2022-01-21 20:16 ` [PATCH v5 11/15] fbsd-nat: Various cleanups to the ::resume entry debug message John Baldwin
2022-01-21 20:16 ` [PATCH v5 12/15] fbsd-nat: Return nullptr rather than failing ::thread_name John Baldwin
2022-01-21 20:16 ` [PATCH v5 13/15] Enable async mode in the target in attach_cmd John Baldwin
2022-01-21 20:16 ` [PATCH v5 14/15] inf-ptrace: Add an event_pipe to be used for async mode in subclasses John Baldwin
2022-01-21 20:16 ` [PATCH v5 15/15] NEWS: Note that the FreeBSD async target supports async mode John Baldwin
2022-01-22  6:28   ` Eli Zaretskii

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=20220121201631.63530-3-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).