valgrind-condense revision 3ce85a5f5264e7118beb6524e120fd8b53a13da4
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch#!/bin/bash
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody#
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# Run Valgrind, condensing logged reports into an exit code.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody#
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# Copyright (C) 2014 Red Hat
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody#
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# This program is free software; you can redistribute it and/or modify
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# it under the terms of the GNU General Public License as published by
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# the Free Software Foundation; either version 3 of the License, or
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# (at your option) any later version.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody#
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# This program is distributed in the hope that it will be useful,
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# but WITHOUT ANY WARRANTY; without even the implied warranty of
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# GNU General Public License for more details.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody#
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# You should have received a copy of the GNU General Public License
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# along with this program. If not, see <http://www.gnu.org/licenses/>.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyset -o nounset -o pipefail -o errexit
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyshopt -s extglob
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyfunction usage()
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody{
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody cat <<EOF
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyUsage: `basename "$0"` ERROR_EXITCODE [PATH_PATTERN...] [-- VALGRIND_ARG...]
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyRun Valgrind, condensing logged reports into an exit code.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyArguments:
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody ERROR_EXITCODE An exit code to return if at least one error is found in
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody Valgrind log files.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody PATH_PATTERN An extended glob pattern matching (original) paths to
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody programs to execute under Valgrind. Execution is skipped
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody and success is returned for non-matching programs. Without
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody patterns, all programs match.
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi VALGRIND_ARG An argument to pass to Valgrind after the arguments
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody specified by `basename "$0"`.
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyThe first non-option VALGRIND_ARG will be considered the path to the program
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyto execute under Valgrind and will be used in naming Valgrind log files as
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodysuch:
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody PROGRAM_NAME.PID.valgrind.log
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodywhere PROGRAM_NAME is the filename portion of the program path and PID is the
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyexecuted process ID. If the last directory of the program path is ".libs" and
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomithe filename begins with "lt-", both are removed to match the name of libtool
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyfrontend script. All files matching PROGRAM_NAME.*.valgrind.log are removed
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodybefore invoking Valgrind.
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyIf an error is found in Valgrind log files, ERROR_EXITCODE is returned,
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyotherwise Valgrind exit code is returned.
992a1726a41b42fa47204565ff17f7c635fcb421Phil CarmodyEOF
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody}
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyif [[ $# == 0 ]]; then
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody echo "Invalid number of arguments." >&2
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody usage >&2
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody exit 1
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyfi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodydeclare error_exitcode="$1"; shift
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodydeclare -a path_pattern_list=()
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare arg
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare got_dash_dash
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare program_path
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare path_pattern
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare match
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare program_name
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydeclare status=0
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi# Extract path patterns
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodywhile [[ $# != 0 ]]; do
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody arg="$1"
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody shift
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi if [[ "$arg" == "--" ]]; then
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody break
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody else
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody path_pattern_list+=("$arg")
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody fi
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodydone
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody# Find program path argument
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodygot_dash_dash=false
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmodyfor arg in "$@"; do
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody if [[ "$arg" == "--" ]]; then
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody got_dash_dash=true
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody elif "$got_dash_dash" || [[ "$arg" != -* ]]; then
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody program_path="$arg"
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody break
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody fi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodydone
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyif [[ -z "${program_path+set}" ]]; then
e2588872c1fe79642589b805aaab9fbb6750771bTimo Sirainen echo "Program path not specified." >&2
618262376e4a087f2047e627baf008884a4085b9Timo Sirainen usage >&2
96f75b44a4950bf20273656e71e2686a508d0bd1Timo Sirainen exit 1
618262376e4a087f2047e627baf008884a4085b9Timo Sirainenfi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody# Match against path patterns, if any
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärviif [[ ${#path_pattern_list[@]} != 0 ]]; then
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody match=false
98c59517ebce19556221065e9231f007bbdd0038Timo Sirainen for path_pattern in "${path_pattern_list[@]}"; do
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody if [[ "$program_path" == $path_pattern ]]; then
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody match=true
0175d37a5ae5a4d146ca41b684bd38d9b03683cbMartti Rannanjärvi fi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody done
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody if ! $match; then
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody exit 0
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody fi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyfi
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody
f9a1f7b6034d692cbd24c1895061b3834f486501Phil Carmody# Generate original path from libtool path
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyprogram_path=`sed -e 's/^\(.*\/\)\?\.libs\/lt-\([^\/]\+\)$/\1\2/' \
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody <<<"$program_path"`
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyprogram_name=`basename "$program_path"`
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyrm -f "$program_name".*.valgrind.log
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyvalgrind --log-file="$program_name.%p.valgrind.log" "$@" || status=$?
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyif grep -q '^==[0-9]\+== *ERROR SUMMARY: *[1-9]' \
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody "$program_name".*.valgrind.log; then
3c5ee51327f075dc13cdacf46135f7f0abbdaafeTimo Sirainen exit "$error_exitcode"
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyelse
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody exit "$status"
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmodyfi
992a1726a41b42fa47204565ff17f7c635fcb421Phil Carmody