blob: 33ab30baf71208cf4f453b383e9132cb1306b75d [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#
njn78660312005-06-29 04:02:24 +000058# Expected stdout (filtered) is kept in <test>.stdout.exp[0-9]* (can be more
nethercote4592db62004-02-29 01:31:09 +000059# than one expected output). It can be missing if it would be empty. Expected
njn78660312005-06-29 04:02:24 +000060# 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,
njn78660312005-06-29 04:02:24 +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 = "";
njn78660312005-06-29 04:02:24 +0000239 if ($f_exp =~ /.*\.exp(\d*)/) {
nethercote4592db62004-02-29 01:31:09 +0000240 $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
njnefc94ad2005-11-12 16:08:09 +0000281 # by an "args:" line, though). Set both VALGRIND_LIB and
282 # VALGRIND_LIB_INNER in case this Valgrind was configured with
283 # --enable-inner.
nethercote137bc552003-11-14 17:47:54 +0000284 my $tool=determine_tool();
njnefc94ad2005-11-12 16:08:09 +0000285 mysystem("VALGRIND_LIB=$tests_dir/.in_place VALGRIND_LIB_INNER=$tests_dir/.in_place "
286 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
287 . "--addrcheck:leak-check=no --tool=$tool $vgopts "
288 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000289
290 if (defined $stdout_filter) {
291 mysystem("$stdout_filter < $name.stdout.out > $tmp");
292 rename($tmp, "$name.stdout.out");
293 }
294
295 mysystem("$stderr_filter < $name.stderr.out > $tmp");
296 rename($tmp, "$name.stderr.out");
297
njn25e49d8e72002-09-23 09:36:25 +0000298
nethercote4592db62004-02-29 01:31:09 +0000299 # Find all the .stdout.exp files. If none, use /dev/null.
300 my @stdout_exps = <$name.stdout.exp*>;
301 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
njn25e49d8e72002-09-23 09:36:25 +0000302
nethercote4592db62004-02-29 01:31:09 +0000303 # Find all the .stderr.exp files. $name.stderr.exp must exist.
304 my @stderr_exps = <$name.stderr.exp*>;
305 (-r "$name.stderr.exp") or die "Could not read `$name.stderr.exp'\n";
306
307 do_diffs($fullname, $name, "stdout", \@stdout_exps);
308 do_diffs($fullname, $name, "stderr", \@stderr_exps);
nethercotec1f8ad92004-04-17 17:25:08 +0000309
nethercote64bc5af2004-11-18 11:57:00 +0000310 if (defined $cleanup) {
311 (system("$cleanup") == 0) or
312 print("(cleanup operation failed: $cleanup)\n");
nethercotec1f8ad92004-04-17 17:25:08 +0000313 }
314
njn9fb16f52003-04-23 17:47:13 +0000315 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000316}
317
318#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000319# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000320#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000321sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000322
njndb3c4692002-10-04 10:03:34 +0000323sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000324{
njndb3c4692002-10-04 10:03:34 +0000325 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000326 $dir =~ s/\/$//; # trim a trailing '/'
327
nethercote362c0d22004-11-18 18:21:56 +0000328 # Ignore dirs into which we should not recurse.
mueller92f0b802003-11-19 00:55:32 +0000329 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000330
nethercote362c0d22004-11-18 18:21:56 +0000331 # Ignore any dir whose name matches that of an architecture which is not
332 # the architecture we are running on (eg. when running on x86, ignore ppc/
333 # directories).
334 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
335 if (256 == system("$tests_dir/tests/cputest $dir")) { return; }
336
njn25e49d8e72002-09-23 09:36:25 +0000337 chdir($dir) or die "Could not change into $dir\n";
338
njn584eaac2002-10-04 15:30:48 +0000339 # Nb: Don't prepend a '/' to the base directory
340 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000341 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000342
njndb3c4692002-10-04 10:03:34 +0000343 my @fs = glob "*";
344 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
345 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
346
347 if ($found_tests) {
348 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000349 }
njn25cac76cb2002-09-23 11:21:57 +0000350 foreach my $f (@fs) {
351 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000352 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000353 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000354 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000355 }
njn25e49d8e72002-09-23 09:36:25 +0000356 }
njndb3c4692002-10-04 10:03:34 +0000357 if ($found_tests) {
358 print "-- Finished tests in $full_dir $dashes\n";
359 }
360
njn25e49d8e72002-09-23 09:36:25 +0000361 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000362}
363
364#----------------------------------------------------------------------------
365# Summarise results
366#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000367sub plural($)
368{
369 return ( $_[0] == 1 ? "" : "s" );
370}
371
njn25e49d8e72002-09-23 09:36:25 +0000372sub summarise_results
373{
njnd8ced862003-04-08 00:47:05 +0000374 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000375
njn9fb16f52003-04-23 17:47:13 +0000376 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
377 $num_tests_done, plural($num_tests_done),
378 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
379 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
380
381 foreach my $failure (@failures) {
382 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000383 }
njn9fb16f52003-04-23 17:47:13 +0000384 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000385}
386
387#----------------------------------------------------------------------------
388# main(), sort of
389#----------------------------------------------------------------------------
390
daywalkerbb936982003-04-23 16:52:06 +0000391# nuke VALGRIND_OPTS
392$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000393
394my @fs = process_command_line();
395foreach my $f (@fs) {
396 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000397 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000398 } else {
399 # Allow the .vgtest suffix to be given or omitted
400 if ($f =~ /.vgtest$/ && -r $f) {
401 # do nothing
402 } elsif (-r "$f.vgtest") {
403 $f = "$f.vgtest";
404 } else {
405 die "`$f' neither a directory nor a readable test file/name\n"
406 }
407 my $dir = `dirname $f`; chomp $dir;
408 my $file = `basename $f`; chomp $file;
409 chdir($dir) or die "Could not change into $dir\n";
410 do_one_test($dir, $file);
411 chdir($tests_dir);
412 }
413}
414summarise_results();
415
nethercoteab422192004-03-01 08:27:37 +0000416if (0 == $num_failures{"stdout"} && 0 == $num_failures{"stderr"}) {
417 exit 0;
418} else {
419 exit 1;
420}
421
njnc2e7f482002-09-27 08:44:17 +0000422##--------------------------------------------------------------------##
423##--- end vg_regtest ---##
424##--------------------------------------------------------------------##