InteractiveDebugger.pm

filter: Statementstmt 39.1% 47 / 120 Branchbran 0.0% 0 / 26 Conditioncond 6.2% 1 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 55.5% 15 / 27 Podpod 28.5% 2 / 7 totaltotal 31.8% 65 / 204 CC 20 SCAR 30.1
CC
20
Cov
30%
debugger_callback
56.0
call
56.0
filter_frames
20.0
CRAP 20.4
↑ Summary InteractiveDebugger/HTML.pm »
1 package Plack::Middleware::InteractiveDebugger;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.01';
6  
7 1 ▶use parent qw( Plack::Middleware );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Plack::Util::Accessor qw( resource );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dump::Streamer;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Devel::StackTrace::WithLexicals;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Eval::WithLexicals;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Plack::Middleware::InteractiveDebugger::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Plack::App::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶my $share = try { File::ShareDir::dist_dir('Plack-Middleware-InteractiveDebugger') } || "share";
Subroutine __ANON__: called CC=1 SCAR=0.2
Condition50%1/2
&try(sub { File::ShareDir::dist_dir('Plack-Middleware-InteractiveDebugger'); } ) || 'share'
l!l
10
MC/DC0%0/1
&try(sub { File::ShareDir::dist_dir('Plack-Middleware-InteractiveDebugger'); } ) || 'share'
&try(sub { File::ShareDir::dist_dir('Plack-Middleware-InteractiveDebugger'); } )
Truth table50%1/2
A
A= &try(sub { File::ShareDir::dist_dir('Plack-Middleware-InteractiveDebugger'); } )
Aresult
11
00
23  
24 {
25 # Hide from stacktrace's own lexicals
26 1 my %traces;
27 0 ▶ sub _traces {
Subroutine _traces: not called CC=2 SCAR=17.9
28 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
29 0 $traces{$_[0]} = $_[1];
30 } else {
31 0 $traces{$_[0]};
32 }
33 }
34 }
35  
36 1 ▶sub prepare_app {
Subroutine prepare_app: not called CC=1 SCAR=2.6
Pod documented
37 0 my $self = shift;
38 0 $self->resource( Plack::App::File->new(root => $share)->to_app );
39 }
40  
41 0 ▶sub debugger_callback {
Subroutine debugger_callback: not called CC=7 SCAR=40.3
Pod undocumented
42 0 my($self, $env) = @_;
43  
44 0 ▶ if ($env->{PATH_INFO} =~ s!/res/!!) {
Branch: 3 branches
BranchTrueFalseTotal
if ($env->{'PATH_INFO'} =~ s[/res/][]) { } 0 0 0
elsif ($env->{'PATH_INFO'} eq '/source') { } 0 0 0
elsif ($env->{'PATH_INFO'} eq '/command') { } 0 0 0
45 0 return $self->resource->($env);
46 } elsif ($env->{PATH_INFO} eq "/source") {
47 0 my $req = Plack::Request->new($env);
48  
49 0 my($trace_id, $idx) = split /-/, $req->query_parameters->{frame};
50 0 my $html = render_source( _traces($trace_id)->frame($idx) );
51  
52 0 return [ 200, [ "Content-Type", "text/html; charset=utf-8" ], [ utf8_safe($html) ] ];
53 } elsif ($env->{PATH_INFO} eq "/command") {
54 0 my $req = Plack::Request->new($env);
55  
56 0 my($trace_id, $idx) = split /-/, $req->query_parameters->{frame};
57 0 my $code = $req->query_parameters->{code};
58  
59 0 my $trace = _traces($trace_id);
60 0 my $frame = $trace->frame($idx);
61  
62 0 ▶ my $lex = $frame->{__eval} ||= do {
Condition0%0/3
$frame->{"__eval"} ||= do { my $e = "Eval::WithLexicals"->new; $e->in_package("InteractiveDebugger::Pad"); $e->lexicals($frame->lexicals || {}); $e }
l!l&&r!l&&!r
000
MC/DC0%0/2
$frame->{"__eval"} ||= do { my $e = "Eval::WithLexicals"->new; $e->in_package("InteractiveDebugger::Pad"); $e->lexicals($frame->lexicals || {}); $e }
$frame->{"__eval"} do { my $e = "Eval::WithLexicals"->new; $e->in_package("InteractiveDebugger::Pad"); $e->lexicals($frame->lexicals || {}); $e }
Truth table0%0/3
A ||= B
A= $frame->{"__eval"}B= do { my $e = "Eval::WithLexicals"->new; $e->in_package("InteractiveDebugger::Pad"); $e->lexicals($frame->lexicals || {}); $e }
ABresult
1X1
011
000
63 0 my $e = Eval::WithLexicals->new;
64 0 $e->in_package("InteractiveDebugger::Pad");
65 0 ▶ $e->lexicals($frame->lexicals || {});
Condition0%0/2
$frame->lexicals || {}
l!l
00
MC/DC0%0/1
$frame->lexicals || {}
$frame->lexicals
Truth table0%0/2
A
A= $frame->lexicals
Aresult
11
00
66 0 $e;
67 };
68  
69 0 ▶ local *InteractiveDebugger::Pad::D = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
71 0 Dump(@_);
72 } else {
73 0 Dump($lex->lexicals);
74 }
75 0 };
76  
77 0 my @ret = eval { $lex->eval($code) };
78 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
79 0 @ret = ($@);
80 }
81  
82 0 return [ 200, [ 'Content-Type', 'text/html' ], [ "perl> $code\n", map encode_html($_), @ret ] ];
83 }
84 }
85  
86 0 ▶sub call {
Subroutine call: not called CC=7 SCAR=40.3
Pod documented
87 0 my($self, $env) = @_;
88  
89 0 ▶ if ($env->{'psgi.multiprocess'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgi.multiprocess"}) 0 0 0
90 0 Carp::croak(__PACKAGE__, " only runs in a single-process mode.");
91 0 return $self->app->($env);
92 }
93  
94 0 ▶ if ($env->{PATH_INFO} =~ s!^/__debugger__!!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"PATH_INFO"} =~ s[^/__debugger__][]) 0 0 0
95 0 return $self->debugger_callback($env);
96 }
97  
98 0 my $trace;
99 0 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 $trace = Devel::StackTrace::WithLexicals->new(
101 indent => 1, message => munge_error($_[0], [ caller ]),
102 );
103 0 die @_;
104 0 };
105  
106 0 my $caught;
107 0 ▶ my $res = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 $self->app->($env);
109 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 $caught = $_;
111 0 [ 500, [ "Content-Type", "text/plain; charset=utf-8" ], [ no_trace_error(utf8_safe($caught)) ] ];
112 0 };
113  
114 0 ▶ if ($trace && ($caught || (ref $res eq 'ARRAY' && $res->[0] == 500)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trace and $caught || ref $res eq "ARRAY" && $res->[0] == 500) 0 0 0
Condition0%0/9
$trace and $caught || ref $res eq "ARRAY" && $res->[0] == 500
!ll&&!rl&&r
000
$caught or ref $res eq "ARRAY" and $res->[0] == 500
l!l&&r!l&&!r
000
ref $res eq "ARRAY" and $res->[0] == 500
!ll&&!rl&&r
000
MC/DC0%0/4
$trace and $caught || ref $res eq "ARRAY" && $res->[0] == 500
$trace $caught ref $res eq "ARRAY" $res->[0] == 500
Truth table0%0/5
A and B or C and D
A= $traceB= $caughtC= ref $res eq "ARRAY"D= $res->[0] == 500
ABCDresult
0XXX0
100X0
10100
11XX1
10111
115 0 $self->filter_frames($trace);
116 0 my $html = render_full($env, $trace);
117  
118 0 $res = [500, ['Content-Type' => 'text/html; charset=utf-8'], [ utf8_safe($html) ]];
119 0 $env->{'psgi.errors'}->print($trace->as_string);
120  
121 0 _traces( refaddr($trace), $trace );
122 }
123  
124 0 undef $trace;
125  
126 0 return $res;
127 }
128  
129 0 ▶sub filter_frames {
Subroutine filter_frames: not called CC=4 SCAR=30.0
Pod undocumented
130 0 my($self, $trace) = @_;
131  
132 0 my @new_frames;
133 0 my @frames = $trace->frames;
134 0 ▶ shift @frames if $frames[0]->filename eq __FILE__;
Branch: 1 branch
BranchTrueFalseTotal
if $frames[0]->filename eq __FILE__ 0 0 0
135  
136 0 for my $frame (@frames) {
137 0 push @new_frames, $frame;
138 0 ▶ last if $frame->filename eq __FILE__;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->filename eq __FILE__ 0 0 0
139 }
140  
141 0 $trace->{frames} = \@new_frames;
142 }
143  
144 # below is a copy from StackTrace
145  
146 0 ▶sub no_trace_error {
Subroutine no_trace_error: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my $msg = shift;
148 0 chomp($msg);
149  
150 0 return <<EOF;
151 The application raised the following error:
152  
153 $msg
154  
155 and the middleware couldn't catch its stack trace, possibly because your application overrides \$SIG{__DIE__} by itself, preventing the middleware from working correctly. Remove the offending code or module that does it: known examples are CGI::Carp and Carp::Always.
156 EOF
157 }
158  
159 0 ▶sub munge_error {
Subroutine munge_error: not called CC=2 SCAR=17.9
Pod undocumented
160 0 my($err, $caller) = @_;
161 0 ▶ return $err if ref $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err 0 0 0
162  
163 # Ugly hack to remove " at ... line ..." automatically appended by perl
164 # If there's a proper way to do this, please let me know.
165 0 $err =~ s/ at \Q$caller->[1]\E line $caller->[2]\.\n$//;
166  
167 0 return $err;
168 }
169  
170 0 ▶sub utf8_safe {
Subroutine utf8_safe: not called CC=2 SCAR=17.9
Pod undocumented
171 0 my $str = shift;
172  
173 # NOTE: I know messing with utf8:: in the code is WRONG, but
174 # because we're running someone else's code that we can't
175 # guarnatee which encoding an exception is encoded, there's no
176 # better way than doing this. The latest Devel::StackTrace::AsHTML
177 # (0.08 or later) encodes high-bit chars as HTML entities, so this
178 # path won't be executed.
179 0 ▶ if (utf8::is_utf8($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($str)) 0 0 0
180 0 utf8::encode($str);
181 }
182  
183 0 $str;
184 }
185  
186 1 1;
187 __END__
↑ Summary InteractiveDebugger/HTML.pm »