]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Implement vFile:setfs in gdbserver
authorGary Benson <gbenson@redhat.com>
Wed, 10 Jun 2015 13:28:44 +0000 (14:28 +0100)
committerGary Benson <gbenson@redhat.com>
Wed, 10 Jun 2015 13:28:44 +0000 (14:28 +0100)
This commit implements the "vFile:setfs" packet in gdbserver.

gdb/gdbserver/ChangeLog:

* target.h (struct target_ops) <multifs_open>: New field.
<multifs_unlink>: Likewise.
<multifs_readlink>: Likewise.
* linux-low.c (nat/linux-namespaces.h): New include.
(linux_target_ops): Initialize the_target->multifs_open,
the_target->multifs_unlink and the_target->multifs_readlink.
* hostio.h (hostio_handle_new_gdb_connection): New declaration.
* hostio.c (hostio_fs_pid): New static variable.
(hostio_handle_new_gdb_connection): New function.
(handle_setfs): Likewise.
(handle_open): Use the_target->multifs_open as appropriate.
(handle_unlink): Use the_target->multifs_unlink as appropriate.
(handle_readlink): Use the_target->multifs_readlink as
appropriate.
(handle_vFile): Handle vFile:setfs packets.
* server.c (handle_query): Call hostio_handle_new_gdb_connection
after target_handle_new_gdb_connection.

gdb/gdbserver/ChangeLog
gdb/gdbserver/hostio.c
gdb/gdbserver/hostio.h
gdb/gdbserver/linux-low.c
gdb/gdbserver/server.c
gdb/gdbserver/target.h

index cb21f4b989af6b670b23da157090e218e7511a08..5c3e44f720e8130099d6127fb82318e11d866c57 100644 (file)
@@ -1,3 +1,23 @@
+2015-06-10  Gary Benson  <gbenson@redhat.com>
+
+       * target.h (struct target_ops) <multifs_open>: New field.
+       <multifs_unlink>: Likewise.
+       <multifs_readlink>: Likewise.
+       * linux-low.c (nat/linux-namespaces.h): New include.
+       (linux_target_ops): Initialize the_target->multifs_open,
+       the_target->multifs_unlink and the_target->multifs_readlink.
+       * hostio.h (hostio_handle_new_gdb_connection): New declaration.
+       * hostio.c (hostio_fs_pid): New static variable.
+       (hostio_handle_new_gdb_connection): New function.
+       (handle_setfs): Likewise.
+       (handle_open): Use the_target->multifs_open as appropriate.
+       (handle_unlink): Use the_target->multifs_unlink as appropriate.
+       (handle_readlink): Use the_target->multifs_readlink as
+       appropriate.
+       (handle_vFile): Handle vFile:setfs packets.
+       * server.c (handle_query): Call hostio_handle_new_gdb_connection
+       after target_handle_new_gdb_connection.
+
 2015-06-10  Gary Benson  <gbenson@redhat.com>
 
        * configure.ac (AC_CHECK_FUNCS): Add setns.
index 811f95825b4d25d1f844f47cb13d110d6bc718c5..b38a6bd05649a79677f0b0f8cdd89967c5daab04 100644 (file)
@@ -243,6 +243,55 @@ hostio_reply_with_data (char *own_buf, char *buffer, int len,
   return input_index;
 }
 
+/* Process ID of inferior whose filesystem hostio functions
+   that take FILENAME arguments will use.  Zero means to use
+   our own filesystem.  */
+
+static int hostio_fs_pid;
+
+/* See hostio.h.  */
+
+void
+hostio_handle_new_gdb_connection (void)
+{
+  hostio_fs_pid = 0;
+}
+
+/* Handle a "vFile:setfs:" packet.  */
+
+static void
+handle_setfs (char *own_buf)
+{
+  char *p;
+  int pid;
+
+  /* If the target doesn't have any of the in-filesystem-of methods
+     then there's no point in GDB sending "vFile:setfs:" packets.  We
+     reply with an empty packet (i.e. we pretend we don't understand
+     "vFile:setfs:") and that should stop GDB sending any more.  */
+  if (the_target->multifs_open == NULL
+      && the_target->multifs_unlink == NULL
+      && the_target->multifs_readlink == NULL)
+    {
+      own_buf[0] = '\0';
+      return;
+    }
+
+  p = own_buf + strlen ("vFile:setfs:");
+
+  if (require_int (&p, &pid)
+      || pid < 0
+      || require_end (p))
+    {
+      hostio_packet_error (own_buf);
+      return;
+    }
+
+  hostio_fs_pid = pid;
+
+  hostio_reply (own_buf, 0);
+}
+
 static void
 handle_open (char *own_buf)
 {
@@ -269,7 +318,11 @@ handle_open (char *own_buf)
 
   /* We do not need to convert MODE, since the fileio protocol
      uses the standard values.  */
-  fd = open (filename, flags, mode);
+  if (hostio_fs_pid != 0 && the_target->multifs_open != NULL)
+    fd = the_target->multifs_open (hostio_fs_pid, filename,
+                                  flags, mode);
+  else
+    fd = open (filename, flags, mode);
 
   if (fd == -1)
     {
@@ -473,7 +526,10 @@ handle_unlink (char *own_buf)
       return;
     }
 
-  ret = unlink (filename);
+  if (hostio_fs_pid != 0 && the_target->multifs_unlink != NULL)
+    ret = the_target->multifs_unlink (hostio_fs_pid, filename);
+  else
+    ret = unlink (filename);
 
   if (ret == -1)
     {
@@ -500,7 +556,13 @@ handle_readlink (char *own_buf, int *new_packet_len)
       return;
     }
 
-  ret = readlink (filename, linkname, sizeof (linkname) - 1);
+  if (hostio_fs_pid != 0 && the_target->multifs_readlink != NULL)
+    ret = the_target->multifs_readlink (hostio_fs_pid, filename,
+                                       linkname,
+                                       sizeof (linkname) - 1);
+  else
+    ret = readlink (filename, linkname, sizeof (linkname) - 1);
+
   if (ret == -1)
     {
       hostio_error (own_buf);
@@ -534,6 +596,8 @@ handle_vFile (char *own_buf, int packet_len, int *new_packet_len)
     handle_unlink (own_buf);
   else if (startswith (own_buf, "vFile:readlink:"))
     handle_readlink (own_buf, new_packet_len);
+  else if (startswith (own_buf, "vFile:setfs:"))
+    handle_setfs (own_buf);
   else
     return 0;
 
index ce951f0c8a62dc66099761b01f31ed84408c74f6..1cb552c362ae02ce702cfe326ca591883742a4e5 100644 (file)
@@ -19,6 +19,9 @@
 #ifndef HOSTIO_H
 #define HOSTIO_H
 
+/* Per-connection setup.  */
+extern void hostio_handle_new_gdb_connection (void);
+
 /* Functions from hostio.c.  */
 extern int handle_vFile (char *, int, int *);
 
index d763c66e5b10ebc73936555071573bfb3961bcdd..3774d1700cd444f7be0b4c0636d7270aeb6e6f9d 100644 (file)
@@ -52,6 +52,7 @@
    definition of elf_fpregset_t.  */
 #include <elf.h>
 #endif
+#include "nat/linux-namespaces.h"
 
 #ifndef SPUFS_MAGIC
 #define SPUFS_MAGIC 0x23c9b64e
@@ -6644,6 +6645,9 @@ static struct target_ops linux_target_ops = {
 #endif
   linux_supports_range_stepping,
   linux_proc_pid_to_exec_file,
+  linux_mntns_open_cloexec,
+  linux_mntns_unlink,
+  linux_mntns_readlink,
 };
 
 static void
index 39692672134887ae5bf35ee7a551790ddb06bf43..70fbefbd65d23273a98d11cb637c215a5347493d 100644 (file)
@@ -2160,7 +2160,8 @@ handle_query (char *own_buf, int packet_len, int *new_packet_len_p)
       if (the_target->pid_to_exec_file != NULL)
        strcat (own_buf, ";qXfer:exec-file:read+");
 
-      /* Reinitialize the target as needed for the new connection.  */
+      /* Reinitialize components as needed for the new connection.  */
+      hostio_handle_new_gdb_connection ();
       target_handle_new_gdb_connection ();
 
       return;
index e9c6be06e64fab99e7f3bd1bd3fe1c4bbcde0b04..9a4086708cf3b15c1931c21d816403d38d4dabf0 100644 (file)
@@ -415,6 +415,27 @@ struct target_ops
      string should be copied into a buffer by the client if the string
      will not be immediately used, or if it must persist.  */
   char *(*pid_to_exec_file) (int pid);
+
+  /* Multiple-filesystem-aware open.  Like open(2), but operating in
+     the filesystem as it appears to process PID.  Systems where all
+     processes share a common filesystem should set this to NULL.
+     If NULL, the caller should fall back to open(2).  */
+  int (*multifs_open) (int pid, const char *filename,
+                      int flags, mode_t mode);
+
+  /* Multiple-filesystem-aware unlink.  Like unlink(2), but operates
+     in the filesystem as it appears to process PID.  Systems where
+     all processes share a common filesystem should set this to NULL.
+     If NULL, the caller should fall back to unlink(2).  */
+  int (*multifs_unlink) (int pid, const char *filename);
+
+  /* Multiple-filesystem-aware readlink.  Like readlink(2), but
+     operating in the filesystem as it appears to process PID.
+     Systems where all processes share a common filesystem should
+     set this to NULL.  If NULL, the caller should fall back to
+     readlink(2).  */
+  ssize_t (*multifs_readlink) (int pid, const char *filename,
+                              char *buf, size_t bufsiz);
 };
 
 extern struct target_ops *the_target;