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

0b42f8
commit 015de6884f6fdebaffd4b7d4c7f14fb4d5fc0bb1
0b42f8
Author: Daniel Colascione <dancol@dancol.org>
0b42f8
Date:   Tue Nov 11 14:18:23 2014 +0000
0b42f8
0b42f8
    Warn users about mismatched PID namespaces
0b42f8
    
0b42f8
    Linux supports multiple "PID namespaces".  Processes in different PID
0b42f8
    namespaces have different views of the system process list.  Sometimes,
0b42f8
    a single process can appear in more than one PID namespace, but with a
0b42f8
    different PID in each.  When GDB and its target are in different PID
0b42f8
    namespaces, various features can break due to the mismatch between
0b42f8
    what the target believes its PID to be and what GDB believes its PID
0b42f8
    to be.  The most visible broken functionality is thread enumeration
0b42f8
    silently failing.
0b42f8
    
0b42f8
    This patch explicitly warns users against trying to debug across PID
0b42f8
    namespaces.
0b42f8
    
0b42f8
    The patch introduced no new failures in my test suite run on an x86_64
0b42f8
    installation of Ubuntu 14.10.  It doesn't include a test: writing an
0b42f8
    automated test that exercises this code would be very involved because
0b42f8
    CLONE_NEWNS requires CAP_SYS_ADMIN; the easier way to reproduce the
0b42f8
    problem is to start a new lxc container.
0b42f8
    
0b42f8
    gdb/
0b42f8
    2014-11-11  Daniel Colascione  <dancol@dancol.org>
0b42f8
    
0b42f8
    	Warn about cross-PID-namespace debugging.
0b42f8
    	* nat/linux-procfs.h (linux_proc_pid_get_ns): New prototype.
0b42f8
    	* nat/linux-procfs.c (linux_proc_pid_get_ns): New function.
0b42f8
    	* linux-thread-db.c (check_pid_namespace_match): New function.
0b42f8
    	(thread_db_inferior_created): Call it.
0b42f8
0b42f8
### a/gdb/ChangeLog
0b42f8
### b/gdb/ChangeLog
0b42f8
## -1,3 +1,11 @@
0b42f8
+2014-11-11  Daniel Colascione  <dancol@dancol.org>
0b42f8
+
0b42f8
+	Warn about cross-PID-namespace debugging.
0b42f8
+	* nat/linux-procfs.h (linux_proc_pid_get_ns): New prototype.
0b42f8
+	* nat/linux-procfs.c (linux_proc_pid_get_ns): New function.
0b42f8
+	* linux-thread-db.c (check_pid_namespace_match): New function.
0b42f8
+	(thread_db_inferior_created): Call it.
0b42f8
+
0b42f8
 2014-11-10  Doug Evans  <xdje42@gmail.com>
0b42f8
 
0b42f8
 	* symmisc.c (print_objfile_statistics): Remove trailing whitespace.
0b42f8
Index: gdb-7.6.1/gdb/linux-thread-db.c
0b42f8
===================================================================
0b42f8
--- gdb-7.6.1.orig/gdb/linux-thread-db.c	2016-03-17 20:43:34.001735230 +0100
0b42f8
+++ gdb-7.6.1/gdb/linux-thread-db.c	2016-03-17 21:54:32.233661083 +0100
0b42f8
@@ -1216,12 +1216,70 @@
0b42f8
     check_for_thread_db ();
0b42f8
 }
0b42f8
 
0b42f8
+// RHEL: gdb/nat/linux-procfs.h
0b42f8
+
0b42f8
+/* Return an opaque string identifying PID's NS namespace or NULL if
0b42f8
+ * the information is unavailable.  The returned string must be
0b42f8
+ * released with xfree.  */
0b42f8
+
0b42f8
+extern char *linux_proc_pid_get_ns (pid_t pid, const char *ns);
0b42f8
+
0b42f8
+// RHEL: gdb/nat/linux-procfs.c
0b42f8
+
0b42f8
+/* See linux-procfs.h declaration.  */
0b42f8
+
0b42f8
+char *
0b42f8
+linux_proc_pid_get_ns (pid_t pid, const char *ns)
0b42f8
+{
0b42f8
+  char buf[100];
0b42f8
+  char nsval[64];
0b42f8
+  int ret;
0b42f8
+  xsnprintf (buf, sizeof (buf), "/proc/%d/ns/%s", (int) pid, ns);
0b42f8
+  ret = readlink (buf, nsval, sizeof (nsval));
0b42f8
+  if (0 < ret && ret < sizeof (nsval))
0b42f8
+    {
0b42f8
+      nsval[ret] = '\0';
0b42f8
+      return xstrdup (nsval);
0b42f8
+    }
0b42f8
+
0b42f8
+  return NULL;
0b42f8
+}
0b42f8
+
0b42f8
+static void
0b42f8
+check_pid_namespace_match (void)
0b42f8
+{
0b42f8
+  /* Check is only relevant for local targets targets.  */
0b42f8
+  if (target_can_run (&current_target))
0b42f8
+    {
0b42f8
+      /* If the child is in a different PID namespace, its idea of its
0b42f8
+	 PID will differ from our idea of its PID.  When we scan the
0b42f8
+	 child's thread list, we'll mistakenly think it has no threads
0b42f8
+	 since the thread PID fields won't match the PID we give to
0b42f8
+	 libthread_db.  */
0b42f8
+      char *our_pid_ns = linux_proc_pid_get_ns (getpid (), "pid");
0b42f8
+      char *inferior_pid_ns = linux_proc_pid_get_ns (
0b42f8
+	ptid_get_pid (inferior_ptid), "pid");
0b42f8
+
0b42f8
+      if (our_pid_ns != NULL && inferior_pid_ns != NULL
0b42f8
+	  && strcmp (our_pid_ns, inferior_pid_ns) != 0)
0b42f8
+	{
0b42f8
+	  warning (_ ("Target and debugger are in different PID "
0b42f8
+		      "namespaces; thread lists and other data are "
0b42f8
+		      "likely unreliable"));
0b42f8
+	}
0b42f8
+
0b42f8
+      xfree (our_pid_ns);
0b42f8
+      xfree (inferior_pid_ns);
0b42f8
+    }
0b42f8
+}
0b42f8
+
0b42f8
 /* This function is called via the inferior_created observer.
0b42f8
    This handles the case of debugging statically linked executables.  */
0b42f8
 
0b42f8
 static void
0b42f8
 thread_db_inferior_created (struct target_ops *target, int from_tty)
0b42f8
 {
0b42f8
+  check_pid_namespace_match ();
0b42f8
   check_for_thread_db ();
0b42f8
 }
0b42f8