| 1 | package Devel::CallStack; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | 2 | require 5.006001; | |||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use vars qw($VERSION | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | $Depth $Full $Reverse $Stdout $Stderr $Out $In $Append | ||||||||||||||||||||||||||||||||||||||
| 9 | $Import | ||||||||||||||||||||||||||||||||||||||
| 10 | 2 | %Cumul); | |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 2 | $VERSION = '0.19'; | |||||||||||||||||||||||||||||||||||||
| 13 | 2 | $Depth = 1e9; # If someone has a callstack this deep, we are in trouble. | |||||||||||||||||||||||||||||||||||||
| 14 | 2 | $Import = 0; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | sub import { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
import: called CC=14 SCAR=37.9
| |||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my $class = shift; | |||||||||||||||||||||||||||||||||||||
| 18 | 2 | for my $i (@_) { | |||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | if ($i =~ /^(?:depth=)?(\d+)$/) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||
| 20 | 0 | $Depth = $1; | |||||||||||||||||||||||||||||||||||||
| 21 | } elsif ($i eq 'full') { | ||||||||||||||||||||||||||||||||||||||
| 22 | 0 | $Full = 1; | |||||||||||||||||||||||||||||||||||||
| 23 | } elsif ($i eq 'reverse') { | ||||||||||||||||||||||||||||||||||||||
| 24 | 0 | $Reverse = 1; | |||||||||||||||||||||||||||||||||||||
| 25 | } elsif ($i eq 'stdout') { | ||||||||||||||||||||||||||||||||||||||
| 26 | 0 | $Stdout = 1; | |||||||||||||||||||||||||||||||||||||
| 27 | } elsif ($i eq 'stderr') { | ||||||||||||||||||||||||||||||||||||||
| 28 | 0 | $Stderr = 1; | |||||||||||||||||||||||||||||||||||||
| 29 | } elsif ($i =~ /^out=(.+)/) { | ||||||||||||||||||||||||||||||||||||||
| 30 | 0 | $Out = $1; | |||||||||||||||||||||||||||||||||||||
| 31 | } elsif ($i =~ /^in(?:=(.+))?/) { | ||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $In = $1 ne "" ? $1 : "callstack.out"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 33 | } elsif ($i eq 'append') { | ||||||||||||||||||||||||||||||||||||||
| 34 | 0 | $Append = 1; | |||||||||||||||||||||||||||||||||||||
| 35 | } else { | ||||||||||||||||||||||||||||||||||||||
| 36 | 1 | die "Devel::CallStack::import: '$i' unknown\n"; | |||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||
| 39 | 1 | &set(); # Otherwise we get the import() call stack captured, too. | |||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | &read($In) if defined $In; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | $Out = "callstack.out" unless defined $Out || $Stdout; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $Devel::CallStack::Out or $Devel::CallStack::Stdout
MC/DC0%0/2
defined $Devel::CallStack::Out or $Devel::CallStack::Stdout
defined $Devel::CallStack::Out
$Devel::CallStack::Stdout
Truth table33%1/3
A or B
A= defined $Devel::CallStack::OutB= $Devel::CallStack::Stdout
| |||||||||||||||||||||||||||||||||||||||
| 42 | 1 | $Import++; # Import was a success. | |||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 46 | 1 | %Cumul = @_; | |||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 50 | 0 | %Cumul; | |||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub write { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
write: called CC=9 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 54 | 2 | ▶ | if ($Import) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 55 | 1 | my $ofh; | |||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | if ($Stdout) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $ofh = select STDOUT; | |||||||||||||||||||||||||||||||||||||
| 58 | } elsif ($Stderr) { | ||||||||||||||||||||||||||||||||||||||
| 59 | 0 | $ofh = select STDERR; | |||||||||||||||||||||||||||||||||||||
| 60 | } elsif (defined $Out) { | ||||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | my $mode = $Append ? ">>" : ">"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | unless (open(OUT, "$mode$Out")) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 63 | 0 | die qq[Devel::CallStack::END: failed to open "$Out" for writing: $!\n]; | |||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||
| 65 | 1 | $ofh = select OUT; | |||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||
| 67 | 1 | for my $s (sort keys %Cumul) { | |||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | next if $s =~ /Devel::CallStack/o; # We do not exist. | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my $d = ($s =~ tr/,/,/) + 1; | |||||||||||||||||||||||||||||||||||||
| 70 | 0 | print "$s $d $Cumul{$s}\n"; | |||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||
| 72 | 1 | select $ofh; | |||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | sub read { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
read: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $fn = shift; | |||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | unless (open(IN, $fn)) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 79 | 0 | die qq[Devel::CallStack::read: failed to open "$fn" for reading: $!\n]; | |||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | while (<IN>) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 82 | 0 | my ($s, $d, $n) = split; | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $s = join(",", reverse split(/,/, $s)) if $Reverse; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 84 | 0 | $Cumul{$s} += $n; | |||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | 0 | close(IN); | |||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | 2 | ▶ | sub END { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 90 | 2 | &write(); | |||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | package DB; | ||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub DB { } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
DB: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | 2 | ▶ | use vars qw($Full $Depth $Reverse %Cumul); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||
| 101 | 2 | *Depth = \$Devel::CallStack::Depth; | |||||||||||||||||||||||||||||||||||||
| 102 | 2 | *Full = \$Devel::CallStack::Full; | |||||||||||||||||||||||||||||||||||||
| 103 | 2 | *Reverse = \$Devel::CallStack::Reverse; | |||||||||||||||||||||||||||||||||||||
| 104 | 2 | *Cumul = \%Devel::CallStack::Cumul; | |||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | sub sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sub: not called CC=9 SCAR=42.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if (my ($p, $s) = ($DB::sub =~ /^(.+)::(.+)/)) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my @s; | |||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($Full) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if (my ($f, $l) = ($DB::sub{$DB::sub} =~ /^(.+):(\d+)/)) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 111 | 0 | @s = ( "$f:$l:${p}::$s" ); | |||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | for (my $i = 0; @s < $Depth; $i++) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my @c = caller($i); | |||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | last unless @c; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 115 | 0 | push @s, "$c[1]:$c[2]:$c[3]"; | |||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||||||||||||
| 119 | 0 | @s = ( $DB::sub ); | |||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | for (my $i = 0; @s < $Depth; $i++) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 121 | 0 | my @c = caller($i); | |||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | last unless @c; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 123 | 0 | push @s, $c[3]; | |||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||
| 126 | $Cumul{ | ||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | join ",", $Reverse ? @s : reverse @s # Ironic, no? | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 128 | }++; | ||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||
| 130 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 131 | 0 | &{$DB::sub}(@_); | |||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||
| 134 | 2 | 1; | |||||||||||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||||||||||||