Re-organize the way to produce stacktrace.
[platform/upstream/glog.git] / src / signalhandler_unittest.sh
1 #! /bin/sh
2 # Copyright 2008 Google, Inc.  All Rights Reserved.
3 # Author: Satoru Takabayashi
4 #
5 # Unit tests for signalhandler.cc.
6
7 die () {
8     echo $1
9     exit 1
10 }
11
12 BINDIR=".libs"
13 LIBGLOG="$BINDIR/libglog.so"
14
15 BINARY="$BINDIR/signalhandler_unittest"
16
17 # Remove temporary files.
18 rm -f signalhandler.out*
19
20 if test -e "$BINARY"; then
21   # We need shared object.
22   export LD_LIBRARY_PATH=$BINDIR
23   export DYLD_LIBRARY_PATH=$BINDIR
24 else
25   # For windows
26   BINARY="./signalhandler_unittest.exe"
27   if ! test -e "$BINARY"; then
28     echo "We coundn't find demangle_unittest binary."
29     exit 1
30   fi
31 fi
32
33 if [ x`$BINARY` != 'xOK' ]; then
34   echo "PASS (No stacktrace support. We don't run this test.)"
35   exit 0
36 fi
37
38 # The PC cannot be obtained in signal handlers on PowerPC correctly.
39 # We just skip the test for PowerPC.
40 if [ x`uname -p` = x"powerpc" ]; then
41   echo "PASS (We don't test the signal handler on PowerPC.)"
42   exit 0
43 fi
44
45 # Test for a case the program kills itself by SIGSEGV.
46 $BINARY segv 2> signalhandler.out1
47 for pattern in SIGSEGV 0xdead main "Aborted at [0-9]"; do
48   if ! grep --quiet "$pattern" signalhandler.out1; then
49     die "'$pattern' should appear in the output"
50   fi
51 done
52
53 # Test for a case the program is killed by this shell script.
54 # $! = the process id of the last command run in the background.
55 # $$ = the process id of this shell.
56 $BINARY loop 2> signalhandler.out2 &
57 # Wait until "looping" is written in the file.  This indicates the program
58 # is ready to accept signals.
59 while true; do
60   if grep --quiet looping signalhandler.out2; then
61     break
62   fi
63 done
64 kill -TERM $!
65 wait $!
66
67 from_pid=''
68 # Only linux has the process ID of the signal sender.
69 if [ x`uname` = "xLinux" ]; then
70   from_pid="from PID $$"
71 fi
72 for pattern in SIGTERM "by PID $!" "$from_pid" main "Aborted at [0-9]"; do
73   if ! grep --quiet "$pattern" signalhandler.out2; then
74     die "'$pattern' should appear in the output"
75   fi
76 done
77
78 # Test for a case the program dies in a non-main thread.
79 $BINARY die_in_thread 2> signalhandler.out3
80 EXPECTED_TID="`sed 's/ .*//' signalhandler.out3`"
81
82 for pattern in SIGFPE DieInThread "TID $EXPECTED_TID" "Aborted at [0-9]"; do
83   if ! grep --quiet "$pattern" signalhandler.out3; then
84     die "'$pattern' should appear in the output"
85   fi
86 done
87
88 # Test for a case the program installs a custom failure writer that writes
89 # stuff to stdout instead of stderr.
90 $BINARY dump_to_stdout 1> signalhandler.out4
91 for pattern in SIGABRT main "Aborted at [0-9]"; do
92   if ! grep --quiet "$pattern" signalhandler.out4; then
93     die "'$pattern' should appear in the output"
94   fi
95 done
96
97 echo PASS