blob: 1ac35158034d847614c9d32a5cc3cf77cdc121d4 [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
njnc9539842002-10-02 13:26:35 +00006# This file is part of Valgrind, an extensible x86 protected-mode
7# emulator for monitoring program execution on x86-Unixes.
njnc2e7f482002-09-27 08:44:17 +00008#
njn0e1b5142003-04-15 14:58:06 +00009# Copyright (C) 2003 Nicholas Nethercote
njnc2e7f482002-09-27 08:44:17 +000010# njn25@cam.ac.uk
11#
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
njnc2e7f482002-09-27 08:44:17 +000034# --valgrind: valgrind to use. Default is one built from this source tree.
njna217f3d2002-09-27 08:26:27 +000035#
njn4ba5a792002-09-30 10:23:54 +000036# The easiest way is to run all tests in valgrind/ with (assuming you installed
37# in $PREFIX):
38#
39# $PREFIX/bin/vg_regtest --all
40#
njna217f3d2002-09-27 08:26:27 +000041# You can specify individual files to test, or whole directories, or both.
jsgf855d93d2003-10-13 22:26:55 +000042# Directories are traversed recursively, except for ones named, for example,
43# CVS/ or docs/.
njn25e49d8e72002-09-23 09:36:25 +000044#
45# Each test is defined in a file <test>.vgtest, containing one or more of the
njna217f3d2002-09-27 08:26:27 +000046# following lines, in any order:
njn25e49d8e72002-09-23 09:36:25 +000047# - prog: <prog to run> (compulsory)
48# - args: <args for prog> (default: none)
49# - vgopts: <Valgrind options> (default: none)
50# - stdout_filter: <filter to run stdout through> (default: none)
njna217f3d2002-09-27 08:26:27 +000051# - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
nethercotec5e1d802004-11-18 12:48:17 +000052# - prereq: <prerequisite command> (default: none)
nethercote64bc5af2004-11-18 11:57:00 +000053# - cleanup: <post-test cleanup cmd to run> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000054#
njna217f3d2002-09-27 08:26:27 +000055# Note that filters are necessary for stderr results to filter out things that
56# always change, eg. process id numbers.
njn25e49d8e72002-09-23 09:36:25 +000057#
nethercote4592db62004-02-29 01:31:09 +000058# Expected stdout (filtered) is kept in <test>.stdout.exp[0-9]? (can be more
59# than one expected output). It can be missing if it would be empty. Expected
60# stderr (filtered) is kept in <test>.stderr.exp[0-9]?.
njn25e49d8e72002-09-23 09:36:25 +000061#
nethercotec5e1d802004-11-18 12:48:17 +000062# The prerequisite command, if present, must return 0 otherwise the test is
63# skipped.
64#
njn25e49d8e72002-09-23 09:36:25 +000065# If results don't match, the output can be found in <test>.std<strm>.out,
nethercote4592db62004-02-29 01:31:09 +000066# and the diff between expected and actual in <test>.std<strm>.diff[0-9]?.
njn25e49d8e72002-09-23 09:36:25 +000067#
nethercote137bc552003-11-14 17:47:54 +000068# Notes on adding regression tests for a new tool are in
69# coregrind/docs/coregrind_tools.html.
njn25e49d8e72002-09-23 09:36:25 +000070#----------------------------------------------------------------------------
71
njn9fb16f52003-04-23 17:47:13 +000072use warnings;
njn25e49d8e72002-09-23 09:36:25 +000073use strict;
74
75#----------------------------------------------------------------------------
76# Global vars
77#----------------------------------------------------------------------------
njnd8ced862003-04-08 00:47:05 +000078my $usage="vg_regtest [--all, --valgrind]\n";
njn25e49d8e72002-09-23 09:36:25 +000079
80my $tmp="vg_regtest.tmp.$$";
81
82# Test variables
83my $vgopts; # valgrind options
84my $prog; # test prog
85my $args; # test prog args
86my $stdout_filter; # filter program to run stdout results file through
87my $stderr_filter; # filter program to run stderr results file through
nethercotec5e1d802004-11-18 12:48:17 +000088my $prereq; # prerequisite test to satisfy before running test
nethercote64bc5af2004-11-18 11:57:00 +000089my $cleanup; # cleanup command to run
njn25e49d8e72002-09-23 09:36:25 +000090
91my @failures; # List of failed tests
92
njn9fb16f52003-04-23 17:47:13 +000093my $num_tests_done = 0;
94my %num_failures = (stderr => 0, stdout => 0);
njn25e49d8e72002-09-23 09:36:25 +000095
njn71fe3e62003-04-23 21:48:20 +000096# Default valgrind to use is this build tree's (uninstalled) one
njn71fe3e62003-04-23 21:48:20 +000097my $valgrind = "./coregrind/valgrind";
njn25e49d8e72002-09-23 09:36:25 +000098
99chomp(my $tests_dir = `pwd`);
100
101# default filter is the one named "filter_stderr" in the test's directory
102my $default_stderr_filter = "filter_stderr";
103
104
105#----------------------------------------------------------------------------
106# Process command line, setup
107#----------------------------------------------------------------------------
108
109# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
110#
111# 1. Can prepend "." onto programs to avoid trouble with users who don't have
112# "." in their path (by making $dir = ".")
113# 2. Can prepend the current dir to make the command absolute to avoid
114# subsequent trouble when we change directories.
115#
116# Also checks the program exists and is executable.
nethercotef4928da2004-06-15 10:54:40 +0000117sub validate_program ($$$$)
njn25e49d8e72002-09-23 09:36:25 +0000118{
nethercotef4928da2004-06-15 10:54:40 +0000119 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000120
121 # If absolute path, leave it alone. If relative, make it
122 # absolute -- by prepending current dir -- so we can change
123 # dirs and still use it.
124 $prog = "$dir/$prog" if ($prog !~ /^\//);
nethercotef4928da2004-06-15 10:54:40 +0000125 if ($must_exist) {
126 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
127 }
njn71fe3e62003-04-23 21:48:20 +0000128 if ($must_be_executable) {
nethercotef4928da2004-06-15 10:54:40 +0000129 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
njn71fe3e62003-04-23 21:48:20 +0000130 }
njn25e49d8e72002-09-23 09:36:25 +0000131
132 return $prog;
133}
134
135sub process_command_line()
136{
137 my $alldirs = 0;
138 my @fs;
139
140 for my $arg (@ARGV) {
141 if ($arg =~ /^-/) {
njnd8ced862003-04-08 00:47:05 +0000142 if ($arg =~ /^--all$/) {
njn25e49d8e72002-09-23 09:36:25 +0000143 $alldirs = 1;
144 } elsif ($arg =~ /^--valgrind=(.*)$/) {
145 $valgrind = $1;
146 } else {
147 die $usage;
148 }
149 } else {
150 push(@fs, $arg);
151 }
152 }
nethercotef4928da2004-06-15 10:54:40 +0000153 $valgrind = validate_program($tests_dir, $valgrind, 1, 0);
njn25e49d8e72002-09-23 09:36:25 +0000154
155 if ($alldirs) {
156 @fs = ();
157 foreach my $f (glob "*") {
158 push(@fs, $f) if (-d $f);
159 }
160 }
161
162 (0 != @fs) or die "No test files or directories specified\n";
163
164 return @fs;
165}
166
167#----------------------------------------------------------------------------
168# Read a .vgtest file
169#----------------------------------------------------------------------------
170sub read_vgtest_file($)
171{
172 my ($f) = @_;
173
174 # Defaults.
nethercotec5e1d802004-11-18 12:48:17 +0000175 ($vgopts, $prog, $args, $stdout_filter, $stderr_filter, $prereq, $cleanup)
nethercotec1f8ad92004-04-17 17:25:08 +0000176 = ("", undef, "", undef, undef, undef, undef);
njn25e49d8e72002-09-23 09:36:25 +0000177
178 # Every test directory must have a "filter_stderr"
nethercotef4928da2004-06-15 10:54:40 +0000179 $stderr_filter = validate_program(".", $default_stderr_filter, 1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000180
181 open(INPUTFILE, "< $f") || die "File $f not openable\n";
182
183 while (my $line = <INPUTFILE>) {
184 if ($line =~ /^\s*vgopts:\s*(.*)$/) {
185 $vgopts = $1;
186 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000187 $prog = validate_program(".", $1, 0, 0);
njn25e49d8e72002-09-23 09:36:25 +0000188 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
189 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000190 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000191 $stdout_filter = validate_program(".", $1, 1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000192 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000193 $stderr_filter = validate_program(".", $1, 1, 1);
nethercotec5e1d802004-11-18 12:48:17 +0000194 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
195 $prereq = $1;
nethercote64bc5af2004-11-18 11:57:00 +0000196 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
197 $cleanup = $1;
njn25e49d8e72002-09-23 09:36:25 +0000198 } else {
199 die "Bad line in $f: $line\n";
200 }
201 }
202 close(INPUTFILE);
203
204 if (!defined $prog) {
nethercotef4928da2004-06-15 10:54:40 +0000205 $prog = ""; # allow no prog for testing error and --help cases
njn25e49d8e72002-09-23 09:36:25 +0000206 }
207}
208
209#----------------------------------------------------------------------------
210# Do one test
211#----------------------------------------------------------------------------
212# Since most of the program time is spent in system() calls, need this to
213# propagate a Ctrl-C enabling us to quit.
214sub mysystem($)
215{
216 (system($_[0]) != 2) or exit 1; # 2 is SIGINT
217}
218
nethercote137bc552003-11-14 17:47:54 +0000219# from a directory name like "/foo/cachesim/tests/" determine the tool name
220sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000221{
222 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000223 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000224 return $1;
225}
226
nethercote4592db62004-02-29 01:31:09 +0000227# Compare output against expected output; it should match at least one of
228# them.
229sub do_diffs($$$$)
230{
231 my ($fullname, $name, $mid, $f_exps) = @_;
232
233 for my $f_exp (@$f_exps) {
234 (-r $f_exp) or die "Could not read `$f_exp'\n";
235
236 my $n = "";
237 if ($f_exp =~ /.*\.exp(\d?)/) {
238 $n = $1;
239 } else {
240 $n = "";
241 ($f_exp eq "/dev/null") or die "Unexpected .exp file: $f_exp\n";
242 }
243
244 #print("diff -C0 $f_exp $name.$mid.out > $name.$mid.diff$n\n");
245 mysystem("diff -C0 $f_exp $name.$mid.out > $name.$mid.diff$n");
246
247 if (not -s "$name.$mid.diff$n") {
248 # A match; remove .out and any previously created .diff files.
249 unlink("$name.$mid.out");
250 unlink(<$name.$mid.diff*>);
251 return;
252 }
253 }
254 # If we reach here, none of the .exp files matched.
255 print "*** $name failed ($mid) ***\n";
256 push(@failures, sprintf("%-40s ($mid)", "$fullname"));
257 $num_failures{$mid}++;
258}
259
njn25e49d8e72002-09-23 09:36:25 +0000260sub do_one_test($$)
261{
262 my ($dir, $vgtest) = @_;
263 $vgtest =~ /^(.*)\.vgtest/;
264 my $name = $1;
265 my $fullname = "$dir/$name";
266
267 read_vgtest_file($vgtest);
268
nethercotec5e1d802004-11-18 12:48:17 +0000269 if (defined $prereq) {
270 if (system("$prereq") != 0) {
271 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
nethercote781bed42004-10-19 16:56:41 +0000272 return;
273 }
nethercoteb1affa82004-01-19 19:14:18 +0000274 }
275
njndb3c4692002-10-04 10:03:34 +0000276 printf("%-16s valgrind $vgopts $prog $args\n", "$name:");
njn25e49d8e72002-09-23 09:36:25 +0000277
nethercote137bc552003-11-14 17:47:54 +0000278 # Pass the appropriate --tool option for the directory (can be overridden
njn71fe3e62003-04-23 21:48:20 +0000279 # by an "args:" or "args.dev:" line, though).
nethercote137bc552003-11-14 17:47:54 +0000280 my $tool=determine_tool();
fitzhardinge98abfc72003-12-16 02:05:15 +0000281 mysystem("VALGRINDLIB=$tests_dir/.in_place $valgrind --tool=$tool $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000282
283 if (defined $stdout_filter) {
284 mysystem("$stdout_filter < $name.stdout.out > $tmp");
285 rename($tmp, "$name.stdout.out");
286 }
287
288 mysystem("$stderr_filter < $name.stderr.out > $tmp");
289 rename($tmp, "$name.stderr.out");
290
njn25e49d8e72002-09-23 09:36:25 +0000291
nethercote4592db62004-02-29 01:31:09 +0000292 # Find all the .stdout.exp files. If none, use /dev/null.
293 my @stdout_exps = <$name.stdout.exp*>;
294 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
njn25e49d8e72002-09-23 09:36:25 +0000295
nethercote4592db62004-02-29 01:31:09 +0000296 # Find all the .stderr.exp files. $name.stderr.exp must exist.
297 my @stderr_exps = <$name.stderr.exp*>;
298 (-r "$name.stderr.exp") or die "Could not read `$name.stderr.exp'\n";
299
300 do_diffs($fullname, $name, "stdout", \@stdout_exps);
301 do_diffs($fullname, $name, "stderr", \@stderr_exps);
nethercotec1f8ad92004-04-17 17:25:08 +0000302
nethercote64bc5af2004-11-18 11:57:00 +0000303 if (defined $cleanup) {
304 (system("$cleanup") == 0) or
305 print("(cleanup operation failed: $cleanup)\n");
nethercotec1f8ad92004-04-17 17:25:08 +0000306 }
307
njn9fb16f52003-04-23 17:47:13 +0000308 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000309}
310
311#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000312# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000313#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000314sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000315
njndb3c4692002-10-04 10:03:34 +0000316sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000317{
njndb3c4692002-10-04 10:03:34 +0000318 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000319 $dir =~ s/\/$//; # trim a trailing '/'
320
nethercote362c0d22004-11-18 18:21:56 +0000321 # Ignore dirs into which we should not recurse.
mueller92f0b802003-11-19 00:55:32 +0000322 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000323
nethercote362c0d22004-11-18 18:21:56 +0000324 # Ignore any dir whose name matches that of an architecture which is not
325 # the architecture we are running on (eg. when running on x86, ignore ppc/
326 # directories).
327 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
328 if (256 == system("$tests_dir/tests/cputest $dir")) { return; }
329
njn25e49d8e72002-09-23 09:36:25 +0000330 chdir($dir) or die "Could not change into $dir\n";
331
njn584eaac2002-10-04 15:30:48 +0000332 # Nb: Don't prepend a '/' to the base directory
333 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000334 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000335
njndb3c4692002-10-04 10:03:34 +0000336 my @fs = glob "*";
337 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
338 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
339
340 if ($found_tests) {
341 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000342 }
njn25cac76cb2002-09-23 11:21:57 +0000343 foreach my $f (@fs) {
344 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000345 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000346 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000347 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000348 }
njn25e49d8e72002-09-23 09:36:25 +0000349 }
njndb3c4692002-10-04 10:03:34 +0000350 if ($found_tests) {
351 print "-- Finished tests in $full_dir $dashes\n";
352 }
353
njn25e49d8e72002-09-23 09:36:25 +0000354 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000355}
356
357#----------------------------------------------------------------------------
358# Summarise results
359#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000360sub plural($)
361{
362 return ( $_[0] == 1 ? "" : "s" );
363}
364
njn25e49d8e72002-09-23 09:36:25 +0000365sub summarise_results
366{
njnd8ced862003-04-08 00:47:05 +0000367 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000368
njn9fb16f52003-04-23 17:47:13 +0000369 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
370 $num_tests_done, plural($num_tests_done),
371 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
372 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
373
374 foreach my $failure (@failures) {
375 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000376 }
njn9fb16f52003-04-23 17:47:13 +0000377 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000378}
379
380#----------------------------------------------------------------------------
381# main(), sort of
382#----------------------------------------------------------------------------
383
daywalkerbb936982003-04-23 16:52:06 +0000384# nuke VALGRIND_OPTS
385$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000386
387my @fs = process_command_line();
388foreach my $f (@fs) {
389 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000390 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000391 } else {
392 # Allow the .vgtest suffix to be given or omitted
393 if ($f =~ /.vgtest$/ && -r $f) {
394 # do nothing
395 } elsif (-r "$f.vgtest") {
396 $f = "$f.vgtest";
397 } else {
398 die "`$f' neither a directory nor a readable test file/name\n"
399 }
400 my $dir = `dirname $f`; chomp $dir;
401 my $file = `basename $f`; chomp $file;
402 chdir($dir) or die "Could not change into $dir\n";
403 do_one_test($dir, $file);
404 chdir($tests_dir);
405 }
406}
407summarise_results();
408
nethercoteab422192004-03-01 08:27:37 +0000409if (0 == $num_failures{"stdout"} && 0 == $num_failures{"stderr"}) {
410 exit 0;
411} else {
412 exit 1;
413}
414
njnc2e7f482002-09-27 08:44:17 +0000415##--------------------------------------------------------------------##
416##--- end vg_regtest ---##
417##--------------------------------------------------------------------##