blob: 56d37d16a28c4e7098c56a26b3347a4e6bce9bc4 [file] [log] [blame]
njn25e49d8e72002-09-23 09:36:25 +00001#! /usr/bin/perl -w
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#
9# Copyright (C) 2002 Nicholas Nethercote
10# 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:
33# --stable: use stable-branch stderr results (*.stderr.exp.hd)
34# --dev: use dev-branch stderr results (*.stderr.exp - default)
35# --all: run tests in all subdirs
njnc2e7f482002-09-27 08:44:17 +000036# --valgrind: valgrind to use. Default is one built from this source tree.
njna217f3d2002-09-27 08:26:27 +000037#
njndb3c4692002-10-04 10:03:34 +000038# The stable-branch/dev-branch distinction allows slight differences in stderr
39# results.
40#
njn4ba5a792002-09-30 10:23:54 +000041# The easiest way is to run all tests in valgrind/ with (assuming you installed
42# in $PREFIX):
43#
44# $PREFIX/bin/vg_regtest --all
45#
njna217f3d2002-09-27 08:26:27 +000046# You can specify individual files to test, or whole directories, or both.
njndb3c4692002-10-04 10:03:34 +000047# Directories are traversed recursively, except for ones name CVS/ or docs/.
njn25e49d8e72002-09-23 09:36:25 +000048#
49# Each test is defined in a file <test>.vgtest, containing one or more of the
njna217f3d2002-09-27 08:26:27 +000050# following lines, in any order:
njn25e49d8e72002-09-23 09:36:25 +000051# - prog: <prog to run> (compulsory)
52# - args: <args for prog> (default: none)
53# - vgopts: <Valgrind options> (default: none)
njna217f3d2002-09-27 08:26:27 +000054# - vgopts.st: <options, --stable only> (default: none)
55# - vgopts.dev: <options, --dev only> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000056# - stdout_filter: <filter to run stdout through> (default: none)
njna217f3d2002-09-27 08:26:27 +000057# - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
njn25e49d8e72002-09-23 09:36:25 +000058#
njna217f3d2002-09-27 08:26:27 +000059# Note that filters are necessary for stderr results to filter out things that
60# always change, eg. process id numbers.
njn25e49d8e72002-09-23 09:36:25 +000061#
njna217f3d2002-09-27 08:26:27 +000062# Expected stdout (filtered) is kept in <test>.stdout.exp. It can be missing
63# if it would be empty.
64#
65# Expected stderr (filtered) is kept in <test>.stderr.exp for --dev, and
66# <test>.stderr.exp.hd for --stable (silly ".hd" extension for historical
67# reasons).
njn25e49d8e72002-09-23 09:36:25 +000068#
69# If results don't match, the output can be found in <test>.std<strm>.out,
70# and the diff between expected and actual in <test>.std<strm>.diff.
71#
njna217f3d2002-09-27 08:26:27 +000072# Notes on adding regression tests for a new skin are in
73# coregrind/docs/skins.html.
njn25e49d8e72002-09-23 09:36:25 +000074#----------------------------------------------------------------------------
75
76use strict;
77
78#----------------------------------------------------------------------------
79# Global vars
80#----------------------------------------------------------------------------
njna217f3d2002-09-27 08:26:27 +000081my $usage="vg_regtest [--stable|--dev, --all, --valgrind]\n";
njn25e49d8e72002-09-23 09:36:25 +000082
83my $tmp="vg_regtest.tmp.$$";
84
85# Test variables
86my $vgopts; # valgrind options
87my $prog; # test prog
88my $args; # test prog args
89my $stdout_filter; # filter program to run stdout results file through
90my $stderr_filter; # filter program to run stderr results file through
91
92my @failures; # List of failed tests
93
njna217f3d2002-09-27 08:26:27 +000094my $exp = ""; # --dev is default
njn25e49d8e72002-09-23 09:36:25 +000095
njnc2e7f482002-09-27 08:44:17 +000096# Default valgrind to use is this build tree's one
97my $prefix="@prefix@";
98my $exec_prefix="@exec_prefix@";
99my $valgrind = "@bindir@/valgrind";
njn25e49d8e72002-09-23 09:36:25 +0000100
101chomp(my $tests_dir = `pwd`);
102
103# default filter is the one named "filter_stderr" in the test's directory
104my $default_stderr_filter = "filter_stderr";
105
106
107#----------------------------------------------------------------------------
108# Process command line, setup
109#----------------------------------------------------------------------------
110
111# If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
112#
113# 1. Can prepend "." onto programs to avoid trouble with users who don't have
114# "." in their path (by making $dir = ".")
115# 2. Can prepend the current dir to make the command absolute to avoid
116# subsequent trouble when we change directories.
117#
118# Also checks the program exists and is executable.
119sub validate_program ($$)
120{
121 my ($dir, $prog) = @_;
122
123 # If absolute path, leave it alone. If relative, make it
124 # absolute -- by prepending current dir -- so we can change
125 # dirs and still use it.
126 $prog = "$dir/$prog" if ($prog !~ /^\//);
127 (-f $prog) or die "`$prog' not found or not a file ($dir)\n";
128 (-x $prog) or die "`$prog' not found or not executable ($dir)\n";
129
130 return $prog;
131}
132
133sub process_command_line()
134{
135 my $alldirs = 0;
136 my @fs;
137
138 for my $arg (@ARGV) {
139 if ($arg =~ /^-/) {
njna217f3d2002-09-27 08:26:27 +0000140 if ($arg =~ /^--stable$/) {
njn25e49d8e72002-09-23 09:36:25 +0000141 $exp = ".hd";
njna217f3d2002-09-27 08:26:27 +0000142 } elsif ($arg =~ /^--dev$/) {
njn25e49d8e72002-09-23 09:36:25 +0000143 $exp = "";
144 } elsif ($arg =~ /^--all$/) {
145 $alldirs = 1;
146 } elsif ($arg =~ /^--valgrind=(.*)$/) {
147 $valgrind = $1;
148 } else {
149 die $usage;
150 }
151 } else {
152 push(@fs, $arg);
153 }
154 }
155 $valgrind = validate_program($tests_dir, $valgrind);
156
157 if ($alldirs) {
158 @fs = ();
159 foreach my $f (glob "*") {
160 push(@fs, $f) if (-d $f);
161 }
162 }
163
164 (0 != @fs) or die "No test files or directories specified\n";
165
166 return @fs;
167}
168
169#----------------------------------------------------------------------------
170# Read a .vgtest file
171#----------------------------------------------------------------------------
172sub read_vgtest_file($)
173{
174 my ($f) = @_;
175
176 # Defaults.
177 ($vgopts, $prog, $args, $stdout_filter, $stderr_filter) =
178 ("", undef, "", undef, undef);
179
180 # Every test directory must have a "filter_stderr"
181 $stderr_filter = validate_program(".", $default_stderr_filter);
182
183 open(INPUTFILE, "< $f") || die "File $f not openable\n";
184
185 while (my $line = <INPUTFILE>) {
186 if ($line =~ /^\s*vgopts:\s*(.*)$/) {
187 $vgopts = $1;
188 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
189 $prog = validate_program(".", $1);
190 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
191 $args = $1;
njna217f3d2002-09-27 08:26:27 +0000192 } elsif ($line =~ /^\s*vgopts\.st:\s*(.*)$/) {
njn25e49d8e72002-09-23 09:36:25 +0000193 $vgopts = $1 if ($exp eq ".hd");
njna217f3d2002-09-27 08:26:27 +0000194 } elsif ($line =~ /^\s*vgopts\.dev:\s*(.*)$/) {
njn25e49d8e72002-09-23 09:36:25 +0000195 $vgopts = $1 if ($exp eq "");
196 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
197 $stdout_filter = validate_program(".", $1);
198 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
199 $stderr_filter = validate_program(".", $1);
200 } else {
201 die "Bad line in $f: $line\n";
202 }
203 }
204 close(INPUTFILE);
205
206 if (!defined $prog) {
207 die "no `prog:' line in `$f'\n";
208 }
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
njn25cac76cb2002-09-23 11:21:57 +0000221# from a directory name like "/foo/cachesim/tests/" determine the skin name
222sub determine_skin()
223{
224 my $dir = `pwd`;
225 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/skin_name/tests/foo
226 return $1;
227}
228
njn25e49d8e72002-09-23 09:36:25 +0000229sub do_one_test($$)
230{
231 my ($dir, $vgtest) = @_;
232 $vgtest =~ /^(.*)\.vgtest/;
233 my $name = $1;
234 my $fullname = "$dir/$name";
235
236 read_vgtest_file($vgtest);
237
njndb3c4692002-10-04 10:03:34 +0000238 printf("%-16s valgrind $vgopts $prog $args\n", "$name:");
njn25e49d8e72002-09-23 09:36:25 +0000239
njna217f3d2002-09-27 08:26:27 +0000240 # If --dev, pass the appropriate --skin option for the directory (can be
241 # overridden by an "args:" or "args.dev:" line, though)
njn25e49d8e72002-09-23 09:36:25 +0000242 if ($exp eq ".hd") {
243 mysystem("$valgrind $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
244 } else {
njn25cac76cb2002-09-23 11:21:57 +0000245 my $skin=determine_skin();
246 mysystem("$valgrind --skin=$skin $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000247 }
248
249 if (defined $stdout_filter) {
250 mysystem("$stdout_filter < $name.stdout.out > $tmp");
251 rename($tmp, "$name.stdout.out");
252 }
253
254 mysystem("$stderr_filter < $name.stderr.out > $tmp");
255 rename($tmp, "$name.stderr.out");
256
257 # If stdout expected empty, .exp file might be missing so diff with
258 # /dev/null
259 my $stdout_exp = ( -r "$name.stdout.exp"
260 ? "$name.stdout.exp"
261 : "/dev/null" );
262
njna217f3d2002-09-27 08:26:27 +0000263 # If stable-branch and dev-branch have the same expected stderr output,
njn25e49d8e72002-09-23 09:36:25 +0000264 # foo.stderr.exp.hd might be missing, so use foo.stderr.exp instead if
njna217f3d2002-09-27 08:26:27 +0000265 # --stable is true.
njn25e49d8e72002-09-23 09:36:25 +0000266 my $stderr_exp = "$name.stderr.exp$exp";
267 if ($exp eq ".hd" && not -r $stderr_exp) {
268 $stderr_exp = "$name.stderr.exp";
269 }
270 (-r $stderr_exp) or die "Could not read `$stderr_exp'\n";
271
272 mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff");
273 mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff");
274
275 for my $ext ("stdout", "stderr") {
276 if (-s "$name.$ext.diff") {
njndb3c4692002-10-04 10:03:34 +0000277 print "*** $name failed ($ext) ***\n";
278 push(@failures, sprintf("%-40s $ext", "$fullname"));
njn25e49d8e72002-09-23 09:36:25 +0000279 } else {
280 unlink("$name.$ext.out", "$name.$ext.diff");
281 }
282 }
283}
284
285#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000286# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000287#----------------------------------------------------------------------------
njndb3c4692002-10-04 10:03:34 +0000288sub test_one_dir($$); # forward declaration
njn25cac76cb2002-09-23 11:21:57 +0000289
njndb3c4692002-10-04 10:03:34 +0000290sub test_one_dir($$)
njn25e49d8e72002-09-23 09:36:25 +0000291{
njndb3c4692002-10-04 10:03:34 +0000292 my ($dir, $prev_dirs) = @_;
njn25e49d8e72002-09-23 09:36:25 +0000293 $dir =~ s/\/$//; # trim a trailing '/'
294
njn25cac76cb2002-09-23 11:21:57 +0000295 if ($dir =~ /^(CVS|docs)$/) { return; } # ignore CVS/ and docs/ dirs
296
njn25e49d8e72002-09-23 09:36:25 +0000297 chdir($dir) or die "Could not change into $dir\n";
298
njn584eaac2002-10-04 15:30:48 +0000299 # Nb: Don't prepend a '/' to the base directory
300 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
njndb3c4692002-10-04 10:03:34 +0000301 my $dashes = "-" x (50 - length $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000302
njndb3c4692002-10-04 10:03:34 +0000303 my @fs = glob "*";
304 my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs;
305 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
306
307 if ($found_tests) {
308 print "-- Running tests in $full_dir $dashes\n";
309 } else {
310 print "-- Found no tests in $full_dir $dashes\n";
311 }
njn25cac76cb2002-09-23 11:21:57 +0000312 foreach my $f (@fs) {
313 if (-d $f) {
njndb3c4692002-10-04 10:03:34 +0000314 test_one_dir($f, $full_dir);
njn25cac76cb2002-09-23 11:21:57 +0000315 } elsif ($f =~ /\.vgtest$/) {
njndb3c4692002-10-04 10:03:34 +0000316 do_one_test($full_dir, $f);
njn25cac76cb2002-09-23 11:21:57 +0000317 }
njn25e49d8e72002-09-23 09:36:25 +0000318 }
njndb3c4692002-10-04 10:03:34 +0000319 if ($found_tests) {
320 print "-- Finished tests in $full_dir $dashes\n";
321 }
322
njn25e49d8e72002-09-23 09:36:25 +0000323 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000324}
325
326#----------------------------------------------------------------------------
327# Summarise results
328#----------------------------------------------------------------------------
329sub summarise_results
330{
njn25cac76cb2002-09-23 11:21:57 +0000331 print "\n== Failed tests ===============================\n";
njn25e49d8e72002-09-23 09:36:25 +0000332 if (0 == @failures) {
333 print " (none)\n";
334 } else {
335 foreach my $failure (@failures) {
336 print "$failure\n";
337 }
338 }
339}
340
341#----------------------------------------------------------------------------
342# main(), sort of
343#----------------------------------------------------------------------------
344
345# undefine $VALGRIND_OPTS
346if ( exists $ENV{VALGRIND_OPTS} ) {
347 undef $ENV{VALGRIND_OPTS};
348}
349
350my @fs = process_command_line();
351foreach my $f (@fs) {
352 if (-d $f) {
njn584eaac2002-10-04 15:30:48 +0000353 test_one_dir($f, "");
njn25e49d8e72002-09-23 09:36:25 +0000354 } else {
355 # Allow the .vgtest suffix to be given or omitted
356 if ($f =~ /.vgtest$/ && -r $f) {
357 # do nothing
358 } elsif (-r "$f.vgtest") {
359 $f = "$f.vgtest";
360 } else {
361 die "`$f' neither a directory nor a readable test file/name\n"
362 }
363 my $dir = `dirname $f`; chomp $dir;
364 my $file = `basename $f`; chomp $file;
365 chdir($dir) or die "Could not change into $dir\n";
366 do_one_test($dir, $file);
367 chdir($tests_dir);
368 }
369}
370summarise_results();
371
njnc2e7f482002-09-27 08:44:17 +0000372##--------------------------------------------------------------------##
373##--- end vg_regtest ---##
374##--------------------------------------------------------------------##