sewardj | 2f68595 | 2002-12-22 19:32:23 +0000 | [diff] [blame] | 1 | #! @PERL@ -w |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 2 | ##--------------------------------------------------------------------## |
| 3 | ##--- Valgrind regression testing script vg_regtest ---## |
| 4 | ##--------------------------------------------------------------------## |
njn | c2e7f48 | 2002-09-27 08:44:17 +0000 | [diff] [blame] | 5 | |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 6 | # This file is part of Valgrind, an extensible x86 protected-mode |
| 7 | # emulator for monitoring program execution on x86-Unixes. |
njn | c2e7f48 | 2002-09-27 08:44:17 +0000 | [diff] [blame] | 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 | #---------------------------------------------------------------------------- |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 30 | # usage: vg_regtest [options] <dirs | files> |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 31 | # |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 32 | # Options: |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 33 | # --all: run tests in all subdirs |
njn | c2e7f48 | 2002-09-27 08:44:17 +0000 | [diff] [blame] | 34 | # --valgrind: valgrind to use. Default is one built from this source tree. |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 35 | # |
njn | 4ba5a79 | 2002-09-30 10:23:54 +0000 | [diff] [blame] | 36 | # 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 | # |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 41 | # You can specify individual files to test, or whole directories, or both. |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 42 | # Directories are traversed recursively, except for ones name CVS/ or docs/. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 43 | # |
| 44 | # Each test is defined in a file <test>.vgtest, containing one or more of the |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 45 | # following lines, in any order: |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 46 | # - prog: <prog to run> (compulsory) |
| 47 | # - args: <args for prog> (default: none) |
| 48 | # - vgopts: <Valgrind options> (default: none) |
| 49 | # - stdout_filter: <filter to run stdout through> (default: none) |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 50 | # - stderr_filter: <filter to run stderr through> (default: ./filter_stderr) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 51 | # |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 52 | # Note that filters are necessary for stderr results to filter out things that |
| 53 | # always change, eg. process id numbers. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 54 | # |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 55 | # Expected stdout (filtered) is kept in <test>.stdout.exp. It can be missing |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 56 | # if it would be empty. Expected stderr (filtered) is kept in |
| 57 | # <test>.stderr.exp. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 58 | # |
| 59 | # If results don't match, the output can be found in <test>.std<strm>.out, |
| 60 | # and the diff between expected and actual in <test>.std<strm>.diff. |
| 61 | # |
njn | a217f3d | 2002-09-27 08:26:27 +0000 | [diff] [blame] | 62 | # Notes on adding regression tests for a new skin are in |
| 63 | # coregrind/docs/skins.html. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 64 | #---------------------------------------------------------------------------- |
| 65 | |
| 66 | use strict; |
| 67 | |
| 68 | #---------------------------------------------------------------------------- |
| 69 | # Global vars |
| 70 | #---------------------------------------------------------------------------- |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 71 | my $usage="vg_regtest [--all, --valgrind]\n"; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 72 | |
| 73 | my $tmp="vg_regtest.tmp.$$"; |
| 74 | |
| 75 | # Test variables |
| 76 | my $vgopts; # valgrind options |
| 77 | my $prog; # test prog |
| 78 | my $args; # test prog args |
| 79 | my $stdout_filter; # filter program to run stdout results file through |
| 80 | my $stderr_filter; # filter program to run stderr results file through |
| 81 | |
| 82 | my @failures; # List of failed tests |
| 83 | |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 84 | my $num_tests_done = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 85 | |
njn | c2e7f48 | 2002-09-27 08:44:17 +0000 | [diff] [blame] | 86 | # Default valgrind to use is this build tree's one |
| 87 | my $prefix="@prefix@"; |
| 88 | my $exec_prefix="@exec_prefix@"; |
| 89 | my $valgrind = "@bindir@/valgrind"; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 90 | |
| 91 | chomp(my $tests_dir = `pwd`); |
| 92 | |
| 93 | # default filter is the one named "filter_stderr" in the test's directory |
| 94 | my $default_stderr_filter = "filter_stderr"; |
| 95 | |
| 96 | |
| 97 | #---------------------------------------------------------------------------- |
| 98 | # Process command line, setup |
| 99 | #---------------------------------------------------------------------------- |
| 100 | |
| 101 | # If $prog is a relative path, it prepends $dir to it. Useful for two reasons: |
| 102 | # |
| 103 | # 1. Can prepend "." onto programs to avoid trouble with users who don't have |
| 104 | # "." in their path (by making $dir = ".") |
| 105 | # 2. Can prepend the current dir to make the command absolute to avoid |
| 106 | # subsequent trouble when we change directories. |
| 107 | # |
| 108 | # Also checks the program exists and is executable. |
| 109 | sub validate_program ($$) |
| 110 | { |
| 111 | my ($dir, $prog) = @_; |
| 112 | |
| 113 | # If absolute path, leave it alone. If relative, make it |
| 114 | # absolute -- by prepending current dir -- so we can change |
| 115 | # dirs and still use it. |
| 116 | $prog = "$dir/$prog" if ($prog !~ /^\//); |
| 117 | (-f $prog) or die "`$prog' not found or not a file ($dir)\n"; |
| 118 | (-x $prog) or die "`$prog' not found or not executable ($dir)\n"; |
| 119 | |
| 120 | return $prog; |
| 121 | } |
| 122 | |
| 123 | sub process_command_line() |
| 124 | { |
| 125 | my $alldirs = 0; |
| 126 | my @fs; |
| 127 | |
| 128 | for my $arg (@ARGV) { |
| 129 | if ($arg =~ /^-/) { |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 130 | if ($arg =~ /^--all$/) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 131 | $alldirs = 1; |
| 132 | } elsif ($arg =~ /^--valgrind=(.*)$/) { |
| 133 | $valgrind = $1; |
| 134 | } else { |
| 135 | die $usage; |
| 136 | } |
| 137 | } else { |
| 138 | push(@fs, $arg); |
| 139 | } |
| 140 | } |
| 141 | $valgrind = validate_program($tests_dir, $valgrind); |
| 142 | |
| 143 | if ($alldirs) { |
| 144 | @fs = (); |
| 145 | foreach my $f (glob "*") { |
| 146 | push(@fs, $f) if (-d $f); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | (0 != @fs) or die "No test files or directories specified\n"; |
| 151 | |
| 152 | return @fs; |
| 153 | } |
| 154 | |
| 155 | #---------------------------------------------------------------------------- |
| 156 | # Read a .vgtest file |
| 157 | #---------------------------------------------------------------------------- |
| 158 | sub read_vgtest_file($) |
| 159 | { |
| 160 | my ($f) = @_; |
| 161 | |
| 162 | # Defaults. |
| 163 | ($vgopts, $prog, $args, $stdout_filter, $stderr_filter) = |
| 164 | ("", undef, "", undef, undef); |
| 165 | |
| 166 | # Every test directory must have a "filter_stderr" |
| 167 | $stderr_filter = validate_program(".", $default_stderr_filter); |
| 168 | |
| 169 | open(INPUTFILE, "< $f") || die "File $f not openable\n"; |
| 170 | |
| 171 | while (my $line = <INPUTFILE>) { |
| 172 | if ($line =~ /^\s*vgopts:\s*(.*)$/) { |
| 173 | $vgopts = $1; |
| 174 | } elsif ($line =~ /^\s*prog:\s*(.*)$/) { |
| 175 | $prog = validate_program(".", $1); |
| 176 | } elsif ($line =~ /^\s*args:\s*(.*)$/) { |
| 177 | $args = $1; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 178 | } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) { |
| 179 | $stdout_filter = validate_program(".", $1); |
| 180 | } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) { |
| 181 | $stderr_filter = validate_program(".", $1); |
| 182 | } else { |
| 183 | die "Bad line in $f: $line\n"; |
| 184 | } |
| 185 | } |
| 186 | close(INPUTFILE); |
| 187 | |
| 188 | if (!defined $prog) { |
| 189 | die "no `prog:' line in `$f'\n"; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #---------------------------------------------------------------------------- |
| 194 | # Do one test |
| 195 | #---------------------------------------------------------------------------- |
| 196 | # Since most of the program time is spent in system() calls, need this to |
| 197 | # propagate a Ctrl-C enabling us to quit. |
| 198 | sub mysystem($) |
| 199 | { |
| 200 | (system($_[0]) != 2) or exit 1; # 2 is SIGINT |
| 201 | } |
| 202 | |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 203 | # from a directory name like "/foo/cachesim/tests/" determine the skin name |
| 204 | sub determine_skin() |
| 205 | { |
| 206 | my $dir = `pwd`; |
| 207 | $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/skin_name/tests/foo |
| 208 | return $1; |
| 209 | } |
| 210 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 211 | sub do_one_test($$) |
| 212 | { |
| 213 | my ($dir, $vgtest) = @_; |
| 214 | $vgtest =~ /^(.*)\.vgtest/; |
| 215 | my $name = $1; |
| 216 | my $fullname = "$dir/$name"; |
| 217 | |
| 218 | read_vgtest_file($vgtest); |
| 219 | |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 220 | printf("%-16s valgrind $vgopts $prog $args\n", "$name:"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 221 | |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 222 | # Pass the appropriate --skin option for the directory (can be overridden |
| 223 | # by an "args:" or "args.dev:" line, though) |
| 224 | my $skin=determine_skin(); |
| 225 | mysystem("$valgrind --skin=$skin $vgopts $prog $args > $name.stdout.out 2> $name.stderr.out"); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 226 | |
| 227 | if (defined $stdout_filter) { |
| 228 | mysystem("$stdout_filter < $name.stdout.out > $tmp"); |
| 229 | rename($tmp, "$name.stdout.out"); |
| 230 | } |
| 231 | |
| 232 | mysystem("$stderr_filter < $name.stderr.out > $tmp"); |
| 233 | rename($tmp, "$name.stderr.out"); |
| 234 | |
| 235 | # If stdout expected empty, .exp file might be missing so diff with |
| 236 | # /dev/null |
| 237 | my $stdout_exp = ( -r "$name.stdout.exp" |
| 238 | ? "$name.stdout.exp" |
| 239 | : "/dev/null" ); |
| 240 | |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 241 | my $stderr_exp = "$name.stderr.exp"; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 242 | (-r $stderr_exp) or die "Could not read `$stderr_exp'\n"; |
| 243 | |
| 244 | mysystem("diff -C0 $stdout_exp $name.stdout.out > $name.stdout.diff"); |
| 245 | mysystem("diff -C0 $stderr_exp $name.stderr.out > $name.stderr.diff"); |
| 246 | |
| 247 | for my $ext ("stdout", "stderr") { |
| 248 | if (-s "$name.$ext.diff") { |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 249 | print "*** $name failed ($ext) ***\n"; |
| 250 | push(@failures, sprintf("%-40s $ext", "$fullname")); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 251 | } else { |
| 252 | unlink("$name.$ext.out", "$name.$ext.diff"); |
| 253 | } |
| 254 | } |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 255 | $num_tests_done++; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | #---------------------------------------------------------------------------- |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 259 | # Test one directory (and any subdirs) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 260 | #---------------------------------------------------------------------------- |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 261 | sub test_one_dir($$); # forward declaration |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 262 | |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 263 | sub test_one_dir($$) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 264 | { |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 265 | my ($dir, $prev_dirs) = @_; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 266 | $dir =~ s/\/$//; # trim a trailing '/' |
| 267 | |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 268 | # ignore CVS/ and docs/ and doc/ dirs |
| 269 | if ($dir =~ /^(CVS|docs|doc)$/) { return; } |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 270 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 271 | chdir($dir) or die "Could not change into $dir\n"; |
| 272 | |
njn | 584eaac | 2002-10-04 15:30:48 +0000 | [diff] [blame] | 273 | # Nb: Don't prepend a '/' to the base directory |
| 274 | my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir; |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 275 | my $dashes = "-" x (50 - length $full_dir); |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 276 | |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 277 | my @fs = glob "*"; |
| 278 | my @vgtests = grep { $_ =~ /\.vgtest$/ } @fs; |
| 279 | my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs)); |
| 280 | |
| 281 | if ($found_tests) { |
| 282 | print "-- Running tests in $full_dir $dashes\n"; |
| 283 | } else { |
| 284 | print "-- Found no tests in $full_dir $dashes\n"; |
| 285 | } |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 286 | foreach my $f (@fs) { |
| 287 | if (-d $f) { |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 288 | test_one_dir($f, $full_dir); |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 289 | } elsif ($f =~ /\.vgtest$/) { |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 290 | do_one_test($full_dir, $f); |
njn25 | cac76cb | 2002-09-23 11:21:57 +0000 | [diff] [blame] | 291 | } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 292 | } |
njn | db3c469 | 2002-10-04 10:03:34 +0000 | [diff] [blame] | 293 | if ($found_tests) { |
| 294 | print "-- Finished tests in $full_dir $dashes\n"; |
| 295 | } |
| 296 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 297 | chdir(".."); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | #---------------------------------------------------------------------------- |
| 301 | # Summarise results |
| 302 | #---------------------------------------------------------------------------- |
| 303 | sub summarise_results |
| 304 | { |
njn | d8ced86 | 2003-04-08 00:47:05 +0000 | [diff] [blame^] | 305 | my $x = ( $num_tests_done == 1 ? "test" : "tests" ); |
| 306 | my $n_failures = @failures; |
| 307 | |
| 308 | print "\n== $num_tests_done $x, $n_failures failed: ===============================\n"; |
| 309 | if (0 == $n_failures) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 310 | print " (none)\n"; |
| 311 | } else { |
| 312 | foreach my $failure (@failures) { |
| 313 | print "$failure\n"; |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | #---------------------------------------------------------------------------- |
| 319 | # main(), sort of |
| 320 | #---------------------------------------------------------------------------- |
| 321 | |
| 322 | # undefine $VALGRIND_OPTS |
njn | bf89f31 | 2002-10-30 15:23:05 +0000 | [diff] [blame] | 323 | # (Commented out 2002-Oct-30; was causing "Use of uninitialized value in undef |
| 324 | # operator" warning --njn) |
| 325 | # if ( exists $ENV{VALGRIND_OPTS} ) { |
| 326 | # undef $ENV{VALGRIND_OPTS}; |
| 327 | # } |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 328 | |
| 329 | my @fs = process_command_line(); |
| 330 | foreach my $f (@fs) { |
| 331 | if (-d $f) { |
njn | 584eaac | 2002-10-04 15:30:48 +0000 | [diff] [blame] | 332 | test_one_dir($f, ""); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 333 | } else { |
| 334 | # Allow the .vgtest suffix to be given or omitted |
| 335 | if ($f =~ /.vgtest$/ && -r $f) { |
| 336 | # do nothing |
| 337 | } elsif (-r "$f.vgtest") { |
| 338 | $f = "$f.vgtest"; |
| 339 | } else { |
| 340 | die "`$f' neither a directory nor a readable test file/name\n" |
| 341 | } |
| 342 | my $dir = `dirname $f`; chomp $dir; |
| 343 | my $file = `basename $f`; chomp $file; |
| 344 | chdir($dir) or die "Could not change into $dir\n"; |
| 345 | do_one_test($dir, $file); |
| 346 | chdir($tests_dir); |
| 347 | } |
| 348 | } |
| 349 | summarise_results(); |
| 350 | |
njn | c2e7f48 | 2002-09-27 08:44:17 +0000 | [diff] [blame] | 351 | ##--------------------------------------------------------------------## |
| 352 | ##--- end vg_regtest ---## |
| 353 | ##--------------------------------------------------------------------## |