3 $header = '$Header: perldb.pl,v 3.0.1.5 90/11/10 01:40:26 lwall Locked $';
5 # This file is automatically included if you do perl -d.
6 # It's probably not useful to include this yourself.
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.
13 # Revision 3.0.1.5 90/11/10 01:40:26 lwall
14 # patch38: the debugger wouldn't stop correctly or do action routines
16 # Revision 3.0.1.4 90/10/15 17:40:38 lwall
17 # patch29: added caller
18 # patch29: the debugger now understands packages and evals
19 # patch29: scripts now run at almost full speed under the debugger
20 # patch29: more variables are settable from debugger
22 # Revision 3.0.1.3 90/08/09 04:00:58 lwall
23 # patch19: debugger now allows continuation lines
24 # patch19: debugger can now dump lists of variables
25 # patch19: debugger can now add aliases easily from prompt
27 # Revision 3.0.1.2 90/03/12 16:39:39 lwall
28 # patch13: perl -d didn't format stack traces of *foo right
29 # patch13: perl -d wiped out scalar return values of subroutines
31 # Revision 3.0.1.1 89/10/26 23:14:02 lwall
32 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
34 # Revision 3.0 89/10/18 15:19:46 lwall
37 # Revision 2.0 88/06/05 00:09:45 root
38 # Baseline version 2.0.
42 open(IN, "</dev/tty") || open(IN, "<&STDIN"); # so we don't dingle stdin
43 open(OUT,">/dev/tty") || open(OUT, ">&STDOUT"); # so we don't dongle stdout
47 $| = 1; # for real STDOUT
50 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
51 print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n";
55 ($package, $filename, $line) = caller;
56 $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
57 "package $package;"; # this won't let them modify, alas
58 local(*dbline) = "_<$filename";
60 if (($stop,$action) = split(/\0/,$dbline{$line})) {
65 &eval("\$DB'signal |= do {$stop;}");
66 $dbline{$line} =~ s/;9($|\0)/$1/;
69 if ($single || $trace || $signal) {
70 print OUT "$package'" unless $sub =~ /'/;
71 print OUT "$sub($filename:$line):\t",$dbline[$line];
72 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
73 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
74 print OUT "$sub($filename:$i):\t",$dbline[$i];
77 &eval($action) if $action;
78 if ($single || $signal) {
80 print OUT $#stack . " levels deep in subroutine calls!\n"
83 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
88 $cmd =~ s/\\$// && do {
93 $cmd =~ /^q$/ && exit 0;
94 $cmd =~ /^$/ && ($cmd = $laststep);
95 push(@hist,$cmd) if length($cmd) > 1;
96 ($i) = split(/\s+/,$cmd);
97 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
102 n Next, steps over subroutine calls.
103 r Return from current subroutine.
104 c [line] Continue; optionally inserts a one-time-only breakpoint
105 at the specified line.
106 <CR> Repeat last n or s.
107 l min+incr List incr+1 lines starting at min.
108 l min-max List lines.
111 - List previous window.
112 w line List window around line.
113 l subname List subroutine.
114 f filename Switch to filename.
115 /pattern/ Search forwards for pattern; final / is optional.
116 ?pattern? Search backwards for pattern.
117 L List breakpoints and actions.
118 S List subroutine names.
121 Set breakpoint; line defaults to the current execution line;
122 condition breaks if it evaluates to true, defaults to \'1\'.
123 b subname [condition]
124 Set breakpoint at first line of subroutine.
125 d [line] Delete breakpoint.
126 D Delete all breakpoints.
128 Set an action to be done before the line is executed.
129 Sequence is: check for breakpoint, print line if necessary,
130 do action, prompt user if breakpoint or step, evaluate line.
131 A Delete all actions.
132 V [pkg [vars]] List some (default all) variables in package (default current).
133 X [vars] Same as \"V currentpackage [vars]\".
134 < command Define command before prompt.
135 > command Define command after prompt.
136 ! number Redo command (default previous command).
137 ! -number Redo number\'th to last command.
138 H -number Display last number commands (default all).
140 p expr Same as \"print DB'OUT expr\" in current package.
141 = [alias value] Define a command alias, or list current aliases.
142 command Execute as a perl statement in current package.
146 $cmd =~ /^t$/ && do {
148 print OUT "Trace = ".($trace?"on":"off")."\n";
150 $cmd =~ /^S$/ && do {
151 foreach $subname (sort(keys %sub)) {
152 print OUT $subname,"\n";
155 $cmd =~ s/^X\b/V $package/;
156 $cmd =~ /^V$/ && do {
157 $cmd = 'V $package'; };
158 $cmd =~ /^V\s*(\S+)\s*(.*)/ && do {
160 @vars = split(' ',$2);
161 do 'dumpvar.pl' unless defined &main'dumpvar;
162 if (defined &main'dumpvar) {
163 &main'dumpvar($packname,@vars);
166 print DB'OUT "dumpvar.pl not available.\n";
169 $cmd =~ /^f\s*(.*)/ && do {
172 print OUT "The old f command is now the r command.\n";
173 print OUT "The new f command switches filenames.\n";
176 if (!defined $_main{'_<' . $file}) {
177 if (($try) = grep(m#^_<.*$file#, keys %_main)) {
178 $file = substr($try,2);
182 if (!defined $_main{'_<' . $file}) {
183 print OUT "There's no code here anything matching $file.\n";
186 elsif ($file ne $filename) {
193 $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
195 $subname = "main'" . $subname unless $subname =~ /'/;
196 $subname = "main" . $subname if substr($subname,0,1) eq "'";
197 ($file,$subrange) = split(/:/,$sub{$subname});
198 if ($file ne $filename) {
204 if (eval($subrange) < -$window) {
205 $subrange =~ s/-.*/+/;
207 $cmd = "l $subrange";
209 print OUT "Subroutine $1 not found.\n";
212 $cmd =~ /^w\s*(\d*)$/ && do {
216 $cmd = 'l ' . $start . '-' . ($start + $incr); };
217 $cmd =~ /^-$/ && do {
219 $cmd = 'l ' . ($start-$window*2) . '+'; };
220 $cmd =~ /^l$/ && do {
222 $cmd = 'l ' . $start . '-' . ($start + $incr); };
223 $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
226 $incr = $window - 1 unless $incr;
227 $cmd = 'l ' . $start . '-' . ($start + $incr); };
228 $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
229 $end = (!$2) ? $max : ($4 ? $4 : $2);
230 $end = $max if $end > $max;
232 $i = $line if $i eq '.';
234 for (; $i <= $end; $i++) {
235 print OUT "$i:\t", $dbline[$i];
238 $start = $i; # remember in case they want more
239 $start = $max if $start > $max;
241 $cmd =~ /^D$/ && do {
242 print OUT "Deleting all breakpoints...\n";
243 for ($i = 1; $i <= $max ; $i++) {
244 if (defined $dbline{$i}) {
245 $dbline{$i} =~ s/^[^\0]+//;
246 if ($dbline{$i} =~ s/^\0?$//) {
252 $cmd =~ /^L$/ && do {
253 for ($i = 1; $i <= $max; $i++) {
254 if (defined $dbline{$i}) {
255 print OUT "$i:\t", $dbline[$i];
256 ($stop,$action) = split(/\0/, $dbline{$i});
257 print OUT " break if (", $stop, ")\n"
259 print OUT " action: ", $action, "\n"
265 $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
268 $subname = "$package'" . $subname unless $subname =~ /'/;
269 $subname = "main" . $subname if substr($subname,0,1) eq "'";
270 ($filename,$i) = split(/[:-]/, $sub{$subname});
272 *dbline = "_<$filename";
273 ++$i while $dbline[$i] == 0 && $i < $#dbline;
274 $dbline{$i} =~ s/^[^\0]*/$cond/;
276 print OUT "Subroutine $subname not found.\n";
279 $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
282 if ($dbline[$i] == 0) {
283 print OUT "Line $i not breakable.\n";
285 $dbline{$i} =~ s/^[^\0]*/$cond/;
288 $cmd =~ /^d\s*(\d+)?/ && do {
290 $dbline{$i} =~ s/^[^\0]*//;
291 delete $dbline{$i} if $dbline{$i} eq '';
293 $cmd =~ /^A$/ && do {
294 for ($i = 1; $i <= $max ; $i++) {
295 if (defined $dbline{$i}) {
296 $dbline{$i} =~ s/\0[^\0]*//;
297 delete $dbline{$i} if $dbline{$i} eq '';
301 $cmd =~ /^<\s*(.*)/ && do {
302 $pre = do action($1);
304 $cmd =~ /^>\s*(.*)/ && do {
305 $post = do action($1);
307 $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
309 if ($dbline[$i] == 0) {
310 print OUT "Line $i may not have an action.\n";
312 $dbline{$i} =~ s/\0[^\0]*//;
313 $dbline{$i} .= "\0" . do action($3);
316 $cmd =~ /^n$/ && do {
320 $cmd =~ /^s$/ && do {
324 $cmd =~ /^c\s*(\d*)\s*$/ && do {
327 if ($dbline[$i] == 0) {
328 print OUT "Line $i not breakable.\n";
331 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
333 for ($i=0; $i <= $#stack; ) {
337 $cmd =~ /^r$/ && do {
338 $stack[$#stack] |= 2;
340 $cmd =~ /^T$/ && do {
341 local($p,$f,$l,$s,$h,$a,@a,@sub);
342 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
345 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
346 $_ = sprintf("%s",$_);
350 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
351 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
352 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
355 $w = $w ? '@ = ' : '$ = ';
356 $a = $h ? '(' . join(', ', @a) . ')' : '';
357 push(@sub, "$w&$s$a from file $f line $l\n");
360 for ($i=0; $i <= $#sub; $i++) {
365 $cmd =~ /^\/(.*)$/ && do {
367 $inpat =~ s:([^\\])/$:$1:;
369 eval '$inpat =~ m'."\n$inpat\n";
380 $start = 1 if ($start > $max);
381 last if ($start == $end);
382 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
383 print OUT "$start:\t", $dbline[$start], "\n";
387 print OUT "/$pat/: not found\n" if ($start == $end);
389 $cmd =~ /^\?(.*)$/ && do {
391 $inpat =~ s:([^\\])\?$:$1:;
393 eval '$inpat =~ m'."\n$inpat\n";
404 $start = $max if ($start <= 0);
405 last if ($start == $end);
406 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
407 print OUT "$start:\t", $dbline[$start], "\n";
411 print OUT "?$pat?: not found\n" if ($start == $end);
413 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
414 pop(@hist) if length($cmd) > 1;
415 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
416 $cmd = $hist[$i] . "\n";
419 $cmd =~ /^!(.+)$/ && do {
421 pop(@hist) if length($cmd) > 1;
422 for ($i = $#hist; $i; --$i) {
423 last if $hist[$i] =~ $pat;
426 print OUT "No such command!\n\n";
429 $cmd = $hist[$i] . "\n";
432 $cmd =~ /^H\s*(-(\d+))?/ && do {
433 $end = $2?($#hist-$2):0;
434 $hist = 0 if $hist < 0;
435 for ($i=$#hist; $i>$end; $i--) {
436 print OUT "$i: ",$hist[$i],"\n"
437 unless $hist[$i] =~ /^.?$/;
440 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
442 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
443 $alias{$k}="s~$k~$v~";
444 print OUT "$k = $v\n";
445 } elsif ($cmd =~ /^=\s*$/) {
446 foreach $k (sort keys(%alias)) {
447 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
448 print OUT "$k = $v\n";
450 print OUT "$k\t$alias{$k}\n";
462 ($@, $!, $[, $,, $/, $\) = @saved;
466 @saved = ($@, $!, $[, $,, $/, $\);
467 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
471 eval "$usercontext $_[0]; &DB'save";
477 while ($action =~ s/\\$//) {
494 push(@stack, $single);
496 $single |= 4 if $#stack == $deep;
499 $single |= pop(@stack);
504 $single |= pop(@stack);
509 $single = 1; # so it stops on first executable statement
511 $SIG{'INT'} = "DB'catch";
512 $deep = 100; # warning if stack gets this deep
520 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
526 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
527 do "$ENV{'LOGDIR'}/.perldb";
529 elsif (-f "$ENV{'HOME'}/.perldb") {
530 do "$ENV{'HOME'}/.perldb";