blib/lib/Devel/ebug/Backend.pm

filter: Statementstmt 0.0% 0 / 137 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 14 Podpod 10.0% 1 / 10 totaltotal 0.3% 1 / 279 CC 50 SCAR 44.3
CC
50
Cov
0%
DB
156.0
sub
156.0
initialise
90.0
CRAP 84.2
« blib/lib/Devel/ebug/Console.pm ↑ Summary lib/Devel/ebug/Wire.pm »
1 package Devel::ebug::Backend;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.69'; # VERSION
7  
8 package DB;
9  
10 use Devel::ebug::Wire;
11 use IO::Socket::INET;
12 use String::Koremutake;
13 use Module::Pluggable
14 search_path => 'Devel::ebug::Backend::Plugin',
15 require => 1;
16  
17 use vars qw(@dbline %dbline);
18  
19 our $VERSION = '0.69'; # VERSION
20  
21 # Let's catch INT signals and set a flag when they occur
22 $SIG{INT} = sub {
23 $DB::signal = 1;
24 return;
25 };
26  
27 my $context = {
28 finished => 0,
29 initialise => 1,
30 mode => "step",
31 stack => [],
32 watch_points => [],
33 };
34  
35  
36 # Commands that the back end can respond to
37 # Set record if the command changes start and should thus be recorded
38 # in order for undo to work properly
39 my %commands = ();
40  
41 sub DB {
42 0 ▶ my ($package, $filename, $line) = caller;
Subroutine DB: not called CC=12 SCAR=50.5
Pod undocumented
43 0 ($context->{package}, $context->{filename}, $context->{line}) =
44 ($package, $filename, $line);
45  
46 0 ▶ initialise() if $context->{initialise};
Branch: 1 branch
BranchTrueFalseTotal
if $context->{"initialise"} 0 0 0
47  
48 # we're here because of a signal, reset the flag
49 0 ▶ if ($DB::signal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DB::signal) 0 0 0
50 0 $DB::signal = 0;
51 }
52  
53 # single step
54 0 my $old_single = $DB::single;
55 0 $DB::single = 1;
56  
57 0 ▶ if (@{ $context->{watch_points} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$context->{"watch_points"};}) 0 0 0
58 0 my %delete;
59 0 foreach my $watch_point (@{ $context->{watch_points} }) {
60 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my $v = eval "package $package; $watch_point"; ## no critic (BuiltinFunctions::ProhibitStringyEval)
62 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
63 0 $context->{watch_single} = 1;
64 0 $delete{$watch_point} = 1;
65 }
66 }
67 0 ▶ if ($context->{watch_single} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{'watch_single'} == 0) { } 0 0 0
68 0 return;
69 } else {
70 0 @{ $context->{watch_points} } =
71 0 grep { !$delete{$_} } @{ $context->{watch_points} };
72 }
73 }
74  
75 # we're here because of a break point, test the condition
76 0 ▶ if ($old_single == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_single == 0) 0 0 0
77 0 my $condition = break_point_condition($filename, $line);
78 0 ▶ if ($condition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition) 0 0 0
79 0 ▶ local $SIG{__WARN__} = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 my $v = eval "package $package; $condition"; ## no critic (BuiltinFunctions::ProhibitStringyEval)
81 0 ▶ unless ($v) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($v) 0 0 0
82 # condition not true, go back to running
83 0 $DB::single = 0;
84 0 return;
85 }
86 }
87 }
88  
89 0 $context->{watch_single} = 1;
90 0 $context->{codeline} = (fetch_codelines($filename, $line - 1))[0];
91 0 chomp $context->{codeline};
92  
93 0 while (1) {
94 0 my $req = get();
95 0 my $command = $req->{command};
96  
97 0 my $sub = $commands{$command}->{sub};
98 0 ▶ if (defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub) { } 0 0 0
99 0 put($sub->($req, $context));
100  
101 0 ▶ if ($context->{last}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"last"}) 0 0 0
102 0 delete $context->{last};
103 0 last;
104 }
105 } else {
106 0 die "unknown command $command";
107 }
108 }
109 }
110  
111 sub initialise {
112 0 ▶ foreach my $plugin (__PACKAGE__->plugins) {
Subroutine initialise: not called CC=9 SCAR=45.0
Pod undocumented
113 0 my $sub = $plugin->can("register_commands");
114 0 ▶ next unless $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
115 0 my %new = &$sub;
116 0 foreach my $command (keys %new) {
117 0 $commands{$command} = $new{$command};
118 }
119 }
120  
121 # Started by Devel::ebug's load(): connect back to the port the frontend
122 # is listening on, which the OS chose for it, so there is nothing to
123 # collide with. Announce ourselves with the secret, so the frontend can
124 # tell us apart from anything else that connects to it.
125 0 ▶ if (my $port = delete $ENV{DEVEL_EBUG_CONNECT}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $port = delete $ENV{'DEVEL_EBUG_CONNECT'}) { } 0 0 0
126 0 ▶ my $socket = IO::Socket::INET->new(
Condition0%0/2
'IO::Socket::INET'->new('PeerAddr', 'localhost', 'PeerPort', $port, 'Proto', 'tcp') || die("Devel::ebug::Backend: could not connect to the frontend on port ${port}: $!")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('PeerAddr', 'localhost', 'PeerPort', $port, 'Proto', 'tcp') || die("Devel::ebug::Backend: could not connect to the frontend on port ${port}: $!")
'IO::Socket::INET'->new('PeerAddr', 'localhost', 'PeerPort', $port, 'Proto', 'tcp')
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('PeerAddr', 'localhost', 'PeerPort', $port, 'Proto', 'tcp')
Aresult
11
00
127 PeerAddr => 'localhost',
128 PeerPort => $port,
129 Proto => 'tcp',
130 ) || die "Devel::ebug::Backend: could not connect to the frontend on port $port: $!";
131 0 local $\; # if we run under perl -l the following line would get mangled
132 0 $socket->print("$ENV{SECRET}\n");
133 0 $context->{socket} = $socket;
134 0 ▶ exit unless handshake();
Branch: 1 branch
BranchTrueFalseTotal
unless handshake() 0 0 0
135 }
136  
137 # Otherwise wait for a frontend to attach (ebug_server, or anything
138 # calling Devel::ebug's attach()), on a port derived from the secret.
139 else {
140 0 my $k = String::Koremutake->new;
141 0 my $int = $k->koremutake_to_integer($ENV{SECRET});
142 0 my $port = 3141 + ($int % 1024);
143 0 ▶ my $server = IO::Socket::INET->new(
Condition0%0/2
'IO::Socket::INET'->new('Listen', 5, 'LocalAddr', 'localhost', 'LocalPort', $port, 'Proto', 'tcp', 'ReuseAddr', 1, 'Reuse', 1) || die("Devel::ebug::Backend: could not listen on port ${port}: $!")
l!l
00
MC/DC0%0/1
'IO::Socket::INET'->new('Listen', 5, 'LocalAddr', 'localhost', 'LocalPort', $port, 'Proto', 'tcp', 'ReuseAddr', 1, 'Reuse', 1) || die("Devel::ebug::Backend: could not listen on port ${port}: $!")
'IO::Socket::INET'->new('Listen', 5, 'LocalAddr', 'localhost', 'LocalPort', $port, 'Proto', 'tcp', 'ReuseAddr', 1, 'Reuse', 1)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('Listen', 5, 'LocalAddr', 'localhost', 'LocalPort', $port, 'Proto', 'tcp', 'ReuseAddr', 1, 'Reuse', 1)
Aresult
11
00
144 Listen => 5,
145 LocalAddr => 'localhost',
146 LocalPort => $port,
147 Proto => 'tcp',
148 ReuseAddr => 1,
149 Reuse => 1,
150 )
151 || die "Devel::ebug::Backend: could not listen on port $port: $!";
152  
153 # A frontend with the wrong secret, for example one of another session
154 # whose port happens to collide with ours, is turned away rather than
155 # ending this session.
156 0 while (1) {
157 0 $context->{socket} = $server->accept;
158 0 ▶ last if handshake();
Branch: 1 branch
BranchTrueFalseTotal
if handshake() 0 0 0
159 0 close delete $context->{socket};
160 }
161 }
162  
163 0 $context->{initialise} = 0;
164 }
165  
166 # The first request on a connection must be a ping carrying our secret.
167 # Returns false, without answering, if it is anything else.
168 sub handshake {
169 0 ▶ my $req = eval { read_request() };
Subroutine handshake: not called CC=6 SCAR=37.4
Pod undocumented
170 return 0 unless $req
171 && ($req->{command} || '') eq 'ping'
172 && defined $req->{secret}
173 0 ▶ && $req->{secret} eq $ENV{SECRET};
Branch: 1 branch
BranchTrueFalseTotal
unless $req and ($req->{"command"} || "") eq "ping" and defined $req->{"secret"} and $req->{"secret"} eq $ENV{"SECRET"} 0 0 0
Condition0%0/11
$req and ($req->{"command"} || "") eq "ping" and defined $req->{"secret"} and $req->{"secret"} eq $ENV{"SECRET"}
!ll&&!rl&&r
000
$req and ($req->{"command"} || "") eq "ping" and defined $req->{"secret"}
!ll&&!rl&&r
000
$req and ($req->{"command"} || "") eq "ping"
!ll&&!rl&&r
000
$req->{'command'} || ''
l!l
00
MC/DC0%0/4
$req and ($req->{"command"} || "") eq "ping" and defined $req->{"secret"} and $req->{"secret"} eq $ENV{"SECRET"}
$req $req->{'command'} defined $req->{"secret"} $req->{"secret"} eq $ENV{"SECRET"}
Truth table0%0/5
A and B and C and D
A= $reqB= $req->{'command'}C= defined $req->{"secret"}D= $req->{"secret"} eq $ENV{"SECRET"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
174 0 put($commands{ping}->{sub}->($req, $context));
175 0 return 1;
176 }
177  
178 sub put {
179 0 ▶ my ($res) = @_;
Subroutine put: not called CC=2 SCAR=17.9
Pod undocumented
180 # Answer in whatever format the request arrived in, so the frontend
181 # decides and the backend needs no configuring.
182 0 ▶ my $data = Devel::ebug::Wire::encode($context->{format} || 'yaml', $res);
Condition0%0/2
$context->{'format'} || 'yaml'
l!l
00
MC/DC0%0/1
$context->{'format'} || 'yaml'
$context->{'format'}
Truth table0%0/2
A
A= $context->{'format'}
Aresult
11
00
183 0 local $\; # if we run under perl -l the following line would get mangled
184 0 $context->{socket}->print($data . "\n");
185 }
186  
187 sub get {
188 0 ▶ my $req = read_request();
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
189 # The frontend has gone away; that is how a session ends, not an error.
190 0 ▶ exit unless $req;
Branch: 1 branch
BranchTrueFalseTotal
unless $req 0 0 0
191 0 push @{ $context->{history} }, $req
192 0 ▶ if exists $commands{ $req->{command} }->{record};
Branch: 1 branch
BranchTrueFalseTotal
if exists $commands{$req->{"command"}}{"record"} 0 0 0
193 0 return $req;
194 }
195  
196 # Read and decode one request, or return undef if the connection is closed.
197 sub read_request {
198 0 ▶ return undef unless $context->{socket};
Subroutine read_request: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless $context->{"socket"} 0 0 0
199 0 local $/= "\n";
200 0 my $data = $context->{socket}->getline;
201 0 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
202 0 $context->{format} = Devel::ebug::Wire::detect($data);
203 0 return Devel::ebug::Wire::decode($context->{format}, $data);
204 }
205  
206 sub sub {
207 0 ▶ my $sub = $DB::sub;
Subroutine sub: not called CC=12 SCAR=50.5
Pod documented
208  
209 0 my $frame = { single => $DB::single, sub => $sub };
210 0 push @{ $context->{stack} }, $frame;
211  
212 # If we are in 'next' mode, then skip all the lines in the sub
213 0 ▶ $DB::single = 0 if defined $context->{mode} && $context->{mode} eq 'next';
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"mode"} and $context->{"mode"} eq "next" 0 0 0
Condition0%0/3
defined $context->{"mode"} and $context->{"mode"} eq "next"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $context->{"mode"} and $context->{"mode"} eq "next"
defined $context->{"mode"} $context->{"mode"} eq "next"
Truth table0%0/3
A and B
A= defined $context->{"mode"}B= $context->{"mode"} eq "next"
ABresult
0X0
100
111
214  
215 no strict 'refs';
216 0 ▶ if (wantarray) { ## no critic (Community::Wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
217 0 my @ret = &$sub;
218  
219 # Restore from $frame (our own lexical) rather than whatever pop
220 # returns, and check $frame->{'return'} the same way. $frame is
221 # the exact object that was pushed, so this is correct even if
222 # $context->{stack} has become misaligned - which can happen if
223 # an earlier, unrelated call's exception got caught further up
224 # the debuggee's own call stack (eg. Tk widgets routinely wrap
225 # internal calls in eval {} blocks for feature detection),
226 # skipping that call's own cleanup and leaving a stale frame
227 # behind. Trusting the popped value here would then read that
228 # stale frame and corrupt $DB::single for callers further up the
229 # stack too, leaving next/step permanently unable to stop the
230 # debuggee again (it just runs to completion, or hangs forever if
231 # that includes something like Tk's MainLoop).
232 0 pop @{ $context->{stack} };
233 0 $DB::single = $frame->{single};
234 0 ▶ $DB::single = 0 if defined $context->{mode} && $context->{mode} eq 'run' && !@{$context->{watch_points}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};} 0 0 0
Condition0%0/6
defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};}
!ll&&!rl&&r
000
defined $context->{"mode"} and $context->{"mode"} eq "run"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};}
defined $context->{"mode"} $context->{"mode"} eq "run" not @{$context->{"watch_points"};}
Truth table0%0/4
A and B and not(C)
A= defined $context->{"mode"}B= $context->{"mode"} eq "run"C= not @{$context->{"watch_points"};}
ABCresult
0XX0
10X0
1100
1111
235  
236 0 ▶ if ($frame->{'return'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{'return'}) { } 0 0 0
237 0 return @{ $frame->{'return'} };
238 } else {
239 0 return @ret;
240 }
241 } else {
242 0 my $ret = &$sub;
243  
244 0 pop @{ $context->{stack} };
245 0 $DB::single = $frame->{single};
246 0 ▶ $DB::single = 0 if defined $context->{mode} && $context->{mode} eq 'run' && !@{$context->{watch_points}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};} 0 0 0
Condition0%0/6
defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};}
!ll&&!rl&&r
000
defined $context->{"mode"} and $context->{"mode"} eq "run"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $context->{"mode"} and $context->{"mode"} eq "run" and not @{$context->{"watch_points"};}
defined $context->{"mode"} $context->{"mode"} eq "run" not @{$context->{"watch_points"};}
Truth table0%0/4
A and B and not(C)
A= defined $context->{"mode"}B= $context->{"mode"} eq "run"C= not @{$context->{"watch_points"};}
ABCresult
0XX0
10X0
1100
1111
247  
248 0 ▶ if ($frame->{'return'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->{'return'}) { } 0 0 0
249 0 return $frame->{'return'}->[0];
250 } else {
251 0 return $ret;
252 }
253 }
254 }
255  
256 sub DB::postponed {
257 # If this is a subroutine, let postponed_sub() deal with it.
258 0 ▶ goto &postponed_sub unless ref \$_[0] eq 'GLOB';
Subroutine postponed: not called CC=5 SCAR=34.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$_[0] eq "GLOB" 0 0 0
259  
260 0 my ($filePath) = @_;
261 0 $filePath =~ s/^.*_<//;
262  
263 0 my ($volume,$directories,$fileName) = File::Spec->splitpath( $filePath );
264  
265 #test if the file name match with relative path/absolute path/single file name
266 0 ▶ if (exists $DB::break_on_load{$filePath}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $DB::break_on_load{$filePath} or exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)} or exists $DB::break_on_load{$fileName}) 0 0 0
Condition0%0/6
exists $DB::break_on_load{$filePath} or exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)} or exists $DB::break_on_load{$fileName}
l!l&&r!l&&!r
000
exists $DB::break_on_load{$filePath} or exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $DB::break_on_load{$filePath} or exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)} or exists $DB::break_on_load{$fileName}
exists $DB::break_on_load{$filePath} exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)} exists $DB::break_on_load{$fileName}
Truth table0%0/4
A or B or C
A= exists $DB::break_on_load{$filePath}B= exists $DB::break_on_load{"File::Spec"->rel2abs($filePath)}C= exists $DB::break_on_load{$fileName}
ABCresult
1XX1
01X1
0011
0000
267 || exists $DB::break_on_load{File::Spec->rel2abs( $filePath)}
268 || exists $DB::break_on_load{$fileName}){
269 0 $DB::single = 1;
270 }
271  
272 }
273  
274  
275 sub fetch_codelines {
276 0 ▶ my ($filename, @lines) = @_;
Subroutine fetch_codelines: not called CC=6 SCAR=37.4
Pod undocumented
277  
278 #use vars qw(@dbline %dbline);
279 0 *dbline = $main::{ '_<' . $filename };
280 0 my @codelines = @dbline;
281  
282 # for modules, not sure why
283 0 ▶ shift @codelines if not defined $codelines[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $codelines[0] 0 0 0
284  
285 # defined!
286 0 ▶ @codelines = map { defined($_) ? $_ : "" } @codelines;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
287  
288 # remove newlines
289 0 s/\s+$// for @codelines;
290  
291 # we run it with -d:ebug::Backend, so remove this extra line
292 0 @codelines = grep { $_ ne 'use Devel::ebug::Backend;' } @codelines;
293  
294 # for some reasons, the perl internals leave the opening POD line
295 # around but strip the rest. so let's strip the opening POD line
296 @codelines =
297 0 ▶ map { /^=(head|over|item|back|over|cut|pod|begin|end|for)/ ? "" : $_ }
Branch: 1 branch
BranchTrueFalseTotal
/^=(head|over|item|back|over|cut|pod|begin|end|for)/ ? : 0 0 0
298 @codelines;
299  
300 0 ▶ if (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 0 0 0
301 0 @codelines = @codelines[@lines];
302 }
303 0 return @codelines;
304 }
305  
306 sub break_point_condition {
307 0 ▶ my ($filename, $line) = @_;
Subroutine break_point_condition: not called CC=1 SCAR=6.9
Pod undocumented
308 0 *dbline = $main::{ '_<' . $filename };
309 0 return $dbline{$line};
310 }
311  
312 sub END {
313 0 ▶ $context->{finished} = 1;
Subroutine END: not called CC=1 SCAR=6.9
314 0 $DB::single = 1;
315 0 DB::fake::at_exit();
316 }
317  
318 package
319 DB::fake;
320  
321 sub at_exit {
322 0 ▶ 1;
Subroutine at_exit: not called CC=1 SCAR=6.9
323 }
324  
325 package DB; # Do not trace this 1; below!
326  
327 1;
328  
329 __END__
« blib/lib/Devel/ebug/Console.pm ↑ Summary lib/Devel/ebug/Wire.pm »