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