LanguageServer/DebuggerProcess.pm untested

filter: Statementstmt 0.0% 0 / 144 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 226 CC 16 SCAR 33.7
CC
16
Cov
0%
launch
90.0
BUILDARGS
30.0
add_path_mapping
12.0
CRAP 28.9
1 package Perl::LanguageServer::DebuggerProcess ;
2  
3 use 5.006;
4 use strict;
5 use Moose ;
6  
7 use Encode::Locale;
8 use Encode;
9 use File::Basename ;
10 use Coro ;
11 use Coro::AIO ;
12 use Data::Dump qw{dump} ;
13  
14 with 'Perl::LanguageServer::IO' ;
15  
16 no warnings 'uninitialized' ;
17  
18 our $session_cnt = 1 ;
19  
20 # ---------------------------------------------------------------------------
21  
22 has 'program' =>
23 (
24 isa => 'Str',
25 is => 'ro'
26 ) ;
27  
28 has 'args' =>
29 (
30 isa => 'ArrayRef | Str',
31 is => 'ro',
32 default => sub { [] },
33 ) ;
34  
35 has 'env' =>
36 (
37 isa => 'HashRef',
38 is => 'ro',
39 default => sub { {} },
40 ) ;
41  
42 has 'cwd' =>
43 (
44 isa => 'Maybe[Str]',
45 is => 'ro',
46 ) ;
47  
48 has 'sudo_user' =>
49 (
50 isa => 'Maybe[Str]',
51 is => 'ro',
52 ) ;
53  
54 has 'use_taint_for_debug' =>
55 (
56 isa => 'Bool',
57 is => 'rw'
58 ) ;
59  
60 has 'path_map' =>
61 (
62 isa => 'Maybe[ArrayRef]',
63 is => 'rw'
64 ) ;
65  
66 has 'stop_on_entry' =>
67 (
68 isa => 'Bool',
69 is => 'ro'
70 ) ;
71  
72 has 'reload_modules' =>
73 (
74 isa => 'Bool',
75 is => 'ro'
76 ) ;
77  
78 has 'session_id' =>
79 (
80 isa => 'Str',
81 is => 'ro'
82 ) ;
83  
84 has 'type' =>
85 (
86 isa => 'Str',
87 is => 'ro'
88 ) ;
89  
90 has 'debug_adapter' =>
91 (
92 isa => 'Perl::LanguageServer',
93 is => 'rw',
94 weak_ref => 1,
95 ) ;
96  
97 has 'pid' =>
98 (
99 isa => 'Int',
100 is => 'rw'
101 ) ;
102  
103  
104 # ---------------------------------------------------------------------------
105  
106 sub BUILDARGS
107 {
108 my ($class, $args) = @_ ;
109  
110 $args -> {env} = { @{$args -> {env}} } if (exists $args -> {env} && ref ($args -> {env}) eq 'ARRAY') ;
111 $args -> {reload_modules} = delete $args -> {reloadModules}?1:0 ;
112 $args -> {stop_on_entry} = delete $args -> {stopOnEntry}?1:0 ;
113 $args -> {session_id} = delete $args -> {__sessionId} || $session_cnt ;
114 $args -> {sudo_user} = delete $args -> {sudoUser} ;
115 $args -> {use_taint_for_debug} = delete $args -> {useTaintForDebug} ;
116 my $map = delete $args -> {pathMap} ;
117 if ($map)
118 {
119 my $fn ;
120 foreach (@$map)
121 {
122 $fn = $_ -> [0] ;
123 $fn =~ s/^file:// ;
124 $fn =~ s/^\/\/\//\// ;
125 $_ -> [2] ||= $fn ;
126 $fn = $_ -> [1] ;
127 $fn =~ s/^file:// ;
128 $fn =~ s/^\/\/\//\// ;
129 $_ -> [3] ||= $fn ;
130 }
131 $args -> {path_map} = $map ;
132 }
133  
134 $session_cnt++ ;
135  
136 return $args ;
137 }
138  
139 # ---------------------------------------------------------------------------
140  
141 sub logger
142 {
143 my $self = shift ;
144  
145 $self -> debug_adapter -> logger (@_) ;
146 }
147  
148 # ---------------------------------------------------------------------------
149  
150 sub file_server2client
151 {
152 my ($self, $workspace, $fn) = @_ ;
153  
154 return $workspace -> file_server2client ($fn, $self -> path_map) ;
155 }
156  
157 # ---------------------------------------------------------------------------
158  
159 sub file_client2server
160 {
161 my ($self, $workspace, $fn) = @_ ;
162  
163 return $workspace -> file_client2server ($fn, $self -> path_map) ;
164 }
165  
166 # ---------------------------------------------------------------------------
167  
168 sub add_path_mapping
169 {
170 my ($self, $fn_server, $fn_client) = @_ ;
171 my $map = $self -> path_map ;
172 $map = $self -> path_map ([]) if (!$map) ;
173  
174  
175 foreach my $m (@$map)
176 {
177 #print STDERR "add file_server2client $m->[2] -> $m->[3]\n" ;
178 return if ($fn_server eq $m->[2]) ;
179 }
180  
181 unshift @$map, ['file://' . $fn_server, 'file://' . $fn_client, $fn_server, $fn_client] ;
182 return ;
183 }
184  
185 # ---------------------------------------------------------------------------
186  
187 sub send_event
188 {
189 my ($self, $event, $body) = @_ ;
190  
191 $self -> debug_adapter -> send_event ($event, $body) ;
192 }
193  
194 # ---------------------------------------------------------------------------
195  
196 sub launch
197 {
198 my ($self, $workspace, $cmd) = @_ ;
199  
200 my $fn = $self -> file_client2server ($workspace, $self -> program) ;
201 my $pid ;
202 {
203 local %ENV = %ENV ;
204 my @sudoargs ;
205 if ($self->sudo_user)
206 {
207 push @sudoargs, "sudo", "-u", $self->sudo_user ;
208 }
209 foreach (keys %{$self -> env})
210 {
211 $ENV{$_} = $self -> env -> {$_} ;
212 push @sudoargs, "$_=" . $self -> env -> {$_} if $self->sudo_user;
213 }
214  
215 my $cwd ;
216 if ($self -> cwd)
217 {
218 my $dir = $self -> cwd ;
219 $dir =~ s/'//g ;
220 $cwd = " chdir '$dir'; " ;
221 }
222  
223 my $inc = $workspace -> perlinc ;
224 my @inc ;
225 @inc = map { ('-I', $_)} @$inc if ($inc) ;
226  
227 $ENV{PLSDI_REMOTE} = '127.0.0.1:' . $self -> debug_adapter -> listen_port ;
228 $ENV{PLSDI_OPTIONS} = $self -> reload_modules?'reload_modules':'' ;
229 $ENV{PERL5DB} = 'BEGIN { $| = 1 ; ' . $cwd . 'require Perl::LanguageServer::DebuggerInterface; DB::DB(); }' ;
230 $ENV{PLSDI_SESSION}= $self -> session_id ;
231 if ($self->sudo_user)
232 {
233 push @sudoargs, "PLSDI_REMOTE=$ENV{PLSDI_REMOTE}" ;
234 push @sudoargs, "PLSDI_OPTIONS=$ENV{PLSDI_OPTIONS}" ;
235 push @sudoargs, "PERL5DB=$ENV{PERL5DB}" ;
236 push @sudoargs, "PLSDI_SESSION=$ENV{PLSDI_SESSION}" ;
237 }
238 if ($self->use_taint_for_debug)
239 {
240 push @inc, "-T" ;
241 }
242  
243 if (ref $self -> args) # ref is array
244 {
245 $pid = $self -> run_async ([@sudoargs, $cmd, @inc, '-d', $fn, @{$self -> args}]) ;
246 }
247 else # no ref is string
248 {
249 $pid = $self -> run_async (join (' ', @sudoargs, $cmd, @inc, '-d', $fn, $self -> args)) ;
250 }
251 }
252  
253 $self -> pid ($pid) ;
254 $self -> send_event ('process',
255 {
256 name => $self -> program,
257 systemProcessId => $pid,
258 isLocalProcess => JSON::true(),
259 startMethod => 'launch',
260 }) ;
261  
262 return ;
263 }
264  
265 # ---------------------------------------------------------------------------
266  
267 sub signal
268 {
269 my ($self, $signal) = @_ ;
270  
271 return if (!$self -> pid) ;
272  
273 $self -> logger ("Send signal $signal to debuggee\n") ;
274  
275 kill $signal, $self -> pid ;
276 }
277  
278 # ---------------------------------------------------------------------------
279  
280 sub on_stdout
281 {
282 my ($self, $data) = @_ ;
283  
284 foreach my $line (split /\r?\n/, $data)
285 {
286 $line = decode(locale => $line);
287 $self -> send_event ('output', { category => 'stdout', output => $line . "\r\n" }) ;
288 }
289 }
290  
291 # ---------------------------------------------------------------------------
292  
293 sub on_stderr
294 {
295 my ($self, $data) = @_ ;
296  
297 foreach my $line (split /\r?\n/, $data)
298 {
299 $line = decode(locale => $line);
300 $self -> send_event ('output', { category => 'stderr', output => $line . "\r\n" }) ;
301 }
302 }
303  
304 # ---------------------------------------------------------------------------
305  
306 sub on_exit
307 {
308 my ($self, $data) = @_ ;
309  
310 $self -> send_event ('terminated') ;
311 $self -> send_event ('exited', { exitCode => ($data>>8)&0xff }) ;
312 }
313  
314 # ---------------------------------------------------------------------------
315  
316 1 ;
317