blob: 53ce9f1629a67d7c4906e6244869020b35e64f2f [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
6# This file is part of Valgrind, an x86 protected-mode emulator
7# designed for debugging and profiling binaries on x86-Unixes.
8#
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#
38# You can specify individual files to test, or whole directories, or both.
39# The stable-branch/dev-branch distinction allows slight differences in stderr
40# results.
njn25e49d8e72002-09-23 09:36:25 +000041#
42# Each test is defined in a file <test>.vgtest, containing one or more of the
njna217f3d2002-09-27 08:26:27 +000043# following lines, in any order:
njn25e49d8e72002-09-23 09:36:25 +000044# - prog: <prog to run> (compulsory)
45# - args: <args for prog> (default: none)
46# - vgopts: <Valgrind options> (default: none)
njna217f3d2002-09-27 08:26:27 +000047# - vgopts.st: <options, --stable only> (default: none)
48# - vgopts.dev: <options, --dev only> (default: none)
njn25e49d8e72002-09-23 09:36:25 +000049# - stdout_filter: <filter to run stdout through> (default: none)
njna217f3d2002-09-27 08:26:27 +000050# - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
njn25e49d8e72002-09-23 09:36:25 +000051#
njna217f3d2002-09-27 08:26:27 +000052# Note that filters are necessary for stderr results to filter out things that
53# always change, eg. process id numbers.
njn25e49d8e72002-09-23 09:36:25 +000054#
njna217f3d2002-09-27 08:26:27 +000055# Expected stdout (filtered) is kept in <test>.stdout.exp. It can be missing
56# if it would be empty.
57#
58# Expected stderr (filtered) is kept in <test>.stderr.exp for --dev, and
59# <test>.stderr.exp.hd for --stable (silly ".hd" extension for historical
60# reasons).
njn25e49d8e72002-09-23 09:36:25 +000061#
62# If results don't match, the output can be found in <test>.std<strm>.out,
63# and the diff between expected and actual in <test>.std<strm>.diff.
64#
njna217f3d2002-09-27 08:26:27 +000065# Notes on adding regression tests for a new skin are in
66# coregrind/docs/skins.html.
njn25e49d8e72002-09-23 09:36:25 +000067#----------------------------------------------------------------------------
68
69use strict;
70
71#----------------------------------------------------------------------------
72# Global vars
73#----------------------------------------------------------------------------
njna217f3d2002-09-27 08:26:27 +000074my $usage="vg_regtest [--stable|--dev, --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
84
85my @failures; # List of failed tests
86
njna217f3d2002-09-27 08:26:27 +000087my $exp = ""; # --dev is default
njn25e49d8e72002-09-23 09:36:25 +000088
njnc2e7f482002-09-27 08:44:17 +000089# Default valgrind to use is this build tree's one
90my $prefix="@prefix@";
91my $exec_prefix="@exec_prefix@";
92my $valgrind = "@bindir@/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.
112sub validate_program ($$)
113{
114 my ($dir, $prog) = @_;
115
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 !~ /^\//);
120 (-f $prog) or die "`$prog' not found or not a file ($dir)\n";
121 (-x $prog) or die "`$prog' not found or not executable ($dir)\n";
122
123 return $prog;
124}
125
126sub process_command_line()
127{
128 my $alldirs = 0;
129 my @fs;
130
131 for my $arg (@ARGV) {
132 if ($arg =~ /^-/) {
njna217f3d2002-09-27 08:26:27 +0000133 if ($arg =~ /^--stable$/) {
njn25e49d8e72002-09-23 09:36:25 +0000134 $exp = ".hd";
njna217f3d2002-09-27 08:26:27 +0000135 } elsif ($arg =~ /^--dev$/) {
njn25e49d8e72002-09-23 09:36:25 +0000136 $exp = "";
137 } elsif ($arg =~ /^--all$/) {
138 $alldirs = 1;
139 } elsif ($arg =~ /^--valgrind=(.*)$/) {
140 $valgrind = $1;
141 } else {
142 die $usage;
143 }
144 } else {
145 push(@fs, $arg);
146 }
147 }
148 $valgrind = validate_program($tests_dir, $valgrind);
149
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.
170 ($vgopts, $prog, $args, $stdout_filter, $stderr_filter) =
171 ("", undef, "", undef, undef);
172
173 # Every test directory must have a "filter_stderr"
174 $stderr_filter = validate_program(".", $default_stderr_filter);
175
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*(.*)$/) {
182 $prog = validate_program(".", $1);
183 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
184 $args = $1;
njna217f3d2002-09-27 08:26:27 +0000185 } elsif ($line =~ /^\s*vgopts\.st:\s*(.*)$/) {
njn25e49d8e72002-09-23 09:36:25 +0000186 $vgopts = $1 if ($exp eq ".hd");
njna217f3d2002-09-27 08:26:27 +0000187 } elsif ($line =~ /^\s*vgopts\.dev:\s*(.*)$/) {
njn25e49d8e72002-09-23 09:36:25 +0000188 $vgopts = $1 if ($exp eq "");
189 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
190 $stdout_filter = validate_program(".", $1);
191 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
192 $stderr_filter = validate_program(".", $1);
193 } else {
194 die "Bad line in $f: $line\n";
195 }
196 }
197 close(INPUTFILE);
198
199 if (!defined $prog) {
200 die "no `prog:' line in `$f'\n";
201 }
202}
203
204#----------------------------------------------------------------------------
205# Do one test
206#----------------------------------------------------------------------------
207# Since most of the program time is spent in system() calls, need this to
208# propagate a Ctrl-C enabling us to quit.
209sub mysystem($)
210{
211 (system($_[0]) != 2) or exit 1; # 2 is SIGINT
212}
213
njn25cac76cb2002-09-23 11:21:57 +0000214# from a directory name like "/foo/cachesim/tests/" determine the skin name
215sub determine_skin()
216{
217 my $dir = `pwd`;
218 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/skin_name/tests/foo
219 return $1;
220}
221
njn25e49d8e72002-09-23 09:36:25 +0000222sub do_one_test($$)
223{
224 my ($dir, $vgtest) = @_;
225 $vgtest =~ /^(.*)\.vgtest/;
226 my $name = $1;
227 my $fullname = "$dir/$name";
228
229 read_vgtest_file($vgtest);
230
231 printf("%-30s valgrind $vgopts $prog $args\n", "$fullname:");
232
njna217f3d2002-09-27 08:26:27 +0000233 # If --dev, pass the appropriate --skin option for the directory (can be
234 # overridden by an "args:" or "args.dev:" line, though)
njn25e49d8e72002-09-23 09:36:25 +0000235 if ($exp eq ".hd") {
236 mysystem("$valgrind $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
237 } else {
njn25cac76cb2002-09-23 11:21:57 +0000238 my $skin=determine_skin();
239 mysystem("$valgrind --skin=$skin $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out");
njn25e49d8e72002-09-23 09:36:25 +0000240 }
241
242 if (defined $stdout_filter) {
243 mysystem("$stdout_filter < $name.stdout.out > $tmp");
244 rename($tmp, "$name.stdout.out");
245 }
246
247 mysystem("$stderr_filter < $name.stderr.out > $tmp");
248 rename($tmp, "$name.stderr.out");
249
250 # If stdout expected empty, .exp file might be missing so diff with
251 # /dev/null
252 my $stdout_exp = ( -r "$name.stdout.exp"
253 ? "$name.stdout.exp"
254 : "/dev/null" );
255
njna217f3d2002-09-27 08:26:27 +0000256 # If stable-branch and dev-branch have the same expected stderr output,
njn25e49d8e72002-09-23 09:36:25 +0000257 # foo.stderr.exp.hd might be missing, so use foo.stderr.exp instead if
njna217f3d2002-09-27 08:26:27 +0000258 # --stable is true.
njn25e49d8e72002-09-23 09:36:25 +0000259 my $stderr_exp = "$name.stderr.exp$exp";
260 if ($exp eq ".hd" && not -r $stderr_exp) {
261 $stderr_exp = "$name.stderr.exp";
262 }
263 (-r $stderr_exp) or die "Could not read `$stderr_exp'\n";
264
265 mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff");
266 mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff");
267
268 for my $ext ("stdout", "stderr") {
269 if (-s "$name.$ext.diff") {
270 print "*** $fullname failed ($ext) ***\n";
271 push(@failures, sprintf("%-30s $ext", "$fullname"));
272 } else {
273 unlink("$name.$ext.out", "$name.$ext.diff");
274 }
275 }
276}
277
278#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000279# Test one directory (and any subdirs)
njn25e49d8e72002-09-23 09:36:25 +0000280#----------------------------------------------------------------------------
njn25cac76cb2002-09-23 11:21:57 +0000281sub test_one_dir($); # forward declaration
282
njn25e49d8e72002-09-23 09:36:25 +0000283sub test_one_dir($)
284{
285 my ($dir) = @_;
286 $dir =~ s/\/$//; # trim a trailing '/'
287
njn25cac76cb2002-09-23 11:21:57 +0000288 if ($dir =~ /^(CVS|docs)$/) { return; } # ignore CVS/ and docs/ dirs
289
njn25e49d8e72002-09-23 09:36:25 +0000290 print "-- Running tests in $dir ----------------------------------\n";
291 chdir($dir) or die "Could not change into $dir\n";
292
njn25cac76cb2002-09-23 11:21:57 +0000293# my @vgtests = glob "*\.vgtest";
294 my @fs = glob "*";
295
296 foreach my $f (@fs) {
297 if (-d $f) {
298 test_one_dir($f);
299 } elsif ($f =~ /\.vgtest$/) {
300 do_one_test($dir, $f);
301 }
njn25e49d8e72002-09-23 09:36:25 +0000302 }
njn25cac76cb2002-09-23 11:21:57 +0000303
304 print "-- Finished tests in $dir ----------------------------------\n";
njn25e49d8e72002-09-23 09:36:25 +0000305 chdir("..");
njn25e49d8e72002-09-23 09:36:25 +0000306}
307
308#----------------------------------------------------------------------------
309# Summarise results
310#----------------------------------------------------------------------------
311sub summarise_results
312{
njn25cac76cb2002-09-23 11:21:57 +0000313 print "\n== Failed tests ===============================\n";
njn25e49d8e72002-09-23 09:36:25 +0000314 if (0 == @failures) {
315 print " (none)\n";
316 } else {
317 foreach my $failure (@failures) {
318 print "$failure\n";
319 }
320 }
321}
322
323#----------------------------------------------------------------------------
324# main(), sort of
325#----------------------------------------------------------------------------
326
327# undefine $VALGRIND_OPTS
328if ( exists $ENV{VALGRIND_OPTS} ) {
329 undef $ENV{VALGRIND_OPTS};
330}
331
332my @fs = process_command_line();
333foreach my $f (@fs) {
334 if (-d $f) {
335 test_one_dir($f);
336 } else {
337 # Allow the .vgtest suffix to be given or omitted
338 if ($f =~ /.vgtest$/ && -r $f) {
339 # do nothing
340 } elsif (-r "$f.vgtest") {
341 $f = "$f.vgtest";
342 } else {
343 die "`$f' neither a directory nor a readable test file/name\n"
344 }
345 my $dir = `dirname $f`; chomp $dir;
346 my $file = `basename $f`; chomp $file;
347 chdir($dir) or die "Could not change into $dir\n";
348 do_one_test($dir, $file);
349 chdir($tests_dir);
350 }
351}
352summarise_results();
353
njnc2e7f482002-09-27 08:44:17 +0000354##--------------------------------------------------------------------##
355##--- end vg_regtest ---##
356##--------------------------------------------------------------------##