blib/lib/Devel/MAT/Tool/Reachability.pm

filter: Statementstmt 86.3% 101 / 117 Branchbran 75.0% 63 / 84 Conditioncond 42.5% 17 / 40 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 83.3% 10 / 12 Podpod 0.0% 0 / 3 totaltotal 69.6% 195 / 280 CC 71 SCAR 46.8
CC
71
Cov
75%
mark_reachable
129.1
_reach2icon
12.0
init_ui
6.0
CRAP 107.8
« blib/lib/Devel/MAT/Tool/Show.pm ↑ Summary blib/lib/Devel/MAT/Tool/ListDanglingPtrs.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::Tool::Reachability 0.56;
7  
8 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use constant FOR_UI => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use List::Util qw( pairvalues );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<Devel::MAT::Tool::Reachability> - analyse how SVs are reachable
21  
22 =head1 DESCRIPTION
23  
24 This C<Devel::MAT> tool determines which SVs are reachable via any known roots
25 and which are not. For reachable SVs, they are classified into several broad
26 categories:
27  
28 =over 2
29  
30 =item *
31  
32 SVs that directly make up the symbol table.
33  
34 =item *
35  
36 SVs that form the padlist of functions or store the names of lexical
37 variables.
38  
39 =item *
40  
41 SVs that hold the value of lexical variables.
42  
43 =item *
44  
45 User data stored in package globals, lexical variables, or referenced
46 recursively via structures stored in them.
47  
48 =item *
49  
50 Miscellaneous other SVs that are used to implement the internals of the
51 interpreter.
52  
53 =back
54  
55 =cut
56  
57 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
58 5 REACH_SYMTAB => 1,
59 REACH_USER => 2,
60 REACH_PADLIST => 3,
61 REACH_LEXICAL => 4,
62 REACH_INTERNAL => 5,
63 5 };
64  
65 1 ▶sub new ( $class, $pmat, %args )
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
66 {
67 2 ▶ *Devel::MAT::SV::reachable = sub ( $sv ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 2 return $sv->{tool_reachable};
69 1 };
70  
71 1 $class->mark_reachable( $pmat->dumpfile, progress => $args{progress} );
72  
73 1 return $class;
74 }
75  
76 5 my @ICONS = (
77 "none", "symtab", "user", "padlist", "lexical", "internal"
78 );
79 0 ▶sub _reach2icon ( $sv )
Subroutine _reach2icon: not called CC=3 SCAR=24.8
80 {
81 0 ▶ my $reach = $sv->{tool_reachable} // 0;
Condition0%0/2
$sv->{"tool_reachable"} // 0
l!l
00
MC/DC0%0/1
$sv->{"tool_reachable"} // 0
$sv->{"tool_reachable"}
Truth table0%0/2
A
A= $sv->{"tool_reachable"}
Aresult
11
00
82  
83 0 ▶ my $icon = $ICONS[$reach] // die "Unknown reachability value $reach";
Condition0%0/2
$ICONS[$reach] // die("Unknown reachability value $reach")
l!l
00
MC/DC0%0/1
$ICONS[$reach] // die("Unknown reachability value $reach")
$ICONS[$reach]
Truth table0%0/2
A
A= $ICONS[$reach]
Aresult
11
00
84 0 return "reachable-$icon";
85 }
86  
87 0 ▶sub init_ui ( $self, $ui )
Subroutine init_ui: not called CC=2 SCAR=17.9
Pod undocumented
88 {
89 0 foreach ( @ICONS ) {
90 0 $ui->register_icon(
91 name => "reachable-$_",
92 svg => "icons/reachable-$_.svg"
93 );
94 }
95  
96 0 my $column = $ui->provides_svlist_column(
97 title => "R",
98 type => "icon",
99 );
100  
101 0 $ui->provides_sv_detail(
102 title => "Reachable",
103 type => "icon",
104 render => \&_reach2icon,
105 );
106  
107 0 $ui->set_svlist_column_values(
108 column => $column,
109 from => \&_reach2icon,
110 );
111 }
112  
113 1 ▶sub mark_reachable ( $self, $df, %args )
Subroutine mark_reachable: called CC=68 SCAR=48.6
Pod undocumented
114 {
115 1 my $progress = $args{progress};
116  
117 1 my @user;
118 my @internal;
119  
120 # First, walk the symbol table
121 {
122 1 my @symtab = ( $df->defstash );
123 1 $symtab[0]->{tool_reachable} = REACH_SYMTAB;
124  
125 1 my $count = 0;
126 1 ▶ while( @symtab ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@symtab) 425 1 426
127 425 my $stash = shift @symtab;
128 425 ▶ $stash->type =~ m/^(?:STASH|CLASS)$/ or
Branch: 1 branch
BranchTrueFalseTotal
$stash->type =~ /^(?:STASH|CLASS)$/u or die 'ARGH! Encountered non-stash ' . $stash->desc_addr 425 0 425
129 die "ARGH! Encountered non-stash ".$stash->desc_addr;
130  
131 425 my @more_symtab;
132 my @more_user;
133  
134 425 foreach my $key ( $stash->keys ) {
135 10428 my $value = $stash->value( $key );
136  
137 # Keys ending :: signify sub-stashes
138 10428 ▶ if( $key =~ m/::$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /::$/u) { } 425 10003 10428
elsif ($value->type eq 'GLOB') { } 9970 33 10003
139 425 push @more_symtab, $value->hash;
140 }
141 # Otherwise it might be a glob
142 elsif( $value->type eq "GLOB" ) {
143 9970 my $gv = $value;
144 9970 $gv->{tool_reachable} = REACH_SYMTAB;
145  
146 9970 ▶ defined $_ and push @more_user, $_ for
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and push @more_user, $_ 9277 50543 59820
147 $gv->scalar, $gv->array, $gv->hash, $gv->code, $gv->io, $gv->form;
148 }
149 # Otherwise it might be a SCALAR/ARRAY/HASH directly in the STASH
150 else {
151 33 push @more_user, $value;
152 }
153  
154 10428 $count++;
155 10428 ▶ $progress->( sprintf "Walking symbol table %d...", $count ) if $progress and $count % 1000 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $progress and $count % 1000 == 0 0 10428 10428
Condition33%1/3
$progress and $count % 1000 == 0
!ll&&!rl&&r
1042800
MC/DC0%0/2
$progress and $count % 1000 == 0
$progress $count % 1000 == 0
Truth table33%1/3
A and B
A= $progressB= $count % 1000 == 0
ABresult
0X0
100
111
156 }
157  
158 !$_->{tool_reachable} and
159 425 ▶ $_->{tool_reachable} = REACH_SYMTAB, push @symtab, $_ for @more_symtab;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"tool_reachable"} or $_->{"tool_reachable"} = 1, push(@symtab, $_) 1 424 425
160  
161 !$_->{tool_reachable} and
162 425 ▶ $_->{tool_reachable} = REACH_USER, push @user, $_ for @more_user;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"tool_reachable"} or $_->{"tool_reachable"} = 2, push(@user, $_) 1327 7983 9310
163  
164 !$_->{tool_reachable} and
165 425 ▶ $_->{tool_reachable} = REACH_INTERNAL, push @internal, $_ for grep { defined }
Branch: 1 branch
BranchTrueFalseTotal
$_->{"tool_reachable"} or $_->{"tool_reachable"} = 5, push(@internal, $_) 1 914 915
166 $stash->backrefs,
167 $stash->mro_linearall,
168 $stash->mro_linearcurrent,
169 $stash->mro_nextmethod,
170 $stash->mro_isa,
171 $stash->magic_svs;
172  
173 425 $count++;
174 425 ▶ $progress->( sprintf "Walking symbol table %d...", $count ) if $progress and $count % 1000 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $progress and $count % 1000 == 0 0 425 425
Condition33%1/3
$progress and $count % 1000 == 0
!ll&&!rl&&r
42500
MC/DC0%0/2
$progress and $count % 1000 == 0
$progress $count % 1000 == 0
Truth table33%1/3
A and B
A= $progressB= $count % 1000 == 0
ABresult
0X0
100
111
175 }
176 }
177  
178 # Next the reachable user data, recursively
179 {
180 1 push @user, $df->main_cv;
181 1 my $count = 0;
182 1 ▶ while( @user ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@user) 35331 1 35332
183 35331 ▶ my $sv = shift @user or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sv = shift @user or next 35331 0 35331
184  
185 35331 my @more_user;
186 my @more_internal;
187  
188 35331 for( $sv->type ) {
189 35331 ▶ if ( $_ eq "REF" ) { push @more_user, $sv->rv if $sv->rv }
Branch: 8 branches
BranchTrueFalseTotal
if ($_ eq 'REF') { } 1972 33359 35331
elsif ($_ eq 'ARRAY') { } 598 32761 33359
elsif ($_ eq 'HASH') { } 637 32124 32761
elsif ($_ eq 'GLOB') { } 1 32123 32124
elsif ($_ eq 'CODE') { } 7054 25069 32123
elsif ($_ eq 'LVALUE') { } 0 25069 25069
elsif ($_ =~ /^(?:UNDEF|BOOL|SCALAR|IO|REGEXP|FORMAT)$/u) { } 25069 0 25069
if $sv->rv 1972 0 1972
190 598 elsif( $_ eq "ARRAY" ) { push @more_user, $sv->elems; }
191 637 elsif( $_ eq "HASH" ) { push @more_user, $sv->values; }
192 elsif( $_ eq "GLOB" ) {
193 1 my $gv = $sv;
194 1 ▶ next if $gv->{tool_reachable}; # already on symbol table
Branch: 1 branch
BranchTrueFalseTotal
if $gv->{"tool_reachable"} 1 0 1
195  
196 0 warn "Found non-SYMTAB GLOB " . $gv->desc_addr . " user reachable\n";
197 # Hard to know if the GV is being used for GVSV, GVAV, GVHV or GVCV
198 0 push @more_user, $gv->scalar, $gv->array, $gv->hash, $gv->code, $gv->egv, $gv->io, $gv->form;
199 }
200 elsif( $_ eq "CODE" ) {
201 7054 my $cv = $sv;
202  
203 7054 my @more_padlist;
204 my @more_lexical;
205  
206 7054 push @more_padlist, $cv->padlist;
207  
208 7054 my $padnames_av = $cv->padnames_av;
209 7054 ▶ if( $padnames_av ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($padnames_av) 0 7054 7054
210 0 push @more_padlist, $padnames_av, $padnames_av->elems;
211 }
212  
213 7054 foreach my $pad ( $cv->pads ) {
214 3793 ▶ $pad or next;
Branch: 1 branch
BranchTrueFalseTotal
$pad or next 3791 2 3793
215 3791 push @more_padlist, $pad;
216  
217 # PAD slot 0 is always @_
218 3791 ▶ if( my $argsav = $pad->elem( 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $argsav = $pad->elem(0)) 3562 229 3791
219 3562 push @more_internal, $argsav;
220 }
221  
222 3791 foreach my $padix ( 1 .. $pad->elems-1 ) {
223 39488 ▶ my $padname_sv = $padnames_av ? $padnames_av->elem( $padix ) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$padnames_av ? : 0 39488 39488
224 39488 ▶ my $padname = $padname_sv && $padname_sv->type eq "SCALAR" ?
Branch: 1 branch
BranchTrueFalseTotal
$padname_sv && $padname_sv->type eq 'SCALAR' ? : 0 39488 39488
Condition33%1/3
$padname_sv and $padname_sv->type eq "SCALAR"
!ll&&!rl&&r
3948800
MC/DC0%0/2
$padname_sv and $padname_sv->type eq "SCALAR"
$padname_sv $padname_sv->type eq "SCALAR"
Truth table33%1/3
A and B
A= $padname_svB= $padname_sv->type eq "SCALAR"
ABresult
0X0
100
111
225 $padname_sv->pv : undef;
226  
227 39488 ▶ my $padsv = $pad->elem( $padix ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $padsv = $pad->elem($padix) or next 39277 211 39488
228 39277 ▶ $padsv->immortal and next;
Branch: 1 branch
BranchTrueFalseTotal
$padsv->immortal and next 0 39277 39277
229  
230 39277 ▶ if( $padname and $padname eq "&" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($padname and $padname eq '&') { } 0 39277 39277
elsif ($padname) { } 0 39277 39277
Condition33%1/3
$padname and $padname eq "&"
!ll&&!rl&&r
3927700
MC/DC0%0/2
$padname and $padname eq "&"
$padname $padname eq "&"
Truth table33%1/3
A and B
A= $padnameB= $padname eq "&"
ABresult
0X0
100
111
231 # Slots named "&" are closure prototype subs
232 0 push @more_user, $padsv;
233 }
234 elsif( $padname ) {
235 # Other named slots are lexical vars
236 0 push @more_lexical, $padsv;
237 }
238 else {
239 # Unnamed slots are just part of the padlist
240 39277 push @more_internal, $padsv;
241 }
242 }
243 }
244  
245 7054 ▶ $_ and push @more_user, $_ for
Branch: 1 branch
BranchTrueFalseTotal
$_ and push @more_user, $_ 28258 8377 36635
246 $cv->scope, $cv->constval, $cv->constants, $cv->globrefs;
247  
248 $_ and !$_->{tool_reachable} and
249 7054 ▶ $_->{tool_reachable} = REACH_PADLIST for @more_padlist;
Branch: 1 branch
BranchTrueFalseTotal
$_ and not $_->{"tool_reachable"} and $_->{"tool_reachable"} = 3 3791 7054 10845
Condition66%2/3
$_ and not $_->{"tool_reachable"}
!ll&&!rl&&r
705403791
MC/DC50%1/2
$_ and not $_->{"tool_reachable"}
$_ not $_->{"tool_reachable"}
Truth table66%2/3
A and not(B)
A= $_B= not $_->{"tool_reachable"}
ABresult
0X0
100
111
250  
251 $_ and !$_->{tool_reachable} and
252 7054 ▶ $_->{tool_reachable} = REACH_LEXICAL, push @user, $_ for @more_lexical;
Branch: 1 branch
BranchTrueFalseTotal
$_ and not $_->{"tool_reachable"} and $_->{"tool_reachable"} = 4, push(@user, $_) 0 0 0
Condition0%0/3
$_ and not $_->{"tool_reachable"}
!ll&&!rl&&r
000
MC/DC0%0/2
$_ and not $_->{"tool_reachable"}
$_ not $_->{"tool_reachable"}
Truth table0%0/3
A and not(B)
A= $_B= not $_->{"tool_reachable"}
ABresult
0X0
100
111
253 }
254 elsif( $_ eq "LVALUE" ) {
255 0 my $lv = $sv;
256  
257 0 ▶ push @more_internal, $lv->target if $lv->target;
Branch: 1 branch
BranchTrueFalseTotal
if $lv->target 0 0 0
258 }
259 elsif( $_ =~ m/^(?:UNDEF|BOOL|SCALAR|IO|REGEXP|FORMAT)$/ ) { } # ignore
260  
261 0 else { warn "Not sure what to do with user data item ".$sv->desc_addr."\n"; }
262 }
263  
264 $_ and !$_->{tool_reachable} and !$_->immortal and
265 35331 ▶ $_->{tool_reachable} = REACH_USER, push @user, $_ for @more_user;
Branch: 1 branch
BranchTrueFalseTotal
$_ and not $_->{"tool_reachable"} and not $_->immortal and $_->{"tool_reachable"} = 2, push(@user, $_) 27347 13026 40373
Condition83%5/6
$_ and not $_->{"tool_reachable"} and not $_->immortal
!ll&&!rl&&r
129537327347
$_ and not $_->{"tool_reachable"}
!ll&&!rl&&r
01295327420
MC/DC66%2/3
$_ and not $_->{"tool_reachable"} and not $_->immortal
$_ not $_->{"tool_reachable"} not $_->immortal
Truth table75%3/4
A and not(B) and not(C)
A= $_B= not $_->{"tool_reachable"}C= not $_->immortal
ABCresult
0XX0
10X0
1100
1111
266  
267 $_ and !$_->{tool_reachable} and !$_->immortal and
268 35331 $_->{tool_reachable} = REACH_INTERNAL, push @internal, $_ for
269 @more_internal,
270 464 ▶ grep { defined } $sv->magic_svs;
Branch: 1 branch
BranchTrueFalseTotal
$_ and not $_->{"tool_reachable"} and not $_->immortal and $_->{"tool_reachable"} = 5, push(@internal, $_) 42016 1287 43303
Condition66%4/6
$_ and not $_->{"tool_reachable"} and not $_->immortal
!ll&&!rl&&r
1287042016
$_ and not $_->{"tool_reachable"}
!ll&&!rl&&r
0128742016
MC/DC33%1/3
$_ and not $_->{"tool_reachable"} and not $_->immortal
$_ not $_->{"tool_reachable"} not $_->immortal
Truth table50%2/4
A and not(B) and not(C)
A= $_B= not $_->{"tool_reachable"}C= not $_->immortal
ABCresult
0XX0
10X0
1100
1111
271  
272 35331 $count++;
273 35331 ▶ $progress->( sprintf "Marking user reachability %d...", $count ) if $progress and $count % 1000 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $progress and $count % 1000 == 0 0 35331 35331
Condition33%1/3
$progress and $count % 1000 == 0
!ll&&!rl&&r
3533100
MC/DC0%0/2
$progress and $count % 1000 == 0
$progress $count % 1000 == 0
Truth table33%1/3
A and B
A= $progressB= $count % 1000 == 0
ABresult
0X0
100
111
274 }
275 }
276  
277 # Finally internals
278 {
279 1 push @internal, pairvalues $df->roots;
280 1 my $count = 0;
281 1 ▶ while( @internal ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@internal) 43685 1 43686
282 43685 ▶ my $sv = shift @internal or next;
Branch: 1 branch
BranchTrueFalseTotal
my $sv = shift @internal or next 43664 21 43685
283 43664 ▶ next if $sv->{tool_reachable};
Branch: 1 branch
BranchTrueFalseTotal
if $sv->{"tool_reachable"} 43408 256 43664
284  
285 256 $sv->{tool_reachable} = REACH_INTERNAL;
286  
287 256 ▶ push @internal, map { $_->sv ? $_->sv : () } $sv->outrefs;
Branch: 1 branch
BranchTrueFalseTotal
$_->sv ? : 691 0 691
288  
289 256 $count++;
290 256 ▶ $progress->( sprintf "Marking internal reachability %d...", $count ) if $progress and $count % 1000 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $progress and $count % 1000 == 0 0 256 256
Condition33%1/3
$progress and $count % 1000 == 0
!ll&&!rl&&r
25600
MC/DC0%0/2
$progress and $count % 1000 == 0
$progress $count % 1000 == 0
Truth table33%1/3
A and B
A= $progressB= $count % 1000 == 0
ABresult
0X0
100
111
291 }
292 }
293 }
294  
295 =head1 SV METHODS
296  
297 =for highlighter language=perl
298  
299 This tool adds the following SV methods.
300  
301 =head2 reachable
302  
303 $r = $sv->reachable;
304  
305 Returns true if the SV is reachable from a known root.
306  
307 =cut
308  
309 =head1 AUTHOR
310  
311 Paul Evans <leonerd@leonerd.org.uk>
312  
313 =cut
314  
315 5 0x55AA;
« blib/lib/Devel/MAT/Tool/Show.pm ↑ Summary blib/lib/Devel/MAT/Tool/ListDanglingPtrs.pm »