3 # Run command in a way that catches crashes
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 2015 Gerald Combs
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.
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.
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.
23 # Run the command we're passed in a subshell, so that said subshell will
24 # catch any signals from it and report it.
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
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.
35 # XXX - on macOS, core dumps are in /cores/core.{PID}; would they appear
36 # elsewhere on any other UN*X?
44 # Core dumped - try to get a stack trace.
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.)
55 # Skip past the env command name.
59 # Skip past environment-variable arguments; anything
60 # with an "=" in it is an environment-variable argument.
62 while expr "$1" : ".*=.*" >/dev/null 2>&1
66 echo last expr command was expr "$1" : ".*=.*"
72 executable=`which "$1"`
75 if [ ! -z "$executable" ]
78 # Found the executable.
79 # Is it a libtool wrapper script? Look for a .libs
82 executable_dirname=`dirname "$executable"`
83 if [ -d "$executable_dirname"/.libs ]
85 is_libtool_wrapper=yes
91 # Now, look for a debugger.
98 # Found dbx. Run it to get a stack trace;
99 # cause the stack trace to go to the standard
102 if [ $is_libtool_wrapper = yes ]
104 $executable_dirname/libtool --mode=execute dbx "$executable" core 1>&2 <<EOF
109 dbx "$executable" core 1>&2 <<EOF
119 # Found gdb. Run it to get a stack trace;
120 # cause the stack trace to go to the standard
123 if [ $is_libtool_wrapper = yes ]
125 $executable_dirname/libtool --mode=execute gdb "$executable" core 1>&2 <<EOF
130 gdb "$executable" core 1>&2 <<EOF