1 package Test::Valgrind;
10 use Perl::Destruct::Level level => 3;
12 use Test::Valgrind::Suppressions;
16 Test::Valgrind - Test Perl code through valgrind.
24 our $VERSION = '0.06';
29 eval 'use Test::Valgrind';
30 plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
32 # Code to inspect for memory leaks/errors.
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.
38 You can also use it from the command-line to test a given script :
40 perl -MTest::Valgrind leaky.pl
42 Due to the nature of perl's memory allocator, this module can't track leaks of Perl objects. This includes non-mortalized scalars and memory cycles. However, it can track leaks of chunks of memory allocated in XS extensions with C<Newx> and friends or C<malloc>. As such, it's complementary to the other very good leak detectors listed in the L</SEE ALSO> section.
46 You can pass parameters to C<import> as a list of key / value pairs, where valid keys are :
54 Also use suppressions from C<$file> besides perl's.
58 C<< no_supp => $bool >>
60 If true, do not use any suppressions.
64 C<< callers => $number >>
66 Specify the maximum stack depth studied when valgrind encounters an error. Raising this number improves granularity. Default is 12.
70 C<< extra => [ @args ] >>
72 Add C<@args> to valgrind parameters.
78 If true, print the raw output of valgrind as diagnostics (may be quite verbose).
82 C<< no_test => $bool >>
84 If true, do not actually output the plan and the tests results.
88 C<< cb => sub { my ($val, $name) = @_; ...; return $passed } >>
90 Specifies a subroutine to execute for each test instead of C<Test::More::is>. It receives the number of bytes leaked in C<$_[0]> and the test name in C<$_[1]>, and is expected to return true if the test passed and false otherwise. Defaults to
94 (defined $_[0] and $_[0] == 0) : 1 : 0
101 my $Test = Test::Builder->new;
106 (defined $_[0] and $_[0] == 0) ? 1 : 0;
110 $Test->is_num($_[0], 0, $_[1]);
116 croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
118 if (!defined $args{run} && !$run) {
119 my ($file, $pm, $next);
122 $next = (caller $l++)[1];
123 last unless defined $next;
124 next unless $next ne '-e' and $next !~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/
126 if ($next =~ /\.pm$/) {
132 unless (defined $file) {
134 return unless defined $pm;
136 my $callers = $args{callers};
137 $callers = 12 unless defined $callers;
138 $callers = int $callers;
139 my $vg = Test::Valgrind::Suppressions::VG_PATH;
140 if (!$vg || !-x $vg) {
141 for (split /:/, $ENV{PATH}) {
149 $Test->plan(skip_all => 'No valgrind executable could be found in your path');
153 pipe my $rdr, my $wtr or croak "pipe(\$rdr, \$wtr): $!";
157 } elsif ($pid == 0) {
158 setpgrp 0, 0 or croak "setpgrp(0, 0): $!";
159 close $rdr or croak "close(\$rdr): $!";
160 open STDERR, '>&', $wtr or croak "open(STDERR, '>&', \$wtr): $!";
164 '--leak-resolution=high',
165 '--num-callers=' . $callers,
168 unless ($args{no_supp}) {
169 for (Test::Valgrind::Suppressions::supp_path(), $args{supp}) {
170 push @args, '--suppressions=' . $_ if $_;
173 if (defined $args{extra} and ref $args{extra} eq 'ARRAY') {
174 push @args, @{$args{extra}};
177 push @args, '-I' . $_ for @INC;
178 push @args, '-MTest::Valgrind=run,1', $file;
179 print STDERR "valgrind @args\n" if $args{diag};
180 local $ENV{PERL_DESTRUCT_LEVEL} = 3;
181 local $ENV{PERL_DL_NONLAZY} = 1;
184 close $wtr or croak "close(\$wtr): $!";
185 local $SIG{INT} = sub { kill -(SIGTERM) => $pid };
186 $Test->plan(tests => 5) unless $args{no_test};
189 'definitely lost', 'indirectly lost', 'possibly lost', 'still reachable'
191 my %res = map { $_ => 0 } @tests;
193 $Test->diag($_) if $args{diag};
194 if (/^=+\d+=+\s*FATAL\s*:\s*(.*)/) {
196 $Test->diag("Valgrind error: $err");
197 $res{$_} = undef for @tests;
199 if (/ERROR\s+SUMMARY\s*:\s+(\d+)/) {
200 $res{errors} = int $1;
201 } elsif (/([a-z][a-z\s]*[a-z])\s*:\s*([\d.,]+)/) {
202 my ($cat, $count) = ($1, $2);
203 if (exists $res{$cat}) {
206 $res{$cat} = int $count;
212 my $cb = ($args{no_test} ? \&_counter
213 : ($args{cb} ? $args{cb} : \&_tester));
215 $failed -= $cb->($res{$_}, 'valgrind ' . $_) ? 1 : 0;
225 You can't use this module to test code given by the C<-e> command-line switch.
227 Results will most likely be better if your perl is built with debugging enabled. Using the latest valgrind available will also help.
229 This module is not really secure. It's definitely not taint safe. That shouldn't be a problem for test files.
231 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.
235 Valgrind 3.1.0 (L<http://valgrind.org>).
237 L<Carp>, L<POSIX> (core modules since perl 5) and L<Test::Builder> (since 5.6.2).
239 L<Perl::Destruct::Level>.
243 L<Devel::Leak>, L<Devel::LeakTrace>, L<Devel::LeakTrace::Fast>.
247 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
249 You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
253 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.
257 You can find documentation for this module with the perldoc command.
259 perldoc Test::Valgrind
261 =head1 ACKNOWLEDGEMENTS
263 Rafaƫl Garcia-Suarez, for writing and instructing me about the existence of L<Perl::Destruct::Level> (Elizabeth Mattijsen is a close second).
265 H.Merijn Brand, for daring to test this thing.
267 =head1 COPYRIGHT & LICENSE
269 Copyright 2008 Vincent Pit, all rights reserved.
271 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
275 1; # End of Test::Valgrind