From patchwork Wed May 16 14:18:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 27284 Received: (qmail 8794 invoked by alias); 16 May 2018 14:18:36 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 8697 invoked by uid 89); 16 May 2018 14:18:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-24.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=xfree X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 16 May 2018 14:18:34 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1AA717D850 for ; Wed, 16 May 2018 14:18:33 +0000 (UTC) Received: from localhost.localdomain (ovpn04.gateway.prod.ext.ams2.redhat.com [10.39.146.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id BEBB910EE961 for ; Wed, 16 May 2018 14:18:32 +0000 (UTC) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH 02/10] remote: Eliminate remote_hostio_close_cleanup Date: Wed, 16 May 2018 15:18:22 +0100 Message-Id: <20180516141830.16859-3-palves@redhat.com> In-Reply-To: <20180516141830.16859-1-palves@redhat.com> References: <20180516141830.16859-1-palves@redhat.com> gdb/ChangeLog: yyyy-mm-dd Pedro Alves * 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(-) 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)