3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# Run Valgrind, condensing logged reports into an exit code.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# Copyright (C) 2014 Red Hat
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# This program is free software; you can redistribute it and/or modify
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# it under the terms of the GNU General Public License as published by
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# the Free Software Foundation; either version 3 of the License, or
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# (at your option) any later version.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# This program is distributed in the hope that it will be useful,
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# but WITHOUT ANY WARRANTY; without even the implied warranty of
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# GNU General Public License for more details.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# You should have received a copy of the GNU General Public License
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# along with this program. If not, see <http://www.gnu.org/licenses/>.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai KondrashovUsage: `basename "$0"` ERROR_EXITCODE [PATH_PATTERN...] [-- VALGRIND_ARG...]
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai KondrashovRun Valgrind, condensing logged reports into an exit code.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov ERROR_EXITCODE An exit code to return if at least one error is found in
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov Valgrind log files.
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov PATH_PATTERN An extended glob pattern matching the (original) path to
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov the program to execute under Valgrind. If the program path
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov doesn't match any patterns, the program is executed
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov directly, without Valgrind. Without patterns any program
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov VALGRIND_ARG An argument to pass to Valgrind after the arguments
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov specified by `basename "$0"`.
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai KondrashovThe first non-option VALGRIND_ARG, or the first VALGRIND_ARG after a "--",
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashovwill be considered the path to the program to execute under Valgrind and will
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashovbe used in naming Valgrind log files as such:
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov PROGRAM_NAME.PID.valgrind.log
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovwhere PROGRAM_NAME is the filename portion of the program path and PID is the
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovexecuted process ID. If the last directory of the program path is ".libs" and
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovthe filename begins with "lt-", both are removed to match the name of libtool
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovfrontend script. All files matching PROGRAM_NAME.*.valgrind.log are removed
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovbefore invoking Valgrind.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai KondrashovIf an error is found in Valgrind log files, ERROR_EXITCODE is returned,
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovotherwise Valgrind exit code is returned.
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovif [[ $# == 0 ]]; then
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# Extract path patterns
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashovwhile [[ $# != 0 ]]; do
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov# Find program argv list in Valgrind arguments
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov if ! "$collecting_argv" && [[ "$arg" == "--" ]]; then
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov elif "$collecting_argv" || [[ "$arg" != -* ]]; then
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashovif [[ ${#program_argv[@]} == 0 ]]; then
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov# Match against path patterns, if any
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashovif [[ ${#path_pattern_list[@]} == 0 ]]; then
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov for path_pattern in "${path_pattern_list[@]}"; do
3ce85a5f5264e7118beb6524e120fd8b53a13da4Nikolai Kondrashov if [[ "$program_path" == $path_pattern ]]; then
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov# Run the program
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov # Generate original path from libtool path
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov program_path=`sed -e 's/^\(.*\/\)\?\.libs\/lt-\([^\/]\+\)$/\1\2/' \
96544fca522e66b4f69b4252854a5f672c96f9c4Nikolai Kondrashov program_name=`basename -- "$program_path"`
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov valgrind --log-file="$program_name.%p.valgrind.log" "$@" || status=$?
96544fca522e66b4f69b4252854a5f672c96f9c4Nikolai Kondrashov if grep -q '^==[0-9]\+== *ERROR SUMMARY: *[1-9]' -- \
5f4f0428c182a9e77d29b39f3749fce03643ac8dNikolai Kondrashov "${program_argv[@]}"