LanguageServer/DebuggerInterface.pm untested

filter: Statementstmt 0.0% 0 / 924 Branchbran 0.0% 0 / 470 Conditioncond 0.0% 0 / 100 MC/DCmcdc 0.0% 0 / 61 Subroutinesub 0.0% 0 / 100 Podpod 0.0% 0 / 82 totaltotal 0.0% 0 / 1737 CC 236 SCAR 43.0
CC
236
Cov
0%
DB
306.0
_recv
240.0
get_globals
182.0
CRAP 73.8
1 #
2 # We include DB package from perl core here, to be able to modify it...
3 #
4  
5 package DB;
6  
7 # "private" globals
8  
9 my ($running, $ready, $deep, $usrctxt, $evalarg,
10 @stack, @saved, @skippkg, @clients);
11 my $preeval = {};
12 my $posteval = {};
13 my $ineval = {};
14  
15 ####
16 #
17 # Globals - must be defined at startup so that clients can refer to
18 # them right after a C<require DB;>
19 #
20 ####
21  
22 BEGIN {
23  
24 # these are hardcoded in perl source (some are magical)
25  
26 $DB::sub = ''; # name of current subroutine
27 %DB::sub = (); # "filename:fromline-toline" for every known sub
28 $DB::single = 0; # single-step flag (set it to 1 to enable stops in BEGIN/use)
29 $DB::signal = 0; # signal flag (will cause a stop at the next line)
30 $DB::trace = 0; # are we tracing through subroutine calls?
31 @DB::args = (); # arguments of current subroutine or @ARGV array
32 @DB::dbline = (); # list of lines in currently loaded file
33 %DB::dbline = (); # actions in current file (keyed by line number)
34 @DB::ret = (); # return value of last sub executed in list context
35 $DB::ret = ''; # return value of last sub executed in scalar context
36  
37 # other "public" globals
38  
39 $DB::package = ''; # current package space
40 $DB::filename = ''; # current filename
41 $DB::subname = ''; # currently executing sub (fully qualified name)
42 $DB::lineno = ''; # current line number
43  
44 $DB::VERSION = $DB::VERSION = '1.07';
45  
46 # initialize private globals to avoid warnings
47  
48 $running = 1; # are we running, or are we stopped?
49 @stack = (0);
50 @clients = ();
51 $deep = 1000;
52 $ready = 0;
53 @saved = ();
54 @skippkg = ();
55 $usrctxt = '';
56 $evalarg = '';
57  
58 # scan args for stdin redirect
59 for (my $i=0; $i <= $#ARGV; $i++) {
60 if ($ARGV[$i] eq "<" && $i < $#ARGV) {
61 # open stdin from file
62 open STDIN, "<", $ARGV[$i+1] or die "open stdin";
63 # remove from ARGV
64 splice @ARGV, $i, 2;
65 }
66 }
67 }
68  
69 ####
70 # entry point for all subroutine calls
71 #
72 sub sub {
73  
74 # this is important, othwise return values might be corrupted...
75 return &$DB::sub if (!$DB::single) ;
76  
77 push(@stack, $DB::single);
78 $DB::single &= 1;
79 $DB::single |= 4 if $#stack == $deep;
80 if ($DB::sub eq 'DESTROY' or substr($DB::sub, -9) eq '::DESTROY' or not defined wantarray) {
81 &$DB::sub;
82 $DB::single |= pop(@stack);
83 $DB::ret = undef;
84 }
85 elsif (wantarray) {
86 @DB::ret = &$DB::sub;
87 $DB::single |= pop(@stack);
88 @DB::ret;
89 }
90 else {
91 $DB::ret = &$DB::sub;
92 $DB::single |= pop(@stack);
93 $DB::ret;
94 }
95 }
96  
97 ####
98 # this is called by perl for every statement
99 #
100 sub DB {
101 return unless $ready;
102 &save;
103 ($DB::package, $DB::filename, $DB::lineno) = caller;
104  
105 return if @skippkg and grep { $_ eq $DB::package } @skippkg;
106  
107 $usrctxt = "package $DB::package;"; # this won't let them modify, alas
108 local(*DB::dbline) = "::_<$DB::filename";
109  
110 my ($stop, $action);
111 if (($stop,$action) = split(/\0/,$DB::dbline{$DB::lineno})) {
112 if ($stop eq '1') {
113 $DB::signal |= 1;
114 }
115 else {
116 $stop = 0 unless $stop; # avoid un_init warning
117 $evalarg = "\$DB::signal |= do { $stop; }"; &eval;
118 $DB::dbline{$DB::lineno} =~ s/;9($|\0)/$1/; # clear any temp breakpt
119 }
120 }
121 if ($DB::single || $DB::trace || $DB::signal) {
122 $DB::subname = ($DB::sub =~ /\'|::/) ? $DB::sub : "${DB::package}::$DB::sub"; #';
123 DB->loadfile($DB::filename, $DB::lineno);
124 }
125 $evalarg = $action, &eval if $action;
126 if ($DB::single || $DB::signal) {
127 _outputall($#stack . " levels deep in subroutine calls.\n") if $DB::single & 4;
128 $DB::single = 0;
129 $DB::signal = 0;
130 $running = 0;
131  
132 &eval if ($evalarg = DB->prestop);
133 my $c;
134 for $c (@clients) {
135 # perform any client-specific prestop actions
136 &eval if ($evalarg = $c->cprestop);
137  
138 # Now sit in an event loop until something sets $running
139 do {
140 $c->idle; # call client event loop; must not block
141 if ($running == 2) { # client wants something eval-ed
142 &eval if ($evalarg = $c->evalcode);
143 $running = 0;
144 }
145 } until $running;
146  
147 # perform any client-specific poststop actions
148 &eval if ($evalarg = $c->cpoststop);
149 }
150 &eval if ($evalarg = DB->poststop);
151 }
152 ($@, $!, $,, $/, $\, $^W) = @saved;
153 ();
154 }
155  
156 ####
157 # this takes its argument via $evalarg to preserve current @_
158 #
159 sub eval {
160 ($@, $!, $,, $/, $\, $^W) = @saved;
161 eval "$usrctxt $evalarg; &DB::save";
162 _outputall($@) if $@;
163 }
164  
165 ###############################################################################
166 # no compile-time subroutine call allowed before this point #
167 ###############################################################################
168  
169 use strict; # this can run only after DB() and sub() are defined
170  
171 sub save {
172 @saved = ($@, $!, $,, $/, $\, $^W);
173 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
174 }
175  
176 sub catch {
177 for (@clients) { $_->awaken; }
178 $DB::signal = 1;
179 $ready = 1;
180 }
181  
182 ####
183 #
184 # Client callable (read inheritable) methods defined after this point
185 #
186 ####
187  
188 sub register {
189 my $s = shift;
190 $s = _clientname($s) if ref($s);
191 push @clients, $s;
192 }
193  
194 sub done {
195 my $s = shift;
196 $s = _clientname($s) if ref($s);
197 @clients = grep {$_ ne $s} @clients;
198 $s->cleanup;
199 # $running = 3 unless @clients;
200 exit(0) unless @clients;
201 }
202  
203 sub _clientname {
204 my $name = shift;
205 "$name" =~ /^(.+)=[A-Z]+\(.+\)$/;
206 return $1;
207 }
208  
209 sub next {
210 my $s = shift;
211 $DB::single = 2;
212 $running = 1;
213 }
214  
215 sub step {
216 my $s = shift;
217 $DB::single = 1;
218 $running = 1;
219 }
220  
221 sub cont {
222 my $s = shift;
223 my $i = shift;
224 $s->set_tbreak($i) if $i;
225 for ($i = 0; $i <= $#stack;) {
226 $stack[$i++] &= ~1;
227 }
228 $DB::single = 0;
229 $running = 1;
230 }
231  
232 ####
233 # XXX caller must experimentally determine $i (since it depends
234 # on how many client call frames are between this call and the DB call).
235 # Such is life.
236 #
237 sub ret {
238 my $s = shift;
239 my $i = shift; # how many levels to get to DB sub
240 $i = 0 unless defined $i;
241 $i -= $#stack-$i if ($#stack-$i < 0) ;
242 $stack[$#stack-$i] |= 1;
243 $DB::single = 0;
244 $running = 1;
245 }
246  
247 ####
248 # XXX caller must experimentally determine $start (since it depends
249 # on how many client call frames are between this call and the DB call).
250 # Such is life.
251 #
252 sub backtrace {
253 my $self = shift;
254 my $start = shift;
255 my($p,$f,$l,$s,$h,$w,$e,$r,$a, @a, @ret,$i);
256 $start = 1 unless $start;
257 for ($i = $start; ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i); $i++) {
258 @a = @DB::args;
259 for (@a) {
260 s/'/\\'/g;
261 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
262 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
263 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
264 }
265 $w = $w ? '@ = ' : '$ = ';
266 $a = $h ? '(' . join(', ', @a) . ')' : '';
267 $e =~ s/\n\s*\;\s*\Z// if $e;
268 $e =~ s/[\\\']/\\$1/g if $e;
269 if ($r) {
270 $s = "require '$e'";
271 } elsif (defined $r) {
272 $s = "eval '$e'";
273 } elsif ($s eq '(eval)') {
274 $s = "eval {...}";
275 }
276 $f = "file '$f'" unless $f eq '-e';
277 push @ret, "$w&$s$a from $f line $l";
278 last if $DB::signal;
279 }
280 return @ret;
281 }
282  
283 sub _outputall {
284 my $c;
285 for $c (@clients) {
286 $c->output(@_);
287 }
288 }
289  
290 sub trace_toggle {
291 my $s = shift;
292 $DB::trace = !$DB::trace;
293 }
294  
295  
296 ####
297 # without args: returns all defined subroutine names
298 # with subname args: returns a listref [file, start, end]
299 #
300 sub subs {
301 my $s = shift;
302 if (@_) {
303 my(@ret) = ();
304 while (@_) {
305 my $name = shift;
306 push @ret, [$DB::sub{$name} =~ /^(.*)\:(\d+)-(\d+)$/]
307 if exists $DB::sub{$name};
308 }
309 return @ret;
310 }
311 return keys %DB::sub;
312 }
313  
314 ####
315 # first argument is a filename whose subs will be returned
316 # if a filename is not supplied, all subs in the current
317 # filename are returned.
318 #
319 sub filesubs {
320 my $s = shift;
321 my $fname = shift;
322 $fname = $DB::filename unless $fname;
323 return grep { $DB::sub{$_} =~ /^$fname/ } keys %DB::sub;
324 }
325  
326 ####
327 # returns a list of all filenames that DB knows about
328 #
329 sub files {
330 my $s = shift;
331 my(@f) = grep(m|^_<|, keys %main::);
332 return map { substr($_,2) } @f;
333 }
334  
335 ####
336 # returns reference to an array holding the lines in currently
337 # loaded file
338 #
339 sub lines {
340 my $s = shift;
341 return \@DB::dbline;
342 }
343  
344 ####
345 # loadfile($file, $line)
346 #
347 sub loadfile {
348 my $s = shift;
349 my($file, $line) = @_;
350 if (!defined $main::{'_<' . $file}) {
351 my $try;
352 if (($try) = grep(m|^_<.*$file|, keys %main::)) {
353 $file = substr($try,2);
354 }
355 }
356 if (defined($main::{'_<' . $file})) {
357 my $c;
358 # _outputall("Loading file $file..");
359 *DB::dbline = "::_<$file";
360 $DB::filename = $file;
361 for $c (@clients) {
362 # print "2 ", $file, '|', $line, "\n";
363 $c->showfile($file, $line);
364 }
365 return $file;
366 }
367 return undef;
368 }
369  
370 sub lineevents {
371 my $s = shift;
372 my $fname = shift;
373 my(%ret) = ();
374 my $i;
375 $fname = $DB::filename unless $fname;
376 local(*DB::dbline) = "::_<$fname";
377 for ($i = 1; $i <= $#DB::dbline; $i++) {
378 $ret{$i} = [$DB::dbline[$i], split(/\0/, $DB::dbline{$i})]
379 if defined $DB::dbline{$i};
380 }
381 return %ret;
382 }
383  
384 sub set_break {
385 my $s = shift;
386 my $i = shift;
387 my $cond = shift;
388 $i ||= $DB::lineno;
389 $cond ||= '1';
390 $i = _find_subline($i) if ($i =~ /\D/);
391 $s->output("Subroutine not found.\n") unless $i;
392 if ($i) {
393 if ($DB::dbline[$i] == 0) {
394 $s->output("Line $i not breakable.\n");
395 }
396 else {
397 $DB::dbline{$i} =~ s/^[^\0]*/$cond/;
398 }
399 }
400 }
401  
402 sub set_tbreak {
403 my $s = shift;
404 my $i = shift;
405 $i = _find_subline($i) if ($i =~ /\D/);
406 $s->output("Subroutine not found.\n") unless $i;
407 if ($i) {
408 if ($DB::dbline[$i] == 0) {
409 $s->output("Line $i not breakable.\n");
410 }
411 else {
412 $DB::dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
413 }
414 }
415 }
416  
417 sub _find_subline {
418 my $name = shift;
419 $name =~ s/\'/::/;
420 $name = "${DB::package}\:\:" . $name if $name !~ /::/;
421 $name = "main" . $name if substr($name,0,2) eq "::";
422 my($fname, $from, $to) = ($DB::sub{$name} =~ /^(.*):(\d+)-(\d+)$/);
423 if ($from) {
424 local *DB::dbline = "::_<$fname";
425 ++$from while $DB::dbline[$from] == 0 && $from < $to;
426 return wantarray?($from, $name, $fname):$from;
427 }
428 return undef;
429 }
430  
431 sub clr_breaks {
432 my $s = shift;
433 my $i;
434 if (@_) {
435 while (@_) {
436 $i = shift;
437 $i = _find_subline($i) if ($i =~ /\D/);
438 $s->output("Subroutine not found.\n") unless $i;
439 if (defined $DB::dbline{$i}) {
440 $DB::dbline{$i} =~ s/^[^\0]+//;
441 if ($DB::dbline{$i} =~ s/^\0?$//) {
442 delete $DB::dbline{$i};
443 }
444 }
445 }
446 }
447 else {
448 for ($i = 1; $i <= $#DB::dbline ; $i++) {
449 if (defined $DB::dbline{$i}) {
450 $DB::dbline{$i} =~ s/^[^\0]+//;
451 if ($DB::dbline{$i} =~ s/^\0?$//) {
452 delete $DB::dbline{$i};
453 }
454 }
455 }
456 }
457 }
458  
459 sub set_action {
460 my $s = shift;
461 my $i = shift;
462 my $act = shift;
463 $i = _find_subline($i) if ($i =~ /\D/);
464 $s->output("Subroutine not found.\n") unless $i;
465 if ($i) {
466 if ($DB::dbline[$i] == 0) {
467 $s->output("Line $i not actionable.\n");
468 }
469 else {
470 $DB::dbline{$i} =~ s/\0[^\0]*//;
471 $DB::dbline{$i} .= "\0" . $act;
472 }
473 }
474 }
475  
476 sub clr_actions {
477 my $s = shift;
478 my $i;
479 if (@_) {
480 while (@_) {
481 my $i = shift;
482 $i = _find_subline($i) if ($i =~ /\D/);
483 $s->output("Subroutine not found.\n") unless $i;
484 if ($i && $DB::dbline[$i] != 0) {
485 $DB::dbline{$i} =~ s/\0[^\0]*//;
486 delete $DB::dbline{$i} if $DB::dbline{$i} =~ s/^\0?$//;
487 }
488 }
489 }
490 else {
491 for ($i = 1; $i <= $#DB::dbline ; $i++) {
492 if (defined $DB::dbline{$i}) {
493 $DB::dbline{$i} =~ s/\0[^\0]*//;
494 delete $DB::dbline{$i} if $DB::dbline{$i} =~ s/^\0?$//;
495 }
496 }
497 }
498 }
499  
500 sub prestop {
501 my ($client, $val) = @_;
502 return defined($val) ? $preeval->{$client} = $val : $preeval->{$client};
503 }
504  
505 sub poststop {
506 my ($client, $val) = @_;
507 return defined($val) ? $posteval->{$client} = $val : $posteval->{$client};
508 }
509  
510 #
511 # "pure virtual" methods
512 #
513  
514 # client-specific pre/post-stop actions.
515 sub cprestop {}
516 sub cpoststop {}
517  
518 # client complete startup
519 sub awaken {}
520  
521 sub skippkg {
522 my $s = shift;
523 push @skippkg, @_ if @_;
524 }
525  
526 sub evalcode {
527 my ($client, $val) = @_;
528 if (defined $val) {
529 $running = 2; # hand over to DB() to evaluate in its context
530 $ineval->{$client} = $val;
531 }
532 return $ineval->{$client};
533 }
534  
535 sub ready {
536 my $s = shift;
537 return $ready = 1;
538 }
539  
540 # stubs
541  
542 sub init {}
543 sub stop {}
544 sub idle {}
545 sub cleanup {}
546 sub output {}
547  
548 #
549 # client init
550 #
551 for (@clients) { $_->init }
552  
553 $SIG{'INT'} = \&DB::catch;
554  
555 # disable this if stepping through END blocks is desired
556 # (looks scary and deconstructivist with Swat)
557 END { $ready = 0 }
558  
559  
560 ##############################################################################
561  
562 package Perl::LanguageServer::DebuggerInterface ;
563  
564 #use DB;
565  
566 our @ISA = qw(DB);
567  
568 use strict ;
569  
570 use IO::Socket ;
571 use JSON ;
572 use PadWalker ;
573 use Scalar::Util qw{blessed reftype looks_like_number};
574 use Hash::SafeKeys;
575 #use Data::Dump qw{pp} ;
576 use File::Basename ;
577 use vars qw{@dbline %dbline $dbline} ;
578  
579 our $max_display = 5 ;
580 our $debug = 0 ;
581 our $session = $ENV{PLSDI_SESSION} || 1 ;
582 our $socket ;
583 our $json = JSON -> new -> utf8(1) -> ascii(1) ;
584 our @evalresult ;
585 our %postponed_breakpoints ;
586 our $breakpoint_id = 1 ;
587 our $loaded = 0 ;
588 our $break_reason ;
589 our $refresh ;
590  
591 __PACKAGE__ -> register ;
592 __PACKAGE__ -> init ;
593  
594 # ---------------------------------------------------------------------------
595  
596 sub logger
597 {
598 my $class = shift ;
599 print STDERR @_ ;
600 }
601  
602 # ---------------------------------------------------------------------------
603  
604 use constant SPECIALS => { _ => 1, INC => 1, ARGV => 1, ENV => 1, ARGVOUT => 1, SIG => 1,
605 STDIN => 1, STDOUT => 1, STDERR => 1,
606 stdin => 1, stdout => 1, stderr => 1} ;
607  
608 use vars qw{%entry @entry $entry %stab} ;
609  
610 # ---------------------------------------------------------------------------
611  
612 sub get_globals
613 {
614 my ($self, $package) = @_ ;
615  
616 my %vars ;
617  
618 my $specials = $package?0:1 ;
619 $package ||= 'main' ;
620 $package .= "::" unless $package =~ /::$/;
621 no strict ;
622 *stab = *{"main::"};
623 while ($package =~ /(\w+?::)/g)
624 {
625 *stab = ${stab}{$1};
626 }
627 use strict ;
628 my $key ;
629 my $val ;
630  
631 while (($key, $val) = each (%stab))
632 {
633 next if ($key eq '_') ;
634 next if ($key =~ /^_</) ;
635 next if ($key =~ /::$/) ;
636 next if ($key eq 'stab') ;
637 next if (!$specials && (SPECIALS -> {$key} || ($key !~ /^[a-zA-Z_]/))) ;
638 next if ($specials && (!SPECIALS -> {$key} && ($key =~ /^[a-zA-Z_]/))) ;
639  
640 local(*entry) = $val;
641 $key =~ s/([\0-\x1f])/'^'.chr(ord($1)+0x40)/eg ;
642  
643 $vars{"\$$key"} = [\$entry, 'eg:\\$' . $package . $key] if (defined $entry) ;
644 $vars{"\@$key"} = [\@entry, 'eg:\\@' . $package . $key] if (@entry) ;
645 $vars{"\%$key"} = [\%entry, 'eg:\\%' . $package . $key] if (%entry) ;
646 #$vars{"\&$key"} = \&entry if (defined &entry) ;
647 my $fileno;
648 $vars{"Handle:$key"} = [\"fileno=$fileno"] if (defined ($fileno = eval{fileno(*entry)})) ;
649 }
650  
651 return \%vars ;
652 }
653  
654 # ---------------------------------------------------------------------------
655  
656 sub get_var_eval
657 {
658 my ($self, $name, $varsrc, $prefix) = @_ ;
659  
660 # use Data::Dump qw{pp} ;
661 # print STDERR "eval ", pp([$name, $varsrc]), "\n" ;
662 my %vars ;
663  
664 $prefix ||= $varsrc?'el:':'eg:' ;
665 my $refexpr ;
666 my $pre ;
667 my $post ;
668 $refexpr = $name ;
669 my $ref = eval ($refexpr) ;
670 if ($@)
671 {
672 $vars{'ERROR'} = [$@] ;
673 }
674 #print STDERR "name=$name ref=$ref refref=", ref ($ref), "reftype=", reftype ($ref), "\n", pp($ref), "\n" ;
675 if (ref ($ref) eq 'REF')
676 {
677 $ref = $$ref ;
678 #print STDERR "deref ----> ref val=$refexpr ref=$ref refref=", ref ($ref), "reftype=", reftype ($ref), "\n" ;
679 $pre = '${' ;
680 $post = '}' ;
681 }
682 if (reftype ($ref) eq 'ARRAY')
683 {
684 my $n = 0 ;
685 foreach my $entry (@$ref)
686 {
687 $vars{"$n"} = [\$entry, $prefix . $pre . '(' . $refexpr . ')' . $post . '->[' . $n . ']' ] ;
688 $n++ ;
689 }
690 }
691 elsif (reftype ($ref) eq 'HASH')
692 {
693 my $iterator = Hash::SafeKeys::save_iterator_state($ref);
694 foreach my $entry (sort keys %$ref)
695 {
696 $vars{"$entry"} = [\$ref -> {$entry}, $prefix . $pre . '(' . $refexpr . ')' . $post . "->{'" . $entry . "'}" ] ;
697 }
698 Hash::SafeKeys::restore_iterator_state($ref, $iterator);
699 }
700 else
701 {
702 $vars{'$'} = [$ref] ;
703 }
704  
705 return \%vars ;
706 }
707  
708 # ---------------------------------------------------------------------------
709  
710 sub get_arguments
711 {
712 my ($self, $frame) = @_ ;
713  
714 my $vars ;
715 my %varsrc ;
716 eval
717 {
718 my @args = _get_caller_args ($frame+2) ;
719 $varsrc{"\@_"} = [\@args, "ea:\$varsrc->{'\@_'}[0]"] ;
720 $varsrc{"\@ARGV"} = [\@main::ARGV, 'eg:\\@main::ARGV'] ;
721 } ;
722 $self -> logger ($@) if ($@) ;
723 return (\%varsrc) ;
724 }
725  
726 # ---------------------------------------------------------------------------
727  
728 sub get_locals
729 {
730 my ($self, $frame) = @_ ;
731  
732 my $vars ;
733 my %varsrc ;
734 eval
735 {
736 $vars = PadWalker::peek_my ($frame) ;
737 foreach my $var (keys %$vars)
738 {
739 $varsrc{$var} =
740 [
741 $vars->{$var},
742 "el:\$varsrc->{'$var'}"
743 ] ;
744 }
745 } ;
746 $self -> logger ($@) if ($@) ;
747 return (\%varsrc, $vars) ;
748 }
749  
750 # ---------------------------------------------------------------------------
751  
752 sub _get_caller_args
753 {
754 my ($caller) = @_ ;
755  
756 local @DB::args ;
757  
758 my @caller_args ;
759 {
760 package DB;
761  
762 my @call_info = caller ($caller) ;
763 #use Data::Dump qw{pp} ;
764 #print STDERR "db::args after caller $caller ", pp(\@DB::args), "\n" ;
765 @caller_args = @DB::args ;
766 }
767  
768 return @caller_args ;
769 }
770  
771 # ---------------------------------------------------------------------------
772  
773 sub _eval_replace
774 {
775 my ($___di_vars, $___di_sigil, $___di_var, $___di_suffix, $___di_frame) = @_ ;
776  
777 #print STDERR "sigil = $___di_sigil var = $___di_var suffix = $___di_suffix\n" ;
778  
779 if ($___di_var eq '_')
780 {
781 my @args = _get_caller_args ($___di_frame + 3) ;
782 $___di_vars -> {'@_'} = \@args ;
783 }
784 #use Data::Dump qw{pp} ;
785 #print STDERR "vars ", pp ($___di_vars),"\n" ;
786 if ($___di_suffix)
787 {
788 return "\$___di_vars->{'\%$___di_var'}{" if ($___di_suffix eq '{' && exists $___di_vars->{"\%$___di_var"}) ;
789 return "\$___di_vars->{'\@$___di_var'}[" if (exists $___di_vars->{"\@$___di_var"});
790 }
791 else
792 {
793 return "\$\#\{\$___di_vars->{'\@$1'}}" if (($___di_var =~ /^#(.+)/) && exists $___di_vars->{"\@$1"}) ;
794 #print STDERR "v = $___di_var 1 = $1\n" ;
795 return "$___di_sigil\{\$___di_vars->{'$___di_sigil$___di_var'}}" if (exists $___di_vars->{"$___di_sigil$___di_var"}) ;
796 }
797  
798 return "$___di_sigil$___di_var$___di_suffix" ;
799 }
800  
801 # ---------------------------------------------------------------------------
802  
803 sub get_eval_result
804 {
805 my ($self, $frame, $package, $expression) = @_;
806  
807 my $___di_vars = PadWalker::peek_my ($frame) ;
808  
809 $expression =~ s/([\%\@\$])(#?\w+)\s*([\[\{])?/_eval_replace($___di_vars, $1, $2, $3, $frame)/eg ;
810  
811 my $code = "package $package ; no strict ; $expression";
812 my %vars ;
813 #print STDERR "frame=$frame code = $code\n" ;
814  
815  
816 my @result = eval $code;
817 if ($@)
818 {
819 $vars{'ERROR'} = [$@] ;
820 }
821 else
822 {
823 if (@result < 2)
824 {
825 if (ref ($result[0]) eq 'REF')
826 {
827 push @evalresult, $result[0] ;
828 }
829 else
830 {
831 push @evalresult, \$result[0] ;
832 }
833 }
834 elsif ($expression =~ /^\s*\\?\s*\%/)
835 {
836 push @evalresult, { @result } ;
837 }
838 else
839 {
840 push @evalresult, \@result ;
841 }
842 $vars{'eval'} = [$evalresult[-1], 'eg:$Perl::LanguageServer::DebuggerInterface::evalresult[' . $#evalresult . ']'] ;
843 }
844  
845 return \%vars ;
846 }
847  
848 # ---------------------------------------------------------------------------
849  
850 sub get_scalar
851 {
852 my $ret = eval
853 {
854 my ($self, $val) = @_ ;
855  
856 return 'undef' if (!defined ($val)) ;
857 my $obj = '' ;
858 $obj = blessed ($val) . ' ' if (blessed ($val)) ;
859 return $obj . '[..]' if (ref ($val) eq 'ARRAY') ;
860 return $obj . '{..}' if (ref ($val) eq 'HASH') ;
861 my $isnum = looks_like_number ($val);
862 $obj . ($isnum?$val:"'$val'") ;
863 } ;
864 return $@ if ($@) ;
865 return $ret ;
866 }
867  
868 # ---------------------------------------------------------------------------
869  
870 sub get_vars
871 {
872 my ($self, $varsrc, $vars, $array) = @_ ;
873  
874 foreach my $k (sort { $array?$a <=> $b:$a cmp $b } keys %$varsrc)
875 {
876 my $key = $k ;
877 my $val = $varsrc -> {$k}[0] ;
878 my $ref = $varsrc -> {$k}[1] ;
879 $key =~ s/([\0-\x1f])/'^'.chr(ord($1)+0x40)/eg ;
880 #print STDERR "k=$k val=$val ref=$ref refref=", ref ($val), "reftype=", reftype ($ref), "\n" ;
881  
882 if (ref ($val) eq 'REF')
883 {
884 $val = $$val ;
885 #print STDERR "deref ----> ref val=$val ref=$ref refref=", ref ($val), "reftype=", reftype ($ref), "\n" ;
886 }
887 my $obj = '' ;
888 $obj = blessed ($val) . ' ' if (blessed ($val)) ;
889  
890 if (reftype ($val) eq 'SCALAR')
891 {
892 push @$vars,
893 {
894 name => $key,
895 value => $obj . $self -> get_scalar ($$val),
896 type => 'Scalar',
897 } ;
898 }
899  
900 if (reftype ($val) eq 'ARRAY')
901 {
902 my $display = $obj . '[' ;
903 my $n = 1 ;
904 foreach (@$val)
905 {
906 $display .= ',' if ($n > 1) ;
907 $display .= $self -> get_scalar ($_) ;
908 if ($n++ >= $max_display)
909 {
910 $display .= ',...' ;
911 last ;
912 }
913 }
914 $display .= ']' ;
915  
916 push @$vars,
917 {
918 name => $key,
919 value => $display,
920 type => 'Array',
921 var_ref => $ref,
922 indexedVariables => scalar (@$val),
923 } ;
924 }
925  
926 if (reftype ($val) eq 'HASH')
927 {
928 my $display = $obj . '{' ;
929 my $n = 1 ;
930 my $iterator = Hash::SafeKeys::save_iterator_state($val);
931 foreach (sort keys %$val)
932 {
933 $display .= ',' if ($n > 1) ;
934 $display .= "$_=>" . $self -> get_scalar ($val->{$_}) ;
935 if ($n++ >= $max_display / 2)
936 {
937 $display .= ',...' ;
938 last ;
939 }
940 }
941 $display .= '}' ;
942  
943 push @$vars,
944 {
945 name => $key,
946 value => $display,
947 type => 'Hash',
948 var_ref => $ref,
949 namedVariables => scalar (keys %$val),
950 } ;
951 Hash::SafeKeys::restore_iterator_state($val, $iterator);
952 }
953  
954 if ($key =~ /^Handle/)
955 {
956 push @$vars,
957 {
958 name => $key,
959 value => $$val,
960 type => 'Filehandle',
961 } ;
962 }
963 }
964 }
965  
966 # ---------------------------------------------------------------------------
967  
968 sub get_varsrc
969 {
970 my ($class, $frame_ref, $package, $type) = @_ ;
971  
972 my @vars ;
973 my $varsrc ;
974 if ($type eq 'l')
975 {
976 ($varsrc) = $class -> get_locals($frame_ref+3) ;
977 }
978 elsif ($type eq 'a')
979 {
980 ($varsrc) = $class -> get_arguments($frame_ref+3) ;
981 }
982 elsif ($type eq 'g')
983 {
984 $varsrc = $class -> get_globals($package) ;
985 }
986 elsif ($type eq 's')
987 {
988 $varsrc = $class -> get_globals() ;
989 }
990 elsif ($type =~ /^eg:(.+)/)
991 {
992 $varsrc = $class -> get_var_eval ($1) ;
993 }
994 elsif ($type =~ /^el:(.+)/)
995 {
996 my $name = $1 ;
997 my ($dummy, $varlocal) = $class -> get_locals($frame_ref+3) ;
998 $varsrc = $class -> get_var_eval ($name, $varlocal) ;
999 }
1000 elsif ($type =~ /^ea:(.+)/)
1001 {
1002 my $name = $1 ;
1003 my ($args, $varlocal) = $class -> get_arguments($frame_ref+3) ;
1004 $varsrc = $class -> get_var_eval ($name, $args, 'ea:') ;
1005 }
1006  
1007 use Data::Dump qw{pp} ;
1008 #print STDERR "vars ", pp ($varsrc),"\n" ;
1009 return $varsrc ;
1010 }
1011  
1012 # ---------------------------------------------------------------------------
1013  
1014 sub req_vars
1015 {
1016 my ($class, $params, $recurse) = @_ ;
1017  
1018 my $thread_ref = $params -> {thread_ref} ;
1019 my $tid = defined ($Coro::current)?$Coro::current+0:1 ;
1020 if ($thread_ref != $tid && !$recurse && ($params -> {type} !~ /^eg:/))
1021 {
1022 my $coro ;
1023 $coro = $class -> find_coro ($thread_ref) ;
1024 return { variables => [] } if (!$coro) ;
1025 my $ret ;
1026 $coro -> call (sub {
1027 $ret = $class -> req_vars ($params, $recurse + 1) ;
1028 }) ;
1029 return $ret ;
1030 }
1031  
1032 my $frame_ref = $params -> {frame_ref} - $recurse ;
1033 my $package = $params -> {'package'} ;
1034 my $type = $params -> {type} ;
1035 my $filter = $params -> {filter} ;
1036 my @vars ;
1037  
1038 my $varsrc = $class -> get_varsrc ($frame_ref, $package, $type) ;
1039  
1040 eval
1041 {
1042 $class -> get_vars ($varsrc, \@vars, $filter) ;
1043 } ;
1044 $class -> logger ($@) if ($@) ;
1045  
1046 return { variables => \@vars } ;
1047 }
1048  
1049 # ---------------------------------------------------------------------------
1050  
1051 sub _set_var_expr
1052 {
1053 my ($class, $type, $setvar, $expr_ref) = @_ ;
1054  
1055 if (!$type)
1056 {
1057 if ($setvar)
1058 {
1059 $$expr_ref = $setvar . '=' . $$expr_ref ;
1060 }
1061 return ;
1062 }
1063  
1064 my $refexpr ;
1065 if ($type =~ /^eg:(.+)/)
1066 {
1067 $refexpr = $1 ;
1068 my $ref = eval ($refexpr) ;
1069 return
1070 {
1071 name => "ERROR",
1072 value => $@,
1073 } if ($@) ;
1074 if (reftype ($ref) eq 'ARRAY')
1075 {
1076 $refexpr .= '[' . $setvar . ']' ;
1077 }
1078 elsif (reftype ($ref) eq 'HASH')
1079 {
1080 $refexpr .= '{' . $setvar . '}' ;
1081 }
1082 elsif (reftype ($ref) eq 'SCALAR')
1083 {
1084 $refexpr = '${' . $refexpr . '}' ;
1085 }
1086 else
1087 {
1088 return
1089 {
1090 name => "ERROR",
1091 value => "Cannot set variable if reference is of type " . reftype ($ref) ,
1092 } ;
1093 }
1094 }
1095 else
1096 {
1097 return
1098 {
1099 name => "ERROR",
1100 value => "Invalid type: $type",
1101 } ;
1102 }
1103  
1104 $$expr_ref = $refexpr . '=' . $$expr_ref ;
1105  
1106 return ;
1107 }
1108  
1109  
1110 # ---------------------------------------------------------------------------
1111  
1112 sub req_setvar
1113 {
1114 my ($class, $params) = @_ ;
1115  
1116 my $thread_ref = $params -> {thread_ref} ;
1117 my $tid = defined ($Coro::current)?$Coro::current+0:1 ;
1118 return undef if ($thread_ref != $tid) ;
1119  
1120 my $frame_ref = $params -> {frame_ref} ;
1121 my $package = $params -> {'package'} ;
1122 my $expression = $params -> {'expression'} ;
1123 my $setvar = $params -> {'setvar'} ;
1124 my $type = $params -> {'type'} ;
1125 my @vars ;
1126 my $resultsrc ;
1127 my $varref ;
1128 my $varsrc = $class -> get_varsrc ($frame_ref, $package, $type) ;
1129 if (!exists $varsrc -> {$setvar})
1130 {
1131 return
1132 {
1133 name => "ERROR",
1134 value => "unknown variable: $setvar",
1135 } ;
1136 }
1137 $varref = $varsrc -> {$setvar}[0] ;
1138 eval
1139 {
1140 $resultsrc = $class -> get_eval_result ($frame_ref+2, $package, $expression) ;
1141  
1142 $$varref = ${$resultsrc -> {eval}[0]} ;
1143 } ;
1144 return
1145 {
1146 name => "ERROR",
1147 value => $@,
1148 } if ($@) ;
1149  
1150 return
1151 {
1152 name => $setvar,
1153 value => "$$varref",
1154 } ;
1155 }
1156  
1157 # ---------------------------------------------------------------------------
1158  
1159 sub req_evaluate
1160 {
1161 my ($class, $params, $recurse) = @_ ;
1162  
1163 return undef if ($params -> {'context'} eq 'hover' && ($params -> {'expression'} !~ /^\s*\\?[\$\@\%]/)) ;
1164  
1165 my $thread_ref = $params -> {thread_ref} ;
1166 my $tid = defined ($Coro::current)?$Coro::current+0:1 ;
1167 if ($thread_ref != $tid && !$recurse)
1168 {
1169 my $coro ;
1170 $coro = $class -> find_coro ($thread_ref) ;
1171 return undef if (!$coro) ;
1172 my $ret ;
1173 $coro -> call (sub {
1174 $ret = $class -> req_evaluate ($params, $recurse + 1) ;
1175 }) ;
1176 return $ret ;
1177 }
1178  
1179 my $frame_ref = $params -> {frame_ref} - $recurse ;
1180 my $package = $params -> {'package'} ;
1181 my $expression = $params -> {'expression'} ;
1182 my @vars ;
1183 my $varsrc ;
1184  
1185 eval
1186 {
1187 $varsrc = $class -> get_eval_result ($frame_ref+2, $package, $expression) ;
1188  
1189 $class -> get_vars ($varsrc, \@vars) ;
1190 } ;
1191 return
1192 {
1193 name => "ERROR",
1194 value => $@,
1195 } if ($@) ;
1196  
1197 return $vars[0] ;
1198 }
1199  
1200 # ---------------------------------------------------------------------------
1201  
1202 sub req_threads
1203 {
1204 my @threads ;
1205  
1206 if (defined &Coro::State::list)
1207 {
1208 foreach my $coro (Coro::State::list())
1209 {
1210 push @threads,
1211 {
1212 name => $coro->debug_desc,
1213 thread_ref => $coro+0,
1214 } ;
1215 }
1216 }
1217 else
1218 {
1219 @threads = { thread_ref => 1, name => 'single'} ;
1220 }
1221  
1222 return { threads => \@threads } ;
1223 }
1224  
1225 # ---------------------------------------------------------------------------
1226  
1227  
1228 sub find_coro
1229 {
1230 my ($class, $pid) = @_;
1231  
1232 return if (!defined &Coro::State::list) ;
1233  
1234 if (my ($coro) = grep ($_ == $pid, Coro::State::list()))
1235 {
1236 return $coro ;
1237 }
1238 else
1239 {
1240 $class -> logger ("$pid: no such coroutine\n") ;
1241 }
1242 return ;
1243 }
1244  
1245 # ---------------------------------------------------------------------------
1246  
1247 sub req_stack
1248 {
1249 my ($class, $params, $recurse) = @_ ;
1250  
1251 my $thread_ref = $params -> {thread_ref} ;
1252 my $tid = defined ($Coro::current)?$Coro::current+0:1 ;
1253 if ($thread_ref != $tid && !$recurse)
1254 {
1255 my $coro ;
1256 $coro = $class -> find_coro ($thread_ref) ;
1257 return { stackFrames => [] } if (!$coro) ;
1258 my $ret ;
1259 $coro -> call (sub {
1260 $ret = $class -> req_stack ($params, 1) ;
1261 }) ;
1262 return $ret ;
1263 }
1264  
1265 my $levels = $params -> {levels} || 999 ;
1266 my $start_frame = $params -> {start} || 0 ;
1267 $start_frame += 3 ;
1268 my @stack ;
1269 {
1270 package DB;
1271  
1272 my $i = 0 ;
1273  
1274 my @frames ;
1275 while ((my @call_info = caller($i++)))
1276 {
1277 my $sub = $call_info[3] ;
1278 push @frames, \@call_info ;
1279 $frames[-2][3] = $sub if (@frames > 1);
1280 }
1281 $frames[-1][3] = '<main>' if (@frames > 0);
1282  
1283 my $n = @frames + 1 ;
1284 $i = $n ;
1285 my $j = -1 ;
1286 while (my $frame = shift @frames)
1287 {
1288 $i-- ;
1289 $j++ ;
1290 next if ($start_frame-- > 0) ;
1291 last if ($levels-- <= 0) ;
1292  
1293 my ($package, $filename, $line, $subroutine, $hasargs) = @$frame ;
1294  
1295 my $sub_name = $subroutine ;
1296 $sub_name = $1 if ($sub_name =~ /.+::(.+?)$/) ;
1297  
1298 my $frame =
1299 {
1300 frame_ref => $j,
1301 name => $sub_name,
1302 source => { path => $filename },
1303 line => $line,
1304 column => 1,
1305 #moduleId => $package,
1306 'package' => $package,
1307 } ;
1308 $j-- if ($sub_name eq '(eval)') ;
1309 push @stack, $frame ;
1310 }
1311 }
1312  
1313 return { stackFrames => \@stack } ;
1314 }
1315  
1316 # ---------------------------------------------------------------------------
1317  
1318 sub _set_breakpoint
1319 {
1320 my ($class, $location, $condition) = @_ ;
1321  
1322 $condition ||= '1';
1323 my $subname ;
1324 my $filename ;
1325 ($location, $subname, $filename) = DB::_find_subline($location) if ($location =~ /\D/);
1326  
1327 return (0, "Subroutine not found.") unless $location ;
1328 return (0) if (!$location) ;
1329  
1330 local *dbline = "::_<$filename" if ($filename) ;
1331 for (my $line = $location; $line <= $location + 10 && $location < @dbline; $line++)
1332 {
1333 if ($dbline[$line] != 0)
1334 {
1335 $dbline{$line+0} =~ s/^[^\0]*/$condition/;
1336 return (1, undef, $line, $filename) ;
1337 }
1338 }
1339  
1340 return (0, "Line $location for sub $subname is not breakable.") if ($subname) ;
1341 return (0, "Line $location is not breakable.") ;
1342 }
1343  
1344 # ---------------------------------------------------------------------------
1345 # abs path no dereference
1346 # copied from package Cwd::Ext and added directory argument
1347 sub abs_path_nd {
1348 my $abs_path = shift;
1349 my $dir = shift ;
1350 return $abs_path if $abs_path=~m{^/$};
1351  
1352 unless( $abs_path=~/^\// ){
1353 if ($dir) {
1354 $abs_path = $dir."/$abs_path";
1355 }
1356 else {
1357 require Cwd;
1358 $abs_path = Cwd::cwd()."/$abs_path";
1359 }
1360 }
1361  
1362 my @elems = split m{/}, $abs_path;
1363 my $ptr = 1;
1364 while($ptr <= $#elems){
1365 if($elems[$ptr] eq '' ){
1366 splice @elems, $ptr, 1;
1367 }
1368  
1369 elsif($elems[$ptr] eq '.' ){
1370 splice @elems, $ptr, 1;
1371 }
1372  
1373 elsif($elems[$ptr] eq '..' ){
1374 if($ptr < 2){
1375 splice @elems, $ptr, 1;
1376 }
1377 else {
1378 $ptr--;
1379 splice @elems, $ptr, 2;
1380 }
1381 }
1382 else {
1383 $ptr++;
1384 }
1385 }
1386  
1387 $#elems ? join q{/}, @elems : q{/};
1388 }
1389  
1390 # ---------------------------------------------------------------------------
1391  
1392 sub req_breakpoint
1393 {
1394 my ($class, $params) = @_ ;
1395  
1396 my $breakpoints = $params -> {breakpoints} ;
1397 my $filename = $params -> {filename} ;
1398 my $real_filename = $params -> {dbg_filename} || $filename ;
1399  
1400 Class::Refresh -> refresh if ($refresh) ;
1401  
1402 if ($filename)
1403 {
1404 my %seen ;
1405 while (!defined $main::{'_<' . $real_filename} && -l $real_filename)
1406 {
1407 my $dir = File::Basename::dirname ($real_filename) ;
1408 $real_filename = readlink ($real_filename) ;
1409 last if (!$real_filename) ;
1410 $real_filename = abs_path_nd ($real_filename, $dir) ;
1411 last if ($seen{$real_filename}++) ;
1412 }
1413  
1414 if (!defined $main::{'_<' . $real_filename})
1415 {
1416 $postponed_breakpoints{$filename} = $breakpoints ;
1417 foreach my $bp (@$breakpoints)
1418 {
1419 $bp -> [6] = $breakpoint_id++ ;
1420 }
1421 return { breakpoints => $breakpoints }
1422 }
1423 }
1424  
1425 local *dbline = "::_<$real_filename" if ($real_filename) ;
1426 if ($real_filename)
1427 {
1428 # Switch the magical hash temporarily.
1429 local *DB::dbline = "::_<$real_filename";
1430 $class -> clr_breaks () ;
1431 $class -> clr_actions () ;
1432 }
1433  
1434 foreach my $bp (@$breakpoints)
1435 {
1436 my $line = $bp -> [0] ;
1437 my $condition = $bp -> [1] ;
1438 ($bp -> [2], $bp -> [3], $bp -> [4], $bp -> [5]) = $class -> _set_breakpoint ($line, $condition) ;
1439 $bp -> [5] = $filename if ($filename) ;
1440 }
1441 return { breakpoints_set => 1, breakpoints => $breakpoints, ($filename ne $real_filename?(real_filename => $real_filename, req_filename => $filename):()) };
1442 }
1443  
1444 # ---------------------------------------------------------------------------
1445  
1446 package DB
1447 {
1448 use vars qw{@dbline %dbline $dbline} ;
1449  
1450 sub postponed
1451 {
1452 my ($arg) = @_ ;
1453  
1454 return if (!$loaded) ;
1455  
1456 # If this is a subroutine...
1457 if (ref(\$arg) ne 'GLOB')
1458 {
1459 return ;
1460 }
1461 # Not a subroutine. Deal with the file.
1462 local *dbline = $arg ;
1463 my $filename = $dbline;
1464 my %seen ;
1465 my $pp_filename = $filename ;
1466 while (!exists $postponed_breakpoints{$pp_filename} && -l $pp_filename)
1467 {
1468 my $dir = File::Basename::dirname ($pp_filename) ;
1469 $pp_filename = readlink ($pp_filename) ;
1470 last if (!$pp_filename) ;
1471 $pp_filename = Perl::LanguageServer::DebuggerInterface::abs_path_nd ($pp_filename, $dir) ;
1472 last if ($seen{$pp_filename}++) ;
1473 }
1474  
1475 #Perl::LanguageServer::DebuggerInterface -> _send ({ command => 'di_loadedfile', arguments => { session_id => $session, reason => 'new', source => { path => $filename}}}) ;
1476  
1477 if (exists $postponed_breakpoints{$pp_filename})
1478 {
1479 my $ret = Perl::LanguageServer::DebuggerInterface -> req_breakpoint ({ breakpoints => $postponed_breakpoints{$pp_filename}, filename => $pp_filename, dbg_filename => $filename }) ;
1480 if ($ret -> {breakpoints_set})
1481 {
1482 delete $postponed_breakpoints{$pp_filename} ;
1483 Perl::LanguageServer::DebuggerInterface -> _send ({ command => 'di_breakpoints',
1484 arguments => { session_id => $session, %$ret}}) ;
1485 }
1486 }
1487 }
1488 }
1489  
1490 # ---------------------------------------------------------------------------
1491  
1492 sub req_source
1493 {
1494 my ($class, $params) = @_ ;
1495  
1496 my $filename = $params -> {filename} ;
1497 my $source = join("", @{$main::{'_<'.$filename}});
1498 $source =~ s/\n;$//;
1499
1500 return { content => $source };
1501 }
1502  
1503 # ---------------------------------------------------------------------------
1504  
1505 sub req_can_break
1506 {
1507 my ($class, $params) = @_ ;
1508  
1509 my $line = $params -> {line} ;
1510 my $end_line = $params -> {end_line} || $line ;
1511 my $filename = $params -> {filename} ;
1512 my $real_filename = $filename ;
1513  
1514 my %seen ;
1515 while (!defined $main::{'_<' . $real_filename} && -l $real_filename)
1516 {
1517 my $dir = File::Basename::dirname ($real_filename) ;
1518 $real_filename = readlink ($real_filename) ;
1519 last if (!$real_filename) ;
1520 $real_filename = abs_path_nd ($real_filename, $dir) ;
1521 last if ($seen{$real_filename}++) ;
1522 }
1523  
1524 return { breakpoints => [] } if (!defined $main::{'_<' . $real_filename}) ;
1525  
1526 Class::Refresh -> refresh if ($refresh) ;
1527  
1528 # Switch the magical hash temporarily.
1529 local *dbline = "::_<$real_filename";
1530  
1531 my @bp ;
1532 for (my $i = $line; $i <= $end_line; $i++)
1533 {
1534 if ($dbline[$line] != 0)
1535 {
1536 push @bp, { line => $line } ;
1537 }
1538 }
1539  
1540 return { breakpoints => \@bp };
1541 }
1542  
1543  
1544 # ---------------------------------------------------------------------------
1545  
1546 sub req_continue
1547 {
1548 my ($class, $params) = @_ ;
1549  
1550 Class::Refresh -> refresh if ($refresh) ;
1551  
1552 @evalresult = () ;
1553 $class -> cont ;
1554  
1555 return ;
1556 }
1557  
1558 # ---------------------------------------------------------------------------
1559  
1560 sub req_step_in
1561 {
1562 my ($class, $params) = @_ ;
1563  
1564 Class::Refresh -> refresh if ($refresh) ;
1565  
1566 @evalresult = () ;
1567 $class -> step ;
1568  
1569 return ;
1570 }
1571  
1572 # ---------------------------------------------------------------------------
1573  
1574 sub req_step_out
1575 {
1576 my ($class, $params) = @_ ;
1577  
1578 Class::Refresh -> refresh if ($refresh) ;
1579  
1580 @evalresult = () ;
1581 $class -> ret (2) ;
1582  
1583 return ;
1584 }
1585  
1586 # ---------------------------------------------------------------------------
1587  
1588 sub req_next
1589 {
1590 my ($class, $params) = @_ ;
1591  
1592 Class::Refresh -> refresh if ($refresh) ;
1593  
1594 @evalresult = () ;
1595 $class -> next ;
1596  
1597 return ;
1598 }
1599  
1600  
1601 # ---------------------------------------------------------------------------
1602  
1603 sub _send
1604 {
1605 my ($class, $result) = @_ ;
1606  
1607 $result -> {type} = 'dbgint' ;
1608  
1609 my $outdata = $json -> encode ($result) ;
1610 use bytes ;
1611 my $len = length($outdata) ;
1612 my $wrdata = "Content-Length: $len\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n$outdata" ;
1613 $socket -> syswrite ($wrdata) ;
1614 if ($debug)
1615 {
1616 $wrdata =~ s/\r//g ;
1617 $class -> logger ($wrdata, "\n") ;
1618 }
1619 }
1620  
1621  
1622 # ---------------------------------------------------------------------------
1623  
1624 sub _recv
1625 {
1626 my ($class) = @_ ;
1627  
1628 $class -> logger ("wait for input\n") if ($debug) ;
1629  
1630 my $line ;
1631 my $cnt ;
1632 my $buffer ;
1633 my $data ;
1634 my %header ;
1635 header:
1636 while (1)
1637 {
1638 $cnt = sysread ($socket, $buffer, 8192, length ($buffer)) ;
1639 die "read_error reading headers ($!)" if ($cnt < 0) ;
1640 return if ($cnt == 0) ;
1641  
1642 while ($buffer =~ s/^(.*?)\R//)
1643 {
1644 $line = $1 ;
1645 $class -> logger ("line=<$line>\n") if ($debug) ;
1646 last header if ($line eq '') ;
1647 $header{$1} = $2 if ($line =~ /(.+?):\s*(.+)/) ;
1648 }
1649 }
1650  
1651 my $len = $header{'Content-Length'} ;
1652 my $data ;
1653 $class -> logger ("len=$len len buffer=", length ($buffer), "\n") if ($debug) ;
1654 while ($len > length ($buffer))
1655 {
1656 $cnt = sysread ($socket, $buffer, $len - length ($buffer), length ($buffer)) ;
1657 die "read_error reading data ($!)" if ($cnt < 0) ;
1658 return if ($cnt == 0) ;
1659 }
1660 if ($len == length ($buffer))
1661 {
1662 $data = $buffer ;
1663 $buffer = '' ;
1664 }
1665 elsif ($len < length ($buffer))
1666 {
1667 $data = substr ($buffer, 0, $len) ;
1668 $buffer = substr ($buffer, $len) ;
1669 }
1670 else
1671 {
1672 die "to few data bytes" ;
1673 }
1674 $class -> logger ("read data=", $data, "\n") if ($debug) ;
1675 $class -> logger ("read header=", "%header", "\n") if ($debug) ;
1676  
1677 my $cmddata = $json -> decode ($data) ;
1678 my $cmd = 'req_' . $cmddata -> {command} ;
1679 if ($class -> can ($cmd))
1680 {
1681 my $result = $class -> $cmd ($cmddata) ;
1682 $class -> _send ({ command => 'di_response', seq => $cmddata -> {seq}, arguments => $result}) ;
1683 return ;
1684 }
1685 die "unknown cmd $cmd" ;
1686 }
1687  
1688  
1689 # ---------------------------------------------------------------------------
1690  
1691 sub awaken
1692 {
1693 my ($class) = @_ ;
1694 $class -> logger ("enter awaken\n") if ($debug) ;
1695  
1696 $break_reason = 'pause' ;
1697 #$class -> _send ({ command => 'di_break', arguments => { session_id => $session, reason => 'pause'}}) ;
1698 }
1699  
1700 # ---------------------------------------------------------------------------
1701  
1702 sub init
1703 {
1704 my ($class) = @_ ;
1705  
1706 $class -> logger ("enter init\n") if ($debug) ;
1707  
1708 $refresh = ($ENV{PLSDI_OPTIONS} =~ /reload_modules/)?1:0 ;
1709 if ($refresh)
1710 {
1711 require Class::Refresh ;
1712 Class::Refresh -> refresh ;
1713 }
1714  
1715 my $remote ;
1716 my $port ;
1717 ($remote, $port) = split /:/, $ENV{PLSDI_REMOTE} ;
1718 if ($remote =~ m/^([0-9.]+)$/) {
1719 $remote = $1; # untaint
1720 }
1721 if ($port =~ m/^(\d+)$/) {
1722 $port = $1; # untaint
1723 }
1724  
1725 $socket = IO::Socket::INET->new(PeerAddr => $remote,
1726 PeerPort => $port,
1727 Proto => 'tcp')
1728 or die "Cannot connect to $remote:$port ($!)";
1729  
1730 $class -> ready (1) ;
1731 }
1732  
1733 # ---------------------------------------------------------------------------
1734  
1735 sub stop
1736 {
1737 my ($class) = @_ ;
1738 $class -> logger ("enter stop @_\n") if ($debug) ;
1739  
1740 }
1741  
1742 # ---------------------------------------------------------------------------
1743  
1744 sub idle
1745 {
1746 my ($class) = @_ ;
1747 $class -> logger ("enter idle @_\n") if ($debug) ;
1748  
1749 my $cmd = $class -> _recv () ;
1750  
1751 }
1752  
1753 # ---------------------------------------------------------------------------
1754  
1755 sub cleanup
1756 {
1757 my ($class) = @_ ;
1758 $class -> logger ("enter cleanup @_\n") if ($debug) ;
1759  
1760 }
1761  
1762 # ---------------------------------------------------------------------------
1763  
1764 sub output
1765 {
1766 my ($class) = @_ ;
1767 $class -> logger ("enter output @_\n") if ($debug) ;
1768  
1769 }
1770  
1771 # ---------------------------------------------------------------------------
1772  
1773 sub showfile
1774 {
1775 my ($class, $filename, $line) = @_ ;
1776 $class -> logger ("enter showfile @_\n") if ($debug) ;
1777  
1778 #$class -> _send ({ command => 'di_showfile', arguments => { session_id => $session, reason => 'new', source => { path => $filename}}}) ;
1779 }
1780  
1781 # ---------------------------------------------------------------------------
1782  
1783 sub evalcode
1784 {
1785 my ($class) = @_ ;
1786 $class -> logger ("enter evalcode @_\n") if ($debug) ;
1787  
1788 }
1789  
1790 # ---------------------------------------------------------------------------
1791  
1792 sub cprestop
1793 {
1794 my ($class) = @_ ;
1795 $class -> logger ("enter cprestop @_\n") if ($debug) ;
1796  
1797 @evalresult = () ;
1798 my $tid = defined ($Coro::current)?$Coro::current+0:1 ;
1799 $class -> _send ({ command => 'di_break',
1800 arguments =>
1801 {
1802 thread_ref => $tid,
1803 session_id => $session,
1804 ($break_reason?(reason => $break_reason):()),
1805 }}) ;
1806 $break_reason = undef ;
1807 }
1808  
1809 # ---------------------------------------------------------------------------
1810  
1811 sub cpoststop
1812 {
1813 my ($class) = @_ ;
1814 $class -> logger ("enter cpoststop @_\n") if ($debug) ;
1815 }
1816  
1817  
1818 # ---------------------------------------------------------------------------
1819  
1820 $loaded = 1 ;
1821  
1822 1 ;