1 package Test::Valgrind;
8 Test::Valgrind - Test Perl code through valgrind.
16 our $VERSION = '1.00';
20 # From the command-line
21 perl -MTest::Valgrind leaky.pl
25 eval 'use Test::Valgrind';
26 plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
29 # In all the test files of a directory
30 prove --exec 'perl -Iblib/lib -Iblib/arch -MTest::Valgrind' t/*.t
34 This module is a front-end to the C<Test::Valgrind::*> API that lets you run Perl code through the C<memcheck> tool of the C<valgrind> memory debugger, to test it for memory errors and leaks.
35 If they aren't available yet, it will first generate suppressions for the current C<perl> interpreter and store them in the portable flavour of F<~/.perl/Test-Valgrind/suppressions/$VERSION>.
36 The actual run will then take place, and tests will be passed or failed according to the result of the analysis.
38 Due to the nature of perl's memory allocator, this module can't track leaks of Perl objects.
39 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>.
40 As such, it's complementary to the other very good leak detectors listed in the L</SEE ALSO> section.
44 You can pass parameters to C<import> as a list of key / value pairs, where valid keys are :
52 The L<Test::Valgrind::Tool> object (or class name) to use.
54 Defaults to L<Test::Valgrind::Tool::memcheck>.
58 C<< action => $action >>
60 The L<Test::Valgrind::Action> object (or class name) to use.
62 Defaults to L<Test::Valgrind::Action::Test>.
68 If true, print the output of the test script as diagnostics.
72 C<< callers => $number >>
74 Specify the maximum stack depth studied when valgrind encounters an error.
75 Raising this number improves granularity.
81 C<< extra_supps => \@files >>
83 Also use suppressions from C<@files> besides C<perl>'s.
87 C<< no_def_supp => $bool >>
89 If true, do not use the default suppression file.
95 # We use as little modules as possible in run mode so that they don't pollute
96 # the analysis. Hence all the requires.
105 Carp::croak('Optional arguments must be passed as key => value pairs');
109 if (defined $args{run} or $run) {
110 require Perl::Destruct::Level;
111 Perl::Destruct::Level::set_destruct_level(3);
113 my $oldfh = select STDOUT;
121 my ($file, $pm, $next);
124 $next = (caller $l++)[1];
125 last unless defined $next;
126 next if $next eq '-e' or $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/ or !-f $next;
127 if ($next =~ /\.pm$/) {
133 unless (defined($file) or defined($file = $pm)) {
134 require Test::Builder;
135 Test::Builder->new->diag('Couldn\'t find a valid source file');
141 open my $fh, '<', $file or last;
144 if ($first and my ($args) = $first =~ /^\s*#\s*!\s*perl\s*(.*)/) {
145 $taint_mode = 1 if $args =~ /(?:^|\s)-T(?:$|\s)/;
149 require Test::Valgrind::Command;
150 my $cmd = Test::Valgrind::Command->new(
152 args => [ '-MTest::Valgrind=run,1', (('-T') x!! $taint_mode), $file ],
155 my $instanceof = sub {
156 require Scalar::Util;
157 Scalar::Util::blessed($_[0]) && $_[0]->isa($_[1]);
160 my $tool = delete $args{tool};
161 unless ($tool->$instanceof('Test::Valgrind::Tool')) {
162 require Test::Valgrind::Tool;
163 $tool = Test::Valgrind::Tool->new(
164 tool => $tool || 'memcheck',
165 callers => delete($args{callers}),
169 my $action = delete $args{action};
170 unless ($action->$instanceof('Test::Valgrind::Action')) {
171 require Test::Valgrind::Action;
172 $action = Test::Valgrind::Action->new(
173 action => $action || 'Test',
174 diag => delete($args{diag}),
178 require Test::Valgrind::Session;
180 Test::Valgrind::Session->new(
181 min_version => $tool->requires_version,
182 map { $_ => delete $args{$_} } qw/extra_supps no_def_supp/
186 $action->abort($sess, $@);
187 exit $action->status($sess);
198 require Test::Valgrind::Report;
199 $action->report($sess, Test::Valgrind::Report->new_diag($@));
202 my $status = $sess->status;
203 $status = 255 unless defined $status;
209 if ($run and eval { require DynaLoader; 1 }) {
211 DynaLoader::dl_unload_file($_) or push @rest, $_ for @DynaLoader::dl_librefs;
212 @DynaLoader::dl_librefs = @rest;
218 You can't use this module to test code given by the C<-e> command-line switch.
220 Perl 5.8 is notorious for leaking like there's no tomorrow, so the suppressions are very likely not to be very accurate on it. Anyhow, results will most likely be better if your perl is built with debugging enabled. Using the latest C<valgrind> available will also help.
222 This module is not really secure. It's definitely not taint safe. That shouldn't be a problem for test files.
224 What your tests output to C<STDOUT> and C<STDERR> is eaten unless you pass the C<diag> option, in which case it will be reprinted as diagnostics.
228 Valgrind 3.1.0 (L<http://valgrind.org>).
230 L<XML::Twig>, L<version>, L<File::HomeDir>, L<Env::Sanctify>, L<Perl::Destruct::Level>.
234 All the C<Test::Valgrind::*> API, including L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Action> and L<Test::Valgrind::Session>.
238 L<Devel::Leak>, L<Devel::LeakTrace>, L<Devel::LeakTrace::Fast>.
242 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
244 You can contact me by mail or on C<irc.perl.org> (vincent).
248 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>.
249 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
253 You can find documentation for this module with the perldoc command.
255 perldoc Test::Valgrind
257 =head1 ACKNOWLEDGEMENTS
259 Rafaƫl Garcia-Suarez, for writing and instructing me about the existence of L<Perl::Destruct::Level> (Elizabeth Mattijsen is a close second).
261 H.Merijn Brand, for daring to test this thing.
263 All you people that showed interest in this module, which motivated me into completely rewriting it.
265 =head1 COPYRIGHT & LICENSE
267 Copyright 2008-2009 Vincent Pit, all rights reserved.
269 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
273 1; # End of Test::Valgrind