3 $header = '$Header: perldb.pl,v 3.0.1.2 90/03/12 16:39:39 lwall Locked $';
5 # This file is automatically included if you do perl -d.
6 # It's probably not useful to include this yourself.
8 # Perl supplies the values for @line and %sub. It effectively inserts
9 # a do DB'DB(<linenum>); in front of every place that can
10 # have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
13 # Revision 3.0.1.2 90/03/12 16:39:39 lwall
14 # patch13: perl -d didn't format stack traces of *foo right
15 # patch13: perl -d wiped out scalar return values of subroutines
17 # Revision 3.0.1.1 89/10/26 23:14:02 lwall
18 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
20 # Revision 3.0 89/10/18 15:19:46 lwall
23 # Revision 2.0 88/06/05 00:09:45 root
24 # Baseline version 2.0.
28 open(IN,"/dev/tty"); # so we don't dingle stdin
29 open(OUT,">/dev/tty"); # so we don't dongle stdout
33 $| = 1; # for real STDOUT
35 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
36 print OUT "\nLoading DB from $header\n\n";
39 local($. ,$@, $!, $[, $,, $/, $\);
40 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
48 $DB'signal |= eval $DB'stop[$DB'line]; print DB'OUT $@;
49 $DB'stop[$DB'line] =~ s/;9$//;
52 if ($single || $trace || $signal) {
53 print OUT "$sub($line):\t",$line[$line];
54 for ($i = $line + 1; $i <= $max && $line[$i] == 0; ++$i) {
55 last if $line[$i] =~ /^\s*(}|#|\n)/;
56 print OUT "$sub($i):\t",$line[$i];
61 eval $DB'action[$DB'line]; print DB'OUT $@;
63 if ($single || $signal) {
66 eval $DB'pre; print DB'OUT $@;
68 print OUT $#stack . " levels deep in subroutine calls!\n"
71 while ((print OUT " DB<", $#hist+1, "> "), $cmd=<IN>) {
76 $cmd =~ /^q$/ && exit 0;
77 $cmd =~ /^$/ && ($cmd = $laststep);
78 push(@hist,$cmd) if length($cmd) > 1;
79 ($i) = split(/\s+/,$cmd);
80 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
85 n Next, steps over subroutine calls.
86 f Finish current subroutine.
87 c [line] Continue; optionally inserts a one-time-only breakpoint
88 at the specified line.
89 <CR> Repeat last n or s.
90 l min+incr List incr+1 lines starting at min.
94 - List previous window.
95 w line List window around line.
96 l subname List subroutine.
97 /pattern/ Search forwards for pattern; final / is optional.
98 ?pattern? Search backwards for pattern.
99 L List breakpoints and actions.
100 S List subroutine names.
103 Set breakpoint; line defaults to the current execution line;
104 condition breaks if it evaluates to true, defaults to \'1\'.
105 b subname [condition]
106 Set breakpoint at first line of subroutine.
107 d [line] Delete breakpoint.
108 D Delete all breakpoints.
110 Set an action to be done before the line is executed.
111 Sequence is: check for breakpoint, print line if necessary,
112 do action, prompt user if breakpoint or step, evaluate line.
113 A Delete all actions.
114 V package List all variables and values in package (default main).
115 < command Define command before prompt.
116 > command Define command after prompt.
117 ! number Redo command (default previous command).
118 ! -number Redo number\'th to last command.
119 H -number Display last number commands (default all).
121 p expr Same as \"package main; print DB'OUT expr\".
122 command Execute as a perl statement.
126 $cmd =~ /^t$/ && do {
128 print OUT "Trace = ".($trace?"on":"off")."\n";
130 $cmd =~ /^S$/ && do {
131 foreach $subname (sort(keys %sub)) {
132 if ($subname =~ /^main'(.*)/) {
136 print OUT $subname,"\n";
140 $cmd =~ /^V$/ && do {
142 $cmd =~ /^V\s*(['A-Za-z_]['\w]*)$/ && do {
144 do 'dumpvar.pl' unless defined &main'dumpvar;
145 if (defined &main'dumpvar) {
146 &main'dumpvar($packname);
149 print DB'OUT "dumpvar.pl not available.\n";
152 $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
154 $subname = "main'" . $subname unless $subname =~ /'/;
155 $subrange = $sub{$subname};
157 if (eval($subrange) < -$window) {
158 $subrange =~ s/-.*/+/;
160 $cmd = "l $subrange";
162 print OUT "Subroutine $1 not found.\n";
165 $cmd =~ /^w\s*(\d*)$/ && do {
169 $cmd = 'l ' . $start . '-' . ($start + $incr); };
170 $cmd =~ /^-$/ && do {
172 $cmd = 'l ' . ($start-$window*2) . '+'; };
173 $cmd =~ /^l$/ && do {
175 $cmd = 'l ' . $start . '-' . ($start + $incr); };
176 $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
179 $incr = $window - 1 unless $incr;
180 $cmd = 'l ' . $start . '-' . ($start + $incr); };
181 $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
182 $end = (!$2) ? $max : ($4 ? $4 : $2);
183 $end = $max if $end > $max;
185 $i = $line if $i eq '.';
187 for (; $i <= $end; $i++) {
188 print OUT "$i:\t", $line[$i];
191 $start = $i; # remember in case they want more
192 $start = $max if $start > $max;
194 $cmd =~ /^D$/ && do {
195 print OUT "Deleting all breakpoints...\n";
196 for ($i = 1; $i <= $max ; $i++) {
200 $cmd =~ /^L$/ && do {
201 for ($i = 1; $i <= $max; $i++) {
202 if ($stop[$i] || $action[$i]) {
203 print OUT "$i:\t", $line[$i];
204 print OUT " break if (", $stop[$i], ")\n"
206 print OUT " action: ", $action[$i], "\n"
212 $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
214 $subname = "main'" . $subname unless $subname =~ /'/;
215 ($i) = split(/-/, $sub{$subname});
217 ++$i while $line[$i] == 0 && $i < $#line;
218 $stop[$i] = $2 ? $2 : 1;
220 print OUT "Subroutine $1 not found.\n";
223 $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
225 if ($line[$i] == 0) {
226 print OUT "Line $i not breakable.\n";
228 $stop[$i] = $2 ? $2 : 1;
231 $cmd =~ /^d\s*(\d+)?/ && do {
235 $cmd =~ /^A$/ && do {
236 for ($i = 1; $i <= $max ; $i++) {
240 $cmd =~ /^<\s*(.*)/ && do {
241 $pre = do action($1);
243 $cmd =~ /^>\s*(.*)/ && do {
244 $post = do action($1);
246 $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
248 if ($line[$i] == 0) {
249 print OUT "Line $i may not have an action.\n";
251 $action[$i] = do action($3);
254 $cmd =~ /^n$/ && do {
258 $cmd =~ /^s$/ && do {
262 $cmd =~ /^c\s*(\d*)\s*$/ && do {
265 if ($line[$i] == 0) {
266 print OUT "Line $i not breakable.\n";
269 $stop[$i] .= ";9"; # add one-time-only b.p.
271 for ($i=0; $i <= $#stack; ) {
275 $cmd =~ /^f$/ && do {
276 $stack[$#stack] |= 2;
278 $cmd =~ /^T$/ && do {
279 for ($i=0; $i <= $#sub; ) {
280 print OUT $sub[$i++], "\n";
284 $cmd =~ /^\/(.*)$/ && do {
286 $inpat =~ s:([^\\])/$:$1:;
288 eval '$inpat =~ m'."\n$inpat\n";
299 $start = 1 if ($start > $max);
300 last if ($start == $end);
301 if ($line[$start] =~ m'."\n$pat\n".'i) {
302 print OUT "$start:\t", $line[$start], "\n";
306 print OUT "/$pat/: not found\n" if ($start == $end);
308 $cmd =~ /^\?(.*)$/ && do {
310 $inpat =~ s:([^\\])\?$:$1:;
312 eval '$inpat =~ m'."\n$inpat\n";
323 $start = $max if ($start <= 0);
324 last if ($start == $end);
325 if ($line[$start] =~ m'."\n$pat\n".'i) {
326 print OUT "$start:\t", $line[$start], "\n";
330 print OUT "?$pat?: not found\n" if ($start == $end);
332 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
333 pop(@hist) if length($cmd) > 1;
334 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
335 $cmd = $hist[$i] . "\n";
338 $cmd =~ /^!(.+)$/ && do {
340 pop(@hist) if length($cmd) > 1;
341 for ($i = $#hist; $i; --$i) {
342 last if $hist[$i] =~ $pat;
345 print OUT "No such command!\n\n";
348 $cmd = $hist[$i] . "\n";
351 $cmd =~ /^H\s*(-(\d+))?/ && do {
352 $end = $2?($#hist-$2):0;
353 $hist = 0 if $hist < 0;
354 for ($i=$#hist; $i>$end; $i--) {
355 print OUT "$i: ",$hist[$i],"\n"
356 unless $hist[$i] =~ /^.?$/;
359 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
368 eval $DB'post; print DB'OUT $@;
375 while ($action =~ s/\\$//) {
387 push(@stack, $single);
389 $single |= 4 if $#stack == $deep;
392 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
393 $_ = sprintf("%s",$_);
397 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
400 push(@sub, $sub . '(' . join(', ', @args) . ') from ' . $line);
404 $single |= pop(@stack);
410 $single |= pop(@stack);
415 $single = 1; # so it stops on first executable statement
418 $SIG{'INT'} = "DB'catch";
419 $deep = 100; # warning if stack gets this deep
427 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
429 push(@sub, 'main(' . join(', ', @args) . ")" );
435 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
436 do "$ENV{'LOGDIR'}/.perldb";
438 elsif (-f "$ENV{'HOME'}/.perldb") {
439 do "$ENV{'HOME'}/.perldb";