2003-01-19 21:07:38 +03:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Program: RunSafely.sh
|
|
|
|
#
|
|
|
|
# Synopsis: This script simply runs another program. If the program works
|
|
|
|
# correctly, this script has no effect, otherwise it will do things
|
|
|
|
# like print a stack trace of a core dump. It always returns
|
|
|
|
# "successful" so that tests will continue to be run.
|
|
|
|
#
|
2003-05-11 21:34:14 +04:00
|
|
|
# This script funnels stdout and stderr from the program into the
|
2006-11-25 11:56:10 +03:00
|
|
|
# fourth argument specified, and outputs a <outfile>.time file which
|
|
|
|
# contains a timing of the program and the program's exit code.
|
2012-02-23 09:03:03 +04:00
|
|
|
#
|
2007-05-04 01:32:39 +04:00
|
|
|
# If optional parameters -r <remote host> -l <remote user> are
|
|
|
|
# specified, it execute the program remotely using rsh.
|
|
|
|
#
|
2012-02-23 09:03:03 +04:00
|
|
|
# Syntax:
|
2006-11-25 11:56:10 +03:00
|
|
|
#
|
2015-12-17 07:33:50 +03:00
|
|
|
# RunSafely.sh [-d <workdir>] [-r <rhost>] [-l <ruser>] [-rc <client>]
|
|
|
|
# [-rp <port>] [-u <under>] [--show-errors]
|
|
|
|
# [-n [-o <stdoutfile>] [-e <stderrfile>]]
|
2015-12-05 07:39:36 +03:00
|
|
|
# -t <timeit> <timeout> <infile> <outfile> <program> <args...>
|
2006-11-25 11:56:10 +03:00
|
|
|
#
|
|
|
|
# where:
|
2015-12-05 07:39:36 +03:00
|
|
|
# <workdir> is the directory where the program is executed
|
2007-05-04 01:32:39 +04:00
|
|
|
# <rhost> is the remote host to execute the program
|
|
|
|
# <ruser> is the username on the remote host
|
2008-09-09 10:12:33 +04:00
|
|
|
# <client> is the remote client used to execute the program
|
2008-09-10 21:11:20 +04:00
|
|
|
# <port> is the port used by the remote client
|
2010-01-14 05:10:24 +03:00
|
|
|
# <under> is a wrapper that the program is run under
|
2015-12-17 07:33:50 +03:00
|
|
|
# <stdoutfile> file where standard output is written to
|
|
|
|
# <stderrfile> file where standard error output is written to
|
2012-04-17 00:29:52 +04:00
|
|
|
# <timeit> is a wrapper that is used to collect timing data
|
2006-11-25 11:56:10 +03:00
|
|
|
# <timeout> is the maximum number of seconds to let the <program> run
|
|
|
|
# <infile> is a file from which standard input is directed
|
2015-12-17 07:33:50 +03:00
|
|
|
# <outfile> is a file to which standard output and error are directed.
|
|
|
|
# If -n was specified this is just used as the basename for the
|
|
|
|
# .time file.
|
2006-11-25 11:56:10 +03:00
|
|
|
# <program> is the path to the program to run
|
|
|
|
# <args...> are the arguments to pass to the program.
|
2003-05-11 21:34:14 +04:00
|
|
|
#
|
2012-10-27 04:28:04 +04:00
|
|
|
# If --show-errors is given, then the output file will be printed if the command
|
2012-10-29 22:25:07 +04:00
|
|
|
# fails (returns a non-zero exit code).
|
2015-12-17 07:33:50 +03:00
|
|
|
# The -n switch will enable "new-style" output. This means the exit status is
|
|
|
|
# not appended to the output anymore and stdout and stderr may be redirected
|
|
|
|
# separately.
|
|
|
|
# Without -n stdout and stderr will both be written to the <outfile> followed by
|
2015-12-05 07:36:12 +03:00
|
|
|
# "exit NN" with NN being the exit status number of the program.
|
2012-10-27 04:28:04 +04:00
|
|
|
|
2006-06-07 22:57:36 +04:00
|
|
|
if [ $# -lt 4 ]; then
|
2012-10-29 04:17:18 +04:00
|
|
|
echo "./RunSafely.sh [-t <PATH>] <timeout> <infile> <outfile>" \
|
2012-03-18 23:37:58 +04:00
|
|
|
"<program> <args...>"
|
2006-11-25 11:56:10 +03:00
|
|
|
exit 1
|
2006-06-07 22:57:36 +04:00
|
|
|
fi
|
|
|
|
|
2008-10-26 01:45:37 +04:00
|
|
|
# Save a copy of the original arguments in a string before we
|
|
|
|
# clobber them with the shift command.
|
|
|
|
ORIG_ARGS="$*"
|
|
|
|
|
2006-06-07 22:57:36 +04:00
|
|
|
DIR=${0%%`basename $0`}
|
2007-05-04 01:32:39 +04:00
|
|
|
|
|
|
|
RHOST=
|
2015-11-26 06:42:38 +03:00
|
|
|
RFLAGS=""
|
2008-09-09 10:12:33 +04:00
|
|
|
RCLIENT=rsh
|
2010-01-14 05:10:24 +03:00
|
|
|
RUN_UNDER=
|
2012-03-18 23:37:58 +04:00
|
|
|
TIMEIT=
|
2012-10-27 04:28:04 +04:00
|
|
|
SHOW_ERRORS=0
|
2015-12-17 07:33:50 +03:00
|
|
|
NEW_MODE=0
|
|
|
|
STDOUT_FILE=""
|
|
|
|
STDERR_FILE=""
|
2015-12-05 07:39:36 +03:00
|
|
|
PWD=`pwd`
|
2016-02-05 22:30:02 +03:00
|
|
|
WORKDIR="$PWD"
|
2015-12-05 07:39:36 +03:00
|
|
|
if [ $1 = "-d" ]; then
|
2016-02-05 22:30:02 +03:00
|
|
|
WORKDIR="$2"
|
2015-12-05 07:39:36 +03:00
|
|
|
shift 2
|
2015-12-05 07:36:12 +03:00
|
|
|
fi
|
2007-05-04 01:32:39 +04:00
|
|
|
if [ $1 = "-r" ]; then
|
|
|
|
RHOST=$2
|
|
|
|
shift 2
|
|
|
|
fi
|
|
|
|
if [ $1 = "-l" ]; then
|
2015-11-26 06:42:38 +03:00
|
|
|
RFLAGS="$RFLAGS -l $2"
|
2007-05-04 01:32:39 +04:00
|
|
|
shift 2
|
|
|
|
fi
|
2008-09-09 10:12:33 +04:00
|
|
|
if [ $1 = "-rc" ]; then
|
|
|
|
RCLIENT=$2
|
|
|
|
shift 2
|
|
|
|
fi
|
2008-09-10 21:11:20 +04:00
|
|
|
if [ $1 = "-rp" ]; then
|
2015-11-26 06:42:38 +03:00
|
|
|
RFLAGS="$RFLAGS -p $2"
|
2008-09-09 10:12:33 +04:00
|
|
|
shift 2
|
|
|
|
fi
|
2010-01-14 05:10:24 +03:00
|
|
|
if [ $1 = "-u" ]; then
|
|
|
|
RUN_UNDER=$2
|
|
|
|
shift 2
|
|
|
|
fi
|
2012-10-27 04:28:04 +04:00
|
|
|
if [ $1 = "--show-errors" ]; then
|
|
|
|
SHOW_ERRORS=1
|
|
|
|
shift 1
|
|
|
|
fi
|
2015-12-17 07:33:50 +03:00
|
|
|
if [ $1 = "-n" ]; then
|
|
|
|
NEW_MODE=1
|
|
|
|
shift 1
|
|
|
|
if [ $1 = "-o" ]; then
|
|
|
|
STDOUT_FILE="$2"
|
|
|
|
shift 2
|
|
|
|
fi
|
|
|
|
if [ $1 = "-e" ]; then
|
|
|
|
STDERR_FILE="$2"
|
|
|
|
shift 2
|
|
|
|
fi
|
|
|
|
fi
|
2011-02-09 03:37:50 +03:00
|
|
|
if [ $1 = "-t" ]; then
|
|
|
|
TIMEIT=$2
|
|
|
|
shift 2
|
|
|
|
fi
|
2007-05-04 01:32:39 +04:00
|
|
|
|
2012-03-18 23:37:58 +04:00
|
|
|
if [ "$TIMEIT" = "" ]; then
|
|
|
|
echo "error: missing required argument -t for path to 'timeit'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-03-17 21:59:50 +04:00
|
|
|
TIMELIMIT=$1
|
2012-10-29 04:17:18 +04:00
|
|
|
INFILE=$2
|
|
|
|
OUTFILE=$3
|
|
|
|
PROGRAM=$4
|
|
|
|
shift 4
|
2003-01-19 21:07:38 +03:00
|
|
|
|
2008-11-13 06:24:49 +03:00
|
|
|
PROG=${PROGRAM}
|
2008-11-22 20:03:09 +03:00
|
|
|
if [ `basename ${PROGRAM}` = "lli" ]; then
|
2008-11-13 06:24:49 +03:00
|
|
|
PROG=`basename ${PROGRAM}`
|
|
|
|
fi
|
|
|
|
|
2015-12-17 07:19:30 +03:00
|
|
|
# Run the command, timing its execution and logging the status summary to
|
|
|
|
# $OUTFILE.time.
|
|
|
|
COMMAND="$RUN_UNDER $PROGRAM $*"
|
|
|
|
|
|
|
|
# Determine absolute paths of infiles/outfiles
|
|
|
|
INFILE="$(cd $(dirname $INFILE); pwd)/$(basename $INFILE)"
|
2015-12-17 07:33:50 +03:00
|
|
|
if [ "$STDOUT_FILE" != "" ]; then
|
|
|
|
case "$STDOUT_FILE" in
|
|
|
|
/*) STDOUT_FILE="$STDOUT_FILE";;
|
|
|
|
*) STDOUT_FILE="$PWD/$STDOUT_FILE";;
|
|
|
|
esac
|
|
|
|
fi
|
|
|
|
if [ "$STDERR_FILE" != "" ]; then
|
|
|
|
case "$STDERR_FILE" in
|
|
|
|
/*) STDERR_FILE="$STDERR_FILE";;
|
|
|
|
*) STDERR_FILE="$PWD/$STDERR_FILE";;
|
|
|
|
esac
|
|
|
|
fi
|
2015-12-17 07:19:30 +03:00
|
|
|
case "$OUTFILE" in
|
|
|
|
/*) OUTFILE="$OUTFILE";;
|
|
|
|
*) OUTFILE="$PWD/$OUTFILE";;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# Use suffix for remotely created files.
|
|
|
|
REMOTE_SUFFIX=""
|
|
|
|
if [ "x$RHOST" != x ]; then
|
|
|
|
REMOTE_SUFFIX=".remote"
|
|
|
|
fi
|
|
|
|
|
2012-10-29 04:17:21 +04:00
|
|
|
# Disable core file emission.
|
2015-12-17 07:19:30 +03:00
|
|
|
TIMEITFLAGS=""
|
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --limit-core 0"
|
2012-03-18 23:12:04 +04:00
|
|
|
|
2012-10-29 04:17:21 +04:00
|
|
|
# Set the CPU limit. We watchdog the process, so this is mostly just for
|
|
|
|
# paranoia.
|
2015-12-17 07:19:30 +03:00
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --limit-cpu $TIMELIMIT"
|
2012-03-18 23:12:04 +04:00
|
|
|
|
2012-10-29 04:17:21 +04:00
|
|
|
# To prevent infinite loops which fill up the disk, specify a limit on size
|
|
|
|
# of files being output by the tests.
|
|
|
|
#
|
|
|
|
# We set the limit at 100MB.
|
2015-12-17 07:19:30 +03:00
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --limit-file-size 104857600"
|
2008-02-15 02:46:20 +03:00
|
|
|
|
2012-10-29 04:17:21 +04:00
|
|
|
# Set the virtual memory limit at 800MB.
|
2015-12-17 07:19:30 +03:00
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --limit-rss-size 838860800"
|
2003-07-03 00:42:05 +04:00
|
|
|
|
2016-02-05 22:30:02 +03:00
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --timeout $TIMELIMIT --chdir $WORKDIR"
|
2015-12-17 07:19:30 +03:00
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --redirect-input ${INFILE}"
|
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --summary ${OUTFILE}.time${REMOTE_SUFFIX}"
|
2015-12-17 07:33:50 +03:00
|
|
|
if [ "$NEW_MODE" = "0" ]; then
|
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --redirect-output ${OUTFILE}${REMOTE_SUFFIX}"
|
|
|
|
else
|
|
|
|
if [ "$STDERR_FILE" != "" ]; then
|
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --redirect-stderr ${STDERR_FILE}${REMOTE_SUFFIX}"
|
|
|
|
fi
|
|
|
|
if [ "$STDOUT_FILE" != "" ]; then
|
|
|
|
TIMEITFLAGS="$TIMEITFLAGS --redirect-stdout ${STDOUT_FILE}${REMOTE_SUFFIX}"
|
|
|
|
fi
|
|
|
|
fi
|
2006-11-25 11:56:10 +03:00
|
|
|
|
2015-12-17 07:19:30 +03:00
|
|
|
# Run the command
|
|
|
|
rm -f "${OUTFILE}.time" "${OUTFILE}" "${STDOUT_FILE}" "${STDERR_FILE}"
|
2007-05-04 01:32:39 +04:00
|
|
|
if [ "x$RHOST" = x ] ; then
|
2015-12-17 07:19:30 +03:00
|
|
|
$TIMEIT $TIMEITFLAGS $COMMAND
|
2007-05-04 01:32:39 +04:00
|
|
|
else
|
2015-12-17 07:19:30 +03:00
|
|
|
rm -f "${OUTFILE}.time${REMOTE_SUFFIX}" "${OUTFILE}${REMOTE_SUFFIX}"
|
2015-12-17 07:33:50 +03:00
|
|
|
rm -f "${STDOUT_FILE}${REMOTE_SUFFIX}" "${STDERR_FILE}${REMOTE_SUFFIX}"
|
2015-12-17 07:19:30 +03:00
|
|
|
|
2016-02-10 06:56:41 +03:00
|
|
|
# Pass LLVM_PROFILE_FILE environment variable along
|
|
|
|
PREFIX=""
|
|
|
|
if [ "${LLVM_PROFILE_FILE}" != "" ]; then
|
|
|
|
PREFIX="LLVM_PROFILE_FILE=\"$LLVM_PROFILE_FILE\" "
|
|
|
|
fi
|
|
|
|
|
2015-12-17 07:19:30 +03:00
|
|
|
# Create .command script
|
2016-02-05 22:30:04 +03:00
|
|
|
COMMANDFILE="${OUTFILE}.command"
|
|
|
|
rm -f "${COMMANDFILE}"
|
2016-02-10 06:56:41 +03:00
|
|
|
echo "${PREFIX}${TIMEIT} $TIMEITFLAGS $COMMAND" > "${COMMANDFILE}"
|
2016-02-05 22:30:04 +03:00
|
|
|
chmod +x "${COMMANDFILE}"
|
2008-03-04 17:38:01 +03:00
|
|
|
|
2016-02-05 22:30:04 +03:00
|
|
|
( $RCLIENT $RFLAGS $RHOST "ls ${COMMANDFILE}" ) > /dev/null 2>&1
|
|
|
|
( $RCLIENT $RFLAGS $RHOST "${COMMANDFILE}" )
|
2012-03-18 23:12:04 +04:00
|
|
|
sleep 1
|
2015-12-17 07:19:30 +03:00
|
|
|
|
|
|
|
# Copy remote files back
|
|
|
|
cp -f "${OUTFILE}.time${REMOTE_SUFFIX}" "${OUTFILE}.time"
|
2016-02-04 08:14:32 +03:00
|
|
|
rm -f "${OUTFILE}.time${REMOTE_SUFFIX}"
|
2015-12-17 07:33:50 +03:00
|
|
|
if [ "$NEW_MODE" = "0" ]; then
|
|
|
|
cp -f "${OUTFILE}${REMOTE_SUFFIX}" "${OUTFILE}"
|
|
|
|
rm -f "${OUTFILE}${REMOTE_SUFFIX}"
|
|
|
|
else
|
|
|
|
if [ "$STDERR_FILE" != "" ]; then
|
|
|
|
cp -f "${STDERR_FILE}${REMOTE_SUFFIX}" "${STDERR_FILE}"
|
|
|
|
rm -f "${STDERR_FILE}${REMOTE_SUFFIX}"
|
|
|
|
fi
|
|
|
|
if [ "$STDOUT_FILE" != "" ]; then
|
|
|
|
cp -f "${STDOUT_FILE}${REMOTE_SUFFIX}" "${STDOUT_FILE}"
|
|
|
|
rm -f "${STDOUT_FILE}${REMOTE_SUFFIX}"
|
|
|
|
fi
|
|
|
|
fi
|
2007-05-04 01:32:39 +04:00
|
|
|
fi
|
2003-07-03 00:42:05 +04:00
|
|
|
|
2006-11-28 10:16:45 +03:00
|
|
|
exitval=`grep '^exit ' $OUTFILE.time | sed -e 's/^exit //'`
|
2008-10-26 01:45:37 +04:00
|
|
|
fail=yes
|
2006-11-28 10:16:45 +03:00
|
|
|
if [ -z "$exitval" ] ; then
|
|
|
|
exitval=99
|
2008-10-26 01:45:37 +04:00
|
|
|
echo "TEST $PROGRAM FAILED: CAN'T GET EXIT CODE!"
|
|
|
|
elif test "$exitval" -eq 126 ; then
|
|
|
|
echo "TEST $PROGRAM FAILED: command not executable (exit status 126)!"
|
|
|
|
elif test "$exitval" -eq 127 ; then
|
|
|
|
echo "TEST $PROGRAM FAILED: command not found (exit status 127)!"
|
|
|
|
elif test "$exitval" -eq 128 ; then
|
|
|
|
# Exit status 128 doesn't have a standard meaning, but it's unlikely
|
|
|
|
# to be expected program behavior.
|
|
|
|
echo "TEST $PROGRAM FAILED: exit status 128!"
|
|
|
|
elif test "$exitval" -gt 128 ; then
|
|
|
|
echo "TEST $PROGRAM FAILED: process terminated by signal (exit status $exitval)!"
|
2012-10-29 22:25:07 +04:00
|
|
|
elif [ "$SHOW_ERRORS" -eq 1 -a "$exitval" -ne 0 ] ; then
|
|
|
|
echo "TEST $PROGRAM FAILED: EXIT != 0"
|
2008-10-26 01:45:37 +04:00
|
|
|
else
|
|
|
|
fail=no
|
2006-11-28 10:16:45 +03:00
|
|
|
fi
|
2015-12-17 07:33:50 +03:00
|
|
|
if [ "$NEW_MODE" = "0" ]; then
|
2015-12-05 07:36:12 +03:00
|
|
|
echo "exit $exitval" >> $OUTFILE
|
|
|
|
fi
|
2006-11-28 10:16:45 +03:00
|
|
|
|
2008-10-26 01:45:37 +04:00
|
|
|
# If we detected a failure, print the name of the test executable to the
|
|
|
|
# output file. This will cause it to compare as different with other runs
|
|
|
|
# of the same test even if they fail in the same way, because they'll have
|
|
|
|
# different command names.
|
|
|
|
if [ "${fail}" != "no" ]; then
|
|
|
|
echo "RunSafely.sh detected a failure with these command-line arguments: " \
|
|
|
|
"$ORIG_ARGS" >> $OUTFILE
|
2012-10-27 04:28:04 +04:00
|
|
|
if [ "$SHOW_ERRORS" = "1" ]; then
|
|
|
|
echo "error: command failed while generating $OUTFILE"
|
|
|
|
echo "---"
|
|
|
|
cat $OUTFILE
|
|
|
|
echo "---"
|
|
|
|
fi
|
2003-07-01 22:04:43 +04:00
|
|
|
fi
|
|
|
|
|
2008-10-26 01:45:37 +04:00
|
|
|
# Always return "successful" so that tests will continue to be run.
|
|
|
|
exit 0
|