Fix a typo in the I/O tests.
[metze/wireshark/wip.git] / test / run_and_catch_crashes
1 #! /bin/sh
2 #
3 # Run command in a way that catches crashes
4 #
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 2015 Gerald Combs
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #
23 # Run the command we're passed in a subshell, so that said subshell will
24 # catch any signals from it and report it.
25 #
26 # This must be done for commands that aren't the last command in the
27 # pipeline, as, given that the exit status of a pipeline is the exit
28 # status of the last command in the pipeline, there's no guarantee that
29 # the shell will bother to pick up the exit status of earlier commands
30 # in the pipeline.
31 #
32 # It can also be done for other tests, to get more information than
33 # "it crashed due to signal XXX" if the tests fail with a crash signal.
34 #
35 # XXX - on macOS, core dumps are in /cores/core.{PID}; would they appear
36 # elsewhere on any other UN*X?
37 #
38 rm -f core
39 "$@"
40 exitstatus=$?
41 if [ -r core ]
42 then
43         #
44         # Core dumped - try to get a stack trace.
45         #
46         # First, find the executable.  Skip past env and any env
47         # arguments to find the actual executable path.  (If you
48         # run a program with an explicit path, and it dumps core,
49         # at least on Solaris the output of "file" on the core dump
50         # will not give the path, so we don't use that.)
51         #
52         if [ "$1" = "env" ]
53         then
54                 #
55                 # Skip past the env command name.
56                 #
57                 shift
58                 #
59                 # Skip past environment-variable arguments; anything
60                 # with an "=" in it is an environment-variable argument.
61                 #
62                 while expr "$1" : ".*=.*" >/dev/null 2>&1
63                 do
64                         shift
65                 done
66 echo last expr command was expr "$1" : ".*=.*"
67         fi
68         if [ -x "$1" ]
69         then
70                 executable="$1"
71         else
72                 executable=`which "$1"`
73         fi
74
75         if [ ! -z "$executable" ]
76         then
77                 #
78                 # Found the executable.
79                 # Is it a libtool wrapper script?  Look for a .libs
80                 # directory.
81                 #
82                 executable_dirname=`dirname "$executable"`
83                 if [ -d "$executable_dirname"/.libs ]
84                 then
85                         is_libtool_wrapper=yes
86                 else
87                         is_libtool_wrapper=no
88                 fi
89
90                 #
91                 # Now, look for a debugger.
92                 # XXX - lldb?
93                 #
94                 dbx=`which dbx`
95                 if [ ! -z "$dbx" ]
96                 then
97                         #
98                         # Found dbx.  Run it to get a stack trace;
99                         # cause the stack trace to go to the standard
100                         # error.
101                         #
102                         if [ $is_libtool_wrapper = yes ]
103                         then
104                                 $executable_dirname/libtool --mode=execute dbx "$executable" core 1>&2 <<EOF
105 where
106 quit
107 EOF
108                         else
109                                 dbx "$executable" core 1>&2 <<EOF
110 where
111 quit
112 EOF
113                         fi
114                 else
115                         gdb=`which gdb`
116                         if [ ! -z "$gdb" ]
117                         then
118                                 #
119                                 # Found gdb.  Run it to get a stack trace;
120                                 # cause the stack trace to go to the standard
121                                 # error.
122                                 #
123                                 if [ $is_libtool_wrapper = yes ]
124                                 then
125                                         $executable_dirname/libtool --mode=execute gdb "$executable" core 1>&2 <<EOF
126 backtrace
127 quit
128 EOF
129                                 else
130                                         gdb "$executable" core 1>&2 <<EOF
131 backtrace
132 quit
133 EOF
134                                 fi
135                         fi
136                 fi
137         fi
138 fi
139 exit $exitstatus