valgrind-condense revision 5f4f0428c182a9e77d29b39f3749fce03643ac8d
f743002678eb67b99bbc29fee116b65d9530fec0wrowe#!/bin/bash
80833bb9a1bf25dcf19e814438a4b311d2e1f4cffuankg#
a34684a59b60a4173c25035d0c627ef17e6dc215rpluem# Run Valgrind, condensing logged reports into an exit code.
35006c658c32693022c67b70a3fcf53399e36bd7covener#
35006c658c32693022c67b70a3fcf53399e36bd7covener# Copyright (C) 2014 Red Hat
35006c658c32693022c67b70a3fcf53399e36bd7covener#
35006c658c32693022c67b70a3fcf53399e36bd7covener# This program is free software; you can redistribute it and/or modify
35006c658c32693022c67b70a3fcf53399e36bd7covener# it under the terms of the GNU General Public License as published by
4330364fc1db390a69cc84d4d0f62138bcd5bf80covener# the Free Software Foundation; either version 3 of the License, or
4330364fc1db390a69cc84d4d0f62138bcd5bf80covener# (at your option) any later version.
4330364fc1db390a69cc84d4d0f62138bcd5bf80covener#
4330364fc1db390a69cc84d4d0f62138bcd5bf80covener# This program is distributed in the hope that it will be useful,
dd1e093863f13a4a3fbf09497cbd5cb039fadd76ylavic# but WITHOUT ANY WARRANTY; without even the implied warranty of
dd1e093863f13a4a3fbf09497cbd5cb039fadd76ylavic# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
dd1e093863f13a4a3fbf09497cbd5cb039fadd76ylavic# GNU General Public License for more details.
dd1e093863f13a4a3fbf09497cbd5cb039fadd76ylavic#
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic# You should have received a copy of the GNU General Public License
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic# along with this program. If not, see <http://www.gnu.org/licenses/>.
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavicset -o nounset -o pipefail -o errexit
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavicshopt -s extglob
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavic
330e16bea8fe9cace4de90c349750c03dfb1fe64ylavicfunction usage()
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener{
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener cat <<EOF
d7205b1a86c51c27b71a2c458dc453fd53a261c1covenerUsage: `basename "$0"` ERROR_EXITCODE [PATH_PATTERN...] [-- VALGRIND_ARG...]
d7205b1a86c51c27b71a2c458dc453fd53a261c1covenerRun Valgrind, condensing logged reports into an exit code.
d7205b1a86c51c27b71a2c458dc453fd53a261c1covener
44ff304057225e944e220e981d434a046d14cf06covenerArguments:
44ff304057225e944e220e981d434a046d14cf06covener ERROR_EXITCODE An exit code to return if at least one error is found in
44ff304057225e944e220e981d434a046d14cf06covener Valgrind log files.
44ff304057225e944e220e981d434a046d14cf06covener PATH_PATTERN An extended glob pattern matching the (original) path to
ea30bfa68d711e27206df00abb140174b4e65ed7ylavic the program to execute under Valgrind. If the program path
ea30bfa68d711e27206df00abb140174b4e65ed7ylavic doesn't match any patterns, the program is executed
ea30bfa68d711e27206df00abb140174b4e65ed7ylavic directly, without Valgrind. Without patterns any program
ea30bfa68d711e27206df00abb140174b4e65ed7ylavic path matches.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin VALGRIND_ARG An argument to pass to Valgrind after the arguments
291eb44b3adaf8247425286615b4f4b69fbea274minfrin specified by `basename "$0"`.
291eb44b3adaf8247425286615b4f4b69fbea274minfrin
5d1ba75b8794925e67591c209085a49279791de9covenerThe first non-option VALGRIND_ARG, or the first VALGRIND_ARG after a "--",
5d1ba75b8794925e67591c209085a49279791de9covenerwill be considered the path to the program to execute under Valgrind and will
5d1ba75b8794925e67591c209085a49279791de9covenerbe used in naming Valgrind log files as such:
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand PROGRAM_NAME.PID.valgrind.log
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrand
032982212dbcc7c3cce95bf89c503bb56e185ac7kbrandwhere PROGRAM_NAME is the filename portion of the program path and PID is the
caad2986f81ab263f7af41467dd622dc9add17f3ylavicexecuted process ID. If the last directory of the program path is ".libs" and
caad2986f81ab263f7af41467dd622dc9add17f3ylavicthe filename begins with "lt-", both are removed to match the name of libtool
caad2986f81ab263f7af41467dd622dc9add17f3ylavicfrontend script. All files matching PROGRAM_NAME.*.valgrind.log are removed
caad2986f81ab263f7af41467dd622dc9add17f3ylavicbefore invoking Valgrind.
45a10d38e6051fd7bdf9d742aaae633d97ff02abjailletc
f7317ff316c2b141feea31bddb74d5d3fa1584edjortonIf an error is found in Valgrind log files, ERROR_EXITCODE is returned,
f7317ff316c2b141feea31bddb74d5d3fa1584edjortonotherwise Valgrind exit code is returned.
939a5386274c80af51ee9fff4b0ca29f0c799da5covenerEOF
939a5386274c80af51ee9fff4b0ca29f0c799da5covener}
939a5386274c80af51ee9fff4b0ca29f0c799da5covener
939a5386274c80af51ee9fff4b0ca29f0c799da5covener
4472df9a373740420f81c4c14a331b54d2f91603covenerif [[ $# == 0 ]]; then
4472df9a373740420f81c4c14a331b54d2f91603covener echo "Invalid number of arguments." >&2
4472df9a373740420f81c4c14a331b54d2f91603covener usage >&2
2165214331e4afafca4048f66f303d0253d7b001covener exit 1
a34684a59b60a4173c25035d0c627ef17e6dc215rpluemfi
a34684a59b60a4173c25035d0c627ef17e6dc215rpluem
08e054046d0c7e5532c66769ba80c69a7b4d8245ylavicdeclare error_exitcode="$1"; shift
08e054046d0c7e5532c66769ba80c69a7b4d8245ylavicdeclare -a path_pattern_list=()
08e054046d0c7e5532c66769ba80c69a7b4d8245ylavicdeclare arg
9bfe773a084210dd794672fbfd3d6d401d7fe122ylavicdeclare collecting_argv
9bfe773a084210dd794672fbfd3d6d401d7fe122ylavicdeclare -a program_argv=()
9bfe773a084210dd794672fbfd3d6d401d7fe122ylavicdeclare program_path
1e2d421a36999d292042a5539971070d54aa6c63ylavicdeclare program_name
1e2d421a36999d292042a5539971070d54aa6c63ylavicdeclare path_pattern
1e2d421a36999d292042a5539971070d54aa6c63ylavicdeclare match
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedoohdeclare status=0
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedooh
fa7ed98b9dc94c5845cf845aea0a44ecacd290c9humbedooh# Extract path patterns
0b67eb8568cd58bb77082703951679b42cf098actrawickwhile [[ $# != 0 ]]; do
0b67eb8568cd58bb77082703951679b42cf098actrawick arg="$1"
0b67eb8568cd58bb77082703951679b42cf098actrawick shift
0b67eb8568cd58bb77082703951679b42cf098actrawick if [[ "$arg" == "--" ]]; then
06bb3e11d3e997937534ae7bd45b3631d3b5e5bacovener break
06bb3e11d3e997937534ae7bd45b3631d3b5e5bacovener else
2165214331e4afafca4048f66f303d0253d7b001covener path_pattern_list+=("$arg")
06bb3e11d3e997937534ae7bd45b3631d3b5e5bacovener fi
5ef3c61605a3a021ff71f488983cb0065f8e1a79covenerdone
fb1985a97912b25ec6564c73e610a31e5fc6e25fcovener
09c87c777bed1655621bb20e1c46cb6b1a63279dcovener# Find program argv list in Valgrind arguments
cf8b985ec0a63b15a1c8f2990d96009a11e0d68ecovenercollecting_argv=false
cf8b985ec0a63b15a1c8f2990d96009a11e0d68ecovenerfor arg in "$@"; do
cf8b985ec0a63b15a1c8f2990d96009a11e0d68ecovener if ! "$collecting_argv" && [[ "$arg" == "--" ]]; then
6bbcfe3fb8489d6e87770d37d97b7a5cd4fabceeylavic collecting_argv=true
6bbcfe3fb8489d6e87770d37d97b7a5cd4fabceeylavic elif "$collecting_argv" || [[ "$arg" != -* ]]; then
6bbcfe3fb8489d6e87770d37d97b7a5cd4fabceeylavic collecting_argv=true
6bbcfe3fb8489d6e87770d37d97b7a5cd4fabceeylavic program_argv+=("$arg")
6502b7b32f980cc2093bb3ebce37e5e4dc68fba4ylavic fi
6502b7b32f980cc2093bb3ebce37e5e4dc68fba4ylavicdone
3060ce7f798fbda7999cd4ddf89b525d2b294185covener
c85eff31536e6bfef1537b2435564d48665435d3rpluemif [[ ${#program_argv[@]} == 0 ]]; then
c85eff31536e6bfef1537b2435564d48665435d3rpluem echo "Program path not specified." >&2
c85eff31536e6bfef1537b2435564d48665435d3rpluem usage >&2
c85eff31536e6bfef1537b2435564d48665435d3rpluem exit 1
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavicfi
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavicprogram_path="${program_argv[0]}"
c1a63b8fad09c419c1a64f75993feb8a343a6801ylavic
e6b4bd1113567627ab6bb6c6a7105e1e01a7d889jailletc# Match against path patterns, if any
e6b4bd1113567627ab6bb6c6a7105e1e01a7d889jailletcif [[ ${#path_pattern_list[@]} == 0 ]]; then
e466c40e1801982602ee0200c9e8b61cc148742djailletc match=true
e466c40e1801982602ee0200c9e8b61cc148742djailletcelse
457468b82e59d01eba00dd9d0817309c8f5e414ejim match=false
457468b82e59d01eba00dd9d0817309c8f5e414ejim for path_pattern in "${path_pattern_list[@]}"; do
457468b82e59d01eba00dd9d0817309c8f5e414ejim if [[ "$program_path" == $path_pattern ]]; then
04983e3bd1754764eec7d6bb772fe3b0bf391771jorton match=true
04983e3bd1754764eec7d6bb772fe3b0bf391771jorton fi
15890c9306ba98f6fc243e15a3c4778ddc7d773erpluem done
15660979a30d251681463de2e0584853890082accovenerfi
15660979a30d251681463de2e0584853890082accovener
15660979a30d251681463de2e0584853890082accovener# Run the program
15660979a30d251681463de2e0584853890082accovenerif $match; then
cfd9415521847b2f9394fad04fb701cfb955f503rjung # Generate original path from libtool path
cfd9415521847b2f9394fad04fb701cfb955f503rjung program_path=`sed -e 's/^\(.*\/\)\?\.libs\/lt-\([^\/]\+\)$/\1\2/' \
cfd9415521847b2f9394fad04fb701cfb955f503rjung <<<"$program_path"`
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe program_name=`basename "$program_path"`
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe rm -f "$program_name".*.valgrind.log
28c31fb73c1264bd1d0ff932573677030b024c7dwrowe valgrind --log-file="$program_name.%p.valgrind.log" "$@" || status=$?
8491e0600f69b0405e156ea8a419653c065c645bcovener
63b9f1f5880391261705f696d7d65507bbe9ace3covener if grep -q '^==[0-9]\+== *ERROR SUMMARY: *[1-9]' \
63b9f1f5880391261705f696d7d65507bbe9ace3covener "$program_name".*.valgrind.log; then
63b9f1f5880391261705f696d7d65507bbe9ace3covener exit "$error_exitcode"
87a26948305eab2bab8a4fb3f2a21f6725055790covener else
87a26948305eab2bab8a4fb3f2a21f6725055790covener exit "$status"
87a26948305eab2bab8a4fb3f2a21f6725055790covener fi
4efd27d2bd53a819a194f8a942f8881c1927755eylavicelse
4efd27d2bd53a819a194f8a942f8881c1927755eylavic "${program_argv[@]}"
4efd27d2bd53a819a194f8a942f8881c1927755eylavicfi
4efd27d2bd53a819a194f8a942f8881c1927755eylavic