b2d009a
2007-04-22  Jan Kratochvil <jan.kratochvil@redhat.com>
b2d009a
b2d009a
	* gdb_gcore.sh: Redirect GDB from `
b2d009a
b2d009a
2007-04-22  Jan Kratochvil <jan.kratochvil@redhat.com>
b2d009a
b2d009a
	* gdb.base/gcorebg.exp, gdb.base/gcorebg.c: New files.
b2d009a
b2d009a
b2d009a
--- gdb-6.6-orig/gdb/gdb_gcore.sh	2005-12-17 17:33:59.000000000 -0500
b2d009a
+++ gdb-6.6/gdb/gdb_gcore.sh	2007-04-22 05:42:50.000000000 -0400
b2d009a
@@ -71,7 +71,9 @@
b2d009a
 quit
b2d009a
 EOF
b2d009a
 
b2d009a
-	gdb -x $tmpfile -batch
b2d009a
+	# `
b2d009a
+	# available but not accessible (SIGTTIN risk)
b2d009a
+	gdb -x $tmpfile -batch 
b2d009a
 
b2d009a
 	if [ -r $name.$pid ] ; then 
b2d009a
 	    rc=0
b2d009a
--- /dev/null	1 Jan 1970 00:00:00 -0000
b2d009a
+++ ./gdb/testsuite/gdb.base/gcorebg.c	25 Feb 2007 12:21:20 -0000
b2d009a
@@ -0,0 +1,43 @@
b2d009a
+#include <stdio.h>
b2d009a
+#include <sys/types.h>
b2d009a
+#include <unistd.h>
b2d009a
+#include <stdlib.h>
b2d009a
+#include <signal.h>
b2d009a
+
b2d009a
+int main (int argc, char **argv)
b2d009a
+{
b2d009a
+  pid_t pid = 0;
b2d009a
+  pid_t ppid;
b2d009a
+  char buf[256];
b2d009a
+
b2d009a
+  if (argc != 4)
b2d009a
+    {
b2d009a
+      fprintf (stderr, "Syntax: %s {standard|detached} <gcore command> <core output file>\n",
b2d009a
+	       argv[0]);
b2d009a
+      exit (1);
b2d009a
+    }
b2d009a
+
b2d009a
+  pid = fork ();
b2d009a
+
b2d009a
+  switch (pid)
b2d009a
+    {
b2d009a
+      case 0:
b2d009a
+        if (strcmp (argv[1], "detached") == 0)
b2d009a
+	  setpgrp ();
b2d009a
+	ppid = getppid ();
b2d009a
+	sprintf (buf, "sh %s -o %s %d", argv[2], argv[3], (int) ppid);
b2d009a
+	system (buf);
b2d009a
+	kill (ppid, SIGTERM);
b2d009a
+	break;
b2d009a
+
b2d009a
+      case -1:
b2d009a
+	perror ("fork err\n");
b2d009a
+	exit (1);
b2d009a
+	break;
b2d009a
+
b2d009a
+      default:
b2d009a
+	sleep (60);
b2d009a
+    }
b2d009a
+
b2d009a
+  return 0;
b2d009a
+}
b2d009a
--- /dev/null	1 Jan 1970 00:00:00 -0000
b2d009a
+++ ./gdb/testsuite/gdb.base/gcorebg.exp	25 Feb 2007 12:21:20 -0000
dd46ae6
@@ -0,0 +1,113 @@
b2d009a
+# Copyright 2007 Free Software Foundation, Inc.
b2d009a
+
b2d009a
+# This program is free software; you can redistribute it and/or modify
b2d009a
+# it under the terms of the GNU General Public License as published by
b2d009a
+# the Free Software Foundation; either version 2 of the License, or
b2d009a
+# (at your option) any later version.
b2d009a
+# 
b2d009a
+# This program is distributed in the hope that it will be useful,
b2d009a
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
b2d009a
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b2d009a
+# GNU General Public License for more details.
b2d009a
+# 
b2d009a
+# You should have received a copy of the GNU General Public License
b2d009a
+# along with this program; if not, write to the Free Software
b2d009a
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
b2d009a
+
b2d009a
+# Please email any bugs, comments, and/or additions to this file to:
b2d009a
+# bug-gdb@prep.ai.mit.edu
b2d009a
+
b2d009a
+# This file was written by Jan Kratochvil <jan.kratochvil@redhat.com>.
b2d009a
+# This is a test for `gdb_gcore.sh' functionality.
b2d009a
+# It also tests a regression with `gdb_gcore.sh' being run without its
b2d009a
+# accessible terminal.
b2d009a
+
b2d009a
+if ![info exists GCORE] {
b2d009a
+    set GCORE "${srcdir}/../gdb_gcore.sh"
b2d009a
+}
b2d009a
+verbose "using GCORE = $GCORE" 2
b2d009a
+
b2d009a
+set testfile "gcorebg"
b2d009a
+set srcfile  ${testfile}.c
b2d009a
+set binfile  ${objdir}/${subdir}/${testfile}
b2d009a
+set corefile ${objdir}/${subdir}/${testfile}.test
b2d009a
+
b2d009a
+if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b2d009a
+     untested gcorebg.exp
b2d009a
+     return -1
b2d009a
+}
b2d009a
+
b2d009a
+# Cleanup.
b2d009a
+
b2d009a
+proc core_clean {} {
b2d009a
+    global corefile
b2d009a
+
b2d009a
+    foreach file [glob -nocomplain [join [list $corefile *] ""]] {
b2d009a
+	verbose "Delete file $file" 1
b2d009a
+	remote_file target delete $file
b2d009a
+    }
b2d009a
+}
b2d009a
+core_clean
b2d009a
+remote_file target delete "./gdb"
b2d009a
+
b2d009a
+# Generate the core file.
b2d009a
+
b2d009a
+# Provide `./gdb' for `gdb_gcore.sh' running it as a bare `gdb' command.
b2d009a
+# Setup also `$PATH' appropriately.
b2d009a
+# If GDB was not found let `gdb_gcore.sh' to find the system GDB by `$PATH'.
b2d009a
+if {$GDB != "gdb"} {
b2d009a
+    file link ./gdb $GDB
b2d009a
+}
b2d009a
+global env
b2d009a
+set oldpath $env(PATH)
b2d009a
+set env(PATH) [join [list . $env(PATH)] ":"]
b2d009a
+verbose "PATH = $env(PATH)" 2
b2d009a
+
b2d009a
+# Test file body.
b2d009a
+# $detached == "standard" || $detached == "detached"
b2d009a
+
b2d009a
+proc test_body { detached } {
b2d009a
+    global binfile
b2d009a
+    global GCORE
b2d009a
+    global corefile
b2d009a
+
b2d009a
+    set res [remote_spawn target "$binfile $detached $GCORE $corefile"]
b2d009a
+    if { $res < 0 || $res == "" } {
b2d009a
+	fail "Spawning $detached gcore"
b2d009a
+	return 1
b2d009a
+    }
b2d009a
+    pass "Spawning $detached gcore"
b2d009a
+    remote_expect target 20 {
b2d009a
+	timeout {
b2d009a
+	    fail "Spawned $detached gcore finished"
b2d009a
+	    remote_exec target "kill -9 -[exp_pid -i $res]"
b2d009a
+	    return 1
b2d009a
+	}
b2d009a
+	eof {
b2d009a
+	    pass "Spawned $detached gcore finished"
b2d009a
+	    remote_wait target 20
b2d009a
+	}
b2d009a
+    }
b2d009a
+
b2d009a
+    if {1 == [llength [glob -nocomplain [join [list $corefile *] ""]]]} {
b2d009a
+	pass "Core file generated by $detached gcore"
b2d009a
+    } else {
b2d009a
+	fail "Core file generated by $detached gcore"
b2d009a
+    }
b2d009a
+    core_clean
b2d009a
+}
b2d009a
+
b2d009a
+# First a general `gdb_gcore.sh' spawn with its controlling terminal available.
b2d009a
+
b2d009a
+test_body standard
b2d009a
+
b2d009a
+# And now `gdb_gcore.sh' spawn without its controlling terminal available.
b2d009a
+# It is spawned through `gcorebg.c' using setpgrp ().
b2d009a
+
b2d009a
+test_body detached
b2d009a
+
b2d009a
+
b2d009a
+# Cleanup.
b2d009a
+
b2d009a
+set env(PATH) $oldpath
b2d009a
+remote_file target delete "./gdb"