blib/lib/Devel/DebugHooks/Commands.pm untested

filter: Statementstmt 0.0% 0 / 528 Branchbran 0.0% 0 / 274 Conditioncond 0.0% 0 / 110 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 20 totaltotal 0.0% 0 / 959 CC 61 SCAR 44.5
CC
61
Cov
0%
_list
210.0
list
210.0
interact
72.0
CRAP 85.5
1 package Devel::DebugHooks::Commands;
2  
3 our $lines_before = 15;
4 our $lines_after = 15;
5  
6 # FIX: segmentation fault when:
7 # use strict;
8 # use warnings;
9  
10 # BEGIN {
11 # if( $DB::options{ w } ) { require 'warnings.pm'; 'warnings'->import(); }
12 # if( $DB::options{ s } ) { require 'strict.pm'; 'strict'->import(); }
13 # if( $options{ d } ) { require 'Data/Dump.pm'; 'Data::Dump'->import( 'pp'); }
14 # }
15  
16 # TODO: implement 'hard_go' to go over all traps to the 'line' or end
17 # TODO: implement do not stop in current sub
18 # TODO: black box command 'T': 50..70 Black box
19 # TODO: enable/disable group of traps
20 # TODO: implement option to show full/short sub options
21 # TODO: split all code by two: user code and core
22 # TODO: implement distance 'T XXX'; Automatically sets XXX to the current line
23 # TODO: implement 'b &' to set breakpoint to current sub
24 # TODO: implement 'd code' command to debug given subroutine
25 # TODO: implement 'l ~TEXT' to list specific lines
26 # TODO: implement 'ge -2' to edit
27 # TODO: implement 's 7', 'n 7', 'go +5' commands to skip N expressions
28 # FIX: when returning from sub by 'r' and upper sub has no any stop point
29 # we out many frames out
30 # TODO: implement tracing subroutine which user run manually 't $subname'
31 # NOTICE: when return we can go into destroy
32 # TODO: returning from try/catch block
33 # Show code flow in NON stop mode
34 # TODO: do not exit on blocks. This is usefull when we want to see result of statement
35 # if() { statement; }
36 # TODO: implement command to list all available packages
37 # TODO: implement command to list all available subs in package ( DB::subs )
38 # TODO: IT: closed variables are created at compile time. We do not see them when just eval at sub
39  
40 my $file_line = qr/(?:(.+):)?(\d+|\.)/;
41  
42  
43 sub file {
44 my( $file, $do_not_set ) = @_;
45  
46 return DB::state( 'list.file' ) // DB::state( 'file' )
47 unless defined $file;
48  
49 my $files = DB::state( 'cmd.f' );
50 $file = $files->[ $file ]
51 if $file =~ m/^(\d+)$/ && exists $files->[ $file ];
52  
53 DB::state( 'list.file', $file ) unless $do_not_set;
54  
55 return $file;
56 }
57  
58  
59  
60 my %cmd_T = (
61 G => '&',
62 C => '=',
63 D => '-',
64 L => '\\',
65 );
66  
67  
68  
69 sub _cursor_position {
70 my( $frames, $file ) = @_;
71  
72 my $run_level = 0;
73 my $lines;
74 for( @$frames ) {
75 # Frames are counted from the end
76 $lines->{ $_->{ line } } = $#$frames -$run_level
77 if $_->{ file } eq $file;
78 $run_level++;
79 }
80  
81 return $lines;
82 }
83  
84  
85  
86 # TODO: implement trim for wide lines to fit text into window size
87 sub _list {
88 my( $file, $from, $to ) = @_;
89  
90 # Fix window boundaries
91 my $source = DB::source( $file );
92 $from = 0 if $from < 0; # TODO: testcase; 0 exists if -d
93 $to = $#$source if $to > $#$source; # TODO: testcase
94  
95 # The place where to display *run marker*: '>>'
96 my $cursor_at = _cursor_position( DB::state( 'stack' ) ,$file );
97  
98  
99 print $DB::OUT "$file\n";
100 my $traps = DB::traps( $file );
101 for my $line ( $from..$to ) {
102 next unless exists $source->[ $line ];
103  
104 # Print flags
105 if( exists $traps->{ $line } ) {
106 print $DB::OUT exists $traps->{ $line }{ action } ? 'a' : ' ';
107 print $DB::OUT exists $traps->{ $line }{ onetime } ? '!'
108 : exists $traps->{ $line }{ breakpoint }{ disabled }? '-'
109 : exists $traps->{ $line }{ breakpoint }{ condition }? 'b' : ' ';
110 }
111 else {
112 print $DB::OUT ' ';
113 }
114  
115  
116 # Print *breakable* and *cursor* marks
117 if( defined( my $level = $cursor_at->{ $line } ) ) {
118 if( $level ) {
119 if( $level < 10 ) {
120 printf $DB::OUT '%d>', $level;
121 }
122 else {
123 printf $DB::OUT '*>';
124 }
125 }
126 else {
127 printf $DB::OUT '>>';
128 }
129 }
130 else {
131 printf $DB::OUT DB::can_break( $file, $line ) ? ' x' : ' ';
132 }
133  
134  
135 # Print source line number
136 print $DB::OUT "$line:";
137  
138  
139 # Print source line
140 (my $sl = $source->[ $line ]) =~ s/\t/ /g; #/
141 $sl = " $sl"; # Space after line number
142 $sl .= "\n" unless $sl =~ m/\n$/s; # Last line maybe without EL
143 $sl =~ s/\s+\n$/\n/s; # Remove whitespaces at EOL
144 print $DB::OUT $sl;
145 }
146 }
147  
148  
149  
150 # TODO: tests 'l;l', 'l 0', 'f;l 19 3', 'l .'
151 sub list {
152 my( $args ) = @_;
153  
154  
155 # Just list source at current position
156 if( $args eq '' ) {
157 my $file = DB::state( 'list.file' );
158 my $line = DB::state( 'list.line' );
159 $line += $lines_after +1;
160 _list( $file, $line, $line +$lines_before +$lines_after );
161  
162 # Move cursor to the next window.
163 # Window is: lines before, current line and lines after
164 DB::state( 'list.line', $line +$lines_before );
165  
166 return 1;
167 }
168  
169  
170 if( my( $stack, $file, $line, $to ) = $args =~ m/^(-)?${file_line}(?:-(\d+))?$/ ) {
171 my $from;
172 if( $stack && !$file ) {
173 # Here $line is stack frame number from the last frame
174 # Frames are counted from the end. -1 subscript is for current frame
175 my $frames = DB::state( 'stack' );
176 return -2 if $line +1 > @$frames;
177 DB::state( 'list.level', $line );
178 ( $file, $line ) = @{ $frames->[ -$line -1 ] }{ qw/ file line / };
179 }
180 elsif( $line eq '.' ) {
181 DB::state( 'list.level', 0 );
182 # TODO: 'current' means file and line! FIX this in other places too
183 $file = DB::state( 'file' );
184 $line = DB::state( 'line' );
185 }
186 else {
187 $file = file( $file );
188 }
189  
190  
191 if( $to ) {
192 $from = $line;
193 $line = $to -$lines_after;
194 }
195 else {
196 $from = $line -$lines_before;
197 $to = $line +$lines_after;
198 }
199  
200 _list( $file, $from, $to );
201  
202  
203 # Move cursor to the next window.
204 # Window is: lines before, current line and lines after
205 DB::state( 'list.file', $file );
206 DB::state( 'list.line', $line );
207 }
208 elsif( my( $ref, $subname ) = $args =~ m/^(\$?)(&\d*|.+)?$/ ) {
209 # 1.Deparse the current sub or n frames before
210 # TODO: Check is it possible to spy subs from goto_frames?
211 # If yes think about interface to access to them ( DB::frames??? )
212 if( $subname =~ /^&(\d*)$/ ) {
213 my $level = $1 // 0;
214 # FIX: 'eval' does not update @DB::stack.
215 # Eval exists at real stack but does not at our
216 my $frames = DB::state( 'stack' );
217 return -2 if $level +1 > @$frames;
218 my $coderef = $frames->[ -$level -1 ]{ sub };
219 return -4 if $coderef eq ''; # The main:: namespace
220 print $DB::OUT "sub $coderef ";
221 $coderef = \&$coderef unless ref $coderef;
222 return deparse( $coderef );
223 }
224  
225 # 2. List sub by code ref in the variable
226 # TODO: findout sub name from the reference
227 # TODO: locate this sub at '_<$file' hash and do usual _list
228 # to show breakpoints, lines etc
229 $ref && return [()
230 ,sub{ deparse( @{ $_[0] } ); return { code => \&interact } }
231 ,"\$$subname"
232 ];
233  
234  
235 # 3. List sub from source
236 $subname = DB::state( 'package' ) ."::${ subname }"
237 if $subname !~ m/::/;
238  
239 # The location format is 'file:from-to'
240 my $location = DB::location( $subname );
241 if( defined $location && $location =~ m/^(.*):(\d+)-(\d+)$/ ) {
242 _list( $1, $2, $3 );
243 }
244  
245 return 1;
246 }
247 else {
248 print $DB::OUT "Can not list: $args\n";
249  
250 return -1;
251 }
252  
253  
254 return 1;
255 }
256  
257  
258  
259 sub deparse {
260 require B::Deparse;
261 my( $coderef ) = @_;
262 return -3 unless ref $coderef eq 'CODE';
263  
264 print $DB::OUT B::Deparse->new("-p", "-sC")
265 ->coderef2text( $coderef )
266 ,"\n"
267 ;
268  
269 return 1;
270 }
271  
272  
273  
274 sub interact {
275 my $handler = shift;
276 my $dbg = $handler->{ context }; # This is better
277 my $str = $dbg->get_command();
278 return unless defined $str;
279 print $DB::OUT +(" "x60 ."*"x20 ."\n")x10 if DB::state( 'ddd' );
280  
281 my $result = process( undef, $str );
282 return unless defined $result;
283  
284 if( $result == 0 ) {
285 # No command found
286 # eval $str; print eval results; goto interact again
287 return[ sub{
288 # We got ARRAYREF if EXPR was evalutated
289 DB::state( 'db.last_eval', $str );
290  
291 if( ref $_[0] eq 'DB::Error' ) {
292 print $DB::OUT "ERROR: $_[0]";
293 }
294 else {
295 print $DB::OUT "\nEvaluation result:\n" if DB::state( 'ddd' );
296 my @res = map{ $_ // $DB::options{ undef } } @{ $_[0] };
297 local $" = $DB::options{ '"' } // $";
298 print $DB::OUT "@res\n";
299 }
300  
301 return $handler;
302 }
303 # EXPRessions to evaluate in user context
304 ,$str
305 ];
306 }
307  
308  
309 #FIX: Implement sub to return interaction command
310 return $handler unless ref $result;
311 return $result;
312 }
313  
314  
315  
316 sub dd {
317 require Data::Dump;
318 Data::Dump::pp( @_ );
319 }
320  
321  
322  
323 sub get_expr {
324 my( $data ) = @_;
325 my @expr = keys %{ $data->{ eval } };
326 # This sub is called with expressions evaluation result
327 # Additionally we pass and source data structure and corresponding keys
328 # Old values we get by those keys
329 return [ sub{ watch_checker( $data, \@expr, \@_ ) }, @expr ];
330 }
331  
332  
333  
334 sub watch_checker {
335 my( $data, $keys, $nv ) = @_;
336  
337 my $stop = 0;
338 for my $i ( 0 .. $#$keys ) { #TODO: IT: for commit:035e182e4f case
339 my $ov = \$data->{ eval }{ $keys->[ $i ] };
340 # dd( $$ov, $nv->[ $i ], $#$$ov );
341 next if defined $$ov && @{ $nv->[ $i ] } == grep {
342 defined $$ov->[$_] && defined $nv->[$i][$_]
343 && $$ov->[$_] eq $nv->[$i][$_]
344 || !defined $$ov->[$_] && !defined $nv->[$i][$_]
345 } 0..$#$$ov;
346  
347 # Do not stop for first time
348 if( defined $$ov ) {
349 $stop ||= 1;
350 local $" = ',';
351 print $DB::OUT $keys->[ $i ] .": @$$ov -> @{ $nv->[ $i ] }\n";
352 }
353  
354 $$ov = $nv->[ $i ];
355 }
356  
357 $stop;
358 }
359  
360  
361  
362 #TODO: I: global watch when we do not rely on $file:$line and just watching
363 # this expression at any place it is mentioned
364 sub watch {
365 my( $file, $line, $expr ) = shift =~ m/^${file_line}(?:\s+(.+))?$/;
366  
367 $line = DB::state( 'line' ) if $line eq '.';
368 $file = file( $file );
369  
370 my $traps = DB::traps( $file );
371  
372  
373 unless( $expr ) {
374 for( defined $line ? ( $line ) : sort{ $a <=> $b } keys %$traps ) {
375 next unless exists $traps->{ $_ }{ watch };
376  
377 print $DB::OUT "line $_:\n";
378 print $DB::OUT " " .dd( $_ ) ."\n"
379 for @{ $traps->{ $_ }{ watch } };
380 }
381  
382 return 1;
383 }
384  
385  
386 unless( DB::can_break( $file, $line ) ) {
387 print $DB::OUT file(). "This line is not breakable. Can not watch at this point\n";
388 }
389  
390  
391 my $data = DB::reg( 'trap', 'watch', $file, $line );
392 $$data->{ code } = \&get_expr;
393 # We do not know $expr result until eval it
394 $$data->{ eval }{ $expr } = undef;
395  
396 1;
397 }
398  
399  
400  
401 sub load {
402 my $self = shift;
403 my( $file ) = @_;
404 $file ||= $ENV{ HOME } .'/.dbgini';
405  
406  
407 my( $stops, $traps ) = do $file;
408  
409 for( keys %$traps ) {
410 # TODO? do we need to can_break( $file, $line )?
411 %{ DB::traps( $_ ) || {} } = %{ $traps->{ $_ } };
412 }
413  
414 #IT: check we are stopped in right place after loading
415 DB::state( 'on_call', $stops );
416  
417  
418 return 1;
419 }
420  
421  
422  
423 # TODO: implement AutoSave option
424 sub save {
425 my $self = shift;
426 my( $file ) = @_;
427 $file ||= $ENV{ HOME } .'/.dbgini';
428  
429 my $traps;
430 for my $source ( keys %$DB::_tfiles ) {
431 $traps->{ $source } = DB::traps( $source );
432 delete $traps->{ $source } # Do not save empty hashes
433 unless keys %{ $traps->{ $source } };
434 }
435  
436 open my $fh, '>', $file or die $!;
437 print $fh dd( DB::state( 'on_call' ), $traps );
438  
439 return 1;
440 }
441  
442  
443  
444 sub trace_variable {
445 my( $var ) = shift;
446  
447 require Devel::DebugHooks::TraceAccess;
448  
449 return [()
450 ,sub { return { code => \&interact } }
451 ,"tie $var, 'Devel::DebugHooks::TraceAccess', \\$var, desc => '$var'"
452 ]
453 }
454  
455  
456  
457 sub get_expr_a {
458 my( $data ) = @_;
459  
460 my @expr = @{ $data->{ eval } };
461 for my $expr_or_cmd ( @expr ) {
462 my $result = process( undef, $expr_or_cmd );
463 $expr_or_cmd = $result if ref $result;
464 }
465  
466 return [ sub{ 0 }, @expr ];
467 }
468  
469  
470 sub action {
471 my( $file, $line, $expr ) = shift =~ m/^${file_line}(?:\s+(.+))$/;
472  
473 $line = DB::state( 'line' ) if $line eq '.';
474 $file = file( $file );
475  
476 my $traps = DB::traps( $file );
477  
478  
479 unless( $expr ) {
480 for( defined $line ? ( $line ) : sort{ $a <=> $b } keys %$traps ) {
481 next unless exists $traps->{ $_ }{ action };
482  
483 print $DB::OUT "line $_:\n";
484 print $DB::OUT " " .dd( $_ ) ."\n"
485 for @{ $traps->{ $_ }{ action } };
486 }
487  
488 return 1;
489 }
490  
491  
492 unless( DB::can_break( $file, $line ) ) {
493 print $DB::OUT file(). "This line is not breakable. Can not set action at this point\n";
494 }
495  
496  
497 my $data = DB::reg( 'trap', 'action', $file, $line );
498 #FIX: register callback only once at some global structure
499 $$data->{ code } = \&get_expr_a;
500 push @{ $$data->{ eval } }, $expr;
501  
502  
503 return 1;
504 }
505  
506  
507  
508 # Stop on the first OP in a given subroutine
509 sub stop_on_call {
510 my( $data, $current_sub ) = @_;
511 my $target_subs = $data->{ list };
512  
513 if(
514 # IF exists && not disabled (value is TRUE)
515 $target_subs->{ $current_sub }
516  
517 # OR exists && not disabled && partially matched name
518 || grep{
519 $target_subs->{ $_ }
520 &&
521 $current_sub =~ m/$_$/
522 } keys %$target_subs
523 ) {
524 DB::state( 'single', 1 ); # Stop on next OP
525 }
526 }
527  
528  
529  
530 # Stop if trap is not disabled and condition evaluated to TRUE value
531 sub stop_on_line {
532 my( $data ) = @_;
533  
534 return 0 if $data->{ disabled } || !exists $data->{ condition };
535  
536 return [ sub{ my $x = shift; @$x && $x->[0] && 1 }, $data->{ condition } ];
537 }
538  
539  
540  
541 sub step_done {
542 my( $data ) = @_;
543 return if --$data->{ steps_left };
544  
545 DB::unreg( 'stop', 'step' );
546 return 1;
547 };
548  
549  
550  
551 sub nested {
552 no warnings 'void';
553 2;
554 printf $DB::OUT "%s at %s:%s\n"
555 ,DB::state( 'single' ), DB::state( 'file' ), DB::state( 'line' );
556 3;
557 }
558  
559  
560  
561 $DB::commands = {()
562 ,debug => sub {
563 no warnings 'void';
564 1;
565 nested();
566 4;
567 }
568 ,'.' => sub {
569 $curr_file = DB::state( 'file' );
570 $line_cursor = DB::state( 'line' );
571  
572 (my $tmp=DB::source()->[ $line_cursor ]) =~ s/^\s+//;
573 print $DB::OUT "$curr_file:$line_cursor $tmp";
574  
575 1;
576 }
577  
578 ,st => sub {
579 print $DB::OUT dd( DB::state( 'stack' ), DB::state( 'goto_frames' ) );
580 print $DB::OUT "S: $DB::single T:$DB::trace A:$DB::signal\n";
581  
582 1;
583 }
584  
585 # Return from sub call to the first OP at some outer frame
586 # In compare to 's' and 'n' commands 'r' will not stop at each OP. So we set
587 # 0 to $DB::single for current frame and N-1 last frames. For target N frame
588 # we set $DB::single value to 1 which will be restored by &pop_frame
589 # Therefore DB::DB will be called at the first OP followed this sub call
590 ,r => sub {
591 my( $frames_out, $sharp ) = shift =~ m/^(\d+)(\^)?$/;
592  
593 my $leave_chain = defined $frames_out;
594 $frames_out //= 1;
595  
596 my $stack = DB::state( 'stack' );
597 my $stack_size = @$stack;
598 $frames_out = $stack_size -$frames_out if $sharp;
599 return -2 if $frames_out < 0; # Do nothing for unexisting frame
600  
601 # Return to the last possible frame. If no frames then exit from script
602 $frames_out = $stack_size if $frames_out > $stack_size;
603  
604 # ... skip N next frames
605 $_->{ single } = 0 for @$stack[ -$frames_out .. -1 ];
606  
607 # and stop at some outer frame
608 $_->{ single } = 1 for @$stack[ -$stack_size .. -$frames_out-1 ];
609  
610 # Do not stop if subcall is maden
611 if( $leave_chain && $frames_out < $stack_size ) { # have parent frame
612 my $handler = DB::reg( 'call', 'step_over' );
613 # FIX: move 'on_frame' handler code to upper frames if we leave current one
614 $$handler->{ code } = sub{ DB::state( 'single', 0 ); 1 };
615 $handler = DB::reg( 'stop', 'step_over' );
616 $$handler->{ code } = sub{
617 DB::unreg( 'stop', 'step_over' );
618 DB::unreg( 'call', 'step_over' );
619 1;
620 };
621 }
622  
623 return;
624 }
625  
626 # Do single step to the next OP
627 # Here we force $DB::single = 1 for current frame and all outer frames.
628 # Because current OP maybe the last OP in sub. It also maybe the last OP in
629 # the outer frame. And so on.
630 ,s => sub {
631 if( shift =~ m/^(\d+)$/ ) {
632 my $handler = DB::reg( 'stop', 'step' );
633 $$handler->{ code } = \&step_done;
634 $$handler->{ steps_left } = $1;
635 }
636  
637 $_->{ single } = 1 for @{ DB::state( 'stack' ) };
638  
639 return;
640 }
641  
642 # Do single step to the next OP. If current OP is sub call. Step over it
643 # ...As for the 's' command we stop at each OP in the script. But when the
644 # sub is called we turn off debugging for that sub at DB::sub.
645 # DB::state( 'single', 0 ) if $DB::single & 2;
646 # After that sub returns $DB::single will be restored because of localizing
647 # Therefore DB::DB will be called at the first OP followed this sub call
648 ,n => sub {
649 if( shift =~ m/^(\d+)$/ ) {
650 my $handler = DB::reg( 'stop', 'step' );
651 $$handler->{ code } = \&step_done;
652 $$handler->{ steps_left } = $1;
653 }
654  
655  
656 # Do not stop if subcall is maden
657 my $handler = DB::reg( 'frame', 'step_over' );
658 # FIX: move handler code to upper frames if we leave current one
659 $$handler->{ code } = sub{ $_[1]{ single } = 0; 1 };
660 $handler = DB::reg( 'stop', 'step_over' );
661 $$handler->{ code } = sub{
662 DB::unreg( 'stop', 'step_over' );
663 DB::unreg( 'frame', 'step_over' );
664 1;
665 };
666  
667 my $stack = DB::state( 'stack' );
668 # If the current OP is last OP in this sub we stop at *some* outer frame
669 $_->{ single } = 2 for @$stack;
670  
671 return;
672 }
673  
674 # Quit from the debugger
675 ,q => sub {
676 for( @$DB::state ) {
677 for( @{ $_->{ stack } } ) { # TODO: implement interface to debugger instance
678 $_->{ single } = 0;
679 }
680 }
681  
682 exit;
683 }
684  
685 # TODO: print list of vars which refer this one
686 ,vars => sub {
687 my( $level, $type, $var ) =
688 (' '.shift) =~ m/^(?:\s+-(\d+))?(?:\s+([amogucs]+))?(?:\s+([\$\%\*\&].*))?$/;
689  
690 for( split '', $type ) {
691 $type |= ~0 if /^a|all$/;
692 $type |= 1 if /^m|my$/;
693 $type |= 2 if /^o|our$/;
694 $type |= 4 if /^g|global$/;
695 $type |= 8 if /^u|used$/;
696 $type |= 16 if /^c|closured$/;
697 $type |= 24 if /^s|sub$/; #u+c
698 }
699 $level //= DB::state( 'list.level' );
700 $type //= DB::state( 'vars.type' ) // 3 unless $var;
701  
702  
703 my $dbg_frames = 0;
704 { # Count debugger frames
705 my @frame;
706 1 while( @frame = caller( $dbg_frames++ ) and $frame[3] ne 'DB::DB' );
707 $dbg_frames--;
708 }
709  
710  
711 #FIX: When we debug debugger we can not 'go <line>' we always stops at
712 #require at third line at PadWalker.pm. Debug who set $DB::state = 1
713 require 'PadWalker.pm';
714 require 'Package/Stash.pm'; # BUG? spoils DB:: by emacs, dbline
715  
716 my $my = PadWalker::peek_my( $level +$dbg_frames );
717 my $our = PadWalker::peek_our( $level +$dbg_frames );
718  
719 if( $type & 1 ) {
720 # TODO: for terminals which support color show
721 # 1. not used variables as grey
722 # 2. closed over variables as green or bold
723 print $DB::OUT "\nMY:\n", join( ', ', sort keys %$my ), "\n";
724 }
725  
726 if( $type & 2 ) {
727 print $DB::OUT "\nOUR:\n", join( ', ', sort keys %$our ), "\n";
728 }
729  
730 if( $type & 4 ) {
731 my $stash = Package::Stash->new( DB::state( 'package' ) )->get_all_symbols();
732 # Show only user defined variables
733 # TODO? implement verbose flag
734 if( DB::state( 'package' ) eq 'main' ) {
735 for( keys %$stash ) {
736 delete $stash->{ $_ } if /::$/;
737 delete $stash->{ $_ } if /^_</;
738 delete $stash->{ $_ } if /^[\x00-0x1f]/; #Remove $^ variables
739 }
740  
741 delete @$stash{ qw# STDERR stderr STDIN stdin STDOUT stdout # };
742 delete @$stash{ qw# SIG INC F ] ENV ; > < ) ( $ " _ # }; # a b
743 delete @$stash{ qw# - + ` & ' #, 0..99 };
744 # BUG? warning still exists despite on explicit escaping of ','
745 delete @$stash{ qw# ARGV ARGVOUT \, . / \\ | # };
746 delete @$stash{ qw# % - : = ^ ~ # };
747 delete @$stash{ qw# ! @ ? # };
748 }
749 delete $stash->{ sub } if DB::state( 'package' ) eq 'DB';
750  
751 my @globals = ();
752 my %sigil = ( SCALAR => '$', ARRAY => '@', HASH => '%' );
753 for my $key ( keys %$stash ) {
754 my $glob = $stash->{ $key };
755 for my $type ( keys %sigil ) {
756 next unless defined *{ $glob }{ $type };
757 next if $type eq 'SCALAR' && !defined $$glob;
758 next if $key =~ /::/;
759 push @globals, $sigil{ $type } .$key;
760 }
761 }
762  
763 print $DB::OUT "\nGLOBAL:\n", join( ', ', sort @globals ), "\n";
764 }
765  
766 if( $type & 8 ) {
767 print $DB::OUT "\nUSED:\n";
768  
769 # First element starts at -1 subscript
770 # FIX: When debug debugger and we step over this statement
771 # the $sub contain reference ot &vars instead of name of last
772 # client's sub
773 my $sub = DB::state( 'stack' )->[ -$level -1 ]{ sub };
774 if( !defined $sub ) {
775 # TODO: Mojolicious::__ANON__[/home/feelsafe/perl_lib/lib/perl5/Mojolicious.pm:119]
776 # convert this to subroutine refs
777 # print $DB::OUT "Not in a sub: $sub\n";
778 print $DB::OUT "Not in a sub\n";
779 }
780 else {
781 $sub = \&$sub;
782 print $DB::OUT join( ', ', sort keys %{ PadWalker::peek_sub( $sub ) } ), "\n";
783 }
784 }
785  
786 if( $type & 16 ) {
787 print $DB::OUT "\nCLOSED OVER:\n";
788  
789 # First elements starts at -1 subscript
790 my $sub = DB::state( 'stack' )->[ -$level -1 ]{ sub };
791 if( !defined $sub ) {
792 print $DB::OUT "Not in a sub\n";
793 # print $DB::OUT (ref $sub ) ."Not in a sub: $sub\n";
794 }
795 else {
796 $sub = \&$sub;
797 print $DB::OUT join( ', ', sort keys %{ (PadWalker::closed_over( $sub ))[0] } ), "\n";
798 }
799 }
800  
801 if( $var ) {
802 my( $sigil, $name, $extra ) = $var =~ m/^(.)(\w+)(.*)$/;
803  
804 $var = $sigil .$name;
805 unless( exists $my->{ $var } || exists $our->{ $var } ) {
806 print $DB::OUT "Variable '$var' does not exists at this scope\n";
807 return -1;
808 }
809  
810 my $value = $my->{ $var } || $our->{ $var };
811 $value = $$value if $sigil eq '$';
812 eval "\$value = \$value$extra; 1" or die $@;
813 print $DB::OUT dd( $value ), "\n";
814 }
815  
816 1;
817 }
818 ,B => sub {
819 my( $args, $opts ) = @_;
820 $opts->{ verbose } //= 1;
821  
822 if( $_[0] eq '*' ) {
823 #TODO: implement removing all traps
824 #B 3:* - remove all traps from file number 3
825 }
826  
827  
828 my( $file, $line, $subname ) = shift =~ m/^${file_line}|([\w:]+|&)$/;
829  
830  
831 if( defined $subname ) {
832 if( $subname eq '&' ) {
833 $subname = DB::state( 'goto_frames' )->[ -1 ][ 3 ];
834 return -1 if ref $subname; # can not set trap on coderef
835 }
836  
837 DB::unreg( 'call', 'breakpoint', $subname );
838 # Q: Should we remove all matched keys?
839 # A: No. You may remove required keys. Maybe *subname?
840 }
841 else {
842 #FIX: this is copy/paste block. see above
843 $line = DB::state( 'line' ) if $line eq '.';
844 my $traps = DB::traps( file( $file, 1 ) );
845 return -1 unless exists $traps->{ $line };
846  
847 # TODO: remove only one action
848 DB::unreg( 'trap', 'breakpoint', $file, $line );
849 }
850  
851  
852 #TODO? use &DB::process
853 $DB::commands->{ b }->() if $opts->{ verbose };
854  
855 1;
856 }
857  
858 ,b => sub {
859 my( $sign, $file, $line, $subname, $condition, $tmp ) =
860 shift =~ m/^([-+])?(?:${file_line}|([\w:]+|&))(?:\s+(.*?))?(!)?$/;
861  
862  
863 if( defined $subname ) {
864 if( $subname eq '&' ) {
865 $subname = DB::state( 'goto_frames' )->[ -1 ][ 3 ];
866 return -1 if ref $subname; # can not set trap on coderef
867 }
868  
869 my $data = DB::reg( 'call', 'breakpoint' );
870 $$data->{ code } = \&stop_on_call;
871 $$data->{ list } =
872 { $subname => defined $sign && $sign eq '-' ? 0 : 1};
873  
874 return 1;
875 }
876  
877  
878 $line = DB::state( 'line' ) if $line eq '.';
879 $file = file( $file, 1 );
880  
881  
882 # list all breakpoints
883 unless( $line ) {
884 my $cmd_f = [];
885 my $file_no = 0;
886 # First display traps in the current file
887 print $DB::OUT "Breakpoints:\n";
888 for my $source ( $file, grep { $_ ne $file } keys %$DB::_tfiles ) {
889 my $traps = DB::traps( $source );
890 next unless keys %$traps;
891  
892 push @$cmd_f, $source;
893 print $DB::OUT $file_no++ ." $source\n";
894  
895 for( sort{ $a <=> $b } keys %$traps ) {
896 # FIX: the trap may be in form '293 => {}' in this case
897 # we do not see it ever
898 # next unless exists $traps->{ $_ }{_breakpoint}{ condition }
899 # || exists $traps->{ $_ }{ breakpoint }{ onetime }
900 # || exists $traps->{ $_ }{ breakpoint }{ disabled }
901 # ;
902  
903 printf $DB::OUT " %-3d%s %s\n"
904 ,$_
905 ,exists $traps->{ $_ }{ onetime } ? '!'
906 :(exists $traps->{ $_ }{ breakpoint }{ disabled } ? '-' : ':')
907 ,$traps->{ $_ }{ breakpoint }{ condition }
908 ;
909  
910 warn "The breakpoint at $_ is zero and should be deleted"
911 if $traps->{ $_ } == 0;
912 }
913 }
914  
915 DB::state( 'cmd.f', $cmd_f );
916  
917 print $DB::OUT "Stop on subs:\n";
918 my $target_subs = DB::state( 'on_call' ); #TODO: get info
919 $target_subs = $target_subs->{'breakpoint'}{ list };
920  
921 print $DB::OUT ' ' .($target_subs->{ $_ } ? ' ' : '-') ."$_\n"
922 for keys %$target_subs;
923  
924 return 1;
925 }
926  
927  
928 # set breakpoint
929 unless( DB::can_break( $file, $line ) ) {
930 print $DB::OUT file(). " -- $file This line is not breakable\n";
931 # Set breakpoint in any case. This is usefull when you edit file
932 # and want to add traps to those new lines
933 # return -1;
934 }
935  
936 my $traps = DB::traps( $file );
937  
938 # One time trap just exists or not.
939 # We stop on it uncoditionally, also we can not disable it
940 if( defined $tmp ) {
941 my $data = DB::reg( 'trap', 'onetime', $file, $line );
942 $$data->{ code } = sub{ DB::unreg( 'trap', 'onetime', $file, $line ); 1 };
943 }
944 else {
945 my $data = DB::reg( 'trap', 'breakpoint', $file, $line );
946 $$data->{ code } = \&stop_on_line;
947  
948 # TODO: Move trap from/into $traps into/from $disabled_traps
949 # This will allow us to not trigger DB::DB if trap is disabled
950 $$data->{ disabled } = 1 if $sign eq '-';
951 delete $$data->{ disabled } if $sign eq '+';
952  
953 $$data->{ condition } = $condition if defined $condition;
954 $$data->{ condition } //= 1; # trap always triggered by default
955 }
956  
957 return 1;
958 }
959  
960 ,go => sub {
961 # If we supply line to go to we just set temporary trap there
962 if( defined $_[0] && $_[0] ne '' ) {
963 #FIX: use &process
964 return 1 if 0 > $DB::commands->{ b }->( "$_[0]!" );
965 }
966  
967 $_->{ single } = 0 for @{ DB::state( 'stack' ) };
968  
969 #TODO: Implement force mode to run code until the end despite on traps
970  
971 return;
972 }
973  
974 ,f => sub {
975 my( $args, $expr ) = @_;
976  
977 # Set current file to selected one:
978 if( $args ne '' && $args =~ /^(\d+)$/ ) {
979 print $DB::OUT file( $args ) ."\n";
980 return 1;
981 }
982  
983 # List available files
984 my $cmd_f = [];
985 my $file_no = 0;
986 for( sort $0, values %INC, DB::sources() ) {
987 # for( sort $0, keys %$expr ) {
988 if( /(?:$args)/ ) {
989 push @$cmd_f, $_;
990 print $DB::OUT $file_no++ ." $_\n";
991 }
992 }
993 DB::state( 'cmd.f', $cmd_f );
994  
995 1;
996 }
997 ,e => sub {
998 return [()
999 ,sub{
1000 if( ref $_[0] eq 'DB::Error' ) {
1001 print $DB::OUT "ERROR: $_[0]";
1002 }
1003 else {
1004 print $DB::OUT dd( @{ $_[0] } ) ."\n";
1005 }
1006 return { code => \&interact };
1007 }
1008 ,length $_[0] ? shift : DB::state( 'db.last_eval' ) // ''
1009 ];
1010 }
1011  
1012  
1013 # TODO: give names to ANON
1014 ,T => sub {
1015 my( $one, $count ) = shift =~ m/^(-?)(\d+)$/;
1016 $count = -1 unless $count; # Show all frames. $count == 0 never
1017  
1018 my $T = {()
1019 ,oneline =>
1020 '"\n$d $type $context $subname$args <-- $file:$line\n"'
1021 ,multiline =>
1022 '"\n$d $type $subname\n $context $args\n <-- $file:$line\n"'
1023 };
1024 my $format = 'multiline';
1025  
1026 my @frames = DB::frames();
1027 my $number = -1;
1028 if( $one ) {
1029 return 1 unless @frames >= $count; # Check we have enough frames
1030 @frames = $frames[ $count -1 ]; # Get only given frame
1031 $number = -$count; # ...and display it number
1032 }
1033 for my $frame ( @frames ) {
1034 my $context = $frame->[7]? '@' : defined $frame->[7]? '$' : ';';
1035 my $type = $cmd_T{ $frame->[0] };
1036 my $subname = $frame->[5];
1037 my $args = $frame->[6] ? $frame->[1] : '';
1038 my $file = $frame->[3];
1039 my $line = $frame->[4];
1040  
1041 if( $args ) {
1042 $args = join ', ', map{ defined $_ ? $_ : '&undef' } @$args;
1043 $args = "($args)";
1044 }
1045  
1046 my $d = $frame->[0] eq 'D' ? 'D' : $number;
1047 print $DB::OUT eval $T->{ $format };
1048 $number-- if $frame->[0] ne 'G';
1049 last unless --$count; # Stop to show frames when $count == 0
1050 }
1051  
1052 return 1;
1053 }
1054  
1055 ,l => \&list
1056 ,w => \&watch
1057 ,load => \&load
1058 ,save => \&save
1059 ,pid => sub {
1060 print $DB::OUT Devel::DebugHooks::Server::tinfo();
1061 return 1;
1062 }
1063 ,t => \&trace_variable
1064 ,a => \&action
1065 ,A => sub {
1066 my( $args, $opts ) = @_;
1067  
1068 if( $_[0] eq '*' ) {
1069 #TODO: implement removing all actions
1070 #B 3:* - remove all traps from file number 3
1071 }
1072  
1073  
1074 my( $file, $line ) = shift =~ m/^${file_line}$/;
1075  
1076  
1077 #FIX: this is copy/paste block. see above
1078 $line = DB::state( 'line' ) if $line eq '.';
1079 my $traps = DB::traps( file( $file, 1 ) );
1080 return -1 unless exists $traps->{ $line };
1081  
1082 # TODO: remove only one action
1083 DB::unreg( 'trap', 'action', $file, $line );
1084  
1085 1;
1086 }
1087 ,ge => sub {
1088 my( $file, $line ) = shift =~ m/^${file_line}$/;
1089 $line = DB::state( 'line' ) unless defined $line;
1090 $file = file( $file );
1091  
1092 `$EDITOR $file`;
1093  
1094 1;
1095 }
1096 ,gef => sub {
1097 my( $file, $line ) = shift =~ m/^${file_line}$/;
1098 $line = DB::state( 'line' ) unless defined $line;
1099 $file = file( $file );
1100  
1101 `$EDITOR -f $file`;
1102  
1103 1;
1104 }
1105 ,suspend => sub {
1106 uwsgi::suspend();
1107  
1108 1;
1109 }
1110 ,R => sub {
1111 `killall uwsgi`;
1112 }
1113 ,d => sub {
1114 return [()
1115 ,sub {
1116 print $DB::OUT "\n@_\n";
1117 return { code => \&interact };
1118 }
1119 ,"\$DB::single = 0; \$^D |= (1<<30);".DB::state( 'db.last_eval', shift )
1120 ]
1121 }
1122 };
1123  
1124  
1125  
1126 sub process {
1127 my( $dbg, $str ) = @_;
1128  
1129 my( $cmd, $args_str ) = $str =~ m/^([\w.]+)(?:\s+(.*))?$/;
1130 $args_str //= '';
1131  
1132  
1133 unless( $cmd && exists $DB::commands->{ $cmd } ) {
1134 print $DB::OUT "No such command: '$str'\n" if DB::state( 'ddd' );
1135 return 0;
1136 }
1137  
1138 # The command also should return defined value to keep interaction
1139 print $DB::OUT "Start to process '$cmd' command\n" if DB::state( 'ddd' );
1140 my $result = eval { $DB::commands->{ $cmd }( $args_str ) };
1141 print $DB::OUT "Command '$cmd' processed\n" if DB::state( 'ddd' );
1142 do{ print $DB::OUT "'$cmd' command died: $@"; return -1; } if $@;
1143  
1144 return $result;
1145 }
1146  
1147  
1148  
1149 1;
1150  
1151 __END__
1152 FIX:
1153 x380: if( $errors = !$self->_check( $self->validation ) ) { #2
1154 x381: warn [ $self->validation ];
1155 x382: } elsif( $errors = $self->_save( $self->validation ) ) { #3
1156 383: }
1157 384: else {
1158 385: # inside 'saved' we may return uri to redirect to
1159 >>x386: $redirect_page = $self->_saved( $self->validation ); #4-a
1160 387: }
1161  
1162 if we 'n' from last expression of _check we do not stop on _save
1163  
1164 FIX: do not create new trap if it not exists before 'b +...' command
1165  
1166 FIX: терминал клинит при выводе большой русской и