blob: 2aa689bdba4a6d9d1424bd0f433444adea59b158 [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)
njn25e49d8e72002-09-23 09:36:25 +000052#
njna217f3d2002-09-27 08:26:27 +000053# Note that filters are necessary for stderr results to filter out things that
54# always change, eg. process id numbers.
njn25e49d8e72002-09-23 09:36:25 +000055#
njna217f3d2002-09-27 08:26:27 +000056# Expected stdout (filtered) is kept in <test>.stdout.exp. It can be missing
njnd8ced862003-04-08 00:47:05 +000057# if it would be empty. Expected stderr (filtered) is kept in
58# <test>.stderr.exp.
njn25e49d8e72002-09-23 09:36:25 +000059#
60# If results don't match, the output can be found in <test>.std<strm>.out,
61# and the diff between expected and actual in <test>.std<strm>.diff.
62#
nethercote137bc552003-11-14 17:47:54 +000063# Notes on adding regression tests for a new tool are in
64# coregrind/docs/coregrind_tools.html.
njn25e49d8e72002-09-23 09:36:25 +000065#----------------------------------------------------------------------------
66
njn9fb16f52003-04-23 17:47:13 +000067use warnings;
njn25e49d8e72002-09-23 09:36:25 +000068use strict;
69
70#----------------------------------------------------------------------------
71# Global vars
72#----------------------------------------------------------------------------
njnd8ced862003-04-08 00:47:05 +000073my $usage="vg_regtest [--all, --valgrind]\n";
njn25e49d8e72002-09-23 09:36:25 +000074
75my $tmp="vg_regtest.tmp.$$";
76
77# Test variables
78my $vgopts; # valgrind options
79my $prog; # test prog
80my $args; # test prog args
81my $stdout_filter; # filter program to run stdout results file through
82my $stderr_filter; # filter program to run stderr results file through
83
84my @failures; # List of failed tests
85
njn9fb16f52003-04-23 17:47:13 +000086my $num_tests_done = 0;
87my %num_failures = (stderr => 0, stdout => 0);
njn25e49d8e72002-09-23 09:36:25 +000088
njn71fe3e62003-04-23 21:48:20 +000089# Default valgrind to use is this build tree's (uninstalled) one
njnc2e7f482002-09-27 08:44:17 +000090my $prefix="@prefix@";
91my $exec_prefix="@exec_prefix@";
njn71fe3e62003-04-23 21:48:20 +000092my $valgrind = "./coregrind/valgrind";
njn25e49d8e72002-09-23 09:36:25 +000093
94chomp(my $tests_dir = `pwd`);
95
96# default filter is the one named "filter_stderr" in the test's directory
97my $default_stderr_filter = "filter_stderr";
98
99
100#----------------------------------------------------------------------------
101# Process command line, setup
102#----------------------------------------------------------------------------
103
104# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
105#
106# 1. Can prepend "." onto programs to avoid trouble with users who don't have
107# "." in their path (by making $dir = ".")
108# 2. Can prepend the current dir to make the command absolute to avoid
109# subsequent trouble when we change directories.
110#
111# Also checks the program exists and is executable.
njn71fe3e62003-04-23 21:48:20 +0000112sub validate_program ($$$)
njn25e49d8e72002-09-23 09:36:25 +0000113{
njn71fe3e62003-04-23 21:48:20 +0000114 my ($dir, $prog, $must_be_executable) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000115
116 # If absolute path, leave it alone. If relative, make it
117 # absolute -- by prepending current dir -- so we can change
118 # dirs and still use it.
119 $prog = "$dir/$prog" if ($prog !~ /^\//);
njn71fe3e62003-04-23 21:48:20 +0000120 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
121 if ($must_be_executable) {
122 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
123 }
njn25e49d8e72002-09-23 09:36:25 +0000124
125 return $prog;
126}
127
128sub process_command_line()
129{
130 my $alldirs = 0;
131 my @fs;
132
133 for my $arg (@ARGV) {
134 if ($arg =~ /^-/) {
njnd8ced862003-04-08 00:47:05 +0000135 if ($arg =~ /^--all$/) {
njn25e49d8e72002-09-23 09:36:25 +0000136 $alldirs = 1;
137 } elsif ($arg =~ /^--valgrind=(.*)$/) {
138 $valgrind = $1;
139 } else {
140 die $usage;
141 }
142 } else {
143 push(@fs, $arg);
144 }
145 }
njn71fe3e62003-04-23 21:48:20 +0000146 $valgrind = validate_program($tests_dir, $valgrind, 0);
njn25e49d8e72002-09-23 09:36:25 +0000147
148 if ($alldirs) {
149 @fs = ();
150 foreach my $f (glob "*") {
151 push(@fs, $f) if (-d $f);
152 }
153 }
154
155 (0 != @fs) or die "No test files or directories specified\n";
156
157 return @fs;
158}
159
160#----------------------------------------------------------------------------
161# Read a .vgtest file
162#----------------------------------------------------------------------------
163sub read_vgtest_file($)
164{
165 my ($f) = @_;
166
167 # Defaults.
168 ($vgopts, $prog, $args, $stdout_filter, $stderr_filter) =
169 ("", undef, "", undef, undef);
170
171 # Every test directory must have a "filter_stderr"
njn71fe3e62003-04-23 21:48:20 +0000172 $stderr_filter = validate_program(".", $default_stderr_filter, 1);
njn25e49d8e72002-09-23 09:36:25 +0000173
174 open(INPUTFILE, "< $f") || die "File $f not openable\n";
175
176 while (my $line = <INPUTFILE>) {
177 if ($line =~ /^\s*vgopts:\s*(.*)$/) {
178 $vgopts = $1;
179 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000180 $prog = validate_program(".", $1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000181 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
182 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000183 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000184 $stdout_filter = validate_program(".", $1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000185 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000186 $stderr_filter = validate_program(".", $1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000187 } else {
188 die "Bad line in $f: $line\n";
189 }
190 }
191 close(INPUTFILE);
192
193 if (!defined $prog) {
194 die "no `prog:' line in `$f'\n";
195 }
196}
197
198#----------------------------------------------------------------------------
199# Do one test
200#----------------------------------------------------------------------------
201# Since most of the program time is spent in system() calls, need this to
202# propagate a Ctrl-C enabling us to quit.
203sub mysystem($)
204{
205 (system($_[0]) != 2) or exit 1; # 2 is SIGINT
206}
207
nethercote137bc552003-11-14 17:47:54 +0000208# from a directory name like "/foo/cachesim/tests/" determine the tool name
209sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000210{
211 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000212 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000213 return $1;
214}
215
njn25e49d8e72002-09-23 09:36:25 +0000216sub do_one_test($$)
217{
218 my ($dir, $vgtest) = @_;
219 $vgtest =~ /^(.*)\.vgtest/;
220 my $name = $1;
221 my $fullname = "$dir/$name";
222
223 read_vgtest_file($vgtest);
224
njndb3c4692002-10-04 10:03:34 +0000225 printf("%-16s valgrind $vgopts $prog $args\n", "$name:");
njn25e49d8e72002-09-23 09:36:25 +0000226
nethercote137bc552003-11-14 17:47:54 +0000227 # Pass the appropriate --tool option for the directory (can be overridden
njn71fe3e62003-04-23 21:48:20 +0000228 # by an "args:" or "args.dev:" line, though).
229 # Also, because the default valgrind is coregrind/valgrind which isn't
230 # executable, prepend `sh'.
nethercote137bc552003-11-14 17:47:54 +0000231 my $tool=determine_tool();
232 mysystem("sh $valgrind --tool=$tool --in-place=$tests_dir $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000233
234 if (defined $stdout_filter) {
235 mysystem("$stdout_filter < $name.stdout.out > $tmp");
236 rename($tmp, "$name.stdout.out");
237 }
238
239 mysystem("$stderr_filter < $name.stderr.out > $tmp");
240 rename($tmp, "$name.stderr.out");
241
242 # If stdout expected empty, .exp file might be missing so diff with
243 # /dev/null
244 my $stdout_exp = ( -r "$name.stdout.exp"
245 ? "$name.stdout.exp"
246 : "/dev/null" );
247
njnd8ced862003-04-08 00:47:05 +0000248 my $stderr_exp = "$name.stderr.exp";
njn25e49d8e72002-09-23 09:36:25 +0000249 (-r $stderr_exp) or die "Could not read `$stderr_exp'\n";
250
251 mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff");
252 mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff");
253
254 for my $ext ("stdout", "stderr") {
255 if (-s "$name.$ext.diff") {
njndb3c4692002-10-04 10:03:34 +0000256 print "*** $name failed ($ext) ***\n";
njn9fb16f52003-04-23 17:47:13 +0000257 push(@failures, sprintf("%-40s ($ext)", "$fullname"));
258 $num_failures{$ext}++;
njn25e49d8e72002-09-23 09:36:25 +0000259 } else {
260 unlink("$name.$ext.out", "$name.$ext.diff");
261 }
262 }
njn9fb16f52003-04-23 17:47:13 +0000263 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000264}
265
266#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000267# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000268#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000269sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000270
njndb3c4692002-10-04 10:03:34 +0000271sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000272{
njndb3c4692002-10-04 10:03:34 +0000273 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000274 $dir =~ s/\/$//; # trim a trailing '/'
275
jsgf855d93d2003-10-13 22:26:55 +0000276 # ignore dirs into which we should not recurse
mueller92f0b802003-11-19 00:55:32 +0000277 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000278
njn25e49d8e72002-09-23 09:36:25 +0000279 chdir($dir) or die "Could not change into $dir\n";
280
njn584eaac2002-10-04 15:30:48 +0000281 # Nb: Don't prepend a '/' to the base directory
282 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000283 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000284
njndb3c4692002-10-04 10:03:34 +0000285 my @fs = glob "*";
286 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
287 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
288
289 if ($found_tests) {
290 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000291 }
njn25cac76cb2002-09-23 11:21:57 +0000292 foreach my $f (@fs) {
293 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000294 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000295 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000296 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000297 }
njn25e49d8e72002-09-23 09:36:25 +0000298 }
njndb3c4692002-10-04 10:03:34 +0000299 if ($found_tests) {
300 print "-- Finished tests in $full_dir $dashes\n";
301 }
302
njn25e49d8e72002-09-23 09:36:25 +0000303 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000304}
305
306#----------------------------------------------------------------------------
307# Summarise results
308#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000309sub plural($)
310{
311 return ( $_[0] == 1 ? "" : "s" );
312}
313
njn25e49d8e72002-09-23 09:36:25 +0000314sub summarise_results
315{
njnd8ced862003-04-08 00:47:05 +0000316 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000317
njn9fb16f52003-04-23 17:47:13 +0000318 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
319 $num_tests_done, plural($num_tests_done),
320 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
321 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
322
323 foreach my $failure (@failures) {
324 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000325 }
njn9fb16f52003-04-23 17:47:13 +0000326 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000327}
328
329#----------------------------------------------------------------------------
330# main(), sort of
331#----------------------------------------------------------------------------
332
daywalkerbb936982003-04-23 16:52:06 +0000333# nuke VALGRIND_OPTS
334$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000335
336my @fs = process_command_line();
337foreach my $f (@fs) {
338 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000339 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000340 } else {
341 # Allow the .vgtest suffix to be given or omitted
342 if ($f =~ /.vgtest$/ && -r $f) {
343 # do nothing
344 } elsif (-r "$f.vgtest") {
345 $f = "$f.vgtest";
346 } else {
347 die "`$f' neither a directory nor a readable test file/name\n"
348 }
349 my $dir = `dirname $f`; chomp $dir;
350 my $file = `basename $f`; chomp $file;
351 chdir($dir) or die "Could not change into $dir\n";
352 do_one_test($dir, $file);
353 chdir($tests_dir);
354 }
355}
356summarise_results();
357
njnc2e7f482002-09-27 08:44:17 +0000358##--------------------------------------------------------------------##
359##--- end vg_regtest ---##
360##--------------------------------------------------------------------##