2 ##--------------------------------------------------------------------##
3 ##--- Valgrind regression testing script vg_regtest ---##
4 ##--------------------------------------------------------------------##
6 # This file is part of Valgrind, a dynamic binary instrumentation
9 # Copyright (C) 2003 Nicholas Nethercote
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.
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.
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
27 # The GNU General Public License is contained in the file COPYING.
29 #----------------------------------------------------------------------------
30 # usage: vg_regtest [options] <dirs | files>
33 # --all: run tests in all subdirs
34 # --valgrind: valgrind launcher to use. Default is ./coregrind/valgrind.
35 # (This option should probably only be used in conjunction with
37 # --valgrind-lib: valgrind libraries to use. Default is $tests_dir/.in_place.
38 # (This option should probably only be used in conjunction with
40 # --keep-unfiltered: keep a copy of the unfiltered output/error output
41 # of each test by adding an extension .unfiltered.out
43 # The easiest way is to run all tests in valgrind/ with (assuming you installed
46 # $PREFIX/bin/vg_regtest --all
48 # You can specify individual files to test, or whole directories, or both.
49 # Directories are traversed recursively, except for ones named, for example,
52 # Each test is defined in a file <test>.vgtest, containing one or more of the
53 # following lines, in any order:
54 # - prog: <prog to run> (compulsory)
55 # - args: <args for prog> (default: none)
56 # - vgopts: <Valgrind options> (default: none;
57 # multiple are allowed)
58 # - stdout_filter: <filter to run stdout through> (default: none)
59 # - stderr_filter: <filter to run stderr through> (default: ./filter_stderr)
61 # - progB: <prog to run in parallel with prog> (default: none)
62 # - argsB: <args for progB> (default: none)
63 # - stdinB: <input file for progB> (default: none)
64 # - stdoutB_filter: <filter progB stdout through> (default: none)
65 # - stderrB_filter: <filter progB stderr through> (default: ./filter_stderr)
67 # - prereq: <prerequisite command> (default: none)
68 # - post: <post-test check command> (default: none)
69 # - cleanup: <post-test cleanup cmd> (default: none)
71 # If prog or probB is a relative path, it will be prefix with the test directory.
72 # Note that filters are necessary for stderr results to filter out things that
73 # always change, eg. process id numbers.
74 # Note that if a progB is specified, it is started in background (before prog).
76 # Expected stdout (filtered) is kept in <test>.stdout.exp* (can be more
77 # than one expected output). It can be missing if it would be empty. Expected
78 # stderr (filtered) is kept in <test>.stderr.exp*. There must be at least
79 # one stderr.exp* file. Any .exp* file that ends in '~' or '#' is ignored;
80 # this is because Emacs creates temporary files of these names.
82 # Expected output for progB is handled similarly, except that
83 # expected stdout and stderr for progB are in <test>.stdoutB.exp*
84 # and <test>.stderrB.exp*.
86 # If results don't match, the output can be found in <test>.std<strm>.out,
87 # and the diff between expected and actual in <test>.std<strm>.diff*.
88 # (for progB, in <test>.std<strm>2.out and <test>.std<strm>2.diff*).
90 # The prerequisite command, if present, works like this:
91 # - if it returns 0 the test is run
92 # - if it returns 1 the test is skipped
93 # - if it returns anything else the script aborts.
94 # The idea here is results other than 0 or 1 are likely to be due to
95 # problems with the commands, and you don't want to conflate them with the 1
96 # case, which would happen if you just tested for zero or non-zero.
98 # The post-test command, if present, must return 0 and its stdout must match
99 # the expected stdout which is kept in <test>.post.exp*.
101 # Sometimes it is useful to run all the tests at a high sanity check
102 # level or with arbitrary other flags. To make this simple, extra
103 # options, applied to all tests run, are read from $EXTRA_REGTEST_OPTS,
104 # and handed to valgrind prior to any other flags specified by the
107 # Some more notes on adding regression tests for a new tool are in
108 # docs/xml/manual-writing-tools.xml.
109 #----------------------------------------------------------------------------
114 #----------------------------------------------------------------------------
116 #----------------------------------------------------------------------------
119 . " vg_regtest [--all, --valgrind, --valgrind-lib, --keep-unfiltered]\n"
120 . " Use EXTRA_REGTEST_OPTS to supply extra args for all tests\n"
123 my $tmp="vg_regtest.tmp.$$";
126 my $vgopts; # valgrind options
127 my $prog; # test prog
128 my $args; # test prog args
129 my $stdout_filter; # filter program to run stdout results file through
130 my $stderr_filter; # filter program to run stderr results file through
131 my $progB; # Same but for progB
133 my $stdoutB_filter; #
134 my $stderrB_filter; #
135 my $stdinB; # Input file for progB
136 my $prereq; # prerequisite test to satisfy before running test
137 my $post; # check command after running test
138 my $cleanup; # cleanup command to run
140 my @failures; # List of failed tests
142 my $num_tests_done = 0;
143 my %num_failures = (stderr => 0, stdout => 0,
144 stderrB => 0, stdoutB => 0,
147 # Default valgrind to use is this build tree's (uninstalled) one
148 my $valgrind = "./coregrind/valgrind";
150 chomp(my $tests_dir = `pwd`);
152 my $valgrind_lib = "$tests_dir/.in_place";
153 my $keepunfiltered = 0;
155 # default filter is the one named "filter_stderr" in the test's directory
156 my $default_stderr_filter = "filter_stderr";
159 #----------------------------------------------------------------------------
160 # Process command line, setup
161 #----------------------------------------------------------------------------
163 # If $prog is a relative path, it prepends $dir to it. Useful for two reasons:
165 # 1. Can prepend "." onto programs to avoid trouble with users who don't have
166 # "." in their path (by making $dir = ".")
167 # 2. Can prepend the current dir to make the command absolute to avoid
168 # subsequent trouble when we change directories.
170 # Also checks the program exists and is executable.
171 sub validate_program ($$$$)
173 my ($dir, $prog, $must_exist, $must_be_executable) = @_;
175 # If absolute path, leave it alone. If relative, make it
176 # absolute -- by prepending current dir -- so we can change
177 # dirs and still use it.
178 $prog = "$dir/$prog" if ($prog !~ /^\//);
180 (-f $prog) or die "vg_regtest: `$prog' not found or not a file ($dir)\n";
182 if ($must_be_executable) {
183 (-x $prog) or die "vg_regtest: `$prog' not executable ($dir)\n";
189 sub process_command_line()
194 for my $arg (@ARGV) {
196 if ($arg =~ /^--all$/) {
198 } elsif ($arg =~ /^--valgrind=(.*)$/) {
200 } elsif ($arg =~ /^--valgrind-lib=(.*)$/) {
202 } elsif ($arg =~ /^--keep-unfiltered$/) {
211 $valgrind = validate_program($tests_dir, $valgrind, 1, 0);
215 foreach my $f (glob "*") {
216 push(@fs, $f) if (-d $f);
220 (0 != @fs) or die "No test files or directories specified\n";
225 #----------------------------------------------------------------------------
226 # Read a .vgtest file
227 #----------------------------------------------------------------------------
228 sub read_vgtest_file($)
233 ($vgopts, $prog, $args) = ("", undef, "");
234 ($stdout_filter, $stderr_filter) = (undef, undef);
235 ($progB, $argsB, $stdinB) = (undef, "", undef);
236 ($stdoutB_filter, $stderrB_filter) = (undef, undef);
237 ($prereq, $post, $cleanup) = (undef, undef, undef);
239 # Every test directory must have a "filter_stderr"
240 $stderr_filter = validate_program(".", $default_stderr_filter, 1, 1);
241 $stderrB_filter = validate_program(".", $default_stderr_filter, 1, 1);
244 open(INPUTFILE, "< $f") || die "File $f not openable\n";
246 while (my $line = <INPUTFILE>) {
247 if ($line =~ /^\s*#/ || $line =~ /^\s*$/) {
249 } elsif ($line =~ /^\s*vgopts:\s*(.*)$/) {
251 $addvgopts =~ s/\${PWD}/$ENV{PWD}/g;
252 $vgopts = $vgopts . " " . $addvgopts; # Nb: Make sure there's a space!
253 } elsif ($line =~ /^\s*prog:\s*(.*)$/) {
254 $prog = validate_program(".", $1, 0, 0);
255 } elsif ($line =~ /^\s*args:\s*(.*)$/) {
257 } elsif ($line =~ /^\s*stdout_filter:\s*(.*)$/) {
258 $stdout_filter = validate_program(".", $1, 1, 1);
259 } elsif ($line =~ /^\s*stderr_filter:\s*(.*)$/) {
260 $stderr_filter = validate_program(".", $1, 1, 1);
261 } elsif ($line =~ /^\s*progB:\s*(.*)$/) {
262 $progB = validate_program(".", $1, 0, 0);
263 } elsif ($line =~ /^\s*argsB:\s*(.*)$/) {
265 } elsif ($line =~ /^\s*stdinB:\s*(.*)$/) {
267 } elsif ($line =~ /^\s*stdoutB_filter:\s*(.*)$/) {
268 $stdoutB_filter = validate_program(".", $1, 1, 1);
269 } elsif ($line =~ /^\s*stderrB_filter:\s*(.*)$/) {
270 $stderrB_filter = validate_program(".", $1, 1, 1);
271 } elsif ($line =~ /^\s*prereq:\s*(.*)$/) {
273 } elsif ($line =~ /^\s*post:\s*(.*)$/) {
275 } elsif ($line =~ /^\s*cleanup:\s*(.*)$/) {
278 die "Bad line in $f: $line\n";
283 if (!defined $prog) {
284 $prog = ""; # allow no prog for testing error and --help cases
288 #----------------------------------------------------------------------------
290 #----------------------------------------------------------------------------
291 # Since most of the program time is spent in system() calls, need this to
292 # propagate a Ctrl-C enabling us to quit.
295 my $exit_code = system($_[0]);
296 ($exit_code == 2) and exit 1; # 2 is SIGINT
300 # if $keepunfiltered, copies $1 to $1.unfiltered.out
302 sub filtered_rename($$)
304 if ($keepunfiltered == 1) {
305 mysystem("cp $_[1] $_[1].unfiltered.out");
307 rename ($_[0], $_[1]);
311 # from a directory name like "/foo/cachesim/tests/" determine the tool name
315 $dir =~ /.*\/([^\/]+)\/tests.*/; # foo/tool_name/tests/foo
319 # Compare output against expected output; it should match at least one of
323 my ($fullname, $name, $mid, $f_exps) = @_;
325 for my $f_exp (@$f_exps) {
326 (-r $f_exp) or die "Could not read `$f_exp'\n";
328 # Emacs produces temporary files that end in '~' and '#'. We ignore
330 if ($f_exp !~ /[~#]$/) {
331 # $n is the (optional) suffix after the ".exp"; we tack it onto
334 if ($f_exp =~ /.*\.exp(.*)$/) {
338 ($f_exp eq "/dev/null") or die "Unexpected .exp file: $f_exp\n";
341 mysystem("@DIFF@ $f_exp $name.$mid.out > $name.$mid.diff$n");
343 if (not -s "$name.$mid.diff$n") {
344 # A match; remove .out and any previously created .diff files.
345 unlink("$name.$mid.out");
346 unlink(<$name.$mid.diff*>);
351 # If we reach here, none of the .exp files matched.
352 print "*** $name failed ($mid) ***\n";
353 push(@failures, sprintf("%-40s ($mid)", "$fullname"));
354 $num_failures{$mid}++;
359 my ($dir, $vgtest) = @_;
360 $vgtest =~ /^(.*)\.vgtest/;
362 my $fullname = "$dir/$name";
364 # Pull any extra options (for example, --sanity-level=4)
365 # from $EXTRA_REGTEST_OPTS.
366 my $maybe_extraopts = $ENV{"EXTRA_REGTEST_OPTS"};
367 my $extraopts = $maybe_extraopts ? $maybe_extraopts : "";
369 read_vgtest_file($vgtest);
371 if (defined $prereq) {
372 my $prereq_res = system("$prereq");
373 if (0 == $prereq_res) {
374 # Do nothing (ie. continue with the test)
375 } elsif (256 == $prereq_res) {
376 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
377 # Prereq failed, skip.
378 printf("%-16s (skipping, prereq failed: $prereq)\n", "$name:");
383 die "prereq returned $prereq_res: $prereq\n";
388 if (defined $progB) {
389 # If there is a progB, let's start it in background:
390 printf("%-16s valgrind $extraopts $vgopts $prog $args (progB: $progB $argsB)\n",
392 # progB.done used to detect child has finished. See below.
393 # Note: redirection of stdout and stderr is before $progB to allow argsB
394 # to e.g. redirect stdoutB to stderrB
395 if (defined $stdinB) {
396 mysystem("(rm -f progB.done;"
397 . " < $stdinB > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
398 . "touch progB.done) &");
400 mysystem("(rm -f progB.done;"
401 . " > $name.stdoutB.out 2> $name.stderrB.out $progB $argsB;"
402 . "touch progB.done) &");
405 printf("%-16s valgrind $extraopts $vgopts $prog $args\n", "$name:");
408 # Pass the appropriate --tool option for the directory (can be overridden
409 # by an "args:" line, though). Set both VALGRIND_LIB and
410 # VALGRIND_LIB_INNER in case this Valgrind was configured with
412 my $tool=determine_tool();
413 mysystem("VALGRIND_LIB=$valgrind_lib VALGRIND_LIB_INNER=$valgrind_lib "
414 . "$valgrind --command-line-only=yes --memcheck:leak-check=no "
415 . "--tool=$tool $extraopts $vgopts "
416 . "$prog $args > $name.stdout.out 2> $name.stderr.out");
419 if (defined $stdout_filter) {
420 mysystem("$stdout_filter < $name.stdout.out > $tmp");
421 filtered_rename($tmp, "$name.stdout.out");
423 # Find all the .stdout.exp files. If none, use /dev/null.
424 my @stdout_exps = <$name.stdout.exp*>;
425 @stdout_exps = ( "/dev/null" ) if (0 == scalar @stdout_exps);
426 do_diffs($fullname, $name, "stdout", \@stdout_exps);
429 mysystem("$stderr_filter < $name.stderr.out > $tmp");
430 filtered_rename($tmp, "$name.stderr.out");
431 # Find all the .stderr.exp files. At least one must exist.
432 my @stderr_exps = <$name.stderr.exp*>;
433 (0 != scalar @stderr_exps) or die "Could not find `$name.stderr.exp*'\n";
434 do_diffs($fullname, $name, "stderr", \@stderr_exps);
436 if (defined $progB) {
437 # wait for the child to be finished
438 # tried things such as:
440 # $SIG{CHLD} = sub { wait };
441 # but nothing worked:
442 # e.g. running mssnapshot.vgtest in a loop failed from time to time
443 # due to some missing output (not yet written?).
444 # So, we search progB.done during max 100 times 100 millisecond.
446 for ($count = 1; $count <= 100; $count++) {
447 (-f "progB.done") or select(undef, undef, undef, 0.100);
450 if (defined $stdoutB_filter) {
451 mysystem("$stdoutB_filter < $name.stdoutB.out > $tmp");
452 filtered_rename($tmp, "$name.stdoutB.out");
454 # Find all the .stdoutB.exp files. If none, use /dev/null.
455 my @stdoutB_exps = <$name.stdoutB.exp*>;
456 @stdoutB_exps = ( "/dev/null" ) if (0 == scalar @stdoutB_exps);
457 do_diffs($fullname, $name, "stdoutB", \@stdoutB_exps);
460 mysystem("$stderrB_filter < $name.stderrB.out > $tmp");
461 filtered_rename($tmp, "$name.stderrB.out");
462 # Find all the .stderrB.exp files. At least one must exist.
463 my @stderrB_exps = <$name.stderrB.exp*>;
464 (0 != scalar @stderrB_exps) or die "Could not find `$name.stderrB.exp*'\n";
465 do_diffs($fullname, $name, "stderrB", \@stderrB_exps);
468 # Maybe do post-test check
470 if (mysystem("$post > $name.post.out") != 0) {
471 print("post check failed: $post\n");
472 $num_failures{"post"}++;
474 # Find all the .post.exp files. If none, use /dev/null.
475 my @post_exps = <$name.post.exp*>;
476 @post_exps = ( "/dev/null" ) if (0 == scalar @post_exps);
477 do_diffs($fullname, $name, "post", \@post_exps);
481 if (defined $cleanup) {
482 (system("$cleanup") == 0) or
483 print("(cleanup operation failed: $cleanup)\n");
489 #----------------------------------------------------------------------------
490 # Test one directory (and any subdirs)
491 #----------------------------------------------------------------------------
492 sub test_one_dir($$); # forward declaration
496 my ($dir, $prev_dirs) = @_;
497 $dir =~ s/\/$//; # trim a trailing '/'
499 # Ignore dirs into which we should not recurse.
500 if ($dir =~ /^(BitKeeper|CVS|SCCS|docs|doc)$/) { return; }
502 (-x "$tests_dir/tests/arch_test") or die
503 "vg_regtest: 'arch_test' is missing. Did you forget to 'make check'?\n";
505 # Ignore any dir whose name matches that of an architecture which is not
506 # the architecture we are running on. Eg. when running on x86, ignore
507 # ppc/ directories ('arch_test' returns 1 for this case). Likewise for
508 # the OS and platform.
509 # Nb: weird Perl-ism -- exit code of '1' is seen by Perl as 256...
510 if (256 == system("$tests_dir/tests/arch_test $dir")) { return; }
511 if (256 == system("$tests_dir/tests/os_test $dir")) { return; }
512 if ($dir =~ /(\w+)-(\w+)/ &&
513 256 == system("sh $tests_dir/tests/platform_test $1 $2")) { return; }
515 chdir($dir) or die "Could not change into $dir\n";
517 # Nb: Don't prepend a '/' to the base directory
518 my $full_dir = $prev_dirs . ($prev_dirs eq "" ? "" : "/") . $dir;
519 my $dashes = "-" x (50 - length $full_dir);
522 my $found_tests = (0 != (grep { $_ =~ /\.vgtest$/ } @fs));
525 print "-- Running tests in $full_dir $dashes\n";
527 foreach my $f (@fs) {
529 test_one_dir($f, $full_dir);
530 } elsif ($f =~ /\.vgtest$/) {
531 do_one_test($full_dir, $f);
535 print "-- Finished tests in $full_dir $dashes\n";
541 #----------------------------------------------------------------------------
543 #----------------------------------------------------------------------------
546 return ( $_[0] == 1 ? "" : "s" );
549 sub summarise_results
551 my $x = ( $num_tests_done == 1 ? "test" : "tests" );
553 printf("\n== %d test%s, %d stderr failure%s, %d stdout failure%s, "
554 . "%d stderrB failure%s, %d stdoutB failure%s, "
555 . "%d post failure%s ==\n",
556 $num_tests_done, plural($num_tests_done),
557 $num_failures{"stderr"}, plural($num_failures{"stderr"}),
558 $num_failures{"stdout"}, plural($num_failures{"stdout"}),
559 $num_failures{"stderrB"}, plural($num_failures{"stderrB"}),
560 $num_failures{"stdoutB"}, plural($num_failures{"stdoutB"}),
561 $num_failures{"post"}, plural($num_failures{"post"}));
563 foreach my $failure (@failures) {
569 #----------------------------------------------------------------------------
571 #----------------------------------------------------------------------------
572 sub warn_about_EXTRA_REGTEST_OPTS()
574 print "WARNING: \$EXTRA_REGTEST_OPTS is set. You probably don't want\n";
575 print "to run the regression tests with it set, unless you are doing some\n";
576 print "strange experiment, and/or you really know what you are doing.\n";
581 $ENV{"VALGRIND_OPTS"} = "";
583 if ($ENV{"EXTRA_REGTEST_OPTS"}) {
585 warn_about_EXTRA_REGTEST_OPTS();
588 my @fs = process_command_line();
589 foreach my $f (@fs) {
591 test_one_dir($f, "");
593 # Allow the .vgtest suffix to be given or omitted
594 if ($f =~ /.vgtest$/ && -r $f) {
596 } elsif (-r "$f.vgtest") {
599 die "`$f' neither a directory nor a readable test file/name\n"
601 my $dir = `dirname $f`; chomp $dir;
602 my $file = `basename $f`; chomp $file;
603 chdir($dir) or die "Could not change into $dir\n";
604 do_one_test($dir, $file);
610 if ($ENV{"EXTRA_REGTEST_OPTS"}) {
611 warn_about_EXTRA_REGTEST_OPTS();
614 if (0 == $num_failures{"stdout"} &&
615 0 == $num_failures{"stderr"} &&
616 0 == $num_failures{"stdoutB"} &&
617 0 == $num_failures{"stderrB"} &&
618 0 == $num_failures{"post"}) {
624 ##--------------------------------------------------------------------##
625 ##--- end vg_regtest ---##
626 ##--------------------------------------------------------------------##