3 # Debugger for Perl 5.001m; perl5db.pl patch level:
5 $header = 'perl5db.pl patch level 0.93';
7 # Enhanced by ilya@math.ohio-state.edu (Ilya Zakharevich)
8 # Latest version available: ftp://ftp.math.ohio-state.edu/pub/users/ilya/perl
10 # modified Perl debugger, to be run from Emacs in perldb-mode
11 # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
12 # Johan Vromans -- upgrade to 4.0 pl 10
13 # Ilya Zakharevich -- patches after 5.001 (and some before ;-)
16 # This file is automatically included if you do perl -d.
17 # It's probably not useful to include this yourself.
19 # Perl supplies the values for @line and %sub. It effectively inserts
20 # a &DB'DB(<linenum>); in front of every place that can have a
21 # breakpoint. Instead of a subroutine call it calls &DB::sub with
22 # $DB::sub being the called subroutine. It also inserts a BEGIN
23 # {require 'perl5db.pl'} before the first line.
25 # Note that no subroutine call is possible until &DB::sub is defined
26 # (for subroutines defined outside this file). In fact the same is
27 # true if $deep is not defined.
32 # At start reads $rcfile that may set important options. This file
33 # may define a subroutine &afterinit that will be executed after the
34 # debugger is initialized.
36 # After $rcfile is read reads environment variable PERLDB_OPTS and parses
37 # it as a rest of `O ...' line in debugger prompt.
39 # The options that can be specified only at startup:
40 # [To set in $rcfile, call &parse_options("optionName=new_value").]
42 # TTY - the TTY to use for debugging i/o.
44 # noTTY - if set, goes in NonStop mode. On interrupt if TTY is not set
45 # uses the value of noTTY or "/tmp/perldbtty$$" to find TTY using
46 # Term::Rendezvous. Current variant is to have the name of TTY in this
49 # ReadLine - If false, dummy ReadLine is used, so you can debug
50 # ReadLine applications.
52 # NonStop - if true, no i/o is performed until interrupt.
54 # LineInfo - file or pipe to print line number info to. If it is a
55 # pipe, a short "emacs like" message is used.
57 # Example $rcfile: (delete leading hashes!)
59 # &parse_options("NonStop=1 LineInfo=db.out");
60 # sub afterinit { $trace = 1; }
62 # The script will run without human intervention, putting trace
63 # information into db.out. (If you interrupt it, you would better
64 # reset LineInfo to something "interactive"!)
72 $dumpvar::dumpDBFiles,
73 $dumpvar::dumpPackages,
74 $dumpvar::quoteHighBit,
83 # $prevwarn = $prevdie = $prevbus = $prevsegv = ''; # Does not help?!
85 $trace = $signal = $single = 0; # Uninitialized warning suppression
86 # (local $^W cannot help - other packages!).
89 $option{PrintRet} = 1;
91 @options = qw(hashDepth arrayDepth DumpDBFiles DumpPackages
92 compactDump veryCompact quote HighBit undefPrint
93 globPrint PrintRet UsageOnly frame
94 TTY noTTY ReadLine NonStop LineInfo
95 recallCommand ShellBang pager tkRunning
96 signalLevel warnLevel dieLevel);
99 hashDepth => \$dumpvar::hashDepth,
100 arrayDepth => \$dumpvar::arrayDepth,
101 DumpDBFiles => \$dumpvar::dumpDBFiles,
102 DumpPackages => \$dumpvar::dumpPackages,
103 HighBit => \$dumpvar::quoteHighBit,
104 undefPrint => \$dumpvar::printUndef,
105 globPrint => \$dumpvar::globPrint,
106 tkRunning => \$readline::Tk_toloop,
107 UsageOnly => \$dumpvar::usageOnly,
112 compactDump => \&dumpvar::compactDump,
113 veryCompact => \&dumpvar::veryCompact,
114 quote => \&dumpvar::quote,
117 ReadLine => \&ReadLine,
118 NonStop => \&NonStop,
119 LineInfo => \&LineInfo,
120 recallCommand => \&recallCommand,
121 ShellBang => \&shellBang,
123 signalLevel => \&signalLevel,
124 warnLevel => \&warnLevel,
125 dieLevel => \&dieLevel,
129 compactDump => 'dumpvar.pl',
130 veryCompact => 'dumpvar.pl',
131 quote => 'dumpvar.pl',
134 # These guys may be defined in $ENV{PERL5DB} :
135 $rl = 1 unless defined $rl;
136 warnLevel($warnLevel);
138 signalLevel($signalLevel);
139 &pager(defined($ENV{PAGER}) ? $ENV{PAGER} : "|more") unless defined $pager;
140 &recallCommand("!") unless defined $prc;
141 &shellBang("!") unless defined $psh;
146 $rcfile="perldb.ini";
151 } elsif (defined $ENV{LOGDIR} and -f "$ENV{LOGDIR}/$rcfile") {
152 do "$ENV{LOGDIR}/$rcfile";
153 } elsif (defined $ENV{HOME} and -f "$ENV{HOME}/$rcfile") {
154 do "$ENV{HOME}/$rcfile";
157 if (defined $ENV{PERLDB_OPTS}) {
158 parse_options($ENV{PERLDB_OPTS});
164 # Is Perl being run from Emacs?
165 $emacs = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
166 $rl = 0, shift(@main::ARGV) if $emacs;
168 #require Term::ReadLine;
171 $console = "/dev/tty";
175 $console = "sys\$command";
179 if (defined $ENV{OS2_SHELL} and $emacs) { # In OS/2
183 $console = $tty if defined $tty;
185 if (defined $console) {
186 open(IN,"+<$console") || open(IN,"<$console") || open(IN,"<&STDIN");
187 open(OUT,"+>$console") || open(OUT,">$console") || open(OUT,">&STDERR")
188 || open(OUT,">&STDOUT"); # so we don't dongle stdout
191 open(OUT,">&STDERR") || open(OUT,">&STDOUT"); # so we don't dongle stdout
192 $console = 'STDIN/OUT';
194 # so open("|more") can read from STDOUT and so we don't dingle stdin
199 $| = 1; # for DB::OUT
202 $LINEINFO = $OUT unless defined $LINEINFO;
203 $lineinfo = $console unless defined $lineinfo;
205 $| = 1; # for real STDOUT
207 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
208 unless ($runnonstop) {
209 print $OUT "\nLoading DB routines from $header\n";
210 print $OUT ("Emacs support ",
211 $emacs ? "enabled" : "available",
213 print $OUT "\nEnter h or `h h' for help.\n\n";
220 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
223 if (defined &afterinit) { # May be defined in $rcfile
227 ############################################################ Subroutines
229 # The following code may be executed now, but gives FPE later:
233 if ($runnonstop) { # Disable until signal
234 for ($i=0; $i <= $#stack; ) {
237 $single = $runnonstop = 0; # Once only
243 if ($option{PrintRet}) {
244 print $OUT "$retctx context return from $lastsub:",
245 ($retctx eq 'list') ? "\n" : " " ;
246 dumpit( ($retctx eq 'list') ? \@ret : $ret );
249 ($package, $filename, $line) = caller;
250 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
251 "package $package;"; # this won't let them modify, alas
252 local(*dbline) = "::_<$filename";
254 if (($stop,$action) = split(/\0/,$dbline{$line})) {
258 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
259 $dbline{$line} =~ s/;9($|\0)/$1/;
262 if ($single || $trace || $signal) {
265 print $LINEINFO "\032\032$filename:$line:0\n";
268 $prefix = $sub =~ /::/ ? "" : "${'package'}::";
269 $prefix .= "$sub($filename:";
270 $after = ($dbline[$line] =~ /\n$/ ? '' : "\n");
271 if (length($prefix) > 30) {
272 print $LINEINFO "$prefix$line):\n$line:\t", $dbline[$line], $after;
277 print $LINEINFO "$prefix$line$infix",$dbline[$line], $after;
279 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) { #{ vi
280 last if $dbline[$i] =~ /^\s*[\;\}\#\n]/;
281 $after = ($dbline[$i] =~ /\n$/ ? '' : "\n");
282 print $LINEINFO "$prefix$i$infix", $dbline[$i], $after;
286 $evalarg = $action, &eval if $action;
287 if ($single || $signal) {
288 local $level = $level + 1;
289 $evalarg = $pre, &eval if $pre;
290 print $OUT $#stack . " levels deep in subroutine calls!\n"
294 while (($term || &setterm),
295 defined ($cmd=$term->readline(" DB" . ('<' x $level) .
296 ($#hist+1) . ('>' x $level) .
298 { # <-- Do we know what this brace is for?
301 $cmd =~ s/\\$/\n/ && do {
302 $cmd .= $term->readline(" cont: ");
305 $cmd =~ /^q$/ && exit 0;
306 $cmd =~ /^$/ && ($cmd = $laststep);
307 push(@hist,$cmd) if length($cmd) > 1;
309 ($i) = split(/\s+/,$cmd);
310 eval "\$cmd =~ $alias{$i}", print $OUT $@ if $alias{$i};
311 $cmd =~ /^h$/ && do {
314 $cmd =~ /^h\s+h$/ && do {
317 $cmd =~ /^h\s+(\S)$/ && do {
319 if ($help =~ /^($asked([\s\S]*?)\n)(\Z|[^\s$asked])/m) {
322 print $OUT "`$asked' is not a debugger command.\n";
325 $cmd =~ /^t$/ && do {
327 print $OUT "Trace = ".($trace?"on":"off")."\n";
329 $cmd =~ /^S(\s+(!)?(.+))?$/ && do {
330 $Srev = defined $2; $Spatt = $3; $Snocheck = ! defined $1;
331 foreach $subname (sort(keys %sub)) {
332 if ($Snocheck or $Srev^($subname =~ /$Spatt/)) {
333 print $OUT $subname,"\n";
337 $cmd =~ s/^X\b/V $package/;
338 $cmd =~ /^V$/ && do {
339 $cmd = "V $package"; };
340 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
341 local ($savout) = select($OUT);
343 @vars = split(' ',$2);
344 do 'dumpvar.pl' unless defined &main::dumpvar;
345 if (defined &main::dumpvar) {
346 &main::dumpvar($packname,@vars);
348 print $OUT "dumpvar.pl not available.\n";
352 $cmd =~ s/^x\b/ / && do { # So that will be evaled
354 $cmd =~ /^f\b\s*(.*)/ && do {
357 print $OUT "The old f command is now the r command.\n";
358 print $OUT "The new f command switches filenames.\n";
361 if (!defined $main::{'_<' . $file}) {
362 if (($try) = grep(m#^_<.*$file#, keys %main::)) {{
363 $file = substr($try,2);
367 if (!defined $main::{'_<' . $file}) {
368 print $OUT "There's no code here matching $file.\n";
370 } elsif ($file ne $filename) {
371 *dbline = "::_<$file";
377 $cmd =~ /^l\b\s*([\':A-Za-z_][\':\w]*)/ && do {
379 $subname =~ s/\'/::/;
380 $subname = "main::".$subname unless $subname =~ /::/;
381 $subname = "main".$subname if substr($subname,0,2) eq "::";
382 @pieces = split(/:/,$sub{$subname});
383 $subrange = pop @pieces;
384 $file = join(':', @pieces);
385 if ($file ne $filename) {
386 *dbline = "::_<$file";
391 if (eval($subrange) < -$window) {
392 $subrange =~ s/-.*/+/;
394 $cmd = "l $subrange";
396 print $OUT "Subroutine $subname not found.\n";
399 $cmd =~ /^w\b\s*(\d*)$/ && do {
403 $cmd = 'l ' . $start . '-' . ($start + $incr); };
404 $cmd =~ /^-$/ && do {
406 $cmd = 'l ' . ($start-$window*2) . '+'; };
407 $cmd =~ /^l$/ && do {
409 $cmd = 'l ' . $start . '-' . ($start + $incr); };
410 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
413 $incr = $window - 1 unless $incr;
414 $cmd = 'l ' . $start . '-' . ($start + $incr); };
415 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
416 $end = (!$2) ? $max : ($4 ? $4 : $2);
417 $end = $max if $end > $max;
419 $i = $line if $i eq '.';
422 print $OUT "\032\032$filename:$i:0\n";
425 for (; $i <= $end; $i++) {
426 print $OUT "$i:\t", $dbline[$i];
430 $start = $i; # remember in case they want more
431 $start = $max if $start > $max;
433 $cmd =~ /^D$/ && do {
434 print $OUT "Deleting all breakpoints...\n";
435 for ($i = 1; $i <= $max ; $i++) {
436 if (defined $dbline{$i}) {
437 $dbline{$i} =~ s/^[^\0]+//;
438 if ($dbline{$i} =~ s/^\0?$//) {
444 $cmd =~ /^L$/ && do {
445 for ($i = 1; $i <= $max; $i++) {
446 if (defined $dbline{$i}) {
447 print $OUT "$i:\t", $dbline[$i];
448 ($stop,$action) = split(/\0/, $dbline{$i});
449 print $OUT " break if (", $stop, ")\n"
451 print $OUT " action: ", $action, "\n"
457 $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
460 $subname =~ s/\'/::/;
461 $subname = "${'package'}::" . $subname
462 unless $subname =~ /::/;
463 $subname = "main".$subname if substr($subname,0,2) eq "::";
464 # Filename below can contain ':'
465 ($file,$i) = ($sub{$subname} =~ /^(.*):(.*)$/);
469 *dbline = "::_<$filename";
471 ++$i while $dbline[$i] == 0 && $i < $max;
472 $dbline{$i} =~ s/^[^\0]*/$cond/;
474 print $OUT "Subroutine $subname not found.\n";
477 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
480 if ($dbline[$i] == 0) {
481 print $OUT "Line $i not breakable.\n";
483 $dbline{$i} =~ s/^[^\0]*/$cond/;
486 $cmd =~ /^d\b\s*(\d+)?/ && do {
488 $dbline{$i} =~ s/^[^\0]*//;
489 delete $dbline{$i} if $dbline{$i} eq '';
491 $cmd =~ /^A$/ && do {
492 for ($i = 1; $i <= $max ; $i++) {
493 if (defined $dbline{$i}) {
494 $dbline{$i} =~ s/\0[^\0]*//;
495 delete $dbline{$i} if $dbline{$i} eq '';
499 $cmd =~ /^O\s*$/ && do {
504 $cmd =~ /^O\s*(\S.*)/ && do {
507 $cmd =~ /^<\s*(.*)/ && do {
510 $cmd =~ /^>\s*(.*)/ && do {
513 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
515 if ($dbline[$i] == 0) {
516 print $OUT "Line $i may not have an action.\n";
518 $dbline{$i} =~ s/\0[^\0]*//;
519 $dbline{$i} .= "\0" . action($j);
522 $cmd =~ /^n$/ && do {
526 $cmd =~ /^s$/ && do {
530 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
533 if ($dbline[$i] == 0) {
534 print $OUT "Line $i not breakable.\n";
537 $dbline{$i} =~ s/($|\0)/;9$1/; # add one-time-only b.p.
539 for ($i=0; $i <= $#stack; ) {
543 $cmd =~ /^r$/ && do {
544 $stack[$#stack] |= 1;
547 $cmd =~ /^T$/ && do {
548 local($p,$f,$l,$s,$h,$a,$e,$r,@a,@sub);
550 ($p,$f,$l,$s,$h,$w,$e,$r) = caller($i);
557 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
558 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
559 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
562 $w = $w ? '@ = ' : '$ = ';
563 $a = $h ? '(' . join(', ', @a) . ')' : '';
564 $e =~ s/\n\s*\;\s*\Z// if $e;
565 $e =~ s/[\\\']/\\$1/g if $e;
568 } elsif (defined $r) {
570 } elsif ($s eq '(eval)') {
573 $f = "file `$f'" unless $f eq '-e';
574 push(@sub, "$w$s$a called from $f line $l\n");
577 for ($i=0; $i <= $#sub; $i++) {
582 $cmd =~ /^\/(.*)$/ && do {
584 $inpat =~ s:([^\\])/$:$1:;
586 eval '$inpat =~ m'."\a$inpat\a";
597 $start = 1 if ($start > $max);
598 last if ($start == $end);
599 if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
601 print $OUT "\032\032$filename:$start:0\n";
603 print $OUT "$start:\t", $dbline[$start], "\n";
608 print $OUT "/$pat/: not found\n" if ($start == $end);
610 $cmd =~ /^\?(.*)$/ && do {
612 $inpat =~ s:([^\\])\?$:$1:;
614 eval '$inpat =~ m'."\a$inpat\a";
625 $start = $max if ($start <= 0);
626 last if ($start == $end);
627 if ($dbline[$start] =~ m' . "\a$pat\a" . 'i) {
629 print $OUT "\032\032$filename:$start:0\n";
631 print $OUT "$start:\t", $dbline[$start], "\n";
636 print $OUT "?$pat?: not found\n" if ($start == $end);
638 $cmd =~ /^$rc+\s*(-)?(\d+)?$/ && do {
639 pop(@hist) if length($cmd) > 1;
640 $i = $1 ? ($#hist-($2?$2:1)) : ($2?$2:$#hist);
641 $cmd = $hist[$i] . "\n";
644 $cmd =~ /^$sh$sh\s*/ && do {
647 $cmd =~ /^$rc([^$rc].*)$/ && do {
649 pop(@hist) if length($cmd) > 1;
650 for ($i = $#hist; $i; --$i) {
651 last if $hist[$i] =~ /$pat/;
654 print $OUT "No such command!\n\n";
657 $cmd = $hist[$i] . "\n";
660 $cmd =~ /^$sh$/ && do {
661 &system($ENV{SHELL}||"/bin/sh");
663 $cmd =~ /^$sh\s*/ && do {
664 &system($ENV{SHELL}||"/bin/sh","-c",$');
666 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
667 $end = $2?($#hist-$2):0;
668 $hist = 0 if $hist < 0;
669 for ($i=$#hist; $i>$end; $i--) {
670 print $OUT "$i: ",$hist[$i],"\n"
671 unless $hist[$i] =~ /^.?$/;
674 $cmd =~ s/^p$/print \$DB::OUT \$_/;
675 $cmd =~ s/^p\b/print \$DB::OUT /;
677 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
678 $alias{$k}="s~$k~$v~";
679 print $OUT "$k = $v\n";
680 } elsif ($cmd =~ /^=\s*$/) {
681 foreach $k (sort keys(%alias)) {
682 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
683 print $OUT "$k = $v\n";
685 print $OUT "$k\t$alias{$k}\n";
690 $cmd =~ /^\|\|?\s*[^|]/ && do {
691 if ($pager =~ /^\|/) {
692 open(SAVEOUT,">&STDOUT") || &warn("Can't save STDOUT");
693 open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
695 open(SAVEOUT,">&OUT") || &warn("Can't save DB::OUT");
697 unless ($piped=open(OUT,$pager)) {
698 &warn("Can't pipe output to `$pager'");
699 if ($pager =~ /^\|/) {
700 open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
701 open(STDOUT,">&SAVEOUT")
702 || &warn("Can't restore STDOUT");
705 open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
709 $SIG{PIPE}= "DB::catch" if $pager =~ /^\|/
710 && "" eq $SIG{PIPE} || "DEFAULT" eq $SIG{PIPE};
711 $selected= select(OUT);
713 select( $selected ), $selected= "" unless $cmd =~ /^\|\|/;
716 # XXX Local variants do not work!
717 $cmd =~ s/^t\s/\$DB::trace = 1;\n/;
718 $cmd =~ s/^s\s/\$DB::single = 1;\n/ && do {$laststep = 's'};
719 $cmd =~ s/^n\s/\$DB::single = 2;\n/ && do {$laststep = 'n'};
721 } # <-- Do we know what this brace is for?
722 $evalarg = "\$^D = \$^D | \$DB::db_stop;\n$cmd"; &eval;
724 $onetimeDump = undef;
730 if ($pager =~ /^\|/) {
731 $?= 0; close(OUT) || &warn("Can't close DB::OUT");
732 &warn( "Pager `$pager' failed: ",
733 ($?>>8) > 128 ? ($?>>8)-256 : ($?>>8),
734 ( $? & 128 ) ? " (core dumped)" : "",
735 ( $? & 127 ) ? " (SIG ".($?&127).")" : "", "\n" ) if $?;
736 open(OUT,">&STDOUT") || &warn("Can't restore DB::OUT");
737 open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
738 $SIG{PIPE}= "DEFAULT" if $SIG{PIPE} eq "DB::catch";
739 # Will stop ignoring SIGPIPE if done like nohup(1)
740 # does SIGINT but Perl doesn't give us a choice.
742 open(OUT,">&SAVEOUT") || &warn("Can't restore DB::OUT");
745 select($selected), $selected= "" unless $selected eq "";
750 $evalarg = $post; &eval;
752 } # if ($single || $signal)
753 ($@, $!, $,, $/, $\, $^W) = @saved;
757 # The following code may be executed now:
761 print $LINEINFO ' ' x $#stack, "entering $sub\n" if $frame;
762 push(@stack, $single);
764 $single |= 4 if $#stack == $deep;
767 $single |= pop(@stack);
770 print $LINEINFO ' ' x $#stack, "exited $sub\n" if $frame;
774 $single |= pop(@stack);
777 print $LINEINFO ' ' x $#stack, "exited $sub\n" if $frame;
783 @saved = ($@, $!, $,, $/, $\, $^W);
784 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
787 # The following takes its argument via $evalarg to preserve current @_
792 local (@stack) = @stack; # guard against recursive debugging
794 my $osingle = $single;
796 @res = eval "$usercontext $evalarg;\n"; # '\n' for nice recursive debug
805 } elsif ($onetimeDump) {
811 local ($savout) = select($OUT);
812 do 'dumpvar.pl' unless defined &main::dumpValue;
813 if (defined &main::dumpValue) {
814 &main::dumpValue(shift);
816 print $OUT "dumpvar.pl not available.\n";
823 while ($action =~ s/\\$//) {
838 # We save, change, then restore STDIN and STDOUT to avoid fork() since
839 # many non-Unix systems can do system() but have problems with fork().
840 open(SAVEIN,"<&STDIN") || &warn("Can't save STDIN");
841 open(SAVEOUT,">&OUT") || &warn("Can't save STDOUT");
842 open(STDIN,"<&IN") || &warn("Can't redirect STDIN");
843 open(STDOUT,">&OUT") || &warn("Can't redirect STDOUT");
845 open(STDIN,"<&SAVEIN") || &warn("Can't restore STDIN");
846 open(STDOUT,">&SAVEOUT") || &warn("Can't restore STDOUT");
847 close(SAVEIN); close(SAVEOUT);
848 &warn( "(Command returned ", ($?>>8) > 128 ? ($?>>8)-256 : ($?>>8), ")",
849 ( $? & 128 ) ? " (core dumped)" : "",
850 ( $? & 127 ) ? " (SIG ".($?&127).")" : "", "\n" ) if $?;
855 eval "require Term::ReadLine;" or die $@;
858 open(IN,"<$tty") or die "Cannot open TTY `$TTY' for read: $!";
859 open(OUT,">$tty") or die "Cannot open TTY `$TTY' for write: $!";
862 my $sel = select($OUT);
866 eval "require Term::Rendezvous;" or die $@;
867 my $rv = $ENV{PERLDB_NOTTY} || "/tmp/perldbtty$$";
868 my $term_rv = new Term::Rendezvous $rv;
870 $OUT = $term_rv->OUT;
874 $term = new Term::ReadLine::Stub 'perldb', $IN, $OUT;
876 $term = new Term::ReadLine 'perldb', $IN, $OUT;
878 $readline::rl_basic_word_break_characters .= "[:"
879 if defined $readline::rl_basic_word_break_characters
880 and index($readline::rl_basic_word_break_characters, ":") == -1;
882 $LINEINFO = $OUT unless defined $LINEINFO;
883 $lineinfo = $console unless defined $lineinfo;
894 if (defined $optionVars{$opt}
895 and defined $ {$optionVars{$opt}}) {
896 $val = $ {$optionVars{$opt}};
897 } elsif (defined $optionAction{$opt}
898 and defined &{$optionAction{$opt}}) {
899 $val = &{$optionAction{$opt}}();
900 } elsif (defined $optionAction{$opt}
901 and not defined $option{$opt}
902 or defined $optionVars{$opt}
903 and not defined $ {$optionVars{$opt}}) {
906 $val = $option{$opt};
908 $val =~ s/[\\\']/\\$&/g;
909 printf $OUT "%20s = '%s'\n", $opt, $val;
915 s/^(\w+)(\s*$|\W)// or print($OUT "Invalid option `$_'\n"), last;
916 my ($opt,$sep) = ($1,$2);
919 print($OUT "Option query `$opt?' followed by non-space `$_'\n"), last
922 } elsif ($sep !~ /\S/) {
924 } elsif ($sep eq "=") {
927 } else { #{ to "let some poor schmuck bounce on the % key in B<vi>."
928 my ($end) = "\\" . substr( ")]>}$sep", index("([<{",$sep), 1 ); #}
929 s/^(([^\\$end]|\\[\\$end])*)$end($|\s+)// or
930 print($OUT "Unclosed option value `$opt$sep$_'\n"), last;
932 $val =~ s/\\([\\$end])/$1/g;
936 grep( /^\Q$opt/ && ($option = $_), @options );
937 $matches = grep( /^\Q$opt/i && ($option = $_), @options )
939 print $OUT "Unknown option `$opt'\n" unless $matches;
940 print $OUT "Ambiguous option `$opt'\n" if $matches > 1;
941 $option{$option} = $val if $matches == 1 and defined $val;
942 eval "require '$optionRequire{$option}'"
943 if $matches == 1 and defined $optionRequire{$option} and defined $val;
944 $ {$optionVars{$option}} = $val
946 and defined $optionVars{$option} and defined $val;
947 & {$optionAction{$option}} ($val)
949 and defined $optionAction{$option}
950 and defined &{$optionAction{$option}} and defined $val;
951 &dump_option($option) if $matches == 1 && $OUT ne \*STDERR; # Not $rcfile
961 my($msg)= join("",@_);
962 $msg .= ": $!\n" unless $msg =~ /\n$/;
968 &warn("Too late to set TTY!\n") if @_;
977 &warn("Too late to set noTTY!\n") if @_;
979 $notty = shift if @_;
986 &warn("Too late to set ReadLine!\n") if @_;
995 &warn("Too late to set up NonStop mode!\n") if @_;
997 $runnonstop = shift if @_;
1005 $pager="|".$pager unless $pager =~ /^(\+?\>|\|)/;
1012 $sh = quotemeta shift;
1013 $sh .= "\\b" if $sh =~ /\w$/;
1017 $psh =~ s/\\(.)/$1/g;
1024 $rc = quotemeta shift;
1025 $rc .= "\\b" if $rc =~ /\w$/;
1029 $prc =~ s/\\(.)/$1/g;
1035 return $lineinfo unless @_;
1037 my $stream = ($lineinfo =~ /^(\+?\>|\|)/) ? $lineinfo : ">$lineinfo";
1038 $emacs = ($stream =~ /^\|/);
1039 open(LINEINFO, "$stream") || &warn("Cannot open `$stream' for write");
1040 $LINEINFO = \*LINEINFO;
1041 my $save = select($LINEINFO);
1050 s [expr] Single step [in expr].
1051 n [expr] Next, steps over subroutine calls [in expr].
1052 <CR> Repeat last n or s command.
1053 r Return from current subroutine.
1054 c [line] Continue; optionally inserts a one-time-only breakpoint
1055 at the specified line.
1056 l min+incr List incr+1 lines starting at min.
1057 l min-max List lines min through max.
1058 l line List single line.
1059 l subname List first window of lines from subroutine.
1060 l List next window of lines.
1061 - List previous window of lines.
1062 w [line] List window around line.
1063 f filename Switch to viewing filename.
1064 /pattern/ Search forwards for pattern; final / is optional.
1065 ?pattern? Search backwards for pattern; final ? is optional.
1066 L List all breakpoints and actions.
1067 S [[!]pattern] List subroutine names [not] matching pattern.
1068 t Toggle trace mode.
1069 t expr Trace through execution of expr.
1070 b [line] [condition]
1071 Set breakpoint; line defaults to the current execution line;
1072 condition breaks if it evaluates to true, defaults to '1'.
1073 b subname [condition]
1074 Set breakpoint at first line of subroutine.
1075 d [line] Delete the breakpoint for line.
1076 D Delete all breakpoints.
1078 Set an action to be done before the line is executed.
1079 Sequence is: check for breakpoint, print line if necessary,
1080 do action, prompt user if breakpoint or step, evaluate line.
1081 A Delete all actions.
1082 V [pkg [vars]] List some (default all) variables in package (default current).
1083 Use ~pattern and !pattern for positive and negative regexps.
1084 X [vars] Same as \"V currentpackage [vars]\".
1085 x expr Evals expression in array context, dumps the result.
1086 O [opt[=val]] [opt\"val\"] [opt?]...
1087 Set or query values of options. val defaults to 1. opt can
1088 be abbreviated. Several options can be listed.
1089 recallCommand, ShellBang: chars used to recall command or spawn shell;
1090 pager: program for output of \"|cmd\";
1091 The following options affect what happens with V, X, and x commands:
1092 arrayDepth, hashDepth: print only first N elements ('' for all);
1093 compactDump, veryCompact: change style of array and hash dump;
1094 globPrint: whether to print contents of globs;
1095 DumpDBFiles: dump arrays holding debugged files;
1096 DumpPackages: dump symbol tables of packages;
1097 quote, HighBit, undefPrint: change style of string dump;
1098 tkRunning: run Tk while prompting (with ReadLine);
1099 signalLevel warnLevel dieLevel: level of verbosity;
1100 Option PrintRet affects printing of return value after r command,
1101 frame affects printing messages on entry and exit from subroutines.
1102 During startup options are initialized from \$ENV{PERLDB_OPTS}.
1103 You can put additional initialization options TTY, noTTY,
1104 ReadLine, and NonStop there.
1105 < command Define command to run before each prompt.
1106 > command Define command to run after each prompt.
1107 $prc number Redo a previous command (default previous command).
1108 $prc -number Redo number'th-to-last command.
1109 $prc pattern Redo last command that started with pattern.
1110 See 'O recallCommand' too.
1111 $psh$psh cmd Run cmd in a subprocess (reads from DB::IN, writes to DB::OUT)"
1112 . ( $rc eq $sh ? "" : "
1113 $psh [cmd] Run cmd in subshell (forces \"\$SHELL -c 'cmd'\")." ) . "
1114 See 'O shellBang' too.
1115 H -number Display last number commands (default all).
1116 p expr Same as \"print DB::OUT expr\" in current package.
1117 |dbcmd Run debugger command, piping DB::OUT to current pager.
1118 ||dbcmd Same as |dbcmd but DB::OUT is temporarilly select()ed as well.
1119 \= [alias value] Define a command alias, or list current aliases.
1120 command Execute as a perl statement in current package.
1121 h [db_command] Get help [on a specific debugger command], enter |h to page.
1122 h h Summary of debugger commands.
1126 $summary = <<"END_SUM";
1127 List/search source lines: Control script execution:
1128 l [ln|sub] List source code T Stack trace
1129 - List previous lines s [expr] Single step [in expr]
1130 w [line] List around line n [expr] Next, steps over subs
1131 f filename View source in file <CR> Repeat last n or s
1132 /pattern/ Search forward r Return from subroutine
1133 ?pattern? Search backward c [line] Continue until line
1134 Debugger controls: L List break pts & actions
1135 O [...] Set debugger options t [expr] Toggle trace [trace expr]
1136 < command Command for before prompt b [ln] [c] Set breakpoint
1137 > command Command for after prompt b sub [c] Set breakpoint for sub
1138 $prc [N|pat] Redo a previous command d [line] Delete a breakpoint
1139 H [-num] Display last num commands D Delete all breakpoints
1140 = [a val] Define/list an alias a [ln] cmd Do cmd before line
1141 h [db_cmd] Get help on command A Delete all actions
1142 |[|]dbcmd Send output to pager $psh\[$psh\] syscmd Run cmd in a subprocess
1144 Data Examination: expr Execute perl code, also see: s,n,t expr
1145 S [[!]pat] List subroutine names [not] matching pattern
1146 V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
1147 X [Vars] Same as \"V current_package [Vars]\".
1148 x expr Evals expression in array context, dumps the result.
1149 p expr Print expression (uses script's current package).
1151 # '); # Fix balance of Emacs parsing
1156 $SIG{'ABRT'} = DEFAULT;
1157 kill 'ABRT', $$ if $panic++;
1158 print $DB::OUT "Got $_[0]!\n"; # in the case cannot continue
1159 local $SIG{__WARN__} = '';
1161 local $Carp::CarpLevel = 2; # mydie + confess
1162 &warn(Carp::longmess("Signal @_"));
1167 local $SIG{__WARN__} = '';
1169 #&warn("Entering dbwarn\n");
1170 my ($mysingle,$mytrace) = ($single,$trace);
1171 $single = 0; $trace = 0;
1172 my $mess = Carp::longmess(@_);
1173 ($single,$trace) = ($mysingle,$mytrace);
1174 #&warn("Warning in dbwarn\n");
1176 #&warn("Exiting dbwarn\n");
1180 local $SIG{__DIE__} = '';
1181 local $SIG{__WARN__} = '';
1182 my $i = 0; my $ineval = 0; my $sub;
1183 #&warn("Entering dbdie\n");
1184 if ($dieLevel != 2) {
1185 while ((undef,undef,undef,$sub) = caller(++$i)) {
1186 $ineval = 1, last if $sub eq '(eval)';
1189 local $SIG{__WARN__} = \&dbwarn;
1190 &warn(@_) if $dieLevel > 2; # Ineval is false during destruction?
1192 #&warn("dieing quietly in dbdie\n") if $ineval and $dieLevel < 2;
1193 die @_ if $ineval and $dieLevel < 2;
1196 # We do not want to debug this chunk (automatic disabling works
1197 # inside DB::DB, but not in Carp).
1198 my ($mysingle,$mytrace) = ($single,$trace);
1199 $single = 0; $trace = 0;
1200 my $mess = Carp::longmess(@_);
1201 ($single,$trace) = ($mysingle,$mytrace);
1202 #&warn("dieing loudly in dbdie\n");
1206 # sub diehard { # Always dump, useful if fatal is
1207 # # deeply in evals.
1208 # local $SIG{__DIE__} = '';
1210 # # We do not want to debug this (automatic disabling works inside DB::DB)
1211 # my ($mysingle,$mytrace) = ($single,$trace);
1212 # $single = 0; $trace = 0;
1213 # my $mess = Carp::longmess(@_);
1214 # ($single,$trace) = ($mysingle,$mytrace);
1220 $prevwarn = $SIG{__WARN__} unless $warnLevel;
1223 $SIG{__WARN__} = 'DB::dbwarn';
1225 $SIG{__WARN__} = $prevwarn;
1233 $prevdie = $SIG{__DIE__} unless $dieLevel;
1236 $SIG{__DIE__} = 'DB::dbdie'; # if $dieLevel < 2;
1237 #$SIG{__DIE__} = 'DB::diehard' if $dieLevel >= 2;
1238 print $OUT "Stack dump during die enabled",
1239 ( $dieLevel == 1 ? " outside of evals" : ""), ".\n";
1240 print $OUT "Dump printed too.\n" if $dieLevel > 2;
1242 $SIG{__DIE__} = $prevdie;
1243 print $OUT "Default die handler restored.\n";
1251 $prevsegv = $SIG{SEGV} unless $signalLevel;
1252 $prevbus = $SIG{BUS} unless $signalLevel;
1253 $signalLevel = shift;
1255 $SIG{SEGV} = 'DB::diesignal';
1256 $SIG{BUS} = 'DB::diesignal';
1258 $SIG{SEGV} = $prevsegv;
1259 $SIG{BUS} = $prevbus;
1265 # The following BEGIN is very handy if debugger goes havoc, debugging debugger?
1267 BEGIN { # This does not compile, alas.
1268 $IN = \*STDIN; # For bugs before DB::OUT has been opened
1269 $OUT = \*STDERR; # For errors before DB::OUT has been opened
1273 $deep = 100; # warning if stack gets this deep
1277 #$SIG{__WARN__} = "DB::dbwarn";
1278 #$SIG{__DIE__} = 'DB::dbdie';
1279 #$SIG{SEGV} = "DB::diesignal";
1280 #$SIG{BUS} = "DB::diesignal";
1281 $SIG{INT} = "DB::catch";
1282 #$SIG{FPE} = "DB::catch";
1283 #warn "SIGFPE installed";
1284 $warnLevel = 1 unless defined $warnLevel;
1285 $dieLevel = 1 unless defined $dieLevel;
1286 $signalLevel = 1 unless defined $signalLevel;
1288 $db_stop = 0; # Compiler warning
1290 $level = 0; # Level of recursive debugging
1293 #use Carp; # This did break, left for debuggin
1298 # modified Perl debugger, to be run from Emacs in perldb-mode
1299 # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
1300 # Johan Vromans -- upgrade to 4.0 pl 10
1302 $header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
1304 # This file is automatically included if you do perl -d.
1305 # It's probably not useful to include this yourself.
1307 # Perl supplies the values for @line and %sub. It effectively inserts
1308 # a &DB'DB(<linenum>); in front of every place that can
1309 # have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
1311 # $Log: perldb.pl,v $
1313 # Is Perl being run from Emacs?
1314 $emacs = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
1315 shift(@main::ARGV) if $emacs;
1317 #require Term::ReadLine;
1321 if (-e "/dev/tty") {
1322 $console = "/dev/tty";
1327 $rcfile="perldb.ini";
1330 $console = "sys\$command";
1331 $rcfile="perldb.ini";
1335 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
1339 $console = "/dev/con";
1343 open(IN, "<$console") || open(IN, "<&STDIN"); # so we don't dingle stdin
1344 open(OUT,">$console") || open(OUT, ">&STDERR")
1345 || open(OUT, ">&STDOUT"); # so we don't dongle stdout
1347 $| = 1; # for DB::OUT
1349 $| = 1; # for real STDOUT
1352 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
1353 print OUT "\nLoading DB routines from $header\n";
1354 print OUT ("Emacs support ",
1355 $emacs ? "enabled" : "available",
1357 print OUT "\nEnter h for help.\n\n";
1363 ($pkg, $filename, $line) = caller;
1364 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
1365 "package $pkg;"; # this won't let them modify, alas
1366 local(*dbline) = "::_<$filename";
1368 if (($stop,$action) = split(/\0/,$dbline{$line})) {
1373 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
1374 $dbline{$line} =~ s/;9($|\0)/$1/;
1377 if ($single || $trace || $signal) {
1379 print OUT "\032\032$filename:$line:0\n";
1381 $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
1382 $prefix .= "$sub($filename:";
1383 if (length($prefix) > 30) {
1384 print OUT "$prefix$line):\n$line:\t",$dbline[$line];
1390 print OUT "$prefix$line$infix",$dbline[$line];
1392 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
1393 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
1394 print OUT "$prefix$i$infix",$dbline[$i];
1398 $evalarg = $action, &eval if $action;
1399 if ($single || $signal) {
1400 $evalarg = $pre, &eval if $pre;
1401 print OUT $#stack . " levels deep in subroutine calls!\n"
1405 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
1409 $cmd eq '' && exit 0;
1411 $cmd =~ s/\\$// && do {
1412 print OUT " cont: ";
1416 $cmd =~ /^q$/ && exit 0;
1417 $cmd =~ /^$/ && ($cmd = $laststep);
1418 push(@hist,$cmd) if length($cmd) > 1;
1419 ($i) = split(/\s+/,$cmd);
1420 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
1421 $cmd =~ /^h$/ && do {
1425 n Next, steps over subroutine calls.
1426 r Return from current subroutine.
1427 c [line] Continue; optionally inserts a one-time-only breakpoint
1428 at the specified line.
1429 <CR> Repeat last n or s.
1430 l min+incr List incr+1 lines starting at min.
1431 l min-max List lines.
1434 - List previous window.
1435 w line List window around line.
1436 l subname List subroutine.
1437 f filename Switch to filename.
1438 /pattern/ Search forwards for pattern; final / is optional.
1439 ?pattern? Search backwards for pattern.
1440 L List breakpoints and actions.
1441 S List subroutine names.
1442 t Toggle trace mode.
1443 b [line] [condition]
1444 Set breakpoint; line defaults to the current execution line;
1445 condition breaks if it evaluates to true, defaults to \'1\'.
1446 b subname [condition]
1447 Set breakpoint at first line of subroutine.
1448 d [line] Delete breakpoint.
1449 D Delete all breakpoints.
1451 Set an action to be done before the line is executed.
1452 Sequence is: check for breakpoint, print line if necessary,
1453 do action, prompt user if breakpoint or step, evaluate line.
1454 A Delete all actions.
1455 V [pkg [vars]] List some (default all) variables in package (default current).
1456 X [vars] Same as \"V currentpackage [vars]\".
1457 < command Define command before prompt.
1458 > command Define command after prompt.
1459 ! number Redo command (default previous command).
1460 ! -number Redo number\'th to last command.
1461 H -number Display last number commands (default all).
1463 p expr Same as \"print DB::OUT expr\" in current package.
1464 = [alias value] Define a command alias, or list current aliases.
1465 command Execute as a perl statement in current package.
1469 $cmd =~ /^t$/ && do {
1471 print OUT "Trace = ".($trace?"on":"off")."\n";
1473 $cmd =~ /^S$/ && do {
1474 foreach $subname (sort(keys %sub)) {
1475 print OUT $subname,"\n";
1478 $cmd =~ s/^X\b/V $pkg/;
1479 $cmd =~ /^V$/ && do {
1481 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
1482 local ($savout) = select(OUT);
1484 @vars = split(' ',$2);
1485 do 'dumpvar.pl' unless defined &main::dumpvar;
1486 if (defined &main::dumpvar) {
1487 &main::dumpvar($packname,@vars);
1490 print DB::OUT "dumpvar.pl not available.\n";
1494 $cmd =~ /^f\b\s*(.*)/ && do {
1497 print OUT "The old f command is now the r command.\n";
1498 print OUT "The new f command switches filenames.\n";
1501 if (!defined $main::{'_<' . $file}) {
1502 if (($try) = grep(m#^_<.*$file#, keys %main::)) {
1503 $file = substr($try,2);
1507 if (!defined $main::{'_<' . $file}) {
1508 print OUT "There's no code here anything matching $file.\n";
1511 elsif ($file ne $filename) {
1512 *dbline = "::_<$file";
1518 $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
1520 $subname = "main::" . $subname unless $subname =~ /'|::/;
1521 $subname = "main" . $subname if substr($subname,0,1)eq "'";
1522 $subname = "main" . $subname if substr($subname,0,2)eq "::";
1523 # VMS filespecs may (usually do) contain ':', so don't use split
1524 ($file,$subrange) = $sub{$subname} =~ /(.*):(.*)/;
1525 if ($file ne $filename) {
1526 *dbline = "::_<$file";
1531 if (eval($subrange) < -$window) {
1532 $subrange =~ s/-.*/+/;
1534 $cmd = "l $subrange";
1536 print OUT "Subroutine $1 not found.\n";
1539 $cmd =~ /^w\b\s*(\d*)$/ && do {
1540 $incr = $window - 1;
1543 $cmd = 'l ' . $start . '-' . ($start + $incr); };
1544 $cmd =~ /^-$/ && do {
1545 $incr = $window - 1;
1546 $cmd = 'l ' . ($start-$window*2) . '+'; };
1547 $cmd =~ /^l$/ && do {
1548 $incr = $window - 1;
1549 $cmd = 'l ' . $start . '-' . ($start + $incr); };
1550 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
1553 $incr = $window - 1 unless $incr;
1554 $cmd = 'l ' . $start . '-' . ($start + $incr); };
1555 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
1556 $end = (!$2) ? $max : ($4 ? $4 : $2);
1557 $end = $max if $end > $max;
1559 $i = $line if $i eq '.';
1562 print OUT "\032\032$filename:$i:0\n";
1565 for (; $i <= $end; $i++) {
1566 print OUT "$i:\t", $dbline[$i];
1570 $start = $i; # remember in case they want more
1571 $start = $max if $start > $max;
1573 $cmd =~ /^D$/ && do {
1574 print OUT "Deleting all breakpoints...\n";
1575 for ($i = 1; $i <= $max ; $i++) {
1576 if (defined $dbline{$i}) {
1577 $dbline{$i} =~ s/^[^\0]+//;
1578 if ($dbline{$i} =~ s/^\0?$//) {
1584 $cmd =~ /^L$/ && do {
1585 for ($i = 1; $i <= $max; $i++) {
1586 if (defined $dbline{$i}) {
1587 print OUT "$i:\t", $dbline[$i];
1588 ($stop,$action) = split(/\0/, $dbline{$i});
1589 print OUT " break if (", $stop, ")\n"
1591 print OUT " action: ", $action, "\n"
1597 $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
1600 $subname = "${pkg}::" . $subname
1601 unless $subname =~ /'|::/;
1602 $subname = "main" . $subname if substr($subname,0,1) eq "'";
1603 $subname = "main" . $subname if substr($subname,0,2) eq "::";
1604 # VMS filespecs may (usually do) contain ':', so don't use split
1605 ($filename,$i) = $sub{$subname} =~ /(.*):(.*)/;
1608 *dbline = "::_<$filename";
1609 ++$i while $dbline[$i] == 0 && $i < $#dbline;
1610 $dbline{$i} =~ s/^[^\0]*/$cond/;
1612 print OUT "Subroutine $subname not found.\n";
1615 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
1618 if ($dbline[$i] == 0) {
1619 print OUT "Line $i not breakable.\n";
1621 $dbline{$i} =~ s/^[^\0]*/$cond/;
1624 $cmd =~ /^d\b\s*(\d+)?/ && do {
1626 $dbline{$i} =~ s/^[^\0]*//;
1627 delete $dbline{$i} if $dbline{$i} eq '';
1629 $cmd =~ /^A$/ && do {
1630 for ($i = 1; $i <= $max ; $i++) {
1631 if (defined $dbline{$i}) {
1632 $dbline{$i} =~ s/\0[^\0]*//;
1633 delete $dbline{$i} if $dbline{$i} eq '';
1637 $cmd =~ /^<\s*(.*)/ && do {
1640 $cmd =~ /^>\s*(.*)/ && do {
1643 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
1645 if ($dbline[$i] == 0) {
1646 print OUT "Line $i may not have an action.\n";
1648 $dbline{$i} =~ s/\0[^\0]*//;
1649 $dbline{$i} .= "\0" . action($3);
1652 $cmd =~ /^n$/ && do {
1656 $cmd =~ /^s$/ && do {
1660 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
1663 if ($dbline[$i] == 0) {
1664 print OUT "Line $i not breakable.\n";
1667 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
1669 for ($i=0; $i <= $#stack; ) {
1673 $cmd =~ /^r$/ && do {
1674 $stack[$#stack] |= 2;
1676 $cmd =~ /^T$/ && do {
1677 local($p,$f,$l,$s,$h,$a,@a,@sub);
1678 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
1684 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
1685 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
1686 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
1689 $w = $w ? '@ = ' : '$ = ';
1690 $a = $h ? '(' . join(', ', @a) . ')' : '';
1691 push(@sub, "$w$s$a from file $f line $l\n");
1694 for ($i=0; $i <= $#sub; $i++) {
1699 $cmd =~ /^\/(.*)$/ && do {
1701 $inpat =~ s:([^\\])/$:$1:;
1703 eval '$inpat =~ m'."\a$inpat\a";
1714 $start = 1 if ($start > $max);
1715 last if ($start == $end);
1716 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
1718 print OUT "\032\032$filename:$start:0\n";
1720 print OUT "$start:\t", $dbline[$start], "\n";
1725 print OUT "/$pat/: not found\n" if ($start == $end);
1727 $cmd =~ /^\?(.*)$/ && do {
1729 $inpat =~ s:([^\\])\?$:$1:;
1731 eval '$inpat =~ m'."\a$inpat\a";
1742 $start = $max if ($start <= 0);
1743 last if ($start == $end);
1744 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
1746 print OUT "\032\032$filename:$start:0\n";
1748 print OUT "$start:\t", $dbline[$start], "\n";
1753 print OUT "?$pat?: not found\n" if ($start == $end);
1755 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
1756 pop(@hist) if length($cmd) > 1;
1757 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
1758 $cmd = $hist[$i] . "\n";
1761 $cmd =~ /^!(.+)$/ && do {
1763 pop(@hist) if length($cmd) > 1;
1764 for ($i = $#hist; $i; --$i) {
1765 last if $hist[$i] =~ $pat;
1768 print OUT "No such command!\n\n";
1771 $cmd = $hist[$i] . "\n";
1774 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
1775 $end = $2?($#hist-$2):0;
1776 $hist = 0 if $hist < 0;
1777 for ($i=$#hist; $i>$end; $i--) {
1778 print OUT "$i: ",$hist[$i],"\n"
1779 unless $hist[$i] =~ /^.?$/;
1782 $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
1783 $cmd =~ /^=/ && do {
1784 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
1785 $alias{$k}="s~$k~$v~";
1786 print OUT "$k = $v\n";
1787 } elsif ($cmd =~ /^=\s*$/) {
1788 foreach $k (sort keys(%alias)) {
1789 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
1790 print OUT "$k = $v\n";
1792 print OUT "$k\t$alias{$k}\n";
1798 $evalarg = $cmd; &eval;
1802 $evalarg = $post; &eval;
1805 ($@, $!, $,, $/, $\, $^W) = @saved;
1810 @saved = ($@, $!, $,, $/, $\, $^W);
1811 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
1814 # The following takes its argument via $evalarg to preserve current @_
1817 eval "$usercontext $evalarg; &DB::save";
1822 local($action) = @_;
1823 while ($action =~ s/\\$//) {
1840 push(@stack, $single);
1842 $single |= 4 if $#stack == $deep;
1845 $single |= pop(@stack);
1850 $single |= pop(@stack);
1855 $trace = $signal = $single = 0; # uninitialized warning suppression
1858 $SIG{'INT'} = "DB::catch";
1859 $deep = 100; # warning if stack gets this deep
1867 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
1873 elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
1874 do "$ENV{'LOGDIR'}/$rcfile";
1876 elsif (-f "$ENV{'HOME'}/$rcfile") {
1877 do "$ENV{'HOME'}/$rcfile";