Patchwork [02/10] remote: Eliminate remote_hostio_close_cleanup

login
register
mail settings
Submitter Pedro Alves
Date May 16, 2018, 2:18 p.m.
Message ID <20180516141830.16859-3-palves@redhat.com>
Download mbox | patch
Permalink /patch/27284/
State New
Headers show

Comments

Pedro Alves - May 16, 2018, 2:18 p.m.
gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* remote.c (remote_hostio_close_cleanup): Delete.
	(class scoped_remote_fd): New.
	(remote_file_put, remote_file_get): Use it.
---
 gdb/remote.c | 89 ++++++++++++++++++++++++++++++++++++++++--------------------
 1 file changed, 60 insertions(+), 29 deletions(-)
Tom Tromey - May 16, 2018, 5:37 p.m.
>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:

Pedro> +  ~scoped_remote_fd ()
Pedro> +  {
Pedro> +    if (m_fd != -1)
Pedro> +      {
Pedro> +	int remote_errno;
Pedro> +	remote_hostio_close (find_target_at (process_stratum),
Pedro> +			     m_fd, &remote_errno);

The only danger here is if remote_hostio_close can throw.  However, this
was already a danger (in theory) before the patch -- there is a rule
(perhaps unwritten and/or unenforced) that one cannot throw during a
"do_cleanups".

I didn't look but some assurance that this can't happen would be good to
have.

Tom
Simon Marchi - May 18, 2018, 8:50 p.m.
On 2018-05-16 10:18 AM, Pedro Alves wrote:
> gdb/ChangeLog:
> yyyy-mm-dd  Pedro Alves  <palves@redhat.com>
> 
> 	* remote.c (remote_hostio_close_cleanup): Delete.
> 	(class scoped_remote_fd): New.
> 	(remote_file_put, remote_file_get): Use it.

LGTM, including catching the exception in the dtor.

Simon

Patch

diff --git a/gdb/remote.c b/gdb/remote.c
index ca72c1a1c2..bc4815c67e 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -12198,20 +12198,52 @@  remote_hostio_error (int errnum)
     error (_("Remote I/O error: %s"), safe_strerror (host_error));
 }
 
-static void
-remote_hostio_close_cleanup (void *opaque)
+/* A RAII wrapper around a remote file descriptor.  */
+
+class scoped_remote_fd
 {
-  int fd = *(int *) opaque;
-  int remote_errno;
+public:
+  explicit scoped_remote_fd (int fd)
+    : m_fd (fd)
+  {
+  }
 
-  remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno);
-}
+  ~scoped_remote_fd ()
+  {
+    if (m_fd != -1)
+      {
+	int remote_errno;
+	remote_hostio_close (find_target_at (process_stratum),
+			     m_fd, &remote_errno);
+      }
+  }
+
+  DISABLE_COPY_AND_ASSIGN (scoped_remote_fd);
+
+  /* Release ownership of the file descriptor, and return it.  */
+  int release () noexcept
+  {
+    int fd = m_fd;
+    m_fd = -1;
+    return fd;
+  }
+
+  /* Return the owned file descriptor.  */
+  int get () const noexcept
+  {
+    return m_fd;
+  }
+
+private:
+  /* The owned remote I/O file descriptor.  */
+  int m_fd;
+};
 
 void
 remote_file_put (const char *local_file, const char *remote_file, int from_tty)
 {
-  struct cleanup *back_to, *close_cleanup;
-  int retcode, fd, remote_errno, bytes, io_size;
+  struct cleanup *back_to;
+  int retcode, remote_errno, bytes, io_size;
   gdb_byte *buffer;
   int bytes_in_buffer;
   int saw_eof;
@@ -12225,11 +12257,12 @@  remote_file_put (const char *local_file, const char *remote_file, int from_tty)
   if (file == NULL)
     perror_with_name (local_file);
 
-  fd = remote_hostio_open (find_target_at (process_stratum), NULL,
-			   remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
-					 | FILEIO_O_TRUNC),
-			   0700, 0, &remote_errno);
-  if (fd == -1)
+  scoped_remote_fd fd
+    (remote_hostio_open (find_target_at (process_stratum), NULL,
+			 remote_file, (FILEIO_O_WRONLY | FILEIO_O_CREAT
+				       | FILEIO_O_TRUNC),
+			 0700, 0, &remote_errno));
+  if (fd.get () == -1)
     remote_hostio_error (remote_errno);
 
   /* Send up to this many bytes at once.  They won't all fit in the
@@ -12238,8 +12271,6 @@  remote_file_put (const char *local_file, const char *remote_file, int from_tty)
   buffer = (gdb_byte *) xmalloc (io_size);
   back_to = make_cleanup (xfree, buffer);
 
-  close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
-
   bytes_in_buffer = 0;
   saw_eof = 0;
   offset = 0;
@@ -12271,7 +12302,7 @@  remote_file_put (const char *local_file, const char *remote_file, int from_tty)
       bytes_in_buffer = 0;
 
       retcode = remote_hostio_pwrite (find_target_at (process_stratum),
-				      fd, buffer, bytes,
+				      fd.get (), buffer, bytes,
 				      offset, &remote_errno);
 
       if (retcode < 0)
@@ -12289,8 +12320,8 @@  remote_file_put (const char *local_file, const char *remote_file, int from_tty)
       offset += retcode;
     }
 
-  discard_cleanups (close_cleanup);
-  if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
+  if (remote_hostio_close (find_target_at (process_stratum),
+			   fd.release (), &remote_errno))
     remote_hostio_error (remote_errno);
 
   if (from_tty)
@@ -12301,8 +12332,8 @@  remote_file_put (const char *local_file, const char *remote_file, int from_tty)
 void
 remote_file_get (const char *remote_file, const char *local_file, int from_tty)
 {
-  struct cleanup *back_to, *close_cleanup;
-  int fd, remote_errno, bytes, io_size;
+  struct cleanup *back_to;
+  int remote_errno, bytes, io_size;
   gdb_byte *buffer;
   ULONGEST offset;
   struct remote_state *rs = get_remote_state ();
@@ -12310,10 +12341,11 @@  remote_file_get (const char *remote_file, const char *local_file, int from_tty)
   if (!rs->remote_desc)
     error (_("command can only be used with remote target"));
 
-  fd = remote_hostio_open (find_target_at (process_stratum), NULL,
-			   remote_file, FILEIO_O_RDONLY, 0, 0,
-			   &remote_errno);
-  if (fd == -1)
+  scoped_remote_fd fd
+    (remote_hostio_open (find_target_at (process_stratum), NULL,
+			 remote_file, FILEIO_O_RDONLY, 0, 0,
+			 &remote_errno));
+  if (fd.get () == -1)
     remote_hostio_error (remote_errno);
 
   gdb_file_up file = gdb_fopen_cloexec (local_file, "wb");
@@ -12326,13 +12358,12 @@  remote_file_get (const char *remote_file, const char *local_file, int from_tty)
   buffer = (gdb_byte *) xmalloc (io_size);
   back_to = make_cleanup (xfree, buffer);
 
-  close_cleanup = make_cleanup (remote_hostio_close_cleanup, &fd);
-
   offset = 0;
   while (1)
     {
       bytes = remote_hostio_pread (find_target_at (process_stratum),
-				   fd, buffer, io_size, offset, &remote_errno);
+				   fd.get (), buffer, io_size, offset,
+				   &remote_errno);
       if (bytes == 0)
 	/* Success, but no bytes, means end-of-file.  */
 	break;
@@ -12346,8 +12377,8 @@  remote_file_get (const char *remote_file, const char *local_file, int from_tty)
 	perror_with_name (local_file);
     }
 
-  discard_cleanups (close_cleanup);
-  if (remote_hostio_close (find_target_at (process_stratum), fd, &remote_errno))
+  if (remote_hostio_close (find_target_at (process_stratum),
+			   fd.release (), &remote_errno))
     remote_hostio_error (remote_errno);
 
   if (from_tty)