blob: 84182d87098404a42e79820336b987458fcd1182 [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).
nethercote137bc552003-11-14 17:47:54 +0000229 my $tool=determine_tool();
fitzhardinge98abfc72003-12-16 02:05:15 +0000230 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 +0000231
232 if (defined $stdout_filter) {
233 mysystem("$stdout_filter < $name.stdout.out > $tmp");
234 rename($tmp, "$name.stdout.out");
235 }
236
237 mysystem("$stderr_filter < $name.stderr.out > $tmp");
238 rename($tmp, "$name.stderr.out");
239
240 # If stdout expected empty, .exp file might be missing so diff with
241 # /dev/null
242 my $stdout_exp = ( -r "$name.stdout.exp"
243 ? "$name.stdout.exp"
244 : "/dev/null" );
245
njnd8ced862003-04-08 00:47:05 +0000246 my $stderr_exp = "$name.stderr.exp";
njn25e49d8e72002-09-23 09:36:25 +0000247 (-r $stderr_exp) or die "Could not read `$stderr_exp'\n";
248
249 mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff");
250 mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff");
251
252 for my $ext ("stdout", "stderr") {
253 if (-s "$name.$ext.diff") {
njndb3c4692002-10-04 10:03:34 +0000254 print "*** $name failed ($ext) ***\n";
njn9fb16f52003-04-23 17:47:13 +0000255 push(@failures, sprintf("%-40s ($ext)", "$fullname"));
256 $num_failures{$ext}++;
njn25e49d8e72002-09-23 09:36:25 +0000257 } else {
258 unlink("$name.$ext.out", "$name.$ext.diff");
259 }
260 }
njn9fb16f52003-04-23 17:47:13 +0000261 $num_tests_done++;
njn25e49d8e72002-09-23 09:36:25 +0000262}
263
264#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000265# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000266#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000267sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000268
njndb3c4692002-10-04 10:03:34 +0000269sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000270{
njndb3c4692002-10-04 10:03:34 +0000271 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000272 $dir =~ s/\/$//; # trim a trailing '/'
273
jsgf855d93d2003-10-13 22:26:55 +0000274 # ignore dirs into which we should not recurse
mueller92f0b802003-11-19 00:55:32 +0000275 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
njn25cac76cb2002-09-23 11:21:57 +0000276
njn25e49d8e72002-09-23 09:36:25 +0000277 chdir($dir) or die "Could not change into $dir\n";
278
njn584eaac2002-10-04 15:30:48 +0000279 # Nb: Don't prepend a '/' to the base directory
280 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000281 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000282
njndb3c4692002-10-04 10:03:34 +0000283 my @fs = glob "*";
284 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
285 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
286
287 if ($found_tests) {
288 print "-- Running tests in $full_dir $dashes\n";
njndb3c4692002-10-04 10:03:34 +0000289 }
njn25cac76cb2002-09-23 11:21:57 +0000290 foreach my $f (@fs) {
291 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000292 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000293 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000294 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000295 }
njn25e49d8e72002-09-23 09:36:25 +0000296 }
njndb3c4692002-10-04 10:03:34 +0000297 if ($found_tests) {
298 print "-- Finished tests in $full_dir $dashes\n";
299 }
300
njn25e49d8e72002-09-23 09:36:25 +0000301 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000302}
303
304#----------------------------------------------------------------------------
305# Summarise results
306#----------------------------------------------------------------------------
njn9fb16f52003-04-23 17:47:13 +0000307sub plural($)
308{
309 return ( $_[0] == 1 ? "" : "s" );
310}
311
njn25e49d8e72002-09-23 09:36:25 +0000312sub summarise_results
313{
njnd8ced862003-04-08 00:47:05 +0000314 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
njnd8ced862003-04-08 00:47:05 +0000315
njn9fb16f52003-04-23 17:47:13 +0000316 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s =================\n",
317 $num_tests_done, plural($num_tests_done),
318 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
319 $num_failures{"stdout"}, plural($num_failures{"stdout"}));
320
321 foreach my $failure (@failures) {
322 print "$failure\n";
njn25e49d8e72002-09-23 09:36:25 +0000323 }
njn9fb16f52003-04-23 17:47:13 +0000324 print "\n";
njn25e49d8e72002-09-23 09:36:25 +0000325}
326
327#----------------------------------------------------------------------------
328# main(), sort of
329#----------------------------------------------------------------------------
330
daywalkerbb936982003-04-23 16:52:06 +0000331# nuke VALGRIND_OPTS
332$ENV{"VALGRIND_OPTS"} = "";
njn25e49d8e72002-09-23 09:36:25 +0000333
334my @fs = process_command_line();
335foreach my $f (@fs) {
336 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000337 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000338 } else {
339 # Allow the .vgtest suffix to be given or omitted
340 if ($f =~ /.vgtest$/ && -r $f) {
341 # do nothing
342 } elsif (-r "$f.vgtest") {
343 $f = "$f.vgtest";
344 } else {
345 die "`$f' neither a directory nor a readable test file/name\n"
346 }
347 my $dir = `dirname $f`; chomp $dir;
348 my $file = `basename $f`; chomp $file;
349 chdir($dir) or die "Could not change into $dir\n";
350 do_one_test($dir, $file);
351 chdir($tests_dir);
352 }
353}
354summarise_results();
355
njnc2e7f482002-09-27 08:44:17 +0000356##--------------------------------------------------------------------##
357##--- end vg_regtest ---##
358##--------------------------------------------------------------------##