4a80f0
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
4a80f0
From: Fedora GDB patches <invalid@email.com>
4a80f0
Date: Fri, 27 Oct 2017 21:07:50 +0200
4a80f0
Subject: gdb-6.3-mapping-zero-inode-test.patch
4a80f0
4a80f0
;; Test GCORE for shmid 0 shared memory mappings.
4a80f0
;;=fedoratest: But it is broken anyway, sometimes the case being tested is not reproducible.
4a80f0
4a80f0
diff --git a/gdb/testsuite/gdb.base/gcore-shmid0.c b/gdb/testsuite/gdb.base/gcore-shmid0.c
4a80f0
new file mode 100644
4a80f0
--- /dev/null
4a80f0
+++ b/gdb/testsuite/gdb.base/gcore-shmid0.c
4a80f0
@@ -0,0 +1,128 @@
4a80f0
+/* Copyright 2007, 2009 Free Software Foundation, Inc.
4a80f0
+
4a80f0
+   This file is part of GDB.
4a80f0
+
4a80f0
+   This program is free software; you can redistribute it and/or modify
4a80f0
+   it under the terms of the GNU General Public License as published by
4a80f0
+   the Free Software Foundation; either version 2 of the License, or (at
4a80f0
+   your option) any later version.
4a80f0
+
4a80f0
+   This program is distributed in the hope that it will be useful, but
4a80f0
+   WITHOUT ANY WARRANTY; without even the implied warranty of
4a80f0
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4a80f0
+   General Public License for more details.
4a80f0
+
4a80f0
+   You should have received a copy of the GNU General Public License
4a80f0
+   along with this program; if not, write to the Free Software
4a80f0
+   Foundation, Inc., 59 Temple Place - Suite 330,
4a80f0
+   Boston, MA 02111-1307, USA.  */
4a80f0
+
4a80f0
+/*
4a80f0
+ * Test GDB's handling of gcore for mapping with a name but zero inode.
4a80f0
+ */
4a80f0
+
4a80f0
+#include <sys/ipc.h>
4a80f0
+#include <sys/shm.h>
4a80f0
+#include <stdio.h>
4a80f0
+#include <errno.h>
4a80f0
+#include <stdlib.h>
4a80f0
+#include <unistd.h>
4a80f0
+#include <assert.h>
4a80f0
+#include <time.h>
4a80f0
+
4a80f0
+/* The same test running in a parallel testsuite may steal us the zero SID,
4a80f0
+   even if we never get any EEXIST.  Just try a while.  */
4a80f0
+
4a80f0
+#define TIMEOUT_SEC 10
4a80f0
+
4a80f0
+static volatile int v;
4a80f0
+
4a80f0
+static void
4a80f0
+initialized (void)
4a80f0
+{
4a80f0
+  v++;
4a80f0
+}
4a80f0
+
4a80f0
+static void
4a80f0
+unresolved (void)
4a80f0
+{
4a80f0
+  v++;
4a80f0
+}
4a80f0
+
4a80f0
+int
4a80f0
+main (void)
4a80f0
+{
4a80f0
+  int sid;
4a80f0
+  unsigned int *addr = (void *) -1L;
4a80f0
+  int attempt, round = 0;
4a80f0
+  time_t ts_start, ts;
4a80f0
+
4a80f0
+  if (time (&ts_start) == (time_t) -1)
4a80f0
+    {
4a80f0
+      printf ("time (): %m\n");
4a80f0
+      exit (1);
4a80f0
+    }
4a80f0
+
4a80f0
+  /* The generated SID will cycle with an increment of 32768, attempt until it
4a80f0
+   * wraps to 0.  */
4a80f0
+
4a80f0
+  for (attempt = 0; addr == (void *) -1L; attempt++)
4a80f0
+    {
4a80f0
+      /* kernel-2.6.25-8.fc9.x86_64 just never returns the value 0 by
4a80f0
+	 shmget(2).  shmget returns SID range 0..1<<31 in steps of 32768,
4a80f0
+	 0x1000 should be enough but wrap the range it to be sure.  */
4a80f0
+
4a80f0
+      if (attempt > 0x21000)
4a80f0
+        {
4a80f0
+	  if (time (&ts) == (time_t) -1)
4a80f0
+	    {
4a80f0
+	      printf ("time (): %m\n");
4a80f0
+	      exit (1);
4a80f0
+	    }
4a80f0
+
4a80f0
+	  if (ts >= ts_start && ts < ts_start + TIMEOUT_SEC)
4a80f0
+	    {
4a80f0
+	      attempt = 0;
4a80f0
+	      round++;
4a80f0
+	      continue;
4a80f0
+	    }
4a80f0
+
4a80f0
+	  printf ("Problem is not reproducible on this kernel (attempt %d, "
4a80f0
+		  "round %d)\n", attempt, round);
4a80f0
+	  unresolved ();
4a80f0
+	  exit (1);
4a80f0
+	}
4a80f0
+
4a80f0
+      sid = shmget ((key_t) rand (), 0x1000, IPC_CREAT | IPC_EXCL | 0777);
4a80f0
+      if (sid == -1)
4a80f0
+	{
4a80f0
+	  if (errno == EEXIST)
4a80f0
+	    continue;
4a80f0
+
4a80f0
+	  printf ("shmget (%d, 0x1000, IPC_CREAT): errno %d\n", 0, errno);
4a80f0
+	  exit (1);
4a80f0
+	}
4a80f0
+
4a80f0
+      /* Use SID only if it is 0, retry it otherwise.  */
4a80f0
+
4a80f0
+      if (sid == 0)
4a80f0
+	{
4a80f0
+	  addr = shmat (sid, NULL, SHM_RND);
4a80f0
+	  if (addr == (void *) -1L)
4a80f0
+	    {
4a80f0
+	      printf ("shmat (%d, NULL, SHM_RND): errno %d\n", sid,
4a80f0
+		      errno);
4a80f0
+	      exit (1);
4a80f0
+	    }
4a80f0
+	}
4a80f0
+      if (shmctl (sid, IPC_RMID, NULL) != 0)
4a80f0
+	{
4a80f0
+	  printf ("shmctl (%d, IPC_RMID, NULL): errno %d\n", sid, errno);
4a80f0
+	  exit (1);
4a80f0
+	}
4a80f0
+    }
4a80f0
+
4a80f0
+  initialized ();
4a80f0
+
4a80f0
+  return 0;
4a80f0
+}
4a80f0
diff --git a/gdb/testsuite/gdb.base/gcore-shmid0.exp b/gdb/testsuite/gdb.base/gcore-shmid0.exp
4a80f0
new file mode 100644
4a80f0
--- /dev/null
4a80f0
+++ b/gdb/testsuite/gdb.base/gcore-shmid0.exp
4a80f0
@@ -0,0 +1,101 @@
4a80f0
+# Copyright 2007, 2009 Free Software Foundation, Inc.
4a80f0
+
4a80f0
+# This program is free software; you can redistribute it and/or modify
4a80f0
+# it under the terms of the GNU General Public License as published by
4a80f0
+# the Free Software Foundation; either version 2 of the License, or
4a80f0
+# (at your option) any later version.
4a80f0
+#
4a80f0
+# This program is distributed in the hope that it will be useful,
4a80f0
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
4a80f0
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4a80f0
+# GNU General Public License for more details.
4a80f0
+#
4a80f0
+# You should have received a copy of the GNU General Public License
4a80f0
+# along with this program; if not, write to the Free Software
4a80f0
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
4a80f0
+
4a80f0
+# Test GDB's handling of gcore for mapping with a name but zero inode.
4a80f0
+
4a80f0
+if { [prepare_for_testing gcore-shmid0.exp gcore-shmid0] } {
4a80f0
+    return -1
4a80f0
+}
4a80f0
+
4a80f0
+# Does this gdb support gcore?
4a80f0
+set test "help gcore"
4a80f0
+gdb_test_multiple $test $test {
4a80f0
+    -re "Undefined command: .gcore.*$gdb_prompt $" {
4a80f0
+	# gcore command not supported -- nothing to test here.
4a80f0
+	unsupported "gdb does not support gcore on this target"
4a80f0
+	return -1;
4a80f0
+    }
4a80f0
+    -re "Save a core file .*$gdb_prompt $" {
4a80f0
+	pass $test
4a80f0
+    }
4a80f0
+}
4a80f0
+
4a80f0
+if { ! [ runto_main ] } then {
4a80f0
+    untested gcore-shmid0.exp
4a80f0
+    return -1
4a80f0
+}
4a80f0
+
4a80f0
+gdb_breakpoint "initialized"
4a80f0
+gdb_breakpoint "unresolved"
4a80f0
+
4a80f0
+set oldtimeout $timeout
4a80f0
+set timeout [expr $oldtimeout + 120]
4a80f0
+
4a80f0
+set test "Continue to initialized."
4a80f0
+gdb_test_multiple "continue" $test {
4a80f0
+    -re "Breakpoint .*, initialized .* at .*\r\n$gdb_prompt $" {
4a80f0
+	pass $test
4a80f0
+    }
4a80f0
+    -re "Breakpoint .*, unresolved .* at .*\r\n$gdb_prompt $" {
4a80f0
+	set timeout $oldtimeout
4a80f0
+	unsupported $test
4a80f0
+	return -1
4a80f0
+    }
4a80f0
+}
4a80f0
+set timeout $oldtimeout
4a80f0
+
4a80f0
+set escapedfilename [string_to_regexp [standard_output_file gcore-shmid0.test]]
4a80f0
+
4a80f0
+set test "save a corefile"
4a80f0
+gdb_test_multiple "gcore [standard_output_file gcore-shmid0.test]" $test {
4a80f0
+    -re "Saved corefile ${escapedfilename}\[\r\n\]+$gdb_prompt $" {
4a80f0
+	pass $test
4a80f0
+    }
4a80f0
+    -re "Can't create a corefile\[\r\n\]+$gdb_prompt $" {
4a80f0
+	unsupported $test
4a80f0
+    }
4a80f0
+}
4a80f0
+
4a80f0
+# Be sure to remove the handle first.
4a80f0
+# But it would get removed even on a kill by GDB as the handle is already
4a80f0
+# deleted, just it is still attached.
4a80f0
+gdb_continue_to_end "finish"
4a80f0
+
4a80f0
+set test "core-file command"
4a80f0
+gdb_test_multiple "core-file [standard_output_file gcore-shmid0.test]" $test {
4a80f0
+    -re ".* program is being debugged already.*y or n. $" {
4a80f0
+	# gdb_load may connect us to a gdbserver.
4a80f0
+	send_gdb "y\n"
4a80f0
+	exp_continue;
4a80f0
+    }
4a80f0
+    -re "Core was generated by .*\r\n\#0  .*\\\(\\\).*\r\n$gdb_prompt $" {
4a80f0
+	# The filename does not fit there anyway so do not check it.
4a80f0
+	pass $test
4a80f0
+    }
4a80f0
+    -re ".*registers from core file: File in wrong format.* $" {
4a80f0
+	fail "core-file command (could not read registers from core file)"
4a80f0
+    }
4a80f0
+}
4a80f0
+
4a80f0
+set test "backtrace"
4a80f0
+gdb_test_multiple "bt" $test {
4a80f0
+    -re "#0 *initialized \\\(\\\) at .*#1 .* main \\\(.*$gdb_prompt $" {
4a80f0
+	pass $test
4a80f0
+    }
4a80f0
+    -re "#0 *initialized \\\(\\\) at .*Cannot access memory at address .*$gdb_prompt $" {
4a80f0
+	fail $test
4a80f0
+    }
4a80f0
+}