public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common
  2015-03-09 11:58 [PATCH 0/3] Properly implement remote_bfd_iovec_stat Gary Benson
@ 2015-03-09 11:58 ` Gary Benson
  2015-03-10 18:30   ` Pedro Alves
  2015-03-09 11:58 ` [PATCH 2/3] Add vFile:fstat: packet to gdbserver Gary Benson
  2015-03-09 11:58 ` [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat Gary Benson
  2 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-09 11:58 UTC (permalink / raw)
  To: gdb-patches

This patch moves remote_fileio_to_fio_stat and its supporting
functions into new files common/common-remote-fileio.[ch].

gdb/ChangeLog:

	* common/common-remote-fileio.h: New file.
	* common/common-remote-fileio.c: Likewise.
	* Makefile.in (SFILES): Add common/common-remote-fileio.c.
	(HFILES_NO_SRCDIR): Add common/common-remote-fileio.h.
	(COMMON_OBS): Add common-remote-fileio.o.
	(common-remote-fileio.o): New rule.
	* remote-fileio.h (common-remote-fileio.h): New include.
	* remote-fileio.c (gdb/fileio.h): Do not include.
	(remote_fileio_to_be): Moved to common-remote-fileio.h.
	(remote_fileio_to_fio_uint): Likewise.
	(remote_fileio_to_fio_time): Likewise.
	(remote_fileio_mode_to_target): Moved to common-remote-fileio.c.
	(remote_fileio_to_fio_mode): Likewise.
	(remote_fileio_to_fio_ulong): Likewise.
	(remote_fileio_to_fio_stat): Likewise.

gdb/gdbserver/ChangeLog:

	* configure.ac (AC_CHECK_MEMBERS): Add checks for
	struct stat.st_blocks and struct stat.st_blksize.
	* configure: Regenerate.
	* config.in: Likewise.
	* Makefile.in (SFILES): Add common/common-remote-fileio.c.
	(OBS): Add common-remote-fileio.o.
	(common-remote-fileio.o): New rule.
---
 gdb/ChangeLog                     |   18 ++++++
 gdb/Makefile.in                   |   10 ++-
 gdb/common/common-remote-fileio.c |  119 +++++++++++++++++++++++++++++++++++++
 gdb/common/common-remote-fileio.h |   61 +++++++++++++++++++
 gdb/gdbserver/ChangeLog           |   10 +++
 gdb/gdbserver/Makefile.in         |    8 ++-
 gdb/gdbserver/config.in           |    6 ++
 gdb/gdbserver/configure           |   77 ++++++++++++++++++++++++
 gdb/gdbserver/configure.ac        |    2 +
 gdb/remote-fileio.c               |  111 ----------------------------------
 gdb/remote-fileio.h               |    2 +
 11 files changed, 308 insertions(+), 116 deletions(-)
 create mode 100644 gdb/common/common-remote-fileio.c
 create mode 100644 gdb/common/common-remote-fileio.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 8c2a4de..0ddcd72 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -877,7 +877,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
 	common/format.c common/filestuff.c btrace.c record-btrace.c ctf.c \
 	target/waitstatus.c common/print-utils.c common/rsp-low.c \
 	common/errors.c common/common-debug.c common/common-exceptions.c \
-	common/btrace-common.c \
+	common/btrace-common.c common/common-remote-fileio.c \
 	$(SUBDIR_GCC_COMPILE_SRCS)
 
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
@@ -966,7 +966,8 @@ common/print-utils.h common/rsp-low.h nat/x86-dregs.h x86-linux-nat.h \
 i386-linux-nat.h common/common-defs.h common/errors.h common/common-types.h \
 common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
 common/common-exceptions.h target/target.h common/symbol.h \
-common/common-regcache.h fbsd-tdep.h nat/linux-personality.h
+common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
+common/common-remote-fileio.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
@@ -1065,7 +1066,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o btrace-common.o \
+	common-exceptions.o btrace-common.o common-remote-fileio.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2244,6 +2245,9 @@ btrace-common.o: ${srcdir}/common/btrace-common.c
 	$(COMPILE) $(srcdir)/common/btrace-common.c
 	$(POSTCOMPILE)
 
+common-remote-fileio.o: ${srcdir}/common/common-remote-fileio.c
+	$(COMPILE) $(srcdir)/common/common-remote-fileio.c
+	$(POSTCOMPILE)
 #
 # gdb/target/ dependencies
 #
diff --git a/gdb/common/common-remote-fileio.c b/gdb/common/common-remote-fileio.c
new file mode 100644
index 0000000..f78b3f7
--- /dev/null
+++ b/gdb/common/common-remote-fileio.c
@@ -0,0 +1,119 @@
+/* Remote File-I/O communications
+
+   Copyright (C) 2003-2015 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "common-defs.h"
+#include "common-remote-fileio.h"
+#include <sys/stat.h>
+
+/* Convert a host-format mode_t into a bitmask of File-I/O flags.  */
+
+static LONGEST
+remote_fileio_mode_to_target (mode_t mode)
+{
+  mode_t tmode = 0;
+
+  if (S_ISREG (mode))
+    tmode |= FILEIO_S_IFREG;
+  if (S_ISDIR (mode))
+    tmode |= FILEIO_S_IFDIR;
+  if (S_ISCHR (mode))
+    tmode |= FILEIO_S_IFCHR;
+  if (mode & S_IRUSR)
+    tmode |= FILEIO_S_IRUSR;
+  if (mode & S_IWUSR)
+    tmode |= FILEIO_S_IWUSR;
+  if (mode & S_IXUSR)
+    tmode |= FILEIO_S_IXUSR;
+#ifdef S_IRGRP
+  if (mode & S_IRGRP)
+    tmode |= FILEIO_S_IRGRP;
+#endif
+#ifdef S_IWRGRP
+  if (mode & S_IWGRP)
+    tmode |= FILEIO_S_IWGRP;
+#endif
+#ifdef S_IXGRP
+  if (mode & S_IXGRP)
+    tmode |= FILEIO_S_IXGRP;
+#endif
+  if (mode & S_IROTH)
+    tmode |= FILEIO_S_IROTH;
+#ifdef S_IWOTH
+  if (mode & S_IWOTH)
+    tmode |= FILEIO_S_IWOTH;
+#endif
+#ifdef S_IXOTH
+  if (mode & S_IXOTH)
+    tmode |= FILEIO_S_IXOTH;
+#endif
+  return tmode;
+}
+
+/* Pack a host-format mode_t into an fio_mode_t.  */
+
+static void
+remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
+{
+  remote_fileio_to_be (remote_fileio_mode_to_target (num),
+		       (char *) fnum, 4);
+}
+
+/* Pack a host-format integer into an fio_ulong_t.  */
+
+static void
+remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
+{
+  remote_fileio_to_be (num, (char *) fnum, 8);
+}
+
+/* See common-remote-fileio.h.  */
+
+void
+remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
+{
+  LONGEST blksize;
+
+  remote_fileio_to_fio_uint ((long) st->st_dev, fst->fst_dev);
+  remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
+  remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
+  remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
+  remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
+  remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
+  remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
+  remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  blksize = st->st_blksize;
+#else
+  blksize = 512;
+#endif
+  remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
+#else
+  /* FIXME: This is correct for DJGPP, but other systems that don't
+     have st_blocks, if any, might prefer 512 instead of st_blksize.
+     (eliz, 30-12-2003)  */
+  remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
+			      / blksize,
+			      fst->fst_blocks);
+#endif
+  remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
+  remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
+  remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
+}
diff --git a/gdb/common/common-remote-fileio.h b/gdb/common/common-remote-fileio.h
new file mode 100644
index 0000000..b838186
--- /dev/null
+++ b/gdb/common/common-remote-fileio.h
@@ -0,0 +1,61 @@
+/* Remote File-I/O communications
+
+   Copyright (C) 2003-2015 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_REMOTE_FILEIO_H
+#define COMMON_REMOTE_FILEIO_H
+
+#include "gdb/fileio.h"
+
+struct stat;
+
+/* Pack a host-format integer into a byte buffer in big-endian format
+   ready for transmission over the remote protocol.  BYTES specifies
+   the size of the integer to pack in bytes.  */
+
+static inline void
+remote_fileio_to_be (LONGEST num, char *buf, int bytes)
+{
+  int i;
+
+  for (i = 0; i < bytes; ++i)
+    buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
+}
+
+/* Pack a host-format integer into an fio_uint_t.  */
+
+static inline void
+remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
+{
+  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
+}
+
+/* Pack a host-format time_t into an fio_time_t.  */
+
+static inline void
+remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
+{
+  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
+}
+
+/* Pack a host-format struct stat into a struct fio_stat.  */
+
+extern void remote_fileio_to_fio_stat (struct stat *st,
+				       struct fio_stat *fst);
+
+#endif /* COMMON_REMOTE_FILEIO_H */
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 6dddf26..b425ee2 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -179,7 +179,8 @@ SFILES=	$(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \
 	$(srcdir)/common/rsp-low.c $(srcdir)/common/errors.c \
 	$(srcdir)/common/common-debug.c $(srcdir)/common/cleanups.c \
 	$(srcdir)/common/common-exceptions.c $(srcdir)/symbol.c \
-	$(srcdir)/common/btrace-common.c
+	$(srcdir)/common/btrace-common.c \
+	$(srcdir)/common/common-remote-fileio.c
 
 DEPFILES = @GDBSERVER_DEPFILES@
 
@@ -193,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
-      common-exceptions.o symbol.o btrace-common.o \
+      common-exceptions.o symbol.o btrace-common.o common-remote-fileio.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -572,6 +573,9 @@ common-exceptions.o: ../common/common-exceptions.c
 waitstatus.o: ../target/waitstatus.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+common-remote-fileio.o: ../common/common-remote-fileio.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 
 # Native object files rules from ../nat
 
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 8114628..3c3bfca 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -210,6 +210,12 @@
 /* Define to 1 if you have the <string.h> header file. */
 #undef HAVE_STRING_H
 
+/* Define to 1 if `struct stat' is a member of `st_blksize'. */
+#undef HAVE_STRUCT_STAT_ST_BLKSIZE
+
+/* Define to 1 if `struct stat' is a member of `st_blocks'. */
+#undef HAVE_STRUCT_STAT_ST_BLOCKS
+
 /* Define to 1 if the target supports __sync_*_compare_and_swap */
 #undef HAVE_SYNC_BUILTINS
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index a14def9..6e807fd 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -2109,6 +2109,63 @@ rm -f conftest.val
   return $ac_retval
 
 } # ac_fn_c_compute_int
+
+# ac_fn_c_check_member LINENO AGGR MEMBER VAR INCLUDES
+# ----------------------------------------------------
+# Tries to find if the field MEMBER exists in type AGGR, after including
+# INCLUDES, setting cache variable VAR accordingly.
+ac_fn_c_check_member ()
+{
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2.$3" >&5
+$as_echo_n "checking for $2.$3... " >&6; }
+if { as_var=$4; eval "test \"\${$as_var+set}\" = set"; }; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$5
+int
+main ()
+{
+static $2 ac_aggr;
+if (ac_aggr.$3)
+return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  eval "$4=yes"
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$5
+int
+main ()
+{
+static $2 ac_aggr;
+if (sizeof ac_aggr.$3)
+return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  eval "$4=yes"
+else
+  eval "$4=no"
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+eval ac_res=\$$4
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+  eval $as_lineno_stack; test "x$as_lineno_stack" = x && { as_lineno=; unset as_lineno;}
+
+} # ac_fn_c_check_member
 cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
@@ -6083,6 +6140,26 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+ac_fn_c_check_member "$LINENO" "struct stat" "st_blocks" "ac_cv_member_struct_stat_st_blocks" "$ac_includes_default"
+if test "x$ac_cv_member_struct_stat_st_blocks" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_BLOCKS 1
+_ACEOF
+
+
+fi
+ac_fn_c_check_member "$LINENO" "struct stat" "st_blksize" "ac_cv_member_struct_stat_st_blksize" "$ac_includes_default"
+if test "x$ac_cv_member_struct_stat_st_blksize" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_BLKSIZE 1
+_ACEOF
+
+
+fi
+
+
 ac_fn_c_check_type "$LINENO" "socklen_t" "ac_cv_type_socklen_t" "#include <sys/types.h>
 #include <sys/socket.h>
 
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index cefc69e..8d0b4e3 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -224,6 +224,8 @@ libiberty_INIT
 
 AC_CHECK_DECLS([strerror, perror, vasprintf, vsnprintf])
 
+AC_CHECK_MEMBERS([struct stat.st_blocks, struct stat.st_blksize])
+
 AC_CHECK_TYPES(socklen_t, [], [],
 [#include <sys/types.h>
 #include <sys/socket.h>
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 00a86ca..0ce0714 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -22,7 +22,6 @@
 #include "defs.h"
 #include "gdbcmd.h"
 #include "remote.h"
-#include "gdb/fileio.h"
 #include "gdb_wait.h"
 #include <sys/stat.h>
 #include "remote-fileio.h"
@@ -194,48 +193,6 @@ remote_fileio_mode_to_host (long mode, int open_call)
   return hmode;
 }
 
-static LONGEST
-remote_fileio_mode_to_target (mode_t mode)
-{
-  mode_t tmode = 0;
-
-  if (S_ISREG(mode))
-    tmode |= FILEIO_S_IFREG;
-  if (S_ISDIR(mode))
-    tmode |= FILEIO_S_IFDIR;
-  if (S_ISCHR(mode))
-    tmode |= FILEIO_S_IFCHR;
-  if (mode & S_IRUSR)
-    tmode |= FILEIO_S_IRUSR;
-  if (mode & S_IWUSR)
-    tmode |= FILEIO_S_IWUSR;
-  if (mode & S_IXUSR)
-    tmode |= FILEIO_S_IXUSR;
-#ifdef S_IRGRP
-  if (mode & S_IRGRP)
-    tmode |= FILEIO_S_IRGRP;
-#endif
-#ifdef S_IWRGRP
-  if (mode & S_IWGRP)
-    tmode |= FILEIO_S_IWGRP;
-#endif
-#ifdef S_IXGRP
-  if (mode & S_IXGRP)
-    tmode |= FILEIO_S_IXGRP;
-#endif
-  if (mode & S_IROTH)
-    tmode |= FILEIO_S_IROTH;
-#ifdef S_IWOTH
-  if (mode & S_IWOTH)
-    tmode |= FILEIO_S_IWOTH;
-#endif
-#ifdef S_IXOTH
-  if (mode & S_IXOTH)
-    tmode |= FILEIO_S_IXOTH;
-#endif
-  return tmode;
-}
-
 static int
 remote_fileio_errno_to_target (int error)
 {
@@ -381,34 +338,6 @@ remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
   return 0;
 }
 
-/* Convert to big endian.  */
-static void
-remote_fileio_to_be (LONGEST num, char *buf, int bytes)
-{
-  int i;
-
-  for (i = 0; i < bytes; ++i)
-    buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
-}
-
-static void
-remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
-{
-  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
-}
-
-static void
-remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
-{
-  remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
-}
-
-static void
-remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
-{
-  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
-}
-
 static void
 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
 {
@@ -416,46 +345,6 @@ remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
 }
 
 static void
-remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
-{
-  remote_fileio_to_be (num, (char *) fnum, 8);
-}
-
-static void
-remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
-{
-  LONGEST blksize;
-
-  /* `st_dev' is set in the calling function.  */
-  remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
-  remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
-  remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
-  remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
-  remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
-  remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
-  remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
-#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
-  blksize = st->st_blksize;
-#else
-  blksize = 512;
-#endif
-  remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
-#if HAVE_STRUCT_STAT_ST_BLOCKS
-  remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
-#else
-  /* FIXME: This is correct for DJGPP, but other systems that don't
-     have st_blocks, if any, might prefer 512 instead of st_blksize.
-     (eliz, 30-12-2003)  */
-  remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
-			      / blksize,
-			      fst->fst_blocks);
-#endif
-  remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
-  remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
-  remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
-}
-
-static void
 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
 {
   remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 40a614a..8b32868 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -22,6 +22,8 @@
 #ifndef REMOTE_FILEIO_H
 #define REMOTE_FILEIO_H
 
+#include "common-remote-fileio.h"
+
 struct cmd_list_element;
 
 /* Unified interface to remote fileio, called in remote.c from
-- 
1.7.1

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

* [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-09 11:58 [PATCH 0/3] Properly implement remote_bfd_iovec_stat Gary Benson
  2015-03-09 11:58 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
  2015-03-09 11:58 ` [PATCH 2/3] Add vFile:fstat: packet to gdbserver Gary Benson
@ 2015-03-09 11:58 ` Gary Benson
  2015-03-09 16:17   ` Eli Zaretskii
  2015-03-10 11:20   ` [PATCH 3/3 v2] " Gary Benson
  2 siblings, 2 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-09 11:58 UTC (permalink / raw)
  To: gdb-patches

This patch implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

gdb/ChangeLog:

	* remote-fileio.h (remote_fileio_to_host_stat): New declaration.
	* remote-fileio.c (remote_fileio_to_host_uint): New function.
	(remote_fileio_to_host_ulong): Likewise.
	(remote_fileio_to_host_mode): Likewise.
	(remote_fileio_to_host_time): Likewise.
	(remote_fileio_to_host_stat): Likewise.
	* remote.c (PACKET_vFile_fstat): New enum value.
	(remote_protocol_features): Register the "vFile:fstat" feature.
	(remote_hostio_fstat): New function.
	(remote_bfd_iovec_stat): Use the above.
	(_initialize_remote): Register new "set/show remote
	hostio-fstat-packet" command.

gdb/doc/ChangeLog:

	* gdb.texinfo (Remote Configuration): Document the
	"set/show remote hostio-fstat-packet" command.
---
 gdb/ChangeLog       |   15 +++++++++
 gdb/doc/ChangeLog   |    5 +++
 gdb/doc/gdb.texinfo |    4 ++
 gdb/remote-fileio.c |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/remote-fileio.h |    4 ++
 gdb/remote.c        |   79 +++++++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 183 insertions(+), 4 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8d4af3c..552da31 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19748,6 +19748,10 @@ are:
 @tab @code{vFile:readlink}
 @tab Host I/O
 
+@item @code{hostio-fstat-packet}
+@tab @code{vFile:fstat}
+@tab Host I/O
+
 @item @code{noack-packet}
 @tab @code{QStartNoAckMode}
 @tab Packet acknowledgment
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0ce0714..aaf839d 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -1321,6 +1321,86 @@ remote_fileio_request (char *buf, int ctrlc_pending_p)
 
   remote_fileio_sig_exit ();
 }
+\f
+
+/* Unpack an fio_uint_t.  */
+
+static unsigned int
+remote_fileio_to_host_uint (fio_uint_t fnum)
+{
+  gdb_byte *buf, *lim;
+  unsigned int num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 4; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_ulong_t.  */
+
+static ULONGEST
+remote_fileio_to_host_ulong (fio_ulong_t fnum)
+{
+  gdb_byte *buf, *lim;
+  ULONGEST num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 8; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_mode_t.  */
+
+static mode_t
+remote_fileio_to_host_mode (fio_mode_t fnum)
+{
+  return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
+				     0);
+}
+
+/* Unpack an fio_time_t.  */
+
+static time_t
+remote_fileio_to_host_time (fio_time_t fnum)
+{
+  return remote_fileio_to_host_uint (fnum);
+}
+
+
+/* See remote-fileio.h.  */
+
+void
+remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
+{
+  memset (st, 0, sizeof (struct stat));
+
+  st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
+  st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
+  st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
+  st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
+  st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
+  st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
+  st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
+  st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
+#endif
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
+#endif
+  st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
+  st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
+  st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
+}
+\f
 
 static void
 set_system_call_allowed (char *args, int from_tty)
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 8b32868..d30ae6a 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -38,4 +38,8 @@ extern void initialize_remote_fileio (
   struct cmd_list_element *remote_set_cmdlist,
   struct cmd_list_element *remote_show_cmdlist);
 
+/* Unpack a struct fio_stat.  */
+extern void remote_fileio_to_host_stat (struct fio_stat *fst,
+					struct stat *st);
+
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index 495dfdc..69bcdd7 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1253,6 +1253,7 @@ enum {
   PACKET_vFile_close,
   PACKET_vFile_unlink,
   PACKET_vFile_readlink,
+  PACKET_vFile_fstat,
   PACKET_qXfer_auxv,
   PACKET_qXfer_features,
   PACKET_qXfer_libraries,
@@ -4042,7 +4043,9 @@ static const struct protocol_feature remote_protocol_features[] = {
   { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_bts_size },
   { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
-  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature }
+  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
+  { "vFile:fstat", PACKET_DISABLE, remote_supported_packet,
+    PACKET_vFile_fstat },
 };
 
 static char *remote_support_xml;
@@ -10059,6 +10062,55 @@ remote_hostio_readlink (struct target_ops *self,
   return ret;
 }
 
+/* Read information about the open file FD on the remote target
+   into ST.  Return 0 on success, or -1 if an error occurs (and
+   set *REMOTE_ERRNO).  */
+
+static int
+remote_hostio_fstat (struct target_ops *self,
+		     int fd, struct stat *st,
+		     int *remote_errno)
+{
+  struct remote_state *rs = get_remote_state ();
+  char *p = rs->buf;
+  int left = get_remote_packet_size ();
+  int attachment_len, ret;
+  char *attachment;
+  struct fio_stat fst;
+  int read_len;
+
+  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
+    {
+      memset (st, 0, sizeof (struct stat));
+      st->st_size = INT_MAX;
+      return 0;
+    }
+
+  remote_buffer_add_string (&p, &left, "vFile:fstat:");
+
+  remote_buffer_add_int (&p, &left, fd);
+
+  ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
+				    remote_errno, &attachment,
+				    &attachment_len);
+  if (ret < 0)
+    return ret;
+
+  read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
+				    (gdb_byte *) &fst, sizeof (fst));
+
+  if (read_len != ret)
+    error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
+
+  if (read_len != sizeof (fst))
+    error (_("vFile:fstat returned %d bytes, but expecting %d."),
+	   read_len, (int) sizeof (fst));
+
+  remote_fileio_to_host_stat (&fst, st);
+
+  return 0;
+}
+
 static int
 remote_fileio_errno_to_host (int errnum)
 {
@@ -10203,9 +10255,25 @@ remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 static int
 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
 {
-  /* FIXME: We should probably implement remote_hostio_stat.  */
-  sb->st_size = INT_MAX;
-  return 0;
+  int fd = *(int *) stream;
+  int remote_errno;
+  int result;
+  const char *filename;
+
+  filename = bfd_get_filename (abfd);
+  gdb_assert (remote_filename_p (filename));
+  filename += strlen (REMOTE_SYSROOT_PREFIX);
+
+  result = remote_hostio_fstat (find_target_at (process_stratum),
+				fd, sb, &remote_errno);
+
+  if (result == -1)
+    {
+      errno = remote_fileio_errno_to_host (remote_errno);
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  return result;
 }
 
 int
@@ -12342,6 +12410,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
 			 "vFile:readlink", "hostio-readlink", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
+			 "vFile:fstat", "hostio-fstat", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
 			 "vAttach", "attach", 0);
 
-- 
1.7.1

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

* [PATCH 0/3] Properly implement remote_bfd_iovec_stat
@ 2015-03-09 11:58 Gary Benson
  2015-03-09 11:58 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-09 11:58 UTC (permalink / raw)
  To: gdb-patches

Hi all,

While poking around in the remote protocol I noticed that
remote_fileio_to_fio_stat is "implemented" like this:

  static int
  remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
  {
    /* FIXME: We should probably implement remote_hostio_stat.  */
    sb->st_size = INT_MAX;
    return 0;
  }

This series implements this call properly.

Built and regtested on RHEL 6.6 x86_64.

Ok to commit?

Cheers,
Gary

--
http://gbenson.net/

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

* [PATCH 2/3] Add vFile:fstat: packet to gdbserver
  2015-03-09 11:58 [PATCH 0/3] Properly implement remote_bfd_iovec_stat Gary Benson
  2015-03-09 11:58 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
@ 2015-03-09 11:58 ` Gary Benson
  2015-03-10 18:31   ` Pedro Alves
  2015-03-09 11:58 ` [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat Gary Benson
  2 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-09 11:58 UTC (permalink / raw)
  To: gdb-patches

This patch adds a new packet "vFile:fstat:" to gdbserver.
This can be used by clients to retrieve information about
open files.

gdb/ChangeLog:

	* NEWS: Announce new vFile:fstat packet.

gdb/doc/ChangeLog:

	* gdb.texinfo (Host I/O Packets): Document vFile:fstat.

gdb/gdbserver/ChangeLog:

	* hostio.c (sys/types.h): New include.
	(sys/stat.h): Likewise.
	(common-remote-fileio.h): Likewise.
	(handle_fstat): New function.
	(handle_vFile): Handle vFile:fstat packets.
	* server.c (handle_query): Report vFile:fstat as supported.
---
 gdb/ChangeLog           |    4 ++++
 gdb/NEWS                |    3 +++
 gdb/doc/ChangeLog       |    4 ++++
 gdb/doc/gdb.texinfo     |    7 +++++++
 gdb/gdbserver/ChangeLog |    9 +++++++++
 gdb/gdbserver/hostio.c  |   41 +++++++++++++++++++++++++++++++++++++++++
 gdb/gdbserver/server.c  |    2 ++
 7 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 49dc0e6..f08f972 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -73,6 +73,9 @@ hwbreak stop reason
   Indicates the target stopped for a hardware breakpoint.  This is
   required for correct non-stop mode operation.
 
+vFile:fstat:
+  Return information about files on the remote system.
+
 * The info record command now shows the recording format and the
   branch tracing configuration for the current thread when using
   the btrace record target.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4b76ce9..8d4af3c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -37407,6 +37407,13 @@ packet is used.  @samp{vFile:write} returns the number of bytes written,
 which may be shorter than the length of @var{data}, or -1 if an
 error occurred.
 
+@item vFile:fstat: @var{fd}
+Get information about the open file corresponding to @var{fd}.
+On success the information is returned as a binary attachment
+and the return value is the size of this attachment in bytes.
+If an error occurs the return value is -1.  The format of the
+returned binary attachment is as described in @ref{struct stat}.
+
 @item vFile:unlink: @var{filename}
 Delete the file at @var{filename} on the target.  Return 0,
 or -1 if an error occurs.  The @var{filename} is a string.
diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c
index ec29eb9..cc0192b 100644
--- a/gdb/gdbserver/hostio.c
+++ b/gdb/gdbserver/hostio.c
@@ -25,6 +25,9 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "common-remote-fileio.h"
 
 extern int remote_debug;
 
@@ -412,6 +415,42 @@ handle_pwrite (char *own_buf, int packet_len)
 }
 
 static void
+handle_fstat (char *own_buf, int *new_packet_len)
+{
+  int fd, bytes_sent;
+  char *p;
+  struct stat st;
+  struct fio_stat fst;
+
+  p = own_buf + strlen ("vFile:fstat:");
+
+  if (require_int (&p, &fd)
+      || require_valid_fd (fd)
+      || require_end (p))
+    {
+      hostio_packet_error (own_buf);
+      return;
+    }
+
+  if (fstat (fd, &st) == -1)
+    {
+      hostio_error (own_buf);
+      return;
+    }
+
+  remote_fileio_to_fio_stat (&st, &fst);
+
+  bytes_sent = hostio_reply_with_data (own_buf,
+				       (char *) &fst, sizeof (fst),
+				       new_packet_len);
+
+  /* If the response does not fit into a single packet, do not attempt
+     to return a partial response, but simply fail.  */
+  if (bytes_sent < sizeof (fst))
+    sprintf (own_buf, "F-1,%x", FILEIO_ENOSYS);
+}
+
+static void
 handle_close (char *own_buf)
 {
   int fd, ret;
@@ -517,6 +556,8 @@ handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
     handle_pread (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:pwrite:"))
     handle_pwrite (own_buf, packet_len);
+  else if (startswith (own_buf, "vFile:fstat:"))
+    handle_fstat (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:close:"))
     handle_close (own_buf);
   else if (startswith (own_buf, "vFile:unlink:"))
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 4189877..4d8faa3 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -2082,6 +2082,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
       if (target_supports_stopped_by_hw_breakpoint ())
 	strcat (own_buf, ";hwbreak+");
 
+      strcat (own_buf, ";vFile:fstat+");
+
       return;
     }
 
-- 
1.7.1

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

* Re: [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-09 11:58 ` [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat Gary Benson
@ 2015-03-09 16:17   ` Eli Zaretskii
  2015-03-10  9:32     ` Gary Benson
  2015-03-10 11:20   ` [PATCH 3/3 v2] " Gary Benson
  1 sibling, 1 reply; 28+ messages in thread
From: Eli Zaretskii @ 2015-03-09 16:17 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches

> From: Gary Benson <gbenson@redhat.com>
> Date: Mon,  9 Mar 2015 11:58:19 +0000
> 
> gdb/doc/ChangeLog:
> 
> 	* gdb.texinfo (Remote Configuration): Document the
> 	"set/show remote hostio-fstat-packet" command.

OK for this part.

Thanks.

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

* Re: [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-09 16:17   ` Eli Zaretskii
@ 2015-03-10  9:32     ` Gary Benson
  2015-03-10 16:40       ` Eli Zaretskii
  0 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-10  9:32 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: gdb-patches

Eli Zaretskii wrote:
> > From: Gary Benson <gbenson@redhat.com>
> > Date: Mon,  9 Mar 2015 11:58:19 +0000
> > 
> > gdb/doc/ChangeLog:
> > 
> > 	* gdb.texinfo (Remote Configuration): Document the
> > 	"set/show remote hostio-fstat-packet" command.
> 
> OK for this part.

Thanks Eli.  Did you see the doc in 2/3 of this series too?
https://sourceware.org/ml/gdb-patches/2015-03/msg00215.html

Thanks,
Gary

-- 
http://gbenson.net/

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

* [PATCH 3/3 v2] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-09 11:58 ` [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat Gary Benson
  2015-03-09 16:17   ` Eli Zaretskii
@ 2015-03-10 11:20   ` Gary Benson
  2015-03-10 15:51     ` [PATCH 3/3 v3] " Gary Benson
  1 sibling, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-10 11:20 UTC (permalink / raw)
  To: gdb-patches

I noticed I left a bit of useless code in remote_bfd_iovec_stat.
This updated patch removes it, the rest is unchanged.

---
This patch implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

gdb/ChangeLog:

	* remote-fileio.h (remote_fileio_to_host_stat): New declaration.
	* remote-fileio.c (remote_fileio_to_host_uint): New function.
	(remote_fileio_to_host_ulong): Likewise.
	(remote_fileio_to_host_mode): Likewise.
	(remote_fileio_to_host_time): Likewise.
	(remote_fileio_to_host_stat): Likewise.
	* remote.c (PACKET_vFile_fstat): New enum value.
	(remote_protocol_features): Register the "vFile:fstat" feature.
	(remote_hostio_fstat): New function.
	(remote_bfd_iovec_stat): Use the above.
	(_initialize_remote): Register new "set/show remote
	hostio-fstat-packet" command.

gdb/doc/ChangeLog:

	* gdb.texinfo (Remote Configuration): Document the
	"set/show remote hostio-fstat-packet" command.
---
 gdb/ChangeLog       |   15 +++++++++
 gdb/doc/ChangeLog   |    5 +++
 gdb/doc/gdb.texinfo |    4 ++
 gdb/remote-fileio.c |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/remote-fileio.h |    4 ++
 gdb/remote.c        |   74 ++++++++++++++++++++++++++++++++++++++++++++--
 6 files changed, 178 insertions(+), 4 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8d4af3c..552da31 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19748,6 +19748,10 @@ are:
 @tab @code{vFile:readlink}
 @tab Host I/O
 
+@item @code{hostio-fstat-packet}
+@tab @code{vFile:fstat}
+@tab Host I/O
+
 @item @code{noack-packet}
 @tab @code{QStartNoAckMode}
 @tab Packet acknowledgment
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0ce0714..aaf839d 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -1321,6 +1321,86 @@ remote_fileio_request (char *buf, int ctrlc_pending_p)
 
   remote_fileio_sig_exit ();
 }
+\f
+
+/* Unpack an fio_uint_t.  */
+
+static unsigned int
+remote_fileio_to_host_uint (fio_uint_t fnum)
+{
+  gdb_byte *buf, *lim;
+  unsigned int num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 4; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_ulong_t.  */
+
+static ULONGEST
+remote_fileio_to_host_ulong (fio_ulong_t fnum)
+{
+  gdb_byte *buf, *lim;
+  ULONGEST num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 8; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_mode_t.  */
+
+static mode_t
+remote_fileio_to_host_mode (fio_mode_t fnum)
+{
+  return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
+				     0);
+}
+
+/* Unpack an fio_time_t.  */
+
+static time_t
+remote_fileio_to_host_time (fio_time_t fnum)
+{
+  return remote_fileio_to_host_uint (fnum);
+}
+
+
+/* See remote-fileio.h.  */
+
+void
+remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
+{
+  memset (st, 0, sizeof (struct stat));
+
+  st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
+  st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
+  st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
+  st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
+  st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
+  st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
+  st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
+  st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
+#endif
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
+#endif
+  st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
+  st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
+  st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
+}
+\f
 
 static void
 set_system_call_allowed (char *args, int from_tty)
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 8b32868..d30ae6a 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -38,4 +38,8 @@ extern void initialize_remote_fileio (
   struct cmd_list_element *remote_set_cmdlist,
   struct cmd_list_element *remote_show_cmdlist);
 
+/* Unpack a struct fio_stat.  */
+extern void remote_fileio_to_host_stat (struct fio_stat *fst,
+					struct stat *st);
+
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index 495dfdc..52adbbb 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1253,6 +1253,7 @@ enum {
   PACKET_vFile_close,
   PACKET_vFile_unlink,
   PACKET_vFile_readlink,
+  PACKET_vFile_fstat,
   PACKET_qXfer_auxv,
   PACKET_qXfer_features,
   PACKET_qXfer_libraries,
@@ -4042,7 +4043,9 @@ static const struct protocol_feature remote_protocol_features[] = {
   { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_bts_size },
   { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
-  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature }
+  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
+  { "vFile:fstat", PACKET_DISABLE, remote_supported_packet,
+    PACKET_vFile_fstat },
 };
 
 static char *remote_support_xml;
@@ -10059,6 +10062,55 @@ remote_hostio_readlink (struct target_ops *self,
   return ret;
 }
 
+/* Read information about the open file FD on the remote target
+   into ST.  Return 0 on success, or -1 if an error occurs (and
+   set *REMOTE_ERRNO).  */
+
+static int
+remote_hostio_fstat (struct target_ops *self,
+		     int fd, struct stat *st,
+		     int *remote_errno)
+{
+  struct remote_state *rs = get_remote_state ();
+  char *p = rs->buf;
+  int left = get_remote_packet_size ();
+  int attachment_len, ret;
+  char *attachment;
+  struct fio_stat fst;
+  int read_len;
+
+  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
+    {
+      memset (st, 0, sizeof (struct stat));
+      st->st_size = INT_MAX;
+      return 0;
+    }
+
+  remote_buffer_add_string (&p, &left, "vFile:fstat:");
+
+  remote_buffer_add_int (&p, &left, fd);
+
+  ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
+				    remote_errno, &attachment,
+				    &attachment_len);
+  if (ret < 0)
+    return ret;
+
+  read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
+				    (gdb_byte *) &fst, sizeof (fst));
+
+  if (read_len != ret)
+    error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
+
+  if (read_len != sizeof (fst))
+    error (_("vFile:fstat returned %d bytes, but expecting %d."),
+	   read_len, (int) sizeof (fst));
+
+  remote_fileio_to_host_stat (&fst, st);
+
+  return 0;
+}
+
 static int
 remote_fileio_errno_to_host (int errnum)
 {
@@ -10203,9 +10255,20 @@ remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 static int
 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
 {
-  /* FIXME: We should probably implement remote_hostio_stat.  */
-  sb->st_size = INT_MAX;
-  return 0;
+  int fd = *(int *) stream;
+  int remote_errno;
+  int result;
+
+  result = remote_hostio_fstat (find_target_at (process_stratum),
+				fd, sb, &remote_errno);
+
+  if (result == -1)
+    {
+      errno = remote_fileio_errno_to_host (remote_errno);
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  return result;
 }
 
 int
@@ -12342,6 +12405,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
 			 "vFile:readlink", "hostio-readlink", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
+			 "vFile:fstat", "hostio-fstat", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
 			 "vAttach", "attach", 0);
 
-- 
1.7.1

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

* [PATCH 3/3 v3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-10 11:20   ` [PATCH 3/3 v2] " Gary Benson
@ 2015-03-10 15:51     ` Gary Benson
  2015-03-10 18:31       ` Pedro Alves
  2015-03-10 18:36       ` Pedro Alves
  0 siblings, 2 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-10 15:51 UTC (permalink / raw)
  To: gdb-patches

Sorry for the noise on this patch, but I found a comment that
needed updating.  This version (v3) of this patch contains that
update (to symfile.c).  Version 2 of this patch removed a few
lines of random debug code I'd left in remote_bfd_iovec_stat.
Aside from those tweaks, the remainder of the patch is as
originally mailed.

---
This patch implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

gdb/ChangeLog:

	* remote-fileio.h (remote_fileio_to_host_stat): New declaration.
	* remote-fileio.c (remote_fileio_to_host_uint): New function.
	(remote_fileio_to_host_ulong): Likewise.
	(remote_fileio_to_host_mode): Likewise.
	(remote_fileio_to_host_time): Likewise.
	(remote_fileio_to_host_stat): Likewise.
	* remote.c (PACKET_vFile_fstat): New enum value.
	(remote_protocol_features): Register the "vFile:fstat" feature.
	(remote_hostio_fstat): New function.
	(remote_bfd_iovec_stat): Use the above.
	(_initialize_remote): Register new "set/show remote
	hostio-fstat-packet" command.
	* symfile.c (separate_debug_file_exists): Update comment.

gdb/doc/ChangeLog:

	* gdb.texinfo (Remote Configuration): Document the
	"set/show remote hostio-fstat-packet" command.
---
 gdb/ChangeLog       |   16 ++++++++++
 gdb/doc/ChangeLog   |    5 +++
 gdb/doc/gdb.texinfo |    4 ++
 gdb/remote-fileio.c |   80 +++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/remote-fileio.h |    4 ++
 gdb/remote.c        |   74 ++++++++++++++++++++++++++++++++++++++++++++--
 gdb/symfile.c       |   17 ++++++-----
 7 files changed, 188 insertions(+), 12 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 8d4af3c..552da31 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19748,6 +19748,10 @@ are:
 @tab @code{vFile:readlink}
 @tab Host I/O
 
+@item @code{hostio-fstat-packet}
+@tab @code{vFile:fstat}
+@tab Host I/O
+
 @item @code{noack-packet}
 @tab @code{QStartNoAckMode}
 @tab Packet acknowledgment
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0ce0714..aaf839d 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -1321,6 +1321,86 @@ remote_fileio_request (char *buf, int ctrlc_pending_p)
 
   remote_fileio_sig_exit ();
 }
+\f
+
+/* Unpack an fio_uint_t.  */
+
+static unsigned int
+remote_fileio_to_host_uint (fio_uint_t fnum)
+{
+  gdb_byte *buf, *lim;
+  unsigned int num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 4; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_ulong_t.  */
+
+static ULONGEST
+remote_fileio_to_host_ulong (fio_ulong_t fnum)
+{
+  gdb_byte *buf, *lim;
+  ULONGEST num = 0;
+
+  for (buf = (gdb_byte *) fnum, lim = buf + 8; buf < lim; buf++)
+    {
+      num <<= 8;
+      num |= *buf;
+    }
+
+  return num;
+}
+
+/* Unpack an fio_mode_t.  */
+
+static mode_t
+remote_fileio_to_host_mode (fio_mode_t fnum)
+{
+  return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
+				     0);
+}
+
+/* Unpack an fio_time_t.  */
+
+static time_t
+remote_fileio_to_host_time (fio_time_t fnum)
+{
+  return remote_fileio_to_host_uint (fnum);
+}
+
+
+/* See remote-fileio.h.  */
+
+void
+remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
+{
+  memset (st, 0, sizeof (struct stat));
+
+  st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
+  st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
+  st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
+  st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
+  st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
+  st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
+  st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
+  st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
+#endif
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
+#endif
+  st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
+  st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
+  st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
+}
+\f
 
 static void
 set_system_call_allowed (char *args, int from_tty)
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 8b32868..d30ae6a 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -38,4 +38,8 @@ extern void initialize_remote_fileio (
   struct cmd_list_element *remote_set_cmdlist,
   struct cmd_list_element *remote_show_cmdlist);
 
+/* Unpack a struct fio_stat.  */
+extern void remote_fileio_to_host_stat (struct fio_stat *fst,
+					struct stat *st);
+
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index 495dfdc..52adbbb 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1253,6 +1253,7 @@ enum {
   PACKET_vFile_close,
   PACKET_vFile_unlink,
   PACKET_vFile_readlink,
+  PACKET_vFile_fstat,
   PACKET_qXfer_auxv,
   PACKET_qXfer_features,
   PACKET_qXfer_libraries,
@@ -4042,7 +4043,9 @@ static const struct protocol_feature remote_protocol_features[] = {
   { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_bts_size },
   { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
-  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature }
+  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
+  { "vFile:fstat", PACKET_DISABLE, remote_supported_packet,
+    PACKET_vFile_fstat },
 };
 
 static char *remote_support_xml;
@@ -10059,6 +10062,55 @@ remote_hostio_readlink (struct target_ops *self,
   return ret;
 }
 
+/* Read information about the open file FD on the remote target
+   into ST.  Return 0 on success, or -1 if an error occurs (and
+   set *REMOTE_ERRNO).  */
+
+static int
+remote_hostio_fstat (struct target_ops *self,
+		     int fd, struct stat *st,
+		     int *remote_errno)
+{
+  struct remote_state *rs = get_remote_state ();
+  char *p = rs->buf;
+  int left = get_remote_packet_size ();
+  int attachment_len, ret;
+  char *attachment;
+  struct fio_stat fst;
+  int read_len;
+
+  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
+    {
+      memset (st, 0, sizeof (struct stat));
+      st->st_size = INT_MAX;
+      return 0;
+    }
+
+  remote_buffer_add_string (&p, &left, "vFile:fstat:");
+
+  remote_buffer_add_int (&p, &left, fd);
+
+  ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
+				    remote_errno, &attachment,
+				    &attachment_len);
+  if (ret < 0)
+    return ret;
+
+  read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
+				    (gdb_byte *) &fst, sizeof (fst));
+
+  if (read_len != ret)
+    error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
+
+  if (read_len != sizeof (fst))
+    error (_("vFile:fstat returned %d bytes, but expecting %d."),
+	   read_len, (int) sizeof (fst));
+
+  remote_fileio_to_host_stat (&fst, st);
+
+  return 0;
+}
+
 static int
 remote_fileio_errno_to_host (int errnum)
 {
@@ -10203,9 +10255,20 @@ remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 static int
 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
 {
-  /* FIXME: We should probably implement remote_hostio_stat.  */
-  sb->st_size = INT_MAX;
-  return 0;
+  int fd = *(int *) stream;
+  int remote_errno;
+  int result;
+
+  result = remote_hostio_fstat (find_target_at (process_stratum),
+				fd, sb, &remote_errno);
+
+  if (result == -1)
+    {
+      errno = remote_fileio_errno_to_host (remote_errno);
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  return result;
 }
 
 int
@@ -12342,6 +12405,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
 			 "vFile:readlink", "hostio-readlink", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
+			 "vFile:fstat", "hostio-fstat", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
 			 "vAttach", "attach", 0);
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index c2a71ec..ba099d3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1377,11 +1377,12 @@ separate_debug_file_exists (const char *name, unsigned long crc,
 
      Some operating systems, e.g. Windows, do not provide a meaningful
      st_ino; they always set it to zero.  (Windows does provide a
-     meaningful st_dev.)  Do not indicate a duplicate library in that
-     case.  While there is no guarantee that a system that provides
-     meaningful inode numbers will never set st_ino to zero, this is
-     merely an optimization, so we do not need to worry about false
-     negatives.  */
+     meaningful st_dev.)  Files accessed from gdbservers that do not
+     support the vFile:fstat packet will also have st_ino set to zero.
+     Do not indicate a duplicate library in either case.  While there
+     is no guarantee that a system that provides meaningful inode
+     numbers will never set st_ino to zero, this is merely an
+     optimization, so we do not need to worry about false negatives.  */
 
   if (bfd_stat (abfd, &abfd_stat) == 0
       && abfd_stat.st_ino != 0
@@ -1409,9 +1410,9 @@ separate_debug_file_exists (const char *name, unsigned long crc,
     {
       unsigned long parent_crc;
 
-      /* If one (or both) the files are accessed for example the via "remote:"
-	 gdbserver way it does not support the bfd_stat operation.  Verify
-	 whether those two files are not the same manually.  */
+      /* If the files could not be verified as different with
+	 bfd_stat then we need to calculate the parent's CRC
+	 to verify whether the files are different or not.  */
 
       if (!verified_as_different)
 	{
-- 
1.7.1

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

* Re: [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-10  9:32     ` Gary Benson
@ 2015-03-10 16:40       ` Eli Zaretskii
  0 siblings, 0 replies; 28+ messages in thread
From: Eli Zaretskii @ 2015-03-10 16:40 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches

> Date: Tue, 10 Mar 2015 09:32:28 +0000
> From: Gary Benson <gbenson@redhat.com>
> Cc: gdb-patches@sourceware.org
> 
> Eli Zaretskii wrote:
> > > From: Gary Benson <gbenson@redhat.com>
> > > Date: Mon,  9 Mar 2015 11:58:19 +0000
> > > 
> > > gdb/doc/ChangeLog:
> > > 
> > > 	* gdb.texinfo (Remote Configuration): Document the
> > > 	"set/show remote hostio-fstat-packet" command.
> > 
> > OK for this part.
> 
> Thanks Eli.  Did you see the doc in 2/3 of this series too?
> https://sourceware.org/ml/gdb-patches/2015-03/msg00215.html

I saw it now, and it's OK as well.

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

* Re: [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common
  2015-03-09 11:58 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
@ 2015-03-10 18:30   ` Pedro Alves
  0 siblings, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-10 18:30 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

On 03/09/2015 11:58 AM, Gary Benson wrote:
> This patch moves remote_fileio_to_fio_stat and its supporting
> functions into new files common/common-remote-fileio.[ch].
> 

Looks good to me.

Thanks,
Pedro Alves

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

* Re: [PATCH 3/3 v3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-10 15:51     ` [PATCH 3/3 v3] " Gary Benson
@ 2015-03-10 18:31       ` Pedro Alves
  2015-03-10 18:36       ` Pedro Alves
  1 sibling, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-10 18:31 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

On 03/10/2015 03:51 PM, Gary Benson wrote:
> 
> +
> +/* Unpack an fio_uint_t.  */
> +
> +static unsigned int
> +remote_fileio_to_host_uint (fio_uint_t fnum)
> +{
> +  gdb_byte *buf, *lim;
> +  unsigned int num = 0;
> +
> +  for (buf = (gdb_byte *) fnum, lim = buf + 4; buf < lim; buf++)
> +    {
> +      num <<= 8;
> +      num |= *buf;
> +    }

How about we use

  extract_unsigned_integer ((gdb_byte *) fnum, 4, BFD_ENDIAN_BIG)

instead?

Otherwise looks good.

Thanks,
Pedro Alves

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

* Re: [PATCH 2/3] Add vFile:fstat: packet to gdbserver
  2015-03-09 11:58 ` [PATCH 2/3] Add vFile:fstat: packet to gdbserver Gary Benson
@ 2015-03-10 18:31   ` Pedro Alves
  2015-03-11 10:33     ` [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver Gary Benson
  0 siblings, 1 reply; 28+ messages in thread
From: Pedro Alves @ 2015-03-10 18:31 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

On 03/09/2015 11:58 AM, Gary Benson wrote:
> This patch adds a new packet "vFile:fstat:" to gdbserver.
> This can be used by clients to retrieve information about
> open files.

Nit: Strictly speaking, it's a bit backwards to first add
packets to gdbserver that gdb doesn't support.  In general,
I prefer that the gdb patch lands first, documenting the
packets/commands.

> 
> gdb/ChangeLog:
> 
> 	* NEWS: Announce new vFile:fstat packet.
> 
> gdb/doc/ChangeLog:
> 
> 	* gdb.texinfo (Host I/O Packets): Document vFile:fstat.

These changes above should be in the patch that teaches
GDB about the packets.  Also, I think you're missing
documentation on the qSupported bits.

> 
> gdb/gdbserver/ChangeLog:
> 
> 	* hostio.c (sys/types.h): New include.
> 	(sys/stat.h): Likewise.
> 	(common-remote-fileio.h): Likewise.
> 	(handle_fstat): New function.
> 	(handle_vFile): Handle vFile:fstat packets.
> 	* server.c (handle_query): Report vFile:fstat as supported.

This part looks good.

> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -37407,6 +37407,13 @@ packet is used.  @samp{vFile:write} returns the number of bytes written,
>  which may be shorter than the length of @var{data}, or -1 if an
>  error occurred.
>  
> +@item vFile:fstat: @var{fd}
> +Get information about the open file corresponding to @var{fd}.
> +On success the information is returned as a binary attachment
> +and the return value is the size of this attachment in bytes.
> +If an error occurs the return value is -1.  The format of the
> +returned binary attachment is as described in @ref{struct stat}.

This should mention/reference the qSupported bit too.

Thanks,
Pedro Alves

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

* Re: [PATCH 3/3 v3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-10 15:51     ` [PATCH 3/3 v3] " Gary Benson
  2015-03-10 18:31       ` Pedro Alves
@ 2015-03-10 18:36       ` Pedro Alves
  2015-03-11 10:23         ` Gary Benson
  1 sibling, 1 reply; 28+ messages in thread
From: Pedro Alves @ 2015-03-10 18:36 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

Sorry, forgot to merge this comment with the other mail...

On 03/10/2015 03:51 PM, Gary Benson wrote:
> +   into ST.  Return 0 on success, or -1 if an error occurs (and
> +   set *REMOTE_ERRNO).  */
> +
> +static int
> +remote_hostio_fstat (struct target_ops *self,
> +		     int fd, struct stat *st,
> +		     int *remote_errno)
> +{
> +  struct remote_state *rs = get_remote_state ();
> +  char *p = rs->buf;
> +  int left = get_remote_packet_size ();
> +  int attachment_len, ret;
> +  char *attachment;
> +  struct fio_stat fst;
> +  int read_len;
> +
> +  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
> +    {
> +      memset (st, 0, sizeof (struct stat));
> +      st->st_size = INT_MAX;

A future reader may wonder why this isn't ENOSYS instead.  I think
a comment here would help.

> +      return 0;
> +    }

Thanks,
Pedro Alves

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

* Re: [PATCH 3/3 v3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-10 18:36       ` Pedro Alves
@ 2015-03-11 10:23         ` Gary Benson
  2015-03-11 11:22           ` Pedro Alves
  0 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-11 10:23 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> On 03/10/2015 03:51 PM, Gary Benson wrote:
> > +/* Unpack an fio_uint_t.  */
> > +
> > +static unsigned int
> > +remote_fileio_to_host_uint (fio_uint_t fnum)
> > +{
> > +  gdb_byte *buf, *lim;
> > +  unsigned int num = 0;
> > +
> > +  for (buf = (gdb_byte *) fnum, lim = buf + 4; buf < lim; buf++)
> > +    {
> > +      num <<= 8;
> > +      num |= *buf;
> > +    }
> 
> How about we use
> 
>   extract_unsigned_integer ((gdb_byte *) fnum, 4, BFD_ENDIAN_BIG)
> 
> instead?

Ok.

> > +   into ST.  Return 0 on success, or -1 if an error occurs (and
> > +   set *REMOTE_ERRNO).  */
> > +
> > +static int
> > +remote_hostio_fstat (struct target_ops *self,
> > +		     int fd, struct stat *st,
> > +		     int *remote_errno)
> > +{
> > +  struct remote_state *rs = get_remote_state ();
> > +  char *p = rs->buf;
> > +  int left = get_remote_packet_size ();
> > +  int attachment_len, ret;
> > +  char *attachment;
> > +  struct fio_stat fst;
> > +  int read_len;
> > +
> > +  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
> > +    {
> > +      memset (st, 0, sizeof (struct stat));
> > +      st->st_size = INT_MAX;
> 
> A future reader may wonder why this isn't ENOSYS instead.  I think
> a comment here would help.

How about this:

      /* Strictly we should return -1, ENOSYS here, but when
	 "set sysroot remote:" was implemented in August 2008
	 BFD's need for a stat function was sidestepped with
	 this hack.  This was not remedied until March 2015
	 so we retain the previous behavior to avoid breaking
	 compatibility.

	 Note that the memset is a March 2015 addition; older
	 GDBs set st_size *and nothing else* so the structure
	 would have garbage in all other fields.  This might
	 break something but retaining the previous behavior
	 here would be just too wrong.  */

Are you ok for me to commit this patch, reordered before the
gdbserver changes, with extract_unsigned_integer used in
remote_fileio_to_host_{uint,ulong}, and that comment added?
(I will regenerate patch 2 with updated docs for Eli to
approve).

Thanks,
Gary

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

* [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver
  2015-03-10 18:31   ` Pedro Alves
@ 2015-03-11 10:33     ` Gary Benson
  2015-03-11 11:35       ` Pedro Alves
  2015-03-11 16:00       ` Eli Zaretskii
  0 siblings, 2 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-11 10:33 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

Eli, could you please review the qSupported part of the docs in
this patch?  The remainder of the patch is unchanged from the
patch you previously approved.

Thanks,
Gary

---
This patch adds a new packet "vFile:fstat:" to gdbserver.
This can be used by clients to retrieve information about
open files.

gdb/ChangeLog:

	* NEWS: Announce new vFile:fstat packet.

gdb/doc/ChangeLog:

	* gdb.texinfo (Host I/O Packets): Document the vFile:fstat
	packet.
	(General Query Packets): Document the vFile:fstat qSupported
	features.

gdb/gdbserver/ChangeLog:

	* hostio.c (sys/types.h): New include.
	(sys/stat.h): Likewise.
	(common-remote-fileio.h): Likewise.
	(handle_fstat): New function.
	(handle_vFile): Handle vFile:fstat packets.
	* server.c (handle_query): Report vFile:fstat as supported.
---
 gdb/ChangeLog           |    4 ++++
 gdb/NEWS                |    3 +++
 gdb/doc/ChangeLog       |    7 +++++++
 gdb/doc/gdb.texinfo     |   15 +++++++++++++++
 gdb/gdbserver/ChangeLog |    9 +++++++++
 gdb/gdbserver/hostio.c  |   41 +++++++++++++++++++++++++++++++++++++++++
 gdb/gdbserver/server.c  |    2 ++
 7 files changed, 81 insertions(+), 0 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 49dc0e6..f08f972 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -73,6 +73,9 @@ hwbreak stop reason
   Indicates the target stopped for a hardware breakpoint.  This is
   required for correct non-stop mode operation.
 
+vFile:fstat:
+  Return information about files on the remote system.
+
 * The info record command now shows the recording format and the
   branch tracing configuration for the current thread when using
   the btrace record target.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 1ee1346..b197c1f 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -36062,6 +36062,11 @@ These are the currently defined stub features and their properties:
 @tab @samp{-}
 @tab No
 
+@item @samp{vFile:fstat}
+@tab No
+@tab @samp{-}
+@tab Yes
+
 @end multitable
 
 These are the currently defined stub features, in more detail:
@@ -36250,6 +36255,9 @@ breakpoints.
 The remote stub reports the @samp{hwbreak} stop reason for hardware
 breakpoints.
 
+@item vFile:fstat
+The remote stub understands the @samp{vFile:fstat} packet.
+
 @end table
 
 @item qSymbol::
@@ -37411,6 +37419,13 @@ packet is used.  @samp{vFile:write} returns the number of bytes written,
 which may be shorter than the length of @var{data}, or -1 if an
 error occurred.
 
+@item vFile:fstat: @var{fd}
+Get information about the open file corresponding to @var{fd}.
+On success the information is returned as a binary attachment
+and the return value is the size of this attachment in bytes.
+If an error occurs the return value is -1.  The format of the
+returned binary attachment is as described in @ref{struct stat}.
+
 @item vFile:unlink: @var{filename}
 Delete the file at @var{filename} on the target.  Return 0,
 or -1 if an error occurs.  The @var{filename} is a string.
diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c
index ec29eb9..cc0192b 100644
--- a/gdb/gdbserver/hostio.c
+++ b/gdb/gdbserver/hostio.c
@@ -25,6 +25,9 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "common-remote-fileio.h"
 
 extern int remote_debug;
 
@@ -412,6 +415,42 @@ handle_pwrite (char *own_buf, int packet_len)
 }
 
 static void
+handle_fstat (char *own_buf, int *new_packet_len)
+{
+  int fd, bytes_sent;
+  char *p;
+  struct stat st;
+  struct fio_stat fst;
+
+  p = own_buf + strlen ("vFile:fstat:");
+
+  if (require_int (&p, &fd)
+      || require_valid_fd (fd)
+      || require_end (p))
+    {
+      hostio_packet_error (own_buf);
+      return;
+    }
+
+  if (fstat (fd, &st) == -1)
+    {
+      hostio_error (own_buf);
+      return;
+    }
+
+  remote_fileio_to_fio_stat (&st, &fst);
+
+  bytes_sent = hostio_reply_with_data (own_buf,
+				       (char *) &fst, sizeof (fst),
+				       new_packet_len);
+
+  /* If the response does not fit into a single packet, do not attempt
+     to return a partial response, but simply fail.  */
+  if (bytes_sent < sizeof (fst))
+    sprintf (own_buf, "F-1,%x", FILEIO_ENOSYS);
+}
+
+static void
 handle_close (char *own_buf)
 {
   int fd, ret;
@@ -517,6 +556,8 @@ handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
     handle_pread (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:pwrite:"))
     handle_pwrite (own_buf, packet_len);
+  else if (startswith (own_buf, "vFile:fstat:"))
+    handle_fstat (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:close:"))
     handle_close (own_buf);
   else if (startswith (own_buf, "vFile:unlink:"))
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 4189877..4d8faa3 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -2082,6 +2082,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
       if (target_supports_stopped_by_hw_breakpoint ())
 	strcat (own_buf, ";hwbreak+");
 
+      strcat (own_buf, ";vFile:fstat+");
+
       return;
     }
 
-- 
1.7.1

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

* Re: [PATCH 3/3 v3] Implement remote_bfd_iovec_stat using vFile:fstat
  2015-03-11 10:23         ` Gary Benson
@ 2015-03-11 11:22           ` Pedro Alves
  2015-03-11 12:58             ` [PATCH 3/3 v4] Implement remote_bfd_iovec_stat Gary Benson
  0 siblings, 1 reply; 28+ messages in thread
From: Pedro Alves @ 2015-03-11 11:22 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches

On 03/11/2015 10:23 AM, Gary Benson wrote:
> Pedro Alves wrote:
>>> +  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
>>> +    {
>>> +      memset (st, 0, sizeof (struct stat));
>>> +      st->st_size = INT_MAX;
>>
>> A future reader may wonder why this isn't ENOSYS instead.  I think
>> a comment here would help.
> 
> How about this:
> 
>       /* Strictly we should return -1, ENOSYS here, but when
> 	 "set sysroot remote:" was implemented in August 2008
> 	 BFD's need for a stat function was sidestepped with
> 	 this hack.  This was not remedied until March 2015
> 	 so we retain the previous behavior to avoid breaking
> 	 compatibility.
> 
> 	 Note that the memset is a March 2015 addition; older
> 	 GDBs set st_size *and nothing else* so the structure
> 	 would have garbage in all other fields.  This might
> 	 break something but retaining the previous behavior
> 	 here would be just too wrong.  */

Thanks.

It also occurred to me something else on the gdbserver patch.
I'll send a follow up.

> Are you ok for me to commit this patch, reordered before the
> gdbserver changes, with extract_unsigned_integer used in
> remote_fileio_to_host_{uint,ulong}, and that comment added?

It be easier for me to just see the patch before answering, but
I think I am, though I'd like to take another look at the docs
patch with everything combined, to cross check whether more
bits would be missing.

I think it'd be good to have a "RSP packets: how to add / best practices
how to document" wiki page/guide, serving as both guidance for
submitters and for cross checking for reviewers, mentioning
things like remembering to document the "set remote foo commands",
qSupported entries, NEWS, etc.

> (I will regenerate patch 2 with updated docs for Eli to
> approve).

Thanks,
Pedro Alves

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

* Re: [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver
  2015-03-11 10:33     ` [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver Gary Benson
@ 2015-03-11 11:35       ` Pedro Alves
  2015-03-11 16:00       ` Eli Zaretskii
  1 sibling, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-11 11:35 UTC (permalink / raw)
  To: Gary Benson, gdb-patches; +Cc: Eli Zaretskii

On 03/11/2015 10:33 AM, Gary Benson wrote:

> --- a/gdb/doc/gdb.texinfo
> +++ b/gdb/doc/gdb.texinfo
> @@ -36062,6 +36062,11 @@ These are the currently defined stub features and their properties:
>  @tab @samp{-}
>  @tab No
>  
> +@item @samp{vFile:fstat}
> +@tab No
> +@tab @samp{-}
> +@tab Yes
> +
>  @end multitable
>  
>  These are the currently defined stub features, in more detail:
> @@ -36250,6 +36255,9 @@ breakpoints.
>  The remote stub reports the @samp{hwbreak} stop reason for hardware
>  breakpoints.
>  
> +@item vFile:fstat
> +The remote stub understands the @samp{vFile:fstat} packet.
> +

Thanks, combined with the "set remote ..." bit, I think that was
all that was missing.

>  @end table
>  
>  @item qSymbol::
> @@ -37411,6 +37419,13 @@ packet is used.  @samp{vFile:write} returns the number of bytes written,
>  which may be shorter than the length of @var{data}, or -1 if an
>  error occurred.
>  
> +@item vFile:fstat: @var{fd}
> +Get information about the open file corresponding to @var{fd}.
> +On success the information is returned as a binary attachment
> +and the return value is the size of this attachment in bytes.
> +If an error occurs the return value is -1.  The format of the
> +returned binary attachment is as described in @ref{struct stat}.
> +

I still think that we should mention the need for the qSupported feature
here.  This is because the other vFile:XXX packets don't have associated
qSupported features - GDB just tries them out and detects empty
response (probes them).

For example for the recent "swbreak" feature, I added:

 The remote stub must also supply the appropriate @samp{qSupported}
 feature indicating support.

> +
> +  remote_fileio_to_fio_stat (&st, &fst);
> +
> +  bytes_sent = hostio_reply_with_data (own_buf,
> +				       (char *) &fst, sizeof (fst),
> +				       new_packet_len);
> +
> +  /* If the response does not fit into a single packet, do not attempt
> +     to return a partial response, but simply fail.  */
> +  if (bytes_sent < sizeof (fst))
> +    sprintf (own_buf, "F-1,%x", FILEIO_ENOSYS);

It occurred to me that this -1 case isn't really an error with the
syscall, but rather a protocol error.  Thus, shouldn't this really
be an RSP E01 error ?

Thanks,
Pedro Alves

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

* [PATCH 3/3 v4] Implement remote_bfd_iovec_stat
  2015-03-11 11:22           ` Pedro Alves
@ 2015-03-11 12:58             ` Gary Benson
  2015-03-11 13:15               ` Pedro Alves
  2015-03-11 13:16               ` Gary Benson
  0 siblings, 2 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-11 12:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

Pedro Alves wrote:
> On 03/11/2015 10:23 AM, Gary Benson wrote:
> > Are you ok for me to commit this patch, reordered before the
> > gdbserver changes, with extract_unsigned_integer used in
> > remote_fileio_to_host_{uint,ulong}, and that comment added?
> 
> It be easier for me to just see the patch before answering, but I
> think I am, though I'd like to take another look at the docs patch
> with everything combined, to cross check whether more bits would be
> missing.

Ok, here goes:

---
This patch implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

gdb/ChangeLog:

	* remote-fileio.h (remote_fileio_to_host_stat): New declaration.
	* remote-fileio.c (remote_fileio_to_host_uint): New function.
	(remote_fileio_to_host_ulong): Likewise.
	(remote_fileio_to_host_mode): Likewise.
	(remote_fileio_to_host_time): Likewise.
	(remote_fileio_to_host_stat): Likewise.
	* remote.c (PACKET_vFile_fstat): New enum value.
	(remote_protocol_features): Register the "vFile:fstat" feature.
	(remote_hostio_fstat): New function.
	(remote_bfd_iovec_stat): Use the above.
	(_initialize_remote): Register new "set/show remote
	hostio-fstat-packet" command.
	* symfile.c (separate_debug_file_exists): Update comment.

gdb/doc/ChangeLog:

	* gdb.texinfo (Remote Configuration): Document the
	"set/show remote hostio-fstat-packet" command.
---
 gdb/ChangeLog       |   16 +++++++++
 gdb/doc/ChangeLog   |    5 +++
 gdb/doc/gdb.texinfo |    4 ++
 gdb/remote-fileio.c |   64 +++++++++++++++++++++++++++++++++++++
 gdb/remote-fileio.h |    4 ++
 gdb/remote.c        |   87 ++++++++++++++++++++++++++++++++++++++++++++++++--
 gdb/symfile.c       |   17 +++++-----
 7 files changed, 185 insertions(+), 12 deletions(-)

diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4b76ce9..1ee1346 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19748,6 +19748,10 @@ are:
 @tab @code{vFile:readlink}
 @tab Host I/O
 
+@item @code{hostio-fstat-packet}
+@tab @code{vFile:fstat}
+@tab Host I/O
+
 @item @code{noack-packet}
 @tab @code{QStartNoAckMode}
 @tab Packet acknowledgment
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0ce0714..3882321 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -1321,6 +1321,70 @@ remote_fileio_request (char *buf, int ctrlc_pending_p)
 
   remote_fileio_sig_exit ();
 }
+\f
+
+/* Unpack an fio_uint_t.  */
+
+static unsigned int
+remote_fileio_to_host_uint (fio_uint_t fnum)
+{
+  return extract_unsigned_integer ((gdb_byte *) fnum, 4,
+				   BFD_ENDIAN_BIG);
+}
+
+/* Unpack an fio_ulong_t.  */
+
+static ULONGEST
+remote_fileio_to_host_ulong (fio_ulong_t fnum)
+{
+  return extract_unsigned_integer ((gdb_byte *) fnum, 8,
+				   BFD_ENDIAN_BIG);
+}
+
+/* Unpack an fio_mode_t.  */
+
+static mode_t
+remote_fileio_to_host_mode (fio_mode_t fnum)
+{
+  return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
+				     0);
+}
+
+/* Unpack an fio_time_t.  */
+
+static time_t
+remote_fileio_to_host_time (fio_time_t fnum)
+{
+  return remote_fileio_to_host_uint (fnum);
+}
+
+
+/* See remote-fileio.h.  */
+
+void
+remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
+{
+  memset (st, 0, sizeof (struct stat));
+
+  st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
+  st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
+  st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
+  st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
+  st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
+  st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
+  st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
+  st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
+#endif
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
+#endif
+  st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
+  st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
+  st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
+}
+\f
 
 static void
 set_system_call_allowed (char *args, int from_tty)
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 8b32868..d30ae6a 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -38,4 +38,8 @@ extern void initialize_remote_fileio (
   struct cmd_list_element *remote_set_cmdlist,
   struct cmd_list_element *remote_show_cmdlist);
 
+/* Unpack a struct fio_stat.  */
+extern void remote_fileio_to_host_stat (struct fio_stat *fst,
+					struct stat *st);
+
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index 495dfdc..14ad127 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1253,6 +1253,7 @@ enum {
   PACKET_vFile_close,
   PACKET_vFile_unlink,
   PACKET_vFile_readlink,
+  PACKET_vFile_fstat,
   PACKET_qXfer_auxv,
   PACKET_qXfer_features,
   PACKET_qXfer_libraries,
@@ -4042,7 +4043,9 @@ static const struct protocol_feature remote_protocol_features[] = {
   { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_bts_size },
   { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
-  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature }
+  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
+  { "vFile:fstat", PACKET_DISABLE, remote_supported_packet,
+    PACKET_vFile_fstat },
 };
 
 static char *remote_support_xml;
@@ -10059,6 +10062,68 @@ remote_hostio_readlink (struct target_ops *self,
   return ret;
 }
 
+/* Read information about the open file FD on the remote target
+   into ST.  Return 0 on success, or -1 if an error occurs (and
+   set *REMOTE_ERRNO).  */
+
+static int
+remote_hostio_fstat (struct target_ops *self,
+		     int fd, struct stat *st,
+		     int *remote_errno)
+{
+  struct remote_state *rs = get_remote_state ();
+  char *p = rs->buf;
+  int left = get_remote_packet_size ();
+  int attachment_len, ret;
+  char *attachment;
+  struct fio_stat fst;
+  int read_len;
+
+  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
+    {
+      /* Strictly we should return -1, ENOSYS here, but when
+	 "set sysroot remote:" was implemented in August 2008
+	 BFD's need for a stat function was sidestepped with
+	 this hack.  This was not remedied until March 2015
+	 so we retain the previous behavior to avoid breaking
+	 compatibility.
+
+	 Note that the memset is a March 2015 addition; older
+	 GDBs set st_size *and nothing else* so the structure
+	 would have garbage in all other fields.  This might
+	 break something but retaining the previous behavior
+	 here would be just too wrong.  */
+
+      memset (st, 0, sizeof (struct stat));
+      st->st_size = INT_MAX;
+      return 0;
+    }
+
+  remote_buffer_add_string (&p, &left, "vFile:fstat:");
+
+  remote_buffer_add_int (&p, &left, fd);
+
+  ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
+				    remote_errno, &attachment,
+				    &attachment_len);
+  if (ret < 0)
+    return ret;
+
+  read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
+				    (gdb_byte *) &fst, sizeof (fst));
+
+  if (read_len != ret)
+    error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
+
+  if (read_len != sizeof (fst))
+    error (_("vFile:fstat returned %d bytes, but expecting %d."),
+	   read_len, (int) sizeof (fst));
+
+  remote_fileio_to_host_stat (&fst, st);
+
+  return 0;
+}
+
 static int
 remote_fileio_errno_to_host (int errnum)
 {
@@ -10203,9 +10268,20 @@ remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 static int
 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
 {
-  /* FIXME: We should probably implement remote_hostio_stat.  */
-  sb->st_size = INT_MAX;
-  return 0;
+  int fd = *(int *) stream;
+  int remote_errno;
+  int result;
+
+  result = remote_hostio_fstat (find_target_at (process_stratum),
+				fd, sb, &remote_errno);
+
+  if (result == -1)
+    {
+      errno = remote_fileio_errno_to_host (remote_errno);
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  return result;
 }
 
 int
@@ -12342,6 +12418,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
 			 "vFile:readlink", "hostio-readlink", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
+			 "vFile:fstat", "hostio-fstat", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
 			 "vAttach", "attach", 0);
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index c2a71ec..ba099d3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1377,11 +1377,12 @@ separate_debug_file_exists (const char *name, unsigned long crc,
 
      Some operating systems, e.g. Windows, do not provide a meaningful
      st_ino; they always set it to zero.  (Windows does provide a
-     meaningful st_dev.)  Do not indicate a duplicate library in that
-     case.  While there is no guarantee that a system that provides
-     meaningful inode numbers will never set st_ino to zero, this is
-     merely an optimization, so we do not need to worry about false
-     negatives.  */
+     meaningful st_dev.)  Files accessed from gdbservers that do not
+     support the vFile:fstat packet will also have st_ino set to zero.
+     Do not indicate a duplicate library in either case.  While there
+     is no guarantee that a system that provides meaningful inode
+     numbers will never set st_ino to zero, this is merely an
+     optimization, so we do not need to worry about false negatives.  */
 
   if (bfd_stat (abfd, &abfd_stat) == 0
       && abfd_stat.st_ino != 0
@@ -1409,9 +1410,9 @@ separate_debug_file_exists (const char *name, unsigned long crc,
     {
       unsigned long parent_crc;
 
-      /* If one (or both) the files are accessed for example the via "remote:"
-	 gdbserver way it does not support the bfd_stat operation.  Verify
-	 whether those two files are not the same manually.  */
+      /* If the files could not be verified as different with
+	 bfd_stat then we need to calculate the parent's CRC
+	 to verify whether the files are different or not.  */
 
       if (!verified_as_different)
 	{
-- 
1.7.1

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

* Re: [PATCH 3/3 v4] Implement remote_bfd_iovec_stat
  2015-03-11 12:58             ` [PATCH 3/3 v4] Implement remote_bfd_iovec_stat Gary Benson
@ 2015-03-11 13:15               ` Pedro Alves
  2015-03-11 13:16               ` Gary Benson
  1 sibling, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-11 13:15 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

On 03/11/2015 12:58 PM, Gary Benson wrote:
> Pedro Alves wrote:
>> On 03/11/2015 10:23 AM, Gary Benson wrote:
>>> Are you ok for me to commit this patch, reordered before the
>>> gdbserver changes, with extract_unsigned_integer used in
>>> remote_fileio_to_host_{uint,ulong}, and that comment added?
>>
>> It be easier for me to just see the patch before answering, but I
>> think I am, though I'd like to take another look at the docs patch
>> with everything combined, to cross check whether more bits would be
>> missing.
> 
> Ok, here goes:

Thanks.  Looks good to me.

Pedro Alves

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

* Re: [PATCH 3/3 v4] Implement remote_bfd_iovec_stat
  2015-03-11 12:58             ` [PATCH 3/3 v4] Implement remote_bfd_iovec_stat Gary Benson
  2015-03-11 13:15               ` Pedro Alves
@ 2015-03-11 13:16               ` Gary Benson
  2015-03-11 13:39                 ` [PATCH 0/3 v2] Gary Benson
                                   ` (3 more replies)
  1 sibling, 4 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-11 13:16 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

Gary Benson wrote:
> Pedro Alves wrote:
> > On 03/11/2015 10:23 AM, Gary Benson wrote:
> > > Are you ok for me to commit this patch, reordered before the
> > > gdbserver changes, with extract_unsigned_integer used in
> > > remote_fileio_to_host_{uint,ulong}, and that comment added?
> > 
> > It be easier for me to just see the patch before answering, but I
> > think I am, though I'd like to take another look at the docs patch
> > with everything combined, to cross check whether more bits would be
> > missing.
> 
> Ok, here goes:
[snip]

Actually, it occurred to me that with the patch reordering the docs
all ought to be in the patch that adds the GDB side, so, ignore the
one I just sent, I'll redo them both.

Cheers,
Gary

-- 
http://gbenson.net/

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

* [PATCH 0/3 v2]
  2015-03-11 13:16               ` Gary Benson
@ 2015-03-11 13:39                 ` Gary Benson
  2015-03-11 18:04                   ` [PATCH 0/3 v2] Properly implement remote_bfd_iovec_stat Gary Benson
  2015-03-11 13:40                 ` [PATCH 2/3 v5] Implement remote_bfd_iovec_stat Gary Benson
                                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-11 13:39 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

Hi all,

There's been a little too much to-and-fro on this series for me
to keep track of.  I *think* everything is approved now except
for the documentation in patch 2/3.  The various differences are
documented in the individual mails.

Built and regtested on RHEL 6.6 x86_64.

Ok to commit?

Cheers,
Gary

--
http://gbenson.net/

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

* [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common
  2015-03-11 13:16               ` Gary Benson
                                   ` (2 preceding siblings ...)
  2015-03-11 13:40                 ` [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver Gary Benson
@ 2015-03-11 13:40                 ` Gary Benson
  3 siblings, 0 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-11 13:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

This patch moves remote_fileio_to_fio_stat and its supporting
functions into new files common/common-remote-fileio.[ch].

This patch is unchanged from the one Pedro approved
(https://sourceware.org/ml/gdb-patches/2015-03/msg00260.html)
it's included here purely for completeness.

gdb/ChangeLog:

	* common/common-remote-fileio.h: New file.
	* common/common-remote-fileio.c: Likewise.
	* Makefile.in (SFILES): Add common/common-remote-fileio.c.
	(HFILES_NO_SRCDIR): Add common/common-remote-fileio.h.
	(COMMON_OBS): Add common-remote-fileio.o.
	(common-remote-fileio.o): New rule.
	* remote-fileio.h (common-remote-fileio.h): New include.
	* remote-fileio.c (gdb/fileio.h): Do not include.
	(remote_fileio_to_be): Moved to common-remote-fileio.h.
	(remote_fileio_to_fio_uint): Likewise.
	(remote_fileio_to_fio_time): Likewise.
	(remote_fileio_mode_to_target): Moved to common-remote-fileio.c.
	(remote_fileio_to_fio_mode): Likewise.
	(remote_fileio_to_fio_ulong): Likewise.
	(remote_fileio_to_fio_stat): Likewise.

gdb/gdbserver/ChangeLog:

	* configure.ac (AC_CHECK_MEMBERS): Add checks for
	struct stat.st_blocks and struct stat.st_blksize.
	* configure: Regenerate.
	* config.in: Likewise.
	* Makefile.in (SFILES): Add common/common-remote-fileio.c.
	(OBS): Add common-remote-fileio.o.
	(common-remote-fileio.o): New rule.
---
 gdb/ChangeLog                     |   18 ++++++
 gdb/Makefile.in                   |   10 ++-
 gdb/common/common-remote-fileio.c |  119 +++++++++++++++++++++++++++++++++++++
 gdb/common/common-remote-fileio.h |   61 +++++++++++++++++++
 gdb/gdbserver/ChangeLog           |   10 +++
 gdb/gdbserver/Makefile.in         |    8 ++-
 gdb/gdbserver/config.in           |    6 ++
 gdb/gdbserver/configure           |   77 ++++++++++++++++++++++++
 gdb/gdbserver/configure.ac        |    2 +
 gdb/remote-fileio.c               |  111 ----------------------------------
 gdb/remote-fileio.h               |    2 +
 11 files changed, 308 insertions(+), 116 deletions(-)
 create mode 100644 gdb/common/common-remote-fileio.c
 create mode 100644 gdb/common/common-remote-fileio.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 8c2a4de..0ddcd72 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -877,7 +877,7 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \
 	common/format.c common/filestuff.c btrace.c record-btrace.c ctf.c \
 	target/waitstatus.c common/print-utils.c common/rsp-low.c \
 	common/errors.c common/common-debug.c common/common-exceptions.c \
-	common/btrace-common.c \
+	common/btrace-common.c common/common-remote-fileio.c \
 	$(SUBDIR_GCC_COMPILE_SRCS)
 
 LINTFILES = $(SFILES) $(YYFILES) $(CONFIG_SRCS) init.c
@@ -966,7 +966,8 @@ common/print-utils.h common/rsp-low.h nat/x86-dregs.h x86-linux-nat.h \
 i386-linux-nat.h common/common-defs.h common/errors.h common/common-types.h \
 common/common-debug.h common/cleanups.h common/gdb_setjmp.h \
 common/common-exceptions.h target/target.h common/symbol.h \
-common/common-regcache.h fbsd-tdep.h nat/linux-personality.h
+common/common-regcache.h fbsd-tdep.h nat/linux-personality.h \
+common/common-remote-fileio.h
 
 # Header files that already have srcdir in them, or which are in objdir.
 
@@ -1065,7 +1066,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \
 	common-utils.o buffer.o ptid.o gdb-dlfcn.o common-agent.o \
 	format.o registry.o btrace.o record-btrace.o waitstatus.o \
 	print-utils.o rsp-low.o errors.o common-debug.o debug.o \
-	common-exceptions.o btrace-common.o \
+	common-exceptions.o btrace-common.o common-remote-fileio.o \
 	$(SUBDIR_GCC_COMPILE_OBS)
 
 TSOBS = inflow.o
@@ -2244,6 +2245,9 @@ btrace-common.o: ${srcdir}/common/btrace-common.c
 	$(COMPILE) $(srcdir)/common/btrace-common.c
 	$(POSTCOMPILE)
 
+common-remote-fileio.o: ${srcdir}/common/common-remote-fileio.c
+	$(COMPILE) $(srcdir)/common/common-remote-fileio.c
+	$(POSTCOMPILE)
 #
 # gdb/target/ dependencies
 #
diff --git a/gdb/common/common-remote-fileio.c b/gdb/common/common-remote-fileio.c
new file mode 100644
index 0000000..f78b3f7
--- /dev/null
+++ b/gdb/common/common-remote-fileio.c
@@ -0,0 +1,119 @@
+/* Remote File-I/O communications
+
+   Copyright (C) 2003-2015 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "common-defs.h"
+#include "common-remote-fileio.h"
+#include <sys/stat.h>
+
+/* Convert a host-format mode_t into a bitmask of File-I/O flags.  */
+
+static LONGEST
+remote_fileio_mode_to_target (mode_t mode)
+{
+  mode_t tmode = 0;
+
+  if (S_ISREG (mode))
+    tmode |= FILEIO_S_IFREG;
+  if (S_ISDIR (mode))
+    tmode |= FILEIO_S_IFDIR;
+  if (S_ISCHR (mode))
+    tmode |= FILEIO_S_IFCHR;
+  if (mode & S_IRUSR)
+    tmode |= FILEIO_S_IRUSR;
+  if (mode & S_IWUSR)
+    tmode |= FILEIO_S_IWUSR;
+  if (mode & S_IXUSR)
+    tmode |= FILEIO_S_IXUSR;
+#ifdef S_IRGRP
+  if (mode & S_IRGRP)
+    tmode |= FILEIO_S_IRGRP;
+#endif
+#ifdef S_IWRGRP
+  if (mode & S_IWGRP)
+    tmode |= FILEIO_S_IWGRP;
+#endif
+#ifdef S_IXGRP
+  if (mode & S_IXGRP)
+    tmode |= FILEIO_S_IXGRP;
+#endif
+  if (mode & S_IROTH)
+    tmode |= FILEIO_S_IROTH;
+#ifdef S_IWOTH
+  if (mode & S_IWOTH)
+    tmode |= FILEIO_S_IWOTH;
+#endif
+#ifdef S_IXOTH
+  if (mode & S_IXOTH)
+    tmode |= FILEIO_S_IXOTH;
+#endif
+  return tmode;
+}
+
+/* Pack a host-format mode_t into an fio_mode_t.  */
+
+static void
+remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
+{
+  remote_fileio_to_be (remote_fileio_mode_to_target (num),
+		       (char *) fnum, 4);
+}
+
+/* Pack a host-format integer into an fio_ulong_t.  */
+
+static void
+remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
+{
+  remote_fileio_to_be (num, (char *) fnum, 8);
+}
+
+/* See common-remote-fileio.h.  */
+
+void
+remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
+{
+  LONGEST blksize;
+
+  remote_fileio_to_fio_uint ((long) st->st_dev, fst->fst_dev);
+  remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
+  remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
+  remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
+  remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
+  remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
+  remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
+  remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  blksize = st->st_blksize;
+#else
+  blksize = 512;
+#endif
+  remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
+#else
+  /* FIXME: This is correct for DJGPP, but other systems that don't
+     have st_blocks, if any, might prefer 512 instead of st_blksize.
+     (eliz, 30-12-2003)  */
+  remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
+			      / blksize,
+			      fst->fst_blocks);
+#endif
+  remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
+  remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
+  remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
+}
diff --git a/gdb/common/common-remote-fileio.h b/gdb/common/common-remote-fileio.h
new file mode 100644
index 0000000..b838186
--- /dev/null
+++ b/gdb/common/common-remote-fileio.h
@@ -0,0 +1,61 @@
+/* Remote File-I/O communications
+
+   Copyright (C) 2003-2015 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef COMMON_REMOTE_FILEIO_H
+#define COMMON_REMOTE_FILEIO_H
+
+#include "gdb/fileio.h"
+
+struct stat;
+
+/* Pack a host-format integer into a byte buffer in big-endian format
+   ready for transmission over the remote protocol.  BYTES specifies
+   the size of the integer to pack in bytes.  */
+
+static inline void
+remote_fileio_to_be (LONGEST num, char *buf, int bytes)
+{
+  int i;
+
+  for (i = 0; i < bytes; ++i)
+    buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
+}
+
+/* Pack a host-format integer into an fio_uint_t.  */
+
+static inline void
+remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
+{
+  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
+}
+
+/* Pack a host-format time_t into an fio_time_t.  */
+
+static inline void
+remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
+{
+  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
+}
+
+/* Pack a host-format struct stat into a struct fio_stat.  */
+
+extern void remote_fileio_to_fio_stat (struct stat *st,
+				       struct fio_stat *fst);
+
+#endif /* COMMON_REMOTE_FILEIO_H */
diff --git a/gdb/gdbserver/Makefile.in b/gdb/gdbserver/Makefile.in
index 6dddf26..b425ee2 100644
--- a/gdb/gdbserver/Makefile.in
+++ b/gdb/gdbserver/Makefile.in
@@ -179,7 +179,8 @@ SFILES=	$(srcdir)/gdbreplay.c $(srcdir)/inferiors.c $(srcdir)/dll.c \
 	$(srcdir)/common/rsp-low.c $(srcdir)/common/errors.c \
 	$(srcdir)/common/common-debug.c $(srcdir)/common/cleanups.c \
 	$(srcdir)/common/common-exceptions.c $(srcdir)/symbol.c \
-	$(srcdir)/common/btrace-common.c
+	$(srcdir)/common/btrace-common.c \
+	$(srcdir)/common/common-remote-fileio.c
 
 DEPFILES = @GDBSERVER_DEPFILES@
 
@@ -193,7 +194,7 @@ OBS = agent.o ax.o inferiors.o regcache.o remote-utils.o server.o signals.o \
       mem-break.o hostio.o event-loop.o tracepoint.o xml-utils.o \
       common-utils.o ptid.o buffer.o format.o filestuff.o dll.o notif.o \
       tdesc.o print-utils.o rsp-low.o errors.o common-debug.o cleanups.o \
-      common-exceptions.o symbol.o btrace-common.o \
+      common-exceptions.o symbol.o btrace-common.o common-remote-fileio.o \
       $(XML_BUILTIN) $(DEPFILES) $(LIBOBJS)
 GDBREPLAY_OBS = gdbreplay.o version.o
 GDBSERVER_LIBS = @GDBSERVER_LIBS@
@@ -572,6 +573,9 @@ common-exceptions.o: ../common/common-exceptions.c
 waitstatus.o: ../target/waitstatus.c
 	$(COMPILE) $<
 	$(POSTCOMPILE)
+common-remote-fileio.o: ../common/common-remote-fileio.c
+	$(COMPILE) $<
+	$(POSTCOMPILE)
 
 # Native object files rules from ../nat
 
diff --git a/gdb/gdbserver/config.in b/gdb/gdbserver/config.in
index 8114628..3c3bfca 100644
--- a/gdb/gdbserver/config.in
+++ b/gdb/gdbserver/config.in
@@ -210,6 +210,12 @@
 /* Define to 1 if you have the <string.h> header file. */
 #undef HAVE_STRING_H
 
+/* Define to 1 if `struct stat' is a member of `st_blksize'. */
+#undef HAVE_STRUCT_STAT_ST_BLKSIZE
+
+/* Define to 1 if `struct stat' is a member of `st_blocks'. */
+#undef HAVE_STRUCT_STAT_ST_BLOCKS
+
 /* Define to 1 if the target supports __sync_*_compare_and_swap */
 #undef HAVE_SYNC_BUILTINS
 
diff --git a/gdb/gdbserver/configure b/gdb/gdbserver/configure
index a14def9..6e807fd 100755
--- a/gdb/gdbserver/configure
+++ b/gdb/gdbserver/configure
@@ -2109,6 +2109,63 @@ rm -f conftest.val
   return $ac_retval
 
 } # ac_fn_c_compute_int
+
+# ac_fn_c_check_member LINENO AGGR MEMBER VAR INCLUDES
+# ----------------------------------------------------
+# Tries to find if the field MEMBER exists in type AGGR, after including
+# INCLUDES, setting cache variable VAR accordingly.
+ac_fn_c_check_member ()
+{
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2.$3" >&5
+$as_echo_n "checking for $2.$3... " >&6; }
+if { as_var=$4; eval "test \"\${$as_var+set}\" = set"; }; then :
+  $as_echo_n "(cached) " >&6
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$5
+int
+main ()
+{
+static $2 ac_aggr;
+if (ac_aggr.$3)
+return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  eval "$4=yes"
+else
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+$5
+int
+main ()
+{
+static $2 ac_aggr;
+if (sizeof ac_aggr.$3)
+return 0;
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_compile "$LINENO"; then :
+  eval "$4=yes"
+else
+  eval "$4=no"
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+fi
+eval ac_res=\$$4
+	       { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
+$as_echo "$ac_res" >&6; }
+  eval $as_lineno_stack; test "x$as_lineno_stack" = x && { as_lineno=; unset as_lineno;}
+
+} # ac_fn_c_check_member
 cat >config.log <<_ACEOF
 This file contains any messages produced by compilers while
 running configure, to aid debugging if configure makes a mistake.
@@ -6083,6 +6140,26 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
+ac_fn_c_check_member "$LINENO" "struct stat" "st_blocks" "ac_cv_member_struct_stat_st_blocks" "$ac_includes_default"
+if test "x$ac_cv_member_struct_stat_st_blocks" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_BLOCKS 1
+_ACEOF
+
+
+fi
+ac_fn_c_check_member "$LINENO" "struct stat" "st_blksize" "ac_cv_member_struct_stat_st_blksize" "$ac_includes_default"
+if test "x$ac_cv_member_struct_stat_st_blksize" = x""yes; then :
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_STRUCT_STAT_ST_BLKSIZE 1
+_ACEOF
+
+
+fi
+
+
 ac_fn_c_check_type "$LINENO" "socklen_t" "ac_cv_type_socklen_t" "#include <sys/types.h>
 #include <sys/socket.h>
 
diff --git a/gdb/gdbserver/configure.ac b/gdb/gdbserver/configure.ac
index cefc69e..8d0b4e3 100644
--- a/gdb/gdbserver/configure.ac
+++ b/gdb/gdbserver/configure.ac
@@ -224,6 +224,8 @@ libiberty_INIT
 
 AC_CHECK_DECLS([strerror, perror, vasprintf, vsnprintf])
 
+AC_CHECK_MEMBERS([struct stat.st_blocks, struct stat.st_blksize])
+
 AC_CHECK_TYPES(socklen_t, [], [],
 [#include <sys/types.h>
 #include <sys/socket.h>
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 00a86ca..0ce0714 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -22,7 +22,6 @@
 #include "defs.h"
 #include "gdbcmd.h"
 #include "remote.h"
-#include "gdb/fileio.h"
 #include "gdb_wait.h"
 #include <sys/stat.h>
 #include "remote-fileio.h"
@@ -194,48 +193,6 @@ remote_fileio_mode_to_host (long mode, int open_call)
   return hmode;
 }
 
-static LONGEST
-remote_fileio_mode_to_target (mode_t mode)
-{
-  mode_t tmode = 0;
-
-  if (S_ISREG(mode))
-    tmode |= FILEIO_S_IFREG;
-  if (S_ISDIR(mode))
-    tmode |= FILEIO_S_IFDIR;
-  if (S_ISCHR(mode))
-    tmode |= FILEIO_S_IFCHR;
-  if (mode & S_IRUSR)
-    tmode |= FILEIO_S_IRUSR;
-  if (mode & S_IWUSR)
-    tmode |= FILEIO_S_IWUSR;
-  if (mode & S_IXUSR)
-    tmode |= FILEIO_S_IXUSR;
-#ifdef S_IRGRP
-  if (mode & S_IRGRP)
-    tmode |= FILEIO_S_IRGRP;
-#endif
-#ifdef S_IWRGRP
-  if (mode & S_IWGRP)
-    tmode |= FILEIO_S_IWGRP;
-#endif
-#ifdef S_IXGRP
-  if (mode & S_IXGRP)
-    tmode |= FILEIO_S_IXGRP;
-#endif
-  if (mode & S_IROTH)
-    tmode |= FILEIO_S_IROTH;
-#ifdef S_IWOTH
-  if (mode & S_IWOTH)
-    tmode |= FILEIO_S_IWOTH;
-#endif
-#ifdef S_IXOTH
-  if (mode & S_IXOTH)
-    tmode |= FILEIO_S_IXOTH;
-#endif
-  return tmode;
-}
-
 static int
 remote_fileio_errno_to_target (int error)
 {
@@ -381,34 +338,6 @@ remote_fileio_extract_ptr_w_len (char **buf, CORE_ADDR *ptrval, int *length)
   return 0;
 }
 
-/* Convert to big endian.  */
-static void
-remote_fileio_to_be (LONGEST num, char *buf, int bytes)
-{
-  int i;
-
-  for (i = 0; i < bytes; ++i)
-    buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
-}
-
-static void
-remote_fileio_to_fio_uint (long num, fio_uint_t fnum)
-{
-  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
-}
-
-static void
-remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum)
-{
-  remote_fileio_to_be (remote_fileio_mode_to_target(num), (char *) fnum, 4);
-}
-
-static void
-remote_fileio_to_fio_time (time_t num, fio_time_t fnum)
-{
-  remote_fileio_to_be ((LONGEST) num, (char *) fnum, 4);
-}
-
 static void
 remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
 {
@@ -416,46 +345,6 @@ remote_fileio_to_fio_long (LONGEST num, fio_long_t fnum)
 }
 
 static void
-remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum)
-{
-  remote_fileio_to_be (num, (char *) fnum, 8);
-}
-
-static void
-remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst)
-{
-  LONGEST blksize;
-
-  /* `st_dev' is set in the calling function.  */
-  remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino);
-  remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode);
-  remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink);
-  remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid);
-  remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid);
-  remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev);
-  remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size);
-#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
-  blksize = st->st_blksize;
-#else
-  blksize = 512;
-#endif
-  remote_fileio_to_fio_ulong (blksize, fst->fst_blksize);
-#if HAVE_STRUCT_STAT_ST_BLOCKS
-  remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
-#else
-  /* FIXME: This is correct for DJGPP, but other systems that don't
-     have st_blocks, if any, might prefer 512 instead of st_blksize.
-     (eliz, 30-12-2003)  */
-  remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1)
-			      / blksize,
-			      fst->fst_blocks);
-#endif
-  remote_fileio_to_fio_time (st->st_atime, fst->fst_atime);
-  remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime);
-  remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime);
-}
-
-static void
 remote_fileio_to_fio_timeval (struct timeval *tv, struct fio_timeval *ftv)
 {
   remote_fileio_to_fio_time (tv->tv_sec, ftv->ftv_sec);
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 40a614a..8b32868 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -22,6 +22,8 @@
 #ifndef REMOTE_FILEIO_H
 #define REMOTE_FILEIO_H
 
+#include "common-remote-fileio.h"
+
 struct cmd_list_element;
 
 /* Unified interface to remote fileio, called in remote.c from
-- 
1.7.1

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

* [PATCH 2/3 v5] Implement remote_bfd_iovec_stat
  2015-03-11 13:16               ` Gary Benson
  2015-03-11 13:39                 ` [PATCH 0/3 v2] Gary Benson
@ 2015-03-11 13:40                 ` Gary Benson
  2015-03-11 13:52                   ` Pedro Alves
  2015-03-11 13:40                 ` [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver Gary Benson
  2015-03-11 13:40                 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
  3 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-11 13:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

This patch implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

This was patch 3/3 in the original series, but it's been moved
before the patch that adds the gdbserver side.  Also, all NEWS
and documentation has been moved into this patch.

The documentation has been updated to mention that vFile:fstat
is not probed by default.

gdb/ChangeLog:

	* remote-fileio.h (remote_fileio_to_host_stat): New declaration.
	* remote-fileio.c (remote_fileio_to_host_uint): New function.
	(remote_fileio_to_host_ulong): Likewise.
	(remote_fileio_to_host_mode): Likewise.
	(remote_fileio_to_host_time): Likewise.
	(remote_fileio_to_host_stat): Likewise.
	* remote.c (PACKET_vFile_fstat): New enum value.
	(remote_protocol_features): Register the "vFile:fstat" feature.
	(remote_hostio_fstat): New function.
	(remote_bfd_iovec_stat): Use the above.
	(_initialize_remote): Register new "set/show remote
	hostio-fstat-packet" command.
	* symfile.c (separate_debug_file_exists): Update comment.
	* NEWS: Announce new vFile:fstat packet.

gdb/doc/ChangeLog:

	* gdb.texinfo (Remote Configuration): Document the
	"set/show remote hostio-fstat-packet" command.
	(General Query Packets): Document the vFile:fstat
	qSupported features.
	(Host I/O Packets): Document the vFile:fstat packet.
---
 gdb/ChangeLog       |   17 ++++++++++
 gdb/NEWS            |    3 ++
 gdb/doc/ChangeLog   |    8 +++++
 gdb/doc/gdb.texinfo |   23 +++++++++++++
 gdb/remote-fileio.c |   64 +++++++++++++++++++++++++++++++++++++
 gdb/remote-fileio.h |    4 ++
 gdb/remote.c        |   87 ++++++++++++++++++++++++++++++++++++++++++++++++--
 gdb/symfile.c       |   17 +++++-----
 8 files changed, 211 insertions(+), 12 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 49dc0e6..f08f972 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -73,6 +73,9 @@ hwbreak stop reason
   Indicates the target stopped for a hardware breakpoint.  This is
   required for correct non-stop mode operation.
 
+vFile:fstat:
+  Return information about files on the remote system.
+
 * The info record command now shows the recording format and the
   branch tracing configuration for the current thread when using
   the btrace record target.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 4b76ce9..9e71642 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -19748,6 +19748,10 @@ are:
 @tab @code{vFile:readlink}
 @tab Host I/O
 
+@item @code{hostio-fstat-packet}
+@tab @code{vFile:fstat}
+@tab Host I/O
+
 @item @code{noack-packet}
 @tab @code{QStartNoAckMode}
 @tab Packet acknowledgment
@@ -36058,6 +36062,11 @@ These are the currently defined stub features and their properties:
 @tab @samp{-}
 @tab No
 
+@item @samp{vFile:fstat}
+@tab No
+@tab @samp{-}
+@tab No
+
 @end multitable
 
 These are the currently defined stub features, in more detail:
@@ -36246,6 +36255,9 @@ breakpoints.
 The remote stub reports the @samp{hwbreak} stop reason for hardware
 breakpoints.
 
+@item vFile:fstat
+The remote stub understands the @samp{vFile:fstat} packet.
+
 @end table
 
 @item qSymbol::
@@ -37407,6 +37419,17 @@ packet is used.  @samp{vFile:write} returns the number of bytes written,
 which may be shorter than the length of @var{data}, or -1 if an
 error occurred.
 
+@item vFile:fstat: @var{fd}
+Get information about the open file corresponding to @var{fd}.
+On success the information is returned as a binary attachment
+and the return value is the size of this attachment in bytes.
+If an error occurs the return value is -1.  The format of the
+returned binary attachment is as described in @ref{struct stat}.
+
+This packet is not probed by default; the remote stub must request
+it, by supplying an appropriate @samp{qSupported} response
+(@pxref{qSupported}).
+
 @item vFile:unlink: @var{filename}
 Delete the file at @var{filename} on the target.  Return 0,
 or -1 if an error occurs.  The @var{filename} is a string.
diff --git a/gdb/remote-fileio.c b/gdb/remote-fileio.c
index 0ce0714..3882321 100644
--- a/gdb/remote-fileio.c
+++ b/gdb/remote-fileio.c
@@ -1321,6 +1321,70 @@ remote_fileio_request (char *buf, int ctrlc_pending_p)
 
   remote_fileio_sig_exit ();
 }
+\f
+
+/* Unpack an fio_uint_t.  */
+
+static unsigned int
+remote_fileio_to_host_uint (fio_uint_t fnum)
+{
+  return extract_unsigned_integer ((gdb_byte *) fnum, 4,
+				   BFD_ENDIAN_BIG);
+}
+
+/* Unpack an fio_ulong_t.  */
+
+static ULONGEST
+remote_fileio_to_host_ulong (fio_ulong_t fnum)
+{
+  return extract_unsigned_integer ((gdb_byte *) fnum, 8,
+				   BFD_ENDIAN_BIG);
+}
+
+/* Unpack an fio_mode_t.  */
+
+static mode_t
+remote_fileio_to_host_mode (fio_mode_t fnum)
+{
+  return remote_fileio_mode_to_host (remote_fileio_to_host_uint (fnum),
+				     0);
+}
+
+/* Unpack an fio_time_t.  */
+
+static time_t
+remote_fileio_to_host_time (fio_time_t fnum)
+{
+  return remote_fileio_to_host_uint (fnum);
+}
+
+
+/* See remote-fileio.h.  */
+
+void
+remote_fileio_to_host_stat (struct fio_stat *fst, struct stat *st)
+{
+  memset (st, 0, sizeof (struct stat));
+
+  st->st_dev = remote_fileio_to_host_uint (fst->fst_dev);
+  st->st_ino = remote_fileio_to_host_uint (fst->fst_ino);
+  st->st_mode = remote_fileio_to_host_mode (fst->fst_mode);
+  st->st_nlink = remote_fileio_to_host_uint (fst->fst_nlink);
+  st->st_uid = remote_fileio_to_host_uint (fst->fst_uid);
+  st->st_gid = remote_fileio_to_host_uint (fst->fst_gid);
+  st->st_rdev = remote_fileio_to_host_uint (fst->fst_rdev);
+  st->st_size = remote_fileio_to_host_ulong (fst->fst_size);
+#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
+  st->st_blksize = remote_fileio_to_host_ulong (fst->fst_blksize);
+#endif
+#if HAVE_STRUCT_STAT_ST_BLOCKS
+  st->st_blocks = remote_fileio_to_host_ulong (fst->fst_blocks);
+#endif
+  st->st_atime = remote_fileio_to_host_time (fst->fst_atime);
+  st->st_mtime = remote_fileio_to_host_time (fst->fst_mtime);
+  st->st_ctime = remote_fileio_to_host_time (fst->fst_ctime);
+}
+\f
 
 static void
 set_system_call_allowed (char *args, int from_tty)
diff --git a/gdb/remote-fileio.h b/gdb/remote-fileio.h
index 8b32868..d30ae6a 100644
--- a/gdb/remote-fileio.h
+++ b/gdb/remote-fileio.h
@@ -38,4 +38,8 @@ extern void initialize_remote_fileio (
   struct cmd_list_element *remote_set_cmdlist,
   struct cmd_list_element *remote_show_cmdlist);
 
+/* Unpack a struct fio_stat.  */
+extern void remote_fileio_to_host_stat (struct fio_stat *fst,
+					struct stat *st);
+
 #endif
diff --git a/gdb/remote.c b/gdb/remote.c
index 495dfdc..14ad127 100644
--- a/gdb/remote.c
+++ b/gdb/remote.c
@@ -1253,6 +1253,7 @@ enum {
   PACKET_vFile_close,
   PACKET_vFile_unlink,
   PACKET_vFile_readlink,
+  PACKET_vFile_fstat,
   PACKET_qXfer_auxv,
   PACKET_qXfer_features,
   PACKET_qXfer_libraries,
@@ -4042,7 +4043,9 @@ static const struct protocol_feature remote_protocol_features[] = {
   { "Qbtrace-conf:bts:size", PACKET_DISABLE, remote_supported_packet,
     PACKET_Qbtrace_conf_bts_size },
   { "swbreak", PACKET_DISABLE, remote_supported_packet, PACKET_swbreak_feature },
-  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature }
+  { "hwbreak", PACKET_DISABLE, remote_supported_packet, PACKET_hwbreak_feature },
+  { "vFile:fstat", PACKET_DISABLE, remote_supported_packet,
+    PACKET_vFile_fstat },
 };
 
 static char *remote_support_xml;
@@ -10059,6 +10062,68 @@ remote_hostio_readlink (struct target_ops *self,
   return ret;
 }
 
+/* Read information about the open file FD on the remote target
+   into ST.  Return 0 on success, or -1 if an error occurs (and
+   set *REMOTE_ERRNO).  */
+
+static int
+remote_hostio_fstat (struct target_ops *self,
+		     int fd, struct stat *st,
+		     int *remote_errno)
+{
+  struct remote_state *rs = get_remote_state ();
+  char *p = rs->buf;
+  int left = get_remote_packet_size ();
+  int attachment_len, ret;
+  char *attachment;
+  struct fio_stat fst;
+  int read_len;
+
+  if (packet_support (PACKET_vFile_fstat) != PACKET_ENABLE)
+    {
+      /* Strictly we should return -1, ENOSYS here, but when
+	 "set sysroot remote:" was implemented in August 2008
+	 BFD's need for a stat function was sidestepped with
+	 this hack.  This was not remedied until March 2015
+	 so we retain the previous behavior to avoid breaking
+	 compatibility.
+
+	 Note that the memset is a March 2015 addition; older
+	 GDBs set st_size *and nothing else* so the structure
+	 would have garbage in all other fields.  This might
+	 break something but retaining the previous behavior
+	 here would be just too wrong.  */
+
+      memset (st, 0, sizeof (struct stat));
+      st->st_size = INT_MAX;
+      return 0;
+    }
+
+  remote_buffer_add_string (&p, &left, "vFile:fstat:");
+
+  remote_buffer_add_int (&p, &left, fd);
+
+  ret = remote_hostio_send_command (p - rs->buf, PACKET_vFile_fstat,
+				    remote_errno, &attachment,
+				    &attachment_len);
+  if (ret < 0)
+    return ret;
+
+  read_len = remote_unescape_input ((gdb_byte *) attachment, attachment_len,
+				    (gdb_byte *) &fst, sizeof (fst));
+
+  if (read_len != ret)
+    error (_("vFile:fstat returned %d, but %d bytes."), ret, read_len);
+
+  if (read_len != sizeof (fst))
+    error (_("vFile:fstat returned %d bytes, but expecting %d."),
+	   read_len, (int) sizeof (fst));
+
+  remote_fileio_to_host_stat (&fst, st);
+
+  return 0;
+}
+
 static int
 remote_fileio_errno_to_host (int errnum)
 {
@@ -10203,9 +10268,20 @@ remote_bfd_iovec_pread (struct bfd *abfd, void *stream, void *buf,
 static int
 remote_bfd_iovec_stat (struct bfd *abfd, void *stream, struct stat *sb)
 {
-  /* FIXME: We should probably implement remote_hostio_stat.  */
-  sb->st_size = INT_MAX;
-  return 0;
+  int fd = *(int *) stream;
+  int remote_errno;
+  int result;
+
+  result = remote_hostio_fstat (find_target_at (process_stratum),
+				fd, sb, &remote_errno);
+
+  if (result == -1)
+    {
+      errno = remote_fileio_errno_to_host (remote_errno);
+      bfd_set_error (bfd_error_system_call);
+    }
+
+  return result;
 }
 
 int
@@ -12342,6 +12418,9 @@ Show the maximum size of the address (in bits) in a memory packet."), NULL,
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_readlink],
 			 "vFile:readlink", "hostio-readlink", 0);
 
+  add_packet_config_cmd (&remote_protocol_packets[PACKET_vFile_fstat],
+			 "vFile:fstat", "hostio-fstat", 0);
+
   add_packet_config_cmd (&remote_protocol_packets[PACKET_vAttach],
 			 "vAttach", "attach", 0);
 
diff --git a/gdb/symfile.c b/gdb/symfile.c
index c2a71ec..ba099d3 100644
--- a/gdb/symfile.c
+++ b/gdb/symfile.c
@@ -1377,11 +1377,12 @@ separate_debug_file_exists (const char *name, unsigned long crc,
 
      Some operating systems, e.g. Windows, do not provide a meaningful
      st_ino; they always set it to zero.  (Windows does provide a
-     meaningful st_dev.)  Do not indicate a duplicate library in that
-     case.  While there is no guarantee that a system that provides
-     meaningful inode numbers will never set st_ino to zero, this is
-     merely an optimization, so we do not need to worry about false
-     negatives.  */
+     meaningful st_dev.)  Files accessed from gdbservers that do not
+     support the vFile:fstat packet will also have st_ino set to zero.
+     Do not indicate a duplicate library in either case.  While there
+     is no guarantee that a system that provides meaningful inode
+     numbers will never set st_ino to zero, this is merely an
+     optimization, so we do not need to worry about false negatives.  */
 
   if (bfd_stat (abfd, &abfd_stat) == 0
       && abfd_stat.st_ino != 0
@@ -1409,9 +1410,9 @@ separate_debug_file_exists (const char *name, unsigned long crc,
     {
       unsigned long parent_crc;
 
-      /* If one (or both) the files are accessed for example the via "remote:"
-	 gdbserver way it does not support the bfd_stat operation.  Verify
-	 whether those two files are not the same manually.  */
+      /* If the files could not be verified as different with
+	 bfd_stat then we need to calculate the parent's CRC
+	 to verify whether the files are different or not.  */
 
       if (!verified_as_different)
 	{
-- 
1.7.1

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

* [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver
  2015-03-11 13:16               ` Gary Benson
  2015-03-11 13:39                 ` [PATCH 0/3 v2] Gary Benson
  2015-03-11 13:40                 ` [PATCH 2/3 v5] Implement remote_bfd_iovec_stat Gary Benson
@ 2015-03-11 13:40                 ` Gary Benson
  2015-03-11 13:53                   ` Pedro Alves
  2015-03-11 13:40                 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
  3 siblings, 1 reply; 28+ messages in thread
From: Gary Benson @ 2015-03-11 13:40 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

This patch adds a new packet "vFile:fstat:" to gdbserver.
This can be used by clients to retrieve information about
open files.

This was patch 2/3 in the original series, but it's been
moved to the end.  Also, all documentation has been moved
into what is now patch 2/3, the patch that adds the GDB side.

handle_fstat has been updated to fail with E01 if the response
doesn't fit in a single packet.

gdb/gdbserver/ChangeLog:

	* hostio.c (sys/types.h): New include.
	(sys/stat.h): Likewise.
	(common-remote-fileio.h): Likewise.
	(handle_fstat): New function.
	(handle_vFile): Handle vFile:fstat packets.
	* server.c (handle_query): Report vFile:fstat as supported.
---
 gdb/gdbserver/ChangeLog |    9 +++++++++
 gdb/gdbserver/hostio.c  |   41 +++++++++++++++++++++++++++++++++++++++++
 gdb/gdbserver/server.c  |    2 ++
 3 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/gdb/gdbserver/hostio.c b/gdb/gdbserver/hostio.c
index ec29eb9..df43e27 100644
--- a/gdb/gdbserver/hostio.c
+++ b/gdb/gdbserver/hostio.c
@@ -25,6 +25,9 @@
 #include <fcntl.h>
 #include <limits.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "common-remote-fileio.h"
 
 extern int remote_debug;
 
@@ -412,6 +415,42 @@ handle_pwrite (char *own_buf, int packet_len)
 }
 
 static void
+handle_fstat (char *own_buf, int *new_packet_len)
+{
+  int fd, bytes_sent;
+  char *p;
+  struct stat st;
+  struct fio_stat fst;
+
+  p = own_buf + strlen ("vFile:fstat:");
+
+  if (require_int (&p, &fd)
+      || require_valid_fd (fd)
+      || require_end (p))
+    {
+      hostio_packet_error (own_buf);
+      return;
+    }
+
+  if (fstat (fd, &st) == -1)
+    {
+      hostio_error (own_buf);
+      return;
+    }
+
+  remote_fileio_to_fio_stat (&st, &fst);
+
+  bytes_sent = hostio_reply_with_data (own_buf,
+				       (char *) &fst, sizeof (fst),
+				       new_packet_len);
+
+  /* If the response does not fit into a single packet, do not attempt
+     to return a partial response, but simply fail.  */
+  if (bytes_sent < sizeof (fst))
+    strcpy (own_buf, "E01");
+}
+
+static void
 handle_close (char *own_buf)
 {
   int fd, ret;
@@ -517,6 +556,8 @@ handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
     handle_pread (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:pwrite:"))
     handle_pwrite (own_buf, packet_len);
+  else if (startswith (own_buf, "vFile:fstat:"))
+    handle_fstat (own_buf, new_packet_len);
   else if (startswith (own_buf, "vFile:close:"))
     handle_close (own_buf);
   else if (startswith (own_buf, "vFile:unlink:"))
diff --git a/gdb/gdbserver/server.c b/gdb/gdbserver/server.c
index 4189877..4d8faa3 100644
--- a/gdb/gdbserver/server.c
+++ b/gdb/gdbserver/server.c
@@ -2082,6 +2082,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
       if (target_supports_stopped_by_hw_breakpoint ())
 	strcat (own_buf, ";hwbreak+");
 
+      strcat (own_buf, ";vFile:fstat+");
+
       return;
     }
 
-- 
1.7.1

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

* Re: [PATCH 2/3 v5] Implement remote_bfd_iovec_stat
  2015-03-11 13:40                 ` [PATCH 2/3 v5] Implement remote_bfd_iovec_stat Gary Benson
@ 2015-03-11 13:52                   ` Pedro Alves
  0 siblings, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-11 13:52 UTC (permalink / raw)
  To: Gary Benson, gdb-patches; +Cc: Eli Zaretskii

On 03/11/2015 01:39 PM, Gary Benson wrote:
> This patch implements the function remote_bfd_iovec_stat using a
> vFile:fstat hostio call to the remote target.  If vFile:fstat is
> not supported GDB creates a dummy result by zeroing the supplied
> stat structure and then setting it's st_size field to INT_MAX.
> This mimic's GDB's previous behaviour, with the exception that
> GDB did not previously zero the structure so all other fields
> would have been returned unchanged, which is to say very likely
> populated with random values from the stack.

Note this reads as if the packet already existed.  It should
be updated to say something like:

Subject: Add "vFile:fstat:", implement remote_bfd_iovec_stat

This patch adds a new packet "vFile:fstat:" to gdb.
This can be used by to retrieve information about files that have
been previously open with vFile:open.
This patch then implements the function remote_bfd_iovec_stat using a
vFile:fstat hostio call to the remote target.  If vFile:fstat is
not supported GDB creates a dummy result by zeroing the supplied
stat structure and then setting it's st_size field to INT_MAX.
This mimic's GDB's previous behaviour, with the exception that
GDB did not previously zero the structure so all other fields
would have been returned unchanged, which is to say very likely
populated with random values from the stack.

> +@item vFile:fstat: @var{fd}
> +Get information about the open file corresponding to @var{fd}.
> +On success the information is returned as a binary attachment
> +and the return value is the size of this attachment in bytes.
> +If an error occurs the return value is -1.  The format of the
> +returned binary attachment is as described in @ref{struct stat}.
> +
> +This packet is not probed by default; the remote stub must request
> +it, by supplying an appropriate @samp{qSupported} response
> +(@pxref{qSupported}).

Thanks for adding this.  The patch looks great to me now.

Thanks,
Pedro Alves

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

* Re: [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver
  2015-03-11 13:40                 ` [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver Gary Benson
@ 2015-03-11 13:53                   ` Pedro Alves
  0 siblings, 0 replies; 28+ messages in thread
From: Pedro Alves @ 2015-03-11 13:53 UTC (permalink / raw)
  To: Gary Benson, gdb-patches; +Cc: Eli Zaretskii

On 03/11/2015 01:39 PM, Gary Benson wrote:
> This patch adds a new packet "vFile:fstat:" to gdbserver.
> This can be used by clients to retrieve information about
> open files.

Should now say something like:

This patch implement support for "vFile:fstat:" in gdbserver.

> +  /* If the response does not fit into a single packet, do not attempt
> +     to return a partial response, but simply fail.  */
> +  if (bytes_sent < sizeof (fst))
> +    strcpy (own_buf, "E01");

Simply use write_enn.

OK with these changes.

Thanks,
Pedro Alves

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

* Re: [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver
  2015-03-11 10:33     ` [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver Gary Benson
  2015-03-11 11:35       ` Pedro Alves
@ 2015-03-11 16:00       ` Eli Zaretskii
  1 sibling, 0 replies; 28+ messages in thread
From: Eli Zaretskii @ 2015-03-11 16:00 UTC (permalink / raw)
  To: Gary Benson; +Cc: gdb-patches, palves

> From: Gary Benson <gbenson@redhat.com>
> Cc: Pedro Alves <palves@redhat.com>, Eli Zaretskii <eliz@gnu.org>
> Date: Wed, 11 Mar 2015 10:33:49 +0000
> 
> Eli, could you please review the qSupported part of the docs in
> this patch?  The remainder of the patch is unchanged from the
> patch you previously approved.

I think I already approved this as well, but you have my OK this time
also.

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

* Re: [PATCH 0/3 v2] Properly implement remote_bfd_iovec_stat
  2015-03-11 13:39                 ` [PATCH 0/3 v2] Gary Benson
@ 2015-03-11 18:04                   ` Gary Benson
  0 siblings, 0 replies; 28+ messages in thread
From: Gary Benson @ 2015-03-11 18:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves, Eli Zaretskii

Gary Benson wrote:
> There's been a little too much to-and-fro on this series for me
> to keep track of.  I *think* everything is approved now except
> for the documentation in patch 2/3.  The various differences are
> documented in the individual mails.
> 
> Built and regtested on RHEL 6.6 x86_64.
> 
> Ok to commit?

This is all pushed now, thank you Pedro and Eli for the reviews.

Cheers,
Gary

-- 
http://gbenson.net/

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

end of thread, other threads:[~2015-03-11 18:04 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-09 11:58 [PATCH 0/3] Properly implement remote_bfd_iovec_stat Gary Benson
2015-03-09 11:58 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson
2015-03-10 18:30   ` Pedro Alves
2015-03-09 11:58 ` [PATCH 2/3] Add vFile:fstat: packet to gdbserver Gary Benson
2015-03-10 18:31   ` Pedro Alves
2015-03-11 10:33     ` [PATCH 2/3 v2] Implement vFile:fstat: packet in gdbserver Gary Benson
2015-03-11 11:35       ` Pedro Alves
2015-03-11 16:00       ` Eli Zaretskii
2015-03-09 11:58 ` [PATCH 3/3] Implement remote_bfd_iovec_stat using vFile:fstat Gary Benson
2015-03-09 16:17   ` Eli Zaretskii
2015-03-10  9:32     ` Gary Benson
2015-03-10 16:40       ` Eli Zaretskii
2015-03-10 11:20   ` [PATCH 3/3 v2] " Gary Benson
2015-03-10 15:51     ` [PATCH 3/3 v3] " Gary Benson
2015-03-10 18:31       ` Pedro Alves
2015-03-10 18:36       ` Pedro Alves
2015-03-11 10:23         ` Gary Benson
2015-03-11 11:22           ` Pedro Alves
2015-03-11 12:58             ` [PATCH 3/3 v4] Implement remote_bfd_iovec_stat Gary Benson
2015-03-11 13:15               ` Pedro Alves
2015-03-11 13:16               ` Gary Benson
2015-03-11 13:39                 ` [PATCH 0/3 v2] Gary Benson
2015-03-11 18:04                   ` [PATCH 0/3 v2] Properly implement remote_bfd_iovec_stat Gary Benson
2015-03-11 13:40                 ` [PATCH 2/3 v5] Implement remote_bfd_iovec_stat Gary Benson
2015-03-11 13:52                   ` Pedro Alves
2015-03-11 13:40                 ` [PATCH 3/3 v3] Implement vFile:fstat: packet in gdbserver Gary Benson
2015-03-11 13:53                   ` Pedro Alves
2015-03-11 13:40                 ` [PATCH 1/3] Move remote_fileio_to_fio_stat to gdb/common Gary Benson

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