]> git.vpit.fr Git - perl/modules/Test-Valgrind.git/blob - lib/Test/Valgrind.pm
Importing Test-Valgrind-0.02.tar.gz
[perl/modules/Test-Valgrind.git] / lib / Test / Valgrind.pm
1 package Test::Valgrind;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7 use POSIX qw/SIGTERM/;
8 use Test::More;
9
10 use Perl::Destruct::Level level => 3;
11
12 use Test::Valgrind::Suppressions;
13
14 =head1 NAME
15
16 Test::Valgrind - Test Perl code through valgrind.
17
18 =head1 VERSION
19
20 Version 0.02
21
22 =cut
23
24 our $VERSION = '0.02';
25
26 =head1 SYNOPSIS
27
28     use Test::More;
29     eval 'use Test::Valgrind';
30     plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
31
32     # Code to inspect for memory leaks/errors.
33
34 =head1 DESCRIPTION
35
36 This module lets you run some code through the B<valgrind> memory debugger, to test it for memory errors and leaks. Just add C<use Test::Valgrind> at the beginning of the code you want to test. Behind the hood, C<Test::Valgrind::import> forks so that the child can basically C<exec 'valgrind', $^X, $0> (except that of course C<$0> isn't right there). The parent then parses the report output by valgrind and pass or fail tests accordingly.
37
38 You can also use it from the command-line to test a given script :
39
40     perl -MTest::Valgrind leaky.pl
41
42 =head1 CONFIGURATION
43
44 You can pass parameters to C<import> as a list of key / value pairs, where valid keys are :
45
46 =over 4
47
48 =item C<< supp => $file >>
49
50 Also use suppressions from C<$file> besides perl's.
51
52 =item C<< no_supp => $bool >>
53
54 If true, do not use any suppressions.
55
56 =item C<< callers => $number >>
57
58 Specify the maximum stack depth studied when valgrind encounters an error. Raising this number improves granularity. Default is 50.
59
60 =item C<< extra => [ @args ] >>
61
62 Add C<@args> to valgrind parameters.
63
64 =item C<< diag => $bool >>
65
66 If true, print the raw output of valgrind as diagnostics (may be quite verbose).
67
68 =item C<< no_test => $bool >>
69
70 If true, do not actually output the plan and the tests results.
71
72 =back
73
74 =cut
75
76 my $run;
77
78 sub import {
79  shift;
80  croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
81  my %args = @_;
82  if (!defined $args{run} && !$run) {
83   my ($file, $next);
84   my $l = 0;
85   while ($l < 1000) {
86    $next = (caller $l++)[1];
87    last unless defined $next;
88    $file = $next;
89   }
90   return if not $file or $file eq '-e';
91   my $valgrind;
92   for (split /:/, $ENV{PATH}) {
93    my $vg = $_ . '/valgrind';
94    if (-x $vg) {
95     $valgrind = $vg;
96     last;
97    }
98   }
99   if (!$valgrind) {
100    plan skip_all => 'No valgrind executable could be found in your path';
101    return;
102   }
103   my $callers = $args{callers} || 50;
104   $callers = int $callers;
105   pipe my $rdr, my $wtr or croak "pipe(\$rdr, \$wtr): $!";
106   my $pid = fork;
107   if (!defined $pid) {
108    croak "fork(): $!";
109   } elsif ($pid == 0) {
110    setpgrp 0, 0 or croak "setpgrp(0, 0): $!";
111    close $rdr or croak "close(\$rdr): $!";
112    open STDERR, '>&', $wtr or croak "open(STDERR, '>&', \$wtr): $!";
113    my @args = (
114     '--tool=memcheck',
115     '--leak-check=full',
116     '--leak-resolution=high',
117     '--num-callers=' . $callers,
118     '--error-limit=yes'
119    );
120    unless ($args{no_supp}) {
121     for (Test::Valgrind::Suppressions::supp_path(), $args{supp}) {
122      push @args, '--suppressions=' . $_ if $_;
123     }
124    }
125    if (defined $args{extra} and ref $args{extra} eq 'ARRAY') {
126     push @args, @{$args{extra}};
127    }
128    push @args, $^X;
129    push @args, '-I' . $_ for @INC;
130    push @args, '-MTest::Valgrind=run,1', $file;
131    print STDERR "valgrind @args\n" if $args{diag};
132    local $ENV{PERL_DESTRUCT_LEVEL} = 3;
133    local $ENV{PERL_DL_NONLAZY} = 1;
134    my $vg = Test::Valgrind::Suppressions::VG_PATH;
135    exec $vg, @args if $vg and -x $vg;
136   }
137   close $wtr or croak "close(\$wtr): $!";
138   local $SIG{INT} = sub { kill -(SIGTERM) => $pid };
139   plan tests => 5 unless $args{no_test};
140   my @tests = (
141    'errors',
142    'definitely lost', 'indirectly lost', 'possibly lost', 'still reachable'
143   );
144   my %res = map { $_ => 0 } @tests;
145   while (<$rdr>) {
146    diag $_ if $args{diag};
147    if (/^=+\d+=+\s*FATAL\s*:\s*(.*)/) {
148     chomp(my $err = $1);
149     diag "Valgrind error: $err";
150     $res{$_} = undef for @tests;
151    }
152    if (/ERROR\s+SUMMARY\s*:\s+(\d+)/) {
153     $res{errors} = int $1;
154    } elsif (/([a-z][a-z\s]*[a-z])\s*:\s*([\d.,]+)/) {
155     my ($cat, $count) = ($1, $2);
156     if (exists $res{$cat}) {
157      $cat =~ s/\s+/ /g;
158      $count =~ s/[.,]//g;
159      $res{$cat} = int $count;
160     }
161    }
162   }
163   waitpid $pid, 0;
164   my $failed = 0;
165   for (@tests) {
166    is($res{$_}, 0, 'valgrind ' . $_) unless $args{no_test};
167    ++$failed if defined $res{$_} and $res{$_} != 0;
168   }
169   exit $failed;
170  } else {
171   $run = 1;
172  }
173 }
174
175 =head1 CAVEATS
176
177 You can't use this module to test code given by the C<-e> command-line switch.
178
179 Results will most likely be better if your perl is built with debugging enabled. Using the latest valgrind available will also help.
180
181 This module is not really secure. It's definitely not taint safe. That shouldn't be a problem for test files.
182
183 If your tests output to STDERR, everything will be eaten in the process. In particular, running this module against test files will obliterate their original test results.
184
185 =head1 DEPENDENCIES
186
187 Valgrind 3.1.0 (L<http://valgrind.org>).
188
189 L<Carp>, L<POSIX> (core modules since perl 5) and L<Test::More> (since 5.6.2).
190
191 L<Perl::Destruct::Level>.
192
193 =head1 AUTHOR
194
195 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
196
197 You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
198
199 =head1 BUGS
200
201 Please report any bugs or feature requests to C<bug-test-valgrind at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Valgrind>.  I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
202
203 =head1 SUPPORT
204
205 You can find documentation for this module with the perldoc command.
206
207     perldoc Test::Valgrind
208
209 =head1 ACKNOWLEDGEMENTS
210
211 RafaĆ«l Garcia-Suarez, for writing and instructing me about the existence of L<Perl::Destruct::Level> (Elizabeth Mattijsen is a close second).
212
213 H.Merijn Brand, for daring to test this thing.
214
215 =head1 COPYRIGHT & LICENSE
216
217 Copyright 2008 Vincent Pit, all rights reserved.
218
219 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
220
221 =cut
222
223 1; # End of Test::Valgrind