LanguageServer/Methods/DebugAdapter.pm untested

filter: Statementstmt 0.0% 0 / 251 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 386 CC 25 SCAR 20.8
CC
25
Cov
0%
_temp_break
20.0
_temp_cont
20.0
_set_breakpoints
20.0
CRAP 8.0
1 package Perl::LanguageServer::Methods::DebugAdapter ;
2  
3 use Moose::Role ;
4  
5 use Coro ;
6 use Coro::AIO ;
7 use Data::Dump qw{dump pp} ;
8 use Perl::LanguageServer::DevTool ;
9 use Perl::LanguageServer::DebuggerProcess ;
10  
11 no warnings 'uninitialized' ;
12  
13 our %debug_adapters ;
14  
15 # ---------------------------------------------------------------------------
16  
17 has 'debugger_process' =>
18 (
19 isa => 'Perl::LanguageServer::DebuggerProcess',
20 is => 'rw'
21 ) ;
22  
23 has 'debug_adapter_interface' =>
24 (
25 isa => 'Perl::LanguageServer',
26 is => 'rw',
27 weak_ref => 1,
28 ) ;
29  
30 has 'ref2id' =>
31 (
32 isa => 'HashRef',
33 is => 'rw',
34 default => sub { {} },
35 ) ;
36  
37 has 'id2ref' =>
38 (
39 isa => 'HashRef',
40 is => 'rw',
41 default => sub { {} },
42 ) ;
43  
44 has 'refcnt' =>
45 (
46 isa => 'Int',
47 is => 'rw',
48 default => 1,
49 ) ;
50  
51 has 'running' =>
52 (
53 isa => 'Int',
54 is => 'rw',
55 default => 0,
56 ) ;
57  
58 has 'in_temp_break' =>
59 (
60 isa => 'Int',
61 is => 'rw',
62 default => 0,
63 ) ;
64  
65 # ---------------------------------------------------------------------------
66  
67 sub getid
68 {
69 my ($self, $parentid, $ref, $param) = @_ ;
70  
71 my $refs = $self -> ref2id ;
72 my $ndx = $parentid . ':' . $ref ;
73  
74 return $refs -> {$ndx} + 0 if (exists $refs -> {$ndx}) ;
75  
76 my $refcnt = $self -> refcnt ;
77 $self -> id2ref -> {$refcnt} = { ref => $ref, ($param?%$param:()) } ;
78 $refs -> {$ndx} = $refcnt+0 ;
79 $refcnt++ ;
80 return $self -> refcnt ($refcnt) - 1 ; # make sure there is no string value, so encode json encodes it as number
81 }
82  
83 # ---------------------------------------------------------------------------
84  
85 sub clear_non_thread_ids
86 {
87 my ($self) = @_ ;
88  
89 my $refs = $self -> ref2id ;
90 my $id2refs = $self -> id2ref ;
91 my $id ;
92 foreach (keys %$refs)
93 {
94 if (/^0:/)
95 {
96 $id = delete $refs -> {$_} ;
97 delete $id2refs -> {$id} ;
98 }
99 }
100 }
101  
102 # ---------------------------------------------------------------------------
103  
104 sub send_event
105 {
106 my ($self, $event, $body) = @_ ;
107  
108 $self -> send_notification ({ type => 'event', event => $event, body => $body }, $self) ;
109 }
110  
111 # ---------------------------------------------------------------------------
112  
113 sub send_request
114 {
115 my ($self, $command, $body) = @_ ;
116  
117 return $self -> debug_adapter_interface -> request ({ command => $command, $body?%$body:() }) ;
118 }
119  
120  
121 # ---------------------------------------------------------------------------
122  
123  
124 sub _dapreq_initialize
125 {
126 my ($self, $workspace, $req) = @_ ;
127  
128 $self -> log_prefix ('DA') ;
129  
130 $Perl::LanguageServer::dev_tool = Perl::LanguageServer::DevTool -> new ({ config => $req -> params }) ;
131 $Perl::LanguageServer::workspace ||= Perl::LanguageServer::Workspace -> new ({ config =>{} }) ;
132  
133 #$self -> logger ('initialize debug adapter', dump ($req -> params),"\n") ;
134  
135 my $caps =
136 {
137 # The debug adapter supports the 'configurationDone' request.
138 supportsConfigurationDoneRequest => JSON::true(),
139  
140 # The debug adapter supports function breakpoints.
141 supportsFunctionBreakpoints => JSON::false(),
142  
143 # The debug adapter supports conditional breakpoints.
144 supportsConditionalBreakpoints => JSON::true(),
145  
146 # The debug adapter supports breakpoints that break execution after a specified number of hits.
147 supportsHitConditionalBreakpoints => JSON::false(),
148  
149 # The debug adapter supports a (side effect free) evaluate request for data hovers.
150 supportsEvaluateForHovers => JSON::true(),
151  
152 # Available filters or options for the setExceptionBreakpoints request.
153 exceptionBreakpointFilters => [],
154  
155 # The debug adapter supports stepping back via the 'stepBack' and 'reverseContinue' requests.
156 supportsStepBack => JSON::false(),
157  
158 # The debug adapter supports setting a variable to a value.
159 supportsSetVariable => JSON::true(),
160  
161 # The debug adapter supports restarting a frame.
162 supportsRestartFrame => JSON::false(),
163  
164 # The debug adapter supports the 'gotoTargets' request.
165 supportsGotoTargetsRequest => JSON::false(),
166  
167 # The debug adapter supports the 'stepInTargets' request.
168 supportsStepInTargetsRequest => JSON::false(),
169  
170 # The debug adapter supports the 'completions' request.
171 supportsCompletionsRequest => JSON::false(),
172  
173 # The set of characters that should trigger completion in a REPL. If not specified, the UI should assume the '.' character.
174 completionTriggerCharacters => [],
175  
176 # The debug adapter supports the 'modules' request.
177 supportsModulesRequest => JSON::true(),
178  
179 # The set of additional module information exposed by the debug adapter.
180 additionalModuleColumns => [],
181  
182 # Checksum algorithms supported by the debug adapter.
183 supportedChecksumAlgorithms => [],
184  
185 # The debug adapter supports the 'restart' request. In this case a client should not implement 'restart' by terminating and relaunching the adapter but by calling the RestartRequest.
186 supportsRestartRequest => JSON::false(),
187  
188 # The debug adapter supports 'exceptionOptions' on the setExceptionBreakpoints request.
189 supportsExceptionOptions => JSON::false(),
190  
191 # The debug adapter supports a 'format' attribute on the stackTraceRequest, variablesRequest, and evaluateRequest.
192 supportsValueFormattingOptions => JSON::false(),
193  
194 # The debug adapter supports the 'exceptionInfo' request.
195 supportsExceptionInfoRequest => JSON::false(),
196  
197 # The debug adapter supports the 'terminateDebuggee' attribute on the 'disconnect' request.
198 supportTerminateDebuggee => JSON::true(),
199  
200 # The debug adapter supports the delayed loading of parts of the stack, which requires that both the 'startFrame' and 'levels' arguments and the 'totalFrames' result of the 'StackTrace' request are supported.
201 supportsDelayedStackTraceLoading => JSON::true(),
202  
203 # The debug adapter supports the 'loadedSources' request.
204 supportsLoadedSourcesRequest => JSON::true(),
205  
206 # The debug adapter supports logpoints by interpreting the 'logMessage' attribute of the SourceBreakpoint.
207 supportsLogPoints => JSON::false(),
208  
209 # The debug adapter supports the 'terminateThreads' request.
210 supportsTerminateThreadsRequest => JSON::true(),
211  
212 # The debug adapter supports the 'setExpression' request.
213 supportsSetExpression => JSON::true(),
214  
215 # The debug adapter supports the 'terminate' request.
216 supportsTerminateRequest => JSON::true(),
217  
218 # The debug adapter supports data breakpoints.
219 supportsDataBreakpoints => JSON::false(),
220  
221 # The debug adapter supports the 'readMemory' request.
222 supportsReadMemoryRequest => JSON::false(),
223  
224 # The debug adapter supports the 'disassemble' request.
225 supportsDisassembleRequest => JSON::false(),
226  
227 # The debug adapter supports the 'cancel' request.
228 supportsCancelRequest => JSON::true(),
229  
230 # The debug adapter supports the 'breakpointLocations' request.
231 supportsBreakpointLocationsRequest => JSON::true(),
232 } ;
233  
234 return $caps ;
235 }
236  
237 # ---------------------------------------------------------------------------
238  
239 sub _check_not_running
240 {
241 my ($self, $workspace) = @_ ;
242  
243 if ($self -> running)
244 {
245 die "Debuggee is running" ;
246 }
247 return ;
248 }
249  
250 # ---------------------------------------------------------------------------
251  
252 sub _temp_break
253 {
254 my ($self, $workspace) = @_ ;
255  
256 my $running = $self -> running ;
257 return if (!$running) ;
258  
259 my $temp_break_guard = Guard::guard
260 {
261 $self -> _temp_cont ($workspace, $running) ;
262 } ;
263  
264 my $cnt = 50 ;
265 my $itb = $self -> in_temp_break ;
266 $self -> in_temp_break ($itb + 1) ;
267 $self -> logger ("in_temp_break = ", $itb + 1, "\n") ;
268 $self -> _dapreq_pause ($workspace) if ($itb == 0);
269 while ($self -> running && $cnt-- > 0)
270 {
271 Coro::AnyEvent::sleep (0.1) ;
272 }
273 $self -> _check_not_running ($workspace) ;
274 $running = 0 if (!$self -> in_temp_break) ;
275  
276 return $temp_break_guard ;
277 }
278  
279 # ---------------------------------------------------------------------------
280  
281 sub _temp_cont
282 {
283 my ($self, $workspace, $old_running) = @_ ;
284  
285 my $itb = $self -> in_temp_break ;
286 $self -> logger ("temp_cont = $itb old_running = $old_running\n") ;
287 return if (!$old_running) ;
288 return if ($itb == 0) ;
289 $self -> in_temp_break ($itb - 1) ;
290 if ($itb == 1)
291 {
292 $self -> running (1) ;
293 $self -> send_request ('continue') ;
294 }
295 }
296  
297  
298 # ---------------------------------------------------------------------------
299  
300 sub _set_breakpoints
301 {
302 my ($self, $workspace, $req, $location, $breakpoints, $source) = @_ ;
303  
304 my $temp_break_guard = $self -> _temp_break ($workspace) ;
305  
306 my @bp ;
307 for (my $i; $i < @$breakpoints; $i++)
308 {
309 push @bp, [$breakpoints -> [$i]{$location}, $breakpoints -> [$i]{condition}]
310 }
311  
312 my $ret = $self -> send_request ('breakpoint',
313 {
314 breakpoints => \@bp,
315 ($source?(filename => $self -> debugger_process -> file_client2server ($workspace, $source -> {path})):()),
316 }) ;
317  
318 if ($req -> params -> {real_filename})
319 {
320 $workspace -> add_path_mapping ($req -> params -> {real_filename}, $self -> debugger_process -> file_server2client ($workspace, $req -> params -> {req_filename}))
321 }
322  
323 my @setbp ;
324 for (my $i; $i < @{$ret -> {breakpoints}}; $i++)
325 {
326 my $bp = $ret -> {breakpoints}[$i] ;
327 push @setbp,
328 {
329 verified => $bp -> [2]?JSON::true ():JSON::false (),
330 message => $bp -> [3],
331 line => $bp -> [4]+0,
332 id => $bp -> [6]+0,
333 source => { path => $self -> debugger_process -> file_server2client ($workspace, $bp -> [5]) },
334 }
335 }
336  
337 return { breakpoints => \@setbp } ;
338 }
339  
340 # ---------------------------------------------------------------------------
341  
342 sub _dapreq_setBreakpoints
343 {
344 my ($self, $workspace, $req) = @_ ;
345  
346 my $breakpoints = $req -> params -> {breakpoints} ;
347 my $source = $req -> params -> {source} ;
348  
349 return { breakpoints => [] } if (!$breakpoints || !$source);
350  
351 return $self -> _set_breakpoints ($workspace, $req, 'line', $breakpoints, $source) ;
352 }
353  
354 # ---------------------------------------------------------------------------
355  
356 sub _dapreq_setFunctionBreakpoints
357 {
358 my ($self, $workspace, $req) = @_ ;
359  
360 my $breakpoints = $req -> params -> {breakpoints} ;
361  
362 return { breakpoints => [] } if (!$breakpoints);
363  
364 return $self -> _set_breakpoints ($workspace, $req, 'name', $breakpoints) ;
365 }
366  
367 # ---------------------------------------------------------------------------
368  
369 sub _dapreq_setExceptionBreakpoints
370 {
371 my ($self, $workspace, $req) = @_ ;
372  
373 return {} ;
374 }
375  
376 # ---------------------------------------------------------------------------
377  
378 sub _dapreq_breakpointLocations
379 {
380 my ($self, $workspace, $req) = @_ ;
381  
382 my $dai = $self -> debug_adapter_interface ;
383 return { breakpoints => [] } if (!$dai || !$dai -> initialized) ;
384  
385 my $temp_break_guard = $self -> _temp_break ($workspace) ;
386  
387 my $source = $req -> params -> {source} ;
388 my $ret = $self -> send_request ('can_break',
389 {
390 line => $req -> params -> {line},
391 end_line => $req -> params -> {endLine},
392 ($source?(filename => $self -> debugger_process -> file_client2server ($workspace, $source -> {path})):()),
393 }) ;
394  
395 foreach (@{$ret -> {breakpoints}})
396 {
397 $_ -> {line} += 0 ;
398 }
399  
400 return $ret ;
401 }
402  
403 # ---------------------------------------------------------------------------
404  
405 sub _dapreq_configurationDone
406 {
407 my ($self, $workspace, $req) = @_ ;
408  
409 if (!$self -> debugger_process -> stop_on_entry)
410 {
411 $self -> running (1) ;
412 $self -> send_request ('continue') ;
413 $self -> send_event ('continued', { allThreadsContinued => JSON::true() }) ;
414 }
415  
416 return {} ;
417 }
418  
419 # ---------------------------------------------------------------------------
420  
421 sub _dapreq_launch
422 {
423 my ($self, $workspace, $req) = @_ ;
424  
425 $self -> _check_not_running ($workspace) ;
426  
427 $self -> running (1) ;
428 my $proc = Perl::LanguageServer::DebuggerProcess -> new ($req -> params) ;
429 $self -> debugger_process ($proc) ;
430 $proc -> debug_adapter ($self) ;
431 $debug_adapters{$proc -> session_id} = $self ;
432 $proc -> launch ($workspace, $workspace -> perlcmd) ;
433  
434 return {} ;
435 }
436  
437 # ---------------------------------------------------------------------------
438  
439 sub _dapreq_loadedSources
440 {
441 my ($self, $workspace, $req) = @_ ;
442  
443 my @sources = ( { path => $self -> debugger_process -> program });
444 return { sources => \@sources } ;
445 }
446  
447 # ---------------------------------------------------------------------------
448  
449 sub _dapreq_threads
450 {
451 my ($self, $workspace, $req) = @_ ;
452  
453 $self -> _check_not_running ($workspace) ;
454  
455 my $threads = $self -> send_request ('threads') ;
456 foreach (@{$threads -> {threads}})
457 {
458 $_ -> {id} = $self -> getid (0, $_ -> {thread_ref}) ;
459 }
460  
461 return $threads ;
462 }
463  
464 # ---------------------------------------------------------------------------
465  
466 sub _dapreq_stackTrace
467 {
468 my ($self, $workspace, $req) = @_ ;
469  
470 $self -> _check_not_running ($workspace) ;
471  
472 my $thread_ref = $self -> id2ref -> {$req -> params -> {threadId}} -> {ref} ;
473 my $frames = $self -> send_request ('stack',
474 {
475 thread_ref => $thread_ref,
476 levels => $req -> params -> {levels},
477 start => $req -> params -> {startFrame},
478 }) ;
479  
480 foreach (@{$frames -> {stackFrames}})
481 {
482 $_ -> {id} = $self -> getid ($req -> params -> {threadId}, $_ -> {frame_ref}, { thread_ref => $thread_ref, package => $_ -> {'package'} }) ;
483 $_ -> {line} += 0 ;
484 $_ -> {column} += 0 ;
485 $_ -> {source}{path} = $self -> debugger_process -> file_server2client ($workspace, $_ -> {source}{path}) ;
486 }
487  
488 return $frames ;
489 }
490  
491 # ---------------------------------------------------------------------------
492  
493 sub _dapreq_scopes
494 {
495 my ($self, $workspace, $req) = @_ ;
496  
497 $self -> _check_not_running ($workspace) ;
498  
499 my $ref = $self -> id2ref -> {$req -> params -> {frameId}} ;
500 my $frame_ref = $ref -> {ref} ;
501 my $thread_ref = $ref -> {thread_ref} ;
502 my $package = $ref -> {package} ;
503  
504 return
505 {
506 scopes =>
507 [
508 { name => 'Locals', presentationHint => 'locals', expensive => JSON::false (),
509 variablesReference => $self -> getid ($req -> params -> {frameId}, 'l', { frame_ref => $frame_ref, thread_ref => $thread_ref, package => $package }), },
510 { name => 'Globals', presentationHint => 'globals', expensive => JSON::true (),
511 variablesReference => $self -> getid ($req -> params -> {frameId}, 'g', { frame_ref => $frame_ref, thread_ref => $thread_ref, package => $package }), },
512 { name => 'Specials', presentationHint => 'specials', expensive => JSON::true (),
513 variablesReference => $self -> getid ($req -> params -> {frameId}, 's', { frame_ref => $frame_ref, thread_ref => $thread_ref, package => $package }), },
514 { name => 'Arguments', presentationHint => 'arguments', expensive => JSON::true (),
515 variablesReference => $self -> getid ($req -> params -> {frameId}, 'a', { frame_ref => $frame_ref, thread_ref => $thread_ref, package => $package }), },
516 ]
517 } ;
518 }
519  
520 # ---------------------------------------------------------------------------
521  
522 sub _dapreq_variables
523 {
524 my ($self, $workspace, $req) = @_ ;
525  
526 $self -> _check_not_running ($workspace) ;
527  
528 my $params = $req -> params ;
529 my $ref = $self -> id2ref -> {$params -> {variablesReference}} ;
530 my $frame_ref = $ref -> {frame_ref} ;
531 my $thread_ref = $ref -> {thread_ref} ;
532 my $package = $ref -> {package} ;
533 my $type = $ref -> {ref} ;
534 #use Data::Dump ;
535 #print STDERR Data::Dump::pp($self -> id2ref), "\n" ;
536 my $variables = $self -> send_request ('vars',
537 {
538 thread_ref => $thread_ref,
539 frame_ref => $frame_ref,
540 'package' => $package,
541 type => $type,
542 #var_ref => $ref,
543 count => $params -> {count},
544 start => $params -> {start},
545 filter => $params -> {filter},
546 }) ;
547  
548 foreach (@{$variables -> {variables}})
549 {
550 $_ -> {variablesReference} = $_ -> {var_ref}?$self -> getid ($req -> params -> {variablesReference},
551 $_ -> {var_ref},
552 {
553 frame_ref => $frame_ref,
554 thread_ref => $thread_ref,
555 'package' => $package,
556 type => $type}):
557 0 ;
558 $_ -> {name} .= '' ; # make sure name is a string, otherwise array indices fails on mac
559 }
560  
561 return $variables ;
562 }
563  
564 # ---------------------------------------------------------------------------
565  
566 sub _dapreq_setVariable
567 {
568 my ($self, $workspace, $req) = @_ ;
569  
570 $self -> _check_not_running ($workspace) ;
571  
572 my $params = $req -> params ;
573 my $ref = $self -> id2ref -> {$params -> {variablesReference}} ;
574 my $frame_ref = $ref -> {frame_ref} ;
575 my $thread_ref = $ref -> {thread_ref} ;
576 my $package = $ref -> {package} ;
577 my $type = $ref -> {ref} ;
578 my $expr = $params->{value} ;
579 my $setvar = $params->{name} ;
580  
581 my $result = $self -> send_request ('setvar',
582 {
583 thread_ref => $thread_ref,
584 frame_ref => $frame_ref,
585 'package' => $package,
586 expression => $expr,
587 type => $type,
588 setvar => $setvar,
589 }) ;
590  
591 $result -> {variablesReference} = $result -> {var_ref}?$self -> getid ($req -> params -> {variablesReference},
592 $result -> {var_ref},
593 {
594 frame_ref => $frame_ref,
595 thread_ref => $thread_ref,
596 'package' => $package,
597 }):
598 0 ;
599 return $result ;
600 }
601  
602  
603 # ---------------------------------------------------------------------------
604  
605 sub _dapreq_source
606 {
607 my ($self, $workspace, $req) = @_ ;
608
609 my $source = $req -> params -> {source} ;
610 $self -> logger ("req_source source =" . pp($source)) ;
611 my $ret = $self -> send_request ('source',
612 {
613 ($source?(filename => $self -> debugger_process -> file_client2server ($workspace, $source -> {path})):()),
614 }) ;
615 $self -> logger ("_dapreq_source ret = " . pp($ret)) ;
616 return $ret;
617 }
618  
619 # ---------------------------------------------------------------------------
620  
621 sub _dapreq_evaluate
622 {
623 my ($self, $workspace, $req) = @_ ;
624  
625 $self -> _check_not_running ($workspace) ;
626  
627 my $ref = $self -> id2ref -> {$req -> params -> {frameId}} ;
628 my $frame_ref = $ref -> {ref} ;
629 my $thread_ref = $ref -> {thread_ref} ;
630 my $package = $ref -> {package} ;
631  
632  
633 my $result = $self -> send_request ('evaluate',
634 {
635 thread_ref => $thread_ref,
636 frame_ref => $frame_ref,
637 'package' => $package,
638 expression => $req -> params -> {expression},
639 context => $req -> params -> {context},
640 }) ;
641  
642 $result -> {variablesReference} = $result -> {var_ref}?$self -> getid ($req -> params -> {variablesReference},
643 $result -> {var_ref},
644 {
645 frame_ref => $frame_ref,
646 thread_ref => $thread_ref,
647 'package' => $package,
648 }):
649 0 ;
650 $result -> {result} = delete $result -> {value} ;
651 return $result ;
652 }
653  
654 # ---------------------------------------------------------------------------
655  
656 sub _dapreq_pause
657 {
658 my ($self, $workspace, $req) = @_ ;
659  
660 $self -> logger ("send SIGINT for pause\n") ;
661 $self -> debugger_process -> signal ('INT') ;
662  
663 return {} ;
664 }
665  
666 # ---------------------------------------------------------------------------
667  
668 sub _dapreq_terminate
669 {
670 my ($self, $workspace, $req) = @_ ;
671  
672 $self -> debugger_process -> signal ('TERM') ;
673  
674 return {} ;
675 }
676  
677 # ---------------------------------------------------------------------------
678  
679 sub _dapreq_disconnect
680 {
681 my ($self, $workspace, $req) = @_ ;
682  
683 $self -> debugger_process -> signal ('KILL') ;
684  
685 return {} ;
686 }
687  
688 # ---------------------------------------------------------------------------
689  
690 sub _dapreq_continue
691 {
692 my ($self, $workspace, $req) = @_ ;
693  
694 $self -> _check_not_running ($workspace) ;
695  
696 $self -> running (1) ;
697 $self -> send_request ('continue', $req?{ thread_id => $req -> {threadId}}:undef) ;
698  
699 return {} ;
700 }
701  
702 # ---------------------------------------------------------------------------
703  
704 sub _dapreq_stepIn
705 {
706 my ($self, $workspace, $req) = @_ ;
707  
708 $self -> _check_not_running ($workspace) ;
709  
710 $self -> running (1) ;
711 $self -> send_request ('step_in', { thread_id => $req -> {threadId}}) ;
712  
713 return {} ;
714 }
715  
716 # ---------------------------------------------------------------------------
717  
718 sub _dapreq_stepOut
719 {
720 my ($self, $workspace, $req) = @_ ;
721  
722 $self -> _check_not_running ($workspace) ;
723  
724 $self -> running (1) ;
725 $self -> send_request ('step_out', { thread_id => $req -> {threadId}}) ;
726  
727 return {} ;
728 }
729  
730 # ---------------------------------------------------------------------------
731  
732 sub _dapreq_next
733 {
734 my ($self, $workspace, $req) = @_ ;
735  
736 $self -> _check_not_running ($workspace) ;
737  
738 $self -> running (1) ;
739 $self -> send_request ('next', { thread_id => $req -> {threadId}}) ;
740  
741 return {} ;
742 }
743  
744  
745 1 ;