Blame SOURCES/gdb-6.5-BEA-testsuite.patch

190f2a
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
190f2a
From: Fedora GDB patches <invalid@email.com>
190f2a
Date: Fri, 27 Oct 2017 21:07:50 +0200
190f2a
Subject: gdb-6.5-BEA-testsuite.patch
190f2a
190f2a
;; Improved testsuite results by the testsuite provided by the courtesy of BEA.
190f2a
;;=fedoratest: For upstream it should be rewritten as a dejagnu test, the test of no "??" was useful.
190f2a
190f2a
diff --git a/gdb/testsuite/gdb.threads/threadcrash.c b/gdb/testsuite/gdb.threads/threadcrash.c
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/testsuite/gdb.threads/threadcrash.c
190f2a
@@ -0,0 +1,301 @@
190f2a
+/*
190f2a
+ * The point of this program is to crash in a multi-threaded app.
190f2a
+ * There are seven threads, doing the following things:
190f2a
+ * * Spinning
190f2a
+ * * Spinning inside a signal handler
190f2a
+ * * Spinning inside a signal handler executing on the altstack
190f2a
+ * * In a syscall
190f2a
+ * * In a syscall inside a signal handler
190f2a
+ * * In a syscall inside a signal handler executing on the altstack
190f2a
+ * * Finally, the main thread crashes in main, with no frills.
190f2a
+ *
190f2a
+ * These are the things threads in JRockit tend to be doing.  If gdb
190f2a
+ * can handle those things, both in core files and during live
190f2a
+ * debugging, that will help (at least) JRockit development.
190f2a
+ *
190f2a
+ * Let the program create a core file, then load the core file into
190f2a
+ * gdb.  Inside gdb, you should be able to do something like this:
190f2a
+ *
190f2a
+ * (gdb) t a a bt
190f2a
+ * 
190f2a
+ * Thread 7 (process 4352):
190f2a
+ * #0  0x001ba7dc in __nanosleep_nocancel () from /lib/tls/libc.so.6
190f2a
+ * #1  0x001ba5ff in sleep () from /lib/tls/libc.so.6
190f2a
+ * #2  0x080488a2 in makeSyscall (ignored=0x0) at threadcrash.c:118
190f2a
+ * #3  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #4  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 6 (process 4353):
190f2a
+ * #0  0x001ba7dc in __nanosleep_nocancel () from /lib/tls/libc.so.6
190f2a
+ * #1  0x001ba5ff in sleep () from /lib/tls/libc.so.6
190f2a
+ * #2  0x0804898f in syscallingSighandler (signo=10, info=0xb6be76f0, context=0xb6be7770)
190f2a
+ *     at threadcrash.c:168
190f2a
+ * #3  <signal handler called>
190f2a
+ * #4  0x006adf5e in pthread_kill () from /lib/tls/libpthread.so.0
190f2a
+ * #5  0x08048a51 in makeSyscallFromSighandler (ignored=0x0) at threadcrash.c:204
190f2a
+ * #6  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #7  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 5 (process 4354):
190f2a
+ * #0  0x001ba7dc in __nanosleep_nocancel () from /lib/tls/libc.so.6
190f2a
+ * #1  0x001ba5ff in sleep () from /lib/tls/libc.so.6
190f2a
+ * #2  0x08048936 in syscallingAltSighandler (signo=3, info=0x959cd70, context=0x959cdf0)
190f2a
+ *     at threadcrash.c:144
190f2a
+ * #3  <signal handler called>
190f2a
+ * #4  0x006adf5e in pthread_kill () from /lib/tls/libpthread.so.0
190f2a
+ * #5  0x080489e2 in makeSyscallFromAltSighandler (ignored=0x0) at threadcrash.c:190
190f2a
+ * #6  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #7  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 4 (process 4355):
190f2a
+ * #0  spin (ignored=0x0) at threadcrash.c:242
190f2a
+ * #1  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #2  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 3 (process 4356):
190f2a
+ * #0  spinningSighandler (signo=12, info=0xb4de46f0, context=0xb4de4770) at threadcrash.c:180
190f2a
+ * #1  <signal handler called>
190f2a
+ * #2  0x006adf5e in pthread_kill () from /lib/tls/libpthread.so.0
190f2a
+ * #3  0x08048b2f in spinFromSighandler (ignored=0x0) at threadcrash.c:232
190f2a
+ * #4  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #5  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 2 (process 4357):
190f2a
+ * #0  spinningAltSighandler (signo=14, info=0x959ee50, context=0x959eed0) at threadcrash.c:156
190f2a
+ * #1  <signal handler called>
190f2a
+ * #2  0x006adf5e in pthread_kill () from /lib/tls/libpthread.so.0
190f2a
+ * #3  0x08048ac0 in spinFromAltSighandler (ignored=0x0) at threadcrash.c:218
190f2a
+ * #4  0x006aadec in start_thread () from /lib/tls/libpthread.so.0
190f2a
+ * #5  0x001ed19a in clone () from /lib/tls/libc.so.6
190f2a
+ * 
190f2a
+ * Thread 1 (process 4351):
190f2a
+ * #0  0x08048cf3 in main (argc=1, argv=0xbfff9d74) at threadcrash.c:273
190f2a
+ * (gdb)
190f2a
+ */
190f2a
+
190f2a
+#include <pthread.h>
190f2a
+#include <signal.h>
190f2a
+#include <assert.h>
190f2a
+#include <unistd.h>
190f2a
+#include <stdio.h>
190f2a
+#include <stdlib.h>
190f2a
+#include <string.h>
190f2a
+
190f2a
+#define SIGSYSCALL_ALT SIGQUIT
190f2a
+#define SIGSYSCALL SIGUSR1
190f2a
+#define SIGSPIN_ALT SIGALRM
190f2a
+#define SIGSPIN SIGUSR2
190f2a
+
190f2a
+typedef void (*sigaction_t)(int, siginfo_t *, void *);
190f2a
+
190f2a
+static void installHandler(int signo, sigaction_t handler, int onAltstack) {
190f2a
+   struct sigaction action;
190f2a
+   sigset_t sigset;
190f2a
+   int result;
190f2a
+   stack_t altstack;
190f2a
+   stack_t oldaltstack;
190f2a
+   
190f2a
+   memset(&action, 0, sizeof(action));
190f2a
+   memset(&altstack, 0, sizeof(altstack));
190f2a
+   memset(&oldaltstack, 0, sizeof(oldaltstack));
190f2a
+   
190f2a
+   if (onAltstack) {
190f2a
+      altstack.ss_sp = malloc(SIGSTKSZ);
190f2a
+      assert(altstack.ss_sp != NULL);
190f2a
+      altstack.ss_size = SIGSTKSZ;
190f2a
+      altstack.ss_flags = 0;
190f2a
+      result = sigaltstack(&altstack, &oldaltstack);
190f2a
+      assert(result == 0);
190f2a
+      assert(oldaltstack.ss_flags == SS_DISABLE);
190f2a
+   }
190f2a
+   
190f2a
+   sigemptyset(&sigset);
190f2a
+   
190f2a
+   action.sa_handler = NULL;
190f2a
+   action.sa_sigaction = handler;
190f2a
+   action.sa_mask = sigset;
190f2a
+   action.sa_flags = SA_SIGINFO;
190f2a
+   if (onAltstack) {
190f2a
+      action.sa_flags |= SA_ONSTACK;
190f2a
+   }
190f2a
+   
190f2a
+   result = sigaction(signo, &action, NULL);
190f2a
+   assert(result == 0);
190f2a
+}
190f2a
+
190f2a
+static void installNormalHandler(int signo, sigaction_t handler) {
190f2a
+   installHandler(signo, handler, 0);
190f2a
+}
190f2a
+
190f2a
+static void installAlthandler(int signo, sigaction_t handler) {
190f2a
+   installHandler(signo, handler, 1);
190f2a
+}
190f2a
+
190f2a
+static void *makeSyscall(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+
190f2a
+   sleep(42);
190f2a
+
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+/* Return true if we're currently executing on the altstack */
190f2a
+static int onAltstack(void) {
190f2a
+   stack_t stack;
190f2a
+   int result;
190f2a
+   
190f2a
+   result = sigaltstack(NULL, &stack);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   return stack.ss_flags & SS_ONSTACK;
190f2a
+}
190f2a
+
190f2a
+static void syscallingAltSighandler(int signo, siginfo_t *info, void *context) {
190f2a
+   (void)signo;
190f2a
+   (void)info;
190f2a
+   (void)context;
190f2a
+   
190f2a
+   if (!onAltstack()) {
190f2a
+      printf("%s() not running on altstack!\n", __FUNCTION__);
190f2a
+   }
190f2a
+   
190f2a
+   sleep(42);
190f2a
+}
190f2a
+
190f2a
+static void spinningAltSighandler(int signo, siginfo_t *info, void *context) {
190f2a
+   (void)signo;
190f2a
+   (void)info;
190f2a
+   (void)context;
190f2a
+   
190f2a
+   if (!onAltstack()) {
190f2a
+      printf("%s() not running on altstack!\n", __FUNCTION__);
190f2a
+   }
190f2a
+   
190f2a
+   while (1);
190f2a
+}
190f2a
+
190f2a
+static void syscallingSighandler(int signo, siginfo_t *info, void *context) {
190f2a
+   (void)signo;
190f2a
+   (void)info;
190f2a
+   (void)context;
190f2a
+   
190f2a
+   if (onAltstack()) {
190f2a
+      printf("%s() running on altstack!\n", __FUNCTION__);
190f2a
+   }
190f2a
+   
190f2a
+   sleep(42);
190f2a
+}
190f2a
+
190f2a
+static void spinningSighandler(int signo, siginfo_t *info, void *context) {
190f2a
+   (void)signo;
190f2a
+   (void)info;
190f2a
+   (void)context;
190f2a
+   
190f2a
+   if (onAltstack()) {
190f2a
+      printf("%s() running on altstack!\n", __FUNCTION__);
190f2a
+   }
190f2a
+   
190f2a
+   while (1);
190f2a
+}
190f2a
+
190f2a
+static void *makeSyscallFromAltSighandler(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+   
190f2a
+   int result;
190f2a
+   
190f2a
+   installAlthandler(SIGSYSCALL_ALT, syscallingAltSighandler);
190f2a
+   
190f2a
+   result = pthread_kill(pthread_self(), SIGSYSCALL_ALT);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+static void *makeSyscallFromSighandler(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+   
190f2a
+   int result;
190f2a
+   
190f2a
+   installNormalHandler(SIGSYSCALL, syscallingSighandler);
190f2a
+   
190f2a
+   result = pthread_kill(pthread_self(), SIGSYSCALL);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+static void *spinFromAltSighandler(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+   
190f2a
+   int result;
190f2a
+   
190f2a
+   installAlthandler(SIGSPIN_ALT, spinningAltSighandler);
190f2a
+   
190f2a
+   result = pthread_kill(pthread_self(), SIGSPIN_ALT);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+static void *spinFromSighandler(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+   
190f2a
+   int result;
190f2a
+   
190f2a
+   installNormalHandler(SIGSPIN, spinningSighandler);
190f2a
+   
190f2a
+   result = pthread_kill(pthread_self(), SIGSPIN);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+static void *spin(void *ignored) {
190f2a
+   (void)ignored;
190f2a
+   
190f2a
+   while (1);
190f2a
+
190f2a
+   fprintf(stderr, "%s: returning\n", __FUNCTION__);
190f2a
+   return NULL;
190f2a
+}
190f2a
+
190f2a
+int main(int argc, char *argv[]) {
190f2a
+   int result;
190f2a
+   pthread_t thread;
190f2a
+   volatile int bad;
190f2a
+   
190f2a
+   result = pthread_create(&thread, NULL, makeSyscall, NULL);
190f2a
+   assert(result == 0);
190f2a
+   result = pthread_create(&thread, NULL, makeSyscallFromSighandler, NULL);
190f2a
+   assert(result == 0);
190f2a
+   result = pthread_create(&thread, NULL, makeSyscallFromAltSighandler, NULL);
190f2a
+   assert(result == 0);
190f2a
+   result = pthread_create(&thread, NULL, spin, NULL);
190f2a
+   assert(result == 0);
190f2a
+   result = pthread_create(&thread, NULL, spinFromSighandler, NULL);
190f2a
+   assert(result == 0);
190f2a
+   result = pthread_create(&thread, NULL, spinFromAltSighandler, NULL);
190f2a
+   assert(result == 0);
190f2a
+   
190f2a
+   // Give threads some time to get going
190f2a
+   sleep(3);
190f2a
+   
190f2a
+   // Crash
190f2a
+   bad = *(int*)7;
190f2a
+
190f2a
+   /* Workaround: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29628
190f2a
+      Simulate use to ensure `DW_AT_location' for them:
190f2a
+      readelf -a --debug threadcrash|grep -A5 -w argc
190f2a
+      --> DW_AT_location    : 2 byte block: 71 0     (DW_OP_breg1: 0)
190f2a
+      This case verified on: gcc-4.1.1-30.i386
190f2a
+      Keep it late to ensure persistency in the registers.  */
190f2a
+   bad = (int) argc;
190f2a
+   bad = (unsigned long) argv;
190f2a
+   
190f2a
+   return 0;
190f2a
+}
190f2a
diff --git a/gdb/testsuite/gdb.threads/threadcrash.exp b/gdb/testsuite/gdb.threads/threadcrash.exp
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/testsuite/gdb.threads/threadcrash.exp
190f2a
@@ -0,0 +1,37 @@
190f2a
+# threadcrash.exp - The point of this program is to crash in a multi-threaded app.
190f2a
+
190f2a
+
190f2a
+set testfile threadcrash
190f2a
+set srcfile ${testfile}.c
190f2a
+set shellfile ${srcdir}/${subdir}/${testfile}.sh
190f2a
+set binfile [standard_output_file ${testfile}]
190f2a
+
190f2a
+set GDB_abs ${GDB}
190f2a
+if [regexp "^\[^/\]" ${GDB_abs}] {
190f2a
+    set GDB_abs $env(PWD)/${GDB_abs}
190f2a
+}
190f2a
+
190f2a
+if [istarget "*-*-linux"] then {
190f2a
+    set target_cflags "-D_MIT_POSIX_THREADS"
190f2a
+} else {
190f2a
+    set target_cflags ""
190f2a
+}
190f2a
+
190f2a
+if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
190f2a
+    return -1
190f2a
+}
190f2a
+
190f2a
+# ${shellfile} argument must not contain any directories.
190f2a
+set fd [open "|bash ${shellfile} ${binfile} $GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]" r]
190f2a
+while { [gets $fd line] >= 0 } {
190f2a
+    if [regexp " PASS: (.*)$" $line trash message] {
190f2a
+	pass $message
190f2a
+    } elseif [regexp " FAIL: (.*)$" $line trash message] {
190f2a
+	fail $message
190f2a
+    }
190f2a
+}
190f2a
+catch {
190f2a
+    close $fd
190f2a
+}
190f2a
+
190f2a
+return 0
190f2a
diff --git a/gdb/testsuite/gdb.threads/threadcrash.sh b/gdb/testsuite/gdb.threads/threadcrash.sh
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/testsuite/gdb.threads/threadcrash.sh
190f2a
@@ -0,0 +1,324 @@
190f2a
+#! /bin/bash
190f2a
+
190f2a
+# NOTE: threadcrash.c *must* be built with debugging symbols
190f2a
+#
190f2a
+# The point of this shell script is to crash treadcrash.c, load the
190f2a
+# resulting core file into gdb and verify that gdb can extract enough
190f2a
+# information from the core file.
190f2a
+#
190f2a
+# The return code from this script is the number of failed tests.
190f2a
+
190f2a
+LOG=gdbresult.log
190f2a
+
190f2a
+if [ $# = 0 ] ; then
190f2a
+    echo >&2 Syntax: $0 \<name of threadcrash binary\> [\<gdb binary\> \<args...\>]
190f2a
+    exit 1
190f2a
+fi
190f2a
+RUNME="$1"
190f2a
+shift
190f2a
+GDB="${*:-gdb}"
190f2a
+
190f2a
+
190f2a
+pf_prefix=""
190f2a
+function pf_prefix() {
190f2a
+	pf_prefix="$*"
190f2a
+}
190f2a
+
190f2a
+set_test=""
190f2a
+function set_test() {
190f2a
+	if [ -n "$set_test" ] ; then
190f2a
+		echo >&2 "DEJAGNU-BASH ERROR: set_test already set"
190f2a
+		exit 1
190f2a
+	fi
190f2a
+	set_test="$*"
190f2a
+	if [ -n "$pf_prefix" ] ; then
190f2a
+		set_test="$pf_prefix: $set_test"
190f2a
+	fi
190f2a
+}
190f2a
+
190f2a
+# INTERNAL
190f2a
+function record_test {
190f2a
+	if [ -z "$set_test" ] ; then
190f2a
+		echo >&2 "DEJAGNU-BASH ERROR: set_test not set"
190f2a
+		exit 1
190f2a
+	fi
190f2a
+	# Provide the leading whitespace delimiter:
190f2a
+	echo " $1: $set_test"
190f2a
+	set_test=""
190f2a
+}
190f2a
+
190f2a
+function pass() {
190f2a
+	record_test PASS
190f2a
+}
190f2a
+function fail() {
190f2a
+	record_test FAIL
190f2a
+}
190f2a
+
190f2a
+
190f2a
+# Verify that the gdb output doesn't contain $1.
190f2a
+function mustNotHave() {
190f2a
+    local BADWORD=$1
190f2a
+    set_test gdb output contains "$BADWORD"
190f2a
+    if grep -q "$BADWORD" $LOG ; then
190f2a
+        fail
190f2a
+        return 1
190f2a
+    fi
190f2a
+    pass
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output contains exactly $1 $2s.
190f2a
+function mustHaveCorrectAmount() {
190f2a
+    local WANTEDNUMBER=$1
190f2a
+    local GOODWORD=$2
190f2a
+    local ACTUALNUMBER=$(grep "$GOODWORD" $LOG | wc -l)
190f2a
+    set_test gdb output contained $ACTUALNUMBER \""$GOODWORD"\", not $WANTEDNUMBER as expected
190f2a
+    if [ $ACTUALNUMBER != $WANTEDNUMBER ] ; then
190f2a
+        fail
190f2a
+        return 1
190f2a
+    fi
190f2a
+    pass
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output contains seven threads
190f2a
+function mustHaveSevenThreads() {
190f2a
+    NTHREADS=$(egrep "^Thread [1-7] \(" $LOG | wc -l)
190f2a
+    set_test gdb output contains $NTHREADS threads, not 7 as expected
190f2a
+    if [ $NTHREADS != 7 ] ; then
190f2a
+        fail
190f2a
+        return 1
190f2a
+    fi
190f2a
+    pass
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output has all parameters on consecutive lines
190f2a
+function mustHaveSequence() {
190f2a
+    SEQUENCE="$*"
190f2a
+    NPARTS=$#
190f2a
+    grep "$1" -A$((NPARTS - 1)) $LOG > matches.log
190f2a
+
190f2a
+    while [ $# -gt 1 ] ; do
190f2a
+        shift
190f2a
+        ((NPARTS--))
190f2a
+        grep "$1" -A$((NPARTS - 1)) matches.log > temp.log
190f2a
+        mv temp.log matches.log
190f2a
+    done
190f2a
+    LASTPART=$1
190f2a
+
190f2a
+    set_test gdb output does not contain the sequence: $SEQUENCE
190f2a
+    if ! grep -q "$LASTPART" matches.log ; then
190f2a
+        fail
190f2a
+        return 1
190f2a
+    fi
190f2a
+    pass
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that $LOG contains all information we want
190f2a
+function verifyLog() {
190f2a
+    local FAILURES=0
190f2a
+    
190f2a
+    mustNotHave '??' || ((FAILURES++))
190f2a
+    mustHaveCorrectAmount 11 threadcrash.c: || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSevenThreads || ((FAILURES++))
190f2a
+    mustHaveSequence sleep "makeSyscall (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence sleep "syscallingSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "makeSyscallFromSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence sleep "syscallingAltSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "makeSyscallFromAltSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence Thread "spin (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence "spinningSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "spinFromSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence "spinningAltSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "spinFromAltSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence Thread "main (argc=1, argv=" || ((FAILURES++))
190f2a
+
190f2a
+    return $FAILURES
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a core file in $LOG
190f2a
+function getLogFromCore() {
190f2a
+    # Make sure we get a core file
190f2a
+    set_test Make sure we get a core file
190f2a
+    if ! ulimit -c unlimited ; then
190f2a
+        fail
190f2a
+        exit 1
190f2a
+    fi
190f2a
+    pass
190f2a
+
190f2a
+    # Run the crasher
190f2a
+    ./$(basename "$RUNME")
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    # Verify that we actually crashed
190f2a
+    set_test $RUNME should have been killed by a signal, got non-signal exit code $EXITCODE
190f2a
+    if [ $EXITCODE -lt 128 ] ; then
190f2a
+        fail
190f2a
+        exit 1
190f2a
+    fi
190f2a
+    pass
190f2a
+
190f2a
+    # Verify that we got a core file
190f2a
+    set_test $RUNME did not create a core file
190f2a
+    if [ ! -r core* ] ; then
190f2a
+        fail
190f2a
+        exit 1
190f2a
+    fi
190f2a
+    pass
190f2a
+
190f2a
+    # Run gdb
190f2a
+    cat > gdbscript.gdb <
190f2a
+set width 0
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | $GDB -nx "./$(basename "$RUNME")" core* > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    set_test gdb exited with error code
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >&2 gdb exited with error code $EXITCODE
190f2a
+	fail
190f2a
+    fi
190f2a
+    pass
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a gcore file in $LOG
190f2a
+function getLogFromGcore() {
190f2a
+    # Create the core file
190f2a
+    rm -f core*
190f2a
+    cat > gdbscript.gdb <
190f2a
+handle SIGQUIT pass noprint nostop
190f2a
+handle SIGUSR1 pass noprint nostop
190f2a
+handle SIGUSR2 pass noprint nostop
190f2a
+handle SIGALRM pass noprint nostop
190f2a
+run
190f2a
+gcore
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | $GDB -nx "./$(basename "$RUNME")" > /dev/null
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    set_test gdb exited with error code when creating gcore file
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >&2 gdb exited with error code $EXITCODE when creating gcore file
190f2a
+	fail
190f2a
+    fi
190f2a
+    pass
190f2a
+    
190f2a
+    # Verify that we got a core file from gcore
190f2a
+    set_test gdb gcore did not create a core file
190f2a
+    if [ ! -r core* ] ; then
190f2a
+        fail
190f2a
+        exit 1
190f2a
+    fi
190f2a
+    pass
190f2a
+
190f2a
+    # Run gdb on the gcore file
190f2a
+    cat > gdbscript.gdb <
190f2a
+set width 0
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | $GDB -nx "./$(basename "$RUNME")" core* > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    set_test gdb exited with error code when examining gcore file
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >&2 gdb exited with error code $EXITCODE when examining gcore file
190f2a
+	fail
190f2a
+    fi
190f2a
+    pass
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a core file in $LOG
190f2a
+function getLogFromLiveProcess() {
190f2a
+    # Run gdb
190f2a
+    cat > gdbscript.gdb <
190f2a
+handle SIGQUIT pass noprint nostop
190f2a
+handle SIGUSR1 pass noprint nostop
190f2a
+handle SIGUSR2 pass noprint nostop
190f2a
+handle SIGALRM pass noprint nostop
190f2a
+set width 0
190f2a
+run
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | $GDB -nx "./$(basename "$RUNME")" > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    set_test gdb exited with error code
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >&2 gdb exited with error code $EXITCODE
190f2a
+	fail
190f2a
+    fi
190f2a
+    pass
190f2a
+}
190f2a
+
190f2a
+####### Main program follows #####################
190f2a
+
190f2a
+# Make sure we don't clobber anybody else's (core) file(s)
190f2a
+WORKDIR=/tmp/$PPID
190f2a
+mkdir -p $WORKDIR
190f2a
+cp "$RUNME" $WORKDIR
190f2a
+cd $WORKDIR
190f2a
+
190f2a
+# Count problems
190f2a
+FAILURES=0
190f2a
+
190f2a
+echo === Testing gdb vs core file...
190f2a
+pf_prefix core file
190f2a
+getLogFromCore
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+pf_prefix
190f2a
+echo === Core file tests done.
190f2a
+
190f2a
+echo
190f2a
+
190f2a
+echo === Testing gdb vs gcore file...
190f2a
+pf_prefix gcore file
190f2a
+getLogFromGcore
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+pf_prefix
190f2a
+echo === Gcore file tests done.
190f2a
+
190f2a
+echo
190f2a
+
190f2a
+echo === Testing gdb vs live process...
190f2a
+pf_prefix live process
190f2a
+getLogFromLiveProcess
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+pf_prefix
190f2a
+echo === Live process tests done.
190f2a
+
190f2a
+# Executive summary
190f2a
+echo
190f2a
+if [ $FAILURES == 0 ] ; then
190f2a
+    echo All tests passed!
190f2a
+else
190f2a
+    echo $FAILURES tests failed!
190f2a
+    echo
190f2a
+    echo Make sure the threadcrash binary contains debugging information \(build with \"gcc -g\"\).
190f2a
+fi
190f2a
+
190f2a
+# Clean up
190f2a
+cd /
190f2a
+rm -rf $WORKDIR
190f2a
+
190f2a
+exit $FAILURES
190f2a
diff --git a/gdb/testsuite/gdb.threads/threadcrash.sh-orig b/gdb/testsuite/gdb.threads/threadcrash.sh-orig
190f2a
new file mode 100644
190f2a
--- /dev/null
190f2a
+++ b/gdb/testsuite/gdb.threads/threadcrash.sh-orig
190f2a
@@ -0,0 +1,248 @@
190f2a
+#! /bin/bash
190f2a
+
190f2a
+# NOTE: threadcrash.c *must* be built with debugging symbols
190f2a
+#
190f2a
+# The point of this shell script is to crash treadcrash.c, load the
190f2a
+# resulting core file into gdb and verify that gdb can extract enough
190f2a
+# information from the core file.
190f2a
+#
190f2a
+# The return code from this script is the number of failed tests.
190f2a
+
190f2a
+LOG=gdbresult.log
190f2a
+
190f2a
+if [ $# != 1 ] ; then
190f2a
+    echo > /dev/stderr Syntax: $0 \<name of threadcrash binary\>
190f2a
+    exit 1
190f2a
+fi
190f2a
+RUNME="$1"
190f2a
+
190f2a
+# Verify that the gdb output doesn't contain $1.
190f2a
+function mustNotHave() {
190f2a
+    local BADWORD=$1
190f2a
+    if grep -q "$BADWORD" $LOG ; then
190f2a
+        echo >> /dev/stderr WARNING: gdb output contains "$BADWORD"
190f2a
+        return 1
190f2a
+    fi
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output contains exactly $1 $2s.
190f2a
+function mustHaveCorrectAmount() {
190f2a
+    local WANTEDNUMBER=$1
190f2a
+    local GOODWORD=$2
190f2a
+    local ACTUALNUMBER=$(grep "$GOODWORD" $LOG | wc -l)
190f2a
+    if [ $ACTUALNUMBER != $WANTEDNUMBER ] ; then
190f2a
+        echo >> /dev/stderr WARNING: gdb output contained $ACTUALNUMBER \""$GOODWORD"\", not $WANTEDNUMBER as expected
190f2a
+        return 1
190f2a
+    fi
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output contains seven threads
190f2a
+function mustHaveSevenThreads() {
190f2a
+    NTHREADS=$(egrep "^Thread [1-7] \(" $LOG | wc -l)
190f2a
+    if [ $NTHREADS != 7 ] ; then
190f2a
+        echo >> /dev/stderr WARNING: gdb output contains $NTHREADS threads, not 7 as expected
190f2a
+        return 1
190f2a
+    fi
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that the gdb output has all parameters on consecutive lines
190f2a
+function mustHaveSequence() {
190f2a
+    SEQUENCE="$*"
190f2a
+    NPARTS=$#
190f2a
+    grep "$1" -A$((NPARTS - 1)) $LOG > matches.log
190f2a
+
190f2a
+    while [ $# -gt 1 ] ; do
190f2a
+        shift
190f2a
+        ((NPARTS--))
190f2a
+        grep "$1" -A$((NPARTS - 1)) matches.log > temp.log
190f2a
+        mv temp.log matches.log
190f2a
+    done
190f2a
+    LASTPART=$1
190f2a
+
190f2a
+    if ! grep -q "$LASTPART" matches.log ; then
190f2a
+        echo >> /dev/stderr WARNING: gdb output does not contain the sequence: $SEQUENCE
190f2a
+        return 1
190f2a
+    fi
190f2a
+    return 0
190f2a
+}
190f2a
+
190f2a
+# Verify that $LOG contains all information we want
190f2a
+function verifyLog() {
190f2a
+    local FAILURES=0
190f2a
+    
190f2a
+    mustNotHave '??' || ((FAILURES++))
190f2a
+    mustHaveCorrectAmount 12 threadcrash.c: || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSevenThreads || ((FAILURES++))
190f2a
+    mustHaveSequence sleep "makeSyscall (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence sleep "syscallingSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "makeSyscallFromSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence sleep "syscallingAltSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "makeSyscallFromAltSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence Thread "spin (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence "spinningSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "spinFromSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence "spinningAltSighandler (signo=" "signal handler called" 0x || ((FAILURES++))
190f2a
+    mustHaveSequence pthread_kill "spinFromAltSighandler (ignored=" || ((FAILURES++))
190f2a
+    
190f2a
+    mustHaveSequence Thread "main (argc=1, argv=" || ((FAILURES++))
190f2a
+
190f2a
+    return $FAILURES
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a core file in $LOG
190f2a
+function getLogFromCore() {
190f2a
+    # Make sure we get a core file
190f2a
+    ulimit -c unlimited || exit 1
190f2a
+
190f2a
+    # Run the crasher
190f2a
+    ./$(basename "$RUNME")
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    # Verify that we actually crashed
190f2a
+    if [ $EXITCODE -lt 128 ] ; then
190f2a
+        echo >> /dev/stderr ERROR: $RUNME should have been killed by a signal, got non-signal exit code $EXITCODE
190f2a
+        exit 1
190f2a
+    fi
190f2a
+
190f2a
+    # Verify that we got a core file
190f2a
+    if [ ! -r core* ] ; then
190f2a
+        echo >> /dev/stderr ERROR: $RUNME did not create a core file
190f2a
+        exit 1
190f2a
+    fi
190f2a
+
190f2a
+    # Run gdb
190f2a
+    cat > gdbscript.gdb <
190f2a
+set width 0
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | gdb -nx "./$(basename "$RUNME")" core* > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >> /dev/stderr WARNING: gdb exited with error code $EXITCODE
190f2a
+    fi
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a gcore file in $LOG
190f2a
+function getLogFromGcore() {
190f2a
+    # Create the core file
190f2a
+    rm -f core*
190f2a
+    cat > gdbscript.gdb <
190f2a
+handle SIGQUIT pass noprint nostop
190f2a
+handle SIGUSR1 pass noprint nostop
190f2a
+handle SIGUSR2 pass noprint nostop
190f2a
+handle SIGALRM pass noprint nostop
190f2a
+run
190f2a
+gcore
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | gdb -nx "./$(basename "$RUNME")" > /dev/null
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >> /dev/stderr WARNING: gdb exited with error code $EXITCODE when creating gcore file
190f2a
+    fi
190f2a
+    
190f2a
+    # Verify that we got a core file from gcore
190f2a
+    if [ ! -r core* ] ; then
190f2a
+        echo >> /dev/stderr ERROR: gdb gcore did not create a core file
190f2a
+        exit 1
190f2a
+    fi
190f2a
+
190f2a
+    # Run gdb on the gcore file
190f2a
+    cat > gdbscript.gdb <
190f2a
+set width 0
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | gdb -nx "./$(basename "$RUNME")" core* > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >> /dev/stderr WARNING: gdb exited with error code $EXITCODE when examining gcore file
190f2a
+    fi
190f2a
+}
190f2a
+
190f2a
+# Put result of debugging a core file in $LOG
190f2a
+function getLogFromLiveProcess() {
190f2a
+    # Run gdb
190f2a
+    cat > gdbscript.gdb <
190f2a
+handle SIGQUIT pass noprint nostop
190f2a
+handle SIGUSR1 pass noprint nostop
190f2a
+handle SIGUSR2 pass noprint nostop
190f2a
+handle SIGALRM pass noprint nostop
190f2a
+set width 0
190f2a
+run
190f2a
+t a a bt 100
190f2a
+quit
190f2a
+EOF
190f2a
+    cat gdbscript.gdb /dev/zero | gdb -nx "./$(basename "$RUNME")" > $LOG
190f2a
+    EXITCODE=$?
190f2a
+
190f2a
+    if [ $EXITCODE != 0 ] ; then
190f2a
+        ((FAILURES++))
190f2a
+        echo >> /dev/stderr WARNING: gdb exited with error code $EXITCODE
190f2a
+    fi
190f2a
+}
190f2a
+
190f2a
+####### Main program follows #####################
190f2a
+
190f2a
+# Make sure we don't clobber anybody else's (core) file(s)
190f2a
+WORKDIR=/tmp/$PPID
190f2a
+mkdir -p $WORKDIR
190f2a
+cp "$RUNME" $WORKDIR
190f2a
+cd $WORKDIR
190f2a
+
190f2a
+# Count problems
190f2a
+FAILURES=0
190f2a
+
190f2a
+echo === Testing gdb vs core file...
190f2a
+getLogFromCore
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+echo === Core file tests done.
190f2a
+
190f2a
+echo
190f2a
+
190f2a
+echo === Testing gdb vs gcore file...
190f2a
+getLogFromGcore
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+echo === Gcore file tests done.
190f2a
+
190f2a
+echo
190f2a
+
190f2a
+echo === Testing gdb vs live process...
190f2a
+getLogFromLiveProcess
190f2a
+verifyLog
190f2a
+((FAILURES+=$?))
190f2a
+echo === Live process tests done.
190f2a
+
190f2a
+# Executive summary
190f2a
+echo
190f2a
+if [ $FAILURES == 0 ] ; then
190f2a
+    echo All tests passed!
190f2a
+else
190f2a
+    echo $FAILURES tests failed!
190f2a
+    echo
190f2a
+    echo Make sure the threadcrash binary contains debugging information \(build with \"gcc -g\"\).
190f2a
+fi
190f2a
+
190f2a
+# Clean up
190f2a
+cd /
190f2a
+rm -rf $WORKDIR
190f2a
+
190f2a
+exit $FAILURES