| 1 | package DB::Hooks::Terminal; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | # We can not track DB::Commands loading if 'use' it before DB::Hooks | ||
| 9 | use DB::Commands qw/ set_command get_command run /; | ||
| 10 | # Commands should be available immediately. This is important when | ||
| 11 | # we are debugging at CT: when DB is in process of loading | ||
| 12 | BEGIN { | ||
| 13 | set_command next_command => \&readline; | ||
| 14 | } | ||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | my $last_command; | ||
| 19 | BEGIN{ $last_command = '' } | ||
| 20 | sub readline { | ||
| 21 | |||
| 22 | print $DB::OUT "\n\e[2;96m", DB::indentation( 'D', $DB::instance +1 ) ,"BG>\e[0m"; | ||
| 23 | my $command = <$DB::IN>; | ||
| 24 | chomp $command; | ||
| 25 | |||
| 26 | return $last_command unless length $command; | ||
| 27 | |||
| 28 | return $last_command = $command; | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | sub on_load { | ||
| 34 | my( $file ) = @_; | ||
| 35 | |||
| 36 | return unless (DB::state( 'TraceLoad' )//0) &1; | ||
| 37 | |||
| 38 | DB::say "\e[32mLoaded\e[0m: \e[1;33m$file\e[0m"; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | sub on_goto { | ||
| 44 | my( $sub ) = @_; | ||
| 45 | |||
| 46 | return unless (DB::state( 'TraceGoto' )//0) &1; | ||
| 47 | |||
| 48 | DB::say "Goto \e[32m$sub\e[0m"; | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | sub on_trace { | ||
| 54 | my( $source, $file, $line ) = @_; | ||
| 55 | $source =~ s/^\s+//; | ||
| 56 | |||
| 57 | DB::say "\e[2;37m$file:$line\e[0m", "t: \e[1;33m$source\e[0m"; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | sub on_call { | ||
| 63 | if( DB::state( 'TraceStack' ) ) { | ||
| 64 | my $stack = DB::state( 'stack' ); | ||
| 65 | DB::state( stack => $stack=[] ) unless $stack; | ||
| 66 | push @$stack, $_[1]; | ||
| 67 | } | ||
| 68 | |||
| 69 | return unless (DB::state( 'TraceCall' )//0) &1; | ||
| 70 | |||
| 71 | my( $sub, $context, @args ) = @_; | ||
| 72 | $context = $context ? '@' : defined $context? '$' : ';'; | ||
| 73 | $sub = "\e[1;32m$sub\e[0m"; | ||
| 74 | @args = DB::vis_undef( @args ); | ||
| 75 | |||
| 76 | local $" = ', '; | ||
| 77 | DB::say "$context $sub( \e[32m@args\e[0m )"; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | sub on_return { | ||
| 83 | if( DB::state( 'TraceStack' ) ) { | ||
| 84 | my $stack = DB::state( 'stack' ); | ||
| 85 | pop @$stack; | ||
| 86 | } | ||
| 87 | |||
| 88 | return unless (DB::state( 'TraceReturn' )//0) &1; | ||
| 89 | |||
| 90 | my( $sub, $context, @values ) = @_; | ||
| 91 | |||
| 92 | $context = $context ? '@' : defined $context? '$' : ';'; | ||
| 93 | $sub = "\e[32m$sub\e[0m"; | ||
| 94 | @values = DB::vis_undef( @values ); | ||
| 95 | |||
| 96 | local $" = ', '; | ||
| 97 | DB::say "$context( \e[32m@values\e[0m ) <-- $sub"; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub on_binteract { | ||
| 103 | run( 'l 0' ); | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | |||
| 108 | sub on_interact { get_command( 'interact' )->( @_ ) }; | ||
| 109 | |||
| 110 | # Subscribe subs to events | ||
| 111 | # on_interact (some debugger commands) does not work without on_call/on_return | ||
| 112 | # NOTICE: subscribe in the given order | ||
| 113 | use DB::Utils qw/ trace call return interact load goto binteract /; | ||
| 114 | |||
| 115 | |||
| 116 | 1; |