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