| 1 | package DB; | ||
| 2 | |||
| 3 | # NOTICE: Subroutines compiled at DB:: package are not debuggable | ||
| 4 | |||
| 5 | ## Manual | ||
| 6 | # Get general information about frames. | ||
| 7 | # In void context instead print them. | ||
| 8 | sub f { | ||
| 9 | my( $count, $lvl ) = @_; | ||
| 10 | $count //= -1; # infinite | ||
| 11 | |||
| 12 | my @frames; | ||
| 13 | local $" = ' - '; | ||
| 14 | while( $count-- && (my @frame = caller( ++$lvl )) ) { | ||
| 15 | push @frames, [ @frame[0..3] ]; | ||
| 16 | } | ||
| 17 | |||
| 18 | |||
| 19 | # Print frames in void context: | ||
| 20 | !defined wantarray | ||
| 21 | or return \@frames; | ||
| 22 | |||
| 23 | DB::say( "@$_" ) for @frames; | ||
| 24 | return; | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | # Returns the number of frames without debugger | ||
| 30 | sub fcount { | ||
| 31 | my $count; my $db = 0; | ||
| 32 | |||
| 33 | if( $db += DB::state( 'dbcall' ) ) { | ||
| 34 | $db++ while (caller( $count++ ))[3] ne 'DB::dbcall'; | ||
| 35 | } | ||
| 36 | $count++ while caller( $count ); | ||
| 37 | |||
| 38 | return $count -$db; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | # Dump the specified amount of frames with all frame information | ||
| 44 | sub frames { | ||
| 45 | my( $count ) = @_; | ||
| 46 | $count ||= -1; # infinite | ||
| 47 | |||
| 48 | my $dbcall = DB::state( 'dbcall' ); | ||
| 49 | my $frames = []; | ||
| 50 | my $lvl = 0; | ||
| 51 | if( ( !defined $_[0] || $_[0] != 0 ) && $dbcall ) { | ||
| 52 | $lvl++ while (caller( $lvl ))[3] ne 'DB::dbcall'; | ||
| 53 | $lvl += $dbcall; | ||
| 54 | } | ||
| 55 | |||
| 56 | while( $count-- && (my @frame = caller( $lvl++ )) ) { | ||
| 57 | if( my $args = eval{ [ @DB::args ] } ) { | ||
| 58 | push @$frames, [ [ @DB::args ], @frame ]; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | push @$frames, [ [ 'FREED' ], @frame ]; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | return $frames; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | # Just put DB::stop in any debugger command and execution will be stopped there | ||
| 71 | # The same as DB::x but for debuggable code (without is_debuggeable flag enabled) | ||
| 72 | # inside debugger. | ||
| 73 | sub stop { | ||
| 74 | # return unless !@_ || $_[0]; | ||
| 75 | return $^D|= (1<<30), $DB::single = 1 if !@_; | ||
| 76 | return $^D|= (1<<30), $DB::single = 1 if $DB::x{ $_[0] }; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | =item x | ||
| 82 | |||
| 83 | DB::x -- break script execution | ||
| 84 | DB::x 'str' -- break script execution if $DB::x{ 'str' } is true | ||
| 85 | |||
| 86 | The second form is useful in cases when some code is executed frequently. Thus we | ||
| 87 | can not put DB::x expression there. So we write 'DB::x = "str"' there. This means | ||
| 88 | execution will be stopped there only when '$DB::x{str} = 1' expression will be | ||
| 89 | reached in a main script. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | our %x; | ||
| 94 | sub x { | ||
| 95 | # return unless !@_ || $_[0] | ||
| 96 | return $DB::single = 1 if !@_; | ||
| 97 | return $DB::single = 1 if $DB::x{ $_[0] }; | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | |||
| 102 | sub xx { | ||
| 103 | return $DB::stop = 1 if !@_; | ||
| 104 | return $DB::single = 1 if $DB::stop && $_[0]; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | # Display the important flags about current state of debugger. | ||
| 110 | sub flags { | ||
| 111 | return "s:$DB::single t:$DB::trace i:$DB::instance c:".@DB::state; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | # Reflect the fact that we started a debug debugger session. | ||
| 117 | sub start_dd { | ||
| 118 | $DB::instance++; | ||
| 119 | DB::say( "\e[91mDebug debugger\e[0m " .DB::flags ) if DB::state( 'ddd' ); | ||
| 120 | DB::state( xx => 1 ); | ||
| 121 | } | ||
| 122 | |||
| 123 | |||
| 124 | |||
| 125 | # Reflect the fact that we stopped a debug debugger session. | ||
| 126 | sub finish_dd { | ||
| 127 | DB::state( xx => undef ); | ||
| 128 | DB::say( "\e[91mFinish debugger debugging\e[0m " .DB::flags ) if DB::state( 'ddd' ); | ||
| 129 | $DB::instance--; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | # Colorise text using ESCape sequences. | ||
| 135 | sub _c { | ||
| 136 | return shift if $ENV{ DEBUGGER_NO_COLOR }; | ||
| 137 | my( $str, $color ) = @_; | ||
| 138 | return "\e[${color}m$str\e[0m"; | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | |||
| 143 | ## External | ||
| 144 | sub deparse { | ||
| 145 | my( $coderef ) = shift; | ||
| 146 | require B::Deparse; | ||
| 147 | return $coderef unless ref $coderef; | ||
| 148 | return B::Deparse->new("-p", "-sC")->coderef2text( $coderef ); | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | # use Sub::Identify qw/ sub_name /; | ||
| 154 | use B qw/ svref_2object /; | ||
| 155 | |||
| 156 | # Returns subroutine name from its CODE ref. | ||
| 157 | sub sub_name { | ||
| 158 | my $r = shift; | ||
| 159 | return $r unless ref $r; | ||
| 160 | return $r unless my $cv = svref_2object( $r ); | ||
| 161 | # return $r unless $cv->isa( 'B::CV' ); | ||
| 162 | # and my $gv = $cv->GV | ||
| 163 | # ; | ||
| 164 | return $r unless $cv->isa( 'B::CV' ); | ||
| 165 | my $gv = $cv->GV; | ||
| 166 | my $name = ''; | ||
| 167 | |||
| 168 | # https://github.com/Perl/perl5/issues/13187#issuecomment-544047320 | ||
| 169 | if ((!$gv or ref($gv) eq 'B::SPECIAL') and $cv->can('NAME_HEK')) { | ||
| 170 | $name = "HEK $gv: >" .$cv->NAME_HEK ."<"; | ||
| 171 | } | ||
| 172 | #$gv or return; | ||
| 173 | |||
| 174 | # if( ref $gv eq 'B::SPECIAL' ) { | ||
| 175 | # warn "$r"; | ||
| 176 | # warn "What is this: $name -- $r\n"; | ||
| 177 | # Devel::Peek::Dump( $r ); | ||
| 178 | # Devel::Peek::Dump( $cv ); | ||
| 179 | # Devel::Peek::Dump( $gv ); | ||
| 180 | # | ||
| 181 | # my $lvl; | ||
| 182 | # local $" = ' - '; | ||
| 183 | # while( my @frame = caller( ++$lvl ) ) { | ||
| 184 | # warn "@frame[0..3]\n"; | ||
| 185 | # } | ||
| 186 | # warn "X\n"; | ||
| 187 | # warn "Deparsed: ". deparse( $r ) ."\n"; | ||
| 188 | # warn "Z\n"; | ||
| 189 | # } | ||
| 190 | # if( ref $gv eq 'B::SPECIAL' ) { | ||
| 191 | # warn "$r" if $DB::z; | ||
| 192 | # } | ||
| 193 | if ( $gv->can('STASH') && (my $st = $gv->STASH) ) { | ||
| 194 | $name = $st->NAME . '::'; | ||
| 195 | }else{ | ||
| 196 | # warn 'NO STASH'; | ||
| 197 | } | ||
| 198 | my $n = $gv->can('NAME') && $gv->NAME; | ||
| 199 | if ( $n ) { | ||
| 200 | $name .= $n; | ||
| 201 | if ( $n eq '__ANON__' ) { | ||
| 202 | $name .= ' defined at ' . $gv->FILE . ':' . $gv->LINE; | ||
| 203 | $name =~ s/$DB::options{ pwd }/~/; | ||
| 204 | } | ||
| 205 | }else{ | ||
| 206 | # warn 'NONAME' | ||
| 207 | } | ||
| 208 | |||
| 209 | # if( $DB::z ) { | ||
| 210 | # $DB::z = 1; | ||
| 211 | # warn "NAME: $name\n"; | ||
| 212 | # warn "Deparsed: ". deparse( $r ) ."\n" if ref $gv eq 'B::SPECIAL'; | ||
| 213 | |||
| 214 | # my $lvl; | ||
| 215 | # local $" = ' - '; | ||
| 216 | # while( my @frame = caller( ++$lvl ) ) { | ||
| 217 | # warn "@frame[0..3]\n"; | ||
| 218 | # } | ||
| 219 | # } | ||
| 220 | |||
| 221 | return $name; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | my $old; | ||
| 227 | ## Data display routines | ||
| 228 | # Returns correct indentaion for the current level of nesting. | ||
| 229 | sub indentation { return (shift//' ')x(shift//$DB::instance) } | ||
| 230 | |||
| 231 | |||
| 232 | sub say { | ||
| 233 | if( (!defined $old || $old != @DB::state) && DB::state( 'ddd' ) &16 ) { | ||
| 234 | unshift @_, DB::_c( "Level: i:$DB::instance c:" .scalar @DB::state, 91 ); | ||
| 235 | $old = @DB::state; | ||
| 236 | } | ||
| 237 | |||
| 238 | local $" = "\n" .indentation( undef ); | ||
| 239 | print $DB::OUT indentation( undef )."@_" =~ s/^(\h+)$//mgr; #/ | ||
| 240 | print $DB::OUT "\n"; | ||
| 241 | } | ||
| 242 | |||
| 243 | |||
| 244 | # Converts all undef values to a configured visible representation. | ||
| 245 | sub vis_undef { | ||
| 246 | my @res = map{ ref $_ ? ref $_ : $_ } map{ $_ // $DB::options{ undef } } @_; | ||
| 247 | return @res if wantarray; | ||
| 248 | # return '' unless @res; # Should we display empty string if there is no result? | ||
| 249 | return (pop @res) // $DB::options{ undef }; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub warn { | ||
| 253 | unshift @_, ''; | ||
| 254 | local $" = indentation( undef, $DB::instance ); | ||
| 255 | print $DB::ERR "@_\n"; | ||
| 256 | } | ||
| 257 | |||
| 258 | use Sub::Metadata qw/ mutate_sub_is_debuggable /; | ||
| 259 | mutate_sub_is_debuggable( \&_c, 0 ); | ||
| 260 | mutate_sub_is_debuggable( \&say, 0 ); | ||
| 261 | mutate_sub_is_debuggable( \&warn, 0 ); | ||
| 262 | mutate_sub_is_debuggable( \&vis_undef, 0 ); | ||
| 263 | mutate_sub_is_debuggable( \&dumper, 0 ); | ||
| 264 | mutate_sub_is_debuggable( \&indentation, 0 ); | ||
| 265 | mutate_sub_is_debuggable( \&DB::source, 0 ); | ||
| 266 | mutate_sub_is_debuggable( \&DB::file, 0 ); | ||
| 267 | mutate_sub_is_debuggable( \&DB::can_break, 0 ); | ||
| 268 | |||
| 269 | |||
| 270 | my %seen; | ||
| 271 | my %gexclude = ( | ||
| 272 | _result_source => 1, | ||
| 273 | _relationship_data => 1, | ||
| 274 | related_resultsets => 1, | ||
| 275 | config => 1, | ||
| 276 | ); | ||
| 277 | |||
| 278 | # TODO: document parameters. It is unclear what is going on here... | ||
| 279 | my %filters = ( | ||
| 280 | 'Mojolicious::Routes' => { children => 1, hidden => 1, reverse => 1, cache => 1, hiding => 1, }, | ||
| 281 | 'Mojolicious::Routes::Route' => { children => 1, parent => 1, 'Mojolicious::Routes::Route' => 1 }, | ||
| 282 | 'Mojolicious::Routes::Match' => { root => 1, }, | ||
| 283 | 'Mojolicious::Controller' => { app => 1, match => 1, tx => 1, }, | ||
| 284 | 'Mojo::Exception' => { frames => 1, }, | ||
| 285 | 'JSON::Validator::OpenAPI::Mojolicious' => { schema => 1, schemas => 1, }, | ||
| 286 | 'Mojolicious::Plugin::OpenAPI' => { validator => 1, route => 1, _default_response => 1, }, | ||
| 287 | 'DBIx::Class::ResultSource::Table' => 1, | ||
| 288 | 'Mojolicious::Validator' => 1, | ||
| 289 | 'Mojolicious::Static' => 1, | ||
| 290 | '@' => [qw/ DBIx::Class::ResultSet Mojolicious::Controller Mojolicious DateTime::TimeZone DateTime DbMapper::DataSet /], | ||
| 291 | 'DBIx::Class::ResultSet' => { _attrs => 1 }, | ||
| 292 | 'Mojolicious::Validator' => { checks => 1, filters => 1, }, | ||
| 293 | 'Mojolicious' => { types => 1, plugins => 1, renderer => 1 }, | ||
| 294 | 'DateTime' => sub{ my $dt= shift; "$dt " .$dt->{tz}{name} }, | ||
| 295 | 'SQL::Translator::Schema::Table' => sub{ shift->name }, | ||
| 296 | 'Mojo::Exception' => sub{ shift ."" }, | ||
| 297 | 'DBIx::Class::Storage::DBI::Cursor' => { args => 1, storage => 1, }, | ||
| 298 | 'Mojolicious::Renderer' => { handlers => 1, tempaltes => 1, }, | ||
| 299 | 'DateTime::TimeZone' => { rules => 1, spans => 1, last_observance => 1, }, | ||
| 300 | 'DBIx::Class::ResultSource::View' => { schema => 1, }, | ||
| 301 | # 'SQL::Translator::Schema::Table' => { schema => 1, }, | ||
| 302 | 'DbMapper::DataSet' => { mapper => 1 }, | ||
| 303 | ); | ||
| 304 | # { package X; | ||
| 305 | |||
| 306 | # We need a custom dumper to not retrigger debugger when calling dumpers at user's space. | ||
| 307 | use Scalar::Util qw/ reftype blessed /; | ||
| 308 | sub DB::dumper { | ||
| 309 | my( $data, $level, $path ) = @_; | ||
| 310 | %seen = () if !defined $level; | ||
| 311 | $level //= $DB::instance; | ||
| 312 | $path //= 'TOPIC'; | ||
| 313 | |||
| 314 | my %exclude = %gexclude; | ||
| 315 | |||
| 316 | my $result = ''; | ||
| 317 | my $type; | ||
| 318 | if( $DB::options{ DumpObjects } && (my $class = blessed( $data )) ) { | ||
| 319 | $result .= "$class "; | ||
| 320 | my $ignore = $filters{ $class }; # Check inherited classes later | ||
| 321 | $ignore //= $filters{ (grep{ $class->isa($_) } $filters{'@'}->@*)[0] }; | ||
| 322 | if( $ignore ) { | ||
| 323 | # Use provided sub to dump data | ||
| 324 | return $result .$ignore->( $data ) if ref $ignore eq 'CODE'; | ||
| 325 | |||
| 326 | # Ignore only deep objects | ||
| 327 | return $result .'IGNORED' if !ref $ignore && $level; | ||
| 328 | @exclude{ keys %$ignore } = values %$ignore if ref $ignore eq 'HASH'; | ||
| 329 | } | ||
| 330 | |||
| 331 | $type = reftype( $data ); | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $type = ref $data; | ||
| 335 | } | ||
| 336 | |||
| 337 | |||
| 338 | if( $type ) { | ||
| 339 | exists $seen{ $data } ? return $seen{ $data } : ($seen{ $data } = $path); | ||
| 340 | } | ||
| 341 | |||
| 342 | if( $type eq 'HASH' ) { | ||
| 343 | $result .= '{'; | ||
| 344 | $result .= join '', map{ | ||
| 345 | my $value = $exclude{$_} ? "IGNORED" : | ||
| 346 | $exclude{ ref $data->{$_} } ? ref( $data->{$_} ) ." IGNORED" : | ||
| 347 | DB::dumper( $data->{$_}, $level +1, "$path\{$_\}" ); | ||
| 348 | |||
| 349 | "\n" .' 'x($level+1) ."$_ => $value," | ||
| 350 | } sort keys %$data; | ||
| 351 | $result .= ((keys %$data)?"\n" .' 'x($level):'') ."}"; | ||
| 352 | |||
| 353 | # TODO: Implement 'expand_tied' flag | ||
| 354 | my $tied = tied %$data; | ||
| 355 | $result .= ' (tied to ' .(ref $tied) .')' if $tied; | ||
| 356 | } | ||
| 357 | elsif( $type eq 'ARRAY' ) { | ||
| 358 | $result .= '['; | ||
| 359 | $result .= join '', map{ | ||
| 360 | my $value = $exclude{ ref $data->[$_] } ? ref( $data->[$_] ) ." IGNORED" : | ||
| 361 | DB::dumper( $data->[$_], $level +1, "$path\[$_\]" ); | ||
| 362 | "\n" .' 'x($level+1) ."$value," | ||
| 363 | } keys @$data; | ||
| 364 | $result .= (@$data?"\n" .' 'x($level):'') ."]"; | ||
| 365 | |||
| 366 | my $tied = tied @$data; | ||
| 367 | $result .= ' (tied to ' .(ref $tied) .')' if $tied; | ||
| 368 | } | ||
| 369 | elsif( $type eq 'CODE' ) { | ||
| 370 | $result .= '&' .DB::sub_name( $data ); | ||
| 371 | # $result .= " #$data"; | ||
| 372 | } | ||
| 373 | elsif( $type eq 'SCALAR' ) { | ||
| 374 | $result .= '\\' .DB::dumper( $$data, $level +1, "$path SCALAR" ); | ||
| 375 | |||
| 376 | my $tied = tied $$data; | ||
| 377 | $result .= ' (tied to ' .(ref $tied) .')' if $tied; | ||
| 378 | } | ||
| 379 | elsif( $type eq 'REF' ) { | ||
| 380 | $result .= '\\' .DB::dumper( $$data, $level +1, "$PATH REF" ); | ||
| 381 | |||
| 382 | my $tied = tied $$data; | ||
| 383 | $result .= ' (tied to ' .(ref $tied) .')' if $tied; | ||
| 384 | } | ||
| 385 | elsif( $type eq 'REGEXP' ) { | ||
| 386 | $result .= "$data"; | ||
| 387 | } | ||
| 388 | else { | ||
| 389 | # TODO: use DB::vis_undef | ||
| 390 | $result .= defined $data ? ($type ? $type : $data) : 'undef'; | ||
| 391 | } | ||
| 392 | |||
| 393 | return $result; | ||
| 394 | } #} | ||
| 395 | |||
| 396 | |||
| 397 | |||
| 398 | # Returns information about a caller sub, file, line and debugger type | ||
| 399 | sub who { | ||
| 400 | my $lvl = @_ ? 0 : 1; | ||
| 401 | my( $p, $f, $l, $callee ) = (caller($lvl))[0..3]; | ||
| 402 | my( $caller ) = (caller($lvl+1))[3]; | ||
| 403 | # $caller =~ s/^$p\:://; # TODO? Implement flag to hide package name | ||
| 404 | |||
| 405 | $callee = shift if @_; | ||
| 406 | # $callee =~ s/^$p\:://; | ||
| 407 | |||
| 408 | my $pwd = $DB::options{ pwd }; | ||
| 409 | $f =~ s/$pwd/~/; | ||
| 410 | $caller =~ s/$pwd/~/; | ||
| 411 | |||
| 412 | if( $caller =~ m/$f/ && $caller =~ m/$l/ ) { | ||
| 413 | $f = $l = ''; | ||
| 414 | } | ||
| 415 | |||
| 416 | |||
| 417 | # Highlight <-- by different color if we are inside debugger? | ||
| 418 | my $arrow = $lvl ? '<--' : | ||
| 419 | $DB::state[$DB::instance]{ dbcall } ? DB::_c("<--", '2;93' ) : DB::_c( "<--", 32 ) | ||
| 420 | ; | ||
| 421 | return DB::_c($callee,'2;33') ." $arrow $caller ($f:$l)"; | ||
| 422 | } | ||
| 423 | |||
| 424 | |||
| 425 | |||
| 426 | ## Event utilities | ||
| 427 | our $events; | ||
| 428 | sub on { push @{ $events->{ $_[0] } }, $_[1] and return $_[1] } | ||
| 429 | sub RT_on { push @{ DB::state( 'events' )->{ $_[0] } }, $_[1] and return $_[1] } | ||
| 430 | |||
| 431 | |||
| 432 | |||
| 433 | sub once { | ||
| 434 | my( $name, $cb ) = @_; | ||
| 435 | |||
| 436 | my $wrapper; $wrapper = sub { | ||
| 437 | DB::unsubscribe( $name => $wrapper ); | ||
| 438 | # TODO? Leak? Undefine $wrapper here. | ||
| 439 | $cb->( @_ ); | ||
| 440 | }; | ||
| 441 | |||
| 442 | return DB::on( $name => $wrapper ); | ||
| 443 | } | ||
| 444 | |||
| 445 | |||
| 446 | |||
| 447 | sub subscribers { $events->{ shift() } //= [] } | ||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | sub unsubscribe { | ||
| 452 | my( $name, $cb ) = @_; | ||
| 453 | |||
| 454 | $events->{ $name } = [ grep { $cb ne $_ } @{ $events->{$name} } ]; | ||
| 455 | delete $events->{ $name } unless @{ $events->{$name} }; | ||
| 456 | |||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | |||
| 461 | |||
| 462 | sub RT_unsubscribe { | ||
| 463 | my( $name, $cb ) = @_; | ||
| 464 | |||
| 465 | my $events = DB::state( 'events' ); | ||
| 466 | $events->{ $name } = [ grep { $cb ne $_ } @{ $events->{$name} } ]; | ||
| 467 | delete $events->{ $name } unless @{ $events->{$name} }; | ||
| 468 | |||
| 469 | return; | ||
| 470 | } | ||
| 471 | |||
| 472 | |||
| 473 | |||
| 474 | # There two types of events: global and per debugger instance. | ||
| 475 | sub events { | ||
| 476 | my( $name ) = @_; | ||
| 477 | |||
| 478 | my $db_events = DB::state( 'events' ); | ||
| 479 | return unless $events->{ $name } || $db_events->{ $name }; | ||
| 480 | |||
| 481 | my @list; | ||
| 482 | $events->{ $name } and push @list, @{ $events->{ $name } }; | ||
| 483 | $db_events->{ $name } and push @list, @{ $db_events->{ $name } }; | ||
| 484 | |||
| 485 | return @list; | ||
| 486 | } | ||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | # Handlers are kind of subroutines which are called via 'process' interface. | ||
| 491 | sub emit { | ||
| 492 | my $name = shift; | ||
| 493 | my $ddd = DB::state( 'ddd' ) &32; | ||
| 494 | |||
| 495 | my @list = DB::events( $name ) or do{ | ||
| 496 | $ddd && DB::say "No subscribers for $name"; | ||
| 497 | return; | ||
| 498 | }; | ||
| 499 | |||
| 500 | if( $ddd ) { | ||
| 501 | DB::say "Event subscriber(s) for $name:"; | ||
| 502 | DB::say " ". DB::sub_name( $_ ) for @list; | ||
| 503 | DB::say; | ||
| 504 | } | ||
| 505 | |||
| 506 | |||
| 507 | if( defined wantarray ) { | ||
| 508 | my @res; | ||
| 509 | # TODO: $_ is global. In this form there could be some issues with stored | ||
| 510 | # context from @DB::args. $_ is linked to en array item and could modified | ||
| 511 | # inside subroutine. Thus stored value will be changed and therefor restored | ||
| 512 | # to a wrong value. | ||
| 513 | push @res, process( $_, @_ ) for @list; | ||
| 514 | |||
| 515 | return @res; | ||
| 516 | } | ||
| 517 | |||
| 518 | |||
| 519 | process( $_, @_ ) for @list; | ||
| 520 | return; | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | |||
| 525 | # Receives CODE and arguments. Runs that code with specified arguments and returns | ||
| 526 | # a result as a scalar value. | ||
| 527 | # I. Result could be another CODE, which will be executed with the original arguments. | ||
| 528 | # ^^^ This is useful, when CODE understands it can not process and wants to fallback | ||
| 529 | # to something more reasonable. | ||
| 530 | # II. Result could be ARRAY. In this case the first item is CODE and the rest items will | ||
| 531 | # be passed to it as arguments after some processing depending on its type. If it is: | ||
| 532 | # a) CODE/ARRAY -- call 'process' recursively. | ||
| 533 | # b) scalar -- evaluate its value at user's context. | ||
| 534 | |||
| 535 | # 'process' is called from 'emit'. No other places which uses this sub at the moment. | ||
| 536 | sub process { | ||
| 537 | my $ddd = DB::state( 'ddd' ) &32; | ||
| 538 | |||
| 539 | my $handler = shift; | ||
| 540 | my( $code, @args, @result ); | ||
| 541 | while( my $htype = ref $handler ) { | ||
| 542 | if( $htype eq 'ARRAY' ) { | ||
| 543 | # We will run $code with list of $expr which are evaluated in user context first | ||
| 544 | $code = shift @$handler; | ||
| 545 | @args = (); | ||
| 546 | |||
| 547 | DB::say "Got list of expressions to evaluate in usercontext:", "@$handler" if $ddd; | ||
| 548 | for my $expr ( @$handler ) { | ||
| 549 | # $expr should be simple string. If it is not it is special | ||
| 550 | push @args, ref $expr ? process( $expr ) : [ DB::eval( $expr ) ]; | ||
| 551 | $args[-1] = bless { error => $@ }, 'DB::Error' if $@; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | elsif( $htype eq 'CODE' ) { | ||
| 555 | $code = $handler; | ||
| 556 | @args = @_; | ||
| 557 | } | ||
| 558 | else { | ||
| 559 | die "Handler type should be ARRAY or CODE"; | ||
| 560 | } | ||
| 561 | |||
| 562 | if( DB::state( 'dd' ) ) { | ||
| 563 | DB::say 'Force debugging for next call: $single = 1' if $ddd; | ||
| 564 | $DB::single = 1; | ||
| 565 | } | ||
| 566 | else { | ||
| 567 | DB::say 'Disable debugging for next call: $single = 0' if $ddd; | ||
| 568 | $DB::single = 0; | ||
| 569 | } | ||
| 570 | |||
| 571 | if( $ddd ) { | ||
| 572 | my $args = @args ? DB::dumper( \@args ) : ''; | ||
| 573 | DB::say "Run callback: " .DB::_c(sub_name( $code ),'2;33') ."( $args )"; | ||
| 574 | } | ||
| 575 | |||
| 576 | |||
| 577 | @result = $code->( @args ); | ||
| 578 | $handler = $result[0]; | ||
| 579 | } | ||
| 580 | |||
| 581 | DB::say "Processing is finished: " .sub_name( $code ) if $ddd; | ||
| 582 | return $handler; # @handler | ||
| 583 | } | ||
| 584 | |||
| 585 | |||
| 586 | |||
| 587 | # Returns TRUE if $filename was compiled/evaled | ||
| 588 | # The file is evaled if it looks like (eval 34) | ||
| 589 | # But this may be changed by #file:line. See ??? for info | ||
| 590 | sub DB::file { | ||
| 591 | my $filename = shift // DB::state( 'file' ); | ||
| 592 | $filename =~ s/^_<//; | ||
| 593 | $filename =~ s/^~/$DB::options{ pwd }/; | ||
| 594 | |||
| 595 | # NOTICE differences: | ||
| 596 | # https://stackoverflow.com/q/56273829/4632019 | ||
| 597 | # https://stackoverflow.com/q/56270222/4632019 | ||
| 598 | # https://stackoverflow.com/q/56260910/4632019 | ||
| 599 | # https://stackoverflow.com/q/56273425/4632019 | ||
| 600 | no strict 'refs'; | ||
| 601 | unless( exists ${ 'main::' }{ "_<$filename" } ) { | ||
| 602 | DB::warn "File '$filename' is not compiled yet"; | ||
| 603 | |||
| 604 | return; | ||
| 605 | } | ||
| 606 | |||
| 607 | return ${ $::{"_<$filename"} } | ||
| 608 | } | ||
| 609 | |||
| 610 | |||
| 611 | |||
| 612 | # Returns source for $filename | ||
| 613 | # r 1 test of cmd_r; s;s;DB::state( dd => 1 );r 1;r;r;r;r;r;r;r;r | ||
| 614 | # And we stop in DB::source, but we do not, because it has is_debaggable == 0 | ||
| 615 | sub DB::source { | ||
| 616 | my $filename = DB::file( shift ); | ||
| 617 | return [] unless defined $filename; | ||
| 618 | |||
| 619 | no strict 'refs'; | ||
| 620 | return \@{ $::{"_<$filename"} }; | ||
| 621 | } | ||
| 622 | |||
| 623 | |||
| 624 | |||
| 625 | # Returns TRUE if we can set trap for $file:line | ||
| 626 | sub DB::can_break { | ||
| 627 | my( $file, $line ) = @_; | ||
| 628 | |||
| 629 | ($file, $line) = split ':', $file | ||
| 630 | unless defined $line; | ||
| 631 | |||
| 632 | $file = DB::file( $file ); | ||
| 633 | return unless defined $file; | ||
| 634 | |||
| 635 | no strict 'refs'; | ||
| 636 | return $line >= 0 && $line <= $#{ $::{"_<$file"} } | ||
| 637 | && ${ $::{"_<$file"} }[ $line ] != 0; | ||
| 638 | |||
| 639 | # http://perldoc.perl.org/perldebguts.html#Debugger-Internals | ||
| 640 | # Values in this array are magical in numeric context: | ||
| 641 | # they compare equal to zero only if the line is not breakable. | ||
| 642 | } | ||
| 643 | |||
| 644 | |||
| 645 | |||
| 646 | # Returns list of compiled files/evaled strings | ||
| 647 | # The $filename for evaled strings looks like (eval 34) | ||
| 648 | sub DB::sources { | ||
| 649 | no strict 'refs'; | ||
| 650 | return grep{ m/^_</ } keys %{ 'main::' }; | ||
| 651 | } | ||
| 652 | |||
| 653 | |||
| 654 | |||
| 655 | # Returns hashref of traps for $filename keyed by $line | ||
| 656 | sub DB::traps { | ||
| 657 | my $filename = DB::file( shift ); | ||
| 658 | return {} unless defined $filename; | ||
| 659 | |||
| 660 | # Keep list of $filenames we perhaps manipulate traps | ||
| 661 | $DB::_tfiles->{ $filename } = 1; | ||
| 662 | |||
| 663 | |||
| 664 | no strict 'refs'; | ||
| 665 | *DB::dbline = $main::{ "_<$filename" }; # WORKRAOUND RT#119799 (see commit) | ||
| 666 | |||
| 667 | return \%{ $::{"_<$filename"} }; | ||
| 668 | } | ||
| 669 | |||
| 670 | |||
| 671 | |||
| 672 | # Returns the location where $subname is defined in the form: | ||
| 673 | # filename:startline-endline | ||
| 674 | sub DB::location { | ||
| 675 | my $sub = shift; | ||
| 676 | |||
| 677 | return unless $sub; | ||
| 678 | $sub = DB::sub_name( $sub ) if ref $sub; # The $sub maybe a CODEREF | ||
| 679 | die "Can not find subroutine location for $sub" if ref $sub; | ||
| 680 | |||
| 681 | # The subs from DB::* are not placed here. Why??? | ||
| 682 | # A? Maybe they are placed after module loaded? | ||
| 683 | return $DB::sub{ $sub }; | ||
| 684 | } | ||
| 685 | |||
| 686 | |||
| 687 | |||
| 688 | # Returns list of all defined not ANON subs. | ||
| 689 | # We may limit the list by supplying regex | ||
| 690 | sub DB::subs { | ||
| 691 | return keys %DB::sub unless @_; | ||
| 692 | |||
| 693 | my $re = shift; | ||
| 694 | return grep { /$re/ } keys %DB::sub; | ||
| 695 | } | ||
| 696 | |||
| 697 | |||
| 698 | |||
| 699 | package DB::Utils; | ||
| 700 | |||
| 701 | use strict; | ||
| 702 | use warnings; | ||
| 703 | |||
| 704 | |||
| 705 | |||
| 706 | sub import { | ||
| 707 | my $pkg = caller; | ||
| 708 | my( $class, @events ) = @_; | ||
| 709 | |||
| 710 | no strict 'refs'; | ||
| 711 | @events = map{ /^on_(.*)$/?$1:() } keys %{ "main::${pkg}::" } | ||
| 712 | unless @events; | ||
| 713 | |||
| 714 | # Subscribe each event handler to event | ||
| 715 | for my $event ( @events ) { | ||
| 716 | my $sub = ${ "main::${pkg}::" }{ "on_$event" }; #IT: check that glob has sub ref | ||
| 717 | DB::on( $event => \&$sub ); | ||
| 718 | } | ||
| 719 | } | ||
| 720 | |||
| 721 | |||
| 722 | 1; |