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 β€” the Test::Valgrind::Command object (or class name) to use.

defaults to Test::Valgrind::Command::PerlScript.

  • tool => $tool β€” the Test::Valgrind::Tool object (or class name) to use.

defaults to Test::Valgrind::Tool::memcheck.

  • action => $action β€” the Test::Valgrind::Action object (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 any perl-related symbol.

defaults to false.

  • extra_supps => \@files β€” also use suppressions from @files besides perl'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::Level is loaded and the destruction level is set to 3.
  • Autoflush on STDOUT is 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 to STDOUT and STDERR is eaten unless you pass the diag option, 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 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