From: Sergio Durigan Junior <sergiodj@redhat.com>
To: GDB Patches <gdb-patches@sourceware.org>
Cc: Pedro Alves <palves@redhat.com>, Tom Tromey <tom@tromey.com>,
Kevin Buettner <kevinb@redhat.com>,
Sergio Durigan Junior <sergiodj@redhat.com>
Subject: [PATCH v2 1/5] Introduce scoped_pipe.h
Date: Tue, 17 Mar 2020 11:47:15 -0400 [thread overview]
Message-ID: <20200317154719.2078283-2-sergiodj@redhat.com> (raw)
In-Reply-To: <20200317154719.2078283-1-sergiodj@redhat.com>
This simple patch introduces gdbsupport/scoped_pipe.h, which is based
on gdbsupport/scoped_fd.h. When the object is instantiated, a pipe is
created using 'gdb_pipe_cloexec'. There are two methods (get_read_end
and get_write_end) that allow the user to obtain the read/write ends
of the pipe (no more messing with [0] and [1]), and when the object is
destroyed, the pipe is closed (both ends).
gdb/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* unittests/scoped_pipe-selftests.c: New file.
gdbsupport/ChangeLog:
yyyy-mm-dd Sergio Durigan Junior <sergiodj@redhat.com>
* scoped_pipe.h: New file.
---
gdb/Makefile.in | 1 +
gdb/unittests/scoped_pipe-selftests.c | 95 +++++++++++++++++++++++++++
gdbsupport/scoped_pipe.h | 63 ++++++++++++++++++
3 files changed, 159 insertions(+)
create mode 100644 gdb/unittests/scoped_pipe-selftests.c
create mode 100644 gdbsupport/scoped_pipe.h
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 0c331af4bf..458863b01c 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -447,6 +447,7 @@ SELFTESTS_SRCS = \
unittests/rsp-low-selftests.c \
unittests/scoped_fd-selftests.c \
unittests/scoped_mmap-selftests.c \
+ unittests/scoped_pipe-selftests.c \
unittests/scoped_restore-selftests.c \
unittests/string_view-selftests.c \
unittests/style-selftests.c \
diff --git a/gdb/unittests/scoped_pipe-selftests.c b/gdb/unittests/scoped_pipe-selftests.c
new file mode 100644
index 0000000000..14abbb1b7b
--- /dev/null
+++ b/gdb/unittests/scoped_pipe-selftests.c
@@ -0,0 +1,95 @@
+/* Self tests for scoped_pipe for GDB, the GNU debugger.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+
+#include "gdbsupport/scoped_pipe.h"
+#include "gdbsupport/selftest.h"
+
+namespace selftests {
+namespace scoped_pipe {
+
+/* Test that the pipe is correctly created. */
+
+static void
+test_create ()
+{
+ ::scoped_pipe spipe;
+
+ SELF_CHECK (spipe.get_read_end () > 0);
+ SELF_CHECK (spipe.get_write_end () > 0);
+}
+
+/* Test that we can write and read from the pipe. */
+
+static void
+test_transmission ()
+{
+ int foo = 123;
+ ::scoped_pipe spipe;
+
+ /* Write to the pipe. */
+ {
+ ssize_t writeret;
+
+ do
+ {
+ writeret = write (spipe.get_write_end (), &foo, sizeof (foo));
+ }
+ while (writeret < 0 && (errno == EAGAIN || errno == EINTR));
+
+ SELF_CHECK (writeret > 0);
+ }
+
+ /* Read from the pipe, and check if the value read is the same as
+ the one that was written. */
+ {
+ ssize_t readret;
+ int read_foo;
+
+ do
+ {
+ readret = read (spipe.get_read_end (), &read_foo, sizeof (read_foo));
+ }
+ while (readret < 0 && (errno == EAGAIN || errno == EINTR));
+
+ SELF_CHECK (readret > 0);
+
+ SELF_CHECK (read_foo == foo);
+ }
+}
+
+/* Run selftests. */
+static void
+run_tests ()
+{
+ test_create ();
+ test_transmission ();
+}
+
+} /* namespace scoped_pipe */
+} /* namespace selftests */
+
+void _initialize_scoped_pipe_selftests ();
+void
+_initialize_scoped_pipe_selftests ()
+{
+ selftests::register_test ("scoped_pipe",
+ selftests::scoped_pipe::run_tests);
+}
diff --git a/gdbsupport/scoped_pipe.h b/gdbsupport/scoped_pipe.h
new file mode 100644
index 0000000000..5180df80ae
--- /dev/null
+++ b/gdbsupport/scoped_pipe.h
@@ -0,0 +1,63 @@
+/* scoped_pipe, automatically close a pipe.
+
+ Copyright (C) 2020 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef GDBSUPPORT_SCOPED_PIPE_H
+#define GDBSUPPORT_SCOPED_PIPE_H
+
+#include <unistd.h>
+#include "filestuff.h"
+
+/* A smart-pointer-like class to automatically close a pipe. */
+
+class scoped_pipe
+{
+public:
+ scoped_pipe ()
+ {
+ if (gdb_pipe_cloexec (m_pipe) < 0)
+ error (_("gdb_pipe_cloexec: %s"), safe_strerror (errno));
+ }
+
+ ~scoped_pipe ()
+ {
+ if (m_pipe[0] >= 0)
+ close (m_pipe[0]);
+ if (m_pipe[1] >= 0)
+ close (m_pipe[1]);
+ }
+
+ DISABLE_COPY_AND_ASSIGN (scoped_pipe);
+
+ /* Get the read end of the pipe. */
+ int get_read_end () const noexcept
+ {
+ return m_pipe[0];
+ }
+
+ /* Get the write end of the pipe. */
+ int get_write_end () const noexcept
+ {
+ return m_pipe[1];
+ }
+
+private:
+ int m_pipe[2];
+};
+
+#endif /* ! GDBSUPPORT_SCOPED_PIPE_H */
--
2.24.1
next prev parent reply other threads:[~2020-03-17 15:47 UTC|newest]
Thread overview: 98+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-19 3:29 [PATCH] Improve ptrace-error detection on Linux targets Sergio Durigan Junior
2019-08-19 9:10 ` Ruslan Kabatsayev
2019-08-19 13:47 ` Sergio Durigan Junior
2019-08-19 14:57 ` Ruslan Kabatsayev
2019-08-19 16:30 ` Christian Biesinger via gdb-patches
2019-08-19 17:04 ` Sergio Durigan Junior
2019-08-19 14:33 ` Eli Zaretskii
2019-08-25 22:38 ` Sergio Durigan Junior
2019-08-19 18:26 ` Pedro Alves
2019-08-25 22:40 ` Sergio Durigan Junior
2019-08-26 18:32 ` [PATCH v2] " Sergio Durigan Junior
2019-08-26 18:35 ` Christian Biesinger via gdb-patches
2019-08-26 20:51 ` Sergio Durigan Junior
2019-08-26 18:44 ` Eli Zaretskii
2019-08-29 14:40 ` Pedro Alves
2019-08-29 19:27 ` Sergio Durigan Junior
2019-08-29 19:48 ` Sergio Durigan Junior
2019-08-30 19:03 ` Pedro Alves
2019-08-30 19:51 ` [PATCH] Remove "\nError: " suffix from nat/fork-inferior.c:trace_start_error warning message Sergio Durigan Junior
2019-08-30 19:54 ` Pedro Alves
2019-08-30 21:06 ` Sergio Durigan Junior
2019-08-30 12:45 ` [PATCH v2] Improve ptrace-error detection on Linux targets Pedro Alves
2019-09-04 19:21 ` Sergio Durigan Junior
2019-09-04 19:31 ` Sergio Durigan Junior
2019-09-04 19:58 ` Pedro Alves
2019-09-04 20:21 ` Sergio Durigan Junior
2019-09-04 20:35 ` Pedro Alves
2019-09-04 20:56 ` Sergio Durigan Junior
2019-09-04 21:23 ` Pedro Alves
2019-09-04 21:36 ` Sergio Durigan Junior
2019-09-05 12:19 ` Pedro Alves
2019-09-05 17:58 ` Sergio Durigan Junior
2019-08-30 12:47 ` Pedro Alves
2019-08-30 14:07 ` Eli Zaretskii
2019-08-30 19:44 ` Sergio Durigan Junior
2019-09-04 19:54 ` [PATCH v3] " Sergio Durigan Junior
2019-09-05 17:04 ` Eli Zaretskii
2019-09-11 1:11 ` [PATCH v4] " Sergio Durigan Junior
2019-09-12 12:39 ` Eli Zaretskii
2019-09-12 18:29 ` Sergio Durigan Junior
2019-09-24 20:40 ` Tom Tromey
2019-09-25 14:14 ` Sergio Durigan Junior
2019-09-25 22:04 ` Tom Tromey
2019-09-26 4:22 ` Sergio Durigan Junior
2019-09-26 4:22 ` [PATCH v5] " Sergio Durigan Junior
2019-09-26 17:32 ` Tom Tromey
2019-09-26 17:48 ` Pedro Alves
2019-09-26 17:51 ` Sergio Durigan Junior
2019-09-26 18:14 ` Pedro Alves
2019-09-26 18:25 ` Sergio Durigan Junior
2019-09-26 17:50 ` Sergio Durigan Junior
2019-09-26 18:13 ` Pedro Alves
2019-09-26 18:23 ` Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 0/6] Improve ptrace-error detection Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 5/6] Document Linux-specific possible ptrace restrictions Sergio Durigan Junior
2020-02-26 21:00 ` Ruslan Kabatsayev
2020-02-26 22:08 ` Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 2/6] Don't reset errno/bfd_error on 'throw_perror_with_name' Sergio Durigan Junior
2020-02-28 15:29 ` Tom Tromey
2020-02-28 16:36 ` Sergio Durigan Junior
2020-02-28 18:58 ` Tom Tromey
2020-02-28 19:50 ` Sergio Durigan Junior
2020-02-28 20:06 ` Pedro Alves
2020-02-28 20:35 ` Sergio Durigan Junior
2020-02-28 21:11 ` Pedro Alves
2020-03-02 20:07 ` Sergio Durigan Junior
2020-02-28 19:49 ` Pedro Alves
2020-02-28 20:01 ` Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 3/6] Expand 'fork_inferior' to check whether 'traceme_fun' succeeded Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 6/6] Fix comment for 'gdb_dlopen' Sergio Durigan Junior
2020-02-26 20:23 ` Christian Biesinger via gdb-patches
2020-02-26 20:49 ` Sergio Durigan Junior
2020-02-28 15:21 ` Tom Tromey
2020-02-28 16:05 ` Sergio Durigan Junior
2020-02-26 20:06 ` [PATCH 1/6] Introduce scoped_pipe.h Sergio Durigan Junior
2020-02-28 15:23 ` Tom Tromey
2020-02-28 16:08 ` Sergio Durigan Junior
2020-02-28 18:57 ` Tom Tromey
2020-02-28 19:48 ` Sergio Durigan Junior
2020-02-28 19:20 ` Pedro Alves
2020-02-28 19:47 ` Sergio Durigan Junior
2020-02-28 20:07 ` Pedro Alves
2020-02-26 20:06 ` [PATCH 4/6] Extend GNU/Linux to check for ptrace error Sergio Durigan Junior
[not found] ` <87v9nh3yme.fsf@redhat.com>
2020-03-15 4:21 ` [PATCH 0/6] Improve ptrace-error detection Sergio Durigan Junior
2020-03-15 21:16 ` Kevin Buettner
2020-03-17 15:47 ` [PATCH v2 0/5] " Sergio Durigan Junior
2020-03-17 15:47 ` Sergio Durigan Junior [this message]
2020-03-17 15:47 ` [PATCH v2 2/5] Don't reset errno/bfd_error on 'throw_perror_with_name' Sergio Durigan Junior
2020-03-27 18:20 ` Pedro Alves
2020-03-17 15:47 ` [PATCH v2 3/5] Expand 'fork_inferior' to check whether 'traceme_fun' succeeded Sergio Durigan Junior
2020-03-27 4:14 ` Kevin Buettner
2020-03-27 13:06 ` Pedro Alves
2020-03-17 15:47 ` [PATCH v2 4/5] Extend GNU/Linux to check for ptrace error Sergio Durigan Junior
2020-03-27 15:28 ` Pedro Alves
2020-03-27 17:02 ` Kevin Buettner
2020-03-17 15:47 ` [PATCH v2 5/5] Document Linux-specific possible ptrace restrictions Sergio Durigan Junior
2020-03-20 0:53 ` [PATCH v2 0/5] Improve ptrace-error detection Kevin Buettner
2020-03-24 18:23 ` Sergio Durigan Junior
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=20200317154719.2078283-2-sergiodj@redhat.com \
--to=sergiodj@redhat.com \
--cc=gdb-patches@sourceware.org \
--cc=kevinb@redhat.com \
--cc=palves@redhat.com \
--cc=tom@tromey.com \
/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).