perl 3.0: (no announcement message available)
[p5sagit/p5-mst-13.2.git] / lib / perldb.pl
1 package DB;
2
3 $header = '$Header: perldb.pl,v 3.0 89/10/18 15:19:46 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 3.0  89/10/18  15:19:46  lwall
14 # 3.0 baseline
15
16 # Revision 2.0  88/06/05  00:09:45  root
17 # Baseline version 2.0.
18
19 #
20
21 open(IN,"/dev/tty");            # so we don't dingle stdin
22 open(OUT,">/dev/tty");  # so we don't dongle stdout
23 select(OUT);
24 $| = 1;                         # for DB'OUT
25 select(STDOUT);
26 $| = 1;                         # for real STDOUT
27
28 $header =~ s/\$Header: perldb.pl,v 3.0 89/10/18 15:19:46 lwall Locked $/$1$2/;
29 print OUT "\nLoading DB from $header\n\n";
30
31 sub DB {
32     local($. ,$@, $!, $[, $,, $/, $\);
33     $[ = 0; $, = ""; $/ = "\n"; $\ = "";
34     ($line) = @_;
35     if ($stop[$line]) {
36         if ($stop eq '1') {
37             $signal |= 1;
38         }
39         else {
40             package main;
41             $DB'signal |= eval $DB'stop[$DB'line];  print DB'OUT $@;
42             $DB'stop[$DB'line] =~ s/;9$//;
43         }
44     }
45     if ($single || $trace || $signal) {
46         print OUT "$sub($line):\t",$line[$line];
47         for ($i = $line + 1; $i <= $max && $line[$i] == 0; ++$i) {
48             last if $line[$i] =~ /^\s*(}|#|\n)/;
49             print OUT "$sub($i):\t",$line[$i];
50         }
51     }
52     if ($action[$line]) {
53         package main;
54         eval $DB'action[$DB'line];  print DB'OUT $@;
55     }
56     if ($single || $signal) {
57         if ($pre) {
58             package main;
59             eval $DB'pre;  print DB'OUT $@;
60         }
61         print OUT $#stack . " levels deep in subroutine calls!\n"
62             if $single & 4;
63         $start = $line;
64         while ((print OUT "  DB<", $#hist+1, "> "), $cmd=<IN>) {
65             $single = 0;
66             $signal = 0;
67             $cmd eq '' && exit 0;
68             chop($cmd);
69             $cmd =~ /^q$/ && exit 0;
70             $cmd =~ /^$/ && ($cmd = $laststep);
71             push(@hist,$cmd) if length($cmd) > 1;
72             ($i) = split(/\s+/,$cmd);
73             eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
74             $cmd =~ /^h$/ && do {
75                 print OUT "
76 T               Stack trace.
77 s               Single step.
78 n               Next, steps over subroutine calls.
79 f               Finish current subroutine.
80 c [line]        Continue; optionally inserts a one-time-only breakpoint 
81                 at the specified line.
82 <CR>            Repeat last n or s.
83 l min+incr      List incr+1 lines starting at min.
84 l min-max       List lines.
85 l line          List line;
86 l               List next window.
87 -               List previous window.
88 w line          List window around line.
89 l subname       List subroutine.
90 /pattern/       Search forwards for pattern; final / is optional.
91 ?pattern?       Search backwards for pattern.
92 L               List breakpoints and actions.
93 S               List subroutine names.
94 t               Toggle trace mode.
95 b [line] [condition]
96                 Set breakpoint; line defaults to the current execution line; 
97                 condition breaks if it evaluates to true, defaults to \'1\'.
98 b subname [condition]
99                 Set breakpoint at first line of subroutine.
100 d [line]        Delete breakpoint.
101 D               Delete all breakpoints.
102 a [line] command
103                 Set an action to be done before the line is executed.
104                 Sequence is: check for breakpoint, print line if necessary,
105                 do action, prompt user if breakpoint or step, evaluate line.
106 A               Delete all actions.
107 V package       List all variables and values in package (default main).
108 < command       Define command before prompt.
109 > command       Define command after prompt.
110 ! number        Redo command (default previous command).
111 ! -number       Redo number\'th to last command.
112 H -number       Display last number commands (default all).
113 q or ^D         Quit.
114 p expr          Same as \"package main; print DB'OUT expr\".
115 command         Execute as a perl statement.
116
117 ";
118                 next; };
119             $cmd =~ /^t$/ && do {
120                 $trace = !$trace;
121                 print OUT "Trace = ".($trace?"on":"off")."\n";
122                 next; };
123             $cmd =~ /^S$/ && do {
124                 foreach $subname (sort(keys %sub)) {
125                     if ($subname =~ /^main'(.*)/) {
126                         print OUT $1,"\n";
127                     }
128                     else {
129                         print OUT $subname,"\n";
130                     }
131                 }
132                 next; };
133             $cmd =~ /^V$/ && do {
134                 $cmd = 'V main'; };
135             $cmd =~ /^V\s*(['A-Za-z_]['\w]*)$/ && do {
136                 $packname = $1;
137                 do 'dumpvar.pl' unless defined &main'dumpvar;
138                 if (defined &main'dumpvar) {
139                     &main'dumpvar($packname);
140                 }
141                 else {
142                     print DB'OUT "dumpvar.pl not available.\n";
143                 }
144                 next; };
145             $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
146                 $subname = $1;
147                 $subname = "main'" . $subname unless $subname =~ /'/;
148                 $subrange = $sub{$subname};
149                 if ($subrange) {
150                     if (eval($subrange) < -$window) {
151                         $subrange =~ s/-.*/+/;
152                     }
153                     $cmd = "l $subrange";
154                 } else {
155                     print OUT "Subroutine $1 not found.\n";
156                     next;
157                 } };
158             $cmd =~ /^w\s*(\d*)$/ && do {
159                 $incr = $window - 1;
160                 $start = $1 if $1;
161                 $start -= $preview;
162                 $cmd = 'l ' . $start . '-' . ($start + $incr); };
163             $cmd =~ /^-$/ && do {
164                 $incr = $window - 1;
165                 $cmd = 'l ' . ($start-$window*2) . '+'; };
166             $cmd =~ /^l$/ && do {
167                 $incr = $window - 1;
168                 $cmd = 'l ' . $start . '-' . ($start + $incr); };
169             $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
170                 $start = $1 if $1;
171                 $incr = $2;
172                 $incr = $window - 1 unless $incr;
173                 $cmd = 'l ' . $start . '-' . ($start + $incr); };
174             $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
175                 $end = (!$2) ? $max : ($4 ? $4 : $2);
176                 $end = $max if $end > $max;
177                 $i = $2;
178                 $i = $line if $i eq '.';
179                 $i = 1 if $i < 1;
180                 for (; $i <= $end; $i++) {
181                     print OUT "$i:\t", $line[$i];
182                     last if $signal;
183                 }
184                 $start = $i;    # remember in case they want more
185                 $start = $max if $start > $max;
186                 next; };
187             $cmd =~ /^D$/ && do {
188                 print OUT "Deleting all breakpoints...\n";
189                 for ($i = 1; $i <= $max ; $i++) {
190                     $stop[$i] = 0;
191                 }
192                 next; };
193             $cmd =~ /^L$/ && do {
194                 for ($i = 1; $i <= $max; $i++) {
195                     if ($stop[$i] || $action[$i]) {
196                         print OUT "$i:\t", $line[$i];
197                         print OUT "  break if (", $stop[$i], ")\n" 
198                             if $stop[$i];
199                         print OUT "  action:  ", $action[$i], "\n" 
200                             if $action[$i];
201                         last if $signal;
202                     }
203                 }
204                 next; };
205             $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
206                 $subname = $1;
207                 $subname = "main'" . $subname unless $subname =~ /'/;
208                 ($i) = split(/-/, $sub{$subname});
209                 if ($i) {
210                     ++$i while $line[$i] == 0 && $i < $#line;
211                     $stop[$i] = $2 ? $2 : 1;
212                 } else {
213                     print OUT "Subroutine $1 not found.\n";
214                 }
215                 next; };
216             $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
217                 $i = ($1?$1:$line);
218                 if ($line[$i] == 0) {
219                     print OUT "Line $i not breakable.\n";
220                 } else {
221                     $stop[$i] = $2 ? $2 : 1;
222                 }
223                 next; };
224             $cmd =~ /^d\s*(\d+)?/ && do {
225                 $i = ($1?$1:$line);
226                 $stop[$i] = '';
227                 next; };
228             $cmd =~ /^A$/ && do {
229                 for ($i = 1; $i <= $max ; $i++) {
230                     $action[$i] = '';
231                 }
232                 next; };
233             $cmd =~ /^<\s*(.*)/ && do {
234                 $pre = do action($1);
235                 next; };
236             $cmd =~ /^>\s*(.*)/ && do {
237                 $post = do action($1);
238                 next; };
239             $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
240                 $i = $1;
241                 if ($line[$i] == 0) {
242                     print OUT "Line $i may not have an action.\n";
243                 } else {
244                     $action[$i] = do action($3);
245                 }
246                 next; };
247             $cmd =~ /^n$/ && do {
248                 $single = 2;
249                 $laststep = $cmd;
250                 last; };
251             $cmd =~ /^s$/ && do {
252                 $single = 1;
253                 $laststep = $cmd;
254                 last; };
255             $cmd =~ /^c\s*(\d*)\s*$/ && do {
256                 $i = $1;
257                 if ($i) {
258                     if ($line[$i] == 0) {
259                         print OUT "Line $i not breakable.\n";
260                         next;
261                     }
262                     $stop[$i] .= ";9";  # add one-time-only b.p.
263                 }
264                 for ($i=0; $i <= $#stack; ) {
265                     $stack[$i++] &= ~1;
266                 }
267                 last; };
268             $cmd =~ /^f$/ && do {
269                 $stack[$#stack] |= 2;
270                 last; };
271             $cmd =~ /^T$/ && do {
272                 for ($i=0; $i <= $#sub; ) {
273                     print OUT $sub[$i++], "\n";
274                     last if $signal;
275                 }
276                 next; };
277             $cmd =~ /^\/(.*)$/ && do {
278                 $inpat = $1;
279                 $inpat =~ s:([^\\])/$:$1:;
280                 if ($inpat ne "") {
281                     eval '$inpat =~ m'."\n$inpat\n";    
282                     if ($@ ne "") {
283                         print OUT "$@";
284                         next;
285                     }
286                     $pat = $inpat;
287                 }
288                 $end = $start;
289                 eval '
290                 for (;;) {
291                     ++$start;
292                     $start = 1 if ($start > $max);
293                     last if ($start == $end);
294                     if ($line[$start] =~ m'."\n$pat\n".'i) {
295                         print OUT "$start:\t", $line[$start], "\n";
296                         last;
297                     }
298                 } ';
299                 print OUT "/$pat/: not found\n" if ($start == $end);
300                 next; };
301             $cmd =~ /^\?(.*)$/ && do {
302                 $inpat = $1;
303                 $inpat =~ s:([^\\])\?$:$1:;
304                 if ($inpat ne "") {
305                     eval '$inpat =~ m'."\n$inpat\n";    
306                     if ($@ ne "") {
307                         print OUT "$@";
308                         next;
309                     }
310                     $pat = $inpat;
311                 }
312                 $end = $start;
313                 eval '
314                 for (;;) {
315                     --$start;
316                     $start = $max if ($start <= 0);
317                     last if ($start == $end);
318                     if ($line[$start] =~ m'."\n$pat\n".'i) {
319                         print OUT "$start:\t", $line[$start], "\n";
320                         last;
321                     }
322                 } ';
323                 print OUT "?$pat?: not found\n" if ($start == $end);
324                 next; };
325             $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
326                 pop(@hist) if length($cmd) > 1;
327                 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
328                 $cmd = $hist[$i] . "\n";
329                 print OUT $cmd;
330                 redo; };
331             $cmd =~ /^!(.+)$/ && do {
332                 $pat = "^$1";
333                 pop(@hist) if length($cmd) > 1;
334                 for ($i = $#hist; $i; --$i) {
335                     last if $hist[$i] =~ $pat;
336                 }
337                 if (!$i) {
338                     print OUT "No such command!\n\n";
339                     next;
340                 }
341                 $cmd = $hist[$i] . "\n";
342                 print OUT $cmd;
343                 redo; };
344             $cmd =~ /^H\s*(-(\d+))?/ && do {
345                 $end = $2?($#hist-$2):0;
346                 $hist = 0 if $hist < 0;
347                 for ($i=$#hist; $i>$end; $i--) {
348                     print OUT "$i: ",$hist[$i],"\n"
349                         unless $hist[$i] =~ /^.?$/;
350                 };
351                 next; };
352             $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
353             {
354                 package main;
355                 eval $DB'cmd;
356             }
357             print OUT $@,"\n";
358         }
359         if ($post) {
360             package main;
361             eval $DB'post;  print DB'OUT $@;
362         }
363     }
364 }
365
366 sub action {
367     local($action) = @_;
368     while ($action =~ s/\\$//) {
369         print OUT "+ ";
370         $action .= <IN>;
371     }
372     $action;
373 }
374
375 sub catch {
376     $signal = 1;
377 }
378
379 sub sub {
380     push(@stack, $single);
381     $single &= 1;
382     $single |= 4 if $#stack == $deep;
383     local(@args) = @_;
384     for (@args) {
385         if (/^Stab/ && length($_) == length($_main{'_main'})) {
386             $_ = sprintf("%s",$_);
387             print "ARG: $_\n";
388         }
389         else {
390             s/'/\\'/g;
391             s/(.*)/'$1'/ unless /^-?[\d.]+$/;
392         }
393     }
394     push(@sub, $sub . '(' . join(', ', @args) . ') from ' . $line);
395     if (wantarray) {
396         @i = &$sub;
397     }
398     else {
399         $i = &$sub;
400         @i = $i;
401     }
402     --$#sub;
403     $single |= pop(@stack);
404     @i;
405 }
406
407 $single = 1;                    # so it stops on first executable statement
408 $max = $#line;
409 @hist = ('?');
410 $SIG{'INT'} = "DB'catch";
411 $deep = 100;            # warning if stack gets this deep
412 $window = 10;
413 $preview = 3;
414
415 @stack = (0);
416 @args = @ARGV;
417 for (@args) {
418     s/'/\\'/g;
419     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
420 }
421 push(@sub, 'main(' . join(', ', @args) . ")" );
422 $sub = 'main';
423
424 if (-f '.perldb') {
425     do './.perldb';
426 }
427 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
428     do "$ENV{'LOGDIR'}/.perldb";
429 }
430 elsif (-f "$ENV{'HOME'}/.perldb") {
431     do "$ENV{'HOME'}/.perldb";
432 }
433
434 1;