]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdbsupport] Add gdb::{waitpid,read,write,close}
authorTom de Vries <tdevries@suse.de>
Fri, 22 Nov 2024 16:44:29 +0000 (17:44 +0100)
committerTom de Vries <tdevries@suse.de>
Fri, 22 Nov 2024 16:44:29 +0000 (17:44 +0100)
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
gdb/nat/linux-waitpid.c
gdbserver/netbsd-low.cc
gdbsupport/eintr.h

index bc6cb5813f96e150fbcb9aee23fa2bf41fcfdb0b..0140c717ca2bf2afef83325492eb8e779b6ae919 100644 (file)
@@ -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");
     }
index 0ac2f9fb2b9ee5eb68bbb30e06d4a4631c3c4059..f4ae612c572aacd645706a17487cef858fe1feb7 100644 (file)
@@ -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);
 }
index 4e459e618ca3b0af8a58b4ce339998c2e1133fe4..04e103563e793a06fbd90a7bbcb139da8a4b9a63 100644 (file)
@@ -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);
 
index e440f935fcf98f1631f9dde2c918be880ab9b330..1b99d1c55c594d2067a5d78367fc078a50234f81 100644 (file)
 #define GDBSUPPORT_EINTR_H
 
 #include <cerrno>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 namespace gdb
 {
@@ -66,6 +71,30 @@ handle_eintr (ErrorValType errval, const Fun &f, const Args &... args)
   return ret;
 }
 
+inline pid_t
+waitpid (pid_t pid, int *wstatus, int options)
+{
+  return gdb::handle_eintr (-1, ::waitpid, pid, wstatus, options);
+}
+
+inline int
+open (const char *pathname, int flags)
+{
+  return gdb::handle_eintr (-1, ::open, pathname, flags);
+}
+
+inline int
+close (int fd)
+{
+  return gdb::handle_eintr (-1, ::close, fd);
+}
+
+inline ssize_t
+read (int fd, void *buf, size_t count)
+{
+  return gdb::handle_eintr (-1, ::read, fd, buf, count);
+}
+
 } /* namespace gdb */
 
 #endif /* GDBSUPPORT_EINTR_H */