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
7 $header = '$RCSfile: perldb.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
9 # This file is automatically included if you do perl -d.
10 # It's probably not useful to include this yourself.
12 # Perl supplies the values for @line and %sub. It effectively inserts
13 # a do DB'DB(<linenum>); in front of every place that can
14 # have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
17 # Revision 4.1 92/08/07 18:24:07 lwall
19 # Revision 4.0.1.3 92/06/08 13:43:57 lwall
20 # patch20: support for MSDOS folded into perldb.pl
21 # patch20: perldb couldn't debug file containing '-', such as STDIN designator
23 # Revision 4.0.1.2 91/11/05 17:55:58 lwall
24 # patch11: perldb.pl modified to run within emacs in perldb-mode
26 # Revision 4.0.1.1 91/06/07 11:17:44 lwall
27 # patch4: added $^P variable to control calling of perldb routines
28 # patch4: debugger sometimes listed wrong number of lines for a statement
30 # Revision 4.0 91/03/20 01:25:50 lwall
33 # Revision 3.0.1.6 91/01/11 18:08:58 lwall
34 # patch42: @_ couldn't be accessed from debugger
36 # Revision 3.0.1.5 90/11/10 01:40:26 lwall
37 # patch38: the debugger wouldn't stop correctly or do action routines
39 # Revision 3.0.1.4 90/10/15 17:40:38 lwall
40 # patch29: added caller
41 # patch29: the debugger now understands packages and evals
42 # patch29: scripts now run at almost full speed under the debugger
43 # patch29: more variables are settable from debugger
45 # Revision 3.0.1.3 90/08/09 04:00:58 lwall
46 # patch19: debugger now allows continuation lines
47 # patch19: debugger can now dump lists of variables
48 # patch19: debugger can now add aliases easily from prompt
50 # Revision 3.0.1.2 90/03/12 16:39:39 lwall
51 # patch13: perl -d didn't format stack traces of *foo right
52 # patch13: perl -d wiped out scalar return values of subroutines
54 # Revision 3.0.1.1 89/10/26 23:14:02 lwall
55 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
57 # Revision 3.0 89/10/18 15:19:46 lwall
60 # Revision 2.0 88/06/05 00:09:45 root
61 # Baseline version 2.0.
66 $console = "/dev/tty";
74 open(IN, "<$console") || open(IN, "<&STDIN"); # so we don't dingle stdin
75 open(OUT,">$console") || open(OUT, ">&STDOUT"); # so we don't dongle stdout
79 $| = 1; # for real STDOUT
82 # Is Perl being run from Emacs?
83 $emacs = $main'ARGV[$[] eq '-emacs';
84 shift(@main'ARGV) if $emacs;
86 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
87 print OUT "\nLoading DB routines from $header\n";
88 print OUT ("Emacs support ",
89 $emacs ? "enabled" : "available",
91 print OUT "\nEnter h for help.\n\n";
95 ($package, $filename, $line) = caller;
96 $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
97 "package $package;"; # this won't let them modify, alas
98 local($^P) = 0; # don't debug our own evals
99 local(*dbline) = "_<$filename";
101 if (($stop,$action) = split(/\0/,$dbline{$line})) {
106 $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
107 $dbline{$line} =~ s/;9($|\0)/$1/;
110 if ($single || $trace || $signal) {
112 print OUT "\032\032$filename:$line:0\n";
114 print OUT "$package'" unless $sub =~ /'/;
115 print OUT "$sub($filename:$line):\t",$dbline[$line];
116 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
117 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
118 print OUT "$sub($filename:$i):\t",$dbline[$i];
122 $evalarg = $action, &eval if $action;
123 if ($single || $signal) {
124 $evalarg = $pre, &eval if $pre;
125 print OUT $#stack . " levels deep in subroutine calls!\n"
129 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
133 $cmd eq '' && exit 0;
135 $cmd =~ s/\\$// && do {
140 $cmd =~ /^q$/ && exit 0;
141 $cmd =~ /^$/ && ($cmd = $laststep);
142 push(@hist,$cmd) if length($cmd) > 1;
143 ($i) = split(/\s+/,$cmd);
144 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
145 $cmd =~ /^h$/ && do {
149 n Next, steps over subroutine calls.
150 r Return from current subroutine.
151 c [line] Continue; optionally inserts a one-time-only breakpoint
152 at the specified line.
153 <CR> Repeat last n or s.
154 l min+incr List incr+1 lines starting at min.
155 l min-max List lines.
158 - List previous window.
159 w line List window around line.
160 l subname List subroutine.
161 f filename Switch to filename.
162 /pattern/ Search forwards for pattern; final / is optional.
163 ?pattern? Search backwards for pattern.
164 L List breakpoints and actions.
165 S List subroutine names.
168 Set breakpoint; line defaults to the current execution line;
169 condition breaks if it evaluates to true, defaults to \'1\'.
170 b subname [condition]
171 Set breakpoint at first line of subroutine.
172 d [line] Delete breakpoint.
173 D Delete all breakpoints.
175 Set an action to be done before the line is executed.
176 Sequence is: check for breakpoint, print line if necessary,
177 do action, prompt user if breakpoint or step, evaluate line.
178 A Delete all actions.
179 V [pkg [vars]] List some (default all) variables in package (default current).
180 X [vars] Same as \"V currentpackage [vars]\".
181 < command Define command before prompt.
182 > command Define command after prompt.
183 ! number Redo command (default previous command).
184 ! -number Redo number\'th to last command.
185 H -number Display last number commands (default all).
187 p expr Same as \"print DB'OUT expr\" in current package.
188 = [alias value] Define a command alias, or list current aliases.
189 command Execute as a perl statement in current package.
193 $cmd =~ /^t$/ && do {
195 print OUT "Trace = ".($trace?"on":"off")."\n";
197 $cmd =~ /^S$/ && do {
198 foreach $subname (sort(keys %sub)) {
199 print OUT $subname,"\n";
202 $cmd =~ s/^X\b/V $package/;
203 $cmd =~ /^V$/ && do {
204 $cmd = "V $package"; };
205 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
206 local ($savout) = select(OUT);
208 @vars = split(' ',$2);
209 do 'dumpvar.pl' unless defined &main'dumpvar;
210 if (defined &main'dumpvar) {
211 &main'dumpvar($packname,@vars);
214 print DB'OUT "dumpvar.pl not available.\n";
218 $cmd =~ /^f\b\s*(.*)/ && do {
221 print OUT "The old f command is now the r command.\n";
222 print OUT "The new f command switches filenames.\n";
225 if (!defined $_main{'_<' . $file}) {
226 if (($try) = grep(m#^_<.*$file#, keys %_main)) {
227 $file = substr($try,2);
231 if (!defined $_main{'_<' . $file}) {
232 print OUT "There's no code here anything matching $file.\n";
235 elsif ($file ne $filename) {
242 $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
244 $subname = "main'" . $subname unless $subname =~ /'/;
245 $subname = "main" . $subname if substr($subname,0,1) eq "'";
246 ($file,$subrange) = split(/:/,$sub{$subname});
247 if ($file ne $filename) {
253 if (eval($subrange) < -$window) {
254 $subrange =~ s/-.*/+/;
256 $cmd = "l $subrange";
258 print OUT "Subroutine $1 not found.\n";
261 $cmd =~ /^w\b\s*(\d*)$/ && do {
265 $cmd = 'l ' . $start . '-' . ($start + $incr); };
266 $cmd =~ /^-$/ && do {
268 $cmd = 'l ' . ($start-$window*2) . '+'; };
269 $cmd =~ /^l$/ && do {
271 $cmd = 'l ' . $start . '-' . ($start + $incr); };
272 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
275 $incr = $window - 1 unless $incr;
276 $cmd = 'l ' . $start . '-' . ($start + $incr); };
277 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
278 $end = (!$2) ? $max : ($4 ? $4 : $2);
279 $end = $max if $end > $max;
281 $i = $line if $i eq '.';
284 print OUT "\032\032$filename:$i:0\n";
287 for (; $i <= $end; $i++) {
288 print OUT "$i:\t", $dbline[$i];
292 $start = $i; # remember in case they want more
293 $start = $max if $start > $max;
295 $cmd =~ /^D$/ && do {
296 print OUT "Deleting all breakpoints...\n";
297 for ($i = 1; $i <= $max ; $i++) {
298 if (defined $dbline{$i}) {
299 $dbline{$i} =~ s/^[^\0]+//;
300 if ($dbline{$i} =~ s/^\0?$//) {
306 $cmd =~ /^L$/ && do {
307 for ($i = 1; $i <= $max; $i++) {
308 if (defined $dbline{$i}) {
309 print OUT "$i:\t", $dbline[$i];
310 ($stop,$action) = split(/\0/, $dbline{$i});
311 print OUT " break if (", $stop, ")\n"
313 print OUT " action: ", $action, "\n"
319 $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
322 $subname = "$package'" . $subname unless $subname =~ /'/;
323 $subname = "main" . $subname if substr($subname,0,1) eq "'";
324 ($filename,$i) = split(/:/, $sub{$subname});
327 *dbline = "_<$filename";
328 ++$i while $dbline[$i] == 0 && $i < $#dbline;
329 $dbline{$i} =~ s/^[^\0]*/$cond/;
331 print OUT "Subroutine $subname not found.\n";
334 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
337 if ($dbline[$i] == 0) {
338 print OUT "Line $i not breakable.\n";
340 $dbline{$i} =~ s/^[^\0]*/$cond/;
343 $cmd =~ /^d\b\s*(\d+)?/ && do {
345 $dbline{$i} =~ s/^[^\0]*//;
346 delete $dbline{$i} if $dbline{$i} eq '';
348 $cmd =~ /^A$/ && do {
349 for ($i = 1; $i <= $max ; $i++) {
350 if (defined $dbline{$i}) {
351 $dbline{$i} =~ s/\0[^\0]*//;
352 delete $dbline{$i} if $dbline{$i} eq '';
356 $cmd =~ /^<\s*(.*)/ && do {
357 $pre = do action($1);
359 $cmd =~ /^>\s*(.*)/ && do {
360 $post = do action($1);
362 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
364 if ($dbline[$i] == 0) {
365 print OUT "Line $i may not have an action.\n";
367 $dbline{$i} =~ s/\0[^\0]*//;
368 $dbline{$i} .= "\0" . do action($3);
371 $cmd =~ /^n$/ && do {
375 $cmd =~ /^s$/ && do {
379 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
382 if ($dbline[$i] == 0) {
383 print OUT "Line $i not breakable.\n";
386 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
388 for ($i=0; $i <= $#stack; ) {
392 $cmd =~ /^r$/ && do {
393 $stack[$#stack] |= 2;
395 $cmd =~ /^T$/ && do {
396 local($p,$f,$l,$s,$h,$a,@a,@sub);
397 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
400 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
401 $_ = sprintf("%s",$_);
405 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
406 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
407 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
410 $w = $w ? '@ = ' : '$ = ';
411 $a = $h ? '(' . join(', ', @a) . ')' : '';
412 push(@sub, "$w&$s$a from file $f line $l\n");
415 for ($i=0; $i <= $#sub; $i++) {
420 $cmd =~ /^\/(.*)$/ && do {
422 $inpat =~ s:([^\\])/$:$1:;
424 eval '$inpat =~ m'."\n$inpat\n";
435 $start = 1 if ($start > $max);
436 last if ($start == $end);
437 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
439 print OUT "\032\032$filename:$start:0\n";
441 print OUT "$start:\t", $dbline[$start], "\n";
446 print OUT "/$pat/: not found\n" if ($start == $end);
448 $cmd =~ /^\?(.*)$/ && do {
450 $inpat =~ s:([^\\])\?$:$1:;
452 eval '$inpat =~ m'."\n$inpat\n";
463 $start = $max if ($start <= 0);
464 last if ($start == $end);
465 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
467 print OUT "\032\032$filename:$start:0\n";
469 print OUT "$start:\t", $dbline[$start], "\n";
474 print OUT "?$pat?: not found\n" if ($start == $end);
476 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
477 pop(@hist) if length($cmd) > 1;
478 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
479 $cmd = $hist[$i] . "\n";
482 $cmd =~ /^!(.+)$/ && do {
484 pop(@hist) if length($cmd) > 1;
485 for ($i = $#hist; $i; --$i) {
486 last if $hist[$i] =~ $pat;
489 print OUT "No such command!\n\n";
492 $cmd = $hist[$i] . "\n";
495 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
496 $end = $2?($#hist-$2):0;
497 $hist = 0 if $hist < 0;
498 for ($i=$#hist; $i>$end; $i--) {
499 print OUT "$i: ",$hist[$i],"\n"
500 unless $hist[$i] =~ /^.?$/;
503 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
505 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
506 $alias{$k}="s~$k~$v~";
507 print OUT "$k = $v\n";
508 } elsif ($cmd =~ /^=\s*$/) {
509 foreach $k (sort keys(%alias)) {
510 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
511 print OUT "$k = $v\n";
513 print OUT "$k\t$alias{$k}\n";
519 $evalarg = $cmd; &eval;
523 $evalarg = $post; &eval;
526 ($@, $!, $[, $,, $/, $\) = @saved;
530 @saved = ($@, $!, $[, $,, $/, $\);
531 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
534 # The following takes its argument via $evalarg to preserve current @_
537 eval "$usercontext $evalarg; &DB'save";
543 while ($action =~ s/\\$//) {
560 push(@stack, $single);
562 $single |= 4 if $#stack == $deep;
565 $single |= pop(@stack);
570 $single |= pop(@stack);
575 $single = 1; # so it stops on first executable statement
577 $SIG{'INT'} = "DB'catch";
578 $deep = 100; # warning if stack gets this deep
586 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
592 elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
593 do "$ENV{'LOGDIR'}/$rcfile";
595 elsif (-f "$ENV{'HOME'}/$rcfile") {
596 do "$ENV{'HOME'}/$rcfile";