Blame SOURCES/gdb-rhbz1186918-gdbserver-in-container-5of8.patch

01917d
Implement vFile:setfs in gdbserver
01917d
01917d
From: Gary Benson <gbenson@redhat.com>
01917d
01917d
This commit implements the "vFile:setfs" packet in gdbserver.
01917d
01917d
gdb/gdbserver/ChangeLog:
01917d
01917d
	* target.h (struct target_ops) <multifs_open>: New field.
01917d
	<multifs_unlink>: Likewise.
01917d
	<multifs_readlink>: Likewise.
01917d
	* linux-low.c (nat/linux-namespaces.h): New include.
01917d
	(linux_target_ops): Initialize the_target->multifs_open,
01917d
	the_target->multifs_unlink and the_target->multifs_readlink.
01917d
	* hostio.h (hostio_handle_new_gdb_connection): New declaration.
01917d
	* hostio.c (hostio_fs_pid): New static variable.
01917d
	(hostio_handle_new_gdb_connection): New function.
01917d
	(handle_setfs): Likewise.
01917d
	(handle_open): Use the_target->multifs_open as appropriate.
01917d
	(handle_unlink): Use the_target->multifs_unlink as appropriate.
01917d
	(handle_readlink): Use the_target->multifs_readlink as
01917d
	appropriate.
01917d
	(handle_vFile): Handle vFile:setfs packets.
01917d
	* server.c (handle_query): Call hostio_handle_new_gdb_connection
01917d
	after target_handle_new_gdb_connection.
01917d
---
01917d
 gdb/gdbserver/ChangeLog   |   20 +++++++++++++
01917d
 gdb/gdbserver/hostio.c    |   70 +++++++++++++++++++++++++++++++++++++++++++--
01917d
 gdb/gdbserver/linux-low.c |    4 +++
01917d
 gdb/gdbserver/server.c    |    2 +
01917d
 gdb/gdbserver/target.h    |   21 ++++++++++++++
01917d
 5 files changed, 114 insertions(+), 3 deletions(-)
01917d
01917d
Index: gdb-7.6.1/gdb/gdbserver/hostio.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/hostio.c	2013-01-01 07:33:00.000000000 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/hostio.c	2016-03-06 23:38:57.315483217 +0100
01917d
@@ -263,6 +263,55 @@
01917d
   return 0;
01917d
 }
01917d
 
01917d
+/* Process ID of inferior whose filesystem hostio functions
01917d
+   that take FILENAME arguments will use.  Zero means to use
01917d
+   our own filesystem.  */
01917d
+
01917d
+static int hostio_fs_pid;
01917d
+
01917d
+/* See hostio.h.  */
01917d
+
01917d
+void
01917d
+hostio_handle_new_gdb_connection (void)
01917d
+{
01917d
+  hostio_fs_pid = 0;
01917d
+}
01917d
+
01917d
+/* Handle a "vFile:setfs:" packet.  */
01917d
+
01917d
+static void
01917d
+handle_setfs (char *own_buf)
01917d
+{
01917d
+  char *p;
01917d
+  int pid;
01917d
+
01917d
+  /* If the target doesn't have any of the in-filesystem-of methods
01917d
+     then there's no point in GDB sending "vFile:setfs:" packets.  We
01917d
+     reply with an empty packet (i.e. we pretend we don't understand
01917d
+     "vFile:setfs:") and that should stop GDB sending any more.  */
01917d
+  if (the_target->multifs_open == NULL
01917d
+      && the_target->multifs_unlink == NULL
01917d
+      && the_target->multifs_readlink == NULL)
01917d
+    {
01917d
+      own_buf[0] = '\0';
01917d
+      return;
01917d
+    }
01917d
+
01917d
+  p = own_buf + strlen ("vFile:setfs:");
01917d
+
01917d
+  if (require_int (&p, &pid)
01917d
+      || pid < 0
01917d
+      || require_end (p))
01917d
+    {
01917d
+      hostio_packet_error (own_buf);
01917d
+      return;
01917d
+    }
01917d
+
01917d
+  hostio_fs_pid = pid;
01917d
+
01917d
+  hostio_reply (own_buf, 0);
01917d
+}
01917d
+
01917d
 static void
01917d
 handle_open (char *own_buf)
01917d
 {
01917d
@@ -287,7 +336,11 @@
01917d
 
01917d
   /* We do not need to convert MODE, since the fileio protocol
01917d
      uses the standard values.  */
01917d
-  fd = open (filename, flags, mode);
01917d
+  if (hostio_fs_pid != 0 && the_target->multifs_open != NULL)
01917d
+    fd = the_target->multifs_open (hostio_fs_pid, filename,
01917d
+				   flags, mode);
01917d
+  else
01917d
+    fd = open (filename, flags, mode);
01917d
 
01917d
   if (fd == -1)
01917d
     {
01917d
@@ -455,7 +508,10 @@
01917d
       return;
01917d
     }
01917d
 
01917d
-  ret = unlink (filename);
01917d
+  if (hostio_fs_pid != 0 && the_target->multifs_unlink != NULL)
01917d
+    ret = the_target->multifs_unlink (hostio_fs_pid, filename);
01917d
+  else
01917d
+    ret = unlink (filename);
01917d
 
01917d
   if (ret == -1)
01917d
     {
01917d
@@ -483,7 +539,13 @@
01917d
       return;
01917d
     }
01917d
 
01917d
-  ret = readlink (filename, linkname, sizeof (linkname) - 1);
01917d
+  if (hostio_fs_pid != 0 && the_target->multifs_readlink != NULL)
01917d
+    ret = the_target->multifs_readlink (hostio_fs_pid, filename,
01917d
+					linkname,
01917d
+					sizeof (linkname) - 1);
01917d
+  else
01917d
+    ret = readlink (filename, linkname, sizeof (linkname) - 1);
01917d
+
01917d
   if (ret == -1)
01917d
     {
01917d
       hostio_error (own_buf);
01917d
@@ -518,6 +580,8 @@
01917d
     handle_unlink (own_buf);
01917d
   else if (strncmp (own_buf, "vFile:readlink:", 15) == 0)
01917d
     handle_readlink (own_buf, new_packet_len);
01917d
+  else if (strncmp (own_buf, "vFile:setfs:", 12) == 0)
01917d
+    handle_setfs (own_buf);
01917d
   else
01917d
     return 0;
01917d
 
01917d
Index: gdb-7.6.1/gdb/gdbserver/linux-low.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/linux-low.c	2016-03-06 23:38:57.259482835 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/linux-low.c	2016-03-06 23:38:57.317483230 +0100
01917d
@@ -50,6 +50,7 @@
01917d
    definition of elf_fpregset_t.  */
01917d
 #include <elf.h>
01917d
 #endif
01917d
+#include "../nat/linux-namespaces.h"
01917d
 
01917d
 #ifndef SPUFS_MAGIC
01917d
 #define SPUFS_MAGIC 0x23c9b64e
01917d
@@ -6019,6 +6020,9 @@
01917d
   NULL,
01917d
 #endif
01917d
   linux_proc_pid_to_exec_file,
01917d
+  linux_mntns_open_cloexec,
01917d
+  linux_mntns_unlink,
01917d
+  linux_mntns_readlink,
01917d
 };
01917d
 
01917d
 static void
01917d
Index: gdb-7.6.1/gdb/gdbserver/server.c
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/server.c	2016-03-06 23:38:57.278482965 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/server.c	2016-03-06 23:41:53.678684044 +0100
01917d
@@ -1873,6 +1873,11 @@
01917d
       if (the_target->pid_to_exec_file != NULL)
01917d
 	strcat (own_buf, ";qXfer:exec-file:read+");
01917d
 
01917d
+      /* Reinitialize components as needed for the new connection.  */
01917d
+{
01917d
+extern void hostio_handle_new_gdb_connection (void);
01917d
+      hostio_handle_new_gdb_connection ();
01917d
+}
01917d
       return;
01917d
     }
01917d
 
01917d
Index: gdb-7.6.1/gdb/gdbserver/target.h
01917d
===================================================================
01917d
--- gdb-7.6.1.orig/gdb/gdbserver/target.h	2016-03-06 23:38:57.261482849 +0100
01917d
+++ gdb-7.6.1/gdb/gdbserver/target.h	2016-03-06 23:38:57.318483237 +0100
01917d
@@ -421,6 +421,27 @@
01917d
      string should be copied into a buffer by the client if the string
01917d
      will not be immediately used, or if it must persist.  */
01917d
   char *(*pid_to_exec_file) (int pid);
01917d
+
01917d
+  /* Multiple-filesystem-aware open.  Like open(2), but operating in
01917d
+     the filesystem as it appears to process PID.  Systems where all
01917d
+     processes share a common filesystem should set this to NULL.
01917d
+     If NULL, the caller should fall back to open(2).  */
01917d
+  int (*multifs_open) (int pid, const char *filename,
01917d
+		       int flags, mode_t mode);
01917d
+
01917d
+  /* Multiple-filesystem-aware unlink.  Like unlink(2), but operates
01917d
+     in the filesystem as it appears to process PID.  Systems where
01917d
+     all processes share a common filesystem should set this to NULL.
01917d
+     If NULL, the caller should fall back to unlink(2).  */
01917d
+  int (*multifs_unlink) (int pid, const char *filename);
01917d
+
01917d
+  /* Multiple-filesystem-aware readlink.  Like readlink(2), but
01917d
+     operating in the filesystem as it appears to process PID.
01917d
+     Systems where all processes share a common filesystem should
01917d
+     set this to NULL.  If NULL, the caller should fall back to
01917d
+     readlink(2).  */
01917d
+  ssize_t (*multifs_readlink) (int pid, const char *filename,
01917d
+			       char *buf, size_t bufsiz);
01917d
 };
01917d
 
01917d
 extern struct target_ops *the_target;