public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] [gdbsupport] Add gdb::{waitpid,read,write,close}
@ 2024-10-28 17:49 Tom de Vries
  2024-10-28 17:49 ` [PATCH 2/3] [gdb] Use gdb::waitpid more often Tom de Vries
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tom de Vries @ 2024-10-28 17:49 UTC (permalink / raw)
  To: gdb-patches

We have gdb::handle_eintr, which allows us to rewrite:
...
  ssize_t ret;
    do
      {
        errno = 0;
        ret = ::write (pipe[1], "+", 1);
      }
    while (ret == -1 && errno == EINTR);
...
into:
...
  ssize_t ret = gdb::handle_eintr (-1, ::write, pipe[1], "+", 1);
...

However, the call to write got a bit mangled, requiring effort to decode it
back to its original form.

Instead, add a new function gdb::write that allows us to write:
...
  ssize_t ret = gdb::write (pipe[1], "+", 1);
...

Likewise for waitpid, read and close.

Tested on x86_64-linux.
---
 gdb/cli/cli-cmds.c      |  2 +-
 gdb/nat/linux-waitpid.c |  2 +-
 gdbserver/netbsd-low.cc | 10 ++++----
 gdbsupport/Makefile.am  |  1 +
 gdbsupport/Makefile.in  |  1 +
 gdbsupport/eintr.cc     | 56 +++++++++++++++++++++++++++++++++++++++++
 gdbsupport/eintr.h      |  7 ++++++
 7 files changed, 72 insertions(+), 7 deletions(-)
 create mode 100644 gdbsupport/eintr.cc

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index 65ac7d6e7fb..230fcd549e1 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -923,7 +923,7 @@ run_under_shell (const char *arg, int from_tty)
 
   if (pid != -1)
     {
-      int ret = gdb::handle_eintr (-1, ::waitpid, pid, &status, 0);
+      int ret = gdb::waitpid (pid, &status, 0);
       if (ret == -1)
 	perror_with_name ("Cannot get status of shell command");
     }
diff --git a/gdb/nat/linux-waitpid.c b/gdb/nat/linux-waitpid.c
index 0ac2f9fb2b9..f4ae612c572 100644
--- a/gdb/nat/linux-waitpid.c
+++ b/gdb/nat/linux-waitpid.c
@@ -51,5 +51,5 @@ status_to_str (int status)
 int
 my_waitpid (int pid, int *status, int flags)
 {
-  return gdb::handle_eintr (-1, ::waitpid, pid, status, flags);
+  return gdb::waitpid (pid, status, flags);
 }
diff --git a/gdbserver/netbsd-low.cc b/gdbserver/netbsd-low.cc
index 4b58826e091..450fbcfb20d 100644
--- a/gdbserver/netbsd-low.cc
+++ b/gdbserver/netbsd-low.cc
@@ -222,7 +222,7 @@ netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus,
   int options = (target_options & TARGET_WNOHANG) ? WNOHANG : 0;
 
   pid_t pid
-    = gdb::handle_eintr (-1, ::waitpid, ptid.pid (), &status, options);
+    = gdb::waitpid (ptid.pid (), &status, options);
 
   if (pid == -1)
     perror_with_name (_("Child process unexpectedly missing"));
@@ -432,7 +432,7 @@ netbsd_process_target::kill (process_info *process)
     return -1;
 
   int status;
-  if (gdb::handle_eintr (-1, ::waitpid, pid, &status, 0) == -1)
+  if (gdb::waitpid (pid, &status, 0) == -1)
     return -1;
   mourn (process);
   return 0;
@@ -1123,15 +1123,15 @@ netbsd_qxfer_libraries_svr4 (const pid_t pid, const char *annex,
 static bool
 elf_64_file_p (const char *file)
 {
-  int fd = gdb::handle_eintr (-1, ::open, file, O_RDONLY);
+  int fd = gdb::open (file, O_RDONLY);
   if (fd < 0)
     perror_with_name (("open"));
 
   Elf64_Ehdr header;
-  ssize_t ret = gdb::handle_eintr (-1, ::read, fd, &header, sizeof (header));
+ ssize_t ret = gdb::read (fd, &header, sizeof (header));
   if (ret == -1)
     perror_with_name (("read"));
-  gdb::handle_eintr (-1, ::close, fd);
+  gdb::close (fd);
   if (ret != sizeof (header))
     error ("Cannot read ELF file header: %s", file);
 
diff --git a/gdbsupport/Makefile.am b/gdbsupport/Makefile.am
index e77298751cd..a3c3a11fdb2 100644
--- a/gdbsupport/Makefile.am
+++ b/gdbsupport/Makefile.am
@@ -61,6 +61,7 @@ libgdbsupport_a_SOURCES = \
     common-inferior.cc \
     common-regcache.cc \
     common-utils.cc \
+    eintr.cc \
     environ.cc \
     errors.cc \
     event-loop.cc \
diff --git a/gdbsupport/Makefile.in b/gdbsupport/Makefile.in
index db3d6f6b4dd..a8696bca377 100644
--- a/gdbsupport/Makefile.in
+++ b/gdbsupport/Makefile.in
@@ -419,6 +419,7 @@ libgdbsupport_a_SOURCES = \
     common-inferior.cc \
     common-regcache.cc \
     common-utils.cc \
+    eintr.cc \
     environ.cc \
     errors.cc \
     event-loop.cc \
diff --git a/gdbsupport/eintr.cc b/gdbsupport/eintr.cc
new file mode 100644
index 00000000000..7e24fa56224
--- /dev/null
+++ b/gdbsupport/eintr.cc
@@ -0,0 +1,56 @@
+/* System calls wrappers for GDB, the GNU debugger.
+
+   Copyright (C) 2024 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 <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+namespace gdb {
+
+extern "C" {
+
+  pid_t
+  waitpid (pid_t pid, int *wstatus, int options)
+  {
+    return gdb::handle_eintr (-1, ::waitpid, pid, wstatus, options);
+  }
+
+  int
+  open (const char *pathname, int flags)
+  {
+    return gdb::handle_eintr (-1, ::open, pathname, flags);
+  }
+
+  int
+  close (int fd)
+  {
+    return gdb::handle_eintr (-1, ::close, fd);
+  }
+
+  ssize_t
+  read (int fd, void *buf, size_t count)
+  {
+    return gdb::handle_eintr (-1, ::read, fd, buf, count);
+  }
+
+}
+
+}
diff --git a/gdbsupport/eintr.h b/gdbsupport/eintr.h
index e440f935fcf..3919c9fd930 100644
--- a/gdbsupport/eintr.h
+++ b/gdbsupport/eintr.h
@@ -66,6 +66,13 @@ handle_eintr (ErrorValType errval, const Fun &f, const Args &... args)
   return ret;
 }
 
+extern "C" {
+  extern pid_t waitpid (pid_t pid, int *wstatus, int options);
+  extern int open (const char *pathname, int flags);
+  extern int close (int fd);
+  extern ssize_t read (int fd, void *buf, size_t count);
+}
+
 } /* namespace gdb */
 
 #endif /* GDBSUPPORT_EINTR_H */

base-commit: 77f6ff446173ac7790f35d43de7d196a768c38b1
-- 
2.35.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-11-22 16:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-28 17:49 [PATCH 1/3] [gdbsupport] Add gdb::{waitpid,read,write,close} Tom de Vries
2024-10-28 17:49 ` [PATCH 2/3] [gdb] Use gdb::waitpid more often Tom de Vries
2024-10-28 17:49 ` [PATCH 3/3] [gdbsupport] Add gdb::wait Tom de Vries
2024-11-01 17:35 ` [PATCH 1/3] [gdbsupport] Add gdb::{waitpid,read,write,close} Tom Tromey
2024-11-05 12:07   ` Tom de Vries
2024-11-22 16:46     ` Tom de Vries

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).