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

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