blob: c8e1d2b71e0bef0306fd81a67f2cbcd5eb6a9b45 [file] [log] [blame]
njn9fb16f52003-04-23 17:47:13 +00001#! @PERL@
njna217f3d2002-09-27 08:26:27 +00002##--------------------------------------------------------------------##
3##--- Valgrind regression testing script vg_regtest ---##
4##--------------------------------------------------------------------##
njnc2e7f482002-09-27 08:44:17 +00005
njnb9c427c2004-12-01 14:14:42 +00006# This file is part of Valgrind, a dynamic binary instrumentation
7# framework.
njnc2e7f482002-09-27 08:44:17 +00008#
florian06766bd2014-06-23 19:33:45 +00009# Copyright (C) 2003-2013 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000010# njn@valgrind.org
njnc2e7f482002-09-27 08:44:17 +000011#
12# This program is free software; you can redistribute it and/or
13# modify it under the terms of the GNU General Public License as
14# published by the Free Software Foundation; either version 2 of the
15# License, or (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful, but
18# WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20# General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25# 02111-1307, USA.
26#
27# The GNU General Public License is contained in the file COPYING.
28
29#----------------------------------------------------------------------------
njna217f3d2002-09-27 08:26:27 +000030# usage: vg_regtest [options] <dirs | files>
njn25e49d8e72002-09-23 09:36:25 +000031#
njna217f3d2002-09-27 08:26:27 +000032# Options:
njna217f3d2002-09-27 08:26:27 +000033# --all: run tests in all subdirs
njn3ed89602006-10-23 18:38:35 +000034# --valgrind: valgrind launcher to use. Default is ./coregrind/valgrind.
35# (This option should probably only be used in conjunction with
36# --valgrind-lib.)
37# --valgrind-lib: valgrind libraries to use. Default is $tests_dir/.in_place.
38# (This option should probably only be used in conjunction with
39# --valgrind.)
sewardjeefeeb72011-05-10 11:01:07 +000040# --keep-unfiltered: keep a copy of the unfiltered output/error output
41# of each test by adding an extension .unfiltered.out
njna217f3d2002-09-27 08:26:27 +000042#
philippe72faf102012-03-11 22:24:03 +000043# --outer-valgrind: run this valgrind under the given outer valgrind.
44# This valgrind must be configured with --enable-inner.
45# --outer-tool: tool to use by the outer valgrind (default memcheck).
46# --outer-args: use this as outer tool args.
philippe69e8f172015-03-14 18:29:35 +000047# --loop-till-fail: loops on the test(s) till one fail, then exit
48# This is useful to obtain detailed trace or --keep-unfiltered
49# output of a non deterministic test failure
philippe72faf102012-03-11 22:24:03 +000050#
njn4ba5a792002-09-30 10:23:54 +000051# The easiest way is to run all tests in valgrind/ with (assuming you installed
52# in $PREFIX):
53#
54# $PREFIX/bin/vg_regtest --all
55#
njna217f3d2002-09-27 08:26:27 +000056# You can specify individual files to test, or whole directories, or both.
jsgf855d93d2003-10-13 22:26:55 +000057# Directories are traversed recursively, except for ones named, for example,
58# CVS/ or docs/.
njn25e49d8e72002-09-23 09:36:25 +000059#
60# Each test is defined in a file <test>.vgtest, containing one or more of the
njna217f3d2002-09-27 08:26:27 +000061# following lines, in any order:
florian8d228442015-04-02 22:02:24 +000062# - prog: <prog to run>
63# - prog-asis: <prog to run>
64# - env: <environment variable for prog> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000065# - args: <args for prog> (default: none)
njnf6b00762009-04-17 04:26:41 +000066# - vgopts: <Valgrind options> (default: none;
67# multiple are allowed)
njn25e49d8e72002-09-23 09:36:25 +000068# - stdout_filter: <filter to run stdout through> (default: none)
njna217f3d2002-09-27 08:26:27 +000069# - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
florian31014da2011-09-26 00:29:44 +000070# - stdout_filter_args: <args for stdout_filter> (default: basename of .vgtest file)
71# - stderr_filter_args: <args for stderr_filter> (default: basename of .vgtest file)
sewardj3b290482011-05-06 21:02:55 +000072#
73# - progB: <prog to run in parallel with prog> (default: none)
74# - argsB: <args for progB> (default: none)
75# - stdinB: <input file for progB> (default: none)
76# - stdoutB_filter: <filter progB stdout through> (default: none)
77# - stderrB_filter: <filter progB stderr through> (default: ./filter_stderr)
florian31014da2011-09-26 00:29:44 +000078# - stdoutB_filter_args: <args for stdout_filterB> (default: basename of .vgtest file)
79# - stderrB_filter_args: <args for stderr_filterB> (default: basename of .vgtest file)
sewardj3b290482011-05-06 21:02:55 +000080#
nethercotec5e1d802004-11-18 12:48:17 +000081# - prereq: <prerequisite command> (default: none)
njn734b8052007-11-01 04:40:37 +000082# - post: <post-test check command> (default: none)
83# - cleanup: <post-test cleanup cmd> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000084#
florian8d228442015-04-02 22:02:24 +000085# One of prog or prog-asis must be specified.
sewardj3b290482011-05-06 21:02:55 +000086# If prog or probB is a relative path, it will be prefix with the test directory.
florian8d228442015-04-02 22:02:24 +000087# prog-asis will be taken as is, i.e. not prefixed with the test directory.
njna217f3d2002-09-27 08:26:27 +000088# Note that filters are necessary for stderr results to filter out things that
89# always change, eg. process id numbers.
sewardj3b290482011-05-06 21:02:55 +000090# Note that if a progB is specified, it is started in background (before prog).
njn25e49d8e72002-09-23 09:36:25 +000091#
florian8d228442015-04-02 22:02:24 +000092# There can be more than one env: declaration. Here is an example:
93# env: PATH=/opt/bin:$PATH
94#
njn1ee25f02008-02-27 18:10:19 +000095# Expected stdout (filtered) is kept in <test>.stdout.exp* (can be more
nethercote4592db62004-02-29 01:31:09 +000096# than one expected output). It can be missing if it would be empty. Expected
sewardj79e88da2007-11-09 23:29:46 +000097# stderr (filtered) is kept in <test>.stderr.exp*. There must be at least
njne8ffe522008-03-02 22:48:14 +000098# one stderr.exp* file. Any .exp* file that ends in '~' or '#' is ignored;
99# this is because Emacs creates temporary files of these names.
njn25e49d8e72002-09-23 09:36:25 +0000100#
sewardj3b290482011-05-06 21:02:55 +0000101# Expected output for progB is handled similarly, except that
102# expected stdout and stderr for progB are in <test>.stdoutB.exp*
103# and <test>.stderrB.exp*.
104#
njn25e49d8e72002-09-23 09:36:25 +0000105# If results don't match, the output can be found in <test>.std<strm>.out,
njn1ee25f02008-02-27 18:10:19 +0000106# and the diff between expected and actual in <test>.std<strm>.diff*.
sewardj3b290482011-05-06 21:02:55 +0000107# (for progB, in <test>.std<strm>2.out and <test>.std<strm>2.diff*).
njn25e49d8e72002-09-23 09:36:25 +0000108#
njn61485ab2009-03-04 04:15:16 +0000109# The prerequisite command, if present, works like this:
110# - if it returns 0 the test is run
111# - if it returns 1 the test is skipped
112# - if it returns anything else the script aborts.
113# The idea here is results other than 0 or 1 are likely to be due to
114# problems with the commands, and you don't want to conflate them with the 1
115# case, which would happen if you just tested for zero or non-zero.
116#
117# The post-test command, if present, must return 0 and its stdout must match
118# the expected stdout which is kept in <test>.post.exp*.
119#
sewardj91368992006-05-26 00:13:21 +0000120# Sometimes it is useful to run all the tests at a high sanity check
121# level or with arbitrary other flags. To make this simple, extra
122# options, applied to all tests run, are read from $EXTRA_REGTEST_OPTS,
123# and handed to valgrind prior to any other flags specified by the
124# .vgtest file.
125#
njn7358d522006-11-26 22:49:58 +0000126# Some more notes on adding regression tests for a new tool are in
127# docs/xml/manual-writing-tools.xml.
njn25e49d8e72002-09-23 09:36:25 +0000128#----------------------------------------------------------------------------
129
njn9fb16f52003-04-23 17:47:13 +0000130use warnings;
njn25e49d8e72002-09-23 09:36:25 +0000131use strict;
132
133#----------------------------------------------------------------------------
134# Global vars
135#----------------------------------------------------------------------------
sewardja8162b22007-11-30 21:24:05 +0000136my $usage="\n"
sewardjeefeeb72011-05-10 11:01:07 +0000137 . "Usage:\n"
philippe72faf102012-03-11 22:24:03 +0000138 . " vg_regtest [--all, --valgrind, --valgrind-lib, --keep-unfiltered\n"
philippe69e8f172015-03-14 18:29:35 +0000139 . " --outer-valgrind, --outer-tool, --outer-args\n"
140 . " --loop-till-fail]\n"
sewardjeefeeb72011-05-10 11:01:07 +0000141 . " Use EXTRA_REGTEST_OPTS to supply extra args for all tests\n"
142 . "\n";
njn25e49d8e72002-09-23 09:36:25 +0000143
144my $tmp="vg_regtest.tmp.$$";
145
146# Test variables
147my $vgopts; # valgrind options
148my $prog; # test prog
149my $args; # test prog args
150my $stdout_filter; # filter program to run stdout results file through
151my $stderr_filter; # filter program to run stderr results file through
florian31014da2011-09-26 00:29:44 +0000152my $stdout_filter_args; # arguments passed to stdout_filter
153my $stderr_filter_args; # arguments passed to stderr_filter
sewardj3b290482011-05-06 21:02:55 +0000154my $progB; # Same but for progB
155my $argsB; #
156my $stdoutB_filter; #
157my $stderrB_filter; #
florian31014da2011-09-26 00:29:44 +0000158my $stdoutB_filter_args;# arguments passed to stdout_filterB
159my $stderrB_filter_args;# arguments passed to stderr_filterB
sewardj3b290482011-05-06 21:02:55 +0000160my $stdinB; # Input file for progB
nethercotec5e1d802004-11-18 12:48:17 +0000161my $prereq; # prerequisite test to satisfy before running test
njn734b8052007-11-01 04:40:37 +0000162my $post; # check command after running test
nethercote64bc5af2004-11-18 11:57:00 +0000163my $cleanup; # cleanup command to run
florian8d228442015-04-02 22:02:24 +0000164my @env = (); # environment variable to set prior calling $prog
njn25e49d8e72002-09-23 09:36:25 +0000165
166my @failures; # List of failed tests
167
njn9fb16f52003-04-23 17:47:13 +0000168my $num_tests_done = 0;
sewardj3b290482011-05-06 21:02:55 +0000169my %num_failures = (stderr => 0, stdout => 0,
170 stderrB => 0, stdoutB => 0,
171 post => 0);
njn25e49d8e72002-09-23 09:36:25 +0000172
njn71fe3e62003-04-23 21:48:20 +0000173# Default valgrind to use is this build tree's (uninstalled) one
njn71fe3e62003-04-23 21:48:20 +0000174my $valgrind = "./coregrind/valgrind";
njn25e49d8e72002-09-23 09:36:25 +0000175
176chomp(my $tests_dir = `pwd`);
177
philippe72faf102012-03-11 22:24:03 +0000178# Outer valgrind to use, and args to use for it.
philippe72faf102012-03-11 22:24:03 +0000179my $outer_valgrind;
180my $outer_tool = "memcheck";
181my $outer_args;
182
njn3ed89602006-10-23 18:38:35 +0000183my $valgrind_lib = "$tests_dir/.in_place";
sewardjeefeeb72011-05-10 11:01:07 +0000184my $keepunfiltered = 0;
philippe69e8f172015-03-14 18:29:35 +0000185my $looptillfail = 0;
njn3ed89602006-10-23 18:38:35 +0000186
njn25e49d8e72002-09-23 09:36:25 +0000187# default filter is the one named "filter_stderr" in the test's directory
188my $default_stderr_filter = "filter_stderr";
189
190
191#----------------------------------------------------------------------------
192# Process command line, setup
193#----------------------------------------------------------------------------
194
195# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
196#
197# 1. Can prepend "." onto programs to avoid trouble with users who don't have
198# "." in their path (by making $dir = ".")
199# 2. Can prepend the current dir to make the command absolute to avoid
200# subsequent trouble when we change directories.
201#
202# Also checks the program exists and is executable.
nethercotef4928da2004-06-15 10:54:40 +0000203sub validate_program ($$$$)
njn25e49d8e72002-09-23 09:36:25 +0000204{
nethercotef4928da2004-06-15 10:54:40 +0000205 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000206
207 # If absolute path, leave it alone. If relative, make it
208 # absolute -- by prepending current dir -- so we can change
209 # dirs and still use it.
210 $prog = "$dir/$prog" if ($prog !~ /^\//);
nethercotef4928da2004-06-15 10:54:40 +0000211 if ($must_exist) {
212 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
213 }
njn71fe3e62003-04-23 21:48:20 +0000214 if ($must_be_executable) {
nethercotef4928da2004-06-15 10:54:40 +0000215 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
njn71fe3e62003-04-23 21:48:20 +0000216 }
njn25e49d8e72002-09-23 09:36:25 +0000217
218 return $prog;
219}
220
221sub process_command_line()
222{
223 my $alldirs = 0;
224 my @fs;
225
226 for my $arg (@ARGV) {
227 if ($arg =~ /^-/) {
njnd8ced862003-04-08 00:47:05 +0000228 if ($arg =~ /^--all$/) {
njn25e49d8e72002-09-23 09:36:25 +0000229 $alldirs = 1;
230 } elsif ($arg =~ /^--valgrind=(.*)$/) {
231 $valgrind = $1;
philippe72faf102012-03-11 22:24:03 +0000232 } elsif ($arg =~ /^--outer-valgrind=(.*)$/) {
233 $outer_valgrind = $1;
234 } elsif ($arg =~ /^--outer-tool=(.*)$/) {
235 $outer_tool = $1;
236 } elsif ($arg =~ /^--outer-args=(.*)$/) {
237 $outer_args = $1;
njn3ed89602006-10-23 18:38:35 +0000238 } elsif ($arg =~ /^--valgrind-lib=(.*)$/) {
239 $valgrind_lib = $1;
sewardjeefeeb72011-05-10 11:01:07 +0000240 } elsif ($arg =~ /^--keep-unfiltered$/) {
241 $keepunfiltered = 1;
philippe69e8f172015-03-14 18:29:35 +0000242 } elsif ($arg =~ /^--loop-till-fail$/) {
243 $looptillfail = 1;
njn25e49d8e72002-09-23 09:36:25 +0000244 } else {
245 die $usage;
246 }
247 } else {
248 push(@fs, $arg);
249 }
250 }
nethercotef4928da2004-06-15 10:54:40 +0000251 $valgrind = validate_program($tests_dir, $valgrind, 1, 0);
philippe72faf102012-03-11 22:24:03 +0000252
253 if (defined $outer_valgrind) {
philippe14ab1a32012-04-08 19:52:38 +0000254 $outer_valgrind = validate_program($tests_dir, $outer_valgrind, 1, 1);
philippe72faf102012-03-11 22:24:03 +0000255 if (not defined $outer_args) {
256 $outer_args =
philippe14ab1a32012-04-08 19:52:38 +0000257 " --command-line-only=yes"
philippe72faf102012-03-11 22:24:03 +0000258 . " --run-libc-freeres=no --sim-hints=enable-outer"
philippe14ab1a32012-04-08 19:52:38 +0000259 . " --smc-check=all-non-file"
philippe72faf102012-03-11 22:24:03 +0000260 . " --vgdb=no --trace-children=yes --read-var-info=no"
philippe0a961fc2014-06-25 23:27:08 +0000261 . " --read-inline-info=yes"
philippe72faf102012-03-11 22:24:03 +0000262 . " --suppressions="
263 . validate_program($tests_dir,"./tests/outer_inner.supp",1,0)
264 . " --memcheck:leak-check=full --memcheck:show-reachable=no"
265 . " ";
266 }
267 }
njn25e49d8e72002-09-23 09:36:25 +0000268
269 if ($alldirs) {
270 @fs = ();
271 foreach my $f (glob "*") {
272 push(@fs, $f) if (-d $f);
273 }
274 }
275
276 (0 != @fs) or die "No test files or directories specified\n";
277
278 return @fs;
279}
280
281#----------------------------------------------------------------------------
282# Read a .vgtest file
283#----------------------------------------------------------------------------
284sub read_vgtest_file($)
285{
286 my ($f) = @_;
287
288 # Defaults.
sewardj3b290482011-05-06 21:02:55 +0000289 ($vgopts, $prog, $args) = ("", undef, "");
290 ($stdout_filter, $stderr_filter) = (undef, undef);
291 ($progB, $argsB, $stdinB) = (undef, "", undef);
292 ($stdoutB_filter, $stderrB_filter) = (undef, undef);
293 ($prereq, $post, $cleanup) = (undef, undef, undef);
florian31014da2011-09-26 00:29:44 +0000294 ($stdout_filter_args, $stderr_filter_args) = (undef, undef);
295 ($stdoutB_filter_args, $stderrB_filter_args) = (undef, undef);
njn25e49d8e72002-09-23 09:36:25 +0000296
297 # Every test directory must have a "filter_stderr"
nethercotef4928da2004-06-15 10:54:40 +0000298 $stderr_filter = validate_program(".", $default_stderr_filter, 1, 1);
sewardj3b290482011-05-06 21:02:55 +0000299 $stderrB_filter = validate_program(".", $default_stderr_filter, 1, 1);
300
njn25e49d8e72002-09-23 09:36:25 +0000301
302 open(INPUTFILE, "< $f") || die "File $f not openable\n";
303
304 while (my $line = <INPUTFILE>) {
sewardjb5f6f512005-03-10 23:59:00 +0000305 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
306 next;
307 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
bartef386252010-08-31 15:15:35 +0000308 my $addvgopts = $1;
309 $addvgopts =~ s/\${PWD}/$ENV{PWD}/g;
310 $vgopts = $vgopts . " " . $addvgopts; # Nb: Make sure there's a space!
njn25e49d8e72002-09-23 09:36:25 +0000311 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000312 $prog = validate_program(".", $1, 0, 0);
florian8d228442015-04-02 22:02:24 +0000313 } elsif ($line =~ /^\s*prog-asis:\s*(.*)$/) {
314 $prog = $1;
njn25e49d8e72002-09-23 09:36:25 +0000315 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
316 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000317 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000318 $stdout_filter = validate_program(".", $1, 1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000319 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000320 $stderr_filter = validate_program(".", $1, 1, 1);
florian31014da2011-09-26 00:29:44 +0000321 } elsif ($line =~ /^\s*stdout_filter_args:\s*(.*)$/) {
322 $stdout_filter_args = $1;
323 } elsif ($line =~ /^\s*stderr_filter_args:\s*(.*)$/) {
324 $stderr_filter_args = $1;
sewardj3b290482011-05-06 21:02:55 +0000325 } elsif ($line =~ /^\s*progB:\s*(.*)$/) {
326 $progB = validate_program(".", $1, 0, 0);
327 } elsif ($line =~ /^\s*argsB:\s*(.*)$/) {
328 $argsB = $1;
329 } elsif ($line =~ /^\s*stdinB:\s*(.*)$/) {
330 $stdinB = $1;
331 } elsif ($line =~ /^\s*stdoutB_filter:\s*(.*)$/) {
332 $stdoutB_filter = validate_program(".", $1, 1, 1);
333 } elsif ($line =~ /^\s*stderrB_filter:\s*(.*)$/) {
334 $stderrB_filter = validate_program(".", $1, 1, 1);
florian4b5c3f02011-10-03 00:19:05 +0000335 } elsif ($line =~ /^\s*stdoutB_filter_args:\s*(.*)$/) {
336 $stdoutB_filter_args = $1;
337 } elsif ($line =~ /^\s*stderrB_filter_args:\s*(.*)$/) {
338 $stderrB_filter_args = $1;
nethercotec5e1d802004-11-18 12:48:17 +0000339 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
340 $prereq = $1;
njn734b8052007-11-01 04:40:37 +0000341 } elsif ($line =~ /^\s*post:\s*(.*)$/) {
342 $post = $1;
nethercote64bc5af2004-11-18 11:57:00 +0000343 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
344 $cleanup = $1;
florian8d228442015-04-02 22:02:24 +0000345 } elsif ($line =~ /^\s*env:\s*(.*)$/) {
346 push @env,$1;
njn25e49d8e72002-09-23 09:36:25 +0000347 } else {
348 die "Bad line in $f: $line\n";
349 }
350 }
351 close(INPUTFILE);
352
353 if (!defined $prog) {
nethercotef4928da2004-06-15 10:54:40 +0000354 $prog = ""; # allow no prog for testing error and --help cases
njn25e49d8e72002-09-23 09:36:25 +0000355 }
356}
357
358#----------------------------------------------------------------------------
359# Do one test
360#----------------------------------------------------------------------------
361# Since most of the program time is spent in system() calls, need this to
362# propagate a Ctrl-C enabling us to quit.
363sub mysystem($)
364{
njn734b8052007-11-01 04:40:37 +0000365 my $exit_code = system($_[0]);
366 ($exit_code == 2) and exit 1; # 2 is SIGINT
367 return $exit_code;
njn25e49d8e72002-09-23 09:36:25 +0000368}
369
sewardjeefeeb72011-05-10 11:01:07 +0000370# if $keepunfiltered, copies $1 to $1.unfiltered.out
371# renames $0 tp $1
372sub filtered_rename($$)
373{
374 if ($keepunfiltered == 1) {
375 mysystem("cp $_[1] $_[1].unfiltered.out");
376 }
377 rename ($_[0], $_[1]);
378}
379
380
nethercote137bc552003-11-14 17:47:54 +0000381# from a directory name like "/foo/cachesim/tests/" determine the tool name
382sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000383{
384 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000385 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000386 return $1;
387}
388
nethercote4592db62004-02-29 01:31:09 +0000389# Compare output against expected output; it should match at least one of
390# them.
391sub do_diffs($$$$)
392{
393 my ($fullname, $name, $mid, $f_exps) = @_;
394
395 for my $f_exp (@$f_exps) {
396 (-r $f_exp) or die "Could not read `$f_exp'\n";
397
njne8ffe522008-03-02 22:48:14 +0000398 # Emacs produces temporary files that end in '~' and '#'. We ignore
399 # these.
400 if ($f_exp !~ /[~#]$/) {
401 # $n is the (optional) suffix after the ".exp"; we tack it onto
402 # the ".diff" file.
403 my $n = "";
404 if ($f_exp =~ /.*\.exp(.*)$/) {
405 $n = $1;
406 } else {
407 $n = "";
408 ($f_exp eq "/dev/null") or die "Unexpected .exp file: $f_exp\n";
409 }
nethercote4592db62004-02-29 01:31:09 +0000410
njn0d2e58f2009-02-25 04:57:56 +0000411 mysystem("@DIFF@ $f_exp $name.$mid.out > $name.$mid.diff$n");
nethercote4592db62004-02-29 01:31:09 +0000412
njne8ffe522008-03-02 22:48:14 +0000413 if (not -s "$name.$mid.diff$n") {
414 # A match; remove .out and any previously created .diff files.
415 unlink("$name.$mid.out");
416 unlink(<$name.$mid.diff*>);
417 return;
418 }
nethercote4592db62004-02-29 01:31:09 +0000419 }
420 }
421 # If we reach here, none of the .exp files matched.
422 print "*** $name failed ($mid) ***\n";
423 push(@failures, sprintf("%-40s ($mid)", "$fullname"));
424 $num_failures{$mid}++;
philippe69e8f172015-03-14 18:29:35 +0000425 if ($looptillfail == 1) {
426 print "Failure encountered, stopping to loop\n";
427 exit 1
428 }
nethercote4592db62004-02-29 01:31:09 +0000429}
430
njn25e49d8e72002-09-23 09:36:25 +0000431sub do_one_test($$)
432{
433 my ($dir, $vgtest) = @_;
434 $vgtest =~ /^(.*)\.vgtest/;
435 my $name = $1;
436 my $fullname = "$dir/$name";
437
sewardj91368992006-05-26 00:13:21 +0000438 # Pull any extra options (for example, --sanity-level=4)
439 # from $EXTRA_REGTEST_OPTS.
440 my $maybe_extraopts = $ENV{"EXTRA_REGTEST_OPTS"};
441 my $extraopts = $maybe_extraopts ? $maybe_extraopts : "";
442
njn25e49d8e72002-09-23 09:36:25 +0000443 read_vgtest_file($vgtest);
444
nethercotec5e1d802004-11-18 12:48:17 +0000445 if (defined $prereq) {
njn61485ab2009-03-04 04:15:16 +0000446 my $prereq_res = system("$prereq");
447 if (0 == $prereq_res) {
448 # Do nothing (ie. continue with the test)
449 } elsif (256 == $prereq_res) {
450 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
451 # Prereq failed, skip.
nethercotec5e1d802004-11-18 12:48:17 +0000452 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
nethercote781bed42004-10-19 16:56:41 +0000453 return;
njn61485ab2009-03-04 04:15:16 +0000454 } else {
455 # Bad prereq; abort.
456 $prereq_res /= 256;
457 die "prereq returned $prereq_res: $prereq\n";
nethercote781bed42004-10-19 16:56:41 +0000458 }
nethercoteb1affa82004-01-19 19:14:18 +0000459 }
460
njn25e49d8e72002-09-23 09:36:25 +0000461
sewardj3b290482011-05-06 21:02:55 +0000462 if (defined $progB) {
463 # If there is a progB, let's start it in background:
464 printf("%-16s valgrind $extraopts $vgopts $prog $args (progB: $progB $argsB)\n",
465 "$name:");
466 # progB.done used to detect child has finished. See below.
467 # Note: redirection of stdout and stderr is before $progB to allow argsB
468 # to e.g. redirect stdoutB to stderrB
469 if (defined $stdinB) {
470 mysystem("(rm -f progB.done;"
471 . " < $stdinB > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
472 . "touch progB.done) &");
473 } else {
474 mysystem("(rm -f progB.done;"
475 . " > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
476 . "touch progB.done) &");
477 }
478 } else {
479 printf("%-16s valgrind $extraopts $vgopts $prog $args\n", "$name:");
480 }
florian8d228442015-04-02 22:02:24 +0000481
482 # Collect environment variables, if any.
483 my $envvars = "";
484 foreach my $e (@env) {
485 $envvars = "$envvars $e";
486 }
487
nethercote137bc552003-11-14 17:47:54 +0000488 # Pass the appropriate --tool option for the directory (can be overridden
philippe14ab1a32012-04-08 19:52:38 +0000489 # by an "args:" line, though).
nethercote137bc552003-11-14 17:47:54 +0000490 my $tool=determine_tool();
philippe72faf102012-03-11 22:24:03 +0000491 if (defined $outer_valgrind ) {
philippe14ab1a32012-04-08 19:52:38 +0000492 # in an outer-inner setup, only set VALGRIND_LIB_INNER
florian8d228442015-04-02 22:02:24 +0000493 mysystem( "$envvars VALGRIND_LIB_INNER=$valgrind_lib "
philippe14ab1a32012-04-08 19:52:38 +0000494 . "$outer_valgrind "
philippe72faf102012-03-11 22:24:03 +0000495 . "--tool=" . $outer_tool . " "
496 . "$outer_args "
497 . "--log-file=" . "$name.outer.log "
498 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
499 . "--sim-hints=no-inner-prefix "
500 . "--tool=$tool $extraopts $vgopts "
501 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
502 } else {
philippe14ab1a32012-04-08 19:52:38 +0000503 # Set both VALGRIND_LIB and VALGRIND_LIB_INNER in case this Valgrind
504 # was configured with --enable-inner.
florian8d228442015-04-02 22:02:24 +0000505 mysystem( "$envvars VALGRIND_LIB=$valgrind_lib VALGRIND_LIB_INNER=$valgrind_lib "
philippe72faf102012-03-11 22:24:03 +0000506 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
507 . "--tool=$tool $extraopts $vgopts "
508 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
509 }
njn25e49d8e72002-09-23 09:36:25 +0000510
njn0001fc32006-04-03 14:25:23 +0000511 # Filter stdout
njn25e49d8e72002-09-23 09:36:25 +0000512 if (defined $stdout_filter) {
florian31014da2011-09-26 00:29:44 +0000513 $stdout_filter_args = $name if (! defined $stdout_filter_args);
514 mysystem("$stdout_filter $stdout_filter_args < $name.stdout.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000515 filtered_rename($tmp, "$name.stdout.out");
njn25e49d8e72002-09-23 09:36:25 +0000516 }
nethercote4592db62004-02-29 01:31:09 +0000517 # Find all the .stdout.exp files. If none, use /dev/null.
518 my @stdout_exps = <$name.stdout.exp*>;
519 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
njn0001fc32006-04-03 14:25:23 +0000520 do_diffs($fullname, $name, "stdout", \@stdout_exps);
njn25e49d8e72002-09-23 09:36:25 +0000521
njn0001fc32006-04-03 14:25:23 +0000522 # Filter stderr
florian31014da2011-09-26 00:29:44 +0000523 $stderr_filter_args = $name if (! defined $stderr_filter_args);
524 mysystem("$stderr_filter $stderr_filter_args < $name.stderr.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000525 filtered_rename($tmp, "$name.stderr.out");
sewardj79e88da2007-11-09 23:29:46 +0000526 # Find all the .stderr.exp files. At least one must exist.
nethercote4592db62004-02-29 01:31:09 +0000527 my @stderr_exps = <$name.stderr.exp*>;
sewardj79e88da2007-11-09 23:29:46 +0000528 (0 != scalar @stderr_exps) or die "Could not find `$name.stderr.exp*'\n";
nethercote4592db62004-02-29 01:31:09 +0000529 do_diffs($fullname, $name, "stderr", \@stderr_exps);
njn0001fc32006-04-03 14:25:23 +0000530
sewardj3b290482011-05-06 21:02:55 +0000531 if (defined $progB) {
532 # wait for the child to be finished
533 # tried things such as:
534 # wait;
535 # $SIG{CHLD} = sub { wait };
536 # but nothing worked:
537 # e.g. running mssnapshot.vgtest in a loop failed from time to time
538 # due to some missing output (not yet written?).
539 # So, we search progB.done during max 100 times 100 millisecond.
540 my $count;
541 for ($count = 1; $count <= 100; $count++) {
542 (-f "progB.done") or select(undef, undef, undef, 0.100);
543 }
544 # Filter stdout
545 if (defined $stdoutB_filter) {
florian31014da2011-09-26 00:29:44 +0000546 $stdoutB_filter_args = $name if (! defined $stdoutB_filter_args);
547 mysystem("$stdoutB_filter $stdoutB_filter_args < $name.stdoutB.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000548 filtered_rename($tmp, "$name.stdoutB.out");
sewardj3b290482011-05-06 21:02:55 +0000549 }
550 # Find all the .stdoutB.exp files. If none, use /dev/null.
551 my @stdoutB_exps = <$name.stdoutB.exp*>;
552 @stdoutB_exps = ( "/dev/null" ) if (0 == scalar @stdoutB_exps);
553 do_diffs($fullname, $name, "stdoutB", \@stdoutB_exps);
554
555 # Filter stderr
florian31014da2011-09-26 00:29:44 +0000556 $stderrB_filter_args = $name if (! defined $stderrB_filter_args);
557 mysystem("$stderrB_filter $stderrB_filter_args < $name.stderrB.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000558 filtered_rename($tmp, "$name.stderrB.out");
sewardj3b290482011-05-06 21:02:55 +0000559 # Find all the .stderrB.exp files. At least one must exist.
560 my @stderrB_exps = <$name.stderrB.exp*>;
561 (0 != scalar @stderrB_exps) or die "Could not find `$name.stderrB.exp*'\n";
562 do_diffs($fullname, $name, "stderrB", \@stderrB_exps);
563 }
564
njn0001fc32006-04-03 14:25:23 +0000565 # Maybe do post-test check
njn734b8052007-11-01 04:40:37 +0000566 if (defined $post) {
567 if (mysystem("$post > $name.post.out") != 0) {
568 print("post check failed: $post\n");
569 $num_failures{"post"}++;
njn0001fc32006-04-03 14:25:23 +0000570 } else {
njn734b8052007-11-01 04:40:37 +0000571 # Find all the .post.exp files. If none, use /dev/null.
572 my @post_exps = <$name.post.exp*>;
573 @post_exps = ( "/dev/null" ) if (0 == scalar @post_exps);
574 do_diffs($fullname, $name, "post", \@post_exps);
njn0001fc32006-04-03 14:25:23 +0000575 }
576 }
nethercotec1f8ad92004-04-17 17:25:08 +0000577
nethercote64bc5af2004-11-18 11:57:00 +0000578 if (defined $cleanup) {
579 (system("$cleanup") == 0) or
580 print("(cleanup operation failed: $cleanup)\n");
nethercotec1f8ad92004-04-17 17:25:08 +0000581 }
582
njn9fb16f52003-04-23 17:47:13 +0000583 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000584}
585
586#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000587# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000588#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000589sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000590
njndb3c4692002-10-04 10:03:34 +0000591sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000592{
njndb3c4692002-10-04 10:03:34 +0000593 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000594 $dir =~ s/\/$//; # trim a trailing '/'
595
nethercote362c0d22004-11-18 18:21:56 +0000596 # Ignore dirs into which we should not recurse.
mueller92f0b802003-11-19 00:55:32 +0000597 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000598
njn107bc572009-02-16 00:42:10 +0000599 (-x "$tests_dir/tests/arch_test") or die
600 "vg_regtest: 'arch_test' is missing. Did you forget to 'make check'?\n";
njn52783ca2005-12-08 22:39:04 +0000601
nethercote362c0d22004-11-18 18:21:56 +0000602 # Ignore any dir whose name matches that of an architecture which is not
njn107bc572009-02-16 00:42:10 +0000603 # the architecture we are running on. Eg. when running on x86, ignore
604 # ppc/ directories ('arch_test' returns 1 for this case). Likewise for
605 # the OS and platform.
nethercote362c0d22004-11-18 18:21:56 +0000606 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
njn61485ab2009-03-04 04:15:16 +0000607 if (256 == system("$tests_dir/tests/arch_test $dir")) { return; }
608 if (256 == system("$tests_dir/tests/os_test $dir")) { return; }
njn107bc572009-02-16 00:42:10 +0000609 if ($dir =~ /(\w+)-(\w+)/ &&
610 256 == system("sh $tests_dir/tests/platform_test $1 $2")) { return; }
nethercote362c0d22004-11-18 18:21:56 +0000611
njn25e49d8e72002-09-23 09:36:25 +0000612 chdir($dir) or die "Could not change into $dir\n";
613
njn584eaac2002-10-04 15:30:48 +0000614 # Nb: Don't prepend a '/' to the base directory
615 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000616 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000617
njndb3c4692002-10-04 10:03:34 +0000618 my @fs = glob "*";
njndb3c4692002-10-04 10:03:34 +0000619 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
620
621 if ($found_tests) {
622 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000623 }
njn25cac76cb2002-09-23 11:21:57 +0000624 foreach my $f (@fs) {
625 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000626 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000627 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000628 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000629 }
njn25e49d8e72002-09-23 09:36:25 +0000630 }
njndb3c4692002-10-04 10:03:34 +0000631 if ($found_tests) {
632 print "-- Finished tests in $full_dir $dashes\n";
633 }
634
njn25e49d8e72002-09-23 09:36:25 +0000635 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000636}
637
638#----------------------------------------------------------------------------
639# Summarise results
640#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000641sub plural($)
642{
643 return ( $_[0] == 1 ? "" : "s" );
644}
645
njn25e49d8e72002-09-23 09:36:25 +0000646sub summarise_results
647{
njnd8ced862003-04-08 00:47:05 +0000648 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000649
njn0001fc32006-04-03 14:25:23 +0000650 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s, "
sewardj3b290482011-05-06 21:02:55 +0000651 . "%d stderrB failure%s, %d stdoutB failure%s, "
njn734b8052007-11-01 04:40:37 +0000652 . "%d post failure%s ==\n",
njn9fb16f52003-04-23 17:47:13 +0000653 $num_tests_done, plural($num_tests_done),
njn0001fc32006-04-03 14:25:23 +0000654 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
655 $num_failures{"stdout"}, plural($num_failures{"stdout"}),
sewardj3b290482011-05-06 21:02:55 +0000656 $num_failures{"stderrB"}, plural($num_failures{"stderrB"}),
657 $num_failures{"stdoutB"}, plural($num_failures{"stdoutB"}),
njn734b8052007-11-01 04:40:37 +0000658 $num_failures{"post"}, plural($num_failures{"post"}));
njn9fb16f52003-04-23 17:47:13 +0000659
660 foreach my $failure (@failures) {
661 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000662 }
njn9fb16f52003-04-23 17:47:13 +0000663 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000664}
665
666#----------------------------------------------------------------------------
667# main(), sort of
668#----------------------------------------------------------------------------
sewardj91368992006-05-26 00:13:21 +0000669sub warn_about_EXTRA_REGTEST_OPTS()
670{
671 print "WARNING: \$EXTRA_REGTEST_OPTS is set. You probably don't want\n";
672 print "to run the regression tests with it set, unless you are doing some\n";
673 print "strange experiment, and/or you really know what you are doing.\n";
674 print "\n";
675}
njn25e49d8e72002-09-23 09:36:25 +0000676
daywalkerbb936982003-04-23 16:52:06 +0000677# nuke VALGRIND_OPTS
678$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000679
sewardj91368992006-05-26 00:13:21 +0000680if ($ENV{"EXTRA_REGTEST_OPTS"}) {
681 print "\n";
682 warn_about_EXTRA_REGTEST_OPTS();
683}
684
njn25e49d8e72002-09-23 09:36:25 +0000685my @fs = process_command_line();
philippe69e8f172015-03-14 18:29:35 +0000686while (1) { # we will exit after one loop, unless looptillfail
687 foreach my $f (@fs) {
688 if (-d $f) {
689 test_one_dir($f, "");
690 } else {
691 # Allow the .vgtest suffix to be given or omitted
692 if ($f =~ /.vgtest$/ && -r $f) {
693 # do nothing
694 } elsif (-r "$f.vgtest") {
695 $f = "$f.vgtest";
696 } else {
697 die "`$f' neither a directory nor a readable test file/name\n"
698 }
699 my $dir = `dirname $f`; chomp $dir;
700 my $file = `basename $f`; chomp $file;
701 chdir($dir) or die "Could not change into $dir\n";
702 do_one_test($dir, $file);
njn25e49d8e72002-09-23 09:36:25 +0000703 }
philippe69e8f172015-03-14 18:29:35 +0000704 chdir($tests_dir);
njn25e49d8e72002-09-23 09:36:25 +0000705 }
philippe69e8f172015-03-14 18:29:35 +0000706 if ($looptillfail == 0) {
707 last;
708 }
njn25e49d8e72002-09-23 09:36:25 +0000709}
710summarise_results();
711
sewardj91368992006-05-26 00:13:21 +0000712if ($ENV{"EXTRA_REGTEST_OPTS"}) {
713 warn_about_EXTRA_REGTEST_OPTS();
714}
715
njn0001fc32006-04-03 14:25:23 +0000716if (0 == $num_failures{"stdout"} &&
717 0 == $num_failures{"stderr"} &&
sewardj3b290482011-05-06 21:02:55 +0000718 0 == $num_failures{"stdoutB"} &&
719 0 == $num_failures{"stderrB"} &&
njn734b8052007-11-01 04:40:37 +0000720 0 == $num_failures{"post"}) {
nethercoteab422192004-03-01 08:27:37 +0000721 exit 0;
722} else {
723 exit 1;
724}
725
njnc2e7f482002-09-27 08:44:17 +0000726##--------------------------------------------------------------------##
727##--- end vg_regtest ---##
728##--------------------------------------------------------------------##