blob: d77916ff196cb5ac23f031e06fc6dafdb43fde57 [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.
47#
njn4ba5a792002-09-30 10:23:54 +000048# The easiest way is to run all tests in valgrind/ with (assuming you installed
49# in $PREFIX):
50#
51# $PREFIX/bin/vg_regtest --all
52#
njna217f3d2002-09-27 08:26:27 +000053# You can specify individual files to test, or whole directories, or both.
jsgf855d93d2003-10-13 22:26:55 +000054# Directories are traversed recursively, except for ones named, for example,
55# CVS/ or docs/.
njn25e49d8e72002-09-23 09:36:25 +000056#
57# Each test is defined in a file <test>.vgtest, containing one or more of the
njna217f3d2002-09-27 08:26:27 +000058# following lines, in any order:
njn25e49d8e72002-09-23 09:36:25 +000059# - prog: <prog to run> (compulsory)
60# - args: <args for prog> (default: none)
njnf6b00762009-04-17 04:26:41 +000061# - vgopts: <Valgrind options> (default: none;
62# multiple are allowed)
njn25e49d8e72002-09-23 09:36:25 +000063# - stdout_filter: <filter to run stdout through> (default: none)
njna217f3d2002-09-27 08:26:27 +000064# - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
florian31014da2011-09-26 00:29:44 +000065# - stdout_filter_args: <args for stdout_filter> (default: basename of .vgtest file)
66# - stderr_filter_args: <args for stderr_filter> (default: basename of .vgtest file)
sewardj3b290482011-05-06 21:02:55 +000067#
68# - progB: <prog to run in parallel with prog> (default: none)
69# - argsB: <args for progB> (default: none)
70# - stdinB: <input file for progB> (default: none)
71# - stdoutB_filter: <filter progB stdout through> (default: none)
72# - stderrB_filter: <filter progB stderr through> (default: ./filter_stderr)
florian31014da2011-09-26 00:29:44 +000073# - stdoutB_filter_args: <args for stdout_filterB> (default: basename of .vgtest file)
74# - stderrB_filter_args: <args for stderr_filterB> (default: basename of .vgtest file)
sewardj3b290482011-05-06 21:02:55 +000075#
nethercotec5e1d802004-11-18 12:48:17 +000076# - prereq: <prerequisite command> (default: none)
njn734b8052007-11-01 04:40:37 +000077# - post: <post-test check command> (default: none)
78# - cleanup: <post-test cleanup cmd> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000079#
sewardj3b290482011-05-06 21:02:55 +000080# If prog or probB is a relative path, it will be prefix with the test directory.
njna217f3d2002-09-27 08:26:27 +000081# Note that filters are necessary for stderr results to filter out things that
82# always change, eg. process id numbers.
sewardj3b290482011-05-06 21:02:55 +000083# Note that if a progB is specified, it is started in background (before prog).
njn25e49d8e72002-09-23 09:36:25 +000084#
njn1ee25f02008-02-27 18:10:19 +000085# Expected stdout (filtered) is kept in <test>.stdout.exp* (can be more
nethercote4592db62004-02-29 01:31:09 +000086# than one expected output). It can be missing if it would be empty. Expected
sewardj79e88da2007-11-09 23:29:46 +000087# stderr (filtered) is kept in <test>.stderr.exp*. There must be at least
njne8ffe522008-03-02 22:48:14 +000088# one stderr.exp* file. Any .exp* file that ends in '~' or '#' is ignored;
89# this is because Emacs creates temporary files of these names.
njn25e49d8e72002-09-23 09:36:25 +000090#
sewardj3b290482011-05-06 21:02:55 +000091# Expected output for progB is handled similarly, except that
92# expected stdout and stderr for progB are in <test>.stdoutB.exp*
93# and <test>.stderrB.exp*.
94#
njn25e49d8e72002-09-23 09:36:25 +000095# If results don't match, the output can be found in <test>.std<strm>.out,
njn1ee25f02008-02-27 18:10:19 +000096# and the diff between expected and actual in <test>.std<strm>.diff*.
sewardj3b290482011-05-06 21:02:55 +000097# (for progB, in <test>.std<strm>2.out and <test>.std<strm>2.diff*).
njn25e49d8e72002-09-23 09:36:25 +000098#
njn61485ab2009-03-04 04:15:16 +000099# The prerequisite command, if present, works like this:
100# - if it returns 0 the test is run
101# - if it returns 1 the test is skipped
102# - if it returns anything else the script aborts.
103# The idea here is results other than 0 or 1 are likely to be due to
104# problems with the commands, and you don't want to conflate them with the 1
105# case, which would happen if you just tested for zero or non-zero.
106#
107# The post-test command, if present, must return 0 and its stdout must match
108# the expected stdout which is kept in <test>.post.exp*.
109#
sewardj91368992006-05-26 00:13:21 +0000110# Sometimes it is useful to run all the tests at a high sanity check
111# level or with arbitrary other flags. To make this simple, extra
112# options, applied to all tests run, are read from $EXTRA_REGTEST_OPTS,
113# and handed to valgrind prior to any other flags specified by the
114# .vgtest file.
115#
njn7358d522006-11-26 22:49:58 +0000116# Some more notes on adding regression tests for a new tool are in
117# docs/xml/manual-writing-tools.xml.
njn25e49d8e72002-09-23 09:36:25 +0000118#----------------------------------------------------------------------------
119
njn9fb16f52003-04-23 17:47:13 +0000120use warnings;
njn25e49d8e72002-09-23 09:36:25 +0000121use strict;
122
123#----------------------------------------------------------------------------
124# Global vars
125#----------------------------------------------------------------------------
sewardja8162b22007-11-30 21:24:05 +0000126my $usage="\n"
sewardjeefeeb72011-05-10 11:01:07 +0000127 . "Usage:\n"
philippe72faf102012-03-11 22:24:03 +0000128 . " vg_regtest [--all, --valgrind, --valgrind-lib, --keep-unfiltered\n"
129 . " --outer-valgrind, --outer-tool, --outer-args]\n"
sewardjeefeeb72011-05-10 11:01:07 +0000130 . " Use EXTRA_REGTEST_OPTS to supply extra args for all tests\n"
131 . "\n";
njn25e49d8e72002-09-23 09:36:25 +0000132
133my $tmp="vg_regtest.tmp.$$";
134
135# Test variables
136my $vgopts; # valgrind options
137my $prog; # test prog
138my $args; # test prog args
139my $stdout_filter; # filter program to run stdout results file through
140my $stderr_filter; # filter program to run stderr results file through
florian31014da2011-09-26 00:29:44 +0000141my $stdout_filter_args; # arguments passed to stdout_filter
142my $stderr_filter_args; # arguments passed to stderr_filter
sewardj3b290482011-05-06 21:02:55 +0000143my $progB; # Same but for progB
144my $argsB; #
145my $stdoutB_filter; #
146my $stderrB_filter; #
florian31014da2011-09-26 00:29:44 +0000147my $stdoutB_filter_args;# arguments passed to stdout_filterB
148my $stderrB_filter_args;# arguments passed to stderr_filterB
sewardj3b290482011-05-06 21:02:55 +0000149my $stdinB; # Input file for progB
nethercotec5e1d802004-11-18 12:48:17 +0000150my $prereq; # prerequisite test to satisfy before running test
njn734b8052007-11-01 04:40:37 +0000151my $post; # check command after running test
nethercote64bc5af2004-11-18 11:57:00 +0000152my $cleanup; # cleanup command to run
njn25e49d8e72002-09-23 09:36:25 +0000153
154my @failures; # List of failed tests
155
njn9fb16f52003-04-23 17:47:13 +0000156my $num_tests_done = 0;
sewardj3b290482011-05-06 21:02:55 +0000157my %num_failures = (stderr => 0, stdout => 0,
158 stderrB => 0, stdoutB => 0,
159 post => 0);
njn25e49d8e72002-09-23 09:36:25 +0000160
njn71fe3e62003-04-23 21:48:20 +0000161# Default valgrind to use is this build tree's (uninstalled) one
njn71fe3e62003-04-23 21:48:20 +0000162my $valgrind = "./coregrind/valgrind";
njn25e49d8e72002-09-23 09:36:25 +0000163
164chomp(my $tests_dir = `pwd`);
165
philippe72faf102012-03-11 22:24:03 +0000166# Outer valgrind to use, and args to use for it.
philippe72faf102012-03-11 22:24:03 +0000167my $outer_valgrind;
168my $outer_tool = "memcheck";
169my $outer_args;
170
njn3ed89602006-10-23 18:38:35 +0000171my $valgrind_lib = "$tests_dir/.in_place";
sewardjeefeeb72011-05-10 11:01:07 +0000172my $keepunfiltered = 0;
njn3ed89602006-10-23 18:38:35 +0000173
njn25e49d8e72002-09-23 09:36:25 +0000174# default filter is the one named "filter_stderr" in the test's directory
175my $default_stderr_filter = "filter_stderr";
176
177
178#----------------------------------------------------------------------------
179# Process command line, setup
180#----------------------------------------------------------------------------
181
182# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
183#
184# 1. Can prepend "." onto programs to avoid trouble with users who don't have
185# "." in their path (by making $dir = ".")
186# 2. Can prepend the current dir to make the command absolute to avoid
187# subsequent trouble when we change directories.
188#
189# Also checks the program exists and is executable.
nethercotef4928da2004-06-15 10:54:40 +0000190sub validate_program ($$$$)
njn25e49d8e72002-09-23 09:36:25 +0000191{
nethercotef4928da2004-06-15 10:54:40 +0000192 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000193
194 # If absolute path, leave it alone. If relative, make it
195 # absolute -- by prepending current dir -- so we can change
196 # dirs and still use it.
197 $prog = "$dir/$prog" if ($prog !~ /^\//);
nethercotef4928da2004-06-15 10:54:40 +0000198 if ($must_exist) {
199 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
200 }
njn71fe3e62003-04-23 21:48:20 +0000201 if ($must_be_executable) {
nethercotef4928da2004-06-15 10:54:40 +0000202 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
njn71fe3e62003-04-23 21:48:20 +0000203 }
njn25e49d8e72002-09-23 09:36:25 +0000204
205 return $prog;
206}
207
208sub process_command_line()
209{
210 my $alldirs = 0;
211 my @fs;
212
213 for my $arg (@ARGV) {
214 if ($arg =~ /^-/) {
njnd8ced862003-04-08 00:47:05 +0000215 if ($arg =~ /^--all$/) {
njn25e49d8e72002-09-23 09:36:25 +0000216 $alldirs = 1;
217 } elsif ($arg =~ /^--valgrind=(.*)$/) {
218 $valgrind = $1;
philippe72faf102012-03-11 22:24:03 +0000219 } elsif ($arg =~ /^--outer-valgrind=(.*)$/) {
220 $outer_valgrind = $1;
221 } elsif ($arg =~ /^--outer-tool=(.*)$/) {
222 $outer_tool = $1;
223 } elsif ($arg =~ /^--outer-args=(.*)$/) {
224 $outer_args = $1;
njn3ed89602006-10-23 18:38:35 +0000225 } elsif ($arg =~ /^--valgrind-lib=(.*)$/) {
226 $valgrind_lib = $1;
sewardjeefeeb72011-05-10 11:01:07 +0000227 } elsif ($arg =~ /^--keep-unfiltered$/) {
228 $keepunfiltered = 1;
njn25e49d8e72002-09-23 09:36:25 +0000229 } else {
230 die $usage;
231 }
232 } else {
233 push(@fs, $arg);
234 }
235 }
nethercotef4928da2004-06-15 10:54:40 +0000236 $valgrind = validate_program($tests_dir, $valgrind, 1, 0);
philippe72faf102012-03-11 22:24:03 +0000237
238 if (defined $outer_valgrind) {
philippe14ab1a32012-04-08 19:52:38 +0000239 $outer_valgrind = validate_program($tests_dir, $outer_valgrind, 1, 1);
philippe72faf102012-03-11 22:24:03 +0000240 if (not defined $outer_args) {
241 $outer_args =
philippe14ab1a32012-04-08 19:52:38 +0000242 " --command-line-only=yes"
philippe72faf102012-03-11 22:24:03 +0000243 . " --run-libc-freeres=no --sim-hints=enable-outer"
philippe14ab1a32012-04-08 19:52:38 +0000244 . " --smc-check=all-non-file"
philippe72faf102012-03-11 22:24:03 +0000245 . " --vgdb=no --trace-children=yes --read-var-info=no"
philippe0a961fc2014-06-25 23:27:08 +0000246 . " --read-inline-info=yes"
philippe72faf102012-03-11 22:24:03 +0000247 . " --suppressions="
248 . validate_program($tests_dir,"./tests/outer_inner.supp",1,0)
249 . " --memcheck:leak-check=full --memcheck:show-reachable=no"
250 . " ";
251 }
252 }
njn25e49d8e72002-09-23 09:36:25 +0000253
254 if ($alldirs) {
255 @fs = ();
256 foreach my $f (glob "*") {
257 push(@fs, $f) if (-d $f);
258 }
259 }
260
261 (0 != @fs) or die "No test files or directories specified\n";
262
263 return @fs;
264}
265
266#----------------------------------------------------------------------------
267# Read a .vgtest file
268#----------------------------------------------------------------------------
269sub read_vgtest_file($)
270{
271 my ($f) = @_;
272
273 # Defaults.
sewardj3b290482011-05-06 21:02:55 +0000274 ($vgopts, $prog, $args) = ("", undef, "");
275 ($stdout_filter, $stderr_filter) = (undef, undef);
276 ($progB, $argsB, $stdinB) = (undef, "", undef);
277 ($stdoutB_filter, $stderrB_filter) = (undef, undef);
278 ($prereq, $post, $cleanup) = (undef, undef, undef);
florian31014da2011-09-26 00:29:44 +0000279 ($stdout_filter_args, $stderr_filter_args) = (undef, undef);
280 ($stdoutB_filter_args, $stderrB_filter_args) = (undef, undef);
njn25e49d8e72002-09-23 09:36:25 +0000281
282 # Every test directory must have a "filter_stderr"
nethercotef4928da2004-06-15 10:54:40 +0000283 $stderr_filter = validate_program(".", $default_stderr_filter, 1, 1);
sewardj3b290482011-05-06 21:02:55 +0000284 $stderrB_filter = validate_program(".", $default_stderr_filter, 1, 1);
285
njn25e49d8e72002-09-23 09:36:25 +0000286
287 open(INPUTFILE, "< $f") || die "File $f not openable\n";
288
289 while (my $line = <INPUTFILE>) {
sewardjb5f6f512005-03-10 23:59:00 +0000290 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
291 next;
292 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
bartef386252010-08-31 15:15:35 +0000293 my $addvgopts = $1;
294 $addvgopts =~ s/\${PWD}/$ENV{PWD}/g;
295 $vgopts = $vgopts . " " . $addvgopts; # Nb: Make sure there's a space!
njn25e49d8e72002-09-23 09:36:25 +0000296 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000297 $prog = validate_program(".", $1, 0, 0);
njn25e49d8e72002-09-23 09:36:25 +0000298 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
299 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000300 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000301 $stdout_filter = validate_program(".", $1, 1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000302 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000303 $stderr_filter = validate_program(".", $1, 1, 1);
florian31014da2011-09-26 00:29:44 +0000304 } elsif ($line =~ /^\s*stdout_filter_args:\s*(.*)$/) {
305 $stdout_filter_args = $1;
306 } elsif ($line =~ /^\s*stderr_filter_args:\s*(.*)$/) {
307 $stderr_filter_args = $1;
sewardj3b290482011-05-06 21:02:55 +0000308 } elsif ($line =~ /^\s*progB:\s*(.*)$/) {
309 $progB = validate_program(".", $1, 0, 0);
310 } elsif ($line =~ /^\s*argsB:\s*(.*)$/) {
311 $argsB = $1;
312 } elsif ($line =~ /^\s*stdinB:\s*(.*)$/) {
313 $stdinB = $1;
314 } elsif ($line =~ /^\s*stdoutB_filter:\s*(.*)$/) {
315 $stdoutB_filter = validate_program(".", $1, 1, 1);
316 } elsif ($line =~ /^\s*stderrB_filter:\s*(.*)$/) {
317 $stderrB_filter = validate_program(".", $1, 1, 1);
florian4b5c3f02011-10-03 00:19:05 +0000318 } elsif ($line =~ /^\s*stdoutB_filter_args:\s*(.*)$/) {
319 $stdoutB_filter_args = $1;
320 } elsif ($line =~ /^\s*stderrB_filter_args:\s*(.*)$/) {
321 $stderrB_filter_args = $1;
nethercotec5e1d802004-11-18 12:48:17 +0000322 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
323 $prereq = $1;
njn734b8052007-11-01 04:40:37 +0000324 } elsif ($line =~ /^\s*post:\s*(.*)$/) {
325 $post = $1;
nethercote64bc5af2004-11-18 11:57:00 +0000326 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
327 $cleanup = $1;
njn25e49d8e72002-09-23 09:36:25 +0000328 } else {
329 die "Bad line in $f: $line\n";
330 }
331 }
332 close(INPUTFILE);
333
334 if (!defined $prog) {
nethercotef4928da2004-06-15 10:54:40 +0000335 $prog = ""; # allow no prog for testing error and --help cases
njn25e49d8e72002-09-23 09:36:25 +0000336 }
337}
338
339#----------------------------------------------------------------------------
340# Do one test
341#----------------------------------------------------------------------------
342# Since most of the program time is spent in system() calls, need this to
343# propagate a Ctrl-C enabling us to quit.
344sub mysystem($)
345{
njn734b8052007-11-01 04:40:37 +0000346 my $exit_code = system($_[0]);
347 ($exit_code == 2) and exit 1; # 2 is SIGINT
348 return $exit_code;
njn25e49d8e72002-09-23 09:36:25 +0000349}
350
sewardjeefeeb72011-05-10 11:01:07 +0000351# if $keepunfiltered, copies $1 to $1.unfiltered.out
352# renames $0 tp $1
353sub filtered_rename($$)
354{
355 if ($keepunfiltered == 1) {
356 mysystem("cp $_[1] $_[1].unfiltered.out");
357 }
358 rename ($_[0], $_[1]);
359}
360
361
nethercote137bc552003-11-14 17:47:54 +0000362# from a directory name like "/foo/cachesim/tests/" determine the tool name
363sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000364{
365 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000366 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000367 return $1;
368}
369
nethercote4592db62004-02-29 01:31:09 +0000370# Compare output against expected output; it should match at least one of
371# them.
372sub do_diffs($$$$)
373{
374 my ($fullname, $name, $mid, $f_exps) = @_;
375
376 for my $f_exp (@$f_exps) {
377 (-r $f_exp) or die "Could not read `$f_exp'\n";
378
njne8ffe522008-03-02 22:48:14 +0000379 # Emacs produces temporary files that end in '~' and '#'. We ignore
380 # these.
381 if ($f_exp !~ /[~#]$/) {
382 # $n is the (optional) suffix after the ".exp"; we tack it onto
383 # the ".diff" file.
384 my $n = "";
385 if ($f_exp =~ /.*\.exp(.*)$/) {
386 $n = $1;
387 } else {
388 $n = "";
389 ($f_exp eq "/dev/null") or die "Unexpected .exp file: $f_exp\n";
390 }
nethercote4592db62004-02-29 01:31:09 +0000391
njn0d2e58f2009-02-25 04:57:56 +0000392 mysystem("@DIFF@ $f_exp $name.$mid.out > $name.$mid.diff$n");
nethercote4592db62004-02-29 01:31:09 +0000393
njne8ffe522008-03-02 22:48:14 +0000394 if (not -s "$name.$mid.diff$n") {
395 # A match; remove .out and any previously created .diff files.
396 unlink("$name.$mid.out");
397 unlink(<$name.$mid.diff*>);
398 return;
399 }
nethercote4592db62004-02-29 01:31:09 +0000400 }
401 }
402 # If we reach here, none of the .exp files matched.
403 print "*** $name failed ($mid) ***\n";
404 push(@failures, sprintf("%-40s ($mid)", "$fullname"));
405 $num_failures{$mid}++;
406}
407
njn25e49d8e72002-09-23 09:36:25 +0000408sub do_one_test($$)
409{
410 my ($dir, $vgtest) = @_;
411 $vgtest =~ /^(.*)\.vgtest/;
412 my $name = $1;
413 my $fullname = "$dir/$name";
414
sewardj91368992006-05-26 00:13:21 +0000415 # Pull any extra options (for example, --sanity-level=4)
416 # from $EXTRA_REGTEST_OPTS.
417 my $maybe_extraopts = $ENV{"EXTRA_REGTEST_OPTS"};
418 my $extraopts = $maybe_extraopts ? $maybe_extraopts : "";
419
njn25e49d8e72002-09-23 09:36:25 +0000420 read_vgtest_file($vgtest);
421
nethercotec5e1d802004-11-18 12:48:17 +0000422 if (defined $prereq) {
njn61485ab2009-03-04 04:15:16 +0000423 my $prereq_res = system("$prereq");
424 if (0 == $prereq_res) {
425 # Do nothing (ie. continue with the test)
426 } elsif (256 == $prereq_res) {
427 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
428 # Prereq failed, skip.
nethercotec5e1d802004-11-18 12:48:17 +0000429 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
nethercote781bed42004-10-19 16:56:41 +0000430 return;
njn61485ab2009-03-04 04:15:16 +0000431 } else {
432 # Bad prereq; abort.
433 $prereq_res /= 256;
434 die "prereq returned $prereq_res: $prereq\n";
nethercote781bed42004-10-19 16:56:41 +0000435 }
nethercoteb1affa82004-01-19 19:14:18 +0000436 }
437
njn25e49d8e72002-09-23 09:36:25 +0000438
sewardj3b290482011-05-06 21:02:55 +0000439 if (defined $progB) {
440 # If there is a progB, let's start it in background:
441 printf("%-16s valgrind $extraopts $vgopts $prog $args (progB: $progB $argsB)\n",
442 "$name:");
443 # progB.done used to detect child has finished. See below.
444 # Note: redirection of stdout and stderr is before $progB to allow argsB
445 # to e.g. redirect stdoutB to stderrB
446 if (defined $stdinB) {
447 mysystem("(rm -f progB.done;"
448 . " < $stdinB > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
449 . "touch progB.done) &");
450 } else {
451 mysystem("(rm -f progB.done;"
452 . " > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
453 . "touch progB.done) &");
454 }
455 } else {
456 printf("%-16s valgrind $extraopts $vgopts $prog $args\n", "$name:");
457 }
458
nethercote137bc552003-11-14 17:47:54 +0000459 # Pass the appropriate --tool option for the directory (can be overridden
philippe14ab1a32012-04-08 19:52:38 +0000460 # by an "args:" line, though).
nethercote137bc552003-11-14 17:47:54 +0000461 my $tool=determine_tool();
philippe72faf102012-03-11 22:24:03 +0000462 if (defined $outer_valgrind ) {
philippe14ab1a32012-04-08 19:52:38 +0000463 # in an outer-inner setup, only set VALGRIND_LIB_INNER
464 mysystem( "VALGRIND_LIB_INNER=$valgrind_lib "
465 . "$outer_valgrind "
philippe72faf102012-03-11 22:24:03 +0000466 . "--tool=" . $outer_tool . " "
467 . "$outer_args "
468 . "--log-file=" . "$name.outer.log "
469 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
470 . "--sim-hints=no-inner-prefix "
471 . "--tool=$tool $extraopts $vgopts "
472 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
473 } else {
philippe14ab1a32012-04-08 19:52:38 +0000474 # Set both VALGRIND_LIB and VALGRIND_LIB_INNER in case this Valgrind
475 # was configured with --enable-inner.
476 mysystem( "VALGRIND_LIB=$valgrind_lib VALGRIND_LIB_INNER=$valgrind_lib "
philippe72faf102012-03-11 22:24:03 +0000477 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
478 . "--tool=$tool $extraopts $vgopts "
479 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
480 }
njn25e49d8e72002-09-23 09:36:25 +0000481
njn0001fc32006-04-03 14:25:23 +0000482 # Filter stdout
njn25e49d8e72002-09-23 09:36:25 +0000483 if (defined $stdout_filter) {
florian31014da2011-09-26 00:29:44 +0000484 $stdout_filter_args = $name if (! defined $stdout_filter_args);
485 mysystem("$stdout_filter $stdout_filter_args < $name.stdout.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000486 filtered_rename($tmp, "$name.stdout.out");
njn25e49d8e72002-09-23 09:36:25 +0000487 }
nethercote4592db62004-02-29 01:31:09 +0000488 # Find all the .stdout.exp files. If none, use /dev/null.
489 my @stdout_exps = <$name.stdout.exp*>;
490 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
njn0001fc32006-04-03 14:25:23 +0000491 do_diffs($fullname, $name, "stdout", \@stdout_exps);
njn25e49d8e72002-09-23 09:36:25 +0000492
njn0001fc32006-04-03 14:25:23 +0000493 # Filter stderr
florian31014da2011-09-26 00:29:44 +0000494 $stderr_filter_args = $name if (! defined $stderr_filter_args);
495 mysystem("$stderr_filter $stderr_filter_args < $name.stderr.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000496 filtered_rename($tmp, "$name.stderr.out");
sewardj79e88da2007-11-09 23:29:46 +0000497 # Find all the .stderr.exp files. At least one must exist.
nethercote4592db62004-02-29 01:31:09 +0000498 my @stderr_exps = <$name.stderr.exp*>;
sewardj79e88da2007-11-09 23:29:46 +0000499 (0 != scalar @stderr_exps) or die "Could not find `$name.stderr.exp*'\n";
nethercote4592db62004-02-29 01:31:09 +0000500 do_diffs($fullname, $name, "stderr", \@stderr_exps);
njn0001fc32006-04-03 14:25:23 +0000501
sewardj3b290482011-05-06 21:02:55 +0000502 if (defined $progB) {
503 # wait for the child to be finished
504 # tried things such as:
505 # wait;
506 # $SIG{CHLD} = sub { wait };
507 # but nothing worked:
508 # e.g. running mssnapshot.vgtest in a loop failed from time to time
509 # due to some missing output (not yet written?).
510 # So, we search progB.done during max 100 times 100 millisecond.
511 my $count;
512 for ($count = 1; $count <= 100; $count++) {
513 (-f "progB.done") or select(undef, undef, undef, 0.100);
514 }
515 # Filter stdout
516 if (defined $stdoutB_filter) {
florian31014da2011-09-26 00:29:44 +0000517 $stdoutB_filter_args = $name if (! defined $stdoutB_filter_args);
518 mysystem("$stdoutB_filter $stdoutB_filter_args < $name.stdoutB.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000519 filtered_rename($tmp, "$name.stdoutB.out");
sewardj3b290482011-05-06 21:02:55 +0000520 }
521 # Find all the .stdoutB.exp files. If none, use /dev/null.
522 my @stdoutB_exps = <$name.stdoutB.exp*>;
523 @stdoutB_exps = ( "/dev/null" ) if (0 == scalar @stdoutB_exps);
524 do_diffs($fullname, $name, "stdoutB", \@stdoutB_exps);
525
526 # Filter stderr
florian31014da2011-09-26 00:29:44 +0000527 $stderrB_filter_args = $name if (! defined $stderrB_filter_args);
528 mysystem("$stderrB_filter $stderrB_filter_args < $name.stderrB.out > $tmp");
sewardjeefeeb72011-05-10 11:01:07 +0000529 filtered_rename($tmp, "$name.stderrB.out");
sewardj3b290482011-05-06 21:02:55 +0000530 # Find all the .stderrB.exp files. At least one must exist.
531 my @stderrB_exps = <$name.stderrB.exp*>;
532 (0 != scalar @stderrB_exps) or die "Could not find `$name.stderrB.exp*'\n";
533 do_diffs($fullname, $name, "stderrB", \@stderrB_exps);
534 }
535
njn0001fc32006-04-03 14:25:23 +0000536 # Maybe do post-test check
njn734b8052007-11-01 04:40:37 +0000537 if (defined $post) {
538 if (mysystem("$post > $name.post.out") != 0) {
539 print("post check failed: $post\n");
540 $num_failures{"post"}++;
njn0001fc32006-04-03 14:25:23 +0000541 } else {
njn734b8052007-11-01 04:40:37 +0000542 # Find all the .post.exp files. If none, use /dev/null.
543 my @post_exps = <$name.post.exp*>;
544 @post_exps = ( "/dev/null" ) if (0 == scalar @post_exps);
545 do_diffs($fullname, $name, "post", \@post_exps);
njn0001fc32006-04-03 14:25:23 +0000546 }
547 }
nethercotec1f8ad92004-04-17 17:25:08 +0000548
nethercote64bc5af2004-11-18 11:57:00 +0000549 if (defined $cleanup) {
550 (system("$cleanup") == 0) or
551 print("(cleanup operation failed: $cleanup)\n");
nethercotec1f8ad92004-04-17 17:25:08 +0000552 }
553
njn9fb16f52003-04-23 17:47:13 +0000554 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000555}
556
557#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000558# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000559#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000560sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000561
njndb3c4692002-10-04 10:03:34 +0000562sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000563{
njndb3c4692002-10-04 10:03:34 +0000564 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000565 $dir =~ s/\/$//; # trim a trailing '/'
566
nethercote362c0d22004-11-18 18:21:56 +0000567 # Ignore dirs into which we should not recurse.
mueller92f0b802003-11-19 00:55:32 +0000568 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000569
njn107bc572009-02-16 00:42:10 +0000570 (-x "$tests_dir/tests/arch_test") or die
571 "vg_regtest: 'arch_test' is missing. Did you forget to 'make check'?\n";
njn52783ca2005-12-08 22:39:04 +0000572
nethercote362c0d22004-11-18 18:21:56 +0000573 # Ignore any dir whose name matches that of an architecture which is not
njn107bc572009-02-16 00:42:10 +0000574 # the architecture we are running on. Eg. when running on x86, ignore
575 # ppc/ directories ('arch_test' returns 1 for this case). Likewise for
576 # the OS and platform.
nethercote362c0d22004-11-18 18:21:56 +0000577 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
njn61485ab2009-03-04 04:15:16 +0000578 if (256 == system("$tests_dir/tests/arch_test $dir")) { return; }
579 if (256 == system("$tests_dir/tests/os_test $dir")) { return; }
njn107bc572009-02-16 00:42:10 +0000580 if ($dir =~ /(\w+)-(\w+)/ &&
581 256 == system("sh $tests_dir/tests/platform_test $1 $2")) { return; }
nethercote362c0d22004-11-18 18:21:56 +0000582
njn25e49d8e72002-09-23 09:36:25 +0000583 chdir($dir) or die "Could not change into $dir\n";
584
njn584eaac2002-10-04 15:30:48 +0000585 # Nb: Don't prepend a '/' to the base directory
586 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000587 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000588
njndb3c4692002-10-04 10:03:34 +0000589 my @fs = glob "*";
njndb3c4692002-10-04 10:03:34 +0000590 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
591
592 if ($found_tests) {
593 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000594 }
njn25cac76cb2002-09-23 11:21:57 +0000595 foreach my $f (@fs) {
596 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000597 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000598 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000599 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000600 }
njn25e49d8e72002-09-23 09:36:25 +0000601 }
njndb3c4692002-10-04 10:03:34 +0000602 if ($found_tests) {
603 print "-- Finished tests in $full_dir $dashes\n";
604 }
605
njn25e49d8e72002-09-23 09:36:25 +0000606 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000607}
608
609#----------------------------------------------------------------------------
610# Summarise results
611#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000612sub plural($)
613{
614 return ( $_[0] == 1 ? "" : "s" );
615}
616
njn25e49d8e72002-09-23 09:36:25 +0000617sub summarise_results
618{
njnd8ced862003-04-08 00:47:05 +0000619 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000620
njn0001fc32006-04-03 14:25:23 +0000621 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s, "
sewardj3b290482011-05-06 21:02:55 +0000622 . "%d stderrB failure%s, %d stdoutB failure%s, "
njn734b8052007-11-01 04:40:37 +0000623 . "%d post failure%s ==\n",
njn9fb16f52003-04-23 17:47:13 +0000624 $num_tests_done, plural($num_tests_done),
njn0001fc32006-04-03 14:25:23 +0000625 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
626 $num_failures{"stdout"}, plural($num_failures{"stdout"}),
sewardj3b290482011-05-06 21:02:55 +0000627 $num_failures{"stderrB"}, plural($num_failures{"stderrB"}),
628 $num_failures{"stdoutB"}, plural($num_failures{"stdoutB"}),
njn734b8052007-11-01 04:40:37 +0000629 $num_failures{"post"}, plural($num_failures{"post"}));
njn9fb16f52003-04-23 17:47:13 +0000630
631 foreach my $failure (@failures) {
632 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000633 }
njn9fb16f52003-04-23 17:47:13 +0000634 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000635}
636
637#----------------------------------------------------------------------------
638# main(), sort of
639#----------------------------------------------------------------------------
sewardj91368992006-05-26 00:13:21 +0000640sub warn_about_EXTRA_REGTEST_OPTS()
641{
642 print "WARNING: \$EXTRA_REGTEST_OPTS is set. You probably don't want\n";
643 print "to run the regression tests with it set, unless you are doing some\n";
644 print "strange experiment, and/or you really know what you are doing.\n";
645 print "\n";
646}
njn25e49d8e72002-09-23 09:36:25 +0000647
daywalkerbb936982003-04-23 16:52:06 +0000648# nuke VALGRIND_OPTS
649$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000650
sewardj91368992006-05-26 00:13:21 +0000651if ($ENV{"EXTRA_REGTEST_OPTS"}) {
652 print "\n";
653 warn_about_EXTRA_REGTEST_OPTS();
654}
655
njn25e49d8e72002-09-23 09:36:25 +0000656my @fs = process_command_line();
657foreach my $f (@fs) {
658 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000659 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000660 } else {
661 # Allow the .vgtest suffix to be given or omitted
662 if ($f =~ /.vgtest$/ && -r $f) {
663 # do nothing
664 } elsif (-r "$f.vgtest") {
665 $f = "$f.vgtest";
666 } else {
667 die "`$f' neither a directory nor a readable test file/name\n"
668 }
669 my $dir = `dirname $f`; chomp $dir;
670 my $file = `basename $f`; chomp $file;
671 chdir($dir) or die "Could not change into $dir\n";
672 do_one_test($dir, $file);
673 chdir($tests_dir);
674 }
675}
676summarise_results();
677
sewardj91368992006-05-26 00:13:21 +0000678if ($ENV{"EXTRA_REGTEST_OPTS"}) {
679 warn_about_EXTRA_REGTEST_OPTS();
680}
681
njn0001fc32006-04-03 14:25:23 +0000682if (0 == $num_failures{"stdout"} &&
683 0 == $num_failures{"stderr"} &&
sewardj3b290482011-05-06 21:02:55 +0000684 0 == $num_failures{"stdoutB"} &&
685 0 == $num_failures{"stderrB"} &&
njn734b8052007-11-01 04:40:37 +0000686 0 == $num_failures{"post"}) {
nethercoteab422192004-03-01 08:27:37 +0000687 exit 0;
688} else {
689 exit 1;
690}
691
njnc2e7f482002-09-27 08:44:17 +0000692##--------------------------------------------------------------------##
693##--- end vg_regtest ---##
694##--------------------------------------------------------------------##