blib/lib/Devel/MAT.pm

filter: Statementstmt 74.4% 181 / 243 Branchbran 47.4% 73 / 154 Conditioncond 59.3% 54 / 91 MC/DCmcdc 38.4% 20 / 52 Subroutinesub 82.9% 34 / 41 Podpod 83.3% 10 / 12 totaltotal 62.7% 372 / 593 CC 115 SCAR 43.5
CC
115
Cov
63%
format_sv_with_value
462.0
format_value
46.6
inref_graph
32.9
CRAP 77.3
« blib/lib/Devel/MAT/InternalTools.pm ↑ Summary blib/lib/Devel/MAT/Tool/Strtab.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 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 List::Util qw( first pairs );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use List::UtilsBy qw( sort_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 ▶use Syntax::Keyword::Match;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 10 ▶use Devel::MAT::Dumpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use Devel::MAT::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Devel::MAT::InternalTools;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 use Module::Pluggable
26 10 ▶ sub_name => "_available_tools",
Subroutine BEGIN: called CC=1 SCAR=0.0
27 search_path => [ "Devel::MAT::Tool" ],
28 10 require => 1;
29  
30 10 require XSLoader;
31 10 XSLoader::load( __PACKAGE__, our $VERSION );
32  
33 =head1 NAME
34  
35 C<Devel::MAT> - Perl Memory Analysis Tool
36  
37 =head1 USER GUIDE
38  
39 B<NEW USERS:>
40  
41 If you are new to the C<Devel::MAT> set of tools, this is probably not the
42 document you want to start with. If you are interested in using C<Devel::MAT>
43 to help diagnose memory-related problems in a F<perl> program you instead want
44 to read the user guide, at L<Devel::MAT::UserGuide>.
45  
46 If you are writing tooling modules to extend the abilities of C<Devel::MAT>
47 then this may indeed by the document for you; read on...
48  
49 =head1 DESCRIPTION
50  
51 A C<Devel::MAT> instance loads a heapdump file, and provides a container to
52 store analysis tools to work on it. Tools may be provided that conform to the
53 L<Devel::MAT::Tool> API, which can help analyse the data and interact with the
54 explorer user interface by using the methods in the L<Devel::MAT::UI> package.
55  
56 =head2 File Format
57  
58 The dump file format is still under development, so at present no guarantees
59 are made on whether files can be loaded over mismatching versions of
60 C<Devel::MAT>. However, as of version 0.11 the format should be more
61 extensible, allowing new SV fields to be added without breaking loading - older
62 tools will ignore new fields and newer tools will just load undef for fields
63 absent in older files. As the distribution approaches maturity the format will
64 be made more stable.
65  
66 =cut
67  
68 =head1 CONSTRUCTOR
69  
70 =for highlighter language=perl
71  
72 =cut
73  
74 =head2 load
75  
76 $pmat = Devel::MAT->load( $path, %args );
77  
78 Loads a heap dump file from the given path, and returns a new C<Devel::MAT>
79 instance wrapping it.
80  
81 =cut
82  
83 8 ▶sub load ( $class, $path, %opts )
Subroutine load: called CC=1 SCAR=0.0
Pod documented
84 {
85 8 my $df = Devel::MAT::Dumpfile->load( $path, %opts );
86  
87 8 return bless {
88 df => $df,
89 }, $class;
90 }
91  
92 =head1 METHODS
93  
94 =cut
95  
96 =head2 dumpfile
97  
98 $df = $pmat->dumpfile;
99  
100 Returns the underlying L<Devel::MAT::Dumpfile> instance backing this analysis
101 object.
102  
103 =cut
104  
105 54 ▶sub dumpfile ( $self )
Subroutine dumpfile: called CC=1 SCAR=0.0
Pod documented
106 {
107 54 return $self->{df};
108 }
109  
110 =head2 available_tools
111  
112 @tools = $pmat->available_tools;
113  
114 Lists the L<Devel::MAT::Tool> classes that are installed and available.
115  
116 =cut
117  
118 {
119 10 my @TOOLS;
120 10 my $TOOLS_LOADED;
121  
122 3865 ▶ sub available_tools ( $self )
Subroutine available_tools: called CC=6 SCAR=20.2
Pod documented
123 {
124 3865 ▶ return @TOOLS if $TOOLS_LOADED;
Branch: 1 branch
BranchTrueFalseTotal
if $TOOLS_LOADED 3861 4 3865
125  
126 4 $TOOLS_LOADED++;
127 4 @TOOLS = map { $_ =~ s/^Devel::MAT::Tool:://; $_ } $self->_available_tools;
128  
129 4 foreach my $name ( @TOOLS ) {
130 148 my $tool_class = "Devel::MAT::Tool::$name";
131 148 ▶ next unless $tool_class->can( "AUTOLOAD_TOOL" ) and $tool_class->AUTOLOAD_TOOL( $self );
Branch: 1 branch
BranchTrueFalseTotal
unless $tool_class->can("AUTOLOAD_TOOL") and $tool_class->AUTOLOAD_TOOL($self) 148 0 148
Condition33%1/3
$tool_class->can("AUTOLOAD_TOOL") and $tool_class->AUTOLOAD_TOOL($self)
!ll&&!rl&&r
14800
MC/DC0%0/2
$tool_class->can("AUTOLOAD_TOOL") and $tool_class->AUTOLOAD_TOOL($self)
$tool_class->can("AUTOLOAD_TOOL") $tool_class->AUTOLOAD_TOOL($self)
Truth table33%1/3
A and B
A= $tool_class->can("AUTOLOAD_TOOL")B= $tool_class->AUTOLOAD_TOOL($self)
ABresult
0X0
100
111
132  
133 0 ▶ $self->{tools}{$name} ||= $tool_class->new( $self );
Condition0%0/3
$self->{"tools"}{$name} ||= $tool_class->new($self)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tools"}{$name} ||= $tool_class->new($self)
$self->{"tools"}{$name} $tool_class->new($self)
Truth table0%0/3
A ||= B
A= $self->{"tools"}{$name}B= $tool_class->new($self)
ABresult
1X1
011
000
134 }
135  
136 4 return @TOOLS;
137 }
138 }
139  
140 =head2 load_tool
141  
142 $tool = $pmat->load_tool( $name );
143  
144 Loads the named L<Devel::MAT::Tool> class.
145  
146 =cut
147  
148 10 ▶sub load_tool ( $self, $name, %args )
Subroutine load_tool: called CC=2 SCAR=7.0
Pod documented
149 {
150 # Ensure tools are 'require'd
151 3861 $self->available_tools;
152  
153 3861 my $tool_class = "Devel::MAT::Tool::$name";
154 3861 ▶ return $self->{tools}{$name} ||= $tool_class->new( $self, %args );
Condition66%2/3
$self->{"tools"}{$name} ||= $tool_class->new($self, %args)
l!l&&r!l&&!r
385650
MC/DC0%0/2
$self->{"tools"}{$name} ||= $tool_class->new($self, %args)
$self->{"tools"}{$name} $tool_class->new($self, %args)
Truth table66%2/3
A ||= B
A= $self->{"tools"}{$name}B= $tool_class->new($self, %args)
ABresult
1X1
011
000
155 }
156  
157 0 ▶sub load_tool_for_command ( $self, $cmd, %args )
Subroutine load_tool_for_command: not called CC=3 SCAR=24.8
Pod undocumented
158 {
159 0 ▶ return $self->{tools_by_command}{$cmd} ||= do {
Condition0%0/3
$self->{"tools_by_command"}{$cmd} ||= do { my $name = &first(sub { my $class = "Devel::MAT::Tool::$_"; $class->can("CMD") and $class->CMD eq $cmd; } , $self->available_tools) or die "Unrecognised command '${cmd}'\n"; $self->load_tool($name, %args) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"tools_by_command"}{$cmd} ||= do { my $name = &first(sub { my $class = "Devel::MAT::Tool::$_"; $class->can("CMD") and $class->CMD eq $cmd; } , $self->available_tools) or die "Unrecognised command '${cmd}'\n"; $self->load_tool($name, %args) }
$self->{"tools_by_command"}{$cmd} do { my $name = &first(sub { my $class = "Devel::MAT::Tool::$_"; $class->can("CMD") and $class->CMD eq $cmd; } , $self->available_tools) or die "Unrecognised command '${cmd}'\n"; $self->load_tool($name, %args) }
Truth table0%0/3
A ||= B
A= $self->{"tools_by_command"}{$cmd}B= do { my $name = &first(sub { my $class = "Devel::MAT::Tool::$_"; $class->can("CMD") and $class->CMD eq $cmd; } , $self->available_tools) or die "Unrecognised command '${cmd}'\n"; $self->load_tool($name, %args) }
ABresult
1X1
011
000
160 0 ▶ my $name = first {
Subroutine __ANON__: not called CC=2 SCAR=17.9
161 0 my $class = "Devel::MAT::Tool::$_";
162 0 ▶ $class->can( "CMD" ) and $class->CMD eq $cmd
Branch: 1 branch
BranchTrueFalseTotal
$class->can("CMD") and $class->CMD eq $cmd 0 0 0
163 0 ▶ } $self->available_tools or die "Unrecognised command '$cmd'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $name = &first(sub { my $class = "Devel::MAT::Tool::$_"; $class->can("CMD") and $class->CMD eq $cmd; } , $self->available_tools) or die "Unrecognised command '${cmd}'\n" 0 0 0
164  
165 0 $self->load_tool( $name, %args );
166 };
167 }
168  
169 =head2 has_tool
170  
171 $bool = $pmat->has_tool( $name );
172  
173 Returns true if the named tool is already loaded.
174  
175 =cut
176  
177 0 ▶sub has_tool ( $self, $name )
Subroutine has_tool: not called CC=1 SCAR=6.9
Pod documented
178 {
179 0 return defined $self->{tools}{$name};
180 }
181  
182 =head2 run_command
183  
184 $pmat->run_command( $inv );
185  
186 Runs a tool command given by the L<Commandable::Invocation> instance.
187  
188 =cut
189  
190 0 ▶sub run_command ( $self, $inv, %args )
Subroutine run_command: not called CC=1 SCAR=6.9
Pod documented
191 {
192 0 my $cmd = $inv->pull_token;
193  
194 $self->load_tool_for_command( $cmd,
195 progress => $args{process},
196 0 )->run_cmd( $inv );
197 }
198  
199 =head2 inref_graph
200  
201 $node = $pmat->inref_graph( $sv, %opts );
202  
203 Traces the tree of inrefs from C<$sv> back towards the known roots, returning
204 a L<Devel::MAT::Graph> node object representing it, within a graph of reverse
205 references back to the known roots.
206  
207 This method will load L<Devel::MAT::Tool::Inrefs> if it isn't yet loaded.
208  
209 The following named options are recognised:
210  
211 =over 4
212  
213 =item depth => INT
214  
215 If specified, stop recursing after the specified count. A depth of 1 will only
216 include immediately referring SVs, 2 will print the referrers of those, etc.
217 Nodes with inrefs that were trimmed because of this limit will appear to be
218 roots with a special name of C<EDEPTH>.
219  
220 =item strong => BOOL
221  
222 =item direct => BOOL
223  
224 Specifies the type of inrefs followed. By default all inrefs are followed.
225 Passing C<strong> will follow only strong direct inrefs. Passing C<direct>
226 will follow only direct inrefs.
227  
228 =item elide => BOOL
229  
230 If true, attempt to neaten up the output by skipping over certain structures.
231  
232 C<REF()>-type SVs will be skipped to their referrant.
233  
234 Members of the symbol table will be printed as being a 'root' element of the
235 given symbol name.
236  
237 C<PAD>s and C<PADLIST>s will be skipped to their referring C<CODE>, giving
238 shorter output for lexical variables.
239  
240 =back
241  
242 =cut
243  
244 3857 ▶sub inref_graph ( $self, $sv, %opts )
Subroutine inref_graph: called CC=28 SCAR=34.9
Pod documented
245 {
246 3857 ▶ my $graph = $opts{graph} //= Devel::MAT::Graph->new( $self->dumpfile );
Condition66%2/3
$opts{"graph"} //= "Devel::MAT::Graph"->new($self->dumpfile)
l!l&&r!l&&!r
385520
MC/DC0%0/2
$opts{"graph"} //= "Devel::MAT::Graph"->new($self->dumpfile)
$opts{"graph"} "Devel::MAT::Graph"->new($self->dumpfile)
Truth table66%2/3
A //= B
A= $opts{"graph"}B= "Devel::MAT::Graph"->new($self->dumpfile)
ABresult
1X1
011
000
247  
248 # TODO: allow separate values for these
249 3857 my $elide_rv = $opts{elide};
250 3857 my $elide_sym = $opts{elide};
251 3857 my $elide_pad = $opts{elide};
252  
253 3857 $self->load_tool( "Inrefs" );
254  
255 3857 ▶ if( $sv->immortal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->immortal) 0 3857 3857
256 0 ▶ my $desc = $sv->type eq "UNDEF" ? "undef" :
Branch: 2 branches
BranchTrueFalseTotal
$sv->type eq 'UNDEF' ? : 0 0 0
$sv->uv ? : 0 0 0
257 $sv->uv ? "true" :
258 "false";
259 0 $graph->add_root( $sv,
260 Devel::MAT::SV::Reference( $desc, strong => undef ) );
261 0 return $graph->get_sv_node( $sv );
262 }
263  
264 3857 my $name;
265 my $foundsv;
266 3857 ▶ if( $elide_sym and $name = $sv->symname and
Branch: 1 branch
BranchTrueFalseTotal
if ($elide_sym and $name = $sv->symname and not $name =~ /^&.*::__ANON__$/u and $foundsv = eval { do { $self->find_symbol($sv->symname) } } and $foundsv->addr == $sv->addr) 1 3856 3857
Condition83%10/12
$elide_sym and $name = $sv->symname and not $name =~ /^&.*::__ANON__$/u and $foundsv = eval { do { $self->find_symbol($sv->symname) } } and $foundsv->addr == $sv->addr
!ll&&!rl&&r
385601
$elide_sym and $name = $sv->symname and not $name =~ /^&.*::__ANON__$/u and $foundsv = eval { do { $self->find_symbol($sv->symname) } }
!ll&&!rl&&r
3846101
$elide_sym and $name = $sv->symname and not $name =~ /^&.*::__ANON__$/u
!ll&&!rl&&r
3846011
$elide_sym and $name = $sv->symname
!ll&&!rl&&r
3841511
MC/DC60%3/5
$elide_sym and $name = $sv->symname and not $name =~ /^&.*::__ANON__$/u and $foundsv = eval { do { $self->find_symbol($sv->symname) } } and $foundsv->addr == $sv->addr
$elide_sym $name = $sv->symname not $name =~ /^&.*::__ANON__$/u $foundsv = eval { do { $self->find_symbol($sv->symname) } } $foundsv->addr == $sv->addr
Truth table66%4/6
A and B and not(C) and D and E
A= $elide_symB= $name = $sv->symnameC= not $name =~ /^&.*::__ANON__$/uD= $foundsv = eval { do { $self->find_symbol($sv->symname) } }E= $foundsv->addr == $sv->addr
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
267 $name !~ m/^&.*::__ANON__$/ and
268 11 $foundsv = eval { $self->find_symbol( $sv->symname ) } and
269 $foundsv->addr == $sv->addr
270 ) {
271 1 $graph->add_root( $sv,
272 Devel::MAT::SV::Reference( "the symbol '" . Devel::MAT::Cmd->format_symbol( $name, $sv ) . "'", strong => undef ) );
273 1 return $graph->get_sv_node( $sv );
274 }
275 3856 ▶ if( $elide_sym and $sv->type eq "GLOB" and $name = $sv->stashname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elide_sym and $sv->type eq "GLOB" and $name = $sv->stashname) 0 3856 3856
Condition50%3/6
$elide_sym and $sv->type eq "GLOB" and $name = $sv->stashname
!ll&&!rl&&r
385600
$elide_sym and $sv->type eq "GLOB"
!ll&&!rl&&r
3841150
MC/DC0%0/3
$elide_sym and $sv->type eq "GLOB" and $name = $sv->stashname
$elide_sym $sv->type eq "GLOB" $name = $sv->stashname
Truth table50%2/4
A and B and C
A= $elide_symB= $sv->type eq "GLOB"C= $name = $sv->stashname
ABCresult
0XX0
10X0
1100
1111
276 0 $graph->add_root( $sv,
277 Devel::MAT::SV::Reference( "the glob '" . Devel::MAT::Cmd->format_symbol( "*$name", $sv ) . '"', strong => undef ) );
278 0 return $graph->get_sv_node( $sv );
279 }
280  
281 3856 $graph->add_sv( $sv );
282  
283 my @inrefs = $opts{strong} ? $sv->inrefs_strong :
284 3856 ▶ $opts{direct} ? $sv->inrefs_direct :
Branch: 2 branches
BranchTrueFalseTotal
$opts{'strong'} ? : 15 3841 3856
$opts{'direct'} ? : 0 3841 3841
285 $sv->inrefs;
286  
287 # If we didn't find anything at the given option level, try harder
288 3856 ▶ if( !@inrefs and $opts{strong} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @inrefs and $opts{"strong"}) 1 3855 3856
Condition100%3/3
not @inrefs and $opts{"strong"}
!ll&&!rl&&r
385411
MC/DC100%2/2
not @inrefs and $opts{"strong"}
not @inrefs $opts{"strong"}
Truth table100%3/3
not(A) and B
A= not @inrefsB= $opts{"strong"}
ABresult
0X0
100
111
289 1 @inrefs = $sv->inrefs_direct;
290 }
291 3856 ▶ if( !@inrefs and $opts{direct} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @inrefs and $opts{"direct"}) 1 3855 3856
Condition100%3/3
not @inrefs and $opts{"direct"}
!ll&&!rl&&r
385411
MC/DC100%2/2
not @inrefs and $opts{"direct"}
not @inrefs $opts{"direct"}
Truth table100%3/3
not(A) and B
A= not @inrefsB= $opts{"direct"}
ABresult
0X0
100
111
292 1 @inrefs = $sv->inrefs;
293 }
294  
295 3856 ▶ if( $elide_rv ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elide_rv) 15 3841 3856
296 15 ▶ @inrefs = map { sub () {
Subroutine __ANON__: called CC=7 SCAR=20.4
297 26 ▶ return $_ unless $_->sv and
Branch: 1 branch
BranchTrueFalseTotal
unless $_->sv and $_->sv->type eq "REF" and $_->name eq "the referrant" 22 4 26
Condition83%5/6
$_->sv and $_->sv->type eq "REF" and $_->name eq "the referrant"
!ll&&!rl&&r
2204
$_->sv and $_->sv->type eq "REF"
!ll&&!rl&&r
2204
MC/DC66%2/3
$_->sv and $_->sv->type eq "REF" and $_->name eq "the referrant"
$_->sv $_->sv->type eq "REF" $_->name eq "the referrant"
Truth table75%3/4
A and B and C
A= $_->svB= $_->sv->type eq "REF"C= $_->name eq "the referrant"
ABCresult
0XX0
10X0
1100
1111
298 $_->sv->type eq "REF" and
299 $_->name eq "the referrant";
300  
301 4 my $rv = $_->sv;
302 my @rvrefs = $opts{strong} ? $rv->inrefs_strong :
303 4 ▶ $opts{direct} ? $rv->inrefs_direct :
Branch: 2 branches
BranchTrueFalseTotal
$opts{'strong'} ? : 4 0 4
$opts{'direct'} ? : 0 0 0
304 $rv->inrefs;
305  
306 4 ▶ return $_ unless @rvrefs == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @rvrefs == 1 0 4 4
307  
308 # Add 'via RV' marker
309 return map {
310 4 Devel::MAT::SV::Reference( Devel::MAT::Cmd->format_note( "(via RV)" ) . " " . $_->name,
311 $_->strength, $_->sv )
312 } @rvrefs;
313 26 }->() } @inrefs;
314 }
315  
316 3856 ▶ if( $elide_pad ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($elide_pad) 15 3841 3856
317 15 ▶ @inrefs = map { sub () {
Subroutine __ANON__: called CC=4 SCAR=13.9
318 26 ▶ return $_ unless $_->sv and
Branch: 1 branch
BranchTrueFalseTotal
unless $_->sv and $_->sv->type eq "PAD" 25 1 26
Condition100%3/3
$_->sv and $_->sv->type eq "PAD"
!ll&&!rl&&r
2231
MC/DC100%2/2
$_->sv and $_->sv->type eq "PAD"
$_->sv $_->sv->type eq "PAD"
Truth table100%3/3
A and B
A= $_->svB= $_->sv->type eq "PAD"
ABresult
0X0
100
111
319 $_->sv->type eq "PAD";
320 1 my $pad = $_->sv;
321 1 my $cv = $pad->padcv;
322 # Even if the CV isn't active, this might be a state variable so we
323 # must always consider pad(1) at least.
324 1 ▶ my ( $depth ) = grep { $cv->pad( $_ ) == $pad } ( 1 .. ( $cv->depth || 1 ) );
Condition50%1/2
$cv->depth || 1
l!l
10
MC/DC0%0/1
$cv->depth || 1
$cv->depth
Truth table50%1/2
A
A= $cv->depth
Aresult
11
00
325 1 return Devel::MAT::SV::Reference( $_->name . " at depth $depth", $_->strength, $cv );
326 26 }->() } @inrefs;
327 }
328  
329 3856 ▶ if( $sv->is_mortal ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sv->is_mortal) 0 3856 3856
330 0 $graph->add_root( $sv,
331 Devel::MAT::SV::Reference( "a mortal", strong => undef ) );
332 }
333  
334 3856 ▶ foreach my $ref ( sort_by { $_->name } @inrefs ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
335 11554 ▶ if( !defined $ref->sv ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref->sv) 4 11550 11554
336 4 $graph->add_root( $sv, $ref );
337 4 next;
338 }
339  
340 11550 ▶ if( defined $opts{depth} and not $opts{depth} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"depth"} and not $opts{"depth"}) 1916 9634 11550
Condition100%3/3
defined $opts{"depth"} and not $opts{"depth"}
!ll&&!rl&&r
2496101916
MC/DC100%2/2
defined $opts{"depth"} and not $opts{"depth"}
defined $opts{"depth"} not $opts{"depth"}
Truth table100%3/3
A and not(B)
A= defined $opts{"depth"}B= not $opts{"depth"}
ABresult
0X0
100
111
341 1916 $graph->add_root( $sv, "EDEPTH" );
342 1916 last;
343 }
344  
345 9634 my @me;
346 9634 ▶ if( $graph->has_sv( $ref->sv ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($graph->has_sv($ref->sv)) { } 5779 3855 9634
347 5779 $graph->add_ref( $ref->sv, $sv, $ref );
348 # Don't recurse into it as it was already found
349 }
350 else {
351 3855 $graph->add_sv( $ref->sv ); # add first to stop inf. loops
352  
353 3855 ▶ defined $opts{depth} ? $self->inref_graph( $ref->sv, %opts, depth => $opts{depth}-1 )
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'depth'} ? : 3840 15 3855
354 : $self->inref_graph( $ref->sv, %opts );
355 3855 $graph->add_ref( $ref->sv, $sv, $ref );
356 }
357 }
358  
359 3856 return $graph->get_sv_node( $sv );
360 }
361  
362 =head2 find_symbol
363  
364 $sv = $pmat->find_symbol( $name );
365  
366 Attempts to walk the symbol table looking for a symbol of the given name,
367 which must include the sigil.
368  
369 =for highlighter
370  
371 $Package::Name::symbol_name => to return a SCALAR SV
372 @Package::Name::symbol_name => to return an ARRAY SV
373 %Package::Name::symbol_name => to return a HASH SV
374 &Package::Name::symbol_name => to return a CODE SV
375  
376 =for highlighter language=perl
377  
378 =cut
379  
380 31 ▶sub find_symbol ( $self, $name )
Subroutine find_symbol: called CC=10 SCAR=24.1
Pod documented
381 {
382 31 ▶ my ( $sigil, $globname ) = $name =~ m/^([\$\@%&])(.*)$/ or
Branch: 1 branch
BranchTrueFalseTotal
() = $name =~ /^([\$\@%&])(.*)$/u or croak("Could not parse sigil from $name") 31 0 31
383 croak "Could not parse sigil from $name";
384  
385 31 my $stashvalue = $self->find_stashvalue( $globname );
386  
387 # Perl 5.22 may take CODE shortcuts
388 31 ▶ if( $sigil eq '&' and $stashvalue->type eq "REF" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sigil eq "&" and $stashvalue->type eq "REF") 0 31 31
Condition66%2/3
$sigil eq "&" and $stashvalue->type eq "REF"
!ll&&!rl&&r
19120
MC/DC0%0/2
$sigil eq "&" and $stashvalue->type eq "REF"
$sigil eq "&" $stashvalue->type eq "REF"
Truth table66%2/3
A and B
A= $sigil eq "&"B= $stashvalue->type eq "REF"
ABresult
0X0
100
111
389 0 return $stashvalue->rv;
390 }
391  
392 31 ▶ $stashvalue->type eq "GLOB" or
Branch: 1 branch
BranchTrueFalseTotal
$stashvalue->type eq "GLOB" or croak("$globname is not a GLOB") 31 0 31
393 croak "$globname is not a GLOB";
394  
395 31 ▶ my $slot = ( $sigil eq '$' ) ? "scalar" :
Branch: 4 branches
BranchTrueFalseTotal
$sigil eq '$' ? : 11 20 31
$sigil eq '@' ? : 6 14 20
$sigil eq '%' ? : 2 12 14
$sigil eq '&' ? : 12 0 12
396 ( $sigil eq '@' ) ? "array" :
397 ( $sigil eq '%' ) ? "hash" :
398 ( $sigil eq '&' ) ? "code" :
399 die "ARGH"; # won't happen
400  
401 31 ▶ my $sv = $stashvalue->$slot or
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $stashvalue->$slot or croak("*$globname has no $slot slot") 21 10 31
402 croak "\*$globname has no $slot slot";
403 21 return $sv;
404 }
405  
406 =head2 find_glob
407  
408 $gv = $pmat->find_glob( $name );
409  
410 Attempts to walk the symbol table looking for a symbol of the given name,
411 returning the C<GLOB> object if found.
412  
413 =head2 find_stash
414  
415 $stash = $pmat->find_stash( $name );
416  
417 Attempts to walk the symbol table looking for a stash of the given name.
418  
419 =cut
420  
421 52 ▶sub find_stashvalue ( $self, $name )
Subroutine find_stashvalue: called CC=4 SCAR=13.9
Pod undocumented
422 {
423 52 my ( $parent, $shortname ) = $name =~ m/^(?:(.*)::)?(.+?)$/;
424  
425 52 my $stash;
426 52 ▶ if( defined $parent and length $parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and length $parent) { } 15 37 52
Condition100%3/3
defined $parent and length $parent
!ll&&!rl&&r
36115
MC/DC100%2/2
defined $parent and length $parent
defined $parent length $parent
Truth table100%3/3
A and B
A= defined $parentB= length $parent
ABresult
0X0
100
111
427 15 $stash = $self->find_stash( $parent );
428 }
429 else {
430 37 $stash = $self->dumpfile->defstash;
431 }
432  
433 52 ▶ my $sv = $stash->value( $shortname ) or
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $stash->value($shortname) or croak($stash->stashname . " has no symbol $shortname") 52 0 52
434 croak $stash->stashname . " has no symbol $shortname";
435 52 return $sv;
436 }
437  
438 21 ▶sub find_glob ( $self, $name )
Subroutine find_glob: called CC=3 SCAR=11.7
Pod documented
439 {
440 21 ▶ my $sv = $self->find_stashvalue( $name ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sv = $self->find_stashvalue($name) or return 21 0 21
441 21 ▶ $sv->type eq "GLOB" or
Branch: 1 branch
BranchTrueFalseTotal
$sv->type eq "GLOB" or croak("$name is not a GLOB") 21 0 21
442 croak "$name is not a GLOB";
443  
444 21 return $sv;
445 }
446  
447 16 ▶sub find_stash ( $self, $name )
Subroutine find_stash: called CC=2 SCAR=7.2
Pod documented
448 {
449 16 my $gv = $self->find_glob( $name . "::" );
450 16 ▶ return $gv->hash ||
Condition50%1/2
$gv->hash || croak("$name has no hash")
l!l
160
MC/DC0%0/1
$gv->hash || croak("$name has no hash")
$gv->hash
Truth table50%1/2
A
A= $gv->hash
Aresult
11
00
451 croak "$name has no hash";
452 }
453  
454 # Some base implementations of Devel::MAT::Cmd formatters
455  
456 10 push @Devel::MAT::Cmd::ISA, qw( Devel::MAT::Cmd::_base );
457  
458 package
459 Devel::MAT::Cmd::_base;
460  
461 10 ▶use B qw( perlstring );
Subroutine BEGIN: called CC=1 SCAR=0.0
462 10 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
463  
464 6 ▶sub print_table ( $self, $rows, %opts )
Subroutine print_table: called CC=17 SCAR=28.6
465 {
466 6 ▶ if( $opts{headings} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"headings"}) 1 5 6
467 1 my @headings = map { $self->format_heading( $_ ) } $opts{headings}->@*;
468 1 $rows = [ \@headings, @$rows ];
469 }
470  
471 6 ▶ return unless @$rows;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rows 0 6 6
472  
473 6 my $cols = max map { scalar @$_ } @$rows;
474  
475 my @colwidths = map {
476 6 my $colidx = $_;
477 # TODO: consider a unicode/terminal-aware version of length here
478 17 ▶ max map { length($_->[$colidx]) // 0 } @$rows;
Condition50%1/2
length $_->[$colidx] // 0
l!l
250
MC/DC0%0/1
length $_->[$colidx] // 0
length $_->[$colidx]
Truth table50%1/2
A
A= length $_->[$colidx]
Aresult
11
00
479 } 0 .. $cols-1;
480  
481 6 ▶ my $align = $opts{align} // "";
Condition100%2/2
$opts{"align"} // ""
l!l
15
MC/DC100%1/1
$opts{"align"} // ""
$opts{"align"}
Truth table100%2/2
A
A= $opts{"align"}
Aresult
11
00
482 6 ▶ $align = [ ( $align ) x $cols ] if !ref $align;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $align 5 1 6
483  
484 6 ▶ my $sep = $opts{sep} // " ";
Condition100%2/2
$opts{"sep"} // " "
l!l
15
MC/DC100%1/1
$opts{"sep"} // " "
$opts{"sep"}
Truth table100%2/2
A
A= $opts{"sep"}
Aresult
11
00
485 6 ▶ $sep = [ ( $sep ) x ($cols - 1) ] if !ref $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sep 6 0 6
486  
487 6 ▶ my @leftalign = map { ($align->[$_]//"") ne "right" } 0 .. $cols-1;
Condition100%2/2
$align->[$_] // ""
l!l
152
MC/DC100%1/1
$align->[$_] // ""
$align->[$_]
Truth table100%2/2
A
A= $align->[$_]
Aresult
11
00
488  
489 my $format = join( "",
490 ( " " x ( $opts{indent} // 0 ) ),
491 ( map {
492 6 ▶ my $col = $_;
Condition100%2/2
$opts{"indent"} // 0
l!l
15
MC/DC100%1/1
$opts{"indent"} // 0
$opts{"indent"}
Truth table100%2/2
A
A= $opts{"indent"}
Aresult
11
00
493 17 my $width = $colwidths[$col];
494 17 ▶ my $flags = $leftalign[$col] ? "-" : "";
Branch: 1 branch
BranchTrueFalseTotal
$leftalign[$col] ? : 16 1 17
495 # If final column should be left-aligned don't bother with width
496 17 ▶ $width = "" if $col == $cols-1 and $leftalign[$col];
Branch: 1 branch
BranchTrueFalseTotal
if $col == $cols - 1 and $leftalign[$col] 6 11 17
Condition66%2/3
$col == $cols - 1 and $leftalign[$col]
!ll&&!rl&&r
1106
MC/DC50%1/2
$col == $cols - 1 and $leftalign[$col]
$col == $cols - 1 $leftalign[$col]
Truth table66%2/3
A and B
A= $col == $cols - 1B= $leftalign[$col]
ABresult
0X0
100
111
497  
498 17 ▶ ( $col ? $sep->[$col-1] : "" ) . "%${flags}${width}s"
Branch: 1 branch
BranchTrueFalseTotal
$col ? : 11 6 17
499 } 0 .. $cols-1 ),
500 ) . "\n";
501  
502 6 foreach my $row ( @$rows ) {
503 9 my @row = @$row;
504 0 ▶ @row or @row = map { "-"x$colwidths[$_] } ( 0 .. $cols-1 );
Branch: 1 branch
BranchTrueFalseTotal
@row or @row = map({"-" x $colwidths[$_];} 0 .. $cols - 1) 9 0 9
505 9 ▶ push @row, "" while @row < $cols; # pad with spaces
Branch: 1 branch
BranchTrueFalseTotal
@row < $cols and do { push @row, "" } 0 9 9
506 9 $self->printf( $format, @row );
507 }
508 }
509  
510 119 ▶sub format_note ( $, $str, $idx = 1 )
Subroutine format_note: called CC=2 SCAR=6.9
511 {
512 119 return $str;
513 }
514  
515 4 ▶sub _format_sv ( $, $ret, $sv )
Subroutine _format_sv: called CC=1 SCAR=0.0
516 {
517 4 return $ret;
518 }
519  
520 4 ▶sub _format_addr ( $, $addr )
Subroutine _format_addr: called CC=1 SCAR=0.0
521 {
522 4 return sprintf "%#x", $addr;
523 }
524  
525 4 ▶sub format_sv ( $, $sv )
Subroutine format_sv: called CC=3 SCAR=11.2
526 {
527 4 my $ret = $sv->desc;
528  
529 4 ▶ if( my $blessed = $sv->blessed ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $blessed = $sv->blessed) 0 4 4
530 0 $ret .= "=" . Devel::MAT::Cmd->format_symbol( $blessed->stashname, $blessed );
531 }
532  
533 4 $ret .= " at " . Devel::MAT::Cmd->_format_addr( $sv->addr );
534  
535 4 ▶ if( my $rootname = $sv->rootname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rootname = $sv->rootname) 2 2 4
536 2 $ret .= "=" . Devel::MAT::Cmd->format_note( $rootname, 1 );
537 }
538  
539 4 return Devel::MAT::Cmd->_format_sv( $ret, $sv );
540 }
541  
542 8594193 ▶sub _format_value ( $, $val )
Subroutine _format_value: called CC=1 SCAR=0.0
543 {
544 8594193 return $val;
545 }
546  
547 8594193 ▶sub format_value ( $, $val, %opts )
Subroutine format_value: called CC=12 SCAR=38.4
548 {
549 8594193 my $text;
550 8594193 ▶ if( $opts{key} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opts{'key'}) { } 2824053 5770140 8594193
elsif ($opts{'index'}) { } 5770140 0 5770140
elsif ($opts{'addr'}) { } 0 0 0
elsif ($opts{'pv'}) { } 0 0 0
551 2824053 my $strval = $val;
552 2824053 ▶ if( $opts{stash} && $strval =~ m/^([\x00-\x1f])([a-zA-Z0-9_]*)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'stash'} and $strval =~ /^([\x00-\x1f])([a-zA-Z0-9_]*)$/u) { } 0 2824053 2824053
elsif (not $strval =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/u) { } 707231 2116822 2824053
Condition33%1/3
$opts{"stash"} and $strval =~ /^([\x00-\x1f])([a-zA-Z0-9_]*)$/u
!ll&&!rl&&r
282405300
MC/DC0%0/2
$opts{"stash"} and $strval =~ /^([\x00-\x1f])([a-zA-Z0-9_]*)$/u
$opts{"stash"} $strval =~ /^([\x00-\x1f])([a-zA-Z0-9_]*)$/u
Truth table33%1/3
A and B
A= $opts{"stash"}B= $strval =~ /^([\x00-\x1f])([a-zA-Z0-9_]*)$/u
ABresult
0X0
100
111
553 0 $strval = "^" . chr( 64 + ord $1 ) . $2;
554 }
555 elsif( $strval !~ m/^[a-zA-Z_][a-zA-Z0-9_]*$/ ) {
556 707231 $strval = perlstring( $val );
557 }
558  
559 2824053 return "{" . Devel::MAT::Cmd->_format_value( $strval ) . "}";
560 }
561 elsif( $opts{index} ) {
562 5770140 return "[" . Devel::MAT::Cmd->_format_value( $val+0 ) . "]";
563 }
564 elsif( $opts{addr} ) {
565 0 return Devel::MAT::Cmd->_format_addr( $val );
566 }
567 elsif( $opts{pv} ) {
568 0 my $truncated;
569 0 ▶ if( my $maxlen = $opts{maxlen} // 64 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $maxlen = $opts{"maxlen"} // 64) 0 0 0
Condition0%0/2
$opts{"maxlen"} // 64
l!l
00
MC/DC0%0/1
$opts{"maxlen"} // 64
$opts{"maxlen"}
Truth table0%0/2
A
A= $opts{"maxlen"}
Aresult
11
00
570 0 ▶ ( $truncated = length $val > $maxlen ) and
Branch: 1 branch
BranchTrueFalseTotal
$truncated = length $val > $maxlen and substr($val, $maxlen) = "" 0 0 0
571 substr( $val, $maxlen ) = "";
572 }
573  
574 0 ▶ return Devel::MAT::Cmd->_format_value(
Branch: 1 branch
BranchTrueFalseTotal
$truncated ? : 0 0 0
575 perlstring( $val ) . ( $truncated ? "..." : "" )
576 );
577 }
578 else {
579 0 return Devel::MAT::Cmd->_format_value( $val );
580 }
581 }
582  
583 1 ▶sub format_symbol ( $, $name, $sv )
Subroutine format_symbol: called CC=1 SCAR=0.0
584 {
585 1 return $name;
586 }
587  
588 0 ▶sub format_bytes ( $, $bytes )
Subroutine format_bytes: not called CC=5 SCAR=34.0
589 {
590 0 ▶ if( $bytes < 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < 1024) 0 0 0
591 0 return sprintf "%d bytes", $bytes;
592 }
593 0 ▶ if( $bytes < 1024**2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < 1048576) 0 0 0
594 0 return sprintf "%.1f KiB", $bytes / 1024;
595 }
596 0 ▶ if( $bytes < 1024**3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < "1073741824") 0 0 0
597 0 return sprintf "%.1f MiB", $bytes / 1024**2;
598 }
599 0 ▶ if( $bytes < 1024**4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes < "1099511627776") 0 0 0
600 0 return sprintf "%.1f GiB", $bytes / 1024**3;
601 }
602 0 return sprintf "%.1f TiB", $bytes / 1024**4;
603 }
604  
605 0 ▶sub format_sv_with_value ( $self, $sv )
Subroutine format_sv_with_value: not called CC=21 SCAR=61.4
606 {
607 0 my $repr = $self->format_sv( $sv );
608  
609 match( $sv->type : eq ) {
610 case( "SCALAR" ) {
611 0 my @reprs;
612  
613 my $num;
614 0 ▶ defined( $num = $sv->nv // $sv->uv ) and
Condition0%0/6
defined($num = $sv->nv // $sv->uv) and push @reprs, $self->format_value($num, "nv", 1)
!ll&&!rl&&r
000
$sv->nv // $sv->uv
l!l&&r!l&&!r
000
MC/DC0%0/3
defined($num = $sv->nv // $sv->uv) and push @reprs, $self->format_value($num, "nv", 1)
$sv->nv $sv->uv push @reprs, $self->format_value($num, "nv", 1)
Truth table0%0/5
A // B and C
A= $sv->nvB= $sv->uvC= push @reprs, $self->format_value($num, "nv", 1)
ABCresult
00X0
1X00
0100
1X11
0111
615 push @reprs, $self->format_value( $num, nv => 1 );
616  
617 0 ▶ defined $sv->pv and
Condition0%0/3
defined $sv->pv and push @reprs, $self->format_value($sv->pv, "pv", 1)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sv->pv and push @reprs, $self->format_value($sv->pv, "pv", 1)
defined $sv->pv push @reprs, $self->format_value($sv->pv, "pv", 1)
Truth table0%0/3
A and B
A= defined $sv->pvB= push @reprs, $self->format_value($sv->pv, "pv", 1)
ABresult
0X0
100
111
618 push @reprs, $self->format_value( $sv->pv, pv => 1 );
619  
620 # Dualvars
621 0 ▶ return "$repr = $reprs[0] / $reprs[1]" if @reprs > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @reprs > 1 0 0 0
622  
623 0 ▶ return "$repr = $reprs[0]" if @reprs;
Branch: 2 branches
BranchTrueFalseTotal
if @reprs 0 0 0
$(Syntax::Keyword::Match/topic) eq 'BOOL' ? : 0 0 0
624 }
625 case( "BOOL" ) {
626 0 ▶ return "$repr = " . $self->format_value( $sv->uv ? "true" : "false" );
Branch: 2 branches
BranchTrueFalseTotal
$sv->uv ? : 0 0 0
$(Syntax::Keyword::Match/topic) eq 'REF' ? : 0 0 0
627 }
628 case( "REF" ) {
629 #return "REF => NULL" if !$sv->rv;
630 0 ▶ return "$repr => " . $self->format_sv_with_value( $sv->rv ) if $sv->rv;
Branch: 2 branches
BranchTrueFalseTotal
if $sv->rv 0 0 0
$(Syntax::Keyword::Match/topic) eq 'ARRAY' ? : 0 0 0
631 }
632 case( "ARRAY" ) {
633 0 ▶ return $repr if $sv->blessed;
Branch: 1 branch
BranchTrueFalseTotal
if $sv->blessed 0 0 0
634  
635 0 my $n_elems = $sv->elems;
636 0 ▶ return "$repr = []" if !$n_elems;
Branch: 1 branch
BranchTrueFalseTotal
unless $n_elems 0 0 0
637  
638 0 my $elem = $self->format_sv( $sv->elem( 0 ) );
639 0 ▶ $elem .= ", ..." if $n_elems > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n_elems > 1 0 0 0
640  
641 0 ▶ return "$repr = [$elem]";
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'HASH' ? : 0 0 0
642 }
643 case( "HASH" ) {
644 0 ▶ return $repr if $sv->blessed;
Branch: 1 branch
BranchTrueFalseTotal
if $sv->blessed 0 0 0
645  
646 0 my $n_values = $sv->values;
647 0 ▶ return "$repr = {}" if !$n_values;
Branch: 1 branch
BranchTrueFalseTotal
unless $n_values 0 0 0
648  
649 0 my $key = ( $sv->keys )[0]; # pick one at random
650 0 my $value = $self->format_value( $key, key => 1 ) . " => " . $self->format_sv( $sv->value( $key ) );
651 0 ▶ $value .= ", ..." if $n_values > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n_values > 1 0 0 0
652  
653 0 ▶ return "$repr = {$value}";
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'GLOB' ? : 0 0 0
654 }
655 case( "GLOB" ) {
656 0 ▶ return "$repr is " . $self->format_symbol( "*" . $sv->stashname, $sv );
Condition0%0/3
$(Syntax::Keyword::Match/topic) eq "STASH" and do { return "$repr is " . $self->format_symbol($sv->stashname, $sv) }
!ll&&!rl&&r
000
MC/DC0%0/2
$(Syntax::Keyword::Match/topic) eq "STASH" and do { return "$repr is " . $self->format_symbol($sv->stashname, $sv) }
$(Syntax::Keyword::Match/topic) eq "STASH" do { return "$repr is " . $self->format_symbol($sv->stashname, $sv) }
Truth table0%0/3
A and B
A= $(Syntax::Keyword::Match/topic) eq "STASH"B= do { return "$repr is " . $self->format_symbol($sv->stashname, $sv) }
ABresult
0X0
100
111
657 }
658 0 ▶ case( "STASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
$(Syntax::Keyword::Match/topic) eq 'SCALAR' ? : 0 0 0
659 0 return "$repr is " . $self->format_symbol( $sv->stashname, $sv );
660 }
661 }
662  
663 0 return $repr;
664 }
665  
666 sub format_heading
667 0 ▶{
Subroutine format_heading: not called CC=1 SCAR=6.9
668 0 shift;
669 0 my ( $text, $level ) = @_;
670  
671 0 return "$text";
672 }
673  
674 =head1 AUTHOR
675  
676 Paul Evans <leonerd@leonerd.org.uk>
677  
678 =cut
679  
680 10 0x55AA;
« blib/lib/Devel/MAT/InternalTools.pm ↑ Summary blib/lib/Devel/MAT/Tool/Strtab.pm »