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