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