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