| 1 | package Devel::DebugHooks::Server; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # We should define utility subs first... | ||
| 7 | ## IO::Async stuff | ||
| 8 | # global DATA | ||
| 9 | my $loop; | ||
| 10 | my $stream; | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | my $ti = 0; | ||
| 15 | sub tinfo { | ||
| 16 | return " - $$ ($loop) w:" .uwsgi::worker_id(); | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | # event handlers | ||
| 21 | sub handle_write_eof { | ||
| 22 | die "Write error: >>@_<<"; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub handle_write_error { | ||
| 27 | die "Write error: >>@_<<"; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | # TODO: Need to check. Because of eof does not seen until write the event is not | ||
| 32 | # occour until write handle is closed | ||
| 33 | sub handle_closed { | ||
| 34 | $DB::OUT = \*STDOUT; | ||
| 35 | undef $stream; | ||
| 36 | my $time = localtime(); | ||
| 37 | `echo "$time Session closed\n" >> /home/feelsafe/loop`; | ||
| 38 | warn "$time Session closed"; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub read_command { | ||
| 43 | my( $self, $buffref, $eof ) = @_; | ||
| 44 | |||
| 45 | warn 'New Command: ' .$$buffref; | ||
| 46 | |||
| 47 | |||
| 48 | if( $$buffref =~ s/^(.*?)\r?(\n)// ) { | ||
| 49 | warn "\nThis is the thread (RC): " .tinfo() ."\n\n" if $ti; | ||
| 50 | |||
| 51 | $$buffref = "$1$2$$buffref" unless defined &readline( "$1$2" ); | ||
| 52 | |||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | warn "Text '$$buffref' is left in buffer" if $$buffref; | ||
| 57 | |||
| 58 | if( $eof ) { | ||
| 59 | warn "TTYIN EOF"; | ||
| 60 | $self->close_when_empty(); | ||
| 61 | } | ||
| 62 | |||
| 63 | return 0; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub start_dbg_session { | ||
| 68 | if( defined $stream ) { | ||
| 69 | $_[0]->write( "Debugging session is attached already\n" ); | ||
| 70 | $_[0]->close_when_empty(); | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | warn "New client connected"; | ||
| 76 | ( $stream ) = @_; | ||
| 77 | |||
| 78 | $stream->configure( | ||
| 79 | close_on_read_eof => 0 | ||
| 80 | ,on_read => \&read_command | ||
| 81 | ,on_write_eof => \&handle_write_eof | ||
| 82 | ,on_write_error => \&handle_write_error | ||
| 83 | ,on_closed => \&handle_closed | ||
| 84 | ,autoflush => 1 | ||
| 85 | ,write_all => 1 | ||
| 86 | ); | ||
| 87 | |||
| 88 | $loop->add( $stream ); | ||
| 89 | $DB::OUT = $stream->read_handle(); | ||
| 90 | |||
| 91 | if( $ti ) { | ||
| 92 | my $str = "This is the thread (Start): " .tinfo() ."\n"; | ||
| 93 | $stream->write( $str ); | ||
| 94 | warn $str; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub listen { | ||
| 100 | my( $loop ) = @_; | ||
| 101 | |||
| 102 | $loop->listen( | ||
| 103 | # family => 'unix', | ||
| 104 | # path => 'file.sock', | ||
| 105 | family => 'inet', | ||
| 106 | socktype => 'stream', | ||
| 107 | host => '127.0.0.1', | ||
| 108 | service => 9000, | ||
| 109 | on_resolve_error => sub { die "Cannot resolve - $_[1] <<< @_\n"; }, | ||
| 110 | on_listen_error => sub { die "Cannot listen - $_[1] <<< @_\n"; }, | ||
| 111 | on_listen => sub { | ||
| 112 | my( $s ) = @_; | ||
| 113 | |||
| 114 | warn "listening on: " .$s->sockhost . ':' .$s->sockport; | ||
| 115 | warn "\nThis is the thread(Listen): " .tinfo() if $ti; | ||
| 116 | }, | ||
| 117 | |||
| 118 | # This sub is invoked for each new incoming connection | ||
| 119 | on_stream => \&start_dbg_session, | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | # Setup and process $loop | ||
| 128 | # We can uncoment this if we want debugger messages were sent to dbg client | ||
| 129 | # BEGIN { | ||
| 130 | # use IO::Async::Loop; | ||
| 131 | # $loop = IO::Async::Loop->new; | ||
| 132 | # # As soon as possible | ||
| 133 | # # But, it is better that only worker listens to :9000 | ||
| 134 | # # And because of BEGIN block the IO::Async modules are noticed by debugger | ||
| 135 | # &listen( $loop ); | ||
| 136 | # sleep 2; # wait dbg client to connect | ||
| 137 | # $loop->loop_once( 0 ); | ||
| 138 | # } | ||
| 139 | use IO::Async::Loop; | ||
| 140 | $loop = IO::Async::Loop->new; | ||
| 141 | |||
| 142 | |||
| 143 | sub uwsgi_signal_handler { | ||
| 144 | # warn time() ." Singal" .tinfo() ."\n" if $ti; | ||
| 145 | warn 'Signal arrive'; | ||
| 146 | |||
| 147 | # First of all we should process loop queue: connect client, read data... | ||
| 148 | $loop->loop_once( 0 ); | ||
| 149 | |||
| 150 | # ...now we can do something with that | ||
| 151 | DB::interact( 1 ); | ||
| 152 | warn 'Signal processed'; | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | |||
| 157 | # uwsgi::postfork( sub{ | ||
| 158 | # print $DB::OUT time() ." Forked\n"; | ||
| 159 | # $DB::commands->{ load }->(); | ||
| 160 | # &listen( $loop ); | ||
| 161 | # $loop->loop_once( 0 ); | ||
| 162 | # # Useless. Signals are sended to worker only after it is forked | ||
| 163 | # # uwsgi::signal( 1 ); | ||
| 164 | # # NOTE: if I 'die' here I get assertion | ||
| 165 | # # uwsgi: perl.c:539: perl_destruct: Assertion `(my_perl->Iscopestack_ix) == 1' failed. | ||
| 166 | # }); | ||
| 167 | |||
| 168 | |||
| 169 | # uwsgi::register_signal( 1, 'workers', \&uwsgi_signal_handler ); | ||
| 170 | # uwsgi::add_timer( 1, 1 ); | ||
| 171 | |||
| 172 | |||
| 173 | |||
| 174 | my $dbg_buffer; | ||
| 175 | sub readline { | ||
| 176 | # set | ||
| 177 | if( @_ ) { | ||
| 178 | return if defined $dbg_buffer; | ||
| 179 | |||
| 180 | return( $dbg_buffer = shift ) | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | # get | ||
| 185 | while( !defined $dbg_buffer ) { | ||
| 186 | if( $stream ) { | ||
| 187 | $stream->invoke_event( 'on_read', \$stream->{ readbuff } ); | ||
| 188 | last if defined $dbg_buffer; | ||
| 189 | } | ||
| 190 | |||
| 191 | $loop->loop_once(); | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | my $result = $dbg_buffer; | ||
| 196 | undef $dbg_buffer; | ||
| 197 | return $result; | ||
| 198 | } | ||
| 199 | # END OF IO::Async stuff | ||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | sub bbreak { | ||
| 204 | my $self = shift; | ||
| 205 | |||
| 206 | Devel::DebugHooks::Commands->process( 'l 0' ); | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | # ... define another utilities that can be called at CT | ||
| 211 | my $last_input = 's'; | ||
| 212 | sub get_command { | ||
| 213 | my $self = shift; | ||
| 214 | |||
| 215 | my $line = &readline(); | ||
| 216 | chomp $line; | ||
| 217 | if( $line ne '' ) { | ||
| 218 | $last_input = $line; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | $line = $last_input; | ||
| 222 | } | ||
| 223 | |||
| 224 | return $line; | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 228 | # ... now we can use DebugHooks | ||
| 229 | our @ISA; | ||
| 230 | |||
| 231 | BEGIN { | ||
| 232 | $DB::options{ trace_load } //= 1; | ||
| 233 | $DB::options{ trace_subs } //= 0; | ||
| 234 | $DB::options{ trace_returns } //= 0; | ||
| 235 | $DB::options{ _debug } //= 0; | ||
| 236 | $DB::options{ dbg_frames } //= 0; | ||
| 237 | $DB::options{ NonStop } //= 0; | ||
| 238 | @DB::options{ qw/ w s / } = ( 1, 1 ); | ||
| 239 | push @ISA, 'Devel::DebugHooks'; | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | |||
| 244 | sub import { | ||
| 245 | my $class = shift; | ||
| 246 | |||
| 247 | $class->SUPER::import( @_ ); | ||
| 248 | } | ||
| 249 | |||
| 250 | |||
| 251 | use Devel::DebugHooks(); | ||
| 252 | |||
| 253 | #TODO: Think about this syntax: | ||
| 254 | # $handler = DB::reg( 'interact', 'Server', $sub, $context, @other args ); | ||
| 255 | # Here we may pass or silent $context. All will depends on our need | ||
| 256 | my $handler = DB::reg( 'interact', 'Server' ); | ||
| 257 | $$handler->{ context } = $DB::dbg; | ||
| 258 | ##FIX: We may pass context as first argument | ||
| 259 | # $$handler->{ args } = [ $DB::dbg ] | ||
| 260 | $$handler->{ code } = \&Devel::DebugHooks::Commands::interact; | ||
| 261 | |||
| 262 | #FIX: Decide where to complete subscribtion: from &import of from RT of module | ||
| 263 | $handler = DB::reg( 'bbreak', 'Server' ); | ||
| 264 | $$handler->{ context } = $DB::dbg; | ||
| 265 | $$handler->{ code } = \&bbreak; | ||
| 266 | |||
| 267 | |||
| 268 | &listen( $loop ); | ||
| 269 | $loop->loop_once( 0 ); | ||
| 270 | |||
| 271 | 1; |