blib/lib/Devel/MAT/Dumpfile.pm

filter: Statementstmt 84.7% 223 / 263 Branchbran 65.4% 89 / 136 Conditioncond 57.6% 15 / 26 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 80.3% 49 / 61 Podpod 94.4% 17 / 18 totaltotal 76.5% 398 / 520 CC 86 SCAR 25.8
CC
86
Cov
77%
load
36.7
_read_sv
27.2
_fixup
12.8
CRAP 13.2
« blib/lib/Devel/MAT/ToolBase/GraphWalker.pm ↑ Summary blib/lib/Devel/MAT/Cmd/Terminal.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2026 -- leonerd@leonerd.org.uk
5  
6 package Devel::MAT::Dumpfile 0.56;
7  
8 10 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 10 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use IO::Handle; # ->read
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use IO::Seekable; # ->tell
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use List::Util qw( pairmap );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 ▶use Devel::MAT::SV;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Devel::MAT::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Struct::Dumb 0.07 qw( readonly_struct );
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 readonly_struct StructType => [qw( name fields )];
25 10 readonly_struct StructField => [qw( name type )];
26  
27 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 10 PMAT_SVxMAGIC => 0x80,
29 10 };
30  
31 =head1 NAME
32  
33 C<Devel::MAT::Dumpfile> - load and analyse a heap dump file
34  
35 =head1 SYNOPSIS
36  
37 =for highlighter language=perl
38  
39 use Devel::MAT::Dumpfile;
40  
41 my $df = Devel::MAT::Dumpfile->load( "path/to/the/file.pmat" );
42  
43 =head1 DESCRIPTION
44  
45 This module provides a class that loads a heap dump file previously written by
46 L<Devel::MAT::Dumper>. It provides accessor methods to obtain various
47 well-known root starting addresses, or to find arbitrary SVs by address. Each
48 SV is represented by an instance of L<Devel::MAT::SV>.
49  
50 =cut
51  
52 10 my @ROOTS;
53 my %ROOTDESC;
54 10 foreach (
55 [ sv_undef => "+the undef SV" ],
56 [ sv_yes => "+the true SV" ],
57 [ sv_no => "+the false SV" ],
58 [ main_cv => "+the main code" ],
59 [ defstash => "+the default stash" ],
60 [ mainstack => "+the main stack AV" ],
61 [ beginav => "+the BEGIN list" ],
62 [ checkav => "+the CHECK list" ],
63 [ unitcheckav => "+the UNITCHECK list" ],
64 [ initav => "+the INIT list" ],
65 [ endav => "+the END list" ],
66 [ strtab => "+the shared string table HV" ],
67 [ envgv => "-the ENV GV" ],
68 [ incgv => "+the INC GV" ],
69 [ statgv => "+the stat GV" ],
70 [ statname => "+the statname SV" ],
71 [ tmpsv => "-the temporary SV" ],
72 [ defgv => "+the default GV" ],
73 [ argvgv => "-the ARGV GV" ],
74 [ argvoutgv => "+the argvout GV" ],
75 [ argvout_stack => "+the argvout stack AV" ],
76 [ errgv => "+the *@ GV" ],
77 [ fdpidav => "+the FD-to-PID mapping AV" ],
78 [ preambleav => "+the compiler preamble AV" ],
79 [ modglobalhv => "+the module data globals HV" ],
80 [ regex_padav => "+the REGEXP pad AV" ],
81 [ sortstash => "+the sort stash" ],
82 [ firstgv => "-the *a GV" ],
83 [ secondgv => "-the *b GV" ],
84 [ debstash => "-the debugger stash" ],
85 [ stashcache => "+the stash cache" ],
86 [ isarev => "+the reverse map of \@ISA dependencies" ],
87 [ registered_mros => "+the registered MROs HV" ],
88 [ rs => "+the IRS" ],
89 [ last_in_gv => "+the last input GV" ],
90 [ ofsgv => "+the OFS GV" ],
91 [ defoutgv => "+the default output GV" ],
92 [ hintgv => "-the hints (%^H) GV" ],
93 [ patchlevel => "+the patch level" ],
94 [ apiversion => "+the API version" ],
95 [ e_script => "+the '-e' script" ],
96 [ mess_sv => "+the message SV" ],
97 [ ors_sv => "+the ORS SV" ],
98 [ encoding => "+the encoding" ],
99 [ blockhooks => "+the block hooks" ],
100 [ custom_ops => "+the custom ops HV" ],
101 [ custom_op_names => "+the custom op names HV" ],
102 [ custom_op_descs => "+the custom op descriptions HV" ],
103 220 map { [ $_ => "+the $_" ] } qw(
104 Latin1 UpperLatin1 AboveLatin1 NonL1NonFinalFold HasMultiCharFold
105 utf8_mark utf8_X_regular_begin utf8_X_extend utf8_toupper utf8_totitle
106 utf8_tolower utf8_tofold utf8_charname_begin utf8_charname_continue
107 utf8_idstart utf8_idcont utf8_xidstart utf8_perl_idstart utf8_perl_idcont
108 utf8_xidcont utf8_foldclosures utf8_foldable ),
109 ) {
110 700 my ( $name, $desc ) = @$_;
111 700 push @ROOTS, $name;
112 700 $ROOTDESC{$name} = $desc;
113  
114 # Autogenerate the accessors
115 45 ▶ my $code = sub ( $self ) {
Subroutine __ANON__: called CC=2 SCAR=7.6
116 45 ▶ $self->{roots}{$name} ? $self->sv_at( $self->{roots}{$name}[0] ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'roots'}{$name} ? : 45 0 45
117 700 };
118 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
119 700 *$name = $code;
120 }
121  
122 0 ▶*ROOTS = sub ( $self ) { @ROOTS };
Subroutine __ANON__: not called CC=1 SCAR=1.2
123  
124 =head1 CONSTRUCTOR
125  
126 =cut
127  
128 =head2 load
129  
130 $df = Devel::MAT::Dumpfile->load( $path, %args );
131  
132 Loads a heap dump file from the given path, and returns a new
133 C<Devel::MAT::Dumpfile> instance representing it.
134  
135 Takes the following named arguments:
136  
137 =over 8
138  
139 =item progress => CODE
140  
141 If given, should be a CODE reference to a function that will be called
142 regularly during the loading process, and given a status message to update the
143 user.
144  
145 =back
146  
147 =cut
148  
149 8 ▶sub load ( $class, $path, %args )
Subroutine load: called CC=32 SCAR=36.0
Pod documented
150 {
151 8 my $progress = $args{progress};
152  
153 8 ▶ $progress->( "Loading file $path..." ) if $progress;
Branch: 1 branch
BranchTrueFalseTotal
if $progress 0 8 8
154  
155 8 ▶ open my $fh, "<", $path or croak "Cannot read $path - $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or croak("Cannot read $path - $!") 8 0 8
156 8 my $self = bless { fh => $fh }, $class;
157  
158 8 my $filelen = -s $fh;
159  
160 # Header
161 8 ▶ $self->_read(4) eq "PMAT" or croak "File magic signature not found";
Branch: 1 branch
BranchTrueFalseTotal
$self->_read(4) eq "PMAT" or croak("File magic signature not found") 8 0 8
162  
163 8 my $flags = $self->_read_u8;
164  
165 8 ▶ my $endian = ( $self->{big_endian} = $flags & 0x01 ) ? ">" : "<";
Branch: 1 branch
BranchTrueFalseTotal
($self->{'big_endian'} = $flags & 1) ? : 0 8 8
166  
167 8 my $u32_fmt = $self->{u32_fmt} = "L$endian";
168 8 my $u64_fmt = $self->{u64_fmt} = "Q$endian";
169  
170 8 ▶ @{$self}{qw( uint_len uint_fmt )} =
Branch: 1 branch
BranchTrueFalseTotal
$flags & 2 ? : 8 0 8
171 ( $flags & 0x02 ) ? ( 8, $u64_fmt ) : ( 4, $u32_fmt );
172  
173 8 ▶ @{$self}{qw( ptr_len ptr_fmt )} =
Branch: 1 branch
BranchTrueFalseTotal
$flags & 4 ? : 8 0 8
174 ( $flags & 0x04 ) ? ( 8, $u64_fmt ) : ( 4, $u32_fmt );
175  
176 8 ▶ @{$self}{qw( nv_len nv_fmt )} =
Branch: 1 branch
BranchTrueFalseTotal
$flags & 8 ? : 0 8 8
177 ( $flags & 0x08 ) ? ( 10, "D$endian" ) : ( 8, "d$endian" );
178  
179 8 $self->{ithreads} = !!( $flags & 0x10 );
180  
181 8 $flags &= ~0x1f;
182 8 ▶ die sprintf "Cannot read %s - unrecognised flags %x\n", $path, $flags if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 0 8 8
183  
184 8 $self->{minus_1} = unpack $self->{uint_fmt}, pack $self->{uint_fmt}, -1;
185  
186 8 ▶ $self->_read_u8 == 0 or die "Cannot read $path - 'zero' header field is not zero";
Branch: 1 branch
BranchTrueFalseTotal
$self->_read_u8 == 0 or die "Cannot read $path - 'zero' header field is not zero" 8 0 8
187  
188 8 ▶ $self->_read_u8 == 0 or die "Cannot read $path - format version major unrecognised";
Branch: 1 branch
BranchTrueFalseTotal
$self->_read_u8 == 0 or die "Cannot read $path - format version major unrecognised" 8 0 8
189  
190 8 ▶ ( $self->{format_minor} = $self->_read_u8 ) <= 6 or
Branch: 1 branch
BranchTrueFalseTotal
($self->{"format_minor"} = $self->_read_u8) <= 6 or die qq[Cannot read $path - format version minor unrecognised ($self->{"format_minor"})] 8 0 8
191 die "Cannot read $path - format version minor unrecognised ($self->{format_minor})";
192  
193 8 ▶ if( $self->{format_minor} < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format_minor"} < 1) 0 8 8
194 0 warn "Loading an earlier format of dumpfile - SV MAGIC annotations may be incorrect\n";
195 }
196  
197 8 $self->{perlver} = $self->_read_u32;
198  
199 8 my $n_types = $self->_read_u8;
200 8 my @sv_sizes = unpack "(a3)*", my $tmp = $self->_read( $n_types * 3 );
201 8 $self->{sv_sizes} = [ map [ unpack "C C C", $_ ], @sv_sizes ];
202  
203 8 ▶ if( $self->{format_minor} >= 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'format_minor'} >= 4) { } 8 0 8
204 8 my $n_extns = $self->_read_u8;
205 8 my @extn_sizes = unpack "(a3)*", my $tmp = $self->_read( $n_extns * 3 );
206 8 $self->{svx_sizes} = [ map [ unpack "C C C", $_ ], @extn_sizes ];
207 }
208 else {
209 # versions < 4 had just one, PMAT_SVxMAGIC
210 $self->{svx_sizes} = [
211 0 [ 2, 2, 0 ], # PMAT_SVxMAGIC
212 ];
213 }
214  
215 8 ▶ if( $self->{format_minor} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format_minor"} >= 2) 8 0 8
216 8 my $n_ctxs = $self->_read_u8;
217 8 my @ctx_sizes = unpack "(a3)*", my $tmp = $self->_read( $n_ctxs * 3 );
218 8 $self->{ctx_sizes} = [ map [ unpack "C C C", $_ ], @ctx_sizes ];
219 }
220  
221 8 $self->{structtypes_by_id} = {};
222  
223 # Roots
224 8 foreach (qw( undef yes no )) {
225 24 my $addr = $self->{"${_}_at"} = $self->_read_ptr;
226 24 my $class = "Devel::MAT::SV::\U$_";
227 24 $self->{uc $_} = $class->new( $self, $addr );
228 }
229  
230 8 $self->{roots} = \my %roots;
231 # The three immortals
232 8 $roots{"sv_$_"} = [ $self->{"\U$_"}->addr, $ROOTDESC{"sv_$_"} ] for qw( undef yes no );
233  
234 8 foreach ( 1 .. $self->_read_u32 ) {
235 488 my $name = $self->_read_str;
236 488 ▶ my $desc = $ROOTDESC{$name} // $name;
Condition66%2/3
$ROOTDESC{$name} // $name
l!l&&r!l&&!r
48080
MC/DC0%0/2
$ROOTDESC{$name} // $name
$ROOTDESC{$name} $name
Truth table66%2/3
A // B
A= $ROOTDESC{$name}B= $name
ABresult
1X1
011
000
237 488 ▶ $desc =~ m/^[+-]/ or $desc = "+$desc";
Branch: 1 branch
BranchTrueFalseTotal
$desc =~ /^[+-]/u or $desc = "+$desc" 480 8 488
238 488 $roots{$name} = [ $self->_read_ptr, $desc ];
239 }
240  
241 # Stack
242 8 my $stacksize = $self->_read_uint;
243 8 $self->{stack_at} = [ map { $self->_read_ptr } 1 .. $stacksize ];
244  
245 # Heap
246 8 $self->{heap} = \my %heap;
247 8 $self->{protosubs_by_oproot} = \my %protosubs_by_oproot;
248 8 ▶ while( my $sv = $self->_read_sv ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sv = $self->_read_sv) 983828 8 983836
249 983828 $heap{$sv->addr} = $sv;
250  
251 # Also identify the protosub of every oproot
252 983828 ▶ if( $sv->type eq "CODE" and $sv->oproot and $sv->is_clone ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->type eq "CODE" and $sv->oproot and $sv->is_clone) 1934 981894 983828
Condition100%6/6
$sv->type eq "CODE" and $sv->oproot and $sv->is_clone
!ll&&!rl&&r
943978379161934
$sv->type eq "CODE" and $sv->oproot
!ll&&!rl&&r
9154242855439850
MC/DC100%3/3
$sv->type eq "CODE" and $sv->oproot and $sv->is_clone
$sv->type eq "CODE" $sv->oproot $sv->is_clone
Truth table100%4/4
A and B and C
A= $sv->type eq "CODE"B= $sv->oprootC= $sv->is_clone
ABCresult
0XX0
10X0
1100
1111
253 1934 $protosubs_by_oproot{$sv->oproot} = $sv;
254 }
255  
256 983828 my $pos = $fh->IO::Seekable::tell; # fully-qualified method for 5.010
257 983828 ▶ $progress->( sprintf "Loading file %d of %d bytes (%.2f%%)",
Branch: 1 branch
BranchTrueFalseTotal
if $progress and keys(%heap) % 5000 == 0 0 983828 983828
Condition33%1/3
$progress and keys(%heap) % 5000 == 0
!ll&&!rl&&r
98382800
MC/DC0%0/2
$progress and keys(%heap) % 5000 == 0
$progress keys(%heap) % 5000 == 0
Truth table33%1/3
A and B
A= $progressB= keys(%heap) % 5000 == 0
ABresult
0X0
100
111
258 $pos, $filelen, 100*$pos / $filelen ) if $progress and (keys(%heap) % 5000) == 0;
259 }
260  
261 # Contexts
262 8 $self->{contexts} = \my @contexts;
263 8 ▶ while( my $ctx = $self->_read_ctx ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = $self->_read_ctx) 5 8 13
264 5 push @contexts, $ctx;
265 }
266  
267 # From here onwards newer files have mortals, older ones don't
268 8 ▶ if( my $mortalcount = $self->_read_uint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mortalcount = $self->_read_uint) 8 0 8
269 8 $self->{mortals_at} = \my @mortals_at;
270 8 push @mortals_at, $self->_read_ptr for 1 .. $mortalcount;
271 8 foreach my $addr ( @mortals_at ) {
272 68 my $sv = $self->sv_at( $addr );
273 68 ▶ unless( $sv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sv) 65 3 68
274 65 warn sprintf "SV address 0x%x is marked mortal but there is no SV", $addr;
275 65 next;
276 }
277 3 $sv->_set_is_mortal;
278 }
279 8 $self->{mortal_floor} = $self->_read_uint;
280 }
281  
282 8 ▶ $self->_fixup( %args ) unless $args{no_fixup};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_fixup"} 8 0 8
283  
284 8 return $self;
285 }
286  
287 0 ▶sub structtype ( $self, $id )
Subroutine structtype: not called CC=2 SCAR=17.9
Pod undocumented
288 {
289 0 ▶ return $self->{structtypes_by_id}{$id} //
Condition0%0/2
$self->{"structtypes_by_id"}{$id} // croak("Dumpfile does not define a struct type of ID=$id\n")
l!l
00
MC/DC0%0/1
$self->{"structtypes_by_id"}{$id} // croak("Dumpfile does not define a struct type of ID=$id\n")
$self->{"structtypes_by_id"}{$id}
Truth table0%0/2
A
A= $self->{"structtypes_by_id"}{$id}
Aresult
11
00
290 croak "Dumpfile does not define a struct type of ID=$id\n";
291 }
292  
293 8 ▶sub _fixup ( $self, %args )
Subroutine _fixup: called CC=12 SCAR=25.5
294 {
295 8 my $progress = $args{progress};
296  
297 8 my $heap = $self->{heap};
298  
299 8 my $heap_total = scalar keys %$heap;
300  
301 # Annotate each root SV
302 8 foreach my $name ( keys $self->{roots}->%* ) {
303 512 ▶ my $sv = $self->root( $name ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $self->root($name) or next 345 167 512
304 345 $sv->{rootname} = $name;
305 }
306  
307 8 my $count = 0;
308 8 ▶ while( my ( $addr ) = each %$heap ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($addr) = each %$heap) 983828 8 983836
309 983828 ▶ my $sv = $heap->{$addr} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $heap->{$addr} or next 983828 0 983828
310  
311 # While dumping we weren't able to determine what ARRAYs were really
312 # PADLISTs. Now we can fix them up
313 983828 ▶ $sv->_fixup if $sv->can( "_fixup" );
Branch: 1 branch
BranchTrueFalseTotal
if $sv->can("_fixup") 169966 813862 983828
314  
315 983828 $count++;
316 983828 ▶ $progress->( sprintf "Fixing %d of %d (%.2f%%)",
Branch: 1 branch
BranchTrueFalseTotal
if $progress and $count % 20000 == 0 0 983828 983828
Condition33%1/3
$progress and $count % 20000 == 0
!ll&&!rl&&r
98382800
MC/DC0%0/2
$progress and $count % 20000 == 0
$progress $count % 20000 == 0
Truth table33%1/3
A and B
A= $progressB= $count % 20000 == 0
ABresult
0X0
100
111
317 $count, $heap_total, 100*$count / $heap_total ) if $progress and ($count % 20000) == 0;
318 }
319  
320 # Walk the SUB contexts setting their true depth
321 8 ▶ if( $self->{format_minor} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"format_minor"} >= 2) 8 0 8
322 8 my %prev_depth_by_cvaddr;
323  
324 8 foreach my $ctx ( $self->{contexts}->@* ) {
325 5 ▶ next unless $ctx->type eq "SUB";
Branch: 1 branch
BranchTrueFalseTotal
unless $ctx->type eq "SUB" 2 3 5
326  
327 3 my $cvaddr = $ctx->{cv_at};
328 3 ▶ $ctx->_set_depth( $prev_depth_by_cvaddr{$cvaddr} // $ctx->cv->depth );
Condition33%1/3
$prev_depth_by_cvaddr{$cvaddr} // $ctx->cv->depth
l!l&&r!l&&!r
030
MC/DC0%0/2
$prev_depth_by_cvaddr{$cvaddr} // $ctx->cv->depth
$prev_depth_by_cvaddr{$cvaddr} $ctx->cv->depth
Truth table33%1/3
A // B
A= $prev_depth_by_cvaddr{$cvaddr}B= $ctx->cv->depth
ABresult
1X1
011
000
329  
330 3 $prev_depth_by_cvaddr{$cvaddr} = $ctx->olddepth;
331 }
332 }
333  
334 8 return $self;
335 }
336  
337 # Nicer interface to IO::Handle
338 2699712 ▶sub _read ( $self, $len )
Subroutine _read: called CC=3 SCAR=11.1
339 {
340 2699712 ▶ return "" if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 6844 2692868 2699712
341 2692868 ▶ defined( $self->{fh}->read( my $buf, $len ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, $len) or croak("Cannot read - $!") 2692868 0 2692868
342 2692868 return $buf;
343 }
344  
345 1978871 ▶sub _read_u8 ( $self )
Subroutine _read_u8: called CC=2 SCAR=7.2
346 {
347 1978871 ▶ defined( $self->{fh}->read( my $buf, 1 ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, 1) or croak("Cannot read - $!") 1978871 0 1978871
348 1978871 return unpack "C", $buf;
349 }
350  
351 16 ▶sub _read_u32 ( $self )
Subroutine _read_u32: called CC=2 SCAR=7.2
352 {
353 16 ▶ defined( $self->{fh}->read( my $buf, 4 ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, 4) or croak("Cannot read - $!") 16 0 16
354 16 return unpack $self->{u32_fmt}, $buf;
355 }
356  
357 0 ▶sub _read_u64 ( $self )
Subroutine _read_u64: not called CC=2 SCAR=17.9
358 {
359 0 ▶ defined( $self->{fh}->read( my $buf, 8 ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, 8) or croak("Cannot read - $!") 0 0 0
360 0 return unpack $self->{u64_fmt}, $buf;
361 }
362  
363 1701873 ▶sub _read_uint ( $self )
Subroutine _read_uint: called CC=2 SCAR=7.2
364 {
365 1701873 ▶ defined( $self->{fh}->read( my $buf, $self->{uint_len} ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, $self->{"uint_len"}) or croak("Cannot read - $!") 1701873 0 1701873
366 1701873 return unpack $self->{uint_fmt}, $buf;
367 }
368  
369 1398367 ▶sub _read_ptr ( $self )
Subroutine _read_ptr: called CC=2 SCAR=7.2
370 {
371 1398367 ▶ defined( $self->{fh}->read( my $buf, $self->{ptr_len} ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, $self->{"ptr_len"}) or croak("Cannot read - $!") 1398367 0 1398367
372 1398367 return unpack $self->{ptr_fmt}, $buf;
373 }
374  
375 1667437 ▶sub _read_ptrs ( $self, $n )
Subroutine _read_ptrs: called CC=2 SCAR=7.2
376 {
377 1667437 ▶ defined( $self->{fh}->read( my $buf, $self->{ptr_len} * $n ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, $self->{"ptr_len"} * $n) or croak("Cannot read - $!") 1667437 0 1667437
378 1667437 return unpack "$self->{ptr_fmt}$n", $buf;
379 }
380  
381 0 ▶sub _read_nv ( $self )
Subroutine _read_nv: not called CC=2 SCAR=17.9
382 {
383 0 ▶ defined( $self->{fh}->read( my $buf, $self->{nv_len} ) ) or croak "Cannot read - $!";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"fh"}->read(my $buf, $self->{"nv_len"}) or croak("Cannot read - $!") 0 0 0
384 0 return unpack $self->{nv_fmt}, $buf;
385 }
386  
387 1280911 ▶sub _read_str ( $self )
Subroutine _read_str: called CC=2 SCAR=6.9
388 {
389 1280911 my $len = $self->_read_uint;
390 1280911 ▶ return undef if $len == $self->{minus_1};
Branch: 1 branch
BranchTrueFalseTotal
if $len == $self->{"minus_1"} 412840 868071 1280911
391 868071 return $self->_read($len);
392 }
393  
394 2147650 ▶sub _read_bytesptrsstrs ( $self, $nbytes, $nptrs, $nstrs )
Subroutine _read_bytesptrsstrs: called CC=4 SCAR=13.9
395 {
396 return
397 ( $nbytes ? $self->_read( $nbytes ) : "" ),
398 ( $nptrs ? [ $self->_read_ptrs( $nptrs ) ] : undef ),
399 2147650 ▶ ( $nstrs ? [ map { $self->_read_str } 1 .. $nstrs ] : undef );
Branch: 3 branches
BranchTrueFalseTotal
$nbytes ? : 1831609 316041 2147650
$nptrs ? : 1612208 535442 2147650
$nstrs ? : 514521 1633129 2147650
400 }
401  
402 983836 ▶sub _read_sv ( $self )
Subroutine _read_sv: called CC=14 SCAR=33.0
403 {
404 983836 while(1) {
405 1163820 my $type = $self->_read_u8;
406 1163820 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 8 1163812 1163820
407  
408 1163812 ▶ if( $type >= 0xF1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type >= 241) { } 0 1163812 1163812
elsif ($type == 240) { } 0 1163812 1163812
elsif ($type >= 128) { } 179984 983828 1163812
409 0 die sprintf "Unrecognised META tag %02X\n", $type;
410 }
411 elsif( $type == 0xF0 ) {
412 # META_STRUCT
413 0 my $id = $self->_read_uint;
414 0 my $nfields = $self->_read_uint;
415 0 my $name = $self->_read_str;
416  
417 0 my @fields;
418 push @fields, StructField(
419 $self->_read_str,
420 $self->_read_u8,
421 0 ) for 1 .. $nfields;
422  
423 0 $self->{structtypes_by_id}{$id} = StructType(
424 $name, \@fields,
425 );
426  
427 0 next;
428 }
429 elsif( $type >= 0x80 ) {
430 179984 my $sizes = $self->{svx_sizes}[$type - 0x80];
431  
432 179984 ▶ if( $self->{format_minor} == 0 and $type == PMAT_SVxMAGIC ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'format_minor'} == 0 and $type == 128) { } 0 179984 179984
elsif (not $sizes) { } 0 179984 179984
Condition33%1/3
$self->{"format_minor"} == 0 and $type == 128
!ll&&!rl&&r
17998400
MC/DC0%0/2
$self->{"format_minor"} == 0 and $type == 128
$self->{"format_minor"} == 0 $type == 128
Truth table33%1/3
A and B
A= $self->{"format_minor"} == 0B= $type == 128
ABresult
0X0
100
111
433 # legacy magic support
434 0 my ( $sv_addr, $obj ) = $self->_read_ptrs(2);
435 0 my $type = chr $self->_read_u8;
436  
437 0 my $sv = $self->sv_at( $sv_addr );
438  
439 # Legacy format didn't have flags, and didn't distinguish obj from ptr
440 # However, the only objs it ever saved were refcounted ones. Lets just
441 # pretend all of them are refcounted objects.
442 0 $sv->more_magic( $type => 0x01, $obj, 0, 0 );
443 }
444 elsif( !$sizes ) {
445 0 die sprintf "Unrecognised SV extension type %02x\n", $type;
446 }
447 else {
448 179984 my $sv_addr = $self->_read_ptr;
449 179984 my @args = $self->_read_bytesptrsstrs( @$sizes );
450  
451 179984 ▶ my $sv = $self->sv_at( $sv_addr ) or
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $self->sv_at($sv_addr) or warn(sprintf("Skipping SVx 0x%02X on missing SV at addr 0x%X\n", $type, $sv_addr)), next 179984 0 179984
452 warn( sprintf "Skipping SVx 0x%02X on missing SV at addr 0x%X\n", $type, $sv_addr ), next;
453  
454 179984 ▶ my $code = $self->can( sprintf "_read_svx_%02X", $type ) or
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can(sprintf("_read_svx_%02X", $type)) or warn(sprintf("Skipping unrecognised SVx 0x%02X\n", $type)), next 179984 0 179984
455 warn( sprintf "Skipping unrecognised SVx 0x%02X\n", $type ), next;
456  
457 179984 $self->$code( $sv, @args );
458 }
459  
460 179984 next;
461 }
462  
463 # First read the "common" header
464 my $sv = Devel::MAT::SV->new( $type, $self,
465 983828 $self->_read_bytesptrsstrs( $self->{sv_sizes}[0]->@* )
466 );
467  
468 983828 ▶ if( $type == 0x7F ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 127) { } 0 983828 983828
469 0 my $structtype = $self->structtype( $sv->structid );
470 0 $sv->load( $structtype->fields );
471 }
472 else {
473 # Values 16=OBJECT and 17=CLASS should warn.
474 # Technically a padname with the field CODEx extension on it should
475 # also warn but in practice we shouldn't see one of those outside of
476 # a class that would have warned first anyway.
477 983828 ▶ $type >= 16 and !$self->{warned_experimental_class}++ and
Branch: 1 branch
BranchTrueFalseTotal
$type >= 16 and not $self->{"warned_experimental_class"}++ and warnings::warnif("experimental", "Support for class features in PMAT file is experimental") 8 983820 983828
Condition100%3/3
$type >= 16 and not $self->{"warned_experimental_class"}++
!ll&&!rl&&r
983778428
MC/DC100%2/2
$type >= 16 and not $self->{"warned_experimental_class"}++
$type >= 16 not $self->{"warned_experimental_class"}++
Truth table100%3/3
A and not(B)
A= $type >= 16B= not $self->{"warned_experimental_class"}++
ABresult
0X0
100
111
478 warnings::warnif experimental => "Support for class features in PMAT file is experimental";
479  
480 983828 my ( $bytes, $nptrs, $nstrs ) = $self->{sv_sizes}[$type]->@*;
481 983828 $sv->load(
482 $self->_read_bytesptrsstrs( $bytes, $nptrs, $nstrs )
483 );
484 }
485  
486 983828 return $sv;
487 }
488 }
489  
490 61511 ▶sub _read_svx_80 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_80: called CC=1 SCAR=0.0
491 {
492 61511 my ( $type, $flags ) = unpack "A1 C", $bytes;
493  
494 61511 $sv->more_magic( $type => $flags, @$ptrs );
495 }
496  
497 2 ▶sub _read_svx_81 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_81: called CC=1 SCAR=0.0
498 {
499 2 $sv->_more_saved( SCALAR => $ptrs->[0] );
500 }
501  
502 1 ▶sub _read_svx_82 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_82: called CC=1 SCAR=0.0
503 {
504 1 $sv->_more_saved( ARRAY => $ptrs->[0] );
505 }
506  
507 1 ▶sub _read_svx_83 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_83: called CC=1 SCAR=0.0
508 {
509 1 $sv->_more_saved( HASH => $ptrs->[0] );
510 }
511  
512 1 ▶sub _read_svx_84 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_84: called CC=2 SCAR=7.2
513 {
514 1 my $index = unpack $self->{uint_fmt}, $bytes;
515  
516 1 ▶ $sv->isa( "Devel::MAT::SV::ARRAY" ) and
Branch: 1 branch
BranchTrueFalseTotal
$sv->isa("Devel::MAT::SV::ARRAY") and $sv->_more_saved($index, $ptrs->[0]) 1 0 1
517 $sv->_more_saved( $index, $ptrs->[0] );
518 }
519  
520 1 ▶sub _read_svx_85 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_85: called CC=2 SCAR=7.6
521 {
522 1 ▶ $sv->isa( "Devel::MAT::SV::HASH" ) and
Branch: 1 branch
BranchTrueFalseTotal
$sv->isa("Devel::MAT::SV::HASH") and $sv->_more_saved($ptrs->[0], $ptrs->[1]) 1 0 1
523 $sv->_more_saved( $ptrs->[0], $ptrs->[1] );
524 }
525  
526 1 ▶sub _read_svx_86 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_86: called CC=1 SCAR=0.0
527 {
528 1 $sv->_more_saved( CODE => $ptrs->[0] );
529 }
530  
531 0 ▶sub _read_svx_87 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_87: not called CC=1 SCAR=6.9
532 {
533 0 $sv->_more_annotations( $ptrs->[0], $strs->[0] );
534 }
535  
536 0 ▶sub _read_svx_88 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_88: not called CC=1 SCAR=6.9
537 {
538 0 my ( $serial, $line ) = unpack "($self->{uint_fmt})2", $bytes;
539 0 my $file = $strs->[0];
540  
541 0 $sv->_debugdata( $serial, $line, $file );
542 }
543  
544 118466 ▶sub _read_svx_89 ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_89: called CC=2 SCAR=7.6
545 {
546 118466 my ( $shared_hek ) = unpack "$self->{ptr_fmt}", $bytes;
547  
548 118466 ▶ if( $sv->type eq "SCALAR" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->type eq 'SCALAR') { } 118466 0 118466
549 118466 $sv->_set_shared_hek_at( $shared_hek );
550 }
551 else {
552 0 warn sprintf "Ignoring SVxSHARED_HEK on non-SCALAR SV addr=%#x\n", $sv->addr;
553 }
554 }
555  
556 0 ▶sub _read_svx_8A ( $self, $sv, $bytes, $ptrs, $strs )
Subroutine _read_svx_8A: not called CC=2 SCAR=17.9
557 {
558 0 my ( $shape, $flags, $priv, $ptrlen, $keyiv ) = unpack "C C S ($self->{uint_fmt})2", $bytes;
559 0 ▶ $flags & 0x02 or undef $keyiv;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 2 or undef $keyiv 0 0 0
560  
561 0 $sv->more_magicv2( $flags, $priv, $ptrlen, $keyiv, @{$ptrs}[0 .. 4] );
562 }
563  
564 13 ▶sub _read_ctx ( $self )
Subroutine _read_ctx: called CC=3 SCAR=11.2
565 {
566 13 my $type = $self->_read_u8;
567 13 ▶ return if !$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 8 5 13
568  
569 5 ▶ if( $self->{format_minor} >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'format_minor'} >= 2) { } 5 0 5
570 my $ctx = Devel::MAT::Context->new( $type, $self,
571 5 $self->_read_bytesptrsstrs( $self->{ctx_sizes}[0]->@* )
572 );
573  
574 $ctx->load(
575 5 $self->_read_bytesptrsstrs( $self->{ctx_sizes}[$type]->@* )
576 );
577  
578 5 return $ctx;
579 }
580 else {
581 0 return Devel::MAT::Context->load_v0_1( $type, $self );
582 }
583 }
584  
585 =head1 METHODS
586  
587 =cut
588  
589 =head2 perlversion
590  
591 $version = $df->perlversion;
592  
593 Returns the version of perl that the heap dump file was created by, as a
594 string in the form C<5.14.2>.
595  
596 =cut
597  
598 3 ▶sub perlversion ( $self )
Subroutine perlversion: called CC=1 SCAR=0.0
Pod documented
599 {
600 3 my $v = $self->{perlver};
601 3 return join ".", $v>>24, ($v>>16) & 0xff, $v&0xffff;
602 }
603  
604 =head2 endian
605  
606 $endian = $df->endian;
607  
608 Returns the endian direction of the perl that the heap dump was created by, as
609 either C<big> or C<little>.
610  
611 =cut
612  
613 0 ▶sub endian ( $self )
Subroutine endian: not called CC=2 SCAR=17.9
Pod documented
614 {
615 0 ▶ return $self->{big_endian} ? "big" : "little";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'big_endian'} ? : 0 0 0
616 }
617  
618 =head2 uint_len
619  
620 $len = $df->uint_len;
621  
622 Returns the length in bytes of a uint field of the perl that the heap dump was
623 created by.
624  
625 =cut
626  
627 0 ▶sub uint_len ( $self )
Subroutine uint_len: not called CC=1 SCAR=6.9
Pod documented
628 {
629 0 return $self->{uint_len};
630 }
631  
632 =head2 ptr_len
633  
634 $len = $df->ptr_len;
635  
636 Returns the length in bytes of a pointer field of the perl that the heap dump
637 was created by.
638  
639 =cut
640  
641 0 ▶sub ptr_len ( $self )
Subroutine ptr_len: not called CC=1 SCAR=6.9
Pod documented
642 {
643 0 return $self->{ptr_len};
644 }
645  
646 =head2 nv_len
647  
648 $len = $df->nv_len;
649  
650 Returns the length in bytes of a double field of the perl that the heap dump
651 was created by.
652  
653 =cut
654  
655 0 ▶sub nv_len ( $self )
Subroutine nv_len: not called CC=1 SCAR=6.9
Pod documented
656 {
657 0 return $self->{nv_len};
658 }
659  
660 =head2 ithreads
661  
662 $ithreads = $df->ithreads;
663  
664 Returns a boolean indicating whether ithread support was enabled in the perl
665 that the heap dump was created by.
666  
667 =cut
668  
669 89359 ▶sub ithreads ( $self )
Subroutine ithreads: called CC=1 SCAR=0.0
Pod documented
670 {
671 89359 return $self->{ithreads};
672 }
673  
674 =head2 roots
675  
676 %roots = $df->roots;
677  
678 Returns a key/value pair list giving the names and SVs at each of the roots.
679  
680 =head2 roots_strong
681  
682 %roots = $df->roots_strong;
683  
684 Returns a key/value pair list giving the names and SVs at each of the roots
685 that count as strong references.
686  
687 =head2 roots_weak
688  
689 %roots = $df->roots_weak;
690  
691 Returns a key/value pair list giving the names and SVs at each of the roots
692 that count as strong references.
693  
694 =cut
695  
696 9 ▶sub _roots ( $self )
Subroutine _roots: called CC=1 SCAR=0.0
697 {
698 return map {
699 576 my ( $root_at, $desc ) = @$_;
700 576 $desc => $self->sv_at( $root_at )
701 9 } values $self->{roots}->%*;
702 }
703  
704 1 ▶sub roots ( $self )
Subroutine roots: called CC=1 SCAR=0.0
Pod documented
705 {
706 1 ▶ return pairmap { substr( $a, 1 ) => $b } $self->_roots;
Subroutine __ANON__: called CC=1 SCAR=0.0
707 }
708  
709 6 ▶sub roots_strong ( $self )
Subroutine roots_strong: called CC=1 SCAR=0.0
Pod documented
710 {
711 6 ▶ return pairmap { $a =~ m/^\+(.*)/ ? ( $1 => $b ) : () } $self->_roots;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^\+(.*)/u ? : 342 42 384
712 }
713  
714 2 ▶sub roots_weak ( $self )
Subroutine roots_weak: called CC=1 SCAR=0.0
Pod documented
715 {
716 2 ▶ return pairmap { $a =~ m/^\-(.*)/ ? ( $1 => $b ) : () } $self->_roots;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^\-(.*)/u ? : 14 114 128
717 }
718  
719 =head2 ROOTS
720  
721 $sv = $df->ROOT;
722  
723 For each of the root names given below, a method exists with that name which
724 returns the SV at that root:
725  
726 =for highlighter
727  
728 main_cv
729 defstash
730 mainstack
731 beginav
732 checkav
733 unitcheckav
734 initav
735 endav
736 strtabhv
737 envgv
738 incgv
739 statgv
740 statname
741 tmpsv
742 defgv
743 argvgv
744 argvoutgv
745 argvout_stack
746 fdpidav
747 preambleav
748 modglobalhv
749 regex_padav
750 sortstash
751 firstgv
752 secondgv
753 debstash
754 stashcache
755 isarev
756 registered_mros
757  
758 =for highlighter language=perl
759  
760 =cut
761  
762 =head2 root_descriptions
763  
764 %rootdescs = $df->root_descriptions;
765  
766 Returns a key/value pair list giving the (method) name and description text of
767 each of the possible roots.
768  
769 =cut
770  
771 0 ▶sub root_descriptions ( $self )
Subroutine root_descriptions: not called CC=1 SCAR=6.9
Pod documented
772 {
773 0 my $roots = $self->{roots};
774 return map {
775 0 $_ => substr $roots->{$_}[1], 1
776 } keys %$roots;
777 }
778  
779 =head2 root_at
780  
781 $addr = $df->root_at( $name );
782  
783 Returns the SV address of the given named root.
784  
785 =cut
786  
787 512 ▶sub root_at ( $self, $name )
Subroutine root_at: called CC=2 SCAR=7.6
Pod documented
788 {
789 512 ▶ return $self->{roots}{$name} ? $self->{roots}{$name}[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'roots'}{$name} ? : 512 0 512
790 }
791  
792 =head2 root
793  
794 $sv = $df->root( $name );
795  
796 Returns the given root SV.
797  
798 =cut
799  
800 512 ▶sub root ( $self, $name )
Subroutine root: called CC=2 SCAR=6.9
Pod documented
801 {
802 512 ▶ my $root_at = $self->root_at( $name ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $root_at = $self->root_at($name) or return 345 167 512
803 345 return $self->sv_at( $root_at );
804 }
805  
806 =head2 heap
807  
808 @svs = $df->heap;
809  
810 Returns all of the heap-allocated SVs, in no particular order
811  
812 =cut
813  
814 6 ▶sub heap ( $self )
Subroutine heap: called CC=1 SCAR=0.0
Pod documented
815 {
816 6 return values $self->{heap}->%*;
817 }
818  
819 =head2 stack
820  
821 @svs = $df->stack;
822  
823 Returns all the SVs on the stack
824  
825 =cut
826  
827 3 ▶sub stack ( $self )
Subroutine stack: called CC=1 SCAR=0.0
Pod documented
828 {
829 3 return map { $self->sv_at( $_ ) } $self->{stack_at}->@*;
830 }
831  
832 =head2 contexts
833  
834 @ctxs = $df->contexts;
835  
836 Returns a list of L<Devel::MAT::Context> objects representing the call context
837 stack in the dumpfile.
838  
839 =cut
840  
841 1 ▶sub contexts ( $self )
Subroutine contexts: called CC=1 SCAR=0.0
Pod documented
842 {
843 1 return $self->{contexts}->@*;
844 }
845  
846 =head2 sv_at
847  
848 $sv = $df->sv_at( $addr );
849  
850 Returns the SV at the given address, or C<undef> if one does not exist.
851  
852 (Note that this is unambiguous, as a Perl-level C<undef> is represented by the
853 immortal C<Devel::MAT::SV::UNDEF> SV).
854  
855 =cut
856  
857 17682296 ▶sub sv_at ( $self, $addr )
Subroutine sv_at: called CC=5 SCAR=16.1
Pod documented
858 {
859 17682296 ▶ return undef if !$addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $addr 1291147 16391149 17682296
860  
861 16391149 ▶ return $self->{UNDEF} if $addr == $self->{undef_at};
Branch: 1 branch
BranchTrueFalseTotal
if $addr == $self->{"undef_at"} 1804 16389345 16391149
862 16389345 ▶ return $self->{YES} if $addr == $self->{yes_at};
Branch: 1 branch
BranchTrueFalseTotal
if $addr == $self->{"yes_at"} 1273 16388072 16389345
863 16388072 ▶ return $self->{NO} if $addr == $self->{no_at};
Branch: 1 branch
BranchTrueFalseTotal
if $addr == $self->{"no_at"} 29 16388043 16388072
864  
865 16388043 return $self->{heap}{$addr};
866 }
867  
868 =head1 AUTHOR
869  
870 Paul Evans <leonerd@leonerd.org.uk>
871  
872 =cut
873  
874 10 0x55AA;
« blib/lib/Devel/MAT/ToolBase/GraphWalker.pm ↑ Summary blib/lib/Devel/MAT/Cmd/Terminal.pm »