blob: 9c3a4d47d134a2f6df7359a4da0e80371d9c3a61 [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#
njn0e1b5142003-04-15 14:58:06 +00009# Copyright (C) 2003 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
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>) {
sewardjb5f6f512005-03-10 23:59:00 +0000184 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
185 next;
186 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
njn25e49d8e72002-09-23 09:36:25 +0000187 $vgopts = $1;
188 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000189 $prog = validate_program(".", $1, 0, 0);
njn25e49d8e72002-09-23 09:36:25 +0000190 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
191 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000192 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000193 $stdout_filter = validate_program(".", $1, 1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000194 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
nethercotef4928da2004-06-15 10:54:40 +0000195 $stderr_filter = validate_program(".", $1, 1, 1);
nethercotec5e1d802004-11-18 12:48:17 +0000196 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
197 $prereq = $1;
nethercote64bc5af2004-11-18 11:57:00 +0000198 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
199 $cleanup = $1;
njn25e49d8e72002-09-23 09:36:25 +0000200 } else {
201 die "Bad line in $f: $line\n";
202 }
203 }
204 close(INPUTFILE);
205
206 if (!defined $prog) {
nethercotef4928da2004-06-15 10:54:40 +0000207 $prog = ""; # allow no prog for testing error and --help cases
njn25e49d8e72002-09-23 09:36:25 +0000208 }
209}
210
211#----------------------------------------------------------------------------
212# Do one test
213#----------------------------------------------------------------------------
214# Since most of the program time is spent in system() calls, need this to
215# propagate a Ctrl-C enabling us to quit.
216sub mysystem($)
217{
218 (system($_[0]) != 2) or exit 1; # 2 is SIGINT
219}
220
nethercote137bc552003-11-14 17:47:54 +0000221# from a directory name like "/foo/cachesim/tests/" determine the tool name
222sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000223{
224 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000225 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000226 return $1;
227}
228
nethercote4592db62004-02-29 01:31:09 +0000229# Compare output against expected output; it should match at least one of
230# them.
231sub do_diffs($$$$)
232{
233 my ($fullname, $name, $mid, $f_exps) = @_;
234
235 for my $f_exp (@$f_exps) {
236 (-r $f_exp) or die "Could not read `$f_exp'\n";
237
238 my $n = "";
239 if ($f_exp =~ /.*\.exp(\d?)/) {
240 $n = $1;
241 } else {
242 $n = "";
243 ($f_exp eq "/dev/null") or die "Unexpected .exp file: $f_exp\n";
244 }
245
246 #print("diff -C0 $f_exp $name.$mid.out > $name.$mid.diff$n\n");
247 mysystem("diff -C0 $f_exp $name.$mid.out > $name.$mid.diff$n");
248
249 if (not -s "$name.$mid.diff$n") {
250 # A match; remove .out and any previously created .diff files.
251 unlink("$name.$mid.out");
252 unlink(<$name.$mid.diff*>);
253 return;
254 }
255 }
256 # If we reach here, none of the .exp files matched.
257 print "*** $name failed ($mid) ***\n";
258 push(@failures, sprintf("%-40s ($mid)", "$fullname"));
259 $num_failures{$mid}++;
260}
261
njn25e49d8e72002-09-23 09:36:25 +0000262sub do_one_test($$)
263{
264 my ($dir, $vgtest) = @_;
265 $vgtest =~ /^(.*)\.vgtest/;
266 my $name = $1;
267 my $fullname = "$dir/$name";
268
269 read_vgtest_file($vgtest);
270
nethercotec5e1d802004-11-18 12:48:17 +0000271 if (defined $prereq) {
272 if (system("$prereq") != 0) {
273 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
nethercote781bed42004-10-19 16:56:41 +0000274 return;
275 }
nethercoteb1affa82004-01-19 19:14:18 +0000276 }
277
njndb3c4692002-10-04 10:03:34 +0000278 printf("%-16s valgrind $vgopts $prog $args\n", "$name:");
njn25e49d8e72002-09-23 09:36:25 +0000279
nethercote137bc552003-11-14 17:47:54 +0000280 # Pass the appropriate --tool option for the directory (can be overridden
sewardjb5f6f512005-03-10 23:59:00 +0000281 # by an "args:" line, though).
nethercote137bc552003-11-14 17:47:54 +0000282 my $tool=determine_tool();
njn6a329422005-03-12 20:38:13 +0000283 mysystem("VALGRINDLIB=$tests_dir/.in_place $valgrind --command-line-only=yes --memcheck:leak-check=no --addrcheck:leak-check=no --tool=$tool $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000284
285 if (defined $stdout_filter) {
286 mysystem("$stdout_filter < $name.stdout.out > $tmp");
287 rename($tmp, "$name.stdout.out");
288 }
289
290 mysystem("$stderr_filter < $name.stderr.out > $tmp");
291 rename($tmp, "$name.stderr.out");
292
njn25e49d8e72002-09-23 09:36:25 +0000293
nethercote4592db62004-02-29 01:31:09 +0000294 # Find all the .stdout.exp files. If none, use /dev/null.
295 my @stdout_exps = <$name.stdout.exp*>;
296 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
njn25e49d8e72002-09-23 09:36:25 +0000297
nethercote4592db62004-02-29 01:31:09 +0000298 # Find all the .stderr.exp files. $name.stderr.exp must exist.
299 my @stderr_exps = <$name.stderr.exp*>;
300 (-r "$name.stderr.exp") or die "Could not read `$name.stderr.exp'\n";
301
302 do_diffs($fullname, $name, "stdout", \@stdout_exps);
303 do_diffs($fullname, $name, "stderr", \@stderr_exps);
nethercotec1f8ad92004-04-17 17:25:08 +0000304
nethercote64bc5af2004-11-18 11:57:00 +0000305 if (defined $cleanup) {
306 (system("$cleanup") == 0) or
307 print("(cleanup operation failed: $cleanup)\n");
nethercotec1f8ad92004-04-17 17:25:08 +0000308 }
309
njn9fb16f52003-04-23 17:47:13 +0000310 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000311}
312
313#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000314# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000315#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000316sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000317
njndb3c4692002-10-04 10:03:34 +0000318sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000319{
njndb3c4692002-10-04 10:03:34 +0000320 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000321 $dir =~ s/\/$//; # trim a trailing '/'
322
nethercote362c0d22004-11-18 18:21:56 +0000323 # Ignore dirs into which we should not recurse.
mueller92f0b802003-11-19 00:55:32 +0000324 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000325
nethercote362c0d22004-11-18 18:21:56 +0000326 # Ignore any dir whose name matches that of an architecture which is not
327 # the architecture we are running on (eg. when running on x86, ignore ppc/
328 # directories).
329 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
330 if (256 == system("$tests_dir/tests/cputest $dir")) { return; }
331
njn25e49d8e72002-09-23 09:36:25 +0000332 chdir($dir) or die "Could not change into $dir\n";
333
njn584eaac2002-10-04 15:30:48 +0000334 # Nb: Don't prepend a '/' to the base directory
335 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000336 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000337
njndb3c4692002-10-04 10:03:34 +0000338 my @fs = glob "*";
339 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
340 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
341
342 if ($found_tests) {
343 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000344 }
njn25cac76cb2002-09-23 11:21:57 +0000345 foreach my $f (@fs) {
346 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000347 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000348 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000349 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000350 }
njn25e49d8e72002-09-23 09:36:25 +0000351 }
njndb3c4692002-10-04 10:03:34 +0000352 if ($found_tests) {
353 print "-- Finished tests in $full_dir $dashes\n";
354 }
355
njn25e49d8e72002-09-23 09:36:25 +0000356 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000357}
358
359#----------------------------------------------------------------------------
360# Summarise results
361#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000362sub plural($)
363{
364 return ( $_[0] == 1 ? "" : "s" );
365}
366
njn25e49d8e72002-09-23 09:36:25 +0000367sub summarise_results
368{
njnd8ced862003-04-08 00:47:05 +0000369 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000370
njn9fb16f52003-04-23 17:47:13 +0000371 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
372 $num_tests_done, plural($num_tests_done),
373 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
374 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
375
376 foreach my $failure (@failures) {
377 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000378 }
njn9fb16f52003-04-23 17:47:13 +0000379 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000380}
381
382#----------------------------------------------------------------------------
383# main(), sort of
384#----------------------------------------------------------------------------
385
daywalkerbb936982003-04-23 16:52:06 +0000386# nuke VALGRIND_OPTS
387$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000388
389my @fs = process_command_line();
390foreach my $f (@fs) {
391 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000392 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000393 } else {
394 # Allow the .vgtest suffix to be given or omitted
395 if ($f =~ /.vgtest$/ && -r $f) {
396 # do nothing
397 } elsif (-r "$f.vgtest") {
398 $f = "$f.vgtest";
399 } else {
400 die "`$f' neither a directory nor a readable test file/name\n"
401 }
402 my $dir = `dirname $f`; chomp $dir;
403 my $file = `basename $f`; chomp $file;
404 chdir($dir) or die "Could not change into $dir\n";
405 do_one_test($dir, $file);
406 chdir($tests_dir);
407 }
408}
409summarise_results();
410
nethercoteab422192004-03-01 08:27:37 +0000411if (0 == $num_failures{"stdout"} && 0 == $num_failures{"stderr"}) {
412 exit 0;
413} else {
414 exit 1;
415}
416
njnc2e7f482002-09-27 08:44:17 +0000417##--------------------------------------------------------------------##
418##--- end vg_regtest ---##
419##--------------------------------------------------------------------##