1 package Test::Valgrind;
8 Test::Valgrind - Generate suppressions, analyse and test any command with valgrind.
16 our $VERSION = '1.16';
20 # From the command-line
21 perl -MTest::Valgrind leaky.pl
23 # From the command-line, snippet style
24 perl -MTest::Valgrind -e 'leaky()'
28 eval 'use Test::Valgrind';
29 plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
32 # In all the test files of a directory
33 prove --exec 'perl -Iblib/lib -Iblib/arch -MTest::Valgrind' t/*.t
37 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 for memory errors and leaks.
38 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>.
39 The actual run will then take place, and tests will be passed or failed according to the result of the analysis.
41 The complete API is much more versatile than this.
42 By declaring an appropriate L<Test::Valgrind::Command> class, you can run any executable (that is, not only Perl scripts) under valgrind, generate the corresponding suppressions on-the-fly and convert the analysis result to TAP output so that it can be incorporated into your project's testsuite.
43 If you're not interested in producing TAP, you can output the results in whatever format you like (for example HTML pages) by defining your own L<Test::Valgrind::Action> class.
45 Due to the nature of perl's memory allocator, this module can't track leaks of Perl objects.
46 This includes non-mortalized scalars and memory cycles.
47 However, it can track leaks of chunks of memory allocated in XS extensions with C<Newx> and friends or C<malloc>.
48 As such, it's complementary to the other very good leak detectors listed in the L</SEE ALSO> section.
54 Test::Valgrind->analyse(%options);
56 Run a C<valgrind> analysis configured by C<%options> :
62 C<< command => $command >>
64 The L<Test::Valgrind::Command> object (or class name) to use.
66 Defaults to L<Test::Valgrind::Command::PerlScript>.
72 The L<Test::Valgrind::Tool> object (or class name) to use.
74 Defaults to L<Test::Valgrind::Tool::memcheck>.
78 C<< action => $action >>
80 The L<Test::Valgrind::Action> object (or class name) to use.
82 Defaults to L<Test::Valgrind::Action::Test>.
88 The file name of the script to analyse.
90 Ignored if you supply your own custom C<command>, but mandatory otherwise.
94 C<< callers => $number >>
96 Specify the maximum stack depth studied when valgrind encounters an error.
97 Raising this number improves granularity.
99 Ignored if you supply your own custom C<tool>, otherwise defaults to C<50>.
105 If true, print the output of the test script as diagnostics.
107 Ignored if you supply your own custom C<action>, otherwise defaults to false.
111 C<< regen_def_supp => $bool >>
113 If true, forcefully regenerate the default suppression file.
119 C<< no_def_supp => $bool >>
121 If true, do not use the default suppression file.
127 C<< allow_no_supp => $bool >>
129 If true, force running the analysis even if the suppression files do not refer to any C<perl>-related symbol.
135 C<< extra_supps => \@files >>
137 Also use suppressions from C<@files> besides C<perl>'s.
150 my %skippable_errors = (
152 'Empty valgrind candidates list',
153 'No appropriate valgrind executable could be found',
159 'No compatible suppressions available',
165 for my $obj (keys %skippable_errors) {
166 my @errors = @{$skippable_errors{$obj} || []};
168 my $rxp = join '|', @errors;
169 $rxp = qr/($rxp)\s+at.*/;
170 $filter_errors{$obj} = sub {
172 if ($err =~ /$rxp/) {
179 $filter_errors{$obj} = sub {
188 require Test::Builder;
189 my $tb = Test::Builder->new;
190 my $plan = $tb->has_plan;
205 my $instanceof = sub {
206 require Scalar::Util;
207 Scalar::Util::blessed($_[0]) && $_[0]->isa($_[1]);
210 my $tool = delete $args{tool};
211 unless ($tool->$instanceof('Test::Valgrind::Tool')) {
212 require Test::Valgrind::Tool;
215 Test::Valgrind::Tool->new(
216 tool => $tool || 'memcheck',
217 callers => delete $args{callers},
221 my ($err, $skippable) = $filter_errors{tool}->($@);
222 _croak($err) unless $skippable;
223 return _default_abort($err);
227 require Test::Valgrind::Session;
229 Test::Valgrind::Session->new(
230 min_version => $tool->requires_version,
231 map { $_ => delete $args{$_} } qw<
240 my ($err, $skippable) = $filter_errors{session}->($@);
241 _croak($err) unless $skippable;
242 return _default_abort($err);
245 my $action = delete $args{action};
246 unless ($action->$instanceof('Test::Valgrind::Action')) {
247 require Test::Valgrind::Action;
250 Test::Valgrind::Action->new(
251 action => $action || 'Test',
252 diag => delete $args{diag},
256 my ($err, $skippable) = $filter_errors{action}->($@);
257 _croak($err) unless $skippable;
258 return _default_abort($err);
262 my $cmd = delete $args{command};
263 unless ($cmd->$instanceof('Test::Valgrind::Command')) {
264 require Test::Valgrind::Command;
267 Test::Valgrind::Command->new(
268 command => $cmd || 'PerlScript',
269 file => delete $args{file},
270 args => [ '-MTest::Valgrind=run,1' ],
274 my ($err, $skippable) = $filter_errors{command}->($@);
275 _croak($err) unless $skippable;
276 $action->abort($sess, $err);
277 return $action->status($sess);
291 my ($err, $skippable) = $filter_errors{run}->($@);
293 $action->abort($sess, $err);
294 return $action->status($sess);
296 require Test::Valgrind::Report;
297 $action->report($sess, Test::Valgrind::Report->new_diag($@));
302 my $status = $sess->status;
303 $status = 255 unless defined $status;
310 use Test::Valgrind %options;
312 In the parent process, L</import> calls L</analyse> with the arguments it received itself - except that if no C<file> option was supplied, it tries to pick the first caller context that looks like a script.
313 When the analysis ends, it exits with the status returned by the action (for the default TAP-generator action, it's the number of failed tests).
315 In the child process, it just C<return>s so that the calling code is actually run under C<valgrind>, albeit two side-effects :
321 L<Perl::Destruct::Level> is loaded and the destruction level is set to C<3>.
325 Autoflush on C<STDOUT> is turned on.
331 # We use as little modules as possible in run mode so that they don't pollute
332 # the analysis. Hence all the requires.
338 $class = ref($class) || $class;
340 _croak('Optional arguments must be passed as key => value pairs') if @_ % 2;
343 if (defined delete $args{run} or $run) {
344 require Perl::Destruct::Level;
345 Perl::Destruct::Level::set_destruct_level(3);
347 my $oldfh = select STDOUT;
355 my $file = delete $args{file};
356 unless (defined $file) {
357 my ($next, $last_pm);
358 for (my $l = 0; 1; ++$l) {
359 $next = (caller $l)[1];
360 last unless defined $next;
361 next if $next =~ /^\s*\(\s*eval\s*\d*\s*\)\s*$/;
362 if ($next =~ /\.pmc?$/) {
369 $file = $last_pm unless defined $file;
372 unless (defined $file) {
373 require Test::Builder;
374 Test::Builder->new->diag('Couldn\'t find a valid source file');
379 exit $class->analyse(
386 my $tmp = File::Temp->new;
388 require Filter::Util::Call;
389 Filter::Util::Call::filter_add(sub {
390 my $status = Filter::Util::Call::filter_read();
393 } elsif ($status == 0) {
395 my $code = $class->analyse(
396 file => $tmp->filename,
409 When set to true, all dynamic extensions that were loaded during the analysis will be unloaded at C<END> time by L<DynaLoader/dl_unload_file>.
411 Since this obfuscates error stack traces, it's disabled by default.
418 if ($dl_unload and $run and eval { require DynaLoader; 1 }) {
420 DynaLoader::dl_unload_file($_) or push @rest, $_ for @DynaLoader::dl_librefs;
421 @DynaLoader::dl_librefs = @rest;
427 Perl 5.8 is notorious for leaking like there's no tomorrow, so the suppressions are very likely not to be complete on it.
428 You also have a better chance to get more accurate results if your perl is built with debugging enabled.
429 Using the latest C<valgrind> available will also help.
431 This module is not really secure.
432 It's definitely not taint safe.
433 That shouldn't be a problem for test files.
435 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.
439 L<XML::Twig>, L<version>, L<File::HomeDir>, L<Env::Sanctify>, L<Perl::Destruct::Level>.
443 All the C<Test::Valgrind::*> API, including L<Test::Valgrind::Command>, L<Test::Valgrind::Tool>, L<Test::Valgrind::Action> and L<Test::Valgrind::Session>.
445 The C<valgrind(1)> man page.
449 L<Devel::Leak>, L<Devel::LeakTrace>, L<Devel::LeakTrace::Fast>.
453 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
455 You can contact me by mail or on C<irc.perl.org> (vincent).
459 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>.
460 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
464 You can find documentation for this module with the perldoc command.
466 perldoc Test::Valgrind
468 =head1 ACKNOWLEDGEMENTS
470 RafaE<euml>l Garcia-Suarez, for writing and instructing me about the existence of L<Perl::Destruct::Level> (Elizabeth Mattijsen is a close second).
472 H.Merijn Brand, for daring to test this thing.
474 David Cantrell, for providing shell access to one of his smokers where the tests were failing.
476 The Debian-perl team, for offering all the feedback they could regarding the build issues they met.
478 All you people that showed interest in this module, which motivated me into completely rewriting it.
480 =head1 COPYRIGHT & LICENSE
482 Copyright 2008,2009,2010,2011,2013,2015 Vincent Pit, all rights reserved.
484 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
488 1; # End of Test::Valgrind