lib/Devel/ebug.pm

filter: Statementstmt 99.2% 137 / 138 Branchbran 77.7% 42 / 54 Conditioncond 75.0% 9 / 12 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 24 / 24 Podpod 33.3% 1 / 3 totaltotal 90.7% 215 / 237 CC 37 SCAR 15.5
CC
37
Cov
92%
_lost
8.3
_accept_backend
8.2
load
7.0
CRAP 4.7
« blib/lib/Devel/ebug/Backend/Plugin/ActionPoints.pm ↑ Summary blib/lib/Devel/ebug/Backend/Plugin/Run.pm »
1 package Devel::ebug;
2  
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use Class::Accessor::Chained::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Devel::StackTrace 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Devel::ebug::Wire;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Proc::Background;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use String::Koremutake;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Text::ParseWords qw(shellwords);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 34 ▶use Module::Pluggable require => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 34 ▶use base qw(Class::Accessor::Chained::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # ABSTRACT: A simple, extensible Perl debugger
19 34 our $VERSION = '0.69'; # VERSION
20  
21 34 __PACKAGE__->mk_accessors(qw(
22 backend
23 port
24 serializer
25 program args socket proc pid running
26 package filename line codeline subroutine finished));
27  
28 # let's run the code under our debugger and connect to the server it
29 # starts up
30 # 'yaml' unless asked otherwise, so existing clients are unaffected.
31 500 ▶sub _serializer {
Subroutine _serializer: called CC=5 SCAR=16.6
32 500 my($self) = @_;
33 500 ▶ my $format = $self->serializer || $ENV{DEVEL_EBUG_SERIALIZER} || 'yaml';
Condition80%4/5
$self->serializer || $ENV{'DEVEL_EBUG_SERIALIZER'} || 'yaml'
l!l
10490
$self->serializer or $ENV{"DEVEL_EBUG_SERIALIZER"}
l!l&&r!l&&!r
100490
MC/DC50%1/2
$self->serializer || $ENV{'DEVEL_EBUG_SERIALIZER'} || 'yaml'
$self->serializer $ENV{"DEVEL_EBUG_SERIALIZER"}
Truth table66%2/3
A or B
A= $self->serializerB= $ENV{"DEVEL_EBUG_SERIALIZER"}
ABresult
1X1
011
000
34 500 ▶ croak "unknown serializer '$format', expected 'yaml' or 'json'"
Branch: 1 branch
BranchTrueFalseTotal
unless $format eq "yaml" or $format eq "json" 0 500 500
Condition66%2/3
$format eq "yaml" or $format eq "json"
l!l&&r!l&&!r
490100
MC/DC0%0/2
$format eq "yaml" or $format eq "json"
$format eq "yaml" $format eq "json"
Truth table66%2/3
A or B
A= $format eq "yaml"B= $format eq "json"
ABresult
1X1
011
000
35 unless $format eq 'yaml' or $format eq 'json';
36 500 return $format;
37 }
38  
39 56 ▶sub load {
Subroutine load: called CC=7 SCAR=19.5
Pod documented
40 56 my $self = shift;
41 56 my $program = $self->program;
42  
43 # import all the plugins into our namespace
44 56 eval { $_->import } for $self->plugins;
45  
46 56 my $k = String::Koremutake->new;
47 56 my $secret = $k->integer_to_koremutake(int(rand(100_000)));
48  
49 # Listen on a port of the OS's choosing and have the backend connect back
50 # to it, so that concurrent sessions can never collide over a port.
51 56 ▶ my $listener = IO::Socket::INET->new(
Condition50%1/2
'IO::Socket::INET'->new('Listen', 1, 'LocalAddr', 'localhost', 'LocalPort', 0, 'Proto', 'tcp') || croak("Devel::ebug: could not listen for the backend: $!")
l!l
560
MC/DC0%0/1
'IO::Socket::INET'->new('Listen', 1, 'LocalAddr', 'localhost', 'LocalPort', 0, 'Proto', 'tcp') || croak("Devel::ebug: could not listen for the backend: $!")
'IO::Socket::INET'->new('Listen', 1, 'LocalAddr', 'localhost', 'LocalPort', 0, 'Proto', 'tcp')
Truth table50%1/2
A
A= 'IO::Socket::INET'->new('Listen', 1, 'LocalAddr', 'localhost', 'LocalPort', 0, 'Proto', 'tcp')
Aresult
11
00
52 Listen => 1,
53 LocalAddr => 'localhost',
54 LocalPort => 0,
55 Proto => 'tcp',
56 ) || croak "Devel::ebug: could not listen for the backend: $!";
57  
58 56 $ENV{SECRET} = $secret;
59 56 $ENV{DEVEL_EBUG_CONNECT} = $listener->sockport;
60 # With args the command is run as a list, so they reach the program
61 # verbatim instead of being split and interpolated by the shell.
62 56 my @command;
63 56 ▶ if (my $args = $self->args) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $args = $self->args) { } 5 51 56
64 5 ▶ my @backend = $self->backend ? _backend_words($self->backend) : ($^X, '-d:ebug::Backend');
Branch: 1 branch
BranchTrueFalseTotal
$self->backend ? : 2 3 5
65 5 @command = (@backend, $program, @$args);
66 } else {
67 51 ▶ my $backend = $self->backend || "$^X -d:ebug::Backend";
Condition100%2/2
$self->backend || "$^X -d:ebug::Backend"
l!l
546
MC/DC100%1/1
$self->backend || "$^X -d:ebug::Backend"
$self->backend
Truth table100%2/2
A
A= $self->backend
Aresult
11
00
68 51 @command = ("$backend $program");
69 }
70 56 my $proc = Proc::Background->new(
71 {'die_upon_destroy' => 1},
72 @command
73 );
74 56 ▶ croak(qq{Devel::ebug: Failed to start up "$program" in load()}) unless $proc->alive;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc->alive 0 56 56
75 56 $self->proc($proc);
76 56 $ENV{SECRET} = "";
77 56 delete $ENV{DEVEL_EBUG_CONNECT};
78  
79 56 $self->socket($self->_accept_backend($listener, $secret));
80 55 close $listener;
81  
82 55 $self->_handshake($secret);
83 }
84  
85 # Split a backend command line into words. On Windows a backslash is a
86 # path separator rather than an escape, so keep it from being eaten (as in
87 # "C:\Strawberry\perl\bin\perl.exe bin/ebug_backend_perl").
88 5 ▶sub _backend_words {
Subroutine _backend_words: called CC=2 SCAR=6.9
89 5 my($backend) = @_;
90 5 ▶ $backend =~ s/\\/\\\\/g if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 2 3 5
91 5 return shellwords($backend);
92 }
93  
94 # Wait for the backend we just started to connect back. It announces
95 # itself by sending the secret, and anything else that connects to the
96 # listening port is turned away.
97 57 ▶sub _accept_backend {
Subroutine _accept_backend: called CC=8 SCAR=21.0
98 57 my($self, $listener, $secret) = @_;
99 57 my $program = $self->program;
100 57 my $select = IO::Select->new($listener);
101 57 my $deadline = time + 30;
102  
103 57 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 673 0 673
104 673 ▶ croak(qq{Devel::ebug: "$program" exited before the debugger could connect to it})
Branch: 1 branch
BranchTrueFalseTotal
unless $self->proc->alive 1 672 673
105 unless $self->proc->alive;
106 672 ▶ next unless $select->can_read(0.1);
Branch: 1 branch
BranchTrueFalseTotal
unless $select->can_read("0.1") 614 58 672
107 58 ▶ my $socket = $listener->accept or next;
Branch: 1 branch
BranchTrueFalseTotal
my $socket = $listener->accept or next 58 0 58
108 58 ▶ my $line = IO::Select->new($socket)->can_read(5) ? $socket->getline : undef;
Branch: 1 branch
BranchTrueFalseTotal
'IO::Select'->new($socket)->can_read(5) ? : 58 0 58
109 58 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 57 1 58
110 57 $line =~ s/\r?\n\z//;
111 57 ▶ return $socket if $line eq $secret;
Branch: 1 branch
BranchTrueFalseTotal
if $line eq $secret 56 1 57
112 }
113 2 close $socket;
114 }
115  
116 0 croak(qq{Devel::ebug: timed out waiting for "$program" to connect to the debugger; }
117 . qq{is an older Devel::ebug::Backend being loaded that does not support DEVEL_EBUG_CONNECT?});
118 }
119  
120 3 ▶sub attach {
Subroutine attach: called CC=5 SCAR=16.1
Pod undocumented
121 3 my ($self, $port, $key) = @_;
122  
123 # import all the plugins into our namespace
124 3 eval { $_->import } for $self->plugins;
125  
126 # try and connect to the server
127 3 my $socket;
128 3 foreach ( 1 .. 10 ) {
129 7 $socket = IO::Socket::INET->new(
130 PeerAddr => "localhost",
131 PeerPort => $port,
132 Proto => 'tcp',
133 ReuseAddr => 1,
134 );
135 7 ▶ last if $socket;
Branch: 1 branch
BranchTrueFalseTotal
if $socket 3 4 7
136 4 sleep 1;
137 }
138 3 ▶ die "Could not connect: $!" unless $socket;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 3 3
139 3 $self->socket($socket);
140  
141 3 $self->_handshake($key, $port);
142 }
143  
144 58 ▶sub _handshake {
Subroutine _handshake: called CC=4 SCAR=13.9
145 58 my ($self, $key, $port) = @_;
146  
147 # talk() would report a closed connection as a lost program; here it
148 # means our secret was turned away, which deserves its own message
149 58 $self->_send(
150 { command => "ping",
151 version => $Devel::ebug::VERSION,
152 secret => $key,
153 }
154 );
155 58 my $response = $self->_receive(1);
156 58 ▶ unless ($response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 1 57 58
157 1 ▶ die "The debugger did not answer the handshake"
Branch: 1 branch
BranchTrueFalseTotal
defined $port ? : 1 0 1
158 . (defined $port ? " on port $port; the key may be wrong, or the port may belong to another session" : "")
159 . "\n";
160 }
161 57 my $version = $response->{version};
162 die "Client version $version != our version $Devel::ebug::VERSION"
163 34 ▶ unless do { no warnings 'uninitialized'; $version eq $Devel::ebug::VERSION };
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x51\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} $version eq $Devel::ebug::VERSION } 0 57 57
164 57 $self->pid($response->{pid});
165 57 $self->running(0);
166  
167 57 $self->basic; # get basic information for the first line
168 }
169  
170 #
171 # FIXME : this would mean that plugin writers don't need to Export stuff
172 #
173 #sub load_plugins {
174 # my $self = shift;
175 # my $obj = Devel::Symdump->new($self->plugins);
176 #
177 # for ($obj->functions) {
178 # my $name = (split /::/)[-1];
179 # next if substr($name,0,1) eq '_';
180 # *basic = \&$_;
181 # }
182 #
183 #}
184  
185  
186  
187 # Requests and responses go over the socket one line at a time; see
188 # Devel::ebug::Wire for how a line is put together.
189 # The debugger went away mid-conversation: the program exited without
190 # the debugger's cleanup running, was killed, or crashed.
191 9 ▶sub _lost {
Subroutine _lost: called CC=8 SCAR=21.2
192 9 my($self) = @_;
193 9 $self->running(0);
194 9 ▶ my $program = defined $self->pid ? sprintf('the program (pid %d)', $self->pid) : 'the program';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->pid ? : 9 0 9
195 9 my $what = "$program has exited or been killed";
196  
197 # When we started it, say how it ended. The connection can close a
198 # moment before the process is reaped, so give it a short while.
199 9 ▶ if (my $proc = $self->proc) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $proc = $self->proc) 9 0 9
200 9 for (1 .. 20) {
201 14 ▶ last unless $proc->alive;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc->alive 9 5 14
202 5 select(undef, undef, undef, 0.05);
203 }
204 9 ▶ my $status = $proc->alive ? undef : $proc->wait;
Branch: 1 branch
BranchTrueFalseTotal
$proc->alive ? : 0 9 9
205 9 ▶ if (defined $status) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $status) 9 0 9
206 9 ▶ $what = $status & 127
Branch: 1 branch
BranchTrueFalseTotal
$status & 127 ? : 3 6 9
207 ? sprintf('%s was killed by signal %d', $program, $status & 127)
208 : sprintf('%s exited with status %d', $program, $status >> 8);
209 }
210 }
211 9 croak "Devel::ebug: lost the connection to the debugger; $what";
212 }
213  
214 392 ▶sub talk {
Subroutine talk: called CC=2 SCAR=6.9
Pod undocumented
215 392 my($self, $req) = @_;
216 392 ▶ croak "Devel::ebug: the program is running; call wait_for_stop() before '$req->{command}'"
Branch: 1 branch
BranchTrueFalseTotal
if $self->running 1 391 392
217 if $self->running;
218 391 $self->_send($req);
219 390 return $self->_receive;
220 }
221  
222 500 ▶sub _send {
Subroutine _send: called CC=2 SCAR=6.9
223 500 my($self, $req) = @_;
224 500 my $format = $self->_serializer;
225 # Writing to a debugger that has gone away raises SIGPIPE, which would
226 # kill the frontend outright; turn it into an error that can be caught.
227 500 local $SIG{PIPE} = 'IGNORE';
228 500 ▶ $self->socket->print(Devel::ebug::Wire::encode($format, $req) . "\n")
Branch: 1 branch
BranchTrueFalseTotal
$self->socket->print(Devel::ebug::Wire::encode($format, $req) . "\n") or $self->_lost 499 1 500
229 or $self->_lost;
230 }
231  
232 # Read one response. A closed connection is an error, unless $allow_eof
233 # is true, in which case it returns undef.
234 499 ▶sub _receive {
Subroutine _receive: called CC=3 SCAR=11.0
235 499 my($self, $allow_eof) = @_;
236 499 my $socket = $self->socket;
237 499 my $data = <$socket>;
238 498 ▶ unless (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 9 489 498
239 9 ▶ return undef if $allow_eof;
Branch: 1 branch
BranchTrueFalseTotal
if $allow_eof 1 8 9
240 8 $self->_lost;
241 }
242  
243 # The backend answers in the format it was asked in, but detect rather
244 # than assume: it costs nothing and keeps a mismatch from being silent.
245 489 return Devel::ebug::Wire::decode(Devel::ebug::Wire::detect($data), $data);
246 }
247  
248 34 1;
249  
250 __END__
« blib/lib/Devel/ebug/Backend/Plugin/ActionPoints.pm ↑ Summary blib/lib/Devel/ebug/Backend/Plugin/Run.pm »