3 $header = '$Header: perldb.pl,v 4.0 91/03/20 01:25:50 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 4.0 91/03/20 01:25:50 lwall
16 # Revision 3.0.1.6 91/01/11 18:08:58 lwall
17 # patch42: @_ couldn't be accessed from debugger
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
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
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
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
37 # Revision 3.0.1.1 89/10/26 23:14:02 lwall
38 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
40 # Revision 3.0 89/10/18 15:19:46 lwall
43 # Revision 2.0 88/06/05 00:09:45 root
44 # Baseline version 2.0.
48 open(IN, "</dev/tty") || open(IN, "<&STDIN"); # so we don't dingle stdin
49 open(OUT,">/dev/tty") || open(OUT, ">&STDOUT"); # so we don't dongle stdout
53 $| = 1; # for real STDOUT
56 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
57 print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n";
61 ($package, $filename, $line) = caller;
62 $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
63 "package $package;"; # this won't let them modify, alas
64 local(*dbline) = "_<$filename";
66 if (($stop,$action) = split(/\0/,$dbline{$line})) {
71 $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
72 $dbline{$line} =~ s/;9($|\0)/$1/;
75 if ($single || $trace || $signal) {
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];
83 $evalarg = $action, &eval if $action;
84 if ($single || $signal) {
85 $evalarg = $pre, &eval if $pre;
86 print OUT $#stack . " levels deep in subroutine calls!\n"
90 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
96 $cmd =~ s/\\$// && do {
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 {
110 n Next, steps over subroutine calls.
111 r Return from current subroutine.
112 c [line] Continue; optionally inserts a one-time-only breakpoint
113 at the specified line.
114 <CR> Repeat last n or s.
115 l min+incr List incr+1 lines starting at min.
116 l min-max List lines.
119 - List previous window.
120 w line List window around line.
121 l subname List subroutine.
122 f filename Switch to filename.
123 /pattern/ Search forwards for pattern; final / is optional.
124 ?pattern? Search backwards for pattern.
125 L List breakpoints and actions.
126 S List subroutine names.
129 Set breakpoint; line defaults to the current execution line;
130 condition breaks if it evaluates to true, defaults to \'1\'.
131 b subname [condition]
132 Set breakpoint at first line of subroutine.
133 d [line] Delete breakpoint.
134 D Delete all breakpoints.
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.
139 A Delete all actions.
140 V [pkg [vars]] List some (default all) variables in package (default current).
141 X [vars] Same as \"V currentpackage [vars]\".
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.
146 H -number Display last number commands (default all).
148 p expr Same as \"print DB'OUT expr\" in current package.
149 = [alias value] Define a command alias, or list current aliases.
150 command Execute as a perl statement in current package.
154 $cmd =~ /^t$/ && do {
156 print OUT "Trace = ".($trace?"on":"off")."\n";
158 $cmd =~ /^S$/ && do {
159 foreach $subname (sort(keys %sub)) {
160 print OUT $subname,"\n";
163 $cmd =~ s/^X\b/V $package/;
164 $cmd =~ /^V$/ && do {
165 $cmd = 'V $package'; };
166 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
168 @vars = split(' ',$2);
169 do 'dumpvar.pl' unless defined &main'dumpvar;
170 if (defined &main'dumpvar) {
171 &main'dumpvar($packname,@vars);
174 print DB'OUT "dumpvar.pl not available.\n";
177 $cmd =~ /^f\b\s*(.*)/ && do {
180 print OUT "The old f command is now the r command.\n";
181 print OUT "The new f command switches filenames.\n";
184 if (!defined $_main{'_<' . $file}) {
185 if (($try) = grep(m#^_<.*$file#, keys %_main)) {
186 $file = substr($try,2);
190 if (!defined $_main{'_<' . $file}) {
191 print OUT "There's no code here anything matching $file.\n";
194 elsif ($file ne $filename) {
201 $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
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) {
212 if (eval($subrange) < -$window) {
213 $subrange =~ s/-.*/+/;
215 $cmd = "l $subrange";
217 print OUT "Subroutine $1 not found.\n";
220 $cmd =~ /^w\b\s*(\d*)$/ && do {
224 $cmd = 'l ' . $start . '-' . ($start + $incr); };
225 $cmd =~ /^-$/ && do {
227 $cmd = 'l ' . ($start-$window*2) . '+'; };
228 $cmd =~ /^l$/ && do {
230 $cmd = 'l ' . $start . '-' . ($start + $incr); };
231 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
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;
240 $i = $line if $i eq '.';
242 for (; $i <= $end; $i++) {
243 print OUT "$i:\t", $dbline[$i];
246 $start = $i; # remember in case they want more
247 $start = $max if $start > $max;
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?$//) {
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"
267 print OUT " action: ", $action, "\n"
273 $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
276 $subname = "$package'" . $subname unless $subname =~ /'/;
277 $subname = "main" . $subname if substr($subname,0,1) eq "'";
278 ($filename,$i) = split(/[:-]/, $sub{$subname});
280 *dbline = "_<$filename";
281 ++$i while $dbline[$i] == 0 && $i < $#dbline;
282 $dbline{$i} =~ s/^[^\0]*/$cond/;
284 print OUT "Subroutine $subname not found.\n";
287 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
290 if ($dbline[$i] == 0) {
291 print OUT "Line $i not breakable.\n";
293 $dbline{$i} =~ s/^[^\0]*/$cond/;
296 $cmd =~ /^d\b\s*(\d+)?/ && do {
298 $dbline{$i} =~ s/^[^\0]*//;
299 delete $dbline{$i} if $dbline{$i} eq '';
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 '';
309 $cmd =~ /^<\s*(.*)/ && do {
310 $pre = do action($1);
312 $cmd =~ /^>\s*(.*)/ && do {
313 $post = do action($1);
315 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
317 if ($dbline[$i] == 0) {
318 print OUT "Line $i may not have an action.\n";
320 $dbline{$i} =~ s/\0[^\0]*//;
321 $dbline{$i} .= "\0" . do action($3);
324 $cmd =~ /^n$/ && do {
328 $cmd =~ /^s$/ && do {
332 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
335 if ($dbline[$i] == 0) {
336 print OUT "Line $i not breakable.\n";
339 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
341 for ($i=0; $i <= $#stack; ) {
345 $cmd =~ /^r$/ && do {
346 $stack[$#stack] |= 2;
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++) {
353 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
354 $_ = sprintf("%s",$_);
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;
363 $w = $w ? '@ = ' : '$ = ';
364 $a = $h ? '(' . join(', ', @a) . ')' : '';
365 push(@sub, "$w&$s$a from file $f line $l\n");
368 for ($i=0; $i <= $#sub; $i++) {
373 $cmd =~ /^\/(.*)$/ && do {
375 $inpat =~ s:([^\\])/$:$1:;
377 eval '$inpat =~ m'."\n$inpat\n";
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";
395 print OUT "/$pat/: not found\n" if ($start == $end);
397 $cmd =~ /^\?(.*)$/ && do {
399 $inpat =~ s:([^\\])\?$:$1:;
401 eval '$inpat =~ m'."\n$inpat\n";
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";
419 print OUT "?$pat?: not found\n" if ($start == $end);
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";
427 $cmd =~ /^!(.+)$/ && do {
429 pop(@hist) if length($cmd) > 1;
430 for ($i = $#hist; $i; --$i) {
431 last if $hist[$i] =~ $pat;
434 print OUT "No such command!\n\n";
437 $cmd = $hist[$i] . "\n";
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] =~ /^.?$/;
448 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
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";
458 print OUT "$k\t$alias{$k}\n";
464 $evalarg = $cmd; &eval;
468 $evalarg = $post; &eval;
471 ($@, $!, $[, $,, $/, $\) = @saved;
475 @saved = ($@, $!, $[, $,, $/, $\);
476 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
479 # The following takes its argument via $evalarg to preserve current @_
482 eval "$usercontext $evalarg; &DB'save";
488 while ($action =~ s/\\$//) {
505 push(@stack, $single);
507 $single |= 4 if $#stack == $deep;
510 $single |= pop(@stack);
515 $single |= pop(@stack);
520 $single = 1; # so it stops on first executable statement
522 $SIG{'INT'} = "DB'catch";
523 $deep = 100; # warning if stack gets this deep
531 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
537 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
538 do "$ENV{'LOGDIR'}/.perldb";
540 elsif (-f "$ENV{'HOME'}/.perldb") {
541 do "$ENV{'HOME'}/.perldb";