Test: More fixes and updates.
[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 # SPDX-License-Identifier: GPL-2.0-or-later
10 #
11 # Run the command we're passed in a subshell, so that said subshell will
12 # catch any signals from it and report it.
13 #
14 # This must be done for commands that aren't the last command in the
15 # pipeline, as, given that the exit status of a pipeline is the exit
16 # status of the last command in the pipeline, there's no guarantee that
17 # the shell will bother to pick up the exit status of earlier commands
18 # in the pipeline.
19 #
20 # It can also be done for other tests, to get more information than
21 # "it crashed due to signal XXX" if the tests fail with a crash signal.
22 #
23 # XXX - on macOS, core dumps are in /cores/core.{PID}; would they appear
24 # elsewhere on any other UN*X?
25 #
26 rm -f core
27 "$@"
28 exitstatus=$?
29 if [ -r core ]
30 then
31         #
32         # Core dumped - try to get a stack trace.
33         #
34         # First, find the executable.  Skip past env and any env
35         # arguments to find the actual executable path.  (If you
36         # run a program with an explicit path, and it dumps core,
37         # at least on Solaris the output of "file" on the core dump
38         # will not give the path, so we don't use that.)
39         #
40         if [ "$1" = "env" ]
41         then
42                 #
43                 # Skip past the env command name.
44                 #
45                 shift
46                 #
47                 # Skip past environment-variable arguments; anything
48                 # with an "=" in it is an environment-variable argument.
49                 #
50                 while expr "$1" : ".*=.*" >/dev/null 2>&1
51                 do
52                         shift
53                 done
54 echo last expr command was expr "$1" : ".*=.*"
55         fi
56         if [ -x "$1" ]
57         then
58                 executable="$1"
59         else
60                 executable=`which "$1"`
61         fi
62
63         if [ ! -z "$executable" ]
64         then
65                 #
66                 # Found the executable.
67                 # Now, look for a debugger.
68                 # XXX - lldb?
69                 #
70                 dbx=`which dbx`
71                 if [ ! -z "$dbx" ]
72                 then
73                         #
74                         # Found dbx.  Run it to get a stack trace;
75                         # cause the stack trace to go to the standard
76                         # error.
77                         #
78                         dbx "$executable" core 1>&2 <<EOF
79 where
80 quit
81 EOF
82                 else
83                         gdb=`which gdb`
84                         if [ ! -z "$gdb" ]
85                         then
86                                 #
87                                 # Found gdb.  Run it to get a stack trace;
88                                 # cause the stack trace to go to the standard
89                                 # error.
90                                 #
91                                 gdb "$executable" core 1>&2 <<EOF
92 backtrace
93 quit
94 EOF
95                         fi
96                 fi
97         fi
98 fi
99 exit $exitstatus