| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use ZeroMQ qw/:all/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Time::HiRes qw(usleep nanosleep); | ||
| 7 | use Storable; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | use Storable; | ||
| 11 | use Devel::Debug::Server; | ||
| 12 | use JSON; | ||
| 13 | use File::Spec; | ||
| 14 | |||
| 15 | # PODNAME: debugServer.pl | ||
| 16 | |||
| 17 | # ABSTRACT: The server to centralize debugging informations | ||
| 18 | |||
| 19 | |||
| 20 | my $cxt = ZeroMQ::Context->new; | ||
| 21 | my $responder = $cxt->socket(ZeroMQ::Constants::ZMQ_REP); | ||
| 22 | $responder->bind("tcp://127.0.0.1:5000"); | ||
| 23 | |||
| 24 | my %processesInfos = (); | ||
| 25 | |||
| 26 | #commandes to send to process to debug (undef = nothing to do) | ||
| 27 | #each command is as below | ||
| 28 | #{command => 'COMMAND_CODE', | ||
| 29 | # arg1 => 'first argument if needed', | ||
| 30 | # arg2 => 'second argument if needed', | ||
| 31 | # arg3 => 'third argument if needed' | ||
| 32 | # } | ||
| 33 | my %commands = (); | ||
| 34 | |||
| 35 | #a hash containing source files | ||
| 36 | my %files = (); | ||
| 37 | |||
| 38 | my $breakPointVersion = 0; | ||
| 39 | my $breakPoints = {}; #all the requested breakpoints | ||
| 40 | my $effectiveBreakpoints = {}; #all the breakpoints effectively set, with their real line number | ||
| 41 | my $lastBreakPointsUpdate = 0; #the last breakpoint list version that was propagate | ||
| 42 | |||
| 43 | #=comment updateProcessInfo | ||
| 44 | # | ||
| 45 | # Update informations of the process into the process table | ||
| 46 | # | ||
| 47 | # my $programInfo = { | ||
| 48 | # pid | ||
| 49 | # name | ||
| 50 | # line | ||
| 51 | # subroutine | ||
| 52 | # package | ||
| 53 | # filename | ||
| 54 | # finished | ||
| 55 | # stackTrace | ||
| 56 | # variables | ||
| 57 | # result | ||
| 58 | # | ||
| 59 | # }; | ||
| 60 | #=cut | ||
| 61 | sub updateProcessInfo { | ||
| 62 | my ($infos) = @_; | ||
| 63 | |||
| 64 | my $pid = $infos->{pid}; | ||
| 65 | $processesInfos{$pid} = $infos; | ||
| 66 | |||
| 67 | #initialize other hashes if necessary | ||
| 68 | if (!exists $commands{$pid}){ | ||
| 69 | $commands{$pid} = undef; | ||
| 70 | } | ||
| 71 | if (!exists $files{$pid}){ | ||
| 72 | $files{$pid} = {fileName => undef, | ||
| 73 | content => '' | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | my $file = $files{$pid}; | ||
| 77 | if (!defined $file->{fileName} || $file->{fileName} ne $infos->{fileName}){ | ||
| 78 | $file->{content} = $infos->{fileContent}; | ||
| 79 | $file->{fileName} = $infos->{fileName}; | ||
| 80 | } | ||
| 81 | return $pid; | ||
| 82 | } | ||
| 83 | |||
| 84 | #=method setRunningProcessInfo | ||
| 85 | # | ||
| 86 | #C<setRunningProcessInfo($pid);> | ||
| 87 | #update the process info when we send the 'continue' command because the process won't update its status until it id finished or it reached a breakpoint | ||
| 88 | # | ||
| 89 | #=cut | ||
| 90 | |||
| 91 | sub setRunningProcessInfo { | ||
| 92 | my ($pid) = @_; | ||
| 93 | my $processInfo = $processesInfos{$pid}; | ||
| 94 | |||
| 95 | my $programInfo = { | ||
| 96 | pid => $processInfo->{pid} , | ||
| 97 | name => $processInfo->{name} , | ||
| 98 | line => '??', | ||
| 99 | subroutine => '??', | ||
| 100 | package => '??', | ||
| 101 | fileName => '??', | ||
| 102 | finished => $processInfo->{finished}, | ||
| 103 | halted => 0, | ||
| 104 | stackTrace => [], | ||
| 105 | variables => {}, | ||
| 106 | result => '', | ||
| 107 | fileContent => $processInfo->{fileContent} , | ||
| 108 | breakPointVersion => $processInfo->{breakPointVersion}, | ||
| 109 | lastEvalCommand => '', | ||
| 110 | lastEvalResult => '', | ||
| 111 | lastUpdateTime => [Time::HiRes::gettimeofday()], | ||
| 112 | }; | ||
| 113 | $processesInfos{$pid} = $programInfo; | ||
| 114 | } | ||
| 115 | |||
| 116 | #=method getDebuggingInfos | ||
| 117 | # | ||
| 118 | #return a hash containg all debugging info + details for $pid | ||
| 119 | # | ||
| 120 | #=cut | ||
| 121 | sub getDebuggingInfos { | ||
| 122 | my ($pid) = @_; | ||
| 123 | |||
| 124 | my $returnedData = {sourceFileName => undef, | ||
| 125 | sourceFileContent => undef}; | ||
| 126 | |||
| 127 | $returnedData->{processesInfo} = \%processesInfos; | ||
| 128 | |||
| 129 | $returnedData->{requestedBreakpoints} = $breakPoints ; | ||
| 130 | $returnedData->{effectiveBreakpoints} = $effectiveBreakpoints; | ||
| 131 | |||
| 132 | |||
| 133 | if (defined $pid && exists $files{$pid}){ | ||
| 134 | my $file = $files{$pid}; | ||
| 135 | |||
| 136 | $returnedData->{sourceFileName } = $file->{fileName}; | ||
| 137 | $returnedData->{sourceFileContent} = $file->{fileContent}; | ||
| 138 | } | ||
| 139 | |||
| 140 | return $returnedData; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub setBreakPoint{ | ||
| 144 | my ($command)=@_; | ||
| 145 | my $file = $command->{arg1}; | ||
| 146 | my $lineNumber = $command->{arg2}; | ||
| 147 | if (! File::Spec->file_name_is_absolute( $file )){ | ||
| 148 | $file = File::Spec->rel2abs( $file ) ; | ||
| 149 | } | ||
| 150 | |||
| 151 | $breakPointVersion ++; | ||
| 152 | $breakPoints->{$file}{$lineNumber} = 1;#condition always true for now | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | #suspend process identified with $pid | ||
| 157 | sub suspend{ | ||
| 158 | my ($pid)=@_; | ||
| 159 | |||
| 160 | if ($processesInfos{$pid}{halted} == 0){ | ||
| 161 | my $processSignaled = kill ( 2 => $pid); #send SIGINT to force process to halt | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | sub removeBreakPoint{ | ||
| 166 | my ($command)=@_; | ||
| 167 | my $file = $command->{arg1}; | ||
| 168 | my $lineNumber = $command->{arg2}; | ||
| 169 | |||
| 170 | $breakPointVersion ++; | ||
| 171 | if (exists $breakPoints->{$file} && exists $breakPoints->{$file}{$lineNumber}){ | ||
| 172 | delete $breakPoints->{$file}{$lineNumber}; | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | sub trace($){ | ||
| 177 | my ($text)=@_; | ||
| 178 | open (my $fh,">>","./trace.log"); | ||
| 179 | $text = "[$$]".$text."\n"; | ||
| 180 | print $fh $text; | ||
| 181 | close $fh; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub updateEffectiveBreakpoints{ | ||
| 185 | my ($effectiveBreakpointsList) = @_; | ||
| 186 | |||
| 187 | for my $breakpoint (@{$effectiveBreakpointsList}){ | ||
| 188 | my $file= $breakpoint->{file}; | ||
| 189 | my $requestedLineNumber = $breakpoint->{requestedLineNumber}; | ||
| 190 | my $effectiveLineNumber= $breakpoint->{effectiveLineNumber}; | ||
| 191 | $effectiveBreakpoints->{$file}->{$requestedLineNumber} = $effectiveLineNumber ; | ||
| 192 | if ($effectiveLineNumber != $requestedLineNumber){ | ||
| 193 | #we are in the case where where the requested line number wasn't on a breakable line, we correct the breakpoints info | ||
| 194 | #only %effectiveBreakpoints keep informations about invalid breakpoints | ||
| 195 | $effectiveBreakpoints->{$file}->{$effectiveLineNumber} = $effectiveLineNumber; | ||
| 196 | delete $breakPoints->{$file}{$requestedLineNumber}; | ||
| 197 | $breakPoints->{$file}{$effectiveLineNumber} = 1;#condition always true for now | ||
| 198 | } | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | my $lastProcessCheck = [Time::HiRes::gettimeofday()]; | ||
| 203 | sub checkProcessAlive(){ | ||
| 204 | if (Time::HiRes::tv_interval ( $lastProcessCheck)< 1){ | ||
| 205 | return; #nothing to do for now | ||
| 206 | } | ||
| 207 | |||
| 208 | foreach my $pid (keys %processesInfos){ | ||
| 209 | if (Time::HiRes::tv_interval ($processesInfos{$pid}{lastUpdateTime}) > 1.5 ){ | ||
| 210 | my $processSignaled = kill ( 0 => $pid); #send no signal to check process alive | ||
| 211 | if ($processSignaled){ | ||
| 212 | $processesInfos{$pid}{lastUpdateTime} = [Time::HiRes::gettimeofday()]; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | $lastProcessCheck = [Time::HiRes::gettimeofday()]; | ||
| 217 | } | ||
| 218 | |||
| 219 | #=method propagateBreakPoints | ||
| 220 | # | ||
| 221 | #propagate new breakpoints to all processes; running processes are interrupted so they update their breakpoints. | ||
| 222 | # | ||
| 223 | #=cut | ||
| 224 | sub propagateBreakPoints { | ||
| 225 | if ($lastBreakPointsUpdate == $breakPointVersion){ | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | foreach my $pid (keys %processesInfos){ | ||
| 229 | if ($processesInfos{$pid}{breakPointVersion} != $lastBreakPointsUpdate | ||
| 230 | && $processesInfos{$pid}{halted} == 0){ | ||
| 231 | $commands{$pid} = { command => $Devel::Debug::Server::RUN_COMMAND }; | ||
| 232 | my $processSignaled = kill ( 2 => $pid); #send SIGINT to force breakpoints refresh | ||
| 233 | if ($processSignaled){ | ||
| 234 | $processesInfos{$pid}{lastUpdateTime} = [Time::HiRes::gettimeofday()]; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | $lastBreakPointsUpdate = $breakPointVersion; | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | #The main loop | ||
| 243 | print "server is started...\n"; | ||
| 244 | |||
| 245 | while (1) { | ||
| 246 | # Wait for the next request from client | ||
| 247 | my $message = $responder->recv(); | ||
| 248 | |||
| 249 | if (defined $message){ | ||
| 250 | my $requestStr = $message->data(); | ||
| 251 | my $request = Storable::thaw($requestStr); | ||
| 252 | my $messageToSend = undef; | ||
| 253 | |||
| 254 | if ($request->{type} eq $Devel::Debug::Server::DEBUG_PROCESS_TYPE){ #message from a debugged process | ||
| 255 | my $pid = updateProcessInfo($request); | ||
| 256 | |||
| 257 | my $commandInfos= $commands{$pid}; | ||
| 258 | $messageToSend = {command => $commandInfos, | ||
| 259 | fileName => $files{$pid}->{fileName}, | ||
| 260 | breakPoints => $breakPoints, | ||
| 261 | breakPointVersion => $breakPointVersion, | ||
| 262 | }; | ||
| 263 | $commands{$pid} = undef; #don't send the same command twice | ||
| 264 | if (defined $commandInfos && defined $commandInfos->{command} | ||
| 265 | && $commandInfos->{command} eq $Devel::Debug::Server::RUN_COMMAND){ | ||
| 266 | setRunningProcessInfo($pid); | ||
| 267 | } | ||
| 268 | } elsif ($request->{type} eq $Devel::Debug::Server::DEBUG_GUI_TYPE){ #message from the GUI | ||
| 269 | my $command = $request->{command}; | ||
| 270 | my $pid = $request->{pid}; | ||
| 271 | if (defined $command){ | ||
| 272 | if ($command->{command} | ||
| 273 | eq $Devel::Debug::Server::SET_BREAKPOINT_COMMAND){ | ||
| 274 | setBreakPoint($command); | ||
| 275 | }elsif ($command->{command} | ||
| 276 | eq $Devel::Debug::Server::REMOVE_BREAKPOINT_COMMAND){ | ||
| 277 | removeBreakPoint($command); | ||
| 278 | }elsif ($command->{command} | ||
| 279 | eq $Devel::Debug::Server::SUSPEND_COMMAND){ | ||
| 280 | suspend($pid); | ||
| 281 | |||
| 282 | }elsif(!defined $commands{$pid}){ | ||
| 283 | $commands{$pid} = $command; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | $messageToSend = getDebuggingInfos($pid); | ||
| 288 | } elsif ($request->{type} eq $Devel::Debug::Server::DEBUG_BREAKPOINT_TYPE){ #breakpoint has been set in debugged process | ||
| 289 | updateEffectiveBreakpoints($request->{effectiveBreakpoints}); | ||
| 290 | $messageToSend = {message =>"NOTHING TO SAY"}; | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | # Send reply back to client | ||
| 296 | $responder->send(Storable::freeze($messageToSend)); | ||
| 297 | |||
| 298 | propagateBreakPoints(); | ||
| 299 | checkProcessAlive(); | ||
| 300 | }else{ | ||
| 301 | usleep(500); | ||
| 302 | } | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | 1; | ||
| 307 | |||
| 308 | __END__ | ||