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 # SPDX-License-Identifier: GPL-2.0-or-later
11 # Run the command we're passed in a subshell, so that said subshell will
12 # catch any signals from it and report it.
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
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.
23 # XXX - on macOS, core dumps are in /cores/core.{PID}; would they appear
24 # elsewhere on any other UN*X?
32 # Core dumped - try to get a stack trace.
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.)
43 # Skip past the env command name.
47 # Skip past environment-variable arguments; anything
48 # with an "=" in it is an environment-variable argument.
50 while expr "$1" : ".*=.*" >/dev/null 2>&1
54 echo last expr command was expr "$1" : ".*=.*"
60 executable=`which "$1"`
63 if [ ! -z "$executable" ]
66 # Found the executable.
67 # Now, look for a debugger.
74 # Found dbx. Run it to get a stack trace;
75 # cause the stack trace to go to the standard
78 dbx "$executable" core 1>&2 <<EOF
87 # Found gdb. Run it to get a stack trace;
88 # cause the stack trace to go to the standard
91 gdb "$executable" core 1>&2 <<EOF