blib/lib/Devel/Debug/Server/Agent.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« bin/debugServer.pl ↑ Summary lib/Devel/Debug/Server.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Devel::Debug::Server::Agent;
4  
5 0 ▶use Devel::Debug::Server;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use Devel::ebug;
7  
8 use Time::HiRes qw(usleep nanosleep);
9 use File::Spec;
10  
11 use Data::Dumper;
12  
13 my $ebug = undef;
14 my $programName = undef;
15 my $breakPointsVersion = -1; #the version of the breakpoints
16 my $lastEvalCommand = '';
17 my $lastEvalResult = '';
18 my %breakPointsToSet = ();
19  
20 sub absoluteName($){
21 my ($fileName)=@_;
22 if (! File::Spec->file_name_is_absolute( $fileName )){
23 $fileName = File::Spec->rel2abs( $fileName ) ;
24 }
25 return $fileName;
26 }
27  
28 #keep the breakpoint list up-to-date with the debug server
29 sub updateBreakPoints {
30 my ($breakPointsServerVersion,$breakPointsList) = @_;
31  
32 if ($breakPointsServerVersion == $breakPointsVersion){
33 return; #first check if there were no modification since last time
34 }
35  
36 $breakPointsVersion = $breakPointsServerVersion;
37 my @breakPoints = $ebug->all_break_points_with_condition();
38 foreach my $breakPoint (@breakPoints) {
39  
40 my $file = absoluteName($breakPoint->{filename});
41 my $line = $breakPoint->{line};
42 my $condition = $breakPoint->{condition};
43 #suppress all breakpoints already set but no more needed
44 if (!(exists $breakPointsList->{$file}
45 && exists $breakPointsList->{$file}{$line})){
46 $ebug->break_point_delete($file,$line);
47 }
48 }
49  
50 my $effectiveBreakpointList = [];
51  
52 my @loadedFilenames = ();
53 foreach my $loadedFile ($ebug->filenames()){
54 push @loadedFilenames, absoluteName($loadedFile);
55 }
56
57 #clean old list that will be rebuilt
58 %breakPointsToSet = () ;
59 #add all new breakpoints
60 foreach my $file (keys %$breakPointsList) {
61 if (grep {$_ eq $file} @loadedFilenames){
62 my $bkPointList = setBreakPointForFile($file,keys %{$breakPointsList->{$file}});
63 push @{$effectiveBreakpointList}, @{$bkPointList};
64 }else{
65 #the source file has not yet been loaded,
66 #store breakpoints and break on file loadind
67 $breakPointsToSet{$file} = $breakPointsList->{$file} ;
68 $ebug->break_on_load($file);
69 }
70 }
71 sendBreakPointsInfo($effectiveBreakpointList);
72 return;
73 }
74  
75 # set breakpoints on newly loaded file $file
76 # return 1 if breakpoints were effectly set
77 # return 0 otherwise
78 sub setDelayedBreakPoints {
79 my ($file) = @_;
80 if (!exists $breakPointsToSet{$file}){
81 return 0;
82 }
83 my $fileBreakPoints = $breakPointsToSet{$file};
84 my $bkPointList = setBreakPointForFile($file,keys %{$fileBreakPoints});
85 delete $breakPointsToSet{$file};
86 sendBreakPointsInfo($bkPointList);
87 return 1;
88 }
89  
90 sub setBreakPointForFile($$){
91 my ($file,@line)=@_;
92 my $effectiveBreakpointList = [];
93 foreach my $line (@line) {
94 my $effectiveLineNumber = $ebug->break_point($file,$line);
95 if (defined $effectiveLineNumber){
96 push (@{$effectiveBreakpointList} ,
97 { file => $file,
98 requestedLineNumber => $line,
99 effectiveLineNumber => $effectiveLineNumber});
100 }
101 }
102 return $effectiveBreakpointList;
103 }
104  
105 sub trace($){
106 my ($text)=@_;
107 open (my $fh,">>","./trace.log");
108 $text = "[$$]".$text."\n";
109 print $fh $text;
110 close $fh;
111 }
112  
113 sub init{
114 my($progName) = @_;
115 $ebug = Devel::ebug->new;
116 my $programName = $progName;
117 $ebug->program($programName);
118 $ebug->backend("ebug_backend_perl");
119 $ebug->load;
120 Devel::Debug::Server::initZeroMQ();
121 }
122  
123 sub run {
124 #no params
125 my $continueRunning = 1;
126 while($continueRunning){
127 $ebug->run;
128 $continueRunning = setDelayedBreakPoints(absoluteName($ebug->filename));
129 }
130  
131 }
132  
133 sub loop {
134 my($progName) = @_;
135
136 init($progName);
137
138 my $status = {
139 result => undef,
140 };
141
142 my $fileName = undef;
143 while (1){
144 my $fileContent = undef;
145 if (!defined $fileName || $fileName ne $ebug->filename()){
146 my @fileLines = $ebug->codelines();
147 $fileContent = \@fileLines;
148 $status->{fileContent} = $fileContent ;
149 }
150 my $message = Devel::Debug::Server::Agent::sendAgentInfos($status);
151
152 my $command = $message->{command};
153 my $result = undef ;
154  
155 $fileName = $message->{fileName};
156  
157 updateBreakPoints($message->{breakPointVersion}, $message->{breakPoints});
158  
159 if (defined $command){
160 my $commandName = $command->{command};
161  
162 my $arg1 = $command->{arg1};
163 my $arg2 = $command->{arg2};
164 my $arg3 = $command->{arg3};
165
166  
167 if ($commandName eq $Devel::Debug::Server::STEP_COMMAND) {
168 clearEvalResult();
169 $ebug->step;
170 } elsif ($commandName eq 'n') {
171 clearEvalResult();
172 $ebug->next;
173 } elsif ($commandName eq $Devel::Debug::Server::RUN_COMMAND) {
174 clearEvalResult();
175 run();
176 } elsif ($commandName eq 'restart') {
177 $ebug->load;
178 } elsif ($commandName eq $Devel::Debug::Server::RETURN_COMMAND) {
179 $ebug->return($arg1);
180 } elsif ($commandName eq 'f') {
181 $result = $ebug->filenames;
182 } elsif ($commandName eq 'b') {
183 $ebug->break_point($arg1, $arg2, $arg3);
184 } elsif ($commandName eq 'd') {
185 $ebug->break_point_delete($arg1, $arg2);
186 } elsif ($commandName eq 'w') {
187 $ebug->watch_point($arg1);
188 } elsif ($commandName eq 'q') {
189 exit;
190 } elsif ($commandName eq 'x') {
191 $lastEvalCommand = $arg1;
192 $lastEvalResult = $ebug->eval("use YAML; Dump($arg1)") || "";
193 } elsif ($commandName eq $Devel::Debug::Server::EVAL_COMMAND) {
194 $lastEvalCommand = $arg1;
195 $lastEvalResult = $ebug->eval($arg1) ;
196 }
197 }
198 $status->{result} = $result;
199 usleep(1000); #wait 1 ms
200 }
201 }
202  
203 sub clearEvalResult {
204 $lastEvalCommand = '';
205 $lastEvalResult = '';
206 }
207  
208 #we notify the server for each breakpoint effectly set, so the real line numbers are stored in the server
209 sub sendBreakPointsInfo {
210 my($effectiveBreakPoints) = @_;
211 if (scalar @{$effectiveBreakPoints} <= 0){
212 return; #nothing to do
213 }
214 my $breakpointsInfo = {
215 type => $Devel::Debug::Server::DEBUG_BREAKPOINT_TYPE,
216 effectiveBreakpoints => $effectiveBreakPoints
217 };
218 return Devel::Debug::Server::send($breakpointsInfo);
219 }
220  
221 sub sendAgentInfos {
222 my($status) = @_;
223 my @stackTrace = $ebug->stack_trace_human();
224 my $variables = $ebug->pad();
225 $variables = {} unless defined $variables;
226 my $programInfo = {
227 pid => $ebug->proc->pid ,
228 name => $programName ,
229 line => $ebug->line,
230 subroutine => $ebug->subroutine,
231 package => $ebug->package,
232 fileName => absoluteName($ebug->filename),
233 finished => $ebug->finished,
234 halted => 1, #program wait debugging commands
235 stackTrace => \@stackTrace,
236 variables => $variables ,
237 result => $status->{result},
238 fileContent => $status->{fileContent},
239 type => $Devel::Debug::Server::DEBUG_PROCESS_TYPE,
240 breakPointVersion => $breakPointsVersion,
241 lastEvalCommand => $lastEvalCommand,
242 lastEvalResult => $lastEvalResult,
243 lastUpdateTime => [Time::HiRes::gettimeofday()],
244 };
245 return Devel::Debug::Server::send($programInfo);
246 }
247  
248 1;
249  
250 __END__
« bin/debugServer.pl ↑ Summary lib/Devel/Debug/Server.pm »