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

filter: Statementstmt 84.6% 83 / 98 Branchbran 64.7% 22 / 34 Conditioncond 68.1% 15 / 22 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 81.8% 18 / 22 Podpod 12.5% 1 / 8 totaltotal 74.2% 147 / 198 CC 37 SCAR 31.4
CC
37
Cov
78%
_inrefs
45.7
run
12.0
init_tool
10.0
CRAP 23.2
« blib/lib/Devel/MAT/SV.pm ↑ Summary blib/lib/Devel/MAT/Tool/Tools.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::Inrefs 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 5 ▶use base qw( Devel::MAT::Tool );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use feature qw( postderef signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶no warnings qw( experimental::postderef experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use List::Util qw( any pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 my %STRENGTH_TO_IDX = (
18 strong => 0,
19 weak => 1,
20 indirect => 2,
21 inferred => 3,
22 );
23 5 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 IDX_ROOTS_STRONG => 4,
25 IDX_ROOTS_WEAK => 5,
26 IDX_STACK => 6,
27 5 };
28  
29 =head1 NAME
30  
31 C<Devel::MAT::Tool::Inrefs> - annotate which SVs are referred to by others
32  
33 =head1 DESCRIPTION
34  
35 This C<Devel::MAT> tool annotates each SV with back-references from other SVs
36 that refer to it. It follows the C<outrefs> method of every heap SV and
37 annotates the referred SVs with back-references pointing back to the SVs that
38 refer to them.
39  
40 =cut
41  
42 2 ▶sub init_tool ( $self )
Subroutine init_tool: called CC=10 SCAR=23.0
Pod documented
43 {
44 2 my $df = $self->df;
45  
46 2 my $heap_total = scalar $df->heap;
47 2 my $count = 0;
48 2 foreach my $sv ( $df->heap ) {
49 245836 foreach ( pairs $sv->outrefs( "NO_DESC" ) ) {
50 423520 my ( $strength, $refsv ) = @$_;
51  
52 423520 ▶ push $refsv->{tool_inrefs}[ $STRENGTH_TO_IDX{ $strength } ]->@*, $sv->addr if !$refsv->immortal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refsv->immortal 420906 2614 423520
53 }
54  
55 245836 $count++;
56 245836 ▶ $self->report_progress( sprintf "Patching refs in %d of %d (%.2f%%)",
Branch: 1 branch
BranchTrueFalseTotal
if $count % 10000 == 0 24 245812 245836
57 $count, $heap_total, 100*$count / $heap_total ) if ($count % 10000) == 0
58 }
59  
60 # Most SVs are not roots or on the stack. To save time later on we'll make
61 # a note of those rare ones that are
62  
63 2 foreach ( pairs $df->roots_strong ) {
64 114 my ( undef, $sv ) = @$_;
65 114 ▶ next unless $sv;
Branch: 1 branch
BranchTrueFalseTotal
unless $sv 36 78 114
66 78 $sv->{tool_inrefs}[IDX_ROOTS_STRONG]++;
67 }
68  
69 2 foreach ( pairs $df->roots_weak ) {
70 14 my ( undef, $sv ) = @$_;
71 14 ▶ next unless $sv;
Branch: 1 branch
BranchTrueFalseTotal
unless $sv 6 8 14
72 8 $sv->{tool_inrefs}[IDX_ROOTS_WEAK]++;
73 }
74  
75 2 foreach my $sv ( $df->stack ) {
76 4 $sv->{tool_inrefs}[IDX_STACK]++;
77 }
78  
79 2 $self->report_progress();
80 }
81  
82 =head1 SV METHODS
83  
84 =for highlighter language=perl
85  
86 This tool adds the following SV methods.
87  
88 =head2 inrefs
89  
90 @refs = $sv->inrefs;
91  
92 Returns a list of Reference objects for each of the SVs that refer to this
93 one. This is formed by the inverse mapping along the SV graph from C<outrefs>.
94  
95 =head2 inrefs_strong
96  
97 =head2 inrefs_weak
98  
99 =head2 inrefs_direct
100  
101 =head2 inrefs_indirect
102  
103 =head2 inrefs_inferred
104  
105 @refs = $sv->inrefs_strong;
106  
107 @refs = $sv->inrefs_weak;
108  
109 @refs = $sv->inrefs_direct;
110  
111 @refs = $sv->inrefs_indirect;
112  
113 @refs = $sv->inrefs_inferred;
114  
115 Returns lists of Reference objects filtered by type, analogous to the various
116 C<outrefs_*> methods.
117  
118 =cut
119  
120 3866 ▶sub Devel::MAT::SV::_inrefs ( $self, @strengths )
Subroutine _inrefs: called CC=26 SCAR=38.2
121 {
122 # In scalar context we don't need to return SVs or Reference instances,
123 # just count them. This allows a lot of optimisations.
124 3866 my $just_count = !wantarray;
125  
126 3866 ▶ $self->{tool_inrefs} ||= [];
Condition100%2/2
$self->{"tool_inrefs"} ||= []
l!l
38642
MC/DC100%1/1
$self->{"tool_inrefs"} ||= []
$self->{"tool_inrefs"}
Truth table100%2/2
A
A= $self->{"tool_inrefs"}
Aresult
11
00
127  
128 3866 my $df = $self->df;
129 3866 my @inrefs;
130 3866 foreach my $strength ( @strengths ) {
131 15400 my %seen;
132 15400 ▶ foreach my $addr ( ( $self->{tool_inrefs}[ $STRENGTH_TO_IDX{$strength} ] // [] )->@* ) {
Condition100%2/2
$self->{"tool_inrefs"}[$STRENGTH_TO_IDX{$strength}] // []
l!l
115273873
MC/DC100%1/1
$self->{"tool_inrefs"}[$STRENGTH_TO_IDX{$strength}] // []
$self->{"tool_inrefs"}[$STRENGTH_TO_IDX{$strength}]
Truth table100%2/2
A
A= $self->{"tool_inrefs"}[$STRENGTH_TO_IDX{$strength}]
Aresult
11
00
133 19202 ▶ if( $just_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_count) { } 0 19202 19202
134 0 push @inrefs, 1;
135 }
136 else {
137 19202 ▶ $seen{$addr}++ and next;
Branch: 1 branch
BranchTrueFalseTotal
$seen{$addr}++ and next 1 19201 19202
138  
139 19201 my $sv = $df->sv_at( $addr );
140  
141 push @inrefs, Devel::MAT::SV::Reference( $_->name, $_->strength, $sv )
142 19201 ▶ for grep { $_->strength eq $strength and $_->sv == $self } $sv->outrefs;
Condition100%3/3
$_->strength eq $strength && $_->sv == $self
!ll&&!rl&&r
29816421248975919202
MC/DC100%2/2
$_->strength eq $strength && $_->sv == $self
$_->strength eq $strength $_->sv == $self
Truth table100%3/3
A && B
A= $_->strength eq $strengthB= $_->sv == $self
ABresult
0X0
100
111
143 }
144 }
145 }
146  
147 3866 ▶ if( $self->{tool_inrefs}[IDX_ROOTS_STRONG] and $strengths[0] eq "strong" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tool_inrefs"}[4] and $strengths[0] eq "strong") 4 3862 3866
Condition66%2/3
$self->{"tool_inrefs"}[4] and $strengths[0] eq "strong"
!ll&&!rl&&r
386204
MC/DC50%1/2
$self->{"tool_inrefs"}[4] and $strengths[0] eq "strong"
$self->{"tool_inrefs"}[4] $strengths[0] eq "strong"
Truth table66%2/3
A and B
A= $self->{"tool_inrefs"}[4]B= $strengths[0] eq "strong"
ABresult
0X0
100
111
148 4 ▶ if( $just_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_count) { } 0 4 4
149 0 push @inrefs, ( 1 ) x $self->{tool_inrefs}[IDX_ROOTS_STRONG];
150 }
151 else {
152 4 foreach ( pairs $df->roots_strong ) {
153 228 my ( $name, $sv ) = @$_;
154 228 ▶ push @inrefs, Devel::MAT::SV::Reference( $name, strong => undef )
Branch: 1 branch
BranchTrueFalseTotal
if defined $sv and $sv == $self 4 224 228
Condition100%3/3
defined $sv and $sv == $self
!ll&&!rl&&r
721524
MC/DC100%2/2
defined $sv and $sv == $self
defined $sv $sv == $self
Truth table100%3/3
A and B
A= defined $svB= $sv == $self
ABresult
0X0
100
111
155 if defined $sv and $sv == $self;
156 }
157 }
158 }
159  
160 0 ▶ if( $self->{tool_inrefs}[IDX_ROOTS_WEAK] and any { $_ eq "weak" } @strengths ) {
Subroutine __ANON__: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tool_inrefs"}[5] and &any(sub { $_ eq "weak"; } , @strengths)) 0 3866 3866
Condition33%1/3
$self->{"tool_inrefs"}[5] and &any(sub { $_ eq "weak"; } , @strengths)
!ll&&!rl&&r
386600
MC/DC0%0/2
$self->{"tool_inrefs"}[5] and &any(sub { $_ eq "weak"; } , @strengths)
$self->{"tool_inrefs"}[5] &any(sub { $_ eq "weak"; } , @strengths)
Truth table33%1/3
A and B
A= $self->{"tool_inrefs"}[5]B= &any(sub { $_ eq "weak"; } , @strengths)
ABresult
0X0
100
111
161 0 ▶ if( $just_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_count) { } 0 0 0
162 0 push @inrefs, ( 1 ) x $self->{tool_inrefs}[IDX_ROOTS_WEAK];
163 }
164 else {
165 0 foreach ( pairs $df->roots_weak ) {
166 0 my ( $name, $sv ) = @$_;
167 0 ▶ push @inrefs, Devel::MAT::SV::Reference( $name, weak => undef )
Branch: 1 branch
BranchTrueFalseTotal
if defined $sv and $sv == $self 0 0 0
Condition0%0/3
defined $sv and $sv == $self
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sv and $sv == $self
defined $sv $sv == $self
Truth table0%0/3
A and B
A= defined $svB= $sv == $self
ABresult
0X0
100
111
168 if defined $sv and $sv == $self;
169 }
170 }
171 }
172  
173 3866 ▶ if( $self->{tool_inrefs}[IDX_STACK] and any { $_ eq "weak" } @strengths ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tool_inrefs"}[6] and &any(sub { $_ eq "weak"; } , @strengths)) 1 3865 3866
Condition66%2/3
$self->{"tool_inrefs"}[6] and &any(sub { $_ eq "weak"; } , @strengths)
!ll&&!rl&&r
386501
MC/DC50%1/2
$self->{"tool_inrefs"}[6] and &any(sub { $_ eq "weak"; } , @strengths)
$self->{"tool_inrefs"}[6] &any(sub { $_ eq "weak"; } , @strengths)
Truth table66%2/3
A and B
A= $self->{"tool_inrefs"}[6]B= &any(sub { $_ eq "weak"; } , @strengths)
ABresult
0X0
100
111
174 1 ▶ if( $just_count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($just_count) { } 0 1 1
175 0 push @inrefs, ( 1 ) x $self->{tool_inrefs}[IDX_STACK];
176 }
177 else {
178 1 foreach my $stacksv ( $df->stack ) {
179 2 ▶ next unless $stacksv->addr == $self->addr;
Branch: 1 branch
BranchTrueFalseTotal
unless $stacksv->addr == $self->addr 1 1 2
180  
181 1 push @inrefs, Devel::MAT::SV::Reference( "a value on the stack", strong => undef );
182 }
183 }
184 }
185  
186 3866 return @inrefs;
187 }
188  
189 # If 'strong' is included in these lists it must be first
190 3844 ▶sub Devel::MAT::SV::inrefs ( $self ) { $self->_inrefs( qw( strong weak indirect inferred )) }
Subroutine inrefs: called CC=1 SCAR=0.0
Pod undocumented
191 19 ▶sub Devel::MAT::SV::inrefs_strong ( $self ) { $self->_inrefs( qw( strong )) }
Subroutine inrefs_strong: called CC=1 SCAR=0.0
Pod undocumented
192 0 ▶sub Devel::MAT::SV::inrefs_weak ( $self ) { $self->_inrefs( qw( weak )) }
Subroutine inrefs_weak: not called CC=1 SCAR=6.9
Pod undocumented
193 2 ▶sub Devel::MAT::SV::inrefs_direct ( $self ) { $self->_inrefs( qw( strong weak )) }
Subroutine inrefs_direct: called CC=1 SCAR=0.0
Pod undocumented
194 1 ▶sub Devel::MAT::SV::inrefs_indirect ( $self ) { $self->_inrefs( qw( indirect )) }
Subroutine inrefs_indirect: called CC=1 SCAR=0.0
Pod undocumented
195 0 ▶sub Devel::MAT::SV::inrefs_inferred ( $self ) { $self->_inrefs( qw( inferred )) }
Subroutine inrefs_inferred: not called CC=1 SCAR=6.9
Pod undocumented
196  
197 =head1 COMANDS
198  
199 =for highlighter
200  
201 =cut
202  
203 =head2 inrefs
204  
205 pmat> inrefs defstash
206 s the hash GLOB(%*) at 0x556e47243e40
207  
208 Shows the incoming references that refer to a given SV.
209  
210 Takes the following named options:
211  
212 =over 4
213  
214 =item --weak
215  
216 Include weak direct references in the output (by default only strong direct
217 ones will be included).
218  
219 =item --all
220  
221 Include both weak and indirect references in the output.
222  
223 =back
224  
225 =cut
226  
227 5 ▶use constant CMD => "inrefs";
Subroutine BEGIN: called CC=1 SCAR=0.0
228 5 ▶use constant CMD_DESC => "Show incoming references to a given SV";
Subroutine BEGIN: called CC=1 SCAR=0.0
229  
230 5 ▶use constant CMD_OPTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
231 weak => { help => "include weak references" },
232 all => { help => "include weak and indirect references",
233 alias => "a" },
234 5 );
235  
236 5 ▶use constant CMD_ARGS_SV => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
237  
238 0 ▶sub run ( $self, $optsref, $sv )
Subroutine run: not called CC=3 SCAR=24.8
Pod undocumented
239 {
240 0 my %opts = $optsref->%*;
241  
242 my $method = $opts{all} ? "inrefs" :
243 0 ▶ $opts{weak} ? "inrefs_direct" :
Branch: 2 branches
BranchTrueFalseTotal
$opts{'all'} ? : 0 0 0
$opts{'weak'} ? : 0 0 0
244 "inrefs_strong";
245  
246 0 require Devel::MAT::Tool::Outrefs;
247 0 Devel::MAT::Tool::Outrefs->show_refs_by_method( $method, $sv );
248 }
249  
250 =head1 AUTHOR
251  
252 Paul Evans <leonerd@leonerd.org.uk>
253  
254 =cut
255  
256 5 0x55AA;
« blib/lib/Devel/MAT/SV.pm ↑ Summary blib/lib/Devel/MAT/Tool/Tools.pm »