blob: 965df82f3d1fabffe24a1ac0f39f7b1d7e262072 [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)
nethercoteb1affa82004-01-19 19:14:18 +000052# - cpu_test: <cpu feature required for test> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000053#
njna217f3d2002-09-27 08:26:27 +000054# Note that filters are necessary for stderr results to filter out things that
55# always change, eg. process id numbers.
njn25e49d8e72002-09-23 09:36:25 +000056#
njna217f3d2002-09-27 08:26:27 +000057# Expected stdout (filtered) is kept in <test>.stdout.exp. It can be missing
njnd8ced862003-04-08 00:47:05 +000058# if it would be empty. Expected stderr (filtered) is kept in
59# <test>.stderr.exp.
njn25e49d8e72002-09-23 09:36:25 +000060#
61# If results don't match, the output can be found in <test>.std<strm>.out,
62# and the diff between expected and actual in <test>.std<strm>.diff.
63#
nethercote137bc552003-11-14 17:47:54 +000064# Notes on adding regression tests for a new tool are in
65# coregrind/docs/coregrind_tools.html.
njn25e49d8e72002-09-23 09:36:25 +000066#----------------------------------------------------------------------------
67
njn9fb16f52003-04-23 17:47:13 +000068use warnings;
njn25e49d8e72002-09-23 09:36:25 +000069use strict;
70
71#----------------------------------------------------------------------------
72# Global vars
73#----------------------------------------------------------------------------
njnd8ced862003-04-08 00:47:05 +000074my $usage="vg_regtest [--all, --valgrind]\n";
njn25e49d8e72002-09-23 09:36:25 +000075
76my $tmp="vg_regtest.tmp.$$";
77
78# Test variables
79my $vgopts; # valgrind options
80my $prog; # test prog
81my $args; # test prog args
82my $stdout_filter; # filter program to run stdout results file through
83my $stderr_filter; # filter program to run stderr results file through
nethercoteb1affa82004-01-19 19:14:18 +000084my $cpu_test; # cpu feature to check for before running test
njn25e49d8e72002-09-23 09:36:25 +000085
86my @failures; # List of failed tests
87
njn9fb16f52003-04-23 17:47:13 +000088my $num_tests_done = 0;
89my %num_failures = (stderr => 0, stdout => 0);
njn25e49d8e72002-09-23 09:36:25 +000090
njn71fe3e62003-04-23 21:48:20 +000091# Default valgrind to use is this build tree's (uninstalled) one
njnc2e7f482002-09-27 08:44:17 +000092my $prefix="@prefix@";
93my $exec_prefix="@exec_prefix@";
njn71fe3e62003-04-23 21:48:20 +000094my $valgrind = "./coregrind/valgrind";
njn25e49d8e72002-09-23 09:36:25 +000095
96chomp(my $tests_dir = `pwd`);
97
98# default filter is the one named "filter_stderr" in the test's directory
99my $default_stderr_filter = "filter_stderr";
100
101
102#----------------------------------------------------------------------------
103# Process command line, setup
104#----------------------------------------------------------------------------
105
106# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
107#
108# 1. Can prepend "." onto programs to avoid trouble with users who don't have
109# "." in their path (by making $dir = ".")
110# 2. Can prepend the current dir to make the command absolute to avoid
111# subsequent trouble when we change directories.
112#
113# Also checks the program exists and is executable.
njn71fe3e62003-04-23 21:48:20 +0000114sub validate_program ($$$)
njn25e49d8e72002-09-23 09:36:25 +0000115{
njn71fe3e62003-04-23 21:48:20 +0000116 my ($dir, $prog, $must_be_executable) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000117
118 # If absolute path, leave it alone. If relative, make it
119 # absolute -- by prepending current dir -- so we can change
120 # dirs and still use it.
121 $prog = "$dir/$prog" if ($prog !~ /^\//);
njn71fe3e62003-04-23 21:48:20 +0000122 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
123 if ($must_be_executable) {
124 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
125 }
njn25e49d8e72002-09-23 09:36:25 +0000126
127 return $prog;
128}
129
130sub process_command_line()
131{
132 my $alldirs = 0;
133 my @fs;
134
135 for my $arg (@ARGV) {
136 if ($arg =~ /^-/) {
njnd8ced862003-04-08 00:47:05 +0000137 if ($arg =~ /^--all$/) {
njn25e49d8e72002-09-23 09:36:25 +0000138 $alldirs = 1;
139 } elsif ($arg =~ /^--valgrind=(.*)$/) {
140 $valgrind = $1;
141 } else {
142 die $usage;
143 }
144 } else {
145 push(@fs, $arg);
146 }
147 }
njn71fe3e62003-04-23 21:48:20 +0000148 $valgrind = validate_program($tests_dir, $valgrind, 0);
njn25e49d8e72002-09-23 09:36:25 +0000149
150 if ($alldirs) {
151 @fs = ();
152 foreach my $f (glob "*") {
153 push(@fs, $f) if (-d $f);
154 }
155 }
156
157 (0 != @fs) or die "No test files or directories specified\n";
158
159 return @fs;
160}
161
162#----------------------------------------------------------------------------
163# Read a .vgtest file
164#----------------------------------------------------------------------------
165sub read_vgtest_file($)
166{
167 my ($f) = @_;
168
169 # Defaults.
nethercoteb1affa82004-01-19 19:14:18 +0000170 ($vgopts, $prog, $args, $stdout_filter, $stderr_filter, $cpu_test) =
171 ("", undef, "", undef, undef, undef);
njn25e49d8e72002-09-23 09:36:25 +0000172
173 # Every test directory must have a "filter_stderr"
njn71fe3e62003-04-23 21:48:20 +0000174 $stderr_filter = validate_program(".", $default_stderr_filter, 1);
njn25e49d8e72002-09-23 09:36:25 +0000175
176 open(INPUTFILE, "< $f") || die "File $f not openable\n";
177
178 while (my $line = <INPUTFILE>) {
179 if ($line =~ /^\s*vgopts:\s*(.*)$/) {
180 $vgopts = $1;
181 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000182 $prog = validate_program(".", $1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000183 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
184 $args = $1;
njn25e49d8e72002-09-23 09:36:25 +0000185 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000186 $stdout_filter = validate_program(".", $1, 1);
njn25e49d8e72002-09-23 09:36:25 +0000187 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
njn71fe3e62003-04-23 21:48:20 +0000188 $stderr_filter = validate_program(".", $1, 1);
nethercoteb1affa82004-01-19 19:14:18 +0000189 } elsif ($line =~ /^\s*cpu_test:\s*(.*)$/) {
190 $cpu_test = $1;
njn25e49d8e72002-09-23 09:36:25 +0000191 } else {
192 die "Bad line in $f: $line\n";
193 }
194 }
195 close(INPUTFILE);
196
197 if (!defined $prog) {
198 die "no `prog:' line in `$f'\n";
199 }
200}
201
202#----------------------------------------------------------------------------
203# Do one test
204#----------------------------------------------------------------------------
205# Since most of the program time is spent in system() calls, need this to
206# propagate a Ctrl-C enabling us to quit.
207sub mysystem($)
208{
209 (system($_[0]) != 2) or exit 1; # 2 is SIGINT
210}
211
nethercote137bc552003-11-14 17:47:54 +0000212# from a directory name like "/foo/cachesim/tests/" determine the tool name
213sub determine_tool()
njn25cac76cb2002-09-23 11:21:57 +0000214{
215 my $dir = `pwd`;
nethercote137bc552003-11-14 17:47:54 +0000216 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
njn25cac76cb2002-09-23 11:21:57 +0000217 return $1;
218}
219
njn25e49d8e72002-09-23 09:36:25 +0000220sub do_one_test($$)
221{
222 my ($dir, $vgtest) = @_;
223 $vgtest =~ /^(.*)\.vgtest/;
224 my $name = $1;
225 my $fullname = "$dir/$name";
226
227 read_vgtest_file($vgtest);
228
nethercoteb1affa82004-01-19 19:14:18 +0000229 if (defined $cpu_test) {
230 return unless system("../../tests/cputest $cpu_test") == 0;
231 }
232
njndb3c4692002-10-04 10:03:34 +0000233 printf("%-16s valgrind $vgopts $prog $args\n", "$name:");
njn25e49d8e72002-09-23 09:36:25 +0000234
nethercote137bc552003-11-14 17:47:54 +0000235 # Pass the appropriate --tool option for the directory (can be overridden
njn71fe3e62003-04-23 21:48:20 +0000236 # by an "args:" or "args.dev:" line, though).
nethercote137bc552003-11-14 17:47:54 +0000237 my $tool=determine_tool();
fitzhardinge98abfc72003-12-16 02:05:15 +0000238 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 +0000239
240 if (defined $stdout_filter) {
241 mysystem("$stdout_filter < $name.stdout.out > $tmp");
242 rename($tmp, "$name.stdout.out");
243 }
244
245 mysystem("$stderr_filter < $name.stderr.out > $tmp");
246 rename($tmp, "$name.stderr.out");
247
248 # If stdout expected empty, .exp file might be missing so diff with
249 # /dev/null
250 my $stdout_exp = ( -r "$name.stdout.exp"
251 ? "$name.stdout.exp"
252 : "/dev/null" );
253
njnd8ced862003-04-08 00:47:05 +0000254 my $stderr_exp = "$name.stderr.exp";
njn25e49d8e72002-09-23 09:36:25 +0000255 (-r $stderr_exp) or die "Could not read `$stderr_exp'\n";
256
257 mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff");
258 mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff");
259
260 for my $ext ("stdout", "stderr") {
261 if (-s "$name.$ext.diff") {
njndb3c4692002-10-04 10:03:34 +0000262 print "*** $name failed ($ext) ***\n";
njn9fb16f52003-04-23 17:47:13 +0000263 push(@failures, sprintf("%-40s ($ext)", "$fullname"));
264 $num_failures{$ext}++;
njn25e49d8e72002-09-23 09:36:25 +0000265 } else {
266 unlink("$name.$ext.out", "$name.$ext.diff");
267 }
268 }
njn9fb16f52003-04-23 17:47:13 +0000269 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000270}
271
272#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000273# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000274#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000275sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000276
njndb3c4692002-10-04 10:03:34 +0000277sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000278{
njndb3c4692002-10-04 10:03:34 +0000279 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000280 $dir =~ s/\/$//; # trim a trailing '/'
281
jsgf855d93d2003-10-13 22:26:55 +0000282 # ignore dirs into which we should not recurse
mueller92f0b802003-11-19 00:55:32 +0000283 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000284
njn25e49d8e72002-09-23 09:36:25 +0000285 chdir($dir) or die "Could not change into $dir\n";
286
njn584eaac2002-10-04 15:30:48 +0000287 # Nb: Don't prepend a '/' to the base directory
288 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000289 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000290
njndb3c4692002-10-04 10:03:34 +0000291 my @fs = glob "*";
292 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
293 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
294
295 if ($found_tests) {
296 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000297 }
njn25cac76cb2002-09-23 11:21:57 +0000298 foreach my $f (@fs) {
299 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000300 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000301 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000302 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000303 }
njn25e49d8e72002-09-23 09:36:25 +0000304 }
njndb3c4692002-10-04 10:03:34 +0000305 if ($found_tests) {
306 print "-- Finished tests in $full_dir $dashes\n";
307 }
308
njn25e49d8e72002-09-23 09:36:25 +0000309 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000310}
311
312#----------------------------------------------------------------------------
313# Summarise results
314#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000315sub plural($)
316{
317 return ( $_[0] == 1 ? "" : "s" );
318}
319
njn25e49d8e72002-09-23 09:36:25 +0000320sub summarise_results
321{
njnd8ced862003-04-08 00:47:05 +0000322 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000323
njn9fb16f52003-04-23 17:47:13 +0000324 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
325 $num_tests_done, plural($num_tests_done),
326 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
327 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
328
329 foreach my $failure (@failures) {
330 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000331 }
njn9fb16f52003-04-23 17:47:13 +0000332 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000333}
334
335#----------------------------------------------------------------------------
336# main(), sort of
337#----------------------------------------------------------------------------
338
daywalkerbb936982003-04-23 16:52:06 +0000339# nuke VALGRIND_OPTS
340$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000341
342my @fs = process_command_line();
343foreach my $f (@fs) {
344 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000345 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000346 } else {
347 # Allow the .vgtest suffix to be given or omitted
348 if ($f =~ /.vgtest$/ && -r $f) {
349 # do nothing
350 } elsif (-r "$f.vgtest") {
351 $f = "$f.vgtest";
352 } else {
353 die "`$f' neither a directory nor a readable test file/name\n"
354 }
355 my $dir = `dirname $f`; chomp $dir;
356 my $file = `basename $f`; chomp $file;
357 chdir($dir) or die "Could not change into $dir\n";
358 do_one_test($dir, $file);
359 chdir($tests_dir);
360 }
361}
362summarise_results();
363
njnc2e7f482002-09-27 08:44:17 +0000364##--------------------------------------------------------------------##
365##--- end vg_regtest ---##
366##--------------------------------------------------------------------##