Test::Valgrind(3pm)
π Test::Valgrind π
okay bestie, so this is a User Contributed Perl Documentation page for the module called Test::Valgrind, and honestly? it's giving "let me run your code through valgrind and tell you every messy little thing it did." iconic. no cap.
β¨ NAME β¨
Test::Valgrind β generate suppressions, analyse and test any command with valgrind.
like, any command. we're not playing around here π
πΈ VERSION πΈ
Version 1.19
π SYNOPSIS π
# From the command-line
perl -MTest::Valgrind leaky.pl
# From the command-line, snippet style
perl -MTest::Valgrind -e 'leaky()'
# In a test file
use Test::More;
eval 'use Test::Valgrind';
plan skip_all => 'Test::Valgrind is required to test your distribution with valgrind' if $@;
leaky();
# In all the test files of a directory
prove --exec 'perl -Iblib/lib -Iblib/arch -MTest::Valgrind' t/*.t
π¦ DESCRIPTION π¦
so basically, this module is a front-end to the Test::Valgrind::* API, and what it does is let you run Perl code through the memcheck tool of the valgrind memory debugger, so you can test for memory errors and leaks. π«Ά if the suppressions aren't available yet, it will first generate them for the current perl interpreter and store them in the portable flavour of ~/.perl/Test-Valgrind/suppressions/$VERSION. then the actual run happens, and tests get passed or failed according to what the analysis says. simple as that. β¨
the complete API is so much more versatile than this, though β highkey. by declaring an appropriate Test::Valgrind::Command class, you can run any executable (yes, not only Perl scripts) under valgrind, generate the corresponding suppressions on-the-fly, and convert the analysis result to TAP output so it can be incorporated into your project's testsuite. if TAP isn't your vibe, you can output the results in whatever format you want (HTML pages, hello π) by defining your own Test::Valgrind::Action class.
now, due to the nature of perl's memory allocator, this module can't track leaks of Perl objects. that includes non-mortalized scalars and memory cycles. π but it can track leaks of chunks of memory allocated in XS extensions with Newx and friends or malloc. so it's complementary to the other very good leak detectors listed down in SEE ALSO. teamwork makes the dream work fr fr π€
π METHODS π
analyse
Test::Valgrind->analyse(%options);
run a valgrind analysis configured by %options :
command => $commandβ theTest::Valgrind::Commandobject (or class name) to use.
defaults to Test::Valgrind::Command::PerlScript.
tool => $toolβ theTest::Valgrind::Toolobject (or class name) to use.
defaults to Test::Valgrind::Tool::memcheck.
action => $actionβ theTest::Valgrind::Actionobject (or class name) to use.
defaults to Test::Valgrind::Action::Test.
file => $fileβ the file name of the script to analyse.
ignored if you supply your own custom command, but mandatory otherwise. no file, no run, bestie π«
callers => $numberβ specify the maximum stack depth studied when valgrind encounters an error. raising this number improves granularity. π
ignored if you supply your own custom tool, otherwise defaults to 24 (the maximum allowed by valgrind).
diag => $boolβ if true, print the output of the test script as diagnostics.
ignored if you supply your own custom action, otherwise defaults to false.
regen_def_supp => $boolβ if true, forcefully regenerate the default suppression file.
defaults to false.
no_def_supp => $boolβ if true, do not use the default suppression file.
defaults to false.
allow_no_supp => $boolβ if true, force running the analysis even if the suppression files do not refer to anyperl-related symbol.
defaults to false.
extra_supps => \@filesβ also use suppressions from @files besidesperl's.
defaults to empty.
import
use Test::Valgrind %options;
in the parent process, import calls analyse with the arguments it received itself β except that if no file option was supplied, it tries to pick the first caller context that looks like a script. π when the analysis ends, it exits with the status returned by the action (for the default TAP-generator action, that's the number of failed tests).
in the child process, it just returns, so that the calling code actually gets run under valgrind β albeit with two side-effects π«Ά:
Perl::Destruct::Levelis loaded and the destruction level is set to3.- Autoflush on
STDOUTis turned on.
β VARIABLES β
$dl_unload
when set to true, all dynamic extensions that were loaded during the analysis will be unloaded at END time by dl_unload_file in DynaLoader.
since this obfuscates error stack traces, it's disabled by default. π
π CAVEATS π
Perl 5.8 is notorious for leaking like there's no tomorrow, so the suppressions are very likely not to be complete on it. you also have a better chance to get more accurate results if your perl is built with debugging enabled. using the latest valgrind available will also help. slay, but like, accurately. π
this module is not really secure. it's definitely not taint safe. that shouldn't be a problem for test files though. π§
what your tests output toSTDOUTandSTDERRis eaten unless you pass thediagoption, in which case it will be reprinted as diagnostics. gone. devoured. π
π DEPENDENCIES π
XML::Twig, File::HomeDir, Env::Sanctify, Perl::Destruct::Level.
π₯ SEE ALSO π₯
all the Test::Valgrind::* API, including Test::Valgrind::Command, Test::Valgrind::Tool, Test::Valgrind::Action and Test::Valgrind::Session.
the valgrind(1) man page.
Test::LeakTrace.
Devel::Leak, Devel::LeakTrace, Devel::LeakTrace::Fast.
π AUTHOR π
Vincent Pit, "<perl at profvince.com>", <http://www.profvince.com>.
you can contact me by mail or on irc.perl.org (vincent). say hi bestie π«Ά
π BUGS π
please report any bugs or feature requests to "bug-test-valgrind at rt.cpan.org", or through the web interface at <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. π
π SUPPORT π
you can find documentation for this module with the perldoc command.
perldoc Test::Valgrind
πΈ ACKNOWLEDGEMENTS πΈ
RafaΓ«l Garcia-Suarez, for writing and instructing me about the existence of Perl::Destruct::Level (Elizabeth Mattijsen is a close second). π₯
H.Merijn Brand, for daring to test this thing. brave. iconic. π«‘
David Cantrell, for providing shell access to one of his smokers where the tests were failing. π»
the Debian-perl team, for offering all the feedback they could regarding the build issues they met.
all you people that showed interest in this module, which motivated me into completely rewriting it. that's you. yes you. π«Άβ¨
β COPYRIGHT & LICENSE β
Copyright 2008,2009,2010,2011,2013,2015,2016 Vincent Pit, all rights reserved.
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page bestie, mwah π
β¨ this lil manual page comes to you as part of perl v5.40.1, dated 2025-04-13, filed under Test::Valgrind(3pm). go forth and leak-check responsibly π¦π
π¦ where this page comes from
- debian trixie: libtest-valgrind-perl 1.19-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtest-valgrind-perl 1.19-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtest-valgrind-perl 1.19-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms