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