VMS-specific changes.
[p5sagit/p5-mst-13.2.git] / lib / perl5db.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
a0d0e21e 7$header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
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
a0d0e21e 13# a &DB'DB(<linenum>); in front of every place that can
a687059c 14# have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
15#
16# $Log: perldb.pl,v $
a0d0e21e 17
4633a7c4 18# Is Perl being run from Emacs?
19$emacs = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
20shift(@main::ARGV) if $emacs;
21
22#require Term::ReadLine;
23
a0d0e21e 24local($^W) = 0;
a687059c 25
83025b21 26if (-e "/dev/tty") {
27 $console = "/dev/tty";
28 $rcfile=".perldb";
29}
a0d0e21e 30elsif (-e "con") {
83025b21 31 $console = "con";
32 $rcfile="perldb.ini";
33}
a0d0e21e 34else {
35 $console = "sys\$command";
36 $rcfile="perldb.ini";
37}
83025b21 38
4633a7c4 39# Around a bug:
40if (defined $ENV{'OS2_SHELL'}) { # In OS/2
41 if ($DB::emacs) {
42 $console = undef;
43 } else {
44 $console = "/dev/con";
45 }
46}
47
83025b21 48open(IN, "<$console") || open(IN, "<&STDIN"); # so we don't dingle stdin
a0d0e21e 49open(OUT,">$console") || open(OUT, ">&STDERR")
8990e307 50 || open(OUT, ">&STDOUT"); # so we don't dongle stdout
a687059c 51select(OUT);
ed6116ce 52$| = 1; # for DB::OUT
a687059c 53select(STDOUT);
54$| = 1; # for real STDOUT
76854fea 55$sub = '';
a687059c 56
03a14243 57$header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
f0fcb552 58print OUT "\nLoading DB routines from $header\n";
59print OUT ("Emacs support ",
60 $emacs ? "enabled" : "available",
61 ".\n");
62print OUT "\nEnter h for help.\n\n";
a687059c 63
748a9306 64@ARGS;
65
a687059c 66sub DB {
76854fea 67 &save;
748a9306 68 ($pkg, $filename, $line) = caller;
a0d0e21e 69 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
748a9306 70 "package $pkg;"; # this won't let them modify, alas
ed6116ce 71 local(*dbline) = "::_<$filename";
76854fea 72 $max = $#dbline;
73 if (($stop,$action) = split(/\0/,$dbline{$line})) {
a687059c 74 if ($stop eq '1') {
75 $signal |= 1;
76 }
77 else {
ed6116ce 78 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
76854fea 79 $dbline{$line} =~ s/;9($|\0)/$1/;
a687059c 80 }
81 }
82 if ($single || $trace || $signal) {
f0fcb552 83 if ($emacs) {
84 print OUT "\032\032$filename:$line:0\n";
85 } else {
748a9306 86 $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
a0d0e21e 87 $prefix .= "$sub($filename:";
88 if (length($prefix) > 30) {
89 print OUT "$prefix$line):\n$line:\t",$dbline[$line];
90 $prefix = "";
91 $infix = ":\t";
92 }
93 else {
94 $infix = "):\t";
95 print OUT "$prefix$line$infix",$dbline[$line];
96 }
f0fcb552 97 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
98 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
a0d0e21e 99 print OUT "$prefix$i$infix",$dbline[$i];
f0fcb552 100 }
a687059c 101 }
102 }
27e2fb84 103 $evalarg = $action, &eval if $action;
a687059c 104 if ($single || $signal) {
27e2fb84 105 $evalarg = $pre, &eval if $pre;
a687059c 106 print OUT $#stack . " levels deep in subroutine calls!\n"
107 if $single & 4;
108 $start = $line;
fe14fcc3 109 CMD:
76854fea 110 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
fe14fcc3 111 {
112 $single = 0;
113 $signal = 0;
114 $cmd eq '' && exit 0;
115 chop($cmd);
116 $cmd =~ s/\\$// && do {
117 print OUT " cont: ";
118 $cmd .= &gets;
119 redo CMD;
120 };
121 $cmd =~ /^q$/ && exit 0;
122 $cmd =~ /^$/ && ($cmd = $laststep);
123 push(@hist,$cmd) if length($cmd) > 1;
124 ($i) = split(/\s+/,$cmd);
125 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
126 $cmd =~ /^h$/ && do {
127 print OUT "
a687059c 128T Stack trace.
129s Single step.
130n Next, steps over subroutine calls.
76854fea 131r Return from current subroutine.
a687059c 132c [line] Continue; optionally inserts a one-time-only breakpoint
133 at the specified line.
134<CR> Repeat last n or s.
135l min+incr List incr+1 lines starting at min.
136l min-max List lines.
137l line List line;
138l List next window.
139- List previous window.
140w line List window around line.
141l subname List subroutine.
76854fea 142f filename Switch to filename.
a687059c 143/pattern/ Search forwards for pattern; final / is optional.
144?pattern? Search backwards for pattern.
145L List breakpoints and actions.
146S List subroutine names.
147t Toggle trace mode.
148b [line] [condition]
149 Set breakpoint; line defaults to the current execution line;
150 condition breaks if it evaluates to true, defaults to \'1\'.
151b subname [condition]
152 Set breakpoint at first line of subroutine.
153d [line] Delete breakpoint.
154D Delete all breakpoints.
155a [line] command
156 Set an action to be done before the line is executed.
157 Sequence is: check for breakpoint, print line if necessary,
158 do action, prompt user if breakpoint or step, evaluate line.
159A Delete all actions.
76854fea 160V [pkg [vars]] List some (default all) variables in package (default current).
161X [vars] Same as \"V currentpackage [vars]\".
a687059c 162< command Define command before prompt.
163> command Define command after prompt.
164! number Redo command (default previous command).
165! -number Redo number\'th to last command.
166H -number Display last number commands (default all).
167q or ^D Quit.
ed6116ce 168p expr Same as \"print DB::OUT expr\" in current package.
33b78306 169= [alias value] Define a command alias, or list current aliases.
76854fea 170command Execute as a perl statement in current package.
a687059c 171
172";
fe14fcc3 173 next CMD; };
174 $cmd =~ /^t$/ && do {
175 $trace = !$trace;
176 print OUT "Trace = ".($trace?"on":"off")."\n";
177 next CMD; };
178 $cmd =~ /^S$/ && do {
179 foreach $subname (sort(keys %sub)) {
180 print OUT $subname,"\n";
181 }
182 next CMD; };
748a9306 183 $cmd =~ s/^X\b/V $pkg/;
fe14fcc3 184 $cmd =~ /^V$/ && do {
748a9306 185 $cmd = "V $pkg"; };
fe14fcc3 186 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
79072805 187 local ($savout) = select(OUT);
fe14fcc3 188 $packname = $1;
189 @vars = split(' ',$2);
ed6116ce 190 do 'dumpvar.pl' unless defined &main::dumpvar;
191 if (defined &main::dumpvar) {
192 &main::dumpvar($packname,@vars);
fe14fcc3 193 }
194 else {
ed6116ce 195 print DB::OUT "dumpvar.pl not available.\n";
76854fea 196 }
79072805 197 select ($savout);
fe14fcc3 198 next CMD; };
199 $cmd =~ /^f\b\s*(.*)/ && do {
200 $file = $1;
201 if (!$file) {
202 print OUT "The old f command is now the r command.\n";
203 print OUT "The new f command switches filenames.\n";
204 next CMD;
a687059c 205 }
a0d0e21e 206 if (!defined $main::{'_<' . $file}) {
207 if (($try) = grep(m#^_<.*$file#, keys %main::)) {
fe14fcc3 208 $file = substr($try,2);
209 print "\n$file:\n";
76854fea 210 }
211 }
a0d0e21e 212 if (!defined $main::{'_<' . $file}) {
fe14fcc3 213 print OUT "There's no code here anything matching $file.\n";
214 next CMD;
215 }
216 elsif ($file ne $filename) {
ed6116ce 217 *dbline = "::_<$file";
fe14fcc3 218 $max = $#dbline;
219 $filename = $file;
220 $start = 1;
221 $cmd = "l";
222 } };
ed6116ce 223 $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
fe14fcc3 224 $subname = $1;
ed6116ce 225 $subname = "main::" . $subname unless $subname =~ /'|::/;
226 $subname = "main" . $subname if substr($subname,0,1)eq "'";
227 $subname = "main" . $subname if substr($subname,0,2)eq "::";
567d72c2 228 # VMS filespecs may (usually do) contain ':', so don't use split
229 ($file,$subrange) = $sub{$subname} =~ /(.*):(.*)/;
fe14fcc3 230 if ($file ne $filename) {
ed6116ce 231 *dbline = "::_<$file";
fe14fcc3 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; };
ed6116ce 302 $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
fe14fcc3 303 $subname = $1;
304 $cond = $2 || '1';
748a9306 305 $subname = "${pkg}::" . $subname
a0d0e21e 306 unless $subname =~ /'|::/;
fe14fcc3 307 $subname = "main" . $subname if substr($subname,0,1) eq "'";
ed6116ce 308 $subname = "main" . $subname if substr($subname,0,2) eq "::";
567d72c2 309 # VMS filespecs may (usually do) contain ':', so don't use split
310 ($filename,$i) = $sub{$subname} =~ /(.*):(.*)/;
83025b21 311 $i += 0;
fe14fcc3 312 if ($i) {
ed6116ce 313 *dbline = "::_<$filename";
fe14fcc3 314 ++$i while $dbline[$i] == 0 && $i < $#dbline;
315 $dbline{$i} =~ s/^[^\0]*/$cond/;
316 } else {
317 print OUT "Subroutine $subname not found.\n";
76854fea 318 }
fe14fcc3 319 next CMD; };
320 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
321 $i = ($1?$1:$line);
322 $cond = $2 || '1';
76854fea 323 if ($dbline[$i] == 0) {
fe14fcc3 324 print OUT "Line $i not breakable.\n";
325 } else {
326 $dbline{$i} =~ s/^[^\0]*/$cond/;
a687059c 327 }
fe14fcc3 328 next CMD; };
329 $cmd =~ /^d\b\s*(\d+)?/ && do {
330 $i = ($1?$1:$line);
331 $dbline{$i} =~ s/^[^\0]*//;
332 delete $dbline{$i} if $dbline{$i} eq '';
333 next CMD; };
334 $cmd =~ /^A$/ && do {
335 for ($i = 1; $i <= $max ; $i++) {
336 if (defined $dbline{$i}) {
337 $dbline{$i} =~ s/\0[^\0]*//;
338 delete $dbline{$i} if $dbline{$i} eq '';
76854fea 339 }
fe14fcc3 340 }
341 next CMD; };
342 $cmd =~ /^<\s*(.*)/ && do {
a0d0e21e 343 $pre = action($1);
fe14fcc3 344 next CMD; };
345 $cmd =~ /^>\s*(.*)/ && do {
a0d0e21e 346 $post = action($1);
fe14fcc3 347 next CMD; };
348 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
349 $i = $1;
350 if ($dbline[$i] == 0) {
351 print OUT "Line $i may not have an action.\n";
352 } else {
353 $dbline{$i} =~ s/\0[^\0]*//;
a0d0e21e 354 $dbline{$i} .= "\0" . action($3);
fe14fcc3 355 }
356 next CMD; };
357 $cmd =~ /^n$/ && do {
358 $single = 2;
359 $laststep = $cmd;
360 last CMD; };
361 $cmd =~ /^s$/ && do {
362 $single = 1;
363 $laststep = $cmd;
364 last CMD; };
365 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
366 $i = $1;
367 if ($i) {
368 if ($dbline[$i] == 0) {
369 print OUT "Line $i not breakable.\n";
370 next CMD;
76854fea 371 }
fe14fcc3 372 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
76854fea 373 }
fe14fcc3 374 for ($i=0; $i <= $#stack; ) {
375 $stack[$i++] &= ~1;
a687059c 376 }
fe14fcc3 377 last CMD; };
378 $cmd =~ /^r$/ && do {
379 $stack[$#stack] |= 2;
380 last CMD; };
381 $cmd =~ /^T$/ && do {
382 local($p,$f,$l,$s,$h,$a,@a,@sub);
383 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
a0d0e21e 384 @a = ();
385 for $arg (@args) {
386 $_ = "$arg";
ed6116ce 387 s/'/\\'/g;
a0d0e21e 388 s/([^\0]*)/'$1'/
389 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
ed6116ce 390 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
391 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
a0d0e21e 392 push(@a, $_);
fe14fcc3 393 }
394 $w = $w ? '@ = ' : '$ = ';
395 $a = $h ? '(' . join(', ', @a) . ')' : '';
a0d0e21e 396 push(@sub, "$w$s$a from file $f line $l\n");
fe14fcc3 397 last if $signal;
a687059c 398 }
fe14fcc3 399 for ($i=0; $i <= $#sub; $i++) {
400 last if $signal;
401 print OUT $sub[$i];
a687059c 402 }
fe14fcc3 403 next CMD; };
404 $cmd =~ /^\/(.*)$/ && do {
405 $inpat = $1;
406 $inpat =~ s:([^\\])/$:$1:;
407 if ($inpat ne "") {
a0d0e21e 408 eval '$inpat =~ m'."\a$inpat\a";
fe14fcc3 409 if ($@ ne "") {
410 print OUT "$@";
411 next CMD;
412 }
413 $pat = $inpat;
a687059c 414 }
fe14fcc3 415 $end = $start;
416 eval '
417 for (;;) {
418 ++$start;
419 $start = 1 if ($start > $max);
420 last if ($start == $end);
a0d0e21e 421 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
f0fcb552 422 if ($emacs) {
423 print OUT "\032\032$filename:$start:0\n";
424 } else {
425 print OUT "$start:\t", $dbline[$start], "\n";
426 }
fe14fcc3 427 last;
428 }
429 } ';
430 print OUT "/$pat/: not found\n" if ($start == $end);
431 next CMD; };
432 $cmd =~ /^\?(.*)$/ && do {
433 $inpat = $1;
434 $inpat =~ s:([^\\])\?$:$1:;
435 if ($inpat ne "") {
a0d0e21e 436 eval '$inpat =~ m'."\a$inpat\a";
fe14fcc3 437 if ($@ ne "") {
438 print OUT "$@";
439 next CMD;
440 }
441 $pat = $inpat;
442 }
443 $end = $start;
444 eval '
445 for (;;) {
446 --$start;
447 $start = $max if ($start <= 0);
448 last if ($start == $end);
a0d0e21e 449 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
f0fcb552 450 if ($emacs) {
451 print OUT "\032\032$filename:$start:0\n";
452 } else {
453 print OUT "$start:\t", $dbline[$start], "\n";
454 }
fe14fcc3 455 last;
456 }
457 } ';
458 print OUT "?$pat?: not found\n" if ($start == $end);
459 next CMD; };
460 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
461 pop(@hist) if length($cmd) > 1;
462 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
463 $cmd = $hist[$i] . "\n";
464 print OUT $cmd;
465 redo CMD; };
466 $cmd =~ /^!(.+)$/ && do {
467 $pat = "^$1";
468 pop(@hist) if length($cmd) > 1;
469 for ($i = $#hist; $i; --$i) {
470 last if $hist[$i] =~ $pat;
471 }
472 if (!$i) {
473 print OUT "No such command!\n\n";
474 next CMD;
475 }
476 $cmd = $hist[$i] . "\n";
477 print OUT $cmd;
478 redo CMD; };
479 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
480 $end = $2?($#hist-$2):0;
481 $hist = 0 if $hist < 0;
482 for ($i=$#hist; $i>$end; $i--) {
483 print OUT "$i: ",$hist[$i],"\n"
484 unless $hist[$i] =~ /^.?$/;
485 };
486 next CMD; };
ed6116ce 487 $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
fe14fcc3 488 $cmd =~ /^=/ && do {
489 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
490 $alias{$k}="s~$k~$v~";
491 print OUT "$k = $v\n";
492 } elsif ($cmd =~ /^=\s*$/) {
493 foreach $k (sort keys(%alias)) {
494 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
495 print OUT "$k = $v\n";
496 } else {
497 print OUT "$k\t$alias{$k}\n";
498 };
33b78306 499 };
500 };
fe14fcc3 501 next CMD; };
502 }
27e2fb84 503 $evalarg = $cmd; &eval;
76854fea 504 print OUT "\n";
a687059c 505 }
506 if ($post) {
27e2fb84 507 $evalarg = $post; &eval;
a687059c 508 }
509 }
748a9306 510 ($@, $!, $,, $/, $\, $^W) = @saved;
511 ();
76854fea 512}
513
514sub save {
a0d0e21e 515 @saved = ($@, $!, $,, $/, $\, $^W);
516 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
76854fea 517}
518
27e2fb84 519# The following takes its argument via $evalarg to preserve current @_
520
76854fea 521sub eval {
ed6116ce 522 eval "$usercontext $evalarg; &DB::save";
76854fea 523 print OUT $@;
a687059c 524}
525
526sub action {
527 local($action) = @_;
528 while ($action =~ s/\\$//) {
529 print OUT "+ ";
76854fea 530 $action .= &gets;
a687059c 531 }
532 $action;
533}
534
76854fea 535sub gets {
536 local($.);
537 <IN>;
538}
539
a687059c 540sub catch {
541 $signal = 1;
542}
543
544sub sub {
545 push(@stack, $single);
546 $single &= 1;
547 $single |= 4 if $#stack == $deep;
a687059c 548 if (wantarray) {
549 @i = &$sub;
79a0689e 550 $single |= pop(@stack);
551 @i;
a687059c 552 }
553 else {
554 $i = &$sub;
79a0689e 555 $single |= pop(@stack);
556 $i;
a687059c 557 }
a687059c 558}
559
a0d0e21e 560$trace = $signal = $single = 0; # uninitialized warning suppression
561
a687059c 562@hist = ('?');
ed6116ce 563$SIG{'INT'} = "DB::catch";
a687059c 564$deep = 100; # warning if stack gets this deep
565$window = 10;
566$preview = 3;
567
568@stack = (0);
76854fea 569@ARGS = @ARGV;
a687059c 570for (@args) {
571 s/'/\\'/g;
572 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
573}
a687059c 574
83025b21 575if (-f $rcfile) {
576 do "./$rcfile";
a687059c 577}
83025b21 578elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
579 do "$ENV{'LOGDIR'}/$rcfile";
a687059c 580}
83025b21 581elsif (-f "$ENV{'HOME'}/$rcfile") {
582 do "$ENV{'HOME'}/$rcfile";
a687059c 583}
584
5851;