valgrind-condense revision 3ce85a5f5264e7118beb6524e120fd8b53a13da4
369N/A#!/bin/bash
369N/A#
369N/A# Run Valgrind, condensing logged reports into an exit code.
369N/A#
369N/A# Copyright (C) 2014 Red Hat
369N/A#
369N/A# This program is free software; you can redistribute it and/or modify
369N/A# it under the terms of the GNU General Public License as published by
369N/A# the Free Software Foundation; either version 3 of the License, or
369N/A# (at your option) any later version.
369N/A#
369N/A# This program is distributed in the hope that it will be useful,
369N/A# but WITHOUT ANY WARRANTY; without even the implied warranty of
369N/A# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
369N/A# GNU General Public License for more details.
369N/A#
369N/A# You should have received a copy of the GNU General Public License
369N/A# along with this program. If not, see <http://www.gnu.org/licenses/>.
369N/A
369N/Aset -o nounset -o pipefail -o errexit
5810N/Ashopt -s extglob
369N/A
3661N/Afunction usage()
3661N/A{
3661N/A cat <<EOF
3661N/AUsage: `basename "$0"` ERROR_EXITCODE [PATH_PATTERN...] [-- VALGRIND_ARG...]
3661N/ARun Valgrind, condensing logged reports into an exit code.
3661N/A
3661N/AArguments:
3661N/A ERROR_EXITCODE An exit code to return if at least one error is found in
3661N/A Valgrind log files.
3661N/A PATH_PATTERN An extended glob pattern matching (original) paths to
3661N/A programs to execute under Valgrind. Execution is skipped
3661N/A and success is returned for non-matching programs. Without
3661N/A patterns, all programs match.
3661N/A VALGRIND_ARG An argument to pass to Valgrind after the arguments
3661N/A specified by `basename "$0"`.
369N/A
4561N/AThe first non-option VALGRIND_ARG will be considered the path to the program
4561N/Ato execute under Valgrind and will be used in naming Valgrind log files as
4561N/Asuch:
4561N/A
4561N/A PROGRAM_NAME.PID.valgrind.log
4561N/A
3996N/Awhere PROGRAM_NAME is the filename portion of the program path and PID is the
369N/Aexecuted process ID. If the last directory of the program path is ".libs" and
369N/Athe filename begins with "lt-", both are removed to match the name of libtool
369N/Afrontend script. All files matching PROGRAM_NAME.*.valgrind.log are removed
369N/Abefore invoking Valgrind.
1273N/A
369N/AIf an error is found in Valgrind log files, ERROR_EXITCODE is returned,
369N/Aotherwise Valgrind exit code is returned.
618N/AEOF
5810N/A}
618N/A
4561N/A
4561N/Aif [[ $# == 0 ]]; then
4561N/A echo "Invalid number of arguments." >&2
369N/A usage >&2
369N/A exit 1
4561N/Afi
369N/A
1574N/Adeclare error_exitcode="$1"; shift
1574N/Adeclare -a path_pattern_list=()
1574N/Adeclare arg
1574N/Adeclare got_dash_dash
1574N/Adeclare program_path
369N/Adeclare path_pattern
369N/Adeclare match
369N/Adeclare program_name
369N/Adeclare status=0
369N/A
369N/A# Extract path patterns
369N/Awhile [[ $# != 0 ]]; do
369N/A arg="$1"
369N/A shift
369N/A if [[ "$arg" == "--" ]]; then
369N/A break
369N/A else
369N/A path_pattern_list+=("$arg")
369N/A fi
369N/Adone
369N/A
369N/A# Find program path argument
369N/Agot_dash_dash=false
369N/Afor arg in "$@"; do
369N/A if [[ "$arg" == "--" ]]; then
369N/A got_dash_dash=true
369N/A elif "$got_dash_dash" || [[ "$arg" != -* ]]; then
369N/A program_path="$arg"
369N/A break
369N/A fi
369N/Adone
369N/A
369N/Aif [[ -z "${program_path+set}" ]]; then
369N/A echo "Program path not specified." >&2
369N/A usage >&2
369N/A exit 1
369N/Afi
369N/A
369N/A# Match against path patterns, if any
5810N/Aif [[ ${#path_pattern_list[@]} != 0 ]]; then
369N/A match=false
369N/A for path_pattern in "${path_pattern_list[@]}"; do
3996N/A if [[ "$program_path" == $path_pattern ]]; then
3996N/A match=true
3996N/A fi
3996N/A done
3996N/A if ! $match; then
4994N/A exit 0
4994N/A fi
3996N/Afi
3996N/A
# Generate original path from libtool path
program_path=`sed -e 's/^\(.*\/\)\?\.libs\/lt-\([^\/]\+\)$/\1\2/' \
<<<"$program_path"`
program_name=`basename "$program_path"`
rm -f "$program_name".*.valgrind.log
valgrind --log-file="$program_name.%p.valgrind.log" "$@" || status=$?
if grep -q '^==[0-9]\+== *ERROR SUMMARY: *[1-9]' \
"$program_name".*.valgrind.log; then
exit "$error_exitcode"
else
exit "$status"
fi