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