Debian Codegraph @

utils/health/valgrind-tests.sh

raw symbols SCIP
1 #!/bin/bash -e
2
3 # Copyright (c) 2014-2022, The Monero Project
4 #
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without modification, are
8 # permitted provided that the following conditions are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright notice, this list of
11 # conditions and the following disclaimer.
12 #
13 # 2. Redistributions in binary form must reproduce the above copyright notice, this list
14 # of conditions and the following disclaimer in the documentation and/or other
15 # materials provided with the distribution.
16 #
17 # 3. Neither the name of the copyright holder nor the names of its contributors may be
18 # used to endorse or promote products derived from this software without specific
19 # prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
22 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
24 # THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 # This script is able to run valgrind's callgrind, cachegrind and memcheck for a given set of executables.
32 # It expects ONE PARAMETER, which points to a file with paths to executables and their arguments, written line by line.
33
34 if [ "$#" -ne 1 ]; then
35 echo "Please provide an argument, which points to a file with paths to executables and their arguments, written line by line. For example:"
36 echo ""
37 echo "ls -l -h"
38 echo "build/tests/unit_tests/unit_tests"
39 exit 1
40 fi
41
42 FILE_IN="$1"
43 DIR_OUT="build/valgrind-output" # Using build as the base output directory, as it's ignored in .gitignore
44
45 function is_file_or_exit {
46 FILE="${1}"
47 if [ -f $FILE ]; then
48 echo "The input file $FILE exists. Can proceed."
49 else
50 echo "The input file $FILE doesn't exist."
51 exit 1
52 fi
53 return 0
54 }
55
56 function is_tool_or_exit {
57 TOOL="${1}"
58 if $(hash ${TOOL}); then
59 echo "${TOOL} is installed. Can proceed."
60 else
61 echo "Please install ${TOOL} to continue."
62 exit 1
63 fi
64 return 0
65 }
66
67 function get_tool_out_file_base {
68 EXE="${1}"
69 TOOL="${2}"
70
71 EXE_NAME=$(basename $EXE)
72 local retval="${DIR_OUT}/${EXE_NAME}-${TOOL}"
73 echo "$retval"
74 }
75
76 function get_tool_out_file {
77 EXE="${1}"
78 TOOL="${2}"
79
80 FILE_OUT_BASE=$(get_tool_out_file_base ${EXE} ${TOOL})
81 local retval="--${TOOL}-out-file=${FILE_OUT_BASE}.out"
82 echo "$retval"
83 }
84
85 function run_valgrind_4_executable {
86 EXE="${1}"
87 ARGS="${2}"
88 TOOL="${3}"
89 EXTRA_OPTS="${4}"
90 FILE_OUT_TOOL="${5}"
91 FILE_OUT_BASE=$(get_tool_out_file_base ${EXE} ${TOOL})
92
93 echo "Runnig '${TOOL}' for '${EXE}' with args '${ARGS}'"
94 echo "EXTRA_OPTS = ${EXTRA_OPTS}"
95 echo "FILE_OUT_TOOL = ${FILE_OUT_TOOL}"
96 if ! valgrind --tool=${TOOL} ${FILE_OUT_TOOL} --log-file="${FILE_OUT_BASE}.log" ${EXTRA_OPTS} ${EXE} ${ARGS}; then
97 echo "FAILED in runnig ${TOOL} for ${EXE} !"
98 fi
99 }
100
101 function run_valgrind_4_executable_callgrind {
102 EXE="${1}"
103 ARGS="${2}"
104 TOOL="callgrind"
105 EXTRA_OPTS="--dump-instr=yes --simulate-cache=yes --collect-jumps=yes"
106 FILE_OUT_TOOL=$(get_tool_out_file ${EXE} ${TOOL})
107
108 run_valgrind_4_executable ${EXE} "${ARGS}" ${TOOL} "${EXTRA_OPTS}" ${FILE_OUT_TOOL}
109 }
110
111 function run_valgrind_4_executable_cachegrind {
112 EXE="${1}"
113 ARGS="${2}"
114 TOOL="cachegrind"
115 EXTRA_OPTS=""
116 FILE_OUT_TOOL=$(get_tool_out_file ${EXE} ${TOOL})
117
118 run_valgrind_4_executable ${EXE} "${ARGS}" ${TOOL} "${EXTRA_OPTS}" ${FILE_OUT_TOOL}
119 }
120
121 function run_valgrind_4_executable_memcheck {
122 EXE="${1}"
123 ARGS="${2}"
124 TOOL="memcheck"
125 #EXTRA_OPTS="--leak-check=yes" # Minimalistic
126 EXTRA_OPTS="--leak-check=full --show-leak-kinds=all --track-origins=yes"
127 FILE_OUT_TOOL="" # memcheck has no special out file, only the log
128
129 run_valgrind_4_executable ${EXE} "${ARGS}" ${TOOL} "${EXTRA_OPTS}" ${FILE_OUT_TOOL}
130 }
131
132 function run_valgrind_4_executable_all {
133 EXE_ARGS_ARR=(${1})
134 EXE=${EXE_ARGS_ARR[0]} # First element of the array
135 ARGS=${EXE_ARGS_ARR[@]:1} # Every next element
136
137 #EXE="ls" # A quick check of the happy path
138 #EXE="nothere" # A quick check of error handling - no such executable
139 #EXE=/bin/false # A quick check of error handling - executable returned != 0
140
141 run_valgrind_4_executable_memcheck ${EXE} "${ARGS}"
142 run_valgrind_4_executable_cachegrind ${EXE} "${ARGS}"
143 run_valgrind_4_executable_callgrind ${EXE} "${ARGS}"
144 }
145
146 is_tool_or_exit valgrind
147 is_file_or_exit "$FILE_IN"
148 echo "All OK."
149 echo "Will perform checks for the following executables and their arguments:"
150 while IFS= read -r line; do
151 echo "$line"
152 done < "$FILE_IN"
153
154 mkdir -p "$DIR_OUT"
155 while IFS= read -r line; do
156 echo "$line"
157 run_valgrind_4_executable_all "$line"
158 done < "$FILE_IN"
159
160 echo "Done. All data saved in ${DIR_OUT}"
161