perl 4.0.00: (no release announcement available)
[p5sagit/p5-mst-13.2.git] / lib / perldb.pl
1 package DB;
2
3 $header = '$Header: perldb.pl,v 4.0 91/03/20 01:25:50 lwall Locked $';
4 #
5 # This file is automatically included if you do perl -d.
6 # It's probably not useful to include this yourself.
7 #
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.
11 #
12 # $Log: perldb.pl,v $
13 # Revision 4.0  91/03/20  01:25:50  lwall
14 # 4.0 baseline.
15
16 # Revision 3.0.1.6  91/01/11  18:08:58  lwall
17 # patch42: @_ couldn't be accessed from debugger
18
19 # Revision 3.0.1.5  90/11/10  01:40:26  lwall
20 # patch38: the debugger wouldn't stop correctly or do action routines
21
22 # Revision 3.0.1.4  90/10/15  17:40:38  lwall
23 # patch29: added caller
24 # patch29: the debugger now understands packages and evals
25 # patch29: scripts now run at almost full speed under the debugger
26 # patch29: more variables are settable from debugger
27
28 # Revision 3.0.1.3  90/08/09  04:00:58  lwall
29 # patch19: debugger now allows continuation lines
30 # patch19: debugger can now dump lists of variables
31 # patch19: debugger can now add aliases easily from prompt
32
33 # Revision 3.0.1.2  90/03/12  16:39:39  lwall
34 # patch13: perl -d didn't format stack traces of *foo right
35 # patch13: perl -d wiped out scalar return values of subroutines
36
37 # Revision 3.0.1.1  89/10/26  23:14:02  lwall
38 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
39
40 # Revision 3.0  89/10/18  15:19:46  lwall
41 # 3.0 baseline
42
43 # Revision 2.0  88/06/05  00:09:45  root
44 # Baseline version 2.0.
45
46 #
47
48 open(IN, "</dev/tty") || open(IN,  "<&STDIN");  # so we don't dingle stdin
49 open(OUT,">/dev/tty") || open(OUT, ">&STDOUT"); # so we don't dongle stdout
50 select(OUT);
51 $| = 1;                         # for DB'OUT
52 select(STDOUT);
53 $| = 1;                         # for real STDOUT
54 $sub = '';
55
56 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
57 print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n";
58
59 sub DB {
60     &save;
61     ($package, $filename, $line) = caller;
62     $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
63         "package $package;";            # this won't let them modify, alas
64     local(*dbline) = "_<$filename";
65     $max = $#dbline;
66     if (($stop,$action) = split(/\0/,$dbline{$line})) {
67         if ($stop eq '1') {
68             $signal |= 1;
69         }
70         else {
71             $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
72             $dbline{$line} =~ s/;9($|\0)/$1/;
73         }
74     }
75     if ($single || $trace || $signal) {
76         print OUT "$package'" unless $sub =~ /'/;
77         print OUT "$sub($filename:$line):\t",$dbline[$line];
78         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
79             last if $dbline[$i] =~ /^\s*(}|#|\n)/;
80             print OUT "$sub($filename:$i):\t",$dbline[$i];
81         }
82     }
83     $evalarg = $action, &eval if $action;
84     if ($single || $signal) {
85         $evalarg = $pre, &eval if $pre;
86         print OUT $#stack . " levels deep in subroutine calls!\n"
87             if $single & 4;
88         $start = $line;
89       CMD:
90         while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
91             {
92                 $single = 0;
93                 $signal = 0;
94                 $cmd eq '' && exit 0;
95                 chop($cmd);
96                 $cmd =~ s/\\$// && do {
97                     print OUT "  cont: ";
98                     $cmd .= &gets;
99                     redo CMD;
100                 };
101                 $cmd =~ /^q$/ && exit 0;
102                 $cmd =~ /^$/ && ($cmd = $laststep);
103                 push(@hist,$cmd) if length($cmd) > 1;
104                 ($i) = split(/\s+/,$cmd);
105                 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
106                 $cmd =~ /^h$/ && do {
107                     print OUT "
108 T               Stack trace.
109 s               Single step.
110 n               Next, steps over subroutine calls.
111 r               Return from current subroutine.
112 c [line]        Continue; optionally inserts a one-time-only breakpoint 
113                 at the specified line.
114 <CR>            Repeat last n or s.
115 l min+incr      List incr+1 lines starting at min.
116 l min-max       List lines.
117 l line          List line;
118 l               List next window.
119 -               List previous window.
120 w line          List window around line.
121 l subname       List subroutine.
122 f filename      Switch to filename.
123 /pattern/       Search forwards for pattern; final / is optional.
124 ?pattern?       Search backwards for pattern.
125 L               List breakpoints and actions.
126 S               List subroutine names.
127 t               Toggle trace mode.
128 b [line] [condition]
129                 Set breakpoint; line defaults to the current execution line; 
130                 condition breaks if it evaluates to true, defaults to \'1\'.
131 b subname [condition]
132                 Set breakpoint at first line of subroutine.
133 d [line]        Delete breakpoint.
134 D               Delete all breakpoints.
135 a [line] command
136                 Set an action to be done before the line is executed.
137                 Sequence is: check for breakpoint, print line if necessary,
138                 do action, prompt user if breakpoint or step, evaluate line.
139 A               Delete all actions.
140 V [pkg [vars]]  List some (default all) variables in package (default current).
141 X [vars]        Same as \"V currentpackage [vars]\".
142 < command       Define command before prompt.
143 > command       Define command after prompt.
144 ! number        Redo command (default previous command).
145 ! -number       Redo number\'th to last command.
146 H -number       Display last number commands (default all).
147 q or ^D         Quit.
148 p expr          Same as \"print DB'OUT expr\" in current package.
149 = [alias value] Define a command alias, or list current aliases.
150 command         Execute as a perl statement in current package.
151
152 ";
153                     next CMD; };
154                 $cmd =~ /^t$/ && do {
155                     $trace = !$trace;
156                     print OUT "Trace = ".($trace?"on":"off")."\n";
157                     next CMD; };
158                 $cmd =~ /^S$/ && do {
159                     foreach $subname (sort(keys %sub)) {
160                         print OUT $subname,"\n";
161                     }
162                     next CMD; };
163                 $cmd =~ s/^X\b/V $package/;
164                 $cmd =~ /^V$/ && do {
165                     $cmd = 'V $package'; };
166                 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
167                     $packname = $1;
168                     @vars = split(' ',$2);
169                     do 'dumpvar.pl' unless defined &main'dumpvar;
170                     if (defined &main'dumpvar) {
171                         &main'dumpvar($packname,@vars);
172                     }
173                     else {
174                         print DB'OUT "dumpvar.pl not available.\n";
175                     }
176                     next CMD; };
177                 $cmd =~ /^f\b\s*(.*)/ && do {
178                     $file = $1;
179                     if (!$file) {
180                         print OUT "The old f command is now the r command.\n";
181                         print OUT "The new f command switches filenames.\n";
182                         next CMD;
183                     }
184                     if (!defined $_main{'_<' . $file}) {
185                         if (($try) = grep(m#^_<.*$file#, keys %_main)) {
186                             $file = substr($try,2);
187                             print "\n$file:\n";
188                         }
189                     }
190                     if (!defined $_main{'_<' . $file}) {
191                         print OUT "There's no code here anything matching $file.\n";
192                         next CMD;
193                     }
194                     elsif ($file ne $filename) {
195                         *dbline = "_<$file";
196                         $max = $#dbline;
197                         $filename = $file;
198                         $start = 1;
199                         $cmd = "l";
200                     } };
201                 $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
202                     $subname = $1;
203                     $subname = "main'" . $subname unless $subname =~ /'/;
204                     $subname = "main" . $subname if substr($subname,0,1) eq "'";
205                     ($file,$subrange) = split(/:/,$sub{$subname});
206                     if ($file ne $filename) {
207                         *dbline = "_<$file";
208                         $max = $#dbline;
209                         $filename = $file;
210                     }
211                     if ($subrange) {
212                         if (eval($subrange) < -$window) {
213                             $subrange =~ s/-.*/+/;
214                         }
215                         $cmd = "l $subrange";
216                     } else {
217                         print OUT "Subroutine $1 not found.\n";
218                         next CMD;
219                     } };
220                 $cmd =~ /^w\b\s*(\d*)$/ && do {
221                     $incr = $window - 1;
222                     $start = $1 if $1;
223                     $start -= $preview;
224                     $cmd = 'l ' . $start . '-' . ($start + $incr); };
225                 $cmd =~ /^-$/ && do {
226                     $incr = $window - 1;
227                     $cmd = 'l ' . ($start-$window*2) . '+'; };
228                 $cmd =~ /^l$/ && do {
229                     $incr = $window - 1;
230                     $cmd = 'l ' . $start . '-' . ($start + $incr); };
231                 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
232                     $start = $1 if $1;
233                     $incr = $2;
234                     $incr = $window - 1 unless $incr;
235                     $cmd = 'l ' . $start . '-' . ($start + $incr); };
236                 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
237                     $end = (!$2) ? $max : ($4 ? $4 : $2);
238                     $end = $max if $end > $max;
239                     $i = $2;
240                     $i = $line if $i eq '.';
241                     $i = 1 if $i < 1;
242                     for (; $i <= $end; $i++) {
243                         print OUT "$i:\t", $dbline[$i];
244                         last if $signal;
245                     }
246                     $start = $i;        # remember in case they want more
247                     $start = $max if $start > $max;
248                     next CMD; };
249                 $cmd =~ /^D$/ && do {
250                     print OUT "Deleting all breakpoints...\n";
251                     for ($i = 1; $i <= $max ; $i++) {
252                         if (defined $dbline{$i}) {
253                             $dbline{$i} =~ s/^[^\0]+//;
254                             if ($dbline{$i} =~ s/^\0?$//) {
255                                 delete $dbline{$i};
256                             }
257                         }
258                     }
259                     next CMD; };
260                 $cmd =~ /^L$/ && do {
261                     for ($i = 1; $i <= $max; $i++) {
262                         if (defined $dbline{$i}) {
263                             print OUT "$i:\t", $dbline[$i];
264                             ($stop,$action) = split(/\0/, $dbline{$i});
265                             print OUT "  break if (", $stop, ")\n" 
266                                 if $stop;
267                             print OUT "  action:  ", $action, "\n" 
268                                 if $action;
269                             last if $signal;
270                         }
271                     }
272                     next CMD; };
273                 $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
274                     $subname = $1;
275                     $cond = $2 || '1';
276                     $subname = "$package'" . $subname unless $subname =~ /'/;
277                     $subname = "main" . $subname if substr($subname,0,1) eq "'";
278                     ($filename,$i) = split(/[:-]/, $sub{$subname});
279                     if ($i) {
280                         *dbline = "_<$filename";
281                         ++$i while $dbline[$i] == 0 && $i < $#dbline;
282                         $dbline{$i} =~ s/^[^\0]*/$cond/;
283                     } else {
284                         print OUT "Subroutine $subname not found.\n";
285                     }
286                     next CMD; };
287                 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
288                     $i = ($1?$1:$line);
289                     $cond = $2 || '1';
290                     if ($dbline[$i] == 0) {
291                         print OUT "Line $i not breakable.\n";
292                     } else {
293                         $dbline{$i} =~ s/^[^\0]*/$cond/;
294                     }
295                     next CMD; };
296                 $cmd =~ /^d\b\s*(\d+)?/ && do {
297                     $i = ($1?$1:$line);
298                     $dbline{$i} =~ s/^[^\0]*//;
299                     delete $dbline{$i} if $dbline{$i} eq '';
300                     next CMD; };
301                 $cmd =~ /^A$/ && do {
302                     for ($i = 1; $i <= $max ; $i++) {
303                         if (defined $dbline{$i}) {
304                             $dbline{$i} =~ s/\0[^\0]*//;
305                             delete $dbline{$i} if $dbline{$i} eq '';
306                         }
307                     }
308                     next CMD; };
309                 $cmd =~ /^<\s*(.*)/ && do {
310                     $pre = do action($1);
311                     next CMD; };
312                 $cmd =~ /^>\s*(.*)/ && do {
313                     $post = do action($1);
314                     next CMD; };
315                 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
316                     $i = $1;
317                     if ($dbline[$i] == 0) {
318                         print OUT "Line $i may not have an action.\n";
319                     } else {
320                         $dbline{$i} =~ s/\0[^\0]*//;
321                         $dbline{$i} .= "\0" . do action($3);
322                     }
323                     next CMD; };
324                 $cmd =~ /^n$/ && do {
325                     $single = 2;
326                     $laststep = $cmd;
327                     last CMD; };
328                 $cmd =~ /^s$/ && do {
329                     $single = 1;
330                     $laststep = $cmd;
331                     last CMD; };
332                 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
333                     $i = $1;
334                     if ($i) {
335                         if ($dbline[$i] == 0) {
336                             print OUT "Line $i not breakable.\n";
337                             next CMD;
338                         }
339                         $dbline{$i} =~ s/(\0|$)/;9$1/;  # add one-time-only b.p.
340                     }
341                     for ($i=0; $i <= $#stack; ) {
342                         $stack[$i++] &= ~1;
343                     }
344                     last CMD; };
345                 $cmd =~ /^r$/ && do {
346                     $stack[$#stack] |= 2;
347                     last CMD; };
348                 $cmd =~ /^T$/ && do {
349                     local($p,$f,$l,$s,$h,$a,@a,@sub);
350                     for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
351                         @a = @args;
352                         for (@a) {
353                             if (/^StB\000/ && length($_) == length($_main{'_main'})) {
354                                 $_ = sprintf("%s",$_);
355                             }
356                             else {
357                                 s/'/\\'/g;
358                                 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
359                                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
360                                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
361                             }
362                         }
363                         $w = $w ? '@ = ' : '$ = ';
364                         $a = $h ? '(' . join(', ', @a) . ')' : '';
365                         push(@sub, "$w&$s$a from file $f line $l\n");
366                         last if $signal;
367                     }
368                     for ($i=0; $i <= $#sub; $i++) {
369                         last if $signal;
370                         print OUT $sub[$i];
371                     }
372                     next CMD; };
373                 $cmd =~ /^\/(.*)$/ && do {
374                     $inpat = $1;
375                     $inpat =~ s:([^\\])/$:$1:;
376                     if ($inpat ne "") {
377                         eval '$inpat =~ m'."\n$inpat\n";        
378                         if ($@ ne "") {
379                             print OUT "$@";
380                             next CMD;
381                         }
382                         $pat = $inpat;
383                     }
384                     $end = $start;
385                     eval '
386                     for (;;) {
387                         ++$start;
388                         $start = 1 if ($start > $max);
389                         last if ($start == $end);
390                         if ($dbline[$start] =~ m'."\n$pat\n".'i) {
391                             print OUT "$start:\t", $dbline[$start], "\n";
392                             last;
393                         }
394                     } ';
395                     print OUT "/$pat/: not found\n" if ($start == $end);
396                     next CMD; };
397                 $cmd =~ /^\?(.*)$/ && do {
398                     $inpat = $1;
399                     $inpat =~ s:([^\\])\?$:$1:;
400                     if ($inpat ne "") {
401                         eval '$inpat =~ m'."\n$inpat\n";        
402                         if ($@ ne "") {
403                             print OUT "$@";
404                             next CMD;
405                         }
406                         $pat = $inpat;
407                     }
408                     $end = $start;
409                     eval '
410                     for (;;) {
411                         --$start;
412                         $start = $max if ($start <= 0);
413                         last if ($start == $end);
414                         if ($dbline[$start] =~ m'."\n$pat\n".'i) {
415                             print OUT "$start:\t", $dbline[$start], "\n";
416                             last;
417                         }
418                     } ';
419                     print OUT "?$pat?: not found\n" if ($start == $end);
420                     next CMD; };
421                 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
422                     pop(@hist) if length($cmd) > 1;
423                     $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
424                     $cmd = $hist[$i] . "\n";
425                     print OUT $cmd;
426                     redo CMD; };
427                 $cmd =~ /^!(.+)$/ && do {
428                     $pat = "^$1";
429                     pop(@hist) if length($cmd) > 1;
430                     for ($i = $#hist; $i; --$i) {
431                         last if $hist[$i] =~ $pat;
432                     }
433                     if (!$i) {
434                         print OUT "No such command!\n\n";
435                         next CMD;
436                     }
437                     $cmd = $hist[$i] . "\n";
438                     print OUT $cmd;
439                     redo CMD; };
440                 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
441                     $end = $2?($#hist-$2):0;
442                     $hist = 0 if $hist < 0;
443                     for ($i=$#hist; $i>$end; $i--) {
444                         print OUT "$i: ",$hist[$i],"\n"
445                             unless $hist[$i] =~ /^.?$/;
446                     };
447                     next CMD; };
448                 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
449                 $cmd =~ /^=/ && do {
450                     if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
451                         $alias{$k}="s~$k~$v~";
452                         print OUT "$k = $v\n";
453                     } elsif ($cmd =~ /^=\s*$/) {
454                         foreach $k (sort keys(%alias)) {
455                             if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
456                                 print OUT "$k = $v\n";
457                             } else {
458                                 print OUT "$k\t$alias{$k}\n";
459                             };
460                         };
461                     };
462                     next CMD; };
463             }
464             $evalarg = $cmd; &eval;
465             print OUT "\n";
466         }
467         if ($post) {
468             $evalarg = $post; &eval;
469         }
470     }
471     ($@, $!, $[, $,, $/, $\) = @saved;
472 }
473
474 sub save {
475     @saved = ($@, $!, $[, $,, $/, $\);
476     $[ = 0; $, = ""; $/ = "\n"; $\ = "";
477 }
478
479 # The following takes its argument via $evalarg to preserve current @_
480
481 sub eval {
482     eval "$usercontext $evalarg; &DB'save";
483     print OUT $@;
484 }
485
486 sub action {
487     local($action) = @_;
488     while ($action =~ s/\\$//) {
489         print OUT "+ ";
490         $action .= &gets;
491     }
492     $action;
493 }
494
495 sub gets {
496     local($.);
497     <IN>;
498 }
499
500 sub catch {
501     $signal = 1;
502 }
503
504 sub sub {
505     push(@stack, $single);
506     $single &= 1;
507     $single |= 4 if $#stack == $deep;
508     if (wantarray) {
509         @i = &$sub;
510         $single |= pop(@stack);
511         @i;
512     }
513     else {
514         $i = &$sub;
515         $single |= pop(@stack);
516         $i;
517     }
518 }
519
520 $single = 1;                    # so it stops on first executable statement
521 @hist = ('?');
522 $SIG{'INT'} = "DB'catch";
523 $deep = 100;            # warning if stack gets this deep
524 $window = 10;
525 $preview = 3;
526
527 @stack = (0);
528 @ARGS = @ARGV;
529 for (@args) {
530     s/'/\\'/g;
531     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
532 }
533
534 if (-f '.perldb') {
535     do './.perldb';
536 }
537 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
538     do "$ENV{'LOGDIR'}/.perldb";
539 }
540 elsif (-f "$ENV{'HOME'}/.perldb") {
541     do "$ENV{'HOME'}/.perldb";
542 }
543
544 1;