| 1 | # Fri Oct 29 16:30:48 2004 Chris Tarnutzer, tarnutzer@ethlife.ethz.ch | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright 2004 by Chris Tarnutzer, 2005-2020 by Christian Jaeger | ||||||||||||||||||||||||||||
| 4 | # Published under the same terms as perl itself | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | Chj::Backtrace | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Add backtrace to all string based exceptions (those thrown by die | ||||||||||||||||||||||||||||
| 14 | "somestring"). | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 TODO | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | Use L<Devel::Confess> or L<Carp Always> instead? | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 NOTE | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | This is alpha software! Read the status section in the package README | ||||||||||||||||||||||||||||
| 25 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | package Chj::Backtrace; | ||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 9 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 9 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Carp::longmess 'usually' inserts a needless repetition | ||||||||||||||||||||||||||||
| 37 | # if the argument was already created by confess: | ||||||||||||||||||||||||||||
| 38 | # Hello at (eval 35) line 1. | ||||||||||||||||||||||||||||
| 39 | # at (eval 35) line 1 | ||||||||||||||||||||||||||||
| 40 | # eval 'package calc; no strict \'vars\'; die "Hello" | ||||||||||||||||||||||||||||
| 41 | # ... | ||||||||||||||||||||||||||||
| 42 | # Clean removes this needless second line/repetition. | ||||||||||||||||||||||||||||
| 43 | # (croak creates a different text so the double duty is not removed.) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 115 | ▶ | sub Clean { | ||||||||||||||||||||||||||
|
Subroutine
Clean: called CC=2 SCAR=10.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | 115 | my ($str) = @_; | |||||||||||||||||||||||||||
| 47 | 115 | $str =~ s/(at [^\n]* line \d+)\.\n (at [^\n]* line \d+)\n/ | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | if ($1 eq $2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | $1.".\n" | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 0 | $1.".\n ".$2."\n" | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | /se; | ||||||||||||||||||||||||||||
| 54 | 115 | $str | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 9 | our $singlestep = 0; #?. | |||||||||||||||||||||||||||
| 58 | 9 | our $only_confess_if_not_already = 1; | |||||||||||||||||||||||||||
| 59 | 9 | our $do_confess_objects = 0; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 122 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=7.3
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # Do not override any handler that a previous FP::Repl::Trap may | ||||||||||||||||||||||||||||
| 64 | # have installed (HACKY): | ||||||||||||||||||||||||||||
| 65 | 122 | ▶ | return if $SIG{__DIE__}->$_isa("FP::Repl::WithRepl::Handler"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 116 | ▶ | $SIG{__DIE__} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.8
| |||||||||||||||||||||||||||||
| 68 | 116 | ▶ | $DB::single = 1 if $singlestep; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 116 | ▶ | if ($only_confess_if_not_already) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 116 | ▶ | if (!$do_confess_objects and ref $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $Chj::Backtrace::do_confess_objects and ref $_[0]
MC/DC50%1/2
not $Chj::Backtrace::do_confess_objects and ref $_[0]
not $Chj::Backtrace::do_confess_objects
ref $_[0]
Truth table66%2/3
not(A) and B
A= not $Chj::Backtrace::do_confess_objectsB= ref $_[0]
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | # exception object | ||||||||||||||||||||||||||||
| 73 | # (ah well, confess does that check anyway!) | ||||||||||||||||||||||||||||
| 74 | 1 | die @_ | |||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #print STDERR "\n------\n@_\n------\n"; | ||||||||||||||||||||||||||||
| 78 | 115 | ▶ | if ($_[0] =~ /^[^\n]*line \d+\.\n/s) { # die, not confess. | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | 98 | die Clean Carp::longmess @_ | |||||||||||||||||||||||||||
| 80 | } elsif ($_[0] =~ /^[^\n]*line \d+\n\t/s) { # confess | ||||||||||||||||||||||||||||
| 81 | 0 | die @_ | |||||||||||||||||||||||||||
| 82 | } else { # unsure | ||||||||||||||||||||||||||||
| 83 | 17 | die Clean Carp::longmess @_ | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||||||||||
| 87 | 0 | die Clean Carp::longmess @_ | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 122 | }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 9 | 1; | |||||||||||||||||||||||||||