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