| 1 | package DB::Hooks::Remote; | ||
| 2 | |||
| 3 | # You still need to use DB::Hooks::Terminal in conjunction with this module | ||
| 4 | # PERL5DB="use DB::Hooks qw'::Terminal ::Remote'" | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | # We can not track DB::Commands loading if 'use' it before DB::Hooks | ||
| 12 | use DB::Commands qw/ set_command get_command run /; | ||
| 13 | # Commands should be available immediately. This is important when | ||
| 14 | # we are debugging at CT: when DB is in process of loading | ||
| 15 | BEGIN { | ||
| 16 | set_command next_command => \&fetch_command; | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | # We should define utility subs first... | ||
| 21 | ## IO::Async stuff | ||
| 22 | # global DATA | ||
| 23 | my $loop; | ||
| 24 | my $stream; | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | my $ti = 0; | ||
| 29 | sub tinfo { | ||
| 30 | return " - $$ ($loop) w:" .uwsgi::worker_id(); | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | # event handlers | ||
| 35 | sub handle_write_eof { | ||
| 36 | die "Write error: >>@_<<"; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub handle_write_error { | ||
| 41 | die "Write error: >>@_<<"; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | # TODO: Need to check. Because of eof does not seen until write the event is not | ||
| 46 | # occour until write handle is closed | ||
| 47 | sub handle_closed { | ||
| 48 | $DB::OUT = \*STDOUT; | ||
| 49 | undef $stream; | ||
| 50 | my $time = localtime(); | ||
| 51 | warn "$time Session closed"; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub read_command { | ||
| 56 | my( $self, $buffref, $eof ) = @_; | ||
| 57 | |||
| 58 | warn 'New Command: ' .$$buffref if length $$buffref; | ||
| 59 | |||
| 60 | |||
| 61 | if( $$buffref =~ s/^(.*?)\r?(\n)// ) { | ||
| 62 | warn "\nThis is the thread (RC): " .tinfo() ."\n\n" if $ti; | ||
| 63 | |||
| 64 | $$buffref = "$1$2$$buffref" unless defined &readline( "$1$2" ); | ||
| 65 | |||
| 66 | return 0; | ||
| 67 | } | ||
| 68 | |||
| 69 | warn "Text '$$buffref' is left in buffer" if $$buffref; | ||
| 70 | |||
| 71 | if( $eof ) { | ||
| 72 | warn "TTYIN EOF"; | ||
| 73 | $self->close_when_empty(); | ||
| 74 | } | ||
| 75 | |||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub start_dbg_session { | ||
| 81 | if( defined $stream ) { | ||
| 82 | $_[0]->write( "Debugging session is attached already\n" ); | ||
| 83 | $_[0]->close_when_empty(); | ||
| 84 | return; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | warn "New client connected"; | ||
| 89 | ( $stream ) = @_; | ||
| 90 | |||
| 91 | $stream->configure( | ||
| 92 | close_on_read_eof => 1 | ||
| 93 | ,on_read => \&read_command | ||
| 94 | ,on_write_eof => \&handle_write_eof | ||
| 95 | ,on_write_error => \&handle_write_error | ||
| 96 | ,on_closed => \&handle_closed | ||
| 97 | ,autoflush => 1 | ||
| 98 | ,write_all => 1 | ||
| 99 | ); | ||
| 100 | |||
| 101 | $loop->add( $stream ); | ||
| 102 | $DB::OUT = $stream->read_handle(); | ||
| 103 | |||
| 104 | if( $ti ) { | ||
| 105 | my $str = "This is the thread (Start): " .tinfo() ."\n"; | ||
| 106 | $stream->write( $str ); | ||
| 107 | warn $str; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub listen { | ||
| 113 | my( $loop ) = @_; | ||
| 114 | |||
| 115 | $loop->listen( | ||
| 116 | # family => 'unix', | ||
| 117 | # path => 'file.sock', | ||
| 118 | family => 'inet', | ||
| 119 | socktype => 'stream', | ||
| 120 | host => '127.0.0.1', | ||
| 121 | service => $ENV{ DBG_PORT } || 9001, | ||
| 122 | on_resolve_error => sub { die "Cannot resolve - $_[1] <<< @_\n"; }, | ||
| 123 | on_listen_error => sub { die "Cannot listen - $_[1] <<< @_\n"; }, | ||
| 124 | on_listen => sub { | ||
| 125 | my( $s ) = @_; | ||
| 126 | |||
| 127 | warn "listening on: " .$s->sockhost . ':' .$s->sockport; | ||
| 128 | warn "\nThis is the thread(Listen): " .tinfo() if $ti; | ||
| 129 | }, | ||
| 130 | |||
| 131 | # This sub is invoked for each new incoming connection | ||
| 132 | on_stream => \&start_dbg_session, | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | use IO::Async::Loop; | ||
| 137 | $loop = IO::Async::Loop->new; | ||
| 138 | |||
| 139 | |||
| 140 | my $dbg_buffer; | ||
| 141 | sub readline { | ||
| 142 | # set | ||
| 143 | if( @_ ) { | ||
| 144 | return if defined $dbg_buffer; | ||
| 145 | |||
| 146 | return( $dbg_buffer = shift ) | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | # get | ||
| 151 | while( !defined $dbg_buffer ) { | ||
| 152 | if( $stream ) { | ||
| 153 | $stream->invoke_event( 'on_read', \$stream->{ readbuff } ); | ||
| 154 | last if defined $dbg_buffer; | ||
| 155 | } | ||
| 156 | |||
| 157 | $loop->loop_once(); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | my $result = $dbg_buffer; | ||
| 162 | undef $dbg_buffer; | ||
| 163 | return $result; | ||
| 164 | } | ||
| 165 | # END OF IO::Async stuff | ||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | my $last_command; | ||
| 170 | BEGIN{ $last_command = '' } | ||
| 171 | sub fetch_command { | ||
| 172 | print $DB::OUT "\n\e[2;96m", DB::indentation( 'D', $DB::instance +1 ) ,"BG>\e[0m"; | ||
| 173 | |||
| 174 | my $command = &readline(); | ||
| 175 | chomp $command; | ||
| 176 | |||
| 177 | return $last_command unless length $command; | ||
| 178 | |||
| 179 | return $last_command = $command; | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | &listen( $loop ); | ||
| 184 | $loop->loop_once( 0 ); | ||
| 185 | |||
| 186 | 1; |