considered to be a breakpoint; C<perl5db.pl> uses C<$break_condition\0$action>.
Values are magical in numeric context: 1 if the line is breakable, 0 if not.
-The scalar C<${"_<$filename"}> simply contains the string C<<< _<$filename> >>>.
+The scalar C<${"_<$filename"}> simply contains the string C<$filename>.
This is also the case for evaluated strings that contain subroutines, or
which are currently being executed. The $filename for C<eval>ed strings looks
like C<(eval 34).
use vars qw(
@args
%break_on_load
- @cmdfhs
$CommandSet
$CreateTTY
$DBGR
%dbline
$dieLevel
$filename
- $hist
$histfile
$histsize
$IN
$ini_warn
$maxtrace
$od
- $onetimedumpDepth
@options
$osingle
$otrace
$rl
@saved
$signalLevel
- $start
$sub
$term
$usercontext
$warnLevel
- $window
);
our (
+ @cmdfhs,
$evalarg,
$frame,
+ $hist,
$ImmediateStop,
$line,
$onetimeDump,
+ $onetimedumpDepth,
%option,
$OUT,
$packname,
$signal,
$single,
+ $start,
%sub,
$subname,
$trace,
+ $window,
);
# Used to save @ARGV and extract any debugger-related flags.
lock($DBGR);
print "Threads support enabled\n";
} else {
- *lock = sub(*) {};
- *share = sub(*) {};
+ *share = sub(\[$@%]) {};
}
}
}
# without threads, $filename is not defined until DB::DB is called
-foreach my $k (keys (%INC)) {
- share(\$main::{'_<'.$filename}) if defined $filename;
-};
+share($main::{'_<'.$filename}) if defined $filename;
# Command-line + PERLLIB:
# Save the contents of @INC before they are modified elsewhere.
=cut
-use vars qw(@hist @truehist %postponed_file @typeahead);
+use vars qw(%postponed_file @typeahead);
+
+our (@hist, @truehist);
sub _restore_shared_globals_after_restart
{
=cut
+# $cmd cannot be an our() variable unfortunately (possible perl bug?).
+
use vars qw(
$action
- %alias
$cmd
- $fall_off_end
$file
$filename_ini
$finished
%had_breakpoints
- $incr
- $laststep
$level
$max
$package
- $rc
- $sh
$try
- $end
);
our (
+ %alias,
$doret,
+ $end,
+ $fall_off_end,
+ $incr,
+ $laststep,
+ $rc,
+ $sh,
$stack_depth,
@stack,
@to_watch,
# see if we should stop. If so, remove the one-time sigil.
elsif ($stop) {
$evalarg = "\$DB::signal |= 1 if do {$stop}";
- &eval;
+ DB::eval();
# If the breakpoint is temporary, then delete its enabled status.
if ($dbline{$line} =~ s/;9($|\0)/$1/) {
_cancel_breakpoint_temp_enabled_status($filename, $line);
return $1;
}
+sub _DB__handle_f_command {
+ if (($file) = $cmd =~ /\Af\b\s*(.*)/) {
+ $file =~ s/\s+$//;
+
+ # help for no arguments (old-style was return from sub).
+ if ( !$file ) {
+ print $OUT
+ "The old f command is now the r command.\n"; # hint
+ print $OUT "The new f command switches filenames.\n";
+ next CMD;
+ } ## end if (!$file)
+
+ # if not in magic file list, try a close match.
+ if ( !defined $main::{ '_<' . $file } ) {
+ if ( ($try) = grep( m#^_<.*$file#, keys %main:: ) ) {
+ {
+ $try = substr( $try, 2 );
+ print $OUT "Choosing $try matching '$file':\n";
+ $file = $try;
+ }
+ } ## end if (($try) = grep(m#^_<.*$file#...
+ } ## end if (!defined $main::{ ...
+
+ # If not successfully switched now, we failed.
+ if ( !defined $main::{ '_<' . $file } ) {
+ print $OUT "No file matching '$file' is loaded.\n";
+ next CMD;
+ }
+
+ # We switched, so switch the debugger internals around.
+ elsif ( $file ne $filename ) {
+ *dbline = $main::{ '_<' . $file };
+ $max = $#dbline;
+ $filename = $file;
+ $start = 1;
+ $cmd = "l";
+ } ## end elsif ($file ne $filename)
+
+ # We didn't switch; say we didn't.
+ else {
+ print $OUT "Already in $file.\n";
+ next CMD;
+ }
+ }
+
+ return;
+}
+
+sub _DB__handle_dot_command {
+ my ($obj) = @_;
+
+ # . command.
+ if ($cmd eq '.') {
+ $incr = -1; # stay at current line
+
+ # Reset everything to the old location.
+ $start = $line;
+ $filename = $filename_ini;
+ *dbline = $main::{ '_<' . $filename };
+ $max = $#dbline;
+
+ # Now where are we?
+ print_lineinfo($obj->position());
+ next CMD;
+ }
+
+ return;
+}
+
+sub _DB__handle_y_command {
+ my ($obj) = @_;
+
+ if (my ($match_level, $match_vars)
+ = $cmd =~ /^y(?:\s+(\d*)\s*(.*))?$/) {
+
+ # See if we've got the necessary support.
+ if (!eval { require PadWalker; PadWalker->VERSION(0.08) }) {
+ my $Err = $@;
+ DB::warn(
+ $Err =~ /locate/
+ ? "PadWalker module not found - please install\n"
+ : $Err
+ );
+ next CMD;
+ }
+
+ # Load up dumpvar if we don't have it. If we can, that is.
+ do 'dumpvar.pl' || die $@ unless defined &main::dumpvar;
+ defined &main::dumpvar
+ or print $OUT "dumpvar.pl not available.\n"
+ and next CMD;
+
+ # Got all the modules we need. Find them and print them.
+ my @vars = split( ' ', $match_vars || '' );
+
+ # Find the pad.
+ my $h = eval { PadWalker::peek_my( ( $match_level || 0 ) + 1 ) };
+
+ # Oops. Can't find it.
+ if (my $Err = $@) {
+ $Err =~ s/ at .*//;
+ DB::warn($Err);
+ next CMD;
+ }
+
+ # Show the desired vars with dumplex().
+ my $savout = select($OUT);
+
+ # Have dumplex dump the lexicals.
+ foreach my $key (sort keys %$h) {
+ dumpvar::dumplex( $key, $h->{$key},
+ defined $option{dumpDepth} ? $option{dumpDepth} : -1,
+ @vars );
+ }
+ select($savout);
+ next CMD;
+ }
+}
+
+sub _DB__handle_c_command {
+ my ($obj) = @_;
+
+ if (my ($new_i) = $cmd =~ m#\Ac\b\s*([\w:]*)\s*\z#) {
+
+ $obj->i_cmd($new_i);
+
+ # Hey, show's over. The debugged program finished
+ # executing already.
+ next CMD if _DB__is_finished();
+
+ # Capture the place to put a one-time break.
+ $subname = $obj->i_cmd;
+
+ # Probably not needed, since we finish an interactive
+ # sub-session anyway...
+ # local $filename = $filename;
+ # local *dbline = *dbline; # XXX Would this work?!
+ #
+ # The above question wonders if localizing the alias
+ # to the magic array works or not. Since it's commented
+ # out, we'll just leave that to speculation for now.
+
+ # If the "subname" isn't all digits, we'll assume it
+ # is a subroutine name, and try to find it.
+ if ( $subname =~ /\D/ ) { # subroutine name
+ # Qualify it to the current package unless it's
+ # already qualified.
+ $subname = $package . "::" . $subname
+ unless $subname =~ /::/;
+
+ # find_sub will return "file:line_number" corresponding
+ # to where the subroutine is defined; we call find_sub,
+ # break up the return value, and assign it in one
+ # operation.
+ ( $file, $new_i ) = ( find_sub($subname) =~ /^(.*):(.*)$/ );
+
+ # Force the line number to be numeric.
+ $obj->i_cmd($new_i + 0);
+
+ # If we got a line number, we found the sub.
+ if ($obj->i_cmd) {
+
+ # Switch all the debugger's internals around so
+ # we're actually working with that file.
+ $filename = $file;
+ *dbline = $main::{ '_<' . $filename };
+
+ # Mark that there's a breakpoint in this file.
+ $had_breakpoints{$filename} |= 1;
+
+ # Scan forward to the first executable line
+ # after the 'sub whatever' line.
+ $max = $#dbline;
+ my $ii = $obj->i_cmd;
+ ++$ii while $dbline[$ii] == 0 && $ii < $max;
+ $obj->i_cmd($ii);
+ } ## end if ($i)
+
+ # We didn't find a sub by that name.
+ else {
+ print $OUT "Subroutine $subname not found.\n";
+ next CMD;
+ }
+ } ## end if ($subname =~ /\D/)
+
+ # At this point, either the subname was all digits (an
+ # absolute line-break request) or we've scanned through
+ # the code following the definition of the sub, looking
+ # for an executable, which we may or may not have found.
+ #
+ # If $i (which we set $subname from) is non-zero, we
+ # got a request to break at some line somewhere. On
+ # one hand, if there wasn't any real subroutine name
+ # involved, this will be a request to break in the current
+ # file at the specified line, so we have to check to make
+ # sure that the line specified really is breakable.
+ #
+ # On the other hand, if there was a subname supplied, the
+ # preceding block has moved us to the proper file and
+ # location within that file, and then scanned forward
+ # looking for the next executable line. We have to make
+ # sure that one was found.
+ #
+ # On the gripping hand, we can't do anything unless the
+ # current value of $i points to a valid breakable line.
+ # Check that.
+ if ($obj->i_cmd) {
+
+ # Breakable?
+ if ( $dbline[$obj->i_cmd] == 0 ) {
+ print $OUT "Line " . $obj->i_cmd . " not breakable.\n";
+ next CMD;
+ }
+
+ # Yes. Set up the one-time-break sigil.
+ $dbline{$obj->i_cmd} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
+ _enable_breakpoint_temp_enabled_status($filename, $obj->i_cmd);
+ } ## end if ($i)
+
+ # Turn off stack tracing from here up.
+ for my $i (0 .. $stack_depth) {
+ $stack[ $i ] &= ~1;
+ }
+ last CMD;
+ }
+
+ return;
+}
+
+sub _DB__handle_forward_slash_command {
+ my ($obj) = @_;
+
+ # The pattern as a string.
+ use vars qw($inpat);
+
+ if (($inpat) = $cmd =~ m#\A/(.*)\z#) {
+
+ # Remove the final slash.
+ $inpat =~ s:([^\\])/$:$1:;
+
+ # If the pattern isn't null ...
+ if ( $inpat ne "" ) {
+
+ # Turn of warn and die procesing for a bit.
+ local $SIG{__DIE__};
+ local $SIG{__WARN__};
+
+ # Create the pattern.
+ eval 'no strict q/vars/; $inpat =~ m' . "\a$inpat\a";
+ if ( $@ ne "" ) {
+
+ # Oops. Bad pattern. No biscuit.
+ # Print the eval error and go back for more
+ # commands.
+ print $OUT "$@";
+ next CMD;
+ }
+ $obj->pat($inpat);
+ } ## end if ($inpat ne "")
+
+ # Set up to stop on wrap-around.
+ $end = $start;
+
+ # Don't move off the current line.
+ $incr = -1;
+
+ my $pat = $obj->pat;
+
+ # Done in eval so nothing breaks if the pattern
+ # does something weird.
+ eval
+ {
+ no strict q/vars/;
+ for (;;) {
+ # Move ahead one line.
+ ++$start;
+
+ # Wrap if we pass the last line.
+ $start = 1 if ($start > $max);
+
+ # Stop if we have gotten back to this line again,
+ last if ($start == $end);
+
+ # A hit! (Note, though, that we are doing
+ # case-insensitive matching. Maybe a qr//
+ # expression would be better, so the user could
+ # do case-sensitive matching if desired.
+ if ($dbline[$start] =~ m/$pat/i) {
+ if ($slave_editor) {
+ # Handle proper escaping in the slave.
+ print $OUT "\032\032$filename:$start:0\n";
+ }
+ else {
+ # Just print the line normally.
+ print $OUT "$start:\t",$dbline[$start],"\n";
+ }
+ # And quit since we found something.
+ last;
+ }
+ }
+ };
+
+ if ($@) {
+ warn $@;
+ }
+
+ # If we wrapped, there never was a match.
+ if ( $start == $end ) {
+ print {$OUT} "/$pat/: not found\n";
+ }
+ next CMD;
+ }
+
+ return;
+}
+
+sub _DB__handle_question_mark_command {
+ my ($obj) = @_;
+
+ # ? - backward pattern search.
+ if (my ($inpat) = $cmd =~ m#\A\?(.*)\z#) {
+
+ # Get the pattern, remove trailing question mark.
+ $inpat =~ s:([^\\])\?$:$1:;
+
+ # If we've got one ...
+ if ( $inpat ne "" ) {
+
+ # Turn off die & warn handlers.
+ local $SIG{__DIE__};
+ local $SIG{__WARN__};
+ eval '$inpat =~ m' . "\a$inpat\a";
+
+ if ( $@ ne "" ) {
+
+ # Ouch. Not good. Print the error.
+ print $OUT $@;
+ next CMD;
+ }
+ $obj->pat($inpat);
+ } ## end if ($inpat ne "")
+
+ # Where we are now is where to stop after wraparound.
+ $end = $start;
+
+ # Don't move away from this line.
+ $incr = -1;
+
+ my $pat = $obj->pat;
+ # Search inside the eval to prevent pattern badness
+ # from killing us.
+ eval {
+ no strict q/vars/;
+ for (;;) {
+ # Back up a line.
+ --$start;
+
+ # Wrap if we pass the first line.
+
+ $start = $max if ($start <= 0);
+
+ # Quit if we get back where we started,
+ last if ($start == $end);
+
+ # Match?
+ if ($dbline[$start] =~ m/$pat/i) {
+ if ($slave_editor) {
+ # Yep, follow slave editor requirements.
+ print $OUT "\032\032$filename:$start:0\n";
+ }
+ else {
+ # Yep, just print normally.
+ print $OUT "$start:\t",$dbline[$start],"\n";
+ }
+
+ # Found, so done.
+ last;
+ }
+ }
+ };
+
+ # Say we failed if the loop never found anything,
+ if ( $start == $end ) {
+ print {$OUT} "?$pat?: not found\n";
+ }
+ next CMD;
+ }
+
+ return;
+}
+
+sub _DB__handle_restart_and_rerun_commands {
+ my ($obj) = @_;
+
+ # R - restart execution.
+ # rerun - controlled restart execution.
+ if (my ($cmd_cmd, $cmd_params) =
+ $cmd =~ /\A((?:R)|(?:rerun\s*(.*)))\z/) {
+ my @args = ($cmd_cmd eq 'R' ? restart() : rerun($cmd_params));
+
+ # Close all non-system fds for a clean restart. A more
+ # correct method would be to close all fds that were not
+ # open when the process started, but this seems to be
+ # hard. See "debugger 'R'estart and open database
+ # connections" on p5p.
+
+ my $max_fd = 1024; # default if POSIX can't be loaded
+ if (eval { require POSIX }) {
+ eval { $max_fd = POSIX::sysconf(POSIX::_SC_OPEN_MAX()) };
+ }
+
+ if (defined $max_fd) {
+ foreach ($^F+1 .. $max_fd-1) {
+ next unless open FD_TO_CLOSE, "<&=$_";
+ close(FD_TO_CLOSE);
+ }
+ }
+
+ # And run Perl again. We use exec() to keep the
+ # PID stable (and that way $ini_pids is still valid).
+ exec(@args) or print {$OUT} "exec failed: $!\n";
+
+ last CMD;
+ }
+
+ return;
+}
+
+sub _DB__handle_run_command_in_pager_command {
+ my ($obj) = @_;
+
+ if ($cmd =~ m#\A\|\|?\s*[^|]#) {
+ if ( $pager =~ /^\|/ ) {
+
+ # Default pager is into a pipe. Redirect I/O.
+ open( SAVEOUT, ">&STDOUT" )
+ || DB::warn("Can't save STDOUT");
+ open( STDOUT, ">&OUT" )
+ || DB::warn("Can't redirect STDOUT");
+ } ## end if ($pager =~ /^\|/)
+ else {
+
+ # Not into a pipe. STDOUT is safe.
+ open( SAVEOUT, ">&OUT" ) || &warn("Can't save DB::OUT");
+ }
+
+ # Fix up environment to record we have less if so.
+ fix_less();
+
+ unless ( $obj->piped(scalar ( open( OUT, $pager ) ) ) ) {
+
+ # Couldn't open pipe to pager.
+ DB::warn("Can't pipe output to '$pager'");
+ if ( $pager =~ /^\|/ ) {
+
+ # Redirect I/O back again.
+ open( OUT, ">&STDOUT" ) # XXX: lost message
+ || DB::warn("Can't restore DB::OUT");
+ open( STDOUT, ">&SAVEOUT" )
+ || DB::warn("Can't restore STDOUT");
+ close(SAVEOUT);
+ } ## end if ($pager =~ /^\|/)
+ else {
+
+ # Redirect I/O. STDOUT already safe.
+ open( OUT, ">&STDOUT" ) # XXX: lost message
+ || DB::warn("Can't restore DB::OUT");
+ }
+ next CMD;
+ } ## end unless ($piped = open(OUT,...
+
+ # Set up broken-pipe handler if necessary.
+ $SIG{PIPE} = \&DB::catch
+ if $pager =~ /^\|/
+ && ( "" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE} );
+
+ OUT->autoflush(1);
+ # Save current filehandle, and put it back.
+ $obj->selected(scalar( select(OUT) ));
+ # Don't put it back if pager was a pipe.
+ if ($cmd !~ /\A\|\|/)
+ {
+ select($obj->selected());
+ $obj->selected("");
+ }
+
+ # Trim off the pipe symbols and run the command now.
+ $cmd =~ s#\A\|+\s*##;
+ redo PIPE;
+ }
+
+ return;
+}
+
+sub _DB__handle_m_command {
+ my ($obj) = @_;
+
+ if ($cmd =~ s#\Am\s+([\w:]+)\s*\z# #) {
+ methods($1);
+ next CMD;
+ }
+
+ # m expr - set up DB::eval to do the work
+ if ($cmd =~ s#\Am\b# #) { # Rest gets done by DB::eval()
+ $onetimeDump = 'methods'; # method output gets used there
+ }
+
+ return;
+}
+
+sub _DB__at_end_of_every_command {
+ my ($obj) = @_;
+
+ # At the end of every command:
+ if ($obj->piped) {
+
+ # Unhook the pipe mechanism now.
+ if ( $pager =~ /^\|/ ) {
+
+ # No error from the child.
+ $? = 0;
+
+ # we cannot warn here: the handle is missing --tchrist
+ close(OUT) || print SAVEOUT "\nCan't close DB::OUT\n";
+
+ # most of the $? crud was coping with broken cshisms
+ # $? is explicitly set to 0, so this never runs.
+ if ($?) {
+ print SAVEOUT "Pager '$pager' failed: ";
+ if ( $? == -1 ) {
+ print SAVEOUT "shell returned -1\n";
+ }
+ elsif ( $? >> 8 ) {
+ print SAVEOUT ( $? & 127 )
+ ? " (SIG#" . ( $? & 127 ) . ")"
+ : "", ( $? & 128 ) ? " -- core dumped" : "", "\n";
+ }
+ else {
+ print SAVEOUT "status ", ( $? >> 8 ), "\n";
+ }
+ } ## end if ($?)
+
+ # Reopen filehandle for our output (if we can) and
+ # restore STDOUT (if we can).
+ open( OUT, ">&STDOUT" ) || &warn("Can't restore DB::OUT");
+ open( STDOUT, ">&SAVEOUT" )
+ || &warn("Can't restore STDOUT");
+
+ # Turn off pipe exception handler if necessary.
+ $SIG{PIPE} = "DEFAULT" if $SIG{PIPE} eq \&DB::catch;
+
+ # Will stop ignoring SIGPIPE if done like nohup(1)
+ # does SIGINT but Perl doesn't give us a choice.
+ } ## end if ($pager =~ /^\|/)
+ else {
+
+ # Non-piped "pager". Just restore STDOUT.
+ open( OUT, ">&SAVEOUT" ) || &warn("Can't restore DB::OUT");
+ }
+
+ # Close filehandle pager was using, restore the normal one
+ # if necessary,
+ close(SAVEOUT);
+
+ if ($obj->selected() ne "") {
+ select($obj->selected);
+ $obj->selected("");
+ }
+
+ # No pipes now.
+ $obj->piped("");
+ } ## end if ($piped)
+
+ return;
+}
+
+# 't' is type.
+# 'm' is method.
+# 'v' is the value (i.e: method name or subroutine ref).
+# 's' is subroutine.
+my %cmd_lookup =
+(
+ '.' => { t => 's', v => \&_DB__handle_dot_command, },
+ 'f' => { t => 's', v => \&_DB__handle_f_command, },
+ 'm' => { t => 's', v => \&_DB__handle_m_command, },
+ 'q' => { t => 'm', v => '_handle_q_command', },
+ 'S' => { t => 'm', v => '_handle_S_command', },
+ 't' => { t => 'm', v => '_handle_t_command', },
+ 'x' => { t => 'm', v => '_handle_x_command', },
+ (map { $_ => { t => 'm', v => '_handle_V_command_and_X_command', }, }
+ ('X', 'V')),
+);
+
sub DB {
# lock the debugger and get the thread id for the prompt
my ($prefix, $after, $infix);
my $pat;
my $explicit_stop;
+ my $piped;
+ my $selected;
if ($ENV{PERL5DB_THREADED}) {
$tid = eval { "[".threads->tid."]" };
}
+ my $i;
+
my $obj = DB::Obj->new(
{
position => \$position,
after => \$after,
explicit_stop => \$explicit_stop,
infix => \$infix,
+ i_cmd => \$i,
+ pat => \$pat,
+ piped => \$piped,
+ selected => \$selected,
},
);
#
# If we have a terminal for input, and we get something back
# from readline(), keep on processing.
- my $piped;
- my $selected;
CMD:
while (_DB__read_next_cmd($tid))
# via direct user input. It allows us to 'redo PIPE' to
# re-execute command processing without reading a new command.
PIPE: {
- my $i = _DB__trim_command_and_return_first_component();
+ $i = _DB__trim_command_and_return_first_component();
=head3 COMMAND ALIASES
print $OUT "Couldn't evaluate '$i' alias: $@";
next CMD;
}
+ $i = _DB__trim_command_and_return_first_component();
} ## end if ($alias{$i})
=head3 MAIN-LINE COMMANDS
=cut
- if ($cmd eq 'q') {
- $fall_off_end = 1;
- clean_ENV();
- exit $?;
+ if (my $cmd_rec = $cmd_lookup{$i}) {
+ my $type = $cmd_rec->{t};
+ my $val = $cmd_rec->{v};
+ if ($type eq 'm') {
+ $obj->$val();
+ }
+ elsif ($type eq 's') {
+ $val->($obj);
+ }
}
=head4 C<t> - trace [n]
=cut
- $obj->_handle_t_command;
-
=head4 C<S> - list subroutines matching/not matching a pattern
Walks through C<%sub>, checking to see whether or not to print the name.
=cut
- $obj->_handle_S_command;
-
=head4 C<X> - list variables in current package
Since the C<V> command actually processes this, just change this to the
=cut
- $obj->_handle_V_command_and_X_command;
-
=head4 C<x> - evaluate and print an expression
Hands the expression off to C<DB::eval>, setting it up to print the value
=cut
- if ($cmd =~ s#\Ax\b# #) { # Remainder gets done by DB::eval()
- $onetimeDump = 'dump'; # main::dumpvar shows the output
-
- # handle special "x 3 blah" syntax XXX propagate
- # doc back to special variables.
- if ( $cmd =~ s#\A\s*(\d+)(?=\s)# #) {
- $onetimedumpDepth = $1;
- }
- }
-
=head4 C<m> - print methods
Just uses C<DB::methods> to determine what methods are available.
=cut
- if ($cmd =~ s#\Am\s+([\w:]+)\s*\z# #) {
- methods($1);
- next CMD;
- }
-
- # m expr - set up DB::eval to do the work
- if ($cmd =~ s#\Am\b# #) { # Rest gets done by DB::eval()
- $onetimeDump = 'methods'; # method output gets used there
- }
-
=head4 C<f> - switch files
=cut
- if (($file) = $cmd =~ /\Af\b\s*(.*)/) {
- $file =~ s/\s+$//;
-
- # help for no arguments (old-style was return from sub).
- if ( !$file ) {
- print $OUT
- "The old f command is now the r command.\n"; # hint
- print $OUT "The new f command switches filenames.\n";
- next CMD;
- } ## end if (!$file)
-
- # if not in magic file list, try a close match.
- if ( !defined $main::{ '_<' . $file } ) {
- if ( ($try) = grep( m#^_<.*$file#, keys %main:: ) ) {
- {
- $try = substr( $try, 2 );
- print $OUT "Choosing $try matching '$file':\n";
- $file = $try;
- }
- } ## end if (($try) = grep(m#^_<.*$file#...
- } ## end if (!defined $main::{ ...
-
- # If not successfully switched now, we failed.
- if ( !defined $main::{ '_<' . $file } ) {
- print $OUT "No file matching '$file' is loaded.\n";
- next CMD;
- }
-
- # We switched, so switch the debugger internals around.
- elsif ( $file ne $filename ) {
- *dbline = $main::{ '_<' . $file };
- $max = $#dbline;
- $filename = $file;
- $start = 1;
- $cmd = "l";
- } ## end elsif ($file ne $filename)
-
- # We didn't switch; say we didn't.
- else {
- print $OUT "Already in $file.\n";
- next CMD;
- }
- }
-
=head4 C<.> - return to last-executed line.
We set C<$incr> to -1 to indicate that the debugger shouldn't move ahead,
=cut
- # . command.
- if ($cmd eq '.') {
- $incr = -1; # stay at current line
-
- # Reset everything to the old location.
- $start = $line;
- $filename = $filename_ini;
- *dbline = $main::{ '_<' . $filename };
- $max = $#dbline;
-
- # Now where are we?
- print_lineinfo($position);
- next CMD;
- }
-
=head4 C<-> - back one window
We change C<$start> to be one window back; if we go back past the first line,
=cut
# - - back a window.
- if ($cmd eq '-') {
-
- # back up by a window; go to 1 if back too far.
- $start -= $incr + $window + 1;
- $start = 1 if $start <= 0;
- $incr = $window - 1;
-
- # Generate and execute a "l +" command (handled below).
- $cmd = 'l ' . ($start) . '+';
- }
+ $obj->_handle_dash_command;
=head3 PRE-580 COMMANDS VS. NEW COMMANDS: C<a, A, b, B, h, l, L, M, o, O, P, v, w, W, E<lt>, E<lt>E<lt>, E<0x7B>, E<0x7B>E<0x7B>>
# All of these commands were remapped in perl 5.8.0;
# we send them off to the secondary dispatcher (see below).
- if (my ($cmd_letter, $my_arg) = $cmd =~ /\A([aAbBeEhilLMoOPvwW]\b|[<>\{]{1,2})\s*(.*)/so) {
- &cmd_wrapper( $cmd_letter, $my_arg, $line );
- next CMD;
- }
+ $obj->_handle_cmd_wrapper_commands;
=head4 C<y> - List lexicals in higher scope
=cut
- if (my ($match_level, $match_vars)
- = $cmd =~ /^y(?:\s+(\d*)\s*(.*))?$/) {
-
- # See if we've got the necessary support.
- eval { require PadWalker; PadWalker->VERSION(0.08) }
- or &warn(
- $@ =~ /locate/
- ? "PadWalker module not found - please install\n"
- : $@
- )
- and next CMD;
-
- # Load up dumpvar if we don't have it. If we can, that is.
- do 'dumpvar.pl' || die $@ unless defined &main::dumpvar;
- defined &main::dumpvar
- or print $OUT "dumpvar.pl not available.\n"
- and next CMD;
-
- # Got all the modules we need. Find them and print them.
- my @vars = split( ' ', $match_vars || '' );
-
- # Find the pad.
- my $h = eval { PadWalker::peek_my( ( $match_level || 0 ) + 1 ) };
-
- # Oops. Can't find it.
- $@ and $@ =~ s/ at .*//, &warn($@), next CMD;
-
- # Show the desired vars with dumplex().
- my $savout = select($OUT);
-
- # Have dumplex dump the lexicals.
- dumpvar::dumplex( $_, $h->{$_},
- defined $option{dumpDepth} ? $option{dumpDepth} : -1,
- @vars )
- for sort keys %$h;
- select($savout);
- next CMD;
- }
+ _DB__handle_y_command($obj);
=head3 COMMANDS NOT WORKING AFTER PROGRAM ENDS
=cut
# n - next
- if ($cmd eq 'n') {
- next CMD if _DB__is_finished();
-
- # Single step, but don't enter subs.
- $single = 2;
-
- # Save for empty command (repeat last).
- $laststep = $cmd;
- last CMD;
- }
+ $obj->_handle_n_command;
=head4 C<s> - single-step, entering subs
=cut
- # s - single step.
- if ($cmd eq 's') {
-
- # Get out and restart the command loop if program
- # has finished.
- next CMD if _DB__is_finished();
-
- # Single step should enter subs.
- $single = 1;
-
- # Save for empty command (repeat last).
- $laststep = $cmd;
- last CMD;
- }
+ $obj->_handle_s_command;
=head4 C<c> - run continuously, setting an optional breakpoint
=cut
# c - start continuous execution.
- if (($i) = $cmd =~ m#\Ac\b\s*([\w:]*)\s*\z#) {
-
- # Hey, show's over. The debugged program finished
- # executing already.
- next CMD if _DB__is_finished();
-
- # Capture the place to put a one-time break.
- $subname = $i;
-
- # Probably not needed, since we finish an interactive
- # sub-session anyway...
- # local $filename = $filename;
- # local *dbline = *dbline; # XXX Would this work?!
- #
- # The above question wonders if localizing the alias
- # to the magic array works or not. Since it's commented
- # out, we'll just leave that to speculation for now.
-
- # If the "subname" isn't all digits, we'll assume it
- # is a subroutine name, and try to find it.
- if ( $subname =~ /\D/ ) { # subroutine name
- # Qualify it to the current package unless it's
- # already qualified.
- $subname = $package . "::" . $subname
- unless $subname =~ /::/;
-
- # find_sub will return "file:line_number" corresponding
- # to where the subroutine is defined; we call find_sub,
- # break up the return value, and assign it in one
- # operation.
- ( $file, $i ) = ( find_sub($subname) =~ /^(.*):(.*)$/ );
-
- # Force the line number to be numeric.
- $i += 0;
-
- # If we got a line number, we found the sub.
- if ($i) {
-
- # Switch all the debugger's internals around so
- # we're actually working with that file.
- $filename = $file;
- *dbline = $main::{ '_<' . $filename };
-
- # Mark that there's a breakpoint in this file.
- $had_breakpoints{$filename} |= 1;
-
- # Scan forward to the first executable line
- # after the 'sub whatever' line.
- $max = $#dbline;
- ++$i while $dbline[$i] == 0 && $i < $max;
- } ## end if ($i)
-
- # We didn't find a sub by that name.
- else {
- print $OUT "Subroutine $subname not found.\n";
- next CMD;
- }
- } ## end if ($subname =~ /\D/)
-
- # At this point, either the subname was all digits (an
- # absolute line-break request) or we've scanned through
- # the code following the definition of the sub, looking
- # for an executable, which we may or may not have found.
- #
- # If $i (which we set $subname from) is non-zero, we
- # got a request to break at some line somewhere. On
- # one hand, if there wasn't any real subroutine name
- # involved, this will be a request to break in the current
- # file at the specified line, so we have to check to make
- # sure that the line specified really is breakable.
- #
- # On the other hand, if there was a subname supplied, the
- # preceding block has moved us to the proper file and
- # location within that file, and then scanned forward
- # looking for the next executable line. We have to make
- # sure that one was found.
- #
- # On the gripping hand, we can't do anything unless the
- # current value of $i points to a valid breakable line.
- # Check that.
- if ($i) {
-
- # Breakable?
- if ( $dbline[$i] == 0 ) {
- print $OUT "Line $i not breakable.\n";
- next CMD;
- }
-
- # Yes. Set up the one-time-break sigil.
- $dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
- _enable_breakpoint_temp_enabled_status($filename, $i);
- } ## end if ($i)
-
- # Turn off stack tracing from here up.
- for my $i (0 .. $stack_depth) {
- $stack[ $i ] &= ~1;
- }
- last CMD;
- }
+ _DB__handle_c_command($obj);
=head4 C<r> - return from a subroutine
=cut
# r - return from the current subroutine.
- if ($cmd eq 'r') {
+ $obj->_handle_r_command;
- # Can't do anything if the program's over.
- next CMD if _DB__is_finished();
-
- # Turn on stack trace.
- $stack[$stack_depth] |= 1;
-
- # Print return value unless the stack is empty.
- $doret = $option{PrintRet} ? $stack_depth - 1 : -2;
- last CMD;
- }
-
-=head4 C<T> - stack trace
+=head4 C<T> - stack trace
Just calls C<DB::print_trace>.
=cut
- if ($cmd eq 'T') {
- print_trace( $OUT, 1 ); # skip DB
- next CMD;
- }
+ $obj->_handle_T_command;
=head4 C<w> - List window around current line.
=cut
- if (my ($arg) = $cmd =~ /\Aw\b\s*(.*)/s) {
- &cmd_w( 'w', $arg );
- next CMD;
- }
+ $obj->_handle_w_command;
=head4 C<W> - watch-expression processing.
=cut
- if (my ($arg) = $cmd =~ /\AW\b\s*(.*)/s) {
- &cmd_W( 'W', $arg );
- next CMD;
- }
+ $obj->_handle_W_command;
=head4 C</> - search forward for a string in the source
=cut
- # The pattern as a string.
- use vars qw($inpat);
-
- if (($inpat) = $cmd =~ m#\A/(.*)\z#) {
-
- # Remove the final slash.
- $inpat =~ s:([^\\])/$:$1:;
-
- # If the pattern isn't null ...
- if ( $inpat ne "" ) {
-
- # Turn of warn and die procesing for a bit.
- local $SIG{__DIE__};
- local $SIG{__WARN__};
-
- # Create the pattern.
- eval 'no strict q/vars/; $inpat =~ m' . "\a$inpat\a";
- if ( $@ ne "" ) {
-
- # Oops. Bad pattern. No biscuit.
- # Print the eval error and go back for more
- # commands.
- print $OUT "$@";
- next CMD;
- }
- $pat = $inpat;
- } ## end if ($inpat ne "")
-
- # Set up to stop on wrap-around.
- $end = $start;
-
- # Don't move off the current line.
- $incr = -1;
-
- # Done in eval so nothing breaks if the pattern
- # does something weird.
- eval '
- no strict q/vars/;
- for (;;) {
- # Move ahead one line.
- ++$start;
-
- # Wrap if we pass the last line.
- $start = 1 if ($start > $max);
-
- # Stop if we have gotten back to this line again,
- last if ($start == $end);
-
- # A hit! (Note, though, that we are doing
- # case-insensitive matching. Maybe a qr//
- # expression would be better, so the user could
- # do case-sensitive matching if desired.
- if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
- if ($slave_editor) {
- # Handle proper escaping in the slave.
- print $OUT "\032\032$filename:$start:0\n";
- }
- else {
- # Just print the line normally.
- print $OUT "$start:\t",$dbline[$start],"\n";
- }
- # And quit since we found something.
- last;
- }
- } ';
-
- # If we wrapped, there never was a match.
- print $OUT "/$pat/: not found\n" if ( $start == $end );
- next CMD;
- }
+ _DB__handle_forward_slash_command($obj);
=head4 C<?> - search backward for a string in the source
=cut
- # ? - backward pattern search.
- if (my ($inpat) = $cmd =~ m#\A\?(.*)\z#) {
-
- # Get the pattern, remove trailing question mark.
- $inpat =~ s:([^\\])\?$:$1:;
-
- # If we've got one ...
- if ( $inpat ne "" ) {
-
- # Turn off die & warn handlers.
- local $SIG{__DIE__};
- local $SIG{__WARN__};
- eval '$inpat =~ m' . "\a$inpat\a";
-
- if ( $@ ne "" ) {
-
- # Ouch. Not good. Print the error.
- print $OUT $@;
- next CMD;
- }
- $pat = $inpat;
- } ## end if ($inpat ne "")
-
- # Where we are now is where to stop after wraparound.
- $end = $start;
-
- # Don't move away from this line.
- $incr = -1;
-
- # Search inside the eval to prevent pattern badness
- # from killing us.
- eval '
- no strict q/vars/;
- for (;;) {
- # Back up a line.
- --$start;
-
- # Wrap if we pass the first line.
-
- $start = $max if ($start <= 0);
-
- # Quit if we get back where we started,
- last if ($start == $end);
-
- # Match?
- if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
- if ($slave_editor) {
- # Yep, follow slave editor requirements.
- print $OUT "\032\032$filename:$start:0\n";
- }
- else {
- # Yep, just print normally.
- print $OUT "$start:\t",$dbline[$start],"\n";
- }
-
- # Found, so done.
- last;
- }
- } ';
-
- # Say we failed if the loop never found anything,
- print $OUT "?$pat?: not found\n" if ( $start == $end );
- next CMD;
- }
+ _DB__handle_question_mark_command($obj);
=head4 C<$rc> - Recall command
=cut
# $rc - recall command.
- if (my ($minus, $arg) = $cmd =~ m#\A$rc+\s*(-)?(\d+)?\z#) {
-
- # No arguments, take one thing off history.
- pop(@hist) if length($cmd) > 1;
-
- # Relative (- found)?
- # Y - index back from most recent (by 1 if bare minus)
- # N - go to that particular command slot or the last
- # thing if nothing following.
- $i = $minus ? ( $#hist - ( $arg || 1 ) ) : ( $arg || $#hist );
-
- # Pick out the command desired.
- $cmd = $hist[$i];
-
- # Print the command to be executed and restart the loop
- # with that command in the buffer.
- print $OUT $cmd, "\n";
- redo CMD;
- }
+ $obj->_handle_rc_recall_command;
=head4 C<$sh$sh> - C<system()> command
=cut
- # $sh$sh - run a shell command (if it's all ASCII).
- # Can't run shell commands with Unicode in the debugger, hmm.
- if (my ($arg) = $cmd =~ m#\A$sh$sh\s*(.*)#ms) {
-
- # System it.
- &system($arg);
- next CMD;
- }
+ $obj->_handle_sh_command;
=head4 C<$rc I<pattern> $rc> - Search command history
=cut
- # $rc pattern $rc - find a command in the history.
- if (my ($arg) = $cmd =~ /\A$rc([^$rc].*)\z/) {
-
- # Create the pattern to use.
- $pat = "^$arg";
-
- # Toss off last entry if length is >1 (and it always is).
- pop(@hist) if length($cmd) > 1;
-
- # Look backward through the history.
- for ( $i = $#hist ; $i ; --$i ) {
- # Stop if we find it.
- last if $hist[$i] =~ /$pat/;
- }
-
- if ( !$i ) {
-
- # Never found it.
- print $OUT "No such command!\n\n";
- next CMD;
- }
-
- # Found it. Put it in the buffer, print it, and process it.
- $cmd = $hist[$i];
- print $OUT $cmd, "\n";
- redo CMD;
- }
+ $obj->_handle_rc_search_history_command;
=head4 C<$sh> - Invoke a shell
=cut
- # $sh - start a shell.
- if ($cmd =~ /\A$sh\z/) {
-
- # Run the user's shell. If none defined, run Bourne.
- # We resume execution when the shell terminates.
- &system( $ENV{SHELL} || "/bin/sh" );
- next CMD;
- }
-
=head4 C<$sh I<command>> - Force execution of a command in a shell
Like the above, but the command is passed to the shell. Again, we use
C<DB::system> to avoid problems with C<STDIN> and C<STDOUT>.
-=cut
-
- # $sh command - start a shell and run a command in it.
- if (my ($arg) = $cmd =~ m#\A$sh\s*(.*)#ms) {
-
- # XXX: using csh or tcsh destroys sigint retvals!
- #&system($1); # use this instead
-
- # use the user's shell, or Bourne if none defined.
- &system( $ENV{SHELL} || "/bin/sh", "-c", $arg );
- next CMD;
- }
-
=head4 C<H> - display commands in history
Prints the contents of C<@hist> (if any).
=cut
- if ($cmd =~ /\AH\b\s*\*/) {
- @hist = @truehist = ();
- print $OUT "History cleansed\n";
- next CMD;
- }
-
- if (my ($num)
- = $cmd =~ /\AH\b\s*(?:-(\d+))?/) {
-
- # Anything other than negative numbers is ignored by
- # the (incorrect) pattern, so this test does nothing.
- $end = $num ? ( $#hist - $num ) : 0;
-
- # Set to the minimum if less than zero.
- $hist = 0 if $hist < 0;
-
- # Start at the end of the array.
- # Stay in while we're still above the ending value.
- # Tick back by one each time around the loop.
- for ( $i = $#hist ; $i > $end ; $i-- ) {
-
- # Print the command unless it has no arguments.
- print $OUT "$i: ", $hist[$i], "\n"
- unless $hist[$i] =~ /^.?$/;
- }
- next CMD;
- }
+ $obj->_handle_H_command;
=head4 C<man, doc, perldoc> - look up documentation
=cut
- # man, perldoc, doc - show manual pages.
- if (my ($man_page)
- = $cmd =~ /\A(?:man|(?:perl)?doc)\b(?:\s+([^(]*))?\z/) {
- runman($man_page);
- next CMD;
- }
+ $obj->_handle_doc_command;
=head4 C<p> - print
=cut
- my $print_cmd = 'print {$DB::OUT} ';
- # p - print (no args): print $_.
- if ($cmd eq 'p') {
- $cmd = $print_cmd . '$_';
- }
-
- # p - print the given expression.
- $cmd =~ s/\Ap\b/$print_cmd /;
+ $obj->_handle_p_command;
=head4 C<=> - define command alias
=cut
# = - set up a command alias.
- if ($cmd =~ s/\A=\s*//) {
- my @keys;
- if ( length $cmd == 0 ) {
-
- # No args, get current aliases.
- @keys = sort keys %alias;
- }
- elsif ( my ( $k, $v ) = ( $cmd =~ /^(\S+)\s+(\S.*)/ ) ) {
-
- # Creating a new alias. $k is alias name, $v is
- # alias value.
-
- # can't use $_ or kill //g state
- for my $x ( $k, $v ) {
-
- # Escape "alarm" characters.
- $x =~ s/\a/\\a/g;
- }
-
- # Substitute key for value, using alarm chars
- # as separators (which is why we escaped them in
- # the command).
- $alias{$k} = "s\a$k\a$v\a";
-
- # Turn off standard warn and die behavior.
- local $SIG{__DIE__};
- local $SIG{__WARN__};
-
- # Is it valid Perl?
- unless ( eval "sub { s\a$k\a$v\a }; 1" ) {
-
- # Nope. Bad alias. Say so and get out.
- print $OUT "Can't alias $k to $v: $@\n";
- delete $alias{$k};
- next CMD;
- }
-
- # We'll only list the new one.
- @keys = ($k);
- } ## end elsif (my ($k, $v) = ($cmd...
-
- # The argument is the alias to list.
- else {
- @keys = ($cmd);
- }
-
- # List aliases.
- for my $k (@keys) {
-
- # Messy metaquoting: Trim the substitution code off.
- # We use control-G as the delimiter because it's not
- # likely to appear in the alias.
- if ( ( my $v = $alias{$k} ) =~ s\as\a$k\a(.*)\a$\a1\a ) {
-
- # Print the alias.
- print $OUT "$k\t= $1\n";
- }
- elsif ( defined $alias{$k} ) {
-
- # Couldn't trim it off; just print the alias code.
- print $OUT "$k\t$alias{$k}\n";
- }
- else {
-
- # No such, dude.
- print "No alias for $k\n";
- }
- } ## end for my $k (@keys)
- next CMD;
- }
+ $obj->_handle_equal_sign_command;
=head4 C<source> - read commands from a file.
=cut
- # source - read commands from a file (or pipe!) and execute.
- if (my ($sourced_fn) = $cmd =~ /\Asource\s+(.*\S)/) {
- if ( open my $fh, $sourced_fn ) {
-
- # Opened OK; stick it in the list of file handles.
- push @cmdfhs, $fh;
- }
- else {
-
- # Couldn't open it.
- &warn("Can't execute '$sourced_fn': $!\n");
- }
- next CMD;
- }
+ $obj->_handle_source_command;
- if (my ($which_cmd, $position)
- = $cmd =~ /^(enable|disable)\s+(\S+)\s*$/) {
+=head4 C<enable> C<disable> - enable or disable breakpoints
- my ($fn, $line_num);
- if ($position =~ m{\A\d+\z})
- {
- $fn = $filename;
- $line_num = $position;
- }
- elsif (my ($new_fn, $new_line_num)
- = $position =~ m{\A(.*):(\d+)\z}) {
- ($fn, $line_num) = ($new_fn, $new_line_num);
- }
- else
- {
- &warn("Wrong spec for enable/disable argument.\n");
- }
+This enables or disables breakpoints.
- if (defined($fn)) {
- if (_has_breakpoint_data_ref($fn, $line_num)) {
- _set_breakpoint_enabled_status($fn, $line_num,
- ($which_cmd eq 'enable' ? 1 : '')
- );
- }
- else {
- &warn("No breakpoint set at ${fn}:${line_num}\n");
- }
- }
+=cut
- next CMD;
- }
+ $obj->_handle_enable_disable_commands;
=head4 C<save> - send current history to a file
=cut
# save source - write commands to a file for later use
- if (my ($new_fn) = $cmd =~ /\Asave\s*(.*)\z/) {
- my $filename = $new_fn || '.perl5dbrc'; # default?
- if ( open my $fh, '>', $filename ) {
-
- # chomp to remove extraneous newlines from source'd files
- chomp( my @truelist =
- map { m/^\s*(save|source)/ ? "#$_" : $_ }
- @truehist );
- print $fh join( "\n", @truelist );
- print "commands saved in $file\n";
- }
- else {
- &warn("Can't save debugger commands in '$new_fn': $!\n");
- }
- next CMD;
- }
+ $obj->_handle_save_command;
=head4 C<R> - restart
# R - restart execution.
# rerun - controlled restart execution.
- if (my ($cmd_cmd, $cmd_params) =
- $cmd =~ /\A((?:R)|(?:rerun\s*(.*)))\z/) {
- my @args = ($cmd_cmd eq 'R' ? restart() : rerun($cmd_params));
-
- # Close all non-system fds for a clean restart. A more
- # correct method would be to close all fds that were not
- # open when the process started, but this seems to be
- # hard. See "debugger 'R'estart and open database
- # connections" on p5p.
-
- my $max_fd = 1024; # default if POSIX can't be loaded
- if (eval { require POSIX }) {
- eval { $max_fd = POSIX::sysconf(POSIX::_SC_OPEN_MAX()) };
- }
-
- if (defined $max_fd) {
- foreach ($^F+1 .. $max_fd-1) {
- next unless open FD_TO_CLOSE, "<&=$_";
- close(FD_TO_CLOSE);
- }
- }
-
- # And run Perl again. We use exec() to keep the
- # PID stable (and that way $ini_pids is still valid).
- exec(@args) || print $OUT "exec failed: $!\n";
-
- last CMD;
- }
+ _DB__handle_restart_and_rerun_commands($obj);
=head4 C<|, ||> - pipe output through the pager.
=cut
# || - run command in the pager, with output to DB::OUT.
- if ($cmd =~ m#\A\|\|?\s*[^|]#) {
- if ( $pager =~ /^\|/ ) {
-
- # Default pager is into a pipe. Redirect I/O.
- open( SAVEOUT, ">&STDOUT" )
- || &warn("Can't save STDOUT");
- open( STDOUT, ">&OUT" )
- || &warn("Can't redirect STDOUT");
- } ## end if ($pager =~ /^\|/)
- else {
-
- # Not into a pipe. STDOUT is safe.
- open( SAVEOUT, ">&OUT" ) || &warn("Can't save DB::OUT");
- }
-
- # Fix up environment to record we have less if so.
- fix_less();
-
- unless ( $piped = open( OUT, $pager ) ) {
-
- # Couldn't open pipe to pager.
- &warn("Can't pipe output to '$pager'");
- if ( $pager =~ /^\|/ ) {
-
- # Redirect I/O back again.
- open( OUT, ">&STDOUT" ) # XXX: lost message
- || &warn("Can't restore DB::OUT");
- open( STDOUT, ">&SAVEOUT" )
- || &warn("Can't restore STDOUT");
- close(SAVEOUT);
- } ## end if ($pager =~ /^\|/)
- else {
-
- # Redirect I/O. STDOUT already safe.
- open( OUT, ">&STDOUT" ) # XXX: lost message
- || &warn("Can't restore DB::OUT");
- }
- next CMD;
- } ## end unless ($piped = open(OUT,...
-
- # Set up broken-pipe handler if necessary.
- $SIG{PIPE} = \&DB::catch
- if $pager =~ /^\|/
- && ( "" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE} );
-
- OUT->autoflush(1);
- # Save current filehandle, and put it back.
- $selected = select(OUT);
- # Don't put it back if pager was a pipe.
- select($selected), $selected = "" unless $cmd =~ /^\|\|/;
-
- # Trim off the pipe symbols and run the command now.
- $cmd =~ s#\A\|+\s*##;
- redo PIPE;
- }
+ _DB__handle_run_command_in_pager_command($obj);
=head3 END OF COMMAND PARSING
=cut
- # t - turn trace on.
- if ($cmd =~ s#\At\s+(\d+)?#\$DB::trace |= 1;\n#) {
- my $trace_arg = $1;
- $trace_to_depth = $trace_arg ? $stack_depth||0 + $1 : 1E9;
- }
-
- # s - single-step. Remember the last command was 's'.
- if ($cmd =~ s/\As\s/\$DB::single = 1;\n/) {
- $laststep = 's';
- }
-
- # n - single-step, but not into subs. Remember last command
- # was 'n'.
- if ($cmd =~ s#\An\s#\$DB::single = 2;\n#) {
- $laststep = 'n';
- }
-
} # PIPE:
# Make sure the flag that says "the debugger's running" is
=cut
continue { # CMD:
-
- # At the end of every command:
- if ($piped) {
-
- # Unhook the pipe mechanism now.
- if ( $pager =~ /^\|/ ) {
-
- # No error from the child.
- $? = 0;
-
- # we cannot warn here: the handle is missing --tchrist
- close(OUT) || print SAVEOUT "\nCan't close DB::OUT\n";
-
- # most of the $? crud was coping with broken cshisms
- # $? is explicitly set to 0, so this never runs.
- if ($?) {
- print SAVEOUT "Pager '$pager' failed: ";
- if ( $? == -1 ) {
- print SAVEOUT "shell returned -1\n";
- }
- elsif ( $? >> 8 ) {
- print SAVEOUT ( $? & 127 )
- ? " (SIG#" . ( $? & 127 ) . ")"
- : "", ( $? & 128 ) ? " -- core dumped" : "", "\n";
- }
- else {
- print SAVEOUT "status ", ( $? >> 8 ), "\n";
- }
- } ## end if ($?)
-
- # Reopen filehandle for our output (if we can) and
- # restore STDOUT (if we can).
- open( OUT, ">&STDOUT" ) || &warn("Can't restore DB::OUT");
- open( STDOUT, ">&SAVEOUT" )
- || &warn("Can't restore STDOUT");
-
- # Turn off pipe exception handler if necessary.
- $SIG{PIPE} = "DEFAULT" if $SIG{PIPE} eq \&DB::catch;
-
- # Will stop ignoring SIGPIPE if done like nohup(1)
- # does SIGINT but Perl doesn't give us a choice.
- } ## end if ($pager =~ /^\|/)
- else {
-
- # Non-piped "pager". Just restore STDOUT.
- open( OUT, ">&SAVEOUT" ) || &warn("Can't restore DB::OUT");
- }
-
- # Close filehandle pager was using, restore the normal one
- # if necessary,
- close(SAVEOUT);
- select($selected), $selected = "" unless $selected eq "";
-
- # No pipes now.
- $piped = "";
- } ## end if ($piped)
+ _DB__at_end_of_every_command($obj);
} # CMD:
=head3 COMMAND LOOP TERMINATION
{
no strict 'refs';
- foreach my $slot_name (qw(after explicit_stop infix position prefix)) {
+ foreach my $slot_name (qw(
+ after explicit_stop infix pat piped position prefix selected i_cmd
+ )) {
my $slot = $slot_name;
*{$slot} = sub {
my $self = shift;
return;
}
+sub _handle_dash_command {
+
+ if ($DB::cmd eq '-') {
+
+ # back up by a window; go to 1 if back too far.
+ $start -= $incr + $window + 1;
+ $start = 1 if $start <= 0;
+ $incr = $window - 1;
+
+ # Generate and execute a "l +" command (handled below).
+ $DB::cmd = 'l ' . ($start) . '+';
+ }
+ return;
+}
+
+sub _n_or_s_commands_generic {
+ my ($self, $new_val) = @_;
+ # n - next
+ next CMD if DB::_DB__is_finished();
+
+ # Single step, but don't enter subs.
+ $single = $new_val;
+
+ # Save for empty command (repeat last).
+ $laststep = $DB::cmd;
+ last CMD;
+}
+
+sub _n_or_s {
+ my ($self, $letter, $new_val) = @_;
+
+ if ($DB::cmd eq $letter) {
+ $self->_n_or_s_commands_generic($new_val);
+ }
+ elsif ($DB::cmd =~ m#\A\Q$letter\E\b#) {
+ $self->_n_or_s_and_arg_commands_generic($letter, $new_val);
+ }
+
+ return;
+}
+
+sub _handle_n_command {
+ my $self = shift;
+
+ return $self->_n_or_s('n', 2);
+}
+
+sub _handle_s_command {
+ my $self = shift;
+
+ return $self->_n_or_s('s', 1);
+}
+
+sub _handle_r_command {
+ my $self = shift;
+ # r - return from the current subroutine.
+ if ($DB::cmd eq 'r') {
+
+ # Can't do anything if the program's over.
+ next CMD if DB::_DB__is_finished();
+
+ # Turn on stack trace.
+ $stack[$stack_depth] |= 1;
+
+ # Print return value unless the stack is empty.
+ $doret = $option{PrintRet} ? $stack_depth - 1 : -2;
+ last CMD;
+ }
+
+ return;
+}
+
+sub _handle_T_command {
+ if ($DB::cmd eq 'T') {
+ DB::print_trace( $OUT, 1 ); # skip DB
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_w_command {
+ if (my ($arg) = $DB::cmd =~ /\Aw\b\s*(.*)/s) {
+ DB::cmd_w( 'w', $arg );
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_W_command {
+ if (my ($arg) = $DB::cmd =~ /\AW\b\s*(.*)/s) {
+ DB::cmd_W( 'W', $arg );
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_rc_recall_command {
+ my $self = shift;
+
+ # $rc - recall command.
+ if (my ($minus, $arg) = $DB::cmd =~ m#\A$rc+\s*(-)?(\d+)?\z#) {
+
+ # No arguments, take one thing off history.
+ pop(@hist) if length($DB::cmd) > 1;
+
+ # Relative (- found)?
+ # Y - index back from most recent (by 1 if bare minus)
+ # N - go to that particular command slot or the last
+ # thing if nothing following.
+ my $new_i = $minus ? ( $#hist - ( $arg || 1 ) ) : ( $arg || $#hist );
+
+ $self->i_cmd($new_i);
+
+ # Pick out the command desired.
+ $DB::cmd = $hist[$self->i_cmd];
+
+ # Print the command to be executed and restart the loop
+ # with that command in the buffer.
+ print {$OUT} $DB::cmd, "\n";
+ redo CMD;
+ }
+
+ return;
+}
+
+sub _handle_rc_search_history_command {
+ my $self = shift;
+
+ # $rc pattern $rc - find a command in the history.
+ if (my ($arg) = $DB::cmd =~ /\A$rc([^$rc].*)\z/) {
+
+ # Create the pattern to use.
+ my $pat = "^$arg";
+ $self->pat($pat);
+
+ # Toss off last entry if length is >1 (and it always is).
+ pop(@hist) if length($DB::cmd) > 1;
+
+ my $i = $self->i_cmd;
+
+ # Look backward through the history.
+ SEARCH_HIST:
+ for ( $i = $#hist ; $i ; --$i ) {
+ # Stop if we find it.
+ last SEARCH_HIST if $hist[$i] =~ /$pat/;
+ }
+
+ $self->i_cmd($i);
+
+ if ( !$self->i_cmd ) {
+
+ # Never found it.
+ print $OUT "No such command!\n\n";
+ next CMD;
+ }
+
+ # Found it. Put it in the buffer, print it, and process it.
+ $DB::cmd = $hist[$self->i_cmd];
+ print $OUT $DB::cmd, "\n";
+ redo CMD;
+ }
+
+ return;
+}
+
+sub _handle_H_command {
+ my $self = shift;
+
+ if ($DB::cmd =~ /\AH\b\s*\*/) {
+ @hist = @truehist = ();
+ print $OUT "History cleansed\n";
+ next CMD;
+ }
+
+ if (my ($num)
+ = $DB::cmd =~ /\AH\b\s*(?:-(\d+))?/) {
+
+ # Anything other than negative numbers is ignored by
+ # the (incorrect) pattern, so this test does nothing.
+ $end = $num ? ( $#hist - $num ) : 0;
+
+ # Set to the minimum if less than zero.
+ $hist = 0 if $hist < 0;
+
+ # Start at the end of the array.
+ # Stay in while we're still above the ending value.
+ # Tick back by one each time around the loop.
+ my $i;
+
+ for ( $i = $#hist ; $i > $end ; $i-- ) {
+
+ # Print the command unless it has no arguments.
+ print $OUT "$i: ", $hist[$i], "\n"
+ unless $hist[$i] =~ /^.?$/;
+ }
+
+ $self->i_cmd($i);
+
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_doc_command {
+ my $self = shift;
+
+ # man, perldoc, doc - show manual pages.
+ if (my ($man_page)
+ = $DB::cmd =~ /\A(?:man|(?:perl)?doc)\b(?:\s+([^(]*))?\z/) {
+ runman($man_page);
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_p_command {
+ my $self = shift;
+
+ my $print_cmd = 'print {$DB::OUT} ';
+ # p - print (no args): print $_.
+ if ($DB::cmd eq 'p') {
+ $DB::cmd = $print_cmd . '$_';
+ }
+
+ # p - print the given expression.
+ $DB::cmd =~ s/\Ap\b/$print_cmd /;
+
+ return;
+}
+
+sub _handle_equal_sign_command {
+ my $self = shift;
+
+ if ($DB::cmd =~ s/\A=\s*//) {
+ my @keys;
+ if ( length $DB::cmd == 0 ) {
+
+ # No args, get current aliases.
+ @keys = sort keys %alias;
+ }
+ elsif ( my ( $k, $v ) = ( $DB::cmd =~ /^(\S+)\s+(\S.*)/ ) ) {
+
+ # Creating a new alias. $k is alias name, $v is
+ # alias value.
+
+ # can't use $_ or kill //g state
+ for my $x ( $k, $v ) {
+
+ # Escape "alarm" characters.
+ $x =~ s/\a/\\a/g;
+ }
+
+ # Substitute key for value, using alarm chars
+ # as separators (which is why we escaped them in
+ # the command).
+ $alias{$k} = "s\a$k\a$v\a";
+
+ # Turn off standard warn and die behavior.
+ local $SIG{__DIE__};
+ local $SIG{__WARN__};
+
+ # Is it valid Perl?
+ unless ( eval "sub { s\a$k\a$v\a }; 1" ) {
+
+ # Nope. Bad alias. Say so and get out.
+ print $OUT "Can't alias $k to $v: $@\n";
+ delete $alias{$k};
+ next CMD;
+ }
+
+ # We'll only list the new one.
+ @keys = ($k);
+ } ## end elsif (my ($k, $v) = ($DB::cmd...
+
+ # The argument is the alias to list.
+ else {
+ @keys = ($DB::cmd);
+ }
+
+ # List aliases.
+ for my $k (@keys) {
+
+ # Messy metaquoting: Trim the substitution code off.
+ # We use control-G as the delimiter because it's not
+ # likely to appear in the alias.
+ if ( ( my $v = $alias{$k} ) =~ s\as\a$k\a(.*)\a$\a1\a ) {
+
+ # Print the alias.
+ print $OUT "$k\t= $1\n";
+ }
+ elsif ( defined $alias{$k} ) {
+
+ # Couldn't trim it off; just print the alias code.
+ print $OUT "$k\t$alias{$k}\n";
+ }
+ else {
+
+ # No such, dude.
+ print "No alias for $k\n";
+ }
+ } ## end for my $k (@keys)
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_source_command {
+ my $self = shift;
+
+ # source - read commands from a file (or pipe!) and execute.
+ if (my ($sourced_fn) = $DB::cmd =~ /\Asource\s+(.*\S)/) {
+ if ( open my $fh, $sourced_fn ) {
+
+ # Opened OK; stick it in the list of file handles.
+ push @cmdfhs, $fh;
+ }
+ else {
+
+ # Couldn't open it.
+ DB::warn("Can't execute '$sourced_fn': $!\n");
+ }
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_enable_disable_commands {
+ my $self = shift;
+
+ if (my ($which_cmd, $position)
+ = $DB::cmd =~ /\A(enable|disable)\s+(\S+)\s*\z/) {
+
+ my ($fn, $line_num);
+ if ($position =~ m{\A\d+\z})
+ {
+ $fn = $DB::filename;
+ $line_num = $position;
+ }
+ elsif (my ($new_fn, $new_line_num)
+ = $position =~ m{\A(.*):(\d+)\z}) {
+ ($fn, $line_num) = ($new_fn, $new_line_num);
+ }
+ else
+ {
+ DB::warn("Wrong spec for enable/disable argument.\n");
+ }
+
+ if (defined($fn)) {
+ if (DB::_has_breakpoint_data_ref($fn, $line_num)) {
+ DB::_set_breakpoint_enabled_status($fn, $line_num,
+ ($which_cmd eq 'enable' ? 1 : '')
+ );
+ }
+ else {
+ DB::warn("No breakpoint set at ${fn}:${line_num}\n");
+ }
+ }
+
+ next CMD;
+ }
+
+ return;
+}
+
+sub _handle_save_command {
+ my $self = shift;
+
+ if (my ($new_fn) = $DB::cmd =~ /\Asave\s*(.*)\z/) {
+ my $filename = $new_fn || '.perl5dbrc'; # default?
+ if ( open my $fh, '>', $filename ) {
+
+ # chomp to remove extraneous newlines from source'd files
+ chomp( my @truelist =
+ map { m/\A\s*(save|source)/ ? "#$_" : $_ }
+ @truehist );
+ print {$fh} join( "\n", @truelist );
+ print "commands saved in $filename\n";
+ }
+ else {
+ DB::warn("Can't save debugger commands in '$new_fn': $!\n");
+ }
+ next CMD;
+ }
+
+ return;
+}
+
+sub _n_or_s_and_arg_commands_generic {
+ my ($self, $letter, $new_val) = @_;
+
+ # s - single-step. Remember the last command was 's'.
+ if ($DB::cmd =~ s#\A\Q$letter\E\s#\$DB::single = $new_val;\n#) {
+ $laststep = $letter;
+ }
+
+ return;
+}
+
+sub _handle_sh_command {
+ my $self = shift;
+
+ # $sh$sh - run a shell command (if it's all ASCII).
+ # Can't run shell commands with Unicode in the debugger, hmm.
+ my $my_cmd = $DB::cmd;
+ if ($my_cmd =~ m#\A$sh#gms) {
+
+ if ($my_cmd =~ m#\G\z#cgms) {
+ # Run the user's shell. If none defined, run Bourne.
+ # We resume execution when the shell terminates.
+ DB::system( $ENV{SHELL} || "/bin/sh" );
+ next CMD;
+ }
+ elsif ($my_cmd =~ m#\G$sh\s*(.*)#cgms) {
+ # System it.
+ DB::system($1);
+ next CMD;
+ }
+ elsif ($my_cmd =~ m#\G\s*(.*)#cgms) {
+ DB::system( $ENV{SHELL} || "/bin/sh", "-c", $1 );
+ next CMD;
+ }
+ }
+}
+
+sub _handle_x_command {
+ my $self = shift;
+
+ if ($DB::cmd =~ s#\Ax\b# #) { # Remainder gets done by DB::eval()
+ $onetimeDump = 'dump'; # main::dumpvar shows the output
+
+ # handle special "x 3 blah" syntax XXX propagate
+ # doc back to special variables.
+ if ( $DB::cmd =~ s#\A\s*(\d+)(?=\s)# #) {
+ $onetimedumpDepth = $1;
+ }
+ }
+
+ return;
+}
+
+sub _handle_q_command {
+ my $self = shift;
+
+ if ($DB::cmd eq 'q') {
+ $fall_off_end = 1;
+ DB::clean_ENV();
+ exit $?;
+ }
+
+ return;
+}
+
+sub _handle_cmd_wrapper_commands {
+ my $self = shift;
+
+ # All of these commands were remapped in perl 5.8.0;
+ # we send them off to the secondary dispatcher (see below).
+ if (my ($cmd_letter, $my_arg) = $DB::cmd =~ /\A([aAbBeEhilLMoOPvwW]\b|[<>\{]{1,2})\s*(.*)/so) {
+ DB::cmd_wrapper( $cmd_letter, $my_arg, $line );
+ next CMD;
+ }
+
+ return;
+}
package DB;
# The following code may be executed now:
# default to the older version of the command.
my $call = 'cmd_'
. ( $set{$CommandSet}{$cmd}
- || ( $cmd =~ /^[<>{]+/o ? 'prepost' : $cmd ) );
+ || ( $cmd =~ /\A[<>{]+/o ? 'prepost' : $cmd ) );
# Call the command subroutine, call it by name.
return __PACKAGE__->can($call)->( $cmd, $line, $dblineno );
$file = $file eq '-e' ? $file : "file '$file'" unless $short;
# Get the actual sub's name, and shorten to $maxtrace's requirement.
- $s = $sub[$i]{sub};
+ $s = $sub[$i]{'sub'};
$s = ( substr $s, 0, $maxtrace - 3 ) . '...' if length $s > $maxtrace;
# Short report uses trimmed file and sub names.
# We save, change, then restore STDIN and STDOUT to avoid fork() since
# some non-Unix systems can do system() but have problems with fork().
- open( SAVEIN, "<&STDIN" ) || &warn("Can't save STDIN");
- open( SAVEOUT, ">&STDOUT" ) || &warn("Can't save STDOUT");
- open( STDIN, "<&IN" ) || &warn("Can't redirect STDIN");
- open( STDOUT, ">&OUT" ) || &warn("Can't redirect STDOUT");
+ open( SAVEIN, "<&STDIN" ) || DB::warn("Can't save STDIN");
+ open( SAVEOUT, ">&STDOUT" ) || DB::warn("Can't save STDOUT");
+ open( STDIN, "<&IN" ) || DB::warn("Can't redirect STDIN");
+ open( STDOUT, ">&OUT" ) || DB::warn("Can't redirect STDOUT");
# XXX: using csh or tcsh destroys sigint retvals!
system(@_);
- open( STDIN, "<&SAVEIN" ) || &warn("Can't restore STDIN");
- open( STDOUT, ">&SAVEOUT" ) || &warn("Can't restore STDOUT");
+ open( STDIN, "<&SAVEIN" ) || DB::warn("Can't restore STDIN");
+ open( STDOUT, ">&SAVEOUT" ) || DB::warn("Can't restore STDOUT");
close(SAVEIN);
close(SAVEOUT);
# most of the $? crud was coping with broken cshisms
if ( $? >> 8 ) {
- &warn( "(Command exited ", ( $? >> 8 ), ")\n" );
+ DB::warn( "(Command exited ", ( $? >> 8 ), ")\n" );
}
elsif ($?) {
- &warn(
+ DB::warn(
"(Command died of SIG#",
( $? & 127 ),
( ( $? & 128 ) ? " -- core dumped" : "" ),