| 1 | package Devel::SimpleTrace; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | { | ||||||||||||||||||||||||||||
| 5 | 6 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Install warn() and die() substitutes | ||||||||||||||||||||||||||||
| 10 | 6 | $SIG{'__WARN__'} = \&_do_warn; | |||||||||||||||||||||||||||
| 11 | 6 | $SIG{'__DIE__' } = \&_do_die; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | my $stderr = ''; | |||||||||||||||||||||||||||
| 14 | 6 | my $in_eval = 0; | |||||||||||||||||||||||||||
| 15 | 6 | my %options = ( | |||||||||||||||||||||||||||
| 16 | showrefs => 0, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # import() | ||||||||||||||||||||||||||||
| 22 | # ------ | ||||||||||||||||||||||||||||
| 23 | 4 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 24 | 4 | my $class = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | for my $opt (@_) { | |||||||||||||||||||||||||||
| 27 | 1 | ▶ | exists $options{$opt} ? $options{$opt} = not $options{$opt} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | : CORE::warn "warning: Unknown option: $opt\n" | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # _use_data_dumper() | ||||||||||||||||||||||||||||
| 35 | # ---------------- | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub _use_data_dumper { | ||||||||||||||||||||||||||
|
Subroutine
_use_data_dumper: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | require Data::Dumper; | |||||||||||||||||||||||||||
| 38 | 1 | import Data::Dumper; | |||||||||||||||||||||||||||
| 39 | 1 | $Data::Dumper::Indent = 1; # no fancy indent | |||||||||||||||||||||||||||
| 40 | 1 | $Data::Dumper::Terse = 1; # don't use $VAR unless needed | |||||||||||||||||||||||||||
| 41 | 1 | $Data::Dumper::Sortkeys = 1; # sort keys | |||||||||||||||||||||||||||
| 42 | #$Data::Dumper::Deparse = 1; # deparse code refs | ||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||
| 44 | 1 | local $^W = 0; | |||||||||||||||||||||||||||
| 45 | 1 | ▶ | *Devel::SimpleTrace::_use_data_dumper = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # _do_warn() | ||||||||||||||||||||||||||||
| 52 | # -------- | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub _do_warn { | ||||||||||||||||||||||||||
|
Subroutine
_do_warn: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 1 | local $SIG{'__WARN__'} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | my $msg = join '', @_; | |||||||||||||||||||||||||||
| 57 | 1 | $msg =~ s/ at (.+?) line (\d+)\.$//; | |||||||||||||||||||||||||||
| 58 | 1 | $stderr .= $msg; | |||||||||||||||||||||||||||
| 59 | 1 | ▶ | $stderr .= "\n" if substr($msg, -1, 1) ne "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | _stack_trace($1, $2); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | print STDERR $stderr; | |||||||||||||||||||||||||||
| 64 | 1 | $stderr = ''; | |||||||||||||||||||||||||||
| 65 | 1 | $in_eval = 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # | ||||||||||||||||||||||||||||
| 70 | # _do_die() | ||||||||||||||||||||||||||||
| 71 | # ------- | ||||||||||||||||||||||||||||
| 72 | 5 | ▶ | sub _do_die { | ||||||||||||||||||||||||||
|
Subroutine
_do_die: called CC=8 SCAR=21.0
| |||||||||||||||||||||||||||||
| 73 | 5 | local $SIG{'__WARN__'} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 74 | 5 | local $SIG{'__DIE__' } = 'DEFAULT'; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 5 | ▶ | CORE::die @_ if ref $_[0] and not $options{showrefs}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $_[0] and not $options{"showrefs"}
MC/DC100%2/2
ref $_[0] and not $options{"showrefs"}
ref $_[0]
not $options{"showrefs"}
Truth table100%3/3
A and not(B)
A= ref $_[0]B= not $options{"showrefs"}
| |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | CORE::die @_ if index($_[0], "\n\tat ") >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 2 | my @args = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | ▶ | _use_data_dumper() if ref $args[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 2 | ▶ | my $msg = join '', map { ref $_ ? "Caught exception object: $_\: ".Dumper($_) : $_ } @args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | $msg =~ s/ at (.+?) line (\d+)\.$//; | |||||||||||||||||||||||||||
| 83 | 2 | $stderr .= $msg; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | $stderr .= "\n" if substr($msg, -1, 1) ne "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 2 | _stack_trace($1, $2); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if($in_eval) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 2 | $@ = $stderr; | |||||||||||||||||||||||||||
| 90 | 2 | $stderr = ''; | |||||||||||||||||||||||||||
| 91 | 2 | $in_eval = 0; | |||||||||||||||||||||||||||
| 92 | 2 | CORE::die $@ | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 0 | print STDERR $stderr; | |||||||||||||||||||||||||||
| 96 | 0 | $stderr = ''; | |||||||||||||||||||||||||||
| 97 | 0 | exit -1 | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # | ||||||||||||||||||||||||||||
| 103 | # _stack_trace() | ||||||||||||||||||||||||||||
| 104 | # ------------ | ||||||||||||||||||||||||||||
| 105 | 3 | ▶ | sub _stack_trace { | ||||||||||||||||||||||||||
|
Subroutine
_stack_trace: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||
| 106 | 3 | my($file,$line) = @_; | |||||||||||||||||||||||||||
| 107 | 3 | ▶ | $file ||= ''; $line ||= ''; | ||||||||||||||||||||||||||
|
Condition100%4/4
$file ||= ""
$line ||= ""
MC/DC100%1/1
$file ||= ""
$file
MC/DC100%1/1
$line ||= ""
$line
Truth table100%2/2
A
A= $file
Truth table100%2/2
A
A= $line
| |||||||||||||||||||||||||||||
| 108 | 3 | ▶ | $file =~ '(eval \d+)' and $file = '<eval>'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | my $level = 2; | |||||||||||||||||||||||||||
| 111 | 3 | my @stack = ( ['', $file, $line] ); # @stack = ( [ function, file, line ], ... ) | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | while(my @context = caller($level++)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 14 | ▶ | $context[1] ||= ''; $context[2] ||= ''; | ||||||||||||||||||||||||||
|
Condition50%2/4
$context[1] ||= ""
$context[2] ||= ""
MC/DC0%0/1
$context[1] ||= ""
$context[1]
MC/DC0%0/1
$context[2] ||= ""
$context[2]
Truth table50%1/2
A
A= $context[1]
Truth table50%1/2
A
A= $context[2]
| |||||||||||||||||||||||||||||
| 115 | 14 | ▶ | $context[1] =~ '(eval \d+)' and $context[1] = '<eval>' and $in_eval = 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$context[1] =~ /(eval \d+)/ and $context[1] = "<eval>"
MC/DC100%1/1
$context[1] =~ /(eval \d+)/ and $context[1] = "<eval>"
$context[1] =~ /(eval \d+)/
Truth table100%2/2
A
A= $context[1] =~ /(eval \d+)/
| |||||||||||||||||||||||||||||
| 116 | 14 | ▶ | $context[3] eq '(eval)' and $context[3] = '<eval>' and $in_eval = 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$context[3] eq "(eval)" and $context[3] = "<eval>"
MC/DC100%1/1
$context[3] eq "(eval)" and $context[3] = "<eval>"
$context[3] eq "(eval)"
Truth table100%2/2
A
A= $context[3] eq "(eval)"
| |||||||||||||||||||||||||||||
| 117 | 14 | $stack[-1][0] = $context[3]; | |||||||||||||||||||||||||||
| 118 | 14 | push @stack, [ '', @context[1, 2] ]; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 3 | ▶ | $stack[-1][0] = (caller($level-2))[0].'::' || 'main::'; | ||||||||||||||||||||||||||
|
Condition50%1/2
(caller $level - 2)[0] . '::' || 'main::'
MC/DC0%0/1
(caller $level - 2)[0] . '::' || 'main::'
(caller $level - 2)[0] . '::'
Truth table50%1/2
A
A= (caller $level - 2)[0] . '::'
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 3 | for my $func (@stack) { | |||||||||||||||||||||||||||
| 123 | 17 | ▶ | $$func[1] eq '' and $$func[1] = 'unknown source'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 17 | ▶ | $$func[2] and $$func[1] .= ':'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 17 | $stderr .= "\tat $$func[0]($$func[1]$$func[2])\n"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 6 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||