/usr/lib/perl5/Test/LeakTrace.pm is in libtest-leaktrace-perl 0.14-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | package Test::LeakTrace;
use 5.008_001;
use strict;
use warnings;
our $VERSION = '0.14';
use XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
use Test::Builder::Module;
our @ISA = qw(Test::Builder::Module);
use Exporter qw(import); # use Exporter::import for backward compatibility
our @EXPORT = qw(
leaktrace leaked_refs leaked_info leaked_count
no_leaks_ok leaks_cmp_ok
count_sv
);
our %EXPORT_TAGS = (
all => \@EXPORT,
test => [qw(no_leaks_ok leaks_cmp_ok)],
util => [qw(leaktrace leaked_refs leaked_info leaked_count count_sv)],
);
sub _do_leaktrace{
my($block, $name, $need_stateinfo, $mode) = @_;
if(!defined($mode) && !defined wantarray){
warnings::warnif void => "Useless use of $name() in void context";
}
if($name eq 'leaked_count') {
my $start;
$start = count_sv();
$block->();
return count_sv() - $start;
}
local $SIG{__DIE__} = 'DEFAULT';
_start($need_stateinfo);
eval{
$block->();
};
if($@){
_finish(-silent);
die $@;
}
return _finish($mode);
}
sub leaked_refs(&){
my($block) = @_;
return _do_leaktrace($block, 'leaked_refs', 0);
}
sub leaked_info(&){
my($block) = @_;
return _do_leaktrace($block, 'leaked_refs', 1);
}
sub leaked_count(&){
my($block) = @_;
return scalar _do_leaktrace($block, 'leaked_count', 0);
}
sub leaktrace(&;$){
my($block, $mode) = @_;
_do_leaktrace($block, 'leaktrace', 1, defined($mode) ? $mode : -simple);
return;
}
sub leaks_cmp_ok(&$$;$){
my($block, $cmp_op, $expected, $description) = @_;
my $Test = __PACKAGE__->builder;
if(!_runops_installed()){
my $mod = exists $INC{'Devel/Cover.pm'} ? 'Devel::Cover' : 'strange runops routines';
return $Test->ok(1, "skipped (under $mod)");
}
# calls to prepare cache in $block
$block->();
my $got = _do_leaktrace($block, 'leaked_count', 0);
my $desc = sprintf 'leaks %s %-2s %s', $got, $cmp_op, $expected;
if(defined $description){
$description .= " ($desc)";
}
else{
$description = $desc;
}
my $result = $Test->cmp_ok($got, $cmp_op, $expected, $description);
if(!$result){
open local(*STDERR), '>', \(my $content = '');
$block->(); # calls it again because opening *STDERR changes the run-time environment
_do_leaktrace($block, 'leaktrace', 1, -verbose);
$Test->diag($content);
}
return $result;
}
sub no_leaks_ok(&;$){
# ($block, $description)
splice @_, 1, 0, ('<=', 0); # ($block, '<=', 0, $description);
goto &leaks_cmp_ok;
}
1;
__END__
=for stopwords sv gfx
=head1 NAME
Test::LeakTrace - Traces memory leaks
=head1 VERSION
This document describes Test::LeakTrace version 0.14.
=head1 SYNOPSIS
use Test::LeakTrace;
# simple report
leaktrace{
# ...
};
# verbose output
leaktrace{
# ...
} -verbose;
# with callback
leaktrace{
# ...
} sub {
my($ref, $file, $line) = @_;
warn "leaked $ref from $file line\n";
};
my @refs = leaked_refs{
# ...
};
my @info = leaked_info{
# ...
};
my $count = leaked_count{
# ...
};
# standard test interface
use Test::LeakTrace;
no_leaks_ok{
# ...
} 'no memory leaks';
leaks_cmp_ok{
# ...
} '<', 10;
=head1 DESCRIPTION
C<Test::LeakTrace> provides several functions that trace memory leaks.
This module scans arenas, the memory allocation system,
so it can detect any leaked SVs in given blocks.
B<Leaked SVs> are SVs which are not released after the end of the scope
they have been created. These SVs include global variables and internal caches.
For example, if you call a method in a tracing block, perl might prepare a cache
for the method. Thus, to trace true leaks, C<no_leaks_ok()> and C<leaks_cmp_ok()>
executes a block more than once.
=head1 INTERFACE
=head2 Exported functions
=head3 C<< leaked_info { BLOCK } >>
Executes I<BLOCK> and returns a list of leaked SVs and places where the SVs
come from, i.e. C<< [$ref, $file, $line] >>.
=head3 C<< leaked_refs { BLOCK } >>
Executes I<BLOCK> and returns a list of leaked SVs.
=head3 C<< leaked_count { BLOCK } >>
Executes I<BLOCK> and returns the number of leaked SVs.
=head3 C<< leaktrace { BLOCK } ?($mode | \&callback) >>
Executes I<BLOCK> and reports leaked SVs to C<*STDERR>.
Defined I<$mode>s are:
=over 4
=item -simple
Default. Reports the leaked SV identity (type and address), file name and line number.
=item -sv_dump
In addition to B<-simple>, dumps the sv content using C<sv_dump()>,
which also implements C<Devel::Peek::Dump()>.
=item -lines
In addition to B<-simple>, prints suspicious source lines.
=item -verbose
Both B<-sv_dump> and B<-lines>.
=back
=head3 C<< no_leaks_ok { BLOCK } ?$description >>
Tests that I<BLOCK> does not leaks SVs. This is a test function
using C<Test::Builder>.
Note that I<BLOCK> is called more than once. This is because
I<BLOCK> might prepare caches which are not memory leaks.
=head3 C<< leaks_cmp_ok { BLOCK } $cmp_op, $number, ?$description >>
Tests that I<BLOCK> leaks a specific number of SVs. This is a test
function using C<Test::Builder>.
Note that I<BLOCK> is called more than once. This is because
I<BLOCK> might prepare caches which are not memory leaks.
=head3 C<< count_sv() >>
Counts all the SVs in the arena.
=head2 Script interface
Like C<Devel::LeakTrace> C<Test::LeakTrace::Script> is provided for whole scripts.
The arguments of C<use Test::LeakTrace::Script> directive is the same as C<leaktrace()>.
$ TEST_LEAKTRACE=-sv_dump perl -MTest::LeakTrace::Script script.pl
$ perl -MTest::LeakTrace::Script=-verbose script.pl
#!perl
# ...
use Test::LeakTrace::Script sub{
my($ref, $file, $line) = @_;
# ...
};
# ...
=head1 EXAMPLES
=head2 Testing modules
Here is a test script template that checks memory leaks.
#!perl -w
use strict;
use constant HAS_LEAKTRACE => eval{ require Test::LeakTrace };
use Test::More HAS_LEAKTRACE ? (tests => 1) : (skip_all => 'require Test::LeakTrace');
use Test::LeakTrace;
use Some::Module;
leaks_cmp_ok{
my $o = Some::Module->new();
$o->something();
$o->something_else();
} '<', 1;
=head1 DEPENDENCIES
Perl 5.8.1 or later, and a C compiler.
=head1 CAVEATS
C<Test::LeakTrace> does not work with C<Devel::Cover> and modules which install
their own C<runops> routines, or the perl executor. So if the test functions of
this module detect strange C<runops> routines, they do nothing and report okay.
=head1 BUGS
No bugs have been reported.
Please report any bugs or feature requests to the author.
=head1 SEE ALSO
L<Devel::LeakTrace>.
L<Devel::LeakTrace::Fast>.
L<Test::TraceObject>.
L<Test::Weak>.
For guts:
L<perlguts>.
L<perlhack>.
F<sv.c>.
=head1 AUTHOR
Goro Fuji(gfx) E<lt>gfuji(at)cpan.orgE<gt>.
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2009-2010, Goro Fuji(gfx). All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|