public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h
@ 2022-08-26 19:54 Simon Marchi
  2022-08-26 19:54 ` [PATCH 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-26 19:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

I don't see why include/gdb/fileio.h is placed there.  It's not
installed by "make install", and it's not included by anything outside
of gdb/gdbserver/gdbsupport.

Move its content back to gdbsupport/fileio.h.  I have omitted the bits
inside an `#if 0`, since it's obviously not used, as well as the
"limits" constants, which are also unused.

Change-Id: I6fbc2ea10fbe4cfcf15f9f76006b31b99c20e5a9
---
 gdb/gdb_bfd.c          |   2 +-
 gdb/remote.c           |   2 +-
 gdb/target.c           |   2 +-
 gdbserver/hostio.cc    |   2 +-
 gdbserver/win32-low.cc |   2 +-
 gdbsupport/fileio.h    | 110 ++++++++++++++++++++++++++++++-
 include/gdb/fileio.h   | 144 -----------------------------------------
 7 files changed, 114 insertions(+), 150 deletions(-)
 delete mode 100644 include/gdb/fileio.h

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 6c03ae5ef05..1079623d79f 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -30,7 +30,7 @@
 #endif
 #endif
 #include "target.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "inferior.h"
 #include "cli/cli-style.h"
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 70f918a7362..5bae65472ed 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -59,7 +59,7 @@
 #include "gdbcore.h"
 
 #include "remote-fileio.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include <sys/stat.h>
 #include "xml-support.h"
 
diff --git a/gdb/target.c b/gdb/target.c
index 41144901fc1..9d698afb09d 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -40,7 +40,7 @@
 #include "exec.h"
 #include "inline-frame.h"
 #include "tracepoint.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "gdbsupport/agent.h"
 #include "auxv.h"
 #include "target-debug.h"
diff --git a/gdbserver/hostio.cc b/gdbserver/hostio.cc
index 2d17b6d1a70..4f763f382bb 100644
--- a/gdbserver/hostio.cc
+++ b/gdbserver/hostio.cc
@@ -19,7 +19,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "hostio.h"
 
 #include <fcntl.h>
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 5e2c028d683..af132f60215 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -20,7 +20,7 @@
 
 #include "server.h"
 #include "regcache.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "mem-break.h"
 #include "win32-low.h"
 #include "gdbthread.h"
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 2b4f977a020..9809c162314 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -20,9 +20,117 @@
 #ifndef COMMON_FILEIO_H
 #define COMMON_FILEIO_H
 
-#include "gdb/fileio.h"
 #include <sys/stat.h>
 
+/* The following flags are defined to be independent of the host
+   as well as the target side implementation of these constants.
+   All constants are defined with a leading FILEIO_ in the name
+   to allow the usage of these constants together with the
+   corresponding implementation dependent constants in one module. */
+
+/* open(2) flags */
+#define FILEIO_O_RDONLY           0x0
+#define FILEIO_O_WRONLY           0x1
+#define FILEIO_O_RDWR             0x2
+#define FILEIO_O_APPEND           0x8
+#define FILEIO_O_CREAT          0x200
+#define FILEIO_O_TRUNC          0x400
+#define FILEIO_O_EXCL           0x800
+#define FILEIO_O_SUPPORTED      (FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
+				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
+				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
+				 FILEIO_O_EXCL)
+
+/* mode_t bits */
+#define FILEIO_S_IFREG        0100000
+#define FILEIO_S_IFDIR         040000
+#define FILEIO_S_IFCHR         020000
+#define FILEIO_S_IRUSR           0400
+#define FILEIO_S_IWUSR           0200
+#define FILEIO_S_IXUSR           0100
+#define FILEIO_S_IRWXU           0700
+#define FILEIO_S_IRGRP            040
+#define FILEIO_S_IWGRP            020
+#define FILEIO_S_IXGRP            010
+#define FILEIO_S_IRWXG            070
+#define FILEIO_S_IROTH             04
+#define FILEIO_S_IWOTH             02
+#define FILEIO_S_IXOTH             01
+#define FILEIO_S_IRWXO             07
+#define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
+				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
+                                    FILEIO_S_IRWXO)
+
+/* lseek(2) flags */
+#define FILEIO_SEEK_SET             0
+#define FILEIO_SEEK_CUR             1
+#define FILEIO_SEEK_END             2
+
+/* errno values */
+#define FILEIO_EPERM                1
+#define FILEIO_ENOENT               2
+#define FILEIO_EINTR                4
+#define FILEIO_EIO                  5
+#define FILEIO_EBADF                9
+#define FILEIO_EACCES              13
+#define FILEIO_EFAULT              14
+#define FILEIO_EBUSY               16
+#define FILEIO_EEXIST              17
+#define FILEIO_ENODEV              19
+#define FILEIO_ENOTDIR             20
+#define FILEIO_EISDIR              21
+#define FILEIO_EINVAL              22
+#define FILEIO_ENFILE              23
+#define FILEIO_EMFILE              24
+#define FILEIO_EFBIG               27
+#define FILEIO_ENOSPC              28
+#define FILEIO_ESPIPE              29
+#define FILEIO_EROFS               30
+#define FILEIO_ENOSYS              88
+#define FILEIO_ENAMETOOLONG        91
+#define FILEIO_EUNKNOWN          9999
+
+#define FIO_INT_LEN   4
+#define FIO_UINT_LEN  4
+#define FIO_MODE_LEN  4
+#define FIO_TIME_LEN  4
+#define FIO_LONG_LEN  8
+#define FIO_ULONG_LEN 8
+
+typedef char fio_int_t[FIO_INT_LEN];
+typedef char fio_uint_t[FIO_UINT_LEN];
+typedef char fio_mode_t[FIO_MODE_LEN];
+typedef char fio_time_t[FIO_TIME_LEN];
+typedef char fio_long_t[FIO_LONG_LEN];
+typedef char fio_ulong_t[FIO_ULONG_LEN];
+
+/* Struct stat as used in protocol.  For complete independence
+   of host/target systems, it's defined as an array with offsets
+   to the members. */
+
+struct fio_stat
+{
+  fio_uint_t  fst_dev;
+  fio_uint_t  fst_ino;
+  fio_mode_t  fst_mode;
+  fio_uint_t  fst_nlink;
+  fio_uint_t  fst_uid;
+  fio_uint_t  fst_gid;
+  fio_uint_t  fst_rdev;
+  fio_ulong_t fst_size;
+  fio_ulong_t fst_blksize;
+  fio_ulong_t fst_blocks;
+  fio_time_t  fst_atime;
+  fio_time_t  fst_mtime;
+  fio_time_t  fst_ctime;
+};
+
+struct fio_timeval
+{
+  fio_time_t  ftv_sec;
+  fio_long_t  ftv_usec;
+};
+
 /* Convert a host-format errno value to a File-I/O error number.  */
 
 extern int host_to_fileio_error (int error);
diff --git a/include/gdb/fileio.h b/include/gdb/fileio.h
deleted file mode 100644
index f472f828f72..00000000000
--- a/include/gdb/fileio.h
+++ /dev/null
@@ -1,144 +0,0 @@
-/* Hosted File I/O interface definitions, for GDB, the GNU Debugger.
-
-   Copyright (C) 2003-2022 Free Software Foundation, Inc.
-
-   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 GDB_FILEIO_H_
-#define GDB_FILEIO_H_
-
-/* The following flags are defined to be independent of the host
-   as well as the target side implementation of these constants.
-   All constants are defined with a leading FILEIO_ in the name
-   to allow the usage of these constants together with the
-   corresponding implementation dependent constants in one module. */
-
-/* open(2) flags */
-#define FILEIO_O_RDONLY           0x0
-#define FILEIO_O_WRONLY           0x1
-#define FILEIO_O_RDWR             0x2
-#define FILEIO_O_APPEND           0x8
-#define FILEIO_O_CREAT          0x200
-#define FILEIO_O_TRUNC          0x400
-#define FILEIO_O_EXCL           0x800
-#define FILEIO_O_SUPPORTED	(FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
-				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
-				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
-				 FILEIO_O_EXCL)
-
-/* mode_t bits */
-#define FILEIO_S_IFREG        0100000
-#define FILEIO_S_IFDIR         040000
-#define FILEIO_S_IFCHR         020000
-#define FILEIO_S_IRUSR           0400
-#define FILEIO_S_IWUSR           0200
-#define FILEIO_S_IXUSR           0100
-#define FILEIO_S_IRWXU           0700
-#define FILEIO_S_IRGRP            040
-#define FILEIO_S_IWGRP            020
-#define FILEIO_S_IXGRP            010
-#define FILEIO_S_IRWXG            070
-#define FILEIO_S_IROTH             04
-#define FILEIO_S_IWOTH             02
-#define FILEIO_S_IXOTH             01
-#define FILEIO_S_IRWXO             07
-#define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
-				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
-                                    FILEIO_S_IRWXO)
-
-/* lseek(2) flags */
-#define FILEIO_SEEK_SET             0
-#define FILEIO_SEEK_CUR             1
-#define FILEIO_SEEK_END             2
-
-/* errno values */
-#define FILEIO_EPERM                1
-#define FILEIO_ENOENT               2
-#define FILEIO_EINTR                4
-#define FILEIO_EIO                  5
-#define FILEIO_EBADF                9
-#define FILEIO_EACCES              13
-#define FILEIO_EFAULT              14
-#define FILEIO_EBUSY               16
-#define FILEIO_EEXIST              17
-#define FILEIO_ENODEV              19
-#define FILEIO_ENOTDIR             20
-#define FILEIO_EISDIR              21
-#define FILEIO_EINVAL              22
-#define FILEIO_ENFILE              23
-#define FILEIO_EMFILE              24
-#define FILEIO_EFBIG               27
-#define FILEIO_ENOSPC              28
-#define FILEIO_ESPIPE              29
-#define FILEIO_EROFS               30
-#define FILEIO_ENOSYS		   88
-#define FILEIO_ENAMETOOLONG        91
-#define FILEIO_EUNKNOWN          9999
-
-/* limits */
-#define FILEIO_INT_MIN    -2147483648L
-#define FILEIO_INT_MAX     2147483647L
-#define FILEIO_UINT_MAX    4294967295UL
-#define FILEIO_LONG_MIN   -9223372036854775808LL
-#define FILEIO_LONG_MAX    9223372036854775807LL
-#define FILEIO_ULONG_MAX   18446744073709551615ULL
-
-/* Integral types as used in protocol. */
-#if 0
-typedef __int32_t fio_int_t;
-typedef __uint32_t fio_uint_t, fio_mode_t, fio_time_t;
-typedef __int64_t fio_long_t;
-typedef __uint64_t fio_ulong_t;
-#endif
-
-#define FIO_INT_LEN   4
-#define FIO_UINT_LEN  4
-#define FIO_MODE_LEN  4
-#define FIO_TIME_LEN  4
-#define FIO_LONG_LEN  8
-#define FIO_ULONG_LEN 8
-
-typedef char fio_int_t[FIO_INT_LEN];   
-typedef char fio_uint_t[FIO_UINT_LEN];
-typedef char fio_mode_t[FIO_MODE_LEN];
-typedef char fio_time_t[FIO_TIME_LEN];
-typedef char fio_long_t[FIO_LONG_LEN];
-typedef char fio_ulong_t[FIO_ULONG_LEN];
-
-/* Struct stat as used in protocol.  For complete independence
-   of host/target systems, it's defined as an array with offsets
-   to the members. */
-
-struct fio_stat {
-  fio_uint_t  fst_dev;
-  fio_uint_t  fst_ino;
-  fio_mode_t  fst_mode;
-  fio_uint_t  fst_nlink;
-  fio_uint_t  fst_uid;
-  fio_uint_t  fst_gid;
-  fio_uint_t  fst_rdev;
-  fio_ulong_t fst_size;
-  fio_ulong_t fst_blksize;
-  fio_ulong_t fst_blocks;
-  fio_time_t  fst_atime;
-  fio_time_t  fst_mtime;
-  fio_time_t  fst_ctime;
-};
-
-struct fio_timeval {
-  fio_time_t  ftv_sec;
-  fio_long_t  ftv_usec;
-};
-
-#endif /* GDB_FILEIO_H_ */
-- 
2.37.1


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

* [PATCH 2/3] gdbsupport: convert FILEIO_* macros to an enum
  2022-08-26 19:54 [PATCH 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
@ 2022-08-26 19:54 ` Simon Marchi
  2022-08-26 19:54 ` [PATCH 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h.cc} and rename Simon Marchi
  2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
  2 siblings, 0 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-26 19:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

Converting from free-form macros to an enum gives a bit of type-safety.
This caught places where we would assign host error numbers to what
should contain a target fileio error number, for instance in
target_fileio_pread.

I added the FILEIO_SUCCESS enumerator, because
remote.c:remote_hostio_parse_result initializes the remote_errno output
variable to 0.  It seems better to have an explicit enumerator than to
assign a value for which there is no enumerator.  I considered
initializing this variable to FILEIO_EUNKNOWN instead, such that if the
remote side replies with an error and omits the errno value, we'll get
an errno that represents an error instead of 0 (which reprensents no
error).  But it's not clear what the consequences of that change would
be, so I prefer to err on the side of caution and just keep the existing
behavior (there is no intended change in behavior with this patch).

Note that remote_hostio_parse_resul still reads blindly what the remote
side sends as a target errno into this variable, so we can still end up
with a nonsensical value here.  It's not good, but out of the scope of
this patch.

Convert host_to_fileio_error and fileio_errno_to_host to return / accept
a fileio_error instead of an int, and cascade the change in the whole
chain that uses that.

Change-Id: I454b0e3fcf0732447bc872252fa8e57d138b0e03
---
 gdb/gdb_bfd.c        | 11 +++---
 gdb/inf-child.c      | 14 ++++----
 gdb/inf-child.h      | 14 ++++----
 gdb/linux-nat.c      |  6 ++--
 gdb/linux-nat.h      |  6 ++--
 gdb/linux-tdep.c     |  2 +-
 gdb/remote.c         | 82 +++++++++++++++++++++++---------------------
 gdb/sparc64-tdep.c   |  8 ++---
 gdb/target.c         | 46 ++++++++++++-------------
 gdb/target.h         | 29 ++++++++--------
 gdbsupport/fileio.cc |  2 +-
 gdbsupport/fileio.h  | 50 ++++++++++++++-------------
 12 files changed, 140 insertions(+), 130 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 1079623d79f..46e2c357e3f 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -305,7 +305,7 @@ gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
 /* Return the system error number corresponding to ERRNUM.  */
 
 static int
-fileio_errno_to_host (int errnum)
+fileio_errno_to_host (fileio_error errnum)
 {
   switch (errnum)
     {
@@ -369,7 +369,8 @@ static void *
 gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure)
 {
   const char *filename = bfd_get_filename (abfd);
-  int fd, target_errno;
+  int fd;
+  fileio_error target_errno;
   int *stream;
   gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure;
 
@@ -399,7 +400,7 @@ gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
 			    file_ptr nbytes, file_ptr offset)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
   file_ptr pos, bytes;
 
   pos = 0;
@@ -442,7 +443,7 @@ static int
 gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
 
   xfree (stream);
 
@@ -471,7 +472,7 @@ gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
 			    struct stat *sb)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
   int result;
 
   result = target_fileio_fstat (fd, sb, &target_errno);
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 56ebd2a5549..ca0af4c1b03 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -233,7 +233,7 @@ inf_child_target::pid_to_exec_file (int pid)
 int
 inf_child_target::fileio_open (struct inferior *inf, const char *filename,
 			       int flags, int mode, int warn_if_slow,
-			       int *target_errno)
+			       fileio_error *target_errno)
 {
   int nat_flags;
   mode_t nat_mode;
@@ -257,7 +257,7 @@ inf_child_target::fileio_open (struct inferior *inf, const char *filename,
 
 int
 inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				 ULONGEST offset, int *target_errno)
+				 ULONGEST offset, fileio_error *target_errno)
 {
   int ret;
 
@@ -284,7 +284,7 @@ inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
-				ULONGEST offset, int *target_errno)
+				ULONGEST offset, fileio_error *target_errno)
 {
   int ret;
 
@@ -310,7 +310,7 @@ inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* Implementation of to_fileio_fstat.  */
 
 int
-inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
+inf_child_target::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   int ret;
 
@@ -324,7 +324,7 @@ inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
 /* Implementation of to_fileio_close.  */
 
 int
-inf_child_target::fileio_close (int fd, int *target_errno)
+inf_child_target::fileio_close (int fd, fileio_error *target_errno)
 {
   int ret;
 
@@ -339,7 +339,7 @@ inf_child_target::fileio_close (int fd, int *target_errno)
 
 int
 inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
-				 int *target_errno)
+				 fileio_error *target_errno)
 {
   int ret;
 
@@ -354,7 +354,7 @@ inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 inf_child_target::fileio_readlink (struct inferior *inf, const char *filename,
-				   int *target_errno)
+				   fileio_error *target_errno)
 {
   /* We support readlink only on systems that also provide a compile-time
      maximum path length (PATH_MAX), at least for now.  */
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index ae5ace46f30..16782038011 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -75,19 +75,19 @@ class inf_child_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
   int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		     ULONGEST offset, int *target_errno) override;
+		     ULONGEST offset, fileio_error *target_errno) override;
   int fileio_pread (int fd, gdb_byte *read_buf, int len,
-		    ULONGEST offset, int *target_errno) override;
-  int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
-  int fileio_close (int fd, int *target_errno) override;
+		    ULONGEST offset, fileio_error *target_errno) override;
+  int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno) override;
+  int fileio_close (int fd, fileio_error *target_errno) override;
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
   gdb::optional<std::string> fileio_readlink (struct inferior *inf,
 					      const char *filename,
-					      int *target_errno) override;
+					      fileio_error *target_errno) override;
   bool use_agent (bool use) override;
 
   bool can_use_agent () override;
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index e638e8ad04e..d4639766088 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4372,7 +4372,7 @@ linux_nat_fileio_pid_of (struct inferior *inf)
 int
 linux_nat_target::fileio_open (struct inferior *inf, const char *filename,
 			       int flags, int mode, int warn_if_slow,
-			       int *target_errno)
+			       fileio_error *target_errno)
 {
   int nat_flags;
   mode_t nat_mode;
@@ -4397,7 +4397,7 @@ linux_nat_target::fileio_open (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename,
-				   int *target_errno)
+				   fileio_error *target_errno)
 {
   char buf[PATH_MAX];
   int len;
@@ -4417,7 +4417,7 @@ linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename,
 
 int
 linux_nat_target::fileio_unlink (struct inferior *inf, const char *filename,
-				 int *target_errno)
+				 fileio_error *target_errno)
 {
   int ret;
 
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 38e253eff36..a9b91a5e908 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -101,16 +101,16 @@ class linux_nat_target : public inf_ptrace_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
 
   gdb::optional<std::string>
     fileio_readlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   int insert_fork_catchpoint (int) override;
   int remove_fork_catchpoint (int) override;
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 5fe6be16c31..cde98e1a8c1 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -813,7 +813,7 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
   int status_f = (what == IP_STATUS || what == IP_ALL);
   int stat_f = (what == IP_STAT || what == IP_ALL);
   char filename[100];
-  int target_errno;
+  fileio_error target_errno;
 
   if (args && isdigit (args[0]))
     {
diff --git a/gdb/remote.c b/gdb/remote.c
index 5bae65472ed..e066384ab2b 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -560,26 +560,26 @@ class remote_target : public process_stratum_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
 
   int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		     ULONGEST offset, int *target_errno) override;
+		     ULONGEST offset, fileio_error *target_errno) override;
 
   int fileio_pread (int fd, gdb_byte *read_buf, int len,
-		    ULONGEST offset, int *target_errno) override;
+		    ULONGEST offset, fileio_error *target_errno) override;
 
-  int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
+  int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno) override;
 
-  int fileio_close (int fd, int *target_errno) override;
+  int fileio_close (int fd, fileio_error *target_errno) override;
 
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   gdb::optional<std::string>
     fileio_readlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   bool supports_enable_disable_tracepoint () override;
 
@@ -701,25 +701,25 @@ class remote_target : public process_stratum_target
   void remote_file_delete (const char *remote_file, int from_tty);
 
   int remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
-			   ULONGEST offset, int *remote_errno);
+			   ULONGEST offset, fileio_error *remote_errno);
   int remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			    ULONGEST offset, int *remote_errno);
+			    ULONGEST offset, fileio_error *remote_errno);
   int remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
-				 ULONGEST offset, int *remote_errno);
+				 ULONGEST offset, fileio_error *remote_errno);
 
   int remote_hostio_send_command (int command_bytes, int which_packet,
-				  int *remote_errno, const char **attachment,
+				  fileio_error *remote_errno, const char **attachment,
 				  int *attachment_len);
   int remote_hostio_set_filesystem (struct inferior *inf,
-				    int *remote_errno);
+				    fileio_error *remote_errno);
   /* We should get rid of this and use fileio_open directly.  */
   int remote_hostio_open (struct inferior *inf, const char *filename,
 			  int flags, int mode, int warn_if_slow,
-			  int *remote_errno);
-  int remote_hostio_close (int fd, int *remote_errno);
+			  fileio_error *remote_errno);
+  int remote_hostio_close (int fd, fileio_error *remote_errno);
 
   int remote_hostio_unlink (inferior *inf, const char *filename,
-			    int *remote_errno);
+			    fileio_error *remote_errno);
 
   struct remote_state *get_remote_state ();
 
@@ -12130,7 +12130,7 @@ remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
 }
 
 /* Parse an I/O result packet from BUFFER.  Set RETCODE to the return
-   value, *REMOTE_ERRNO to the remote error number or zero if none
+   value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
    was included, and *ATTACHMENT to point to the start of the annex
    if any.  The length of the packet isn't needed here; there may
    be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
@@ -12140,11 +12140,11 @@ remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
 
 static int
 remote_hostio_parse_result (const char *buffer, int *retcode,
-			    int *remote_errno, const char **attachment)
+			    fileio_error *remote_errno, const char **attachment)
 {
   char *p, *p2;
 
-  *remote_errno = 0;
+  *remote_errno = FILEIO_SUCCESS;
   *attachment = NULL;
 
   if (buffer[0] != 'F')
@@ -12159,7 +12159,7 @@ remote_hostio_parse_result (const char *buffer, int *retcode,
   if (*p == ',')
     {
       errno = 0;
-      *remote_errno = strtol (p + 1, &p2, 16);
+      *remote_errno = (fileio_error) strtol (p + 1, &p2, 16);
       if (errno != 0 || p + 1 == p2)
 	return -1;
       p = p2;
@@ -12196,7 +12196,7 @@ remote_hostio_parse_result (const char *buffer, int *retcode,
 
 int
 remote_target::remote_hostio_send_command (int command_bytes, int which_packet,
-					   int *remote_errno, const char **attachment,
+					   fileio_error *remote_errno, const char **attachment,
 					   int *attachment_len)
 {
   struct remote_state *rs = get_remote_state ();
@@ -12281,7 +12281,7 @@ readahead_cache::invalidate_fd (int fd)
 
 int
 remote_target::remote_hostio_set_filesystem (struct inferior *inf,
-					     int *remote_errno)
+					     fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
@@ -12318,7 +12318,7 @@ remote_target::remote_hostio_set_filesystem (struct inferior *inf,
 int
 remote_target::remote_hostio_open (inferior *inf, const char *filename,
 				   int flags, int mode, int warn_if_slow,
-				   int *remote_errno)
+				   fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12361,7 +12361,7 @@ remote_target::remote_hostio_open (inferior *inf, const char *filename,
 int
 remote_target::fileio_open (struct inferior *inf, const char *filename,
 			    int flags, int mode, int warn_if_slow,
-			    int *remote_errno)
+			    fileio_error *remote_errno)
 {
   return remote_hostio_open (inf, filename, flags, mode, warn_if_slow,
 			     remote_errno);
@@ -12371,7 +12371,7 @@ remote_target::fileio_open (struct inferior *inf, const char *filename,
 
 int
 remote_target::remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				     ULONGEST offset, int *remote_errno)
+				     ULONGEST offset, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12398,7 +12398,7 @@ remote_target::remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			      ULONGEST offset, int *remote_errno)
+			      ULONGEST offset, fileio_error *remote_errno)
 {
   return remote_hostio_pwrite (fd, write_buf, len, offset, remote_errno);
 }
@@ -12408,7 +12408,7 @@ remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 remote_target::remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
-					  ULONGEST offset, int *remote_errno)
+					  ULONGEST offset, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12468,7 +12468,7 @@ readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
 
 int
 remote_target::remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
-				    ULONGEST offset, int *remote_errno)
+				    ULONGEST offset, fileio_error *remote_errno)
 {
   int ret;
   struct remote_state *rs = get_remote_state ();
@@ -12508,7 +12508,7 @@ remote_target::remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
 
 int
 remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
-			     ULONGEST offset, int *remote_errno)
+			     ULONGEST offset, fileio_error *remote_errno)
 {
   return remote_hostio_pread (fd, read_buf, len, offset, remote_errno);
 }
@@ -12516,7 +12516,7 @@ remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* Implementation of to_fileio_close.  */
 
 int
-remote_target::remote_hostio_close (int fd, int *remote_errno)
+remote_target::remote_hostio_close (int fd, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12533,7 +12533,7 @@ remote_target::remote_hostio_close (int fd, int *remote_errno)
 }
 
 int
-remote_target::fileio_close (int fd, int *remote_errno)
+remote_target::fileio_close (int fd, fileio_error *remote_errno)
 {
   return remote_hostio_close (fd, remote_errno);
 }
@@ -12542,7 +12542,7 @@ remote_target::fileio_close (int fd, int *remote_errno)
 
 int
 remote_target::remote_hostio_unlink (inferior *inf, const char *filename,
-				     int *remote_errno)
+				     fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12562,7 +12562,7 @@ remote_target::remote_hostio_unlink (inferior *inf, const char *filename,
 
 int
 remote_target::fileio_unlink (struct inferior *inf, const char *filename,
-			      int *remote_errno)
+			      fileio_error *remote_errno)
 {
   return remote_hostio_unlink (inf, filename, remote_errno);
 }
@@ -12571,7 +12571,7 @@ remote_target::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 remote_target::fileio_readlink (struct inferior *inf, const char *filename,
-				int *remote_errno)
+				fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12608,7 +12608,7 @@ remote_target::fileio_readlink (struct inferior *inf, const char *filename,
 /* Implementation of to_fileio_fstat.  */
 
 int
-remote_target::fileio_fstat (int fd, struct stat *st, int *remote_errno)
+remote_target::fileio_fstat (int fd, struct stat *st, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12680,7 +12680,8 @@ remote_target::filesystem_is_local ()
 
       if (ps == PACKET_SUPPORT_UNKNOWN)
 	{
-	  int fd, remote_errno;
+	  int fd;
+	  fileio_error remote_errno;
 
 	  /* Try opening a file to probe support.  The supplied
 	     filename is irrelevant, we only care about whether
@@ -12792,7 +12793,7 @@ class scoped_remote_fd
       {
 	try
 	  {
-	    int remote_errno;
+	    fileio_error remote_errno;
 	    m_remote->remote_hostio_close (m_fd, &remote_errno);
 	  }
 	catch (...)
@@ -12843,7 +12844,8 @@ void
 remote_target::remote_file_put (const char *local_file, const char *remote_file,
 				int from_tty)
 {
-  int retcode, remote_errno, bytes, io_size;
+  int retcode, bytes, io_size;
+  fileio_error remote_errno;
   int bytes_in_buffer;
   int saw_eof;
   ULONGEST offset;
@@ -12935,7 +12937,8 @@ void
 remote_target::remote_file_get (const char *remote_file, const char *local_file,
 				int from_tty)
 {
-  int remote_errno, bytes, io_size;
+  fileio_error remote_errno;
+  int bytes, io_size;
   ULONGEST offset;
 
   scoped_remote_fd fd
@@ -12993,7 +12996,8 @@ remote_file_delete (const char *remote_file, int from_tty)
 void
 remote_target::remote_file_delete (const char *remote_file, int from_tty)
 {
-  int retcode, remote_errno;
+  int retcode;
+  fileio_error remote_errno;
 
   retcode = remote_hostio_unlink (NULL, remote_file, &remote_errno);
   if (retcode == -1)
diff --git a/gdb/sparc64-tdep.c b/gdb/sparc64-tdep.c
index 5ca5f2dca8c..3842bf5a556 100644
--- a/gdb/sparc64-tdep.c
+++ b/gdb/sparc64-tdep.c
@@ -166,7 +166,7 @@ get_adi_info (pid_t pid)
 void
 sparc64_forget_process (pid_t pid)
 {
-  int target_errno;
+  fileio_error target_errno;
 
   for (auto pit = adi_proc_list.before_begin (),
 	 it = std::next (pit);
@@ -287,7 +287,7 @@ adi_tag_fd (void)
 
   char cl_name[MAX_PROC_NAME_SIZE];
   snprintf (cl_name, sizeof(cl_name), "/proc/%ld/adi/tags", (long) pid);
-  int target_errno;
+  fileio_error target_errno;
   proc->stat.tag_fd = target_fileio_open (NULL, cl_name, O_RDWR|O_EXCL, 
 					  false, 0, &target_errno);
   return proc->stat.tag_fd;
@@ -350,7 +350,7 @@ adi_read_versions (CORE_ADDR vaddr, size_t size, gdb_byte *tags)
 	    paddress (target_gdbarch (), vaddr * ast.blksize));
     }
 
-  int target_errno;
+  fileio_error target_errno;
   return target_fileio_pread (fd, tags, size, vaddr, &target_errno);
 }
 
@@ -371,7 +371,7 @@ adi_write_versions (CORE_ADDR vaddr, size_t size, unsigned char *tags)
 	    paddress (target_gdbarch (), vaddr * ast.blksize));
     }
 
-  int target_errno;
+  fileio_error target_errno;
   return target_fileio_pwrite (fd, tags, size, vaddr, &target_errno);
 }
 
diff --git a/gdb/target.c b/gdb/target.c
index 9d698afb09d..28560983625 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -3206,7 +3206,7 @@ fileio_fd_to_fh (int fd)
 int
 target_ops::fileio_open (struct inferior *inf, const char *filename,
 			 int flags, int mode, int warn_if_slow,
-			 int *target_errno)
+			 fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3214,7 +3214,7 @@ target_ops::fileio_open (struct inferior *inf, const char *filename,
 
 int
 target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			   ULONGEST offset, int *target_errno)
+			   ULONGEST offset, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3222,21 +3222,21 @@ target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len,
-			  ULONGEST offset, int *target_errno)
+			  ULONGEST offset, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
 }
 
 int
-target_ops::fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_ops::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
 }
 
 int
-target_ops::fileio_close (int fd, int *target_errno)
+target_ops::fileio_close (int fd, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3244,7 +3244,7 @@ target_ops::fileio_close (int fd, int *target_errno)
 
 int
 target_ops::fileio_unlink (struct inferior *inf, const char *filename,
-			   int *target_errno)
+			   fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3252,7 +3252,7 @@ target_ops::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 target_ops::fileio_readlink (struct inferior *inf, const char *filename,
-			     int *target_errno)
+			     fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return {};
@@ -3262,7 +3262,7 @@ target_ops::fileio_readlink (struct inferior *inf, const char *filename,
 
 int
 target_fileio_open (struct inferior *inf, const char *filename,
-		    int flags, int mode, bool warn_if_slow, int *target_errno)
+		    int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3296,15 +3296,15 @@ target_fileio_open (struct inferior *inf, const char *filename,
 
 int
 target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		      ULONGEST offset, int *target_errno)
+		      ULONGEST offset, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_pwrite (fh->target_fd, write_buf,
 				     len, offset, target_errno);
@@ -3322,15 +3322,15 @@ target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 target_fileio_pread (int fd, gdb_byte *read_buf, int len,
-		     ULONGEST offset, int *target_errno)
+		     ULONGEST offset, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_pread (fh->target_fd, read_buf,
 				    len, offset, target_errno);
@@ -3347,15 +3347,15 @@ target_fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* See target.h.  */
 
 int
-target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
 
@@ -3369,13 +3369,13 @@ target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
 /* See target.h.  */
 
 int
-target_fileio_close (int fd, int *target_errno)
+target_fileio_close (int fd, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else
     {
       if (fh->target != NULL)
@@ -3397,7 +3397,7 @@ target_fileio_close (int fd, int *target_errno)
 
 int
 target_fileio_unlink (struct inferior *inf, const char *filename,
-		      int *target_errno)
+		      fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3423,7 +3423,7 @@ target_fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 target_fileio_readlink (struct inferior *inf, const char *filename,
-			int *target_errno)
+			fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3461,7 +3461,7 @@ class scoped_target_fd
   {
     if (m_fd >= 0)
       {
-	int target_errno;
+	fileio_error target_errno;
 
 	target_fileio_close (m_fd, &target_errno);
       }
@@ -3493,7 +3493,7 @@ target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
   size_t buf_alloc, buf_pos;
   gdb_byte *buf;
   LONGEST n;
-  int target_errno;
+  fileio_error target_errno;
 
   scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
 					   0700, false, &target_errno));
diff --git a/gdb/target.h b/gdb/target.h
index 7e52716a9e8..0b784278c4f 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -81,6 +81,7 @@ struct inferior;
 #include "command.h"
 #include "disasm-flags.h"
 #include "tracepoint.h"
+#include "gdbsupport/fileio.h"
 
 #include "gdbsupport/break-common.h" /* For enum target_hw_bp_type.  */
 
@@ -952,28 +953,28 @@ struct target_ops
        *TARGET_ERRNO).  */
     virtual int fileio_open (struct inferior *inf, const char *filename,
 			     int flags, int mode, int warn_if_slow,
-			     int *target_errno);
+			     fileio_error *target_errno);
 
     /* Write up to LEN bytes from WRITE_BUF to FD on the target.
        Return the number of bytes written, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
     virtual int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			       ULONGEST offset, int *target_errno);
+			       ULONGEST offset, fileio_error *target_errno);
 
     /* Read up to LEN bytes FD on the target into READ_BUF.
        Return the number of bytes read, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
     virtual int fileio_pread (int fd, gdb_byte *read_buf, int len,
-			      ULONGEST offset, int *target_errno);
+			      ULONGEST offset, fileio_error *target_errno);
 
     /* Get information about the file opened as FD and put it in
        SB.  Return 0 on success, or -1 if an error occurs (and set
        *TARGET_ERRNO).  */
-    virtual int fileio_fstat (int fd, struct stat *sb, int *target_errno);
+    virtual int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno);
 
     /* Close FD on the target.  Return 0, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
-    virtual int fileio_close (int fd, int *target_errno);
+    virtual int fileio_close (int fd, fileio_error *target_errno);
 
     /* Unlink FILENAME on the target, in the filesystem as seen by
        INF.  If INF is NULL, use the filesystem seen by the debugger
@@ -981,7 +982,7 @@ struct target_ops
        -1 if an error occurs (and set *TARGET_ERRNO).  */
     virtual int fileio_unlink (struct inferior *inf,
 			       const char *filename,
-			       int *target_errno);
+			       fileio_error *target_errno);
 
     /* Read value of symbolic link FILENAME on the target, in the
        filesystem as seen by INF.  If INF is NULL, use the filesystem
@@ -990,7 +991,7 @@ struct target_ops
        occurs (and set *TARGET_ERRNO).  */
     virtual gdb::optional<std::string> fileio_readlink (struct inferior *inf,
 							const char *filename,
-							int *target_errno);
+							fileio_error *target_errno);
 
     /* Implement the "info proc" command.  Returns true if the target
        actually implemented the command, false otherwise.  */
@@ -2165,29 +2166,29 @@ extern bool target_filesystem_is_local ();
 extern int target_fileio_open (struct inferior *inf,
 			       const char *filename, int flags,
 			       int mode, bool warn_if_slow,
-			       int *target_errno);
+			       fileio_error *target_errno);
 
 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
    Return the number of bytes written, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
 extern int target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				 ULONGEST offset, int *target_errno);
+				 ULONGEST offset, fileio_error *target_errno);
 
 /* Read up to LEN bytes FD on the target into READ_BUF.
    Return the number of bytes read, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
 extern int target_fileio_pread (int fd, gdb_byte *read_buf, int len,
-				ULONGEST offset, int *target_errno);
+				ULONGEST offset, fileio_error *target_errno);
 
 /* Get information about the file opened as FD on the target
    and put it in SB.  Return 0 on success, or -1 if an error
    occurs (and set *TARGET_ERRNO).  */
 extern int target_fileio_fstat (int fd, struct stat *sb,
-				int *target_errno);
+				fileio_error *target_errno);
 
 /* Close FD on the target.  Return 0, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
-extern int target_fileio_close (int fd, int *target_errno);
+extern int target_fileio_close (int fd, fileio_error *target_errno);
 
 /* Unlink FILENAME on the target, in the filesystem as seen by INF.
    If INF is NULL, use the filesystem seen by the debugger (GDB or,
@@ -2195,7 +2196,7 @@ extern int target_fileio_close (int fd, int *target_errno);
    occurs (and set *TARGET_ERRNO).  */
 extern int target_fileio_unlink (struct inferior *inf,
 				 const char *filename,
-				 int *target_errno);
+				 fileio_error *target_errno);
 
 /* Read value of symbolic link FILENAME on the target, in the
    filesystem as seen by INF.  If INF is NULL, use the filesystem seen
@@ -2203,7 +2204,7 @@ extern int target_fileio_unlink (struct inferior *inf,
    Return a null-terminated string allocated via xmalloc, or NULL if
    an error occurs (and set *TARGET_ERRNO).  */
 extern gdb::optional<std::string> target_fileio_readlink
-    (struct inferior *inf, const char *filename, int *target_errno);
+    (struct inferior *inf, const char *filename, fileio_error *target_errno);
 
 /* Read target file FILENAME, in the filesystem as seen by INF.  If
    INF is NULL, use the filesystem seen by the debugger (GDB or, for
diff --git a/gdbsupport/fileio.cc b/gdbsupport/fileio.cc
index b4feb8583ab..db7c1a7c488 100644
--- a/gdbsupport/fileio.cc
+++ b/gdbsupport/fileio.cc
@@ -24,7 +24,7 @@
 
 /* See fileio.h.  */
 
-int
+fileio_error
 host_to_fileio_error (int error)
 {
   switch (error)
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 9809c162314..6a5297c6433 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -67,28 +67,32 @@
 #define FILEIO_SEEK_END             2
 
 /* errno values */
-#define FILEIO_EPERM                1
-#define FILEIO_ENOENT               2
-#define FILEIO_EINTR                4
-#define FILEIO_EIO                  5
-#define FILEIO_EBADF                9
-#define FILEIO_EACCES              13
-#define FILEIO_EFAULT              14
-#define FILEIO_EBUSY               16
-#define FILEIO_EEXIST              17
-#define FILEIO_ENODEV              19
-#define FILEIO_ENOTDIR             20
-#define FILEIO_EISDIR              21
-#define FILEIO_EINVAL              22
-#define FILEIO_ENFILE              23
-#define FILEIO_EMFILE              24
-#define FILEIO_EFBIG               27
-#define FILEIO_ENOSPC              28
-#define FILEIO_ESPIPE              29
-#define FILEIO_EROFS               30
-#define FILEIO_ENOSYS              88
-#define FILEIO_ENAMETOOLONG        91
-#define FILEIO_EUNKNOWN          9999
+enum fileio_error
+{
+  FILEIO_SUCCESS      =    0,
+  FILEIO_EPERM        =    1,
+  FILEIO_ENOENT       =    2,
+  FILEIO_EINTR        =    4,
+  FILEIO_EIO          =    5,
+  FILEIO_EBADF        =    9,
+  FILEIO_EACCES       =   13,
+  FILEIO_EFAULT       =   14,
+  FILEIO_EBUSY        =   16,
+  FILEIO_EEXIST       =   17,
+  FILEIO_ENODEV       =   19,
+  FILEIO_ENOTDIR      =   20,
+  FILEIO_EISDIR       =   21,
+  FILEIO_EINVAL       =   22,
+  FILEIO_ENFILE       =   23,
+  FILEIO_EMFILE       =   24,
+  FILEIO_EFBIG        =   27,
+  FILEIO_ENOSPC       =   28,
+  FILEIO_ESPIPE       =   29,
+  FILEIO_EROFS        =   30,
+  FILEIO_ENOSYS       =   88,
+  FILEIO_ENAMETOOLONG =   91,
+  FILEIO_EUNKNOWN     = 9999,
+};
 
 #define FIO_INT_LEN   4
 #define FIO_UINT_LEN  4
@@ -133,7 +137,7 @@ struct fio_timeval
 
 /* Convert a host-format errno value to a File-I/O error number.  */
 
-extern int host_to_fileio_error (int error);
+extern fileio_error host_to_fileio_error (int error);
 
 /* Convert File-I/O open flags FFLAGS to host format, storing
    the result in *FLAGS.  Return 0 on success, -1 on error.  */
-- 
2.37.1


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

* [PATCH 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h.cc} and rename
  2022-08-26 19:54 [PATCH 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
  2022-08-26 19:54 ` [PATCH 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
@ 2022-08-26 19:54 ` Simon Marchi
  2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
  2 siblings, 0 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-26 19:54 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

We have a downstream change in ROCm-GDB to move fileio_errno_to_host to
gdbsupport/fileio.{h,cc}, so we can use it outside of gdb_bfd.c.  Even
though there is nothing upstream that would need this, I think it's a
change that makes sense, that fileio_errno_to_host belongs there,
alongside host_to_fileio_error.

Rename it fileio_error_to_host, for symmetry with host_to_fileio_error.

Change-Id: Ib9b8807683de2f809c94a5303e708acc2251a0df
---
 gdb/gdb_bfd.c        | 53 --------------------------------------------
 gdbsupport/fileio.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++
 gdbsupport/fileio.h  |  4 ++++
 3 files changed, 57 insertions(+), 53 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 46e2c357e3f..f3a0afe7b26 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -302,59 +302,6 @@ gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
 			      mem_bfd_iovec_stat);
 }
 
-/* Return the system error number corresponding to ERRNUM.  */
-
-static int
-fileio_errno_to_host (fileio_error errnum)
-{
-  switch (errnum)
-    {
-      case FILEIO_EPERM:
-	return EPERM;
-      case FILEIO_ENOENT:
-	return ENOENT;
-      case FILEIO_EINTR:
-	return EINTR;
-      case FILEIO_EIO:
-	return EIO;
-      case FILEIO_EBADF:
-	return EBADF;
-      case FILEIO_EACCES:
-	return EACCES;
-      case FILEIO_EFAULT:
-	return EFAULT;
-      case FILEIO_EBUSY:
-	return EBUSY;
-      case FILEIO_EEXIST:
-	return EEXIST;
-      case FILEIO_ENODEV:
-	return ENODEV;
-      case FILEIO_ENOTDIR:
-	return ENOTDIR;
-      case FILEIO_EISDIR:
-	return EISDIR;
-      case FILEIO_EINVAL:
-	return EINVAL;
-      case FILEIO_ENFILE:
-	return ENFILE;
-      case FILEIO_EMFILE:
-	return EMFILE;
-      case FILEIO_EFBIG:
-	return EFBIG;
-      case FILEIO_ENOSPC:
-	return ENOSPC;
-      case FILEIO_ESPIPE:
-	return ESPIPE;
-      case FILEIO_EROFS:
-	return EROFS;
-      case FILEIO_ENOSYS:
-	return ENOSYS;
-      case FILEIO_ENAMETOOLONG:
-	return ENAMETOOLONG;
-    }
-  return -1;
-}
-
 /* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open.  */
 struct gdb_bfd_open_closure
 {
diff --git a/gdbsupport/fileio.cc b/gdbsupport/fileio.cc
index db7c1a7c488..b3864e86c85 100644
--- a/gdbsupport/fileio.cc
+++ b/gdbsupport/fileio.cc
@@ -77,6 +77,59 @@ host_to_fileio_error (int error)
 
 /* See fileio.h.  */
 
+int
+fileio_errno_to_host (fileio_error errnum)
+{
+  switch (errnum)
+    {
+      case FILEIO_EPERM:
+	return EPERM;
+      case FILEIO_ENOENT:
+	return ENOENT;
+      case FILEIO_EINTR:
+	return EINTR;
+      case FILEIO_EIO:
+	return EIO;
+      case FILEIO_EBADF:
+	return EBADF;
+      case FILEIO_EACCES:
+	return EACCES;
+      case FILEIO_EFAULT:
+	return EFAULT;
+      case FILEIO_EBUSY:
+	return EBUSY;
+      case FILEIO_EEXIST:
+	return EEXIST;
+      case FILEIO_ENODEV:
+	return ENODEV;
+      case FILEIO_ENOTDIR:
+	return ENOTDIR;
+      case FILEIO_EISDIR:
+	return EISDIR;
+      case FILEIO_EINVAL:
+	return EINVAL;
+      case FILEIO_ENFILE:
+	return ENFILE;
+      case FILEIO_EMFILE:
+	return EMFILE;
+      case FILEIO_EFBIG:
+	return EFBIG;
+      case FILEIO_ENOSPC:
+	return ENOSPC;
+      case FILEIO_ESPIPE:
+	return ESPIPE;
+      case FILEIO_EROFS:
+	return EROFS;
+      case FILEIO_ENOSYS:
+	return ENOSYS;
+      case FILEIO_ENAMETOOLONG:
+	return ENAMETOOLONG;
+    }
+  return -1;
+}
+
+/* See fileio.h.  */
+
 int
 fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
 {
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 6a5297c6433..619f7e9f5e6 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -139,6 +139,10 @@ struct fio_timeval
 
 extern fileio_error host_to_fileio_error (int error);
 
+/* Convert a File-I/O error number to a host-format errno value.  */
+
+extern int fileio_errno_to_host (fileio_error errnum);
+
 /* Convert File-I/O open flags FFLAGS to host format, storing
    the result in *FLAGS.  Return 0 on success, -1 on error.  */
 
-- 
2.37.1


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

* [PATCH v2 0/3] fileio error cleanups
  2022-08-26 19:54 [PATCH 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
  2022-08-26 19:54 ` [PATCH 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
  2022-08-26 19:54 ` [PATCH 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h.cc} and rename Simon Marchi
@ 2022-08-29 16:29 ` Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
                     ` (2 more replies)
  2 siblings, 3 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-29 16:29 UTC (permalink / raw)
  To: gdb-patches

This is a v2 of:

  https://inbox.sourceware.org/gdb-patches/20220826195459.2540898-1-simon.marchi@efficios.com/

I only updated patch 3:

  - The commit message claimed the function was renamed, but the code
    didn't actually rename it.  Now it does.
  - I noticed that remote.c contained an identical fileio error to errno
    function, so change remote.c to use the common function too.

Simon Marchi (3):
  gdbsupport: move include/gdb/fileio.h contents to fileio.h
  gdbsupport: convert FILEIO_* macros to an enum
  gdbsupport: move fileio_errno_to_host to fileio.{h,cc} and rename

 gdb/gdb_bfd.c          |  70 +++-----------------
 gdb/inf-child.c        |  14 ++--
 gdb/inf-child.h        |  14 ++--
 gdb/linux-nat.c        |   6 +-
 gdb/linux-nat.h        |   6 +-
 gdb/linux-tdep.c       |   2 +-
 gdb/remote.c           | 139 +++++++++++++--------------------------
 gdb/sparc64-tdep.c     |   8 +--
 gdb/target.c           |  48 +++++++-------
 gdb/target.h           |  29 +++++----
 gdbserver/hostio.cc    |   2 +-
 gdbserver/win32-low.cc |   2 +-
 gdbsupport/fileio.cc   |  55 +++++++++++++++-
 gdbsupport/fileio.h    | 120 +++++++++++++++++++++++++++++++++-
 include/gdb/fileio.h   | 144 -----------------------------------------
 15 files changed, 293 insertions(+), 366 deletions(-)
 delete mode 100644 include/gdb/fileio.h


base-commit: 27d582267a1d06e94661979f8893799ac235a768
-- 
2.37.2


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

* [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h
  2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
@ 2022-08-29 16:29   ` Simon Marchi
  2022-09-21 18:13     ` Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h, cc} and rename Simon Marchi
  2 siblings, 1 reply; 8+ messages in thread
From: Simon Marchi @ 2022-08-29 16:29 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

I don't see why include/gdb/fileio.h is placed there.  It's not
installed by "make install", and it's not included by anything outside
of gdb/gdbserver/gdbsupport.

Move its content back to gdbsupport/fileio.h.  I have omitted the bits
inside an `#if 0`, since it's obviously not used, as well as the
"limits" constants, which are also unused.

Change-Id: I6fbc2ea10fbe4cfcf15f9f76006b31b99c20e5a9
---
 gdb/gdb_bfd.c          |   2 +-
 gdb/remote.c           |   2 +-
 gdb/target.c           |   2 +-
 gdbserver/hostio.cc    |   2 +-
 gdbserver/win32-low.cc |   2 +-
 gdbsupport/fileio.h    | 110 ++++++++++++++++++++++++++++++-
 include/gdb/fileio.h   | 144 -----------------------------------------
 7 files changed, 114 insertions(+), 150 deletions(-)
 delete mode 100644 include/gdb/fileio.h

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 6c03ae5ef053..1079623d79f7 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -30,7 +30,7 @@
 #endif
 #endif
 #include "target.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "inferior.h"
 #include "cli/cli-style.h"
 
diff --git a/gdb/remote.c b/gdb/remote.c
index 70f918a7362c..5bae65472ed0 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -59,7 +59,7 @@
 #include "gdbcore.h"
 
 #include "remote-fileio.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include <sys/stat.h>
 #include "xml-support.h"
 
diff --git a/gdb/target.c b/gdb/target.c
index 41144901fc19..9d698afb09dc 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -40,7 +40,7 @@
 #include "exec.h"
 #include "inline-frame.h"
 #include "tracepoint.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "gdbsupport/agent.h"
 #include "auxv.h"
 #include "target-debug.h"
diff --git a/gdbserver/hostio.cc b/gdbserver/hostio.cc
index 2d17b6d1a705..4f763f382bb0 100644
--- a/gdbserver/hostio.cc
+++ b/gdbserver/hostio.cc
@@ -19,7 +19,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "hostio.h"
 
 #include <fcntl.h>
diff --git a/gdbserver/win32-low.cc b/gdbserver/win32-low.cc
index 5e2c028d683e..af132f60215d 100644
--- a/gdbserver/win32-low.cc
+++ b/gdbserver/win32-low.cc
@@ -20,7 +20,7 @@
 
 #include "server.h"
 #include "regcache.h"
-#include "gdb/fileio.h"
+#include "gdbsupport/fileio.h"
 #include "mem-break.h"
 #include "win32-low.h"
 #include "gdbthread.h"
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 2b4f977a0206..9809c1623140 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -20,9 +20,117 @@
 #ifndef COMMON_FILEIO_H
 #define COMMON_FILEIO_H
 
-#include "gdb/fileio.h"
 #include <sys/stat.h>
 
+/* The following flags are defined to be independent of the host
+   as well as the target side implementation of these constants.
+   All constants are defined with a leading FILEIO_ in the name
+   to allow the usage of these constants together with the
+   corresponding implementation dependent constants in one module. */
+
+/* open(2) flags */
+#define FILEIO_O_RDONLY           0x0
+#define FILEIO_O_WRONLY           0x1
+#define FILEIO_O_RDWR             0x2
+#define FILEIO_O_APPEND           0x8
+#define FILEIO_O_CREAT          0x200
+#define FILEIO_O_TRUNC          0x400
+#define FILEIO_O_EXCL           0x800
+#define FILEIO_O_SUPPORTED      (FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
+				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
+				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
+				 FILEIO_O_EXCL)
+
+/* mode_t bits */
+#define FILEIO_S_IFREG        0100000
+#define FILEIO_S_IFDIR         040000
+#define FILEIO_S_IFCHR         020000
+#define FILEIO_S_IRUSR           0400
+#define FILEIO_S_IWUSR           0200
+#define FILEIO_S_IXUSR           0100
+#define FILEIO_S_IRWXU           0700
+#define FILEIO_S_IRGRP            040
+#define FILEIO_S_IWGRP            020
+#define FILEIO_S_IXGRP            010
+#define FILEIO_S_IRWXG            070
+#define FILEIO_S_IROTH             04
+#define FILEIO_S_IWOTH             02
+#define FILEIO_S_IXOTH             01
+#define FILEIO_S_IRWXO             07
+#define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
+				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
+                                    FILEIO_S_IRWXO)
+
+/* lseek(2) flags */
+#define FILEIO_SEEK_SET             0
+#define FILEIO_SEEK_CUR             1
+#define FILEIO_SEEK_END             2
+
+/* errno values */
+#define FILEIO_EPERM                1
+#define FILEIO_ENOENT               2
+#define FILEIO_EINTR                4
+#define FILEIO_EIO                  5
+#define FILEIO_EBADF                9
+#define FILEIO_EACCES              13
+#define FILEIO_EFAULT              14
+#define FILEIO_EBUSY               16
+#define FILEIO_EEXIST              17
+#define FILEIO_ENODEV              19
+#define FILEIO_ENOTDIR             20
+#define FILEIO_EISDIR              21
+#define FILEIO_EINVAL              22
+#define FILEIO_ENFILE              23
+#define FILEIO_EMFILE              24
+#define FILEIO_EFBIG               27
+#define FILEIO_ENOSPC              28
+#define FILEIO_ESPIPE              29
+#define FILEIO_EROFS               30
+#define FILEIO_ENOSYS              88
+#define FILEIO_ENAMETOOLONG        91
+#define FILEIO_EUNKNOWN          9999
+
+#define FIO_INT_LEN   4
+#define FIO_UINT_LEN  4
+#define FIO_MODE_LEN  4
+#define FIO_TIME_LEN  4
+#define FIO_LONG_LEN  8
+#define FIO_ULONG_LEN 8
+
+typedef char fio_int_t[FIO_INT_LEN];
+typedef char fio_uint_t[FIO_UINT_LEN];
+typedef char fio_mode_t[FIO_MODE_LEN];
+typedef char fio_time_t[FIO_TIME_LEN];
+typedef char fio_long_t[FIO_LONG_LEN];
+typedef char fio_ulong_t[FIO_ULONG_LEN];
+
+/* Struct stat as used in protocol.  For complete independence
+   of host/target systems, it's defined as an array with offsets
+   to the members. */
+
+struct fio_stat
+{
+  fio_uint_t  fst_dev;
+  fio_uint_t  fst_ino;
+  fio_mode_t  fst_mode;
+  fio_uint_t  fst_nlink;
+  fio_uint_t  fst_uid;
+  fio_uint_t  fst_gid;
+  fio_uint_t  fst_rdev;
+  fio_ulong_t fst_size;
+  fio_ulong_t fst_blksize;
+  fio_ulong_t fst_blocks;
+  fio_time_t  fst_atime;
+  fio_time_t  fst_mtime;
+  fio_time_t  fst_ctime;
+};
+
+struct fio_timeval
+{
+  fio_time_t  ftv_sec;
+  fio_long_t  ftv_usec;
+};
+
 /* Convert a host-format errno value to a File-I/O error number.  */
 
 extern int host_to_fileio_error (int error);
diff --git a/include/gdb/fileio.h b/include/gdb/fileio.h
deleted file mode 100644
index f472f828f722..000000000000
--- a/include/gdb/fileio.h
+++ /dev/null
@@ -1,144 +0,0 @@
-/* Hosted File I/O interface definitions, for GDB, the GNU Debugger.
-
-   Copyright (C) 2003-2022 Free Software Foundation, Inc.
-
-   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 GDB_FILEIO_H_
-#define GDB_FILEIO_H_
-
-/* The following flags are defined to be independent of the host
-   as well as the target side implementation of these constants.
-   All constants are defined with a leading FILEIO_ in the name
-   to allow the usage of these constants together with the
-   corresponding implementation dependent constants in one module. */
-
-/* open(2) flags */
-#define FILEIO_O_RDONLY           0x0
-#define FILEIO_O_WRONLY           0x1
-#define FILEIO_O_RDWR             0x2
-#define FILEIO_O_APPEND           0x8
-#define FILEIO_O_CREAT          0x200
-#define FILEIO_O_TRUNC          0x400
-#define FILEIO_O_EXCL           0x800
-#define FILEIO_O_SUPPORTED	(FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
-				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
-				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
-				 FILEIO_O_EXCL)
-
-/* mode_t bits */
-#define FILEIO_S_IFREG        0100000
-#define FILEIO_S_IFDIR         040000
-#define FILEIO_S_IFCHR         020000
-#define FILEIO_S_IRUSR           0400
-#define FILEIO_S_IWUSR           0200
-#define FILEIO_S_IXUSR           0100
-#define FILEIO_S_IRWXU           0700
-#define FILEIO_S_IRGRP            040
-#define FILEIO_S_IWGRP            020
-#define FILEIO_S_IXGRP            010
-#define FILEIO_S_IRWXG            070
-#define FILEIO_S_IROTH             04
-#define FILEIO_S_IWOTH             02
-#define FILEIO_S_IXOTH             01
-#define FILEIO_S_IRWXO             07
-#define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
-				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
-                                    FILEIO_S_IRWXO)
-
-/* lseek(2) flags */
-#define FILEIO_SEEK_SET             0
-#define FILEIO_SEEK_CUR             1
-#define FILEIO_SEEK_END             2
-
-/* errno values */
-#define FILEIO_EPERM                1
-#define FILEIO_ENOENT               2
-#define FILEIO_EINTR                4
-#define FILEIO_EIO                  5
-#define FILEIO_EBADF                9
-#define FILEIO_EACCES              13
-#define FILEIO_EFAULT              14
-#define FILEIO_EBUSY               16
-#define FILEIO_EEXIST              17
-#define FILEIO_ENODEV              19
-#define FILEIO_ENOTDIR             20
-#define FILEIO_EISDIR              21
-#define FILEIO_EINVAL              22
-#define FILEIO_ENFILE              23
-#define FILEIO_EMFILE              24
-#define FILEIO_EFBIG               27
-#define FILEIO_ENOSPC              28
-#define FILEIO_ESPIPE              29
-#define FILEIO_EROFS               30
-#define FILEIO_ENOSYS		   88
-#define FILEIO_ENAMETOOLONG        91
-#define FILEIO_EUNKNOWN          9999
-
-/* limits */
-#define FILEIO_INT_MIN    -2147483648L
-#define FILEIO_INT_MAX     2147483647L
-#define FILEIO_UINT_MAX    4294967295UL
-#define FILEIO_LONG_MIN   -9223372036854775808LL
-#define FILEIO_LONG_MAX    9223372036854775807LL
-#define FILEIO_ULONG_MAX   18446744073709551615ULL
-
-/* Integral types as used in protocol. */
-#if 0
-typedef __int32_t fio_int_t;
-typedef __uint32_t fio_uint_t, fio_mode_t, fio_time_t;
-typedef __int64_t fio_long_t;
-typedef __uint64_t fio_ulong_t;
-#endif
-
-#define FIO_INT_LEN   4
-#define FIO_UINT_LEN  4
-#define FIO_MODE_LEN  4
-#define FIO_TIME_LEN  4
-#define FIO_LONG_LEN  8
-#define FIO_ULONG_LEN 8
-
-typedef char fio_int_t[FIO_INT_LEN];   
-typedef char fio_uint_t[FIO_UINT_LEN];
-typedef char fio_mode_t[FIO_MODE_LEN];
-typedef char fio_time_t[FIO_TIME_LEN];
-typedef char fio_long_t[FIO_LONG_LEN];
-typedef char fio_ulong_t[FIO_ULONG_LEN];
-
-/* Struct stat as used in protocol.  For complete independence
-   of host/target systems, it's defined as an array with offsets
-   to the members. */
-
-struct fio_stat {
-  fio_uint_t  fst_dev;
-  fio_uint_t  fst_ino;
-  fio_mode_t  fst_mode;
-  fio_uint_t  fst_nlink;
-  fio_uint_t  fst_uid;
-  fio_uint_t  fst_gid;
-  fio_uint_t  fst_rdev;
-  fio_ulong_t fst_size;
-  fio_ulong_t fst_blksize;
-  fio_ulong_t fst_blocks;
-  fio_time_t  fst_atime;
-  fio_time_t  fst_mtime;
-  fio_time_t  fst_ctime;
-};
-
-struct fio_timeval {
-  fio_time_t  ftv_sec;
-  fio_long_t  ftv_usec;
-};
-
-#endif /* GDB_FILEIO_H_ */
-- 
2.37.2


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

* [PATCH v2 2/3] gdbsupport: convert FILEIO_* macros to an enum
  2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
@ 2022-08-29 16:29   ` Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h, cc} and rename Simon Marchi
  2 siblings, 0 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-29 16:29 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

From: Simon Marchi <simon.marchi@efficios.com>

Converting from free-form macros to an enum gives a bit of type-safety.
This caught places where we would assign host error numbers to what
should contain a target fileio error number, for instance in
target_fileio_pread.

I added the FILEIO_SUCCESS enumerator, because
remote.c:remote_hostio_parse_result initializes the remote_errno output
variable to 0.  It seems better to have an explicit enumerator than to
assign a value for which there is no enumerator.  I considered
initializing this variable to FILEIO_EUNKNOWN instead, such that if the
remote side replies with an error and omits the errno value, we'll get
an errno that represents an error instead of 0 (which reprensents no
error).  But it's not clear what the consequences of that change would
be, so I prefer to err on the side of caution and just keep the existing
behavior (there is no intended change in behavior with this patch).

Note that remote_hostio_parse_resul still reads blindly what the remote
side sends as a target errno into this variable, so we can still end up
with a nonsensical value here.  It's not good, but out of the scope of
this patch.

Convert host_to_fileio_error and fileio_errno_to_host to return / accept
a fileio_error instead of an int, and cascade the change in the whole
chain that uses that.

Change-Id: I454b0e3fcf0732447bc872252fa8e57d138b0e03
---
 gdb/gdb_bfd.c        | 11 +++---
 gdb/inf-child.c      | 14 ++++----
 gdb/inf-child.h      | 14 ++++----
 gdb/linux-nat.c      |  6 ++--
 gdb/linux-nat.h      |  6 ++--
 gdb/linux-tdep.c     |  2 +-
 gdb/remote.c         | 86 +++++++++++++++++++++++---------------------
 gdb/sparc64-tdep.c   |  8 ++---
 gdb/target.c         | 46 ++++++++++++------------
 gdb/target.h         | 29 +++++++--------
 gdbsupport/fileio.cc |  2 +-
 gdbsupport/fileio.h  | 50 ++++++++++++++------------
 12 files changed, 142 insertions(+), 132 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 1079623d79f7..46e2c357e3f4 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -305,7 +305,7 @@ gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
 /* Return the system error number corresponding to ERRNUM.  */
 
 static int
-fileio_errno_to_host (int errnum)
+fileio_errno_to_host (fileio_error errnum)
 {
   switch (errnum)
     {
@@ -369,7 +369,8 @@ static void *
 gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure)
 {
   const char *filename = bfd_get_filename (abfd);
-  int fd, target_errno;
+  int fd;
+  fileio_error target_errno;
   int *stream;
   gdb_bfd_open_closure *oclosure = (gdb_bfd_open_closure *) open_closure;
 
@@ -399,7 +400,7 @@ gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
 			    file_ptr nbytes, file_ptr offset)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
   file_ptr pos, bytes;
 
   pos = 0;
@@ -442,7 +443,7 @@ static int
 gdb_bfd_iovec_fileio_close (struct bfd *abfd, void *stream)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
 
   xfree (stream);
 
@@ -471,7 +472,7 @@ gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
 			    struct stat *sb)
 {
   int fd = *(int *) stream;
-  int target_errno;
+  fileio_error target_errno;
   int result;
 
   result = target_fileio_fstat (fd, sb, &target_errno);
diff --git a/gdb/inf-child.c b/gdb/inf-child.c
index 56ebd2a5549c..ca0af4c1b032 100644
--- a/gdb/inf-child.c
+++ b/gdb/inf-child.c
@@ -233,7 +233,7 @@ inf_child_target::pid_to_exec_file (int pid)
 int
 inf_child_target::fileio_open (struct inferior *inf, const char *filename,
 			       int flags, int mode, int warn_if_slow,
-			       int *target_errno)
+			       fileio_error *target_errno)
 {
   int nat_flags;
   mode_t nat_mode;
@@ -257,7 +257,7 @@ inf_child_target::fileio_open (struct inferior *inf, const char *filename,
 
 int
 inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				 ULONGEST offset, int *target_errno)
+				 ULONGEST offset, fileio_error *target_errno)
 {
   int ret;
 
@@ -284,7 +284,7 @@ inf_child_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
-				ULONGEST offset, int *target_errno)
+				ULONGEST offset, fileio_error *target_errno)
 {
   int ret;
 
@@ -310,7 +310,7 @@ inf_child_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* Implementation of to_fileio_fstat.  */
 
 int
-inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
+inf_child_target::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   int ret;
 
@@ -324,7 +324,7 @@ inf_child_target::fileio_fstat (int fd, struct stat *sb, int *target_errno)
 /* Implementation of to_fileio_close.  */
 
 int
-inf_child_target::fileio_close (int fd, int *target_errno)
+inf_child_target::fileio_close (int fd, fileio_error *target_errno)
 {
   int ret;
 
@@ -339,7 +339,7 @@ inf_child_target::fileio_close (int fd, int *target_errno)
 
 int
 inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
-				 int *target_errno)
+				 fileio_error *target_errno)
 {
   int ret;
 
@@ -354,7 +354,7 @@ inf_child_target::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 inf_child_target::fileio_readlink (struct inferior *inf, const char *filename,
-				   int *target_errno)
+				   fileio_error *target_errno)
 {
   /* We support readlink only on systems that also provide a compile-time
      maximum path length (PATH_MAX), at least for now.  */
diff --git a/gdb/inf-child.h b/gdb/inf-child.h
index ae5ace46f30e..167820380112 100644
--- a/gdb/inf-child.h
+++ b/gdb/inf-child.h
@@ -75,19 +75,19 @@ class inf_child_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
   int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		     ULONGEST offset, int *target_errno) override;
+		     ULONGEST offset, fileio_error *target_errno) override;
   int fileio_pread (int fd, gdb_byte *read_buf, int len,
-		    ULONGEST offset, int *target_errno) override;
-  int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
-  int fileio_close (int fd, int *target_errno) override;
+		    ULONGEST offset, fileio_error *target_errno) override;
+  int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno) override;
+  int fileio_close (int fd, fileio_error *target_errno) override;
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
   gdb::optional<std::string> fileio_readlink (struct inferior *inf,
 					      const char *filename,
-					      int *target_errno) override;
+					      fileio_error *target_errno) override;
   bool use_agent (bool use) override;
 
   bool can_use_agent () override;
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index e638e8ad04ee..d4639766088c 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4372,7 +4372,7 @@ linux_nat_fileio_pid_of (struct inferior *inf)
 int
 linux_nat_target::fileio_open (struct inferior *inf, const char *filename,
 			       int flags, int mode, int warn_if_slow,
-			       int *target_errno)
+			       fileio_error *target_errno)
 {
   int nat_flags;
   mode_t nat_mode;
@@ -4397,7 +4397,7 @@ linux_nat_target::fileio_open (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename,
-				   int *target_errno)
+				   fileio_error *target_errno)
 {
   char buf[PATH_MAX];
   int len;
@@ -4417,7 +4417,7 @@ linux_nat_target::fileio_readlink (struct inferior *inf, const char *filename,
 
 int
 linux_nat_target::fileio_unlink (struct inferior *inf, const char *filename,
-				 int *target_errno)
+				 fileio_error *target_errno)
 {
   int ret;
 
diff --git a/gdb/linux-nat.h b/gdb/linux-nat.h
index 38e253eff364..a9b91a5e908b 100644
--- a/gdb/linux-nat.h
+++ b/gdb/linux-nat.h
@@ -101,16 +101,16 @@ class linux_nat_target : public inf_ptrace_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
 
   gdb::optional<std::string>
     fileio_readlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   int insert_fork_catchpoint (int) override;
   int remove_fork_catchpoint (int) override;
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 5fe6be16c311..cde98e1a8c18 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -813,7 +813,7 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
   int status_f = (what == IP_STATUS || what == IP_ALL);
   int stat_f = (what == IP_STAT || what == IP_ALL);
   char filename[100];
-  int target_errno;
+  fileio_error target_errno;
 
   if (args && isdigit (args[0]))
     {
diff --git a/gdb/remote.c b/gdb/remote.c
index 5bae65472ed0..b57d26a70edb 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -560,26 +560,26 @@ class remote_target : public process_stratum_target
 
   int fileio_open (struct inferior *inf, const char *filename,
 		   int flags, int mode, int warn_if_slow,
-		   int *target_errno) override;
+		   fileio_error *target_errno) override;
 
   int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		     ULONGEST offset, int *target_errno) override;
+		     ULONGEST offset, fileio_error *target_errno) override;
 
   int fileio_pread (int fd, gdb_byte *read_buf, int len,
-		    ULONGEST offset, int *target_errno) override;
+		    ULONGEST offset, fileio_error *target_errno) override;
 
-  int fileio_fstat (int fd, struct stat *sb, int *target_errno) override;
+  int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno) override;
 
-  int fileio_close (int fd, int *target_errno) override;
+  int fileio_close (int fd, fileio_error *target_errno) override;
 
   int fileio_unlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   gdb::optional<std::string>
     fileio_readlink (struct inferior *inf,
 		     const char *filename,
-		     int *target_errno) override;
+		     fileio_error *target_errno) override;
 
   bool supports_enable_disable_tracepoint () override;
 
@@ -701,25 +701,25 @@ class remote_target : public process_stratum_target
   void remote_file_delete (const char *remote_file, int from_tty);
 
   int remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
-			   ULONGEST offset, int *remote_errno);
+			   ULONGEST offset, fileio_error *remote_errno);
   int remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			    ULONGEST offset, int *remote_errno);
+			    ULONGEST offset, fileio_error *remote_errno);
   int remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
-				 ULONGEST offset, int *remote_errno);
+				 ULONGEST offset, fileio_error *remote_errno);
 
   int remote_hostio_send_command (int command_bytes, int which_packet,
-				  int *remote_errno, const char **attachment,
+				  fileio_error *remote_errno, const char **attachment,
 				  int *attachment_len);
   int remote_hostio_set_filesystem (struct inferior *inf,
-				    int *remote_errno);
+				    fileio_error *remote_errno);
   /* We should get rid of this and use fileio_open directly.  */
   int remote_hostio_open (struct inferior *inf, const char *filename,
 			  int flags, int mode, int warn_if_slow,
-			  int *remote_errno);
-  int remote_hostio_close (int fd, int *remote_errno);
+			  fileio_error *remote_errno);
+  int remote_hostio_close (int fd, fileio_error *remote_errno);
 
   int remote_hostio_unlink (inferior *inf, const char *filename,
-			    int *remote_errno);
+			    fileio_error *remote_errno);
 
   struct remote_state *get_remote_state ();
 
@@ -12130,7 +12130,7 @@ remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
 }
 
 /* Parse an I/O result packet from BUFFER.  Set RETCODE to the return
-   value, *REMOTE_ERRNO to the remote error number or zero if none
+   value, *REMOTE_ERRNO to the remote error number or FILEIO_SUCCESS if none
    was included, and *ATTACHMENT to point to the start of the annex
    if any.  The length of the packet isn't needed here; there may
    be NUL bytes in BUFFER, but they will be after *ATTACHMENT.
@@ -12140,11 +12140,11 @@ remote_buffer_add_int (char **buffer, int *left, ULONGEST value)
 
 static int
 remote_hostio_parse_result (const char *buffer, int *retcode,
-			    int *remote_errno, const char **attachment)
+			    fileio_error *remote_errno, const char **attachment)
 {
   char *p, *p2;
 
-  *remote_errno = 0;
+  *remote_errno = FILEIO_SUCCESS;
   *attachment = NULL;
 
   if (buffer[0] != 'F')
@@ -12159,7 +12159,7 @@ remote_hostio_parse_result (const char *buffer, int *retcode,
   if (*p == ',')
     {
       errno = 0;
-      *remote_errno = strtol (p + 1, &p2, 16);
+      *remote_errno = (fileio_error) strtol (p + 1, &p2, 16);
       if (errno != 0 || p + 1 == p2)
 	return -1;
       p = p2;
@@ -12196,7 +12196,7 @@ remote_hostio_parse_result (const char *buffer, int *retcode,
 
 int
 remote_target::remote_hostio_send_command (int command_bytes, int which_packet,
-					   int *remote_errno, const char **attachment,
+					   fileio_error *remote_errno, const char **attachment,
 					   int *attachment_len)
 {
   struct remote_state *rs = get_remote_state ();
@@ -12281,7 +12281,7 @@ readahead_cache::invalidate_fd (int fd)
 
 int
 remote_target::remote_hostio_set_filesystem (struct inferior *inf,
-					     int *remote_errno)
+					     fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   int required_pid = (inf == NULL || inf->fake_pid_p) ? 0 : inf->pid;
@@ -12318,7 +12318,7 @@ remote_target::remote_hostio_set_filesystem (struct inferior *inf,
 int
 remote_target::remote_hostio_open (inferior *inf, const char *filename,
 				   int flags, int mode, int warn_if_slow,
-				   int *remote_errno)
+				   fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12361,7 +12361,7 @@ remote_target::remote_hostio_open (inferior *inf, const char *filename,
 int
 remote_target::fileio_open (struct inferior *inf, const char *filename,
 			    int flags, int mode, int warn_if_slow,
-			    int *remote_errno)
+			    fileio_error *remote_errno)
 {
   return remote_hostio_open (inf, filename, flags, mode, warn_if_slow,
 			     remote_errno);
@@ -12371,7 +12371,7 @@ remote_target::fileio_open (struct inferior *inf, const char *filename,
 
 int
 remote_target::remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				     ULONGEST offset, int *remote_errno)
+				     ULONGEST offset, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12398,7 +12398,7 @@ remote_target::remote_hostio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			      ULONGEST offset, int *remote_errno)
+			      ULONGEST offset, fileio_error *remote_errno)
 {
   return remote_hostio_pwrite (fd, write_buf, len, offset, remote_errno);
 }
@@ -12408,7 +12408,7 @@ remote_target::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 remote_target::remote_hostio_pread_vFile (int fd, gdb_byte *read_buf, int len,
-					  ULONGEST offset, int *remote_errno)
+					  ULONGEST offset, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12468,7 +12468,7 @@ readahead_cache::pread (int fd, gdb_byte *read_buf, size_t len,
 
 int
 remote_target::remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
-				    ULONGEST offset, int *remote_errno)
+				    ULONGEST offset, fileio_error *remote_errno)
 {
   int ret;
   struct remote_state *rs = get_remote_state ();
@@ -12508,7 +12508,7 @@ remote_target::remote_hostio_pread (int fd, gdb_byte *read_buf, int len,
 
 int
 remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
-			     ULONGEST offset, int *remote_errno)
+			     ULONGEST offset, fileio_error *remote_errno)
 {
   return remote_hostio_pread (fd, read_buf, len, offset, remote_errno);
 }
@@ -12516,7 +12516,7 @@ remote_target::fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* Implementation of to_fileio_close.  */
 
 int
-remote_target::remote_hostio_close (int fd, int *remote_errno)
+remote_target::remote_hostio_close (int fd, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12533,7 +12533,7 @@ remote_target::remote_hostio_close (int fd, int *remote_errno)
 }
 
 int
-remote_target::fileio_close (int fd, int *remote_errno)
+remote_target::fileio_close (int fd, fileio_error *remote_errno)
 {
   return remote_hostio_close (fd, remote_errno);
 }
@@ -12542,7 +12542,7 @@ remote_target::fileio_close (int fd, int *remote_errno)
 
 int
 remote_target::remote_hostio_unlink (inferior *inf, const char *filename,
-				     int *remote_errno)
+				     fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12562,7 +12562,7 @@ remote_target::remote_hostio_unlink (inferior *inf, const char *filename,
 
 int
 remote_target::fileio_unlink (struct inferior *inf, const char *filename,
-			      int *remote_errno)
+			      fileio_error *remote_errno)
 {
   return remote_hostio_unlink (inf, filename, remote_errno);
 }
@@ -12571,7 +12571,7 @@ remote_target::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 remote_target::fileio_readlink (struct inferior *inf, const char *filename,
-				int *remote_errno)
+				fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12608,7 +12608,7 @@ remote_target::fileio_readlink (struct inferior *inf, const char *filename,
 /* Implementation of to_fileio_fstat.  */
 
 int
-remote_target::fileio_fstat (int fd, struct stat *st, int *remote_errno)
+remote_target::fileio_fstat (int fd, struct stat *st, fileio_error *remote_errno)
 {
   struct remote_state *rs = get_remote_state ();
   char *p = rs->buf.data ();
@@ -12680,7 +12680,8 @@ remote_target::filesystem_is_local ()
 
       if (ps == PACKET_SUPPORT_UNKNOWN)
 	{
-	  int fd, remote_errno;
+	  int fd;
+	  fileio_error remote_errno;
 
 	  /* Try opening a file to probe support.  The supplied
 	     filename is irrelevant, we only care about whether
@@ -12715,7 +12716,7 @@ remote_target::filesystem_is_local ()
 }
 
 static int
-remote_fileio_errno_to_host (int errnum)
+remote_fileio_errno_to_host (fileio_error errnum)
 {
   switch (errnum)
     {
@@ -12766,7 +12767,7 @@ remote_fileio_errno_to_host (int errnum)
 }
 
 static char *
-remote_hostio_error (int errnum)
+remote_hostio_error (fileio_error errnum)
 {
   int host_error = remote_fileio_errno_to_host (errnum);
 
@@ -12792,7 +12793,7 @@ class scoped_remote_fd
       {
 	try
 	  {
-	    int remote_errno;
+	    fileio_error remote_errno;
 	    m_remote->remote_hostio_close (m_fd, &remote_errno);
 	  }
 	catch (...)
@@ -12843,7 +12844,8 @@ void
 remote_target::remote_file_put (const char *local_file, const char *remote_file,
 				int from_tty)
 {
-  int retcode, remote_errno, bytes, io_size;
+  int retcode, bytes, io_size;
+  fileio_error remote_errno;
   int bytes_in_buffer;
   int saw_eof;
   ULONGEST offset;
@@ -12935,7 +12937,8 @@ void
 remote_target::remote_file_get (const char *remote_file, const char *local_file,
 				int from_tty)
 {
-  int remote_errno, bytes, io_size;
+  fileio_error remote_errno;
+  int bytes, io_size;
   ULONGEST offset;
 
   scoped_remote_fd fd
@@ -12993,7 +12996,8 @@ remote_file_delete (const char *remote_file, int from_tty)
 void
 remote_target::remote_file_delete (const char *remote_file, int from_tty)
 {
-  int retcode, remote_errno;
+  int retcode;
+  fileio_error remote_errno;
 
   retcode = remote_hostio_unlink (NULL, remote_file, &remote_errno);
   if (retcode == -1)
diff --git a/gdb/sparc64-tdep.c b/gdb/sparc64-tdep.c
index 5ca5f2dca8c7..3842bf5a5567 100644
--- a/gdb/sparc64-tdep.c
+++ b/gdb/sparc64-tdep.c
@@ -166,7 +166,7 @@ get_adi_info (pid_t pid)
 void
 sparc64_forget_process (pid_t pid)
 {
-  int target_errno;
+  fileio_error target_errno;
 
   for (auto pit = adi_proc_list.before_begin (),
 	 it = std::next (pit);
@@ -287,7 +287,7 @@ adi_tag_fd (void)
 
   char cl_name[MAX_PROC_NAME_SIZE];
   snprintf (cl_name, sizeof(cl_name), "/proc/%ld/adi/tags", (long) pid);
-  int target_errno;
+  fileio_error target_errno;
   proc->stat.tag_fd = target_fileio_open (NULL, cl_name, O_RDWR|O_EXCL, 
 					  false, 0, &target_errno);
   return proc->stat.tag_fd;
@@ -350,7 +350,7 @@ adi_read_versions (CORE_ADDR vaddr, size_t size, gdb_byte *tags)
 	    paddress (target_gdbarch (), vaddr * ast.blksize));
     }
 
-  int target_errno;
+  fileio_error target_errno;
   return target_fileio_pread (fd, tags, size, vaddr, &target_errno);
 }
 
@@ -371,7 +371,7 @@ adi_write_versions (CORE_ADDR vaddr, size_t size, unsigned char *tags)
 	    paddress (target_gdbarch (), vaddr * ast.blksize));
     }
 
-  int target_errno;
+  fileio_error target_errno;
   return target_fileio_pwrite (fd, tags, size, vaddr, &target_errno);
 }
 
diff --git a/gdb/target.c b/gdb/target.c
index 9d698afb09dc..28560983625b 100644
--- a/gdb/target.c
+++ b/gdb/target.c
@@ -3206,7 +3206,7 @@ fileio_fd_to_fh (int fd)
 int
 target_ops::fileio_open (struct inferior *inf, const char *filename,
 			 int flags, int mode, int warn_if_slow,
-			 int *target_errno)
+			 fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3214,7 +3214,7 @@ target_ops::fileio_open (struct inferior *inf, const char *filename,
 
 int
 target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			   ULONGEST offset, int *target_errno)
+			   ULONGEST offset, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3222,21 +3222,21 @@ target_ops::fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 target_ops::fileio_pread (int fd, gdb_byte *read_buf, int len,
-			  ULONGEST offset, int *target_errno)
+			  ULONGEST offset, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
 }
 
 int
-target_ops::fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_ops::fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
 }
 
 int
-target_ops::fileio_close (int fd, int *target_errno)
+target_ops::fileio_close (int fd, fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3244,7 +3244,7 @@ target_ops::fileio_close (int fd, int *target_errno)
 
 int
 target_ops::fileio_unlink (struct inferior *inf, const char *filename,
-			   int *target_errno)
+			   fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return -1;
@@ -3252,7 +3252,7 @@ target_ops::fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 target_ops::fileio_readlink (struct inferior *inf, const char *filename,
-			     int *target_errno)
+			     fileio_error *target_errno)
 {
   *target_errno = FILEIO_ENOSYS;
   return {};
@@ -3262,7 +3262,7 @@ target_ops::fileio_readlink (struct inferior *inf, const char *filename,
 
 int
 target_fileio_open (struct inferior *inf, const char *filename,
-		    int flags, int mode, bool warn_if_slow, int *target_errno)
+		    int flags, int mode, bool warn_if_slow, fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3296,15 +3296,15 @@ target_fileio_open (struct inferior *inf, const char *filename,
 
 int
 target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-		      ULONGEST offset, int *target_errno)
+		      ULONGEST offset, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_pwrite (fh->target_fd, write_buf,
 				     len, offset, target_errno);
@@ -3322,15 +3322,15 @@ target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
 
 int
 target_fileio_pread (int fd, gdb_byte *read_buf, int len,
-		     ULONGEST offset, int *target_errno)
+		     ULONGEST offset, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_pread (fh->target_fd, read_buf,
 				    len, offset, target_errno);
@@ -3347,15 +3347,15 @@ target_fileio_pread (int fd, gdb_byte *read_buf, int len,
 /* See target.h.  */
 
 int
-target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
+target_fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else if (fh->target == NULL)
-    *target_errno = EIO;
+    *target_errno = FILEIO_EIO;
   else
     ret = fh->target->fileio_fstat (fh->target_fd, sb, target_errno);
 
@@ -3369,13 +3369,13 @@ target_fileio_fstat (int fd, struct stat *sb, int *target_errno)
 /* See target.h.  */
 
 int
-target_fileio_close (int fd, int *target_errno)
+target_fileio_close (int fd, fileio_error *target_errno)
 {
   fileio_fh_t *fh = fileio_fd_to_fh (fd);
   int ret = -1;
 
   if (fh->is_closed ())
-    *target_errno = EBADF;
+    *target_errno = FILEIO_EBADF;
   else
     {
       if (fh->target != NULL)
@@ -3397,7 +3397,7 @@ target_fileio_close (int fd, int *target_errno)
 
 int
 target_fileio_unlink (struct inferior *inf, const char *filename,
-		      int *target_errno)
+		      fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3423,7 +3423,7 @@ target_fileio_unlink (struct inferior *inf, const char *filename,
 
 gdb::optional<std::string>
 target_fileio_readlink (struct inferior *inf, const char *filename,
-			int *target_errno)
+			fileio_error *target_errno)
 {
   for (target_ops *t = default_fileio_target (); t != NULL; t = t->beneath ())
     {
@@ -3461,7 +3461,7 @@ class scoped_target_fd
   {
     if (m_fd >= 0)
       {
-	int target_errno;
+	fileio_error target_errno;
 
 	target_fileio_close (m_fd, &target_errno);
       }
@@ -3493,7 +3493,7 @@ target_fileio_read_alloc_1 (struct inferior *inf, const char *filename,
   size_t buf_alloc, buf_pos;
   gdb_byte *buf;
   LONGEST n;
-  int target_errno;
+  fileio_error target_errno;
 
   scoped_target_fd fd (target_fileio_open (inf, filename, FILEIO_O_RDONLY,
 					   0700, false, &target_errno));
diff --git a/gdb/target.h b/gdb/target.h
index 7e52716a9e83..0b784278c4f8 100644
--- a/gdb/target.h
+++ b/gdb/target.h
@@ -81,6 +81,7 @@ struct inferior;
 #include "command.h"
 #include "disasm-flags.h"
 #include "tracepoint.h"
+#include "gdbsupport/fileio.h"
 
 #include "gdbsupport/break-common.h" /* For enum target_hw_bp_type.  */
 
@@ -952,28 +953,28 @@ struct target_ops
        *TARGET_ERRNO).  */
     virtual int fileio_open (struct inferior *inf, const char *filename,
 			     int flags, int mode, int warn_if_slow,
-			     int *target_errno);
+			     fileio_error *target_errno);
 
     /* Write up to LEN bytes from WRITE_BUF to FD on the target.
        Return the number of bytes written, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
     virtual int fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-			       ULONGEST offset, int *target_errno);
+			       ULONGEST offset, fileio_error *target_errno);
 
     /* Read up to LEN bytes FD on the target into READ_BUF.
        Return the number of bytes read, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
     virtual int fileio_pread (int fd, gdb_byte *read_buf, int len,
-			      ULONGEST offset, int *target_errno);
+			      ULONGEST offset, fileio_error *target_errno);
 
     /* Get information about the file opened as FD and put it in
        SB.  Return 0 on success, or -1 if an error occurs (and set
        *TARGET_ERRNO).  */
-    virtual int fileio_fstat (int fd, struct stat *sb, int *target_errno);
+    virtual int fileio_fstat (int fd, struct stat *sb, fileio_error *target_errno);
 
     /* Close FD on the target.  Return 0, or -1 if an error occurs
        (and set *TARGET_ERRNO).  */
-    virtual int fileio_close (int fd, int *target_errno);
+    virtual int fileio_close (int fd, fileio_error *target_errno);
 
     /* Unlink FILENAME on the target, in the filesystem as seen by
        INF.  If INF is NULL, use the filesystem seen by the debugger
@@ -981,7 +982,7 @@ struct target_ops
        -1 if an error occurs (and set *TARGET_ERRNO).  */
     virtual int fileio_unlink (struct inferior *inf,
 			       const char *filename,
-			       int *target_errno);
+			       fileio_error *target_errno);
 
     /* Read value of symbolic link FILENAME on the target, in the
        filesystem as seen by INF.  If INF is NULL, use the filesystem
@@ -990,7 +991,7 @@ struct target_ops
        occurs (and set *TARGET_ERRNO).  */
     virtual gdb::optional<std::string> fileio_readlink (struct inferior *inf,
 							const char *filename,
-							int *target_errno);
+							fileio_error *target_errno);
 
     /* Implement the "info proc" command.  Returns true if the target
        actually implemented the command, false otherwise.  */
@@ -2165,29 +2166,29 @@ extern bool target_filesystem_is_local ();
 extern int target_fileio_open (struct inferior *inf,
 			       const char *filename, int flags,
 			       int mode, bool warn_if_slow,
-			       int *target_errno);
+			       fileio_error *target_errno);
 
 /* Write up to LEN bytes from WRITE_BUF to FD on the target.
    Return the number of bytes written, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
 extern int target_fileio_pwrite (int fd, const gdb_byte *write_buf, int len,
-				 ULONGEST offset, int *target_errno);
+				 ULONGEST offset, fileio_error *target_errno);
 
 /* Read up to LEN bytes FD on the target into READ_BUF.
    Return the number of bytes read, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
 extern int target_fileio_pread (int fd, gdb_byte *read_buf, int len,
-				ULONGEST offset, int *target_errno);
+				ULONGEST offset, fileio_error *target_errno);
 
 /* Get information about the file opened as FD on the target
    and put it in SB.  Return 0 on success, or -1 if an error
    occurs (and set *TARGET_ERRNO).  */
 extern int target_fileio_fstat (int fd, struct stat *sb,
-				int *target_errno);
+				fileio_error *target_errno);
 
 /* Close FD on the target.  Return 0, or -1 if an error occurs
    (and set *TARGET_ERRNO).  */
-extern int target_fileio_close (int fd, int *target_errno);
+extern int target_fileio_close (int fd, fileio_error *target_errno);
 
 /* Unlink FILENAME on the target, in the filesystem as seen by INF.
    If INF is NULL, use the filesystem seen by the debugger (GDB or,
@@ -2195,7 +2196,7 @@ extern int target_fileio_close (int fd, int *target_errno);
    occurs (and set *TARGET_ERRNO).  */
 extern int target_fileio_unlink (struct inferior *inf,
 				 const char *filename,
-				 int *target_errno);
+				 fileio_error *target_errno);
 
 /* Read value of symbolic link FILENAME on the target, in the
    filesystem as seen by INF.  If INF is NULL, use the filesystem seen
@@ -2203,7 +2204,7 @@ extern int target_fileio_unlink (struct inferior *inf,
    Return a null-terminated string allocated via xmalloc, or NULL if
    an error occurs (and set *TARGET_ERRNO).  */
 extern gdb::optional<std::string> target_fileio_readlink
-    (struct inferior *inf, const char *filename, int *target_errno);
+    (struct inferior *inf, const char *filename, fileio_error *target_errno);
 
 /* Read target file FILENAME, in the filesystem as seen by INF.  If
    INF is NULL, use the filesystem seen by the debugger (GDB or, for
diff --git a/gdbsupport/fileio.cc b/gdbsupport/fileio.cc
index b4feb8583ab0..db7c1a7c488d 100644
--- a/gdbsupport/fileio.cc
+++ b/gdbsupport/fileio.cc
@@ -24,7 +24,7 @@
 
 /* See fileio.h.  */
 
-int
+fileio_error
 host_to_fileio_error (int error)
 {
   switch (error)
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 9809c1623140..6a5297c64330 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -67,28 +67,32 @@
 #define FILEIO_SEEK_END             2
 
 /* errno values */
-#define FILEIO_EPERM                1
-#define FILEIO_ENOENT               2
-#define FILEIO_EINTR                4
-#define FILEIO_EIO                  5
-#define FILEIO_EBADF                9
-#define FILEIO_EACCES              13
-#define FILEIO_EFAULT              14
-#define FILEIO_EBUSY               16
-#define FILEIO_EEXIST              17
-#define FILEIO_ENODEV              19
-#define FILEIO_ENOTDIR             20
-#define FILEIO_EISDIR              21
-#define FILEIO_EINVAL              22
-#define FILEIO_ENFILE              23
-#define FILEIO_EMFILE              24
-#define FILEIO_EFBIG               27
-#define FILEIO_ENOSPC              28
-#define FILEIO_ESPIPE              29
-#define FILEIO_EROFS               30
-#define FILEIO_ENOSYS              88
-#define FILEIO_ENAMETOOLONG        91
-#define FILEIO_EUNKNOWN          9999
+enum fileio_error
+{
+  FILEIO_SUCCESS      =    0,
+  FILEIO_EPERM        =    1,
+  FILEIO_ENOENT       =    2,
+  FILEIO_EINTR        =    4,
+  FILEIO_EIO          =    5,
+  FILEIO_EBADF        =    9,
+  FILEIO_EACCES       =   13,
+  FILEIO_EFAULT       =   14,
+  FILEIO_EBUSY        =   16,
+  FILEIO_EEXIST       =   17,
+  FILEIO_ENODEV       =   19,
+  FILEIO_ENOTDIR      =   20,
+  FILEIO_EISDIR       =   21,
+  FILEIO_EINVAL       =   22,
+  FILEIO_ENFILE       =   23,
+  FILEIO_EMFILE       =   24,
+  FILEIO_EFBIG        =   27,
+  FILEIO_ENOSPC       =   28,
+  FILEIO_ESPIPE       =   29,
+  FILEIO_EROFS        =   30,
+  FILEIO_ENOSYS       =   88,
+  FILEIO_ENAMETOOLONG =   91,
+  FILEIO_EUNKNOWN     = 9999,
+};
 
 #define FIO_INT_LEN   4
 #define FIO_UINT_LEN  4
@@ -133,7 +137,7 @@ struct fio_timeval
 
 /* Convert a host-format errno value to a File-I/O error number.  */
 
-extern int host_to_fileio_error (int error);
+extern fileio_error host_to_fileio_error (int error);
 
 /* Convert File-I/O open flags FFLAGS to host format, storing
    the result in *FLAGS.  Return 0 on success, -1 on error.  */
-- 
2.37.2


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

* [PATCH v2 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h, cc} and rename
  2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
  2022-08-29 16:29   ` [PATCH v2 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
@ 2022-08-29 16:29   ` Simon Marchi
  2 siblings, 0 replies; 8+ messages in thread
From: Simon Marchi @ 2022-08-29 16:29 UTC (permalink / raw)
  To: gdb-patches

gdb_bfd.c and remote.c contain identical implementations of a
fileio_error -> errno function.  Factor that out to
gdbsupport/fileio.{h,cc}.

Rename it fileio_error_to_host, for symmetry with host_to_fileio_error.

Change-Id: Ib9b8807683de2f809c94a5303e708acc2251a0df
---
 gdb/gdb_bfd.c        | 59 +++-----------------------------------------
 gdb/remote.c         | 53 +--------------------------------------
 gdbsupport/fileio.cc | 53 +++++++++++++++++++++++++++++++++++++++
 gdbsupport/fileio.h  |  4 +++
 4 files changed, 61 insertions(+), 108 deletions(-)

diff --git a/gdb/gdb_bfd.c b/gdb/gdb_bfd.c
index 46e2c357e3f4..58244708eb07 100644
--- a/gdb/gdb_bfd.c
+++ b/gdb/gdb_bfd.c
@@ -302,59 +302,6 @@ gdb_bfd_open_from_target_memory (CORE_ADDR addr, ULONGEST size,
 			      mem_bfd_iovec_stat);
 }
 
-/* Return the system error number corresponding to ERRNUM.  */
-
-static int
-fileio_errno_to_host (fileio_error errnum)
-{
-  switch (errnum)
-    {
-      case FILEIO_EPERM:
-	return EPERM;
-      case FILEIO_ENOENT:
-	return ENOENT;
-      case FILEIO_EINTR:
-	return EINTR;
-      case FILEIO_EIO:
-	return EIO;
-      case FILEIO_EBADF:
-	return EBADF;
-      case FILEIO_EACCES:
-	return EACCES;
-      case FILEIO_EFAULT:
-	return EFAULT;
-      case FILEIO_EBUSY:
-	return EBUSY;
-      case FILEIO_EEXIST:
-	return EEXIST;
-      case FILEIO_ENODEV:
-	return ENODEV;
-      case FILEIO_ENOTDIR:
-	return ENOTDIR;
-      case FILEIO_EISDIR:
-	return EISDIR;
-      case FILEIO_EINVAL:
-	return EINVAL;
-      case FILEIO_ENFILE:
-	return ENFILE;
-      case FILEIO_EMFILE:
-	return EMFILE;
-      case FILEIO_EFBIG:
-	return EFBIG;
-      case FILEIO_ENOSPC:
-	return ENOSPC;
-      case FILEIO_ESPIPE:
-	return ESPIPE;
-      case FILEIO_EROFS:
-	return EROFS;
-      case FILEIO_ENOSYS:
-	return ENOSYS;
-      case FILEIO_ENAMETOOLONG:
-	return ENAMETOOLONG;
-    }
-  return -1;
-}
-
 /* bfd_openr_iovec OPEN_CLOSURE data for gdb_bfd_open.  */
 struct gdb_bfd_open_closure
 {
@@ -382,7 +329,7 @@ gdb_bfd_iovec_fileio_open (struct bfd *abfd, void *open_closure)
 			   &target_errno);
   if (fd == -1)
     {
-      errno = fileio_errno_to_host (target_errno);
+      errno = fileio_error_to_host (target_errno);
       bfd_set_error (bfd_error_system_call);
       return NULL;
     }
@@ -416,7 +363,7 @@ gdb_bfd_iovec_fileio_pread (struct bfd *abfd, void *stream, void *buf,
 	break;
       if (bytes == -1)
 	{
-	  errno = fileio_errno_to_host (target_errno);
+	  errno = fileio_error_to_host (target_errno);
 	  bfd_set_error (bfd_error_system_call);
 	  return -1;
 	}
@@ -478,7 +425,7 @@ gdb_bfd_iovec_fileio_fstat (struct bfd *abfd, void *stream,
   result = target_fileio_fstat (fd, sb, &target_errno);
   if (result == -1)
     {
-      errno = fileio_errno_to_host (target_errno);
+      errno = fileio_error_to_host (target_errno);
       bfd_set_error (bfd_error_system_call);
     }
 
diff --git a/gdb/remote.c b/gdb/remote.c
index b57d26a70edb..2f6cb2d01eeb 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -12715,61 +12715,10 @@ remote_target::filesystem_is_local ()
   return false;
 }
 
-static int
-remote_fileio_errno_to_host (fileio_error errnum)
-{
-  switch (errnum)
-    {
-      case FILEIO_EPERM:
-	return EPERM;
-      case FILEIO_ENOENT:
-	return ENOENT;
-      case FILEIO_EINTR:
-	return EINTR;
-      case FILEIO_EIO:
-	return EIO;
-      case FILEIO_EBADF:
-	return EBADF;
-      case FILEIO_EACCES:
-	return EACCES;
-      case FILEIO_EFAULT:
-	return EFAULT;
-      case FILEIO_EBUSY:
-	return EBUSY;
-      case FILEIO_EEXIST:
-	return EEXIST;
-      case FILEIO_ENODEV:
-	return ENODEV;
-      case FILEIO_ENOTDIR:
-	return ENOTDIR;
-      case FILEIO_EISDIR:
-	return EISDIR;
-      case FILEIO_EINVAL:
-	return EINVAL;
-      case FILEIO_ENFILE:
-	return ENFILE;
-      case FILEIO_EMFILE:
-	return EMFILE;
-      case FILEIO_EFBIG:
-	return EFBIG;
-      case FILEIO_ENOSPC:
-	return ENOSPC;
-      case FILEIO_ESPIPE:
-	return ESPIPE;
-      case FILEIO_EROFS:
-	return EROFS;
-      case FILEIO_ENOSYS:
-	return ENOSYS;
-      case FILEIO_ENAMETOOLONG:
-	return ENAMETOOLONG;
-    }
-  return -1;
-}
-
 static char *
 remote_hostio_error (fileio_error errnum)
 {
-  int host_error = remote_fileio_errno_to_host (errnum);
+  int host_error = fileio_error_to_host (errnum);
 
   if (host_error == -1)
     error (_("Unknown remote I/O error %d"), errnum);
diff --git a/gdbsupport/fileio.cc b/gdbsupport/fileio.cc
index db7c1a7c488d..60a08f649770 100644
--- a/gdbsupport/fileio.cc
+++ b/gdbsupport/fileio.cc
@@ -77,6 +77,59 @@ host_to_fileio_error (int error)
 
 /* See fileio.h.  */
 
+int
+fileio_error_to_host (fileio_error errnum)
+{
+  switch (errnum)
+    {
+      case FILEIO_EPERM:
+	return EPERM;
+      case FILEIO_ENOENT:
+	return ENOENT;
+      case FILEIO_EINTR:
+	return EINTR;
+      case FILEIO_EIO:
+	return EIO;
+      case FILEIO_EBADF:
+	return EBADF;
+      case FILEIO_EACCES:
+	return EACCES;
+      case FILEIO_EFAULT:
+	return EFAULT;
+      case FILEIO_EBUSY:
+	return EBUSY;
+      case FILEIO_EEXIST:
+	return EEXIST;
+      case FILEIO_ENODEV:
+	return ENODEV;
+      case FILEIO_ENOTDIR:
+	return ENOTDIR;
+      case FILEIO_EISDIR:
+	return EISDIR;
+      case FILEIO_EINVAL:
+	return EINVAL;
+      case FILEIO_ENFILE:
+	return ENFILE;
+      case FILEIO_EMFILE:
+	return EMFILE;
+      case FILEIO_EFBIG:
+	return EFBIG;
+      case FILEIO_ENOSPC:
+	return ENOSPC;
+      case FILEIO_ESPIPE:
+	return ESPIPE;
+      case FILEIO_EROFS:
+	return EROFS;
+      case FILEIO_ENOSYS:
+	return ENOSYS;
+      case FILEIO_ENAMETOOLONG:
+	return ENAMETOOLONG;
+    }
+  return -1;
+}
+
+/* See fileio.h.  */
+
 int
 fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
 {
diff --git a/gdbsupport/fileio.h b/gdbsupport/fileio.h
index 6a5297c64330..203e671f3e73 100644
--- a/gdbsupport/fileio.h
+++ b/gdbsupport/fileio.h
@@ -139,6 +139,10 @@ struct fio_timeval
 
 extern fileio_error host_to_fileio_error (int error);
 
+/* Convert a File-I/O error number to a host-format errno value.  */
+
+extern int fileio_error_to_host (fileio_error errnum);
+
 /* Convert File-I/O open flags FFLAGS to host format, storing
    the result in *FLAGS.  Return 0 on success, -1 on error.  */
 
-- 
2.37.2


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

* Re: [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h
  2022-08-29 16:29   ` [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
@ 2022-09-21 18:13     ` Simon Marchi
  0 siblings, 0 replies; 8+ messages in thread
From: Simon Marchi @ 2022-09-21 18:13 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches; +Cc: Simon Marchi



On 2022-08-29 12:29, Simon Marchi via Gdb-patches wrote:
> From: Simon Marchi <simon.marchi@efficios.com>
> 
> I don't see why include/gdb/fileio.h is placed there.  It's not
> installed by "make install", and it's not included by anything outside
> of gdb/gdbserver/gdbsupport.
> 
> Move its content back to gdbsupport/fileio.h.  I have omitted the bits
> inside an `#if 0`, since it's obviously not used, as well as the
> "limits" constants, which are also unused.
> 
> Change-Id: I6fbc2ea10fbe4cfcf15f9f76006b31b99c20e5a9

I pushed this series.

Simon

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

end of thread, other threads:[~2022-09-21 18:13 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-26 19:54 [PATCH 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
2022-08-26 19:54 ` [PATCH 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
2022-08-26 19:54 ` [PATCH 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h.cc} and rename Simon Marchi
2022-08-29 16:29 ` [PATCH v2 0/3] fileio error cleanups Simon Marchi
2022-08-29 16:29   ` [PATCH v2 1/3] gdbsupport: move include/gdb/fileio.h contents to fileio.h Simon Marchi
2022-09-21 18:13     ` Simon Marchi
2022-08-29 16:29   ` [PATCH v2 2/3] gdbsupport: convert FILEIO_* macros to an enum Simon Marchi
2022-08-29 16:29   ` [PATCH v2 3/3] gdbsupport: move fileio_errno_to_host to fileio.{h, cc} and rename Simon Marchi

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