blib/lib/Devel/Module/Trace.pm

filter: Statementstmt 54.3% 69 / 127 Branchbran 19.7% 13 / 66 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.4% 15 / 21 Podpod 75.0% 3 / 4 totaltotal 43.9% 102 / 232 CC 71 SCAR 34.3
CC
71
Cov
42%
print_pretty
90.0
print_pretty
90.0
_get_max_pp_size
42.0
CRAP 30.8
↑ Summary
1 ## no critic
2 package # hide package name from indexer
3 DB;
4 # allow -d:Devel::Module::Trace loading
5 0 ▶sub DB {}
Subroutine DB: not called CC=1 SCAR=0.0
Pod undocumented
6 ## use critic
7  
8 package Devel::Module::Trace;
9  
10 =head1 NAME
11  
12 Devel::Module::Trace - Trace module origins
13  
14 =head1 DESCRIPTION
15  
16 This module traces use/require statements to print the origins of loaded modules
17  
18 =head1 SYNOPSIS
19  
20 =over 4
21  
22 # load module
23 use Devel::Module::Trace;
24  
25 # load other modules
26 use Some::Other::Modules;
27 require Even::More::Modules;
28  
29 # output results
30 Devel::Module::Trace::print_pretty();
31  
32 # using directly
33 perl -d:Module::Trace=print -M<Module> -e exit
34  
35 =back
36  
37 =cut
38  
39 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use Devel::OverrideGlobalRequire;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 1 our $VERSION = '0.02';
46  
47 ################################################################################
48 1 ▶$Devel::Module::Trace::modules = [] unless defined $Devel::Module::Trace::modules;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::modules 1 0 1
49 1 my $cur_lvl = $Devel::Module::Trace::modules;
50 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶ use Time::HiRes qw/gettimeofday tv_interval time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 $^P = $^P | 0x400; # Save source code lines, see perldoc perlvar
53 };
54  
55 ################################################################################
56 1 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=19.9
57 1 ▶ $Devel::Module::Trace::print = 0 unless defined $Devel::Module::Trace::print;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::print 1 0 1
58 1 ▶ $Devel::Module::Trace::filter = [] unless defined $Devel::Module::Trace::filter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::filter 1 0 1
59 1 ▶ $Devel::Module::Trace::enabled = 0 unless defined $Devel::Module::Trace::enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::enabled 1 0 1
60 1 ▶ $Devel::Module::Trace::save = undef unless defined $Devel::Module::Trace::save;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::save 1 0 1
61 1 ▶ $Devel::Module::Trace::autostart = 1 unless defined $Devel::Module::Trace::autostart;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Devel::Module::Trace::autostart 1 0 1
62 }
63 1 ▶sub import {
Subroutine import: called CC=6 SCAR=33.1
64 1 my(undef, @options) = @_;
65 1 for my $option (@options) {
66 0 ▶ if($option eq 'print') {
Branch: 4 branches
BranchTrueFalseTotal
if ($option eq 'print') { } 0 0 0
elsif ($option eq 'noautostart') { } 0 0 0
elsif ($option =~ /filter=(.*)$/) { } 0 0 0
elsif ($option =~ /save=(.*)$/) { } 0 0 0
67 0 $Devel::Module::Trace::print = 1;
68 }
69 elsif($option eq 'noautostart') {
70 0 $Devel::Module::Trace::autostart = 0;
71 }
72 elsif($option =~ 'filter=(.*)$') {
73 0 my $filter = $1;
74 0 push @{$Devel::Module::Trace::filter}, $filter;
75 }
76 elsif($option =~ 'save=(.*)$') {
77 0 $Devel::Module::Trace::save = $1;
78 } else {
79 0 die("unknown option: ".$option);
80 }
81 }
82 1 return;
83 }
84  
85 ################################################################################
86  
87 =head1 METHODS
88  
89 =head2 raw_result
90  
91 raw_result()
92  
93 returns an array with the raw result list.
94  
95 =cut
96 1 ▶sub raw_result {
Subroutine raw_result: called CC=1 SCAR=0.0
Pod documented
97 1 return($Devel::Module::Trace::modules);
98 }
99  
100 ################################################################################
101  
102 =head2 save
103  
104 save(<filename>)
105  
106 save results to given file
107  
108 =cut
109 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod documented
110 0 my($file) = @_;
111 0 ▶ open(my $fh, '>', $file) or die("cannot write to $file: $!");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "cannot write to ${file}: $!" 0 0 0
112 0 print $fh Dumper({
113 result => raw_result(),
114 filter => $Devel::Module::Trace::filter,
115 script => $0,
116 });
117 0 close($fh);
118 0 print STDERR $file." written\n";
119 0 return;
120 }
121  
122 ################################################################################
123  
124 =head2 print_pretty
125  
126 print_pretty()
127  
128 prints the results as ascii table to STDERR.
129  
130 =cut
131 0 ▶sub print_pretty {
Subroutine print_pretty: not called CC=9 SCAR=45.0
Pod documented
132 0 my($raw, $indent, $max_module, $max_caller, $max_indent) = @_;
133 0 ▶ $raw = raw_result() unless $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw 0 0 0
134 0 ▶ if(!$indent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($indent) 0 0 0
135 0 $indent = 0;
136 # get max caller and module
137 0 ($max_module, $max_caller) = _get_max_pp_size(raw_result(), 0, 0, 0);
138 0 ▶ return if $max_module == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $max_module == 0 0 0 0
139 0 ▶ print " ","-"x($max_module+$max_caller+34), "\n" if $indent == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $indent == 0 0 0 0
140 }
141 0 for my $mod (@{$raw}) {
142 0 ▶ next if _filtered($mod->{'name'});
Branch: 1 branch
BranchTrueFalseTotal
if _filtered($mod->{"name"}) 0 0 0
143 0 my($time, $milliseconds) = split(/\./mx, $mod->{'time'});
144 printf(STDERR "| %s%08.5f | %-".$indent."s %-".($max_module-$indent)."s | %.6f | %-".$max_caller."s |\n",
145 POSIX::strftime("%H:%M:", localtime($time)),
146 POSIX::strftime("%S", localtime($time)).'.'.$milliseconds,
147 "",
148 $mod->{'name'},
149 $mod->{'elapsed'},
150 0 $mod->{'caller'},
151 );
152 0 ▶ if($mod->{'sub'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{"sub"}) 0 0 0
153 0 print_pretty($mod->{'sub'}, $indent+4, $max_module, $max_caller, $max_indent);
154 }
155 }
156 0 ▶ print " ","-"x($max_module+$max_caller+34), "\n" if $indent == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $indent == 0 0 0 0
157 0 return;
158 }
159  
160 ################################################################################
161 1 ▶sub _enable {
Subroutine _enable: called CC=1 SCAR=0.0
162 1 $Devel::Module::Trace::enabled = 1;
163 1 Devel::OverrideGlobalRequire::override_global_require(\&_trace_use);
164 1 return;
165 }
166  
167 ################################################################################
168 7 ▶sub _trace_use {
Subroutine _trace_use: called CC=9 SCAR=24.2
169 7 my($next_require,$module_name) = @_;
170 7 ▶ if(!$Devel::Module::Trace::enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Devel::Module::Trace::enabled) 6 1 7
171 6 return &{$next_require}();
172 }
173 1 my($p,$f,$l) = caller(1);
174 1 my $code;
175 {
176 ## no critics
177 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
178 1 $code = \@{"::_<$f"};
179 ## use critics
180 }
181 1 ▶ if(!$code->[$l]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code->[$l]) 0 1 1
182 0 return &{$next_require}();
183 }
184 1 my $code_str = $code->[$l];
185 1 my $i = $l-1;
186 # try to concatenate previous lines if statement was multilined
187 1 ▶ while($i > 0 && $code->[$i] && $code->[$i] !~ m/^(.*\}|.*\;|=cut)\s*$/mxo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 and $code->[$i] and not $code->[$i] =~ /^(.*\}|.*\;|=cut)\s*$/mox) 0 1 1
Condition33%2/6
$i > 0 and $code->[$i] and not $code->[$i] =~ /^(.*\}|.*\;|=cut)\s*$/mox
!ll&&!rl&&r
100
$i > 0 and $code->[$i]
!ll&&!rl&&r
100
MC/DC0%0/3
$i > 0 and $code->[$i] and not $code->[$i] =~ /^(.*\}|.*\;|=cut)\s*$/mox
$i > 0 $code->[$i] not $code->[$i] =~ /^(.*\}|.*\;|=cut)\s*$/mox
Truth table25%1/4
A and B and not(C)
A= $i > 0B= $code->[$i]C= not $code->[$i] =~ /^(.*\}|.*\;|=cut)\s*$/mox
ABCresult
0XX0
10X0
1100
1111
188 0 ▶ if($code->[$i] !~ m/^\s*$|^\s*\#/mxo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code->[$i] =~ /^\s*$|^\s*\#/mox) 0 0 0
189 0 $code_str = $code->[$i].$code_str;
190 }
191 0 $i--;
192 }
193 1 ▶ if($code_str !~ m/^\s*(use|require)/mxo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code_str =~ /^\s*(use|require)/mox) 0 1 1
194 0 return &{$next_require}();
195 }
196 1 my $mod = {
197 package => $p,
198 name => $module_name,
199 caller => $f.':'.$l,
200 caller_f => $f,
201 caller_l => $l,
202 time => time
203 };
204 1 my $t0 = [gettimeofday];
205 1 my $old_lvl = $cur_lvl;
206 1 $cur_lvl = [];
207 1 my $res = &{$next_require}();
208 1 my $elapsed = tv_interval($t0);
209 1 $mod->{'elapsed'} = $elapsed;
210 1 ▶ $mod->{'sub'} = $cur_lvl if scalar @{$cur_lvl};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$cur_lvl;} 0 1 1
211 1 $cur_lvl = $old_lvl;
212 1 push(@{$cur_lvl}, $mod);
213 1 return $res;
214 }
215  
216 ################################################################################
217 1 ▶sub _disable {
Subroutine _disable: called CC=1 SCAR=0.0
218 1 $Devel::Module::Trace::enabled = 0;
219 1 return;
220 }
221  
222 ################################################################################
223 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
224 1 ▶ _enable() if $Devel::Module::Trace::autostart;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Module::Trace::autostart 1 0 1
225 };
226  
227 ################################################################################
228 0 ▶sub _filtered {
Subroutine _filtered: not called CC=5 SCAR=34.0
229 0 my($mod) = @_;
230 0 for my $f (@{$Devel::Module::Trace::filter}) {
231 0 ▶ if($mod =~ m|$f|mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /$f/mx) 0 0 0
232 0 return(1);
233 }
234 0 ▶ if($f eq 'perl' && $mod =~ m|^[\d\.]+$|mx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq "perl" and $mod =~ /^[\d\.]+$/mx) 0 0 0
Condition0%0/3
$f eq "perl" and $mod =~ /^[\d\.]+$/mx
!ll&&!rl&&r
000
MC/DC0%0/2
$f eq "perl" and $mod =~ /^[\d\.]+$/mx
$f eq "perl" $mod =~ /^[\d\.]+$/mx
Truth table0%0/3
A and B
A= $f eq "perl"B= $mod =~ /^[\d\.]+$/mx
ABresult
0X0
100
111
235 0 return(1);
236 }
237 }
238 0 return;
239 }
240  
241 ################################################################################
242 0 ▶sub _get_max_pp_size {
Subroutine _get_max_pp_size: not called CC=6 SCAR=37.4
243 0 my($mods, $max_module, $max_caller, $cur_indent) = @_;
244 0 for my $mod (@{$mods}) {
245 0 ▶ next if _filtered($mod);
Branch: 1 branch
BranchTrueFalseTotal
if _filtered($mod) 0 0 0
246 0 my $l1 = length($mod->{'name'}) + $cur_indent;
247 0 my $l2 = length($mod->{'caller'});
248 0 ▶ $max_module = $l1 if $max_module < $l1;
Branch: 1 branch
BranchTrueFalseTotal
if $max_module < $l1 0 0 0
249 0 ▶ $max_caller = $l2 if $max_caller < $l2;
Branch: 1 branch
BranchTrueFalseTotal
if $max_caller < $l2 0 0 0
250 0 ▶ if($mod->{'sub'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{"sub"}) 0 0 0
251 0 ($max_module, $max_caller) = _get_max_pp_size($mod->{'sub'}, $max_module, $max_caller, $cur_indent+4);
252 }
253 }
254 0 return($max_module, $max_caller);
255 }
256  
257 ################################################################################
258 END {
259 0 ▶ print_pretty() if $Devel::Module::Trace::print;
Subroutine END: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Module::Trace::print 0 0 0
260 0 ▶ save($Devel::Module::Trace::save) if $Devel::Module::Trace::save;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Module::Trace::save 0 0 0
261 };
262  
263 ################################################################################
264  
265 1 1;
266  
267 =head1 TODO
268  
269 * add waterfall charts output
270  
271 =head1 REPOSITORY
272  
273 Git: http://github.com/sni/perl-devel-module-trace
274  
275 =head1 SEE ALSO
276  
277 L<Devel::OverrideGlobalRequire>
278  
279 =head1 AUTHOR
280  
281 Sven Nierlein, C<< <nierlein at cpan.org> >>
282  
283 =head1 COPYRIGHT & LICENSE
284  
285 Copyright 2015 Sven Nierlein.
286  
287 This program is free software; you can redistribute it and/or modify it
288 under the terms of either: the GNU General Public License as published
289 by the Free Software Foundation; or the Artistic License.
290  
291 See http://dev.perl.org/licenses/ for more information.
292  
293 =cut
↑ Summary