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.0.1.2 $$Date: 91/11/05 17:55:58 $';
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.0.1.2 91/11/05 17:55:58 lwall
18 # patch11: perldb.pl modified to run within emacs in perldb-mode
20 # Revision 4.0.1.1 91/06/07 11:17:44 lwall
21 # patch4: added $^P variable to control calling of perldb routines
22 # patch4: debugger sometimes listed wrong number of lines for a statement
24 # Revision 4.0 91/03/20 01:25:50 lwall
27 # Revision 3.0.1.6 91/01/11 18:08:58 lwall
28 # patch42: @_ couldn't be accessed from debugger
30 # Revision 3.0.1.5 90/11/10 01:40:26 lwall
31 # patch38: the debugger wouldn't stop correctly or do action routines
33 # Revision 3.0.1.4 90/10/15 17:40:38 lwall
34 # patch29: added caller
35 # patch29: the debugger now understands packages and evals
36 # patch29: scripts now run at almost full speed under the debugger
37 # patch29: more variables are settable from debugger
39 # Revision 3.0.1.3 90/08/09 04:00:58 lwall
40 # patch19: debugger now allows continuation lines
41 # patch19: debugger can now dump lists of variables
42 # patch19: debugger can now add aliases easily from prompt
44 # Revision 3.0.1.2 90/03/12 16:39:39 lwall
45 # patch13: perl -d didn't format stack traces of *foo right
46 # patch13: perl -d wiped out scalar return values of subroutines
48 # Revision 3.0.1.1 89/10/26 23:14:02 lwall
49 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
51 # Revision 3.0 89/10/18 15:19:46 lwall
54 # Revision 2.0 88/06/05 00:09:45 root
55 # Baseline version 2.0.
59 open(IN, "</dev/tty") || open(IN, "<&STDIN"); # so we don't dingle stdin
60 open(OUT,">/dev/tty") || open(OUT, ">&STDOUT"); # so we don't dongle stdout
64 $| = 1; # for real STDOUT
67 # Is Perl being run from Emacs?
68 $emacs = $main'ARGV[$[] eq '-emacs';
69 shift(@main'ARGV) if $emacs;
71 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
72 print OUT "\nLoading DB routines from $header\n";
73 print OUT ("Emacs support ",
74 $emacs ? "enabled" : "available",
76 print OUT "\nEnter h for help.\n\n";
80 ($package, $filename, $line) = caller;
81 $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
82 "package $package;"; # this won't let them modify, alas
83 local($^P) = 0; # don't debug our own evals
84 local(*dbline) = "_<$filename";
86 if (($stop,$action) = split(/\0/,$dbline{$line})) {
91 $evalarg = "\$DB'signal |= do {$stop;}"; &eval;
92 $dbline{$line} =~ s/;9($|\0)/$1/;
95 if ($single || $trace || $signal) {
97 print OUT "\032\032$filename:$line:0\n";
99 print OUT "$package'" unless $sub =~ /'/;
100 print OUT "$sub($filename:$line):\t",$dbline[$line];
101 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
102 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
103 print OUT "$sub($filename:$i):\t",$dbline[$i];
107 $evalarg = $action, &eval if $action;
108 if ($single || $signal) {
109 $evalarg = $pre, &eval if $pre;
110 print OUT $#stack . " levels deep in subroutine calls!\n"
114 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
118 $cmd eq '' && exit 0;
120 $cmd =~ s/\\$// && do {
125 $cmd =~ /^q$/ && exit 0;
126 $cmd =~ /^$/ && ($cmd = $laststep);
127 push(@hist,$cmd) if length($cmd) > 1;
128 ($i) = split(/\s+/,$cmd);
129 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
130 $cmd =~ /^h$/ && do {
134 n Next, steps over subroutine calls.
135 r Return from current subroutine.
136 c [line] Continue; optionally inserts a one-time-only breakpoint
137 at the specified line.
138 <CR> Repeat last n or s.
139 l min+incr List incr+1 lines starting at min.
140 l min-max List lines.
143 - List previous window.
144 w line List window around line.
145 l subname List subroutine.
146 f filename Switch to filename.
147 /pattern/ Search forwards for pattern; final / is optional.
148 ?pattern? Search backwards for pattern.
149 L List breakpoints and actions.
150 S List subroutine names.
153 Set breakpoint; line defaults to the current execution line;
154 condition breaks if it evaluates to true, defaults to \'1\'.
155 b subname [condition]
156 Set breakpoint at first line of subroutine.
157 d [line] Delete breakpoint.
158 D Delete all breakpoints.
160 Set an action to be done before the line is executed.
161 Sequence is: check for breakpoint, print line if necessary,
162 do action, prompt user if breakpoint or step, evaluate line.
163 A Delete all actions.
164 V [pkg [vars]] List some (default all) variables in package (default current).
165 X [vars] Same as \"V currentpackage [vars]\".
166 < command Define command before prompt.
167 > command Define command after prompt.
168 ! number Redo command (default previous command).
169 ! -number Redo number\'th to last command.
170 H -number Display last number commands (default all).
172 p expr Same as \"print DB'OUT expr\" in current package.
173 = [alias value] Define a command alias, or list current aliases.
174 command Execute as a perl statement in current package.
178 $cmd =~ /^t$/ && do {
180 print OUT "Trace = ".($trace?"on":"off")."\n";
182 $cmd =~ /^S$/ && do {
183 foreach $subname (sort(keys %sub)) {
184 print OUT $subname,"\n";
187 $cmd =~ s/^X\b/V $package/;
188 $cmd =~ /^V$/ && do {
189 $cmd = 'V $package'; };
190 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
192 @vars = split(' ',$2);
193 do 'dumpvar.pl' unless defined &main'dumpvar;
194 if (defined &main'dumpvar) {
195 &main'dumpvar($packname,@vars);
198 print DB'OUT "dumpvar.pl not available.\n";
201 $cmd =~ /^f\b\s*(.*)/ && do {
204 print OUT "The old f command is now the r command.\n";
205 print OUT "The new f command switches filenames.\n";
208 if (!defined $_main{'_<' . $file}) {
209 if (($try) = grep(m#^_<.*$file#, keys %_main)) {
210 $file = substr($try,2);
214 if (!defined $_main{'_<' . $file}) {
215 print OUT "There's no code here anything matching $file.\n";
218 elsif ($file ne $filename) {
225 $cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
227 $subname = "main'" . $subname unless $subname =~ /'/;
228 $subname = "main" . $subname if substr($subname,0,1) eq "'";
229 ($file,$subrange) = split(/:/,$sub{$subname});
230 if ($file ne $filename) {
236 if (eval($subrange) < -$window) {
237 $subrange =~ s/-.*/+/;
239 $cmd = "l $subrange";
241 print OUT "Subroutine $1 not found.\n";
244 $cmd =~ /^w\b\s*(\d*)$/ && do {
248 $cmd = 'l ' . $start . '-' . ($start + $incr); };
249 $cmd =~ /^-$/ && do {
251 $cmd = 'l ' . ($start-$window*2) . '+'; };
252 $cmd =~ /^l$/ && do {
254 $cmd = 'l ' . $start . '-' . ($start + $incr); };
255 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
258 $incr = $window - 1 unless $incr;
259 $cmd = 'l ' . $start . '-' . ($start + $incr); };
260 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
261 $end = (!$2) ? $max : ($4 ? $4 : $2);
262 $end = $max if $end > $max;
264 $i = $line if $i eq '.';
267 print OUT "\032\032$filename:$i:0\n";
270 for (; $i <= $end; $i++) {
271 print OUT "$i:\t", $dbline[$i];
275 $start = $i; # remember in case they want more
276 $start = $max if $start > $max;
278 $cmd =~ /^D$/ && do {
279 print OUT "Deleting all breakpoints...\n";
280 for ($i = 1; $i <= $max ; $i++) {
281 if (defined $dbline{$i}) {
282 $dbline{$i} =~ s/^[^\0]+//;
283 if ($dbline{$i} =~ s/^\0?$//) {
289 $cmd =~ /^L$/ && do {
290 for ($i = 1; $i <= $max; $i++) {
291 if (defined $dbline{$i}) {
292 print OUT "$i:\t", $dbline[$i];
293 ($stop,$action) = split(/\0/, $dbline{$i});
294 print OUT " break if (", $stop, ")\n"
296 print OUT " action: ", $action, "\n"
302 $cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
305 $subname = "$package'" . $subname unless $subname =~ /'/;
306 $subname = "main" . $subname if substr($subname,0,1) eq "'";
307 ($filename,$i) = split(/[:-]/, $sub{$subname});
309 *dbline = "_<$filename";
310 ++$i while $dbline[$i] == 0 && $i < $#dbline;
311 $dbline{$i} =~ s/^[^\0]*/$cond/;
313 print OUT "Subroutine $subname not found.\n";
316 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
319 if ($dbline[$i] == 0) {
320 print OUT "Line $i not breakable.\n";
322 $dbline{$i} =~ s/^[^\0]*/$cond/;
325 $cmd =~ /^d\b\s*(\d+)?/ && do {
327 $dbline{$i} =~ s/^[^\0]*//;
328 delete $dbline{$i} if $dbline{$i} eq '';
330 $cmd =~ /^A$/ && do {
331 for ($i = 1; $i <= $max ; $i++) {
332 if (defined $dbline{$i}) {
333 $dbline{$i} =~ s/\0[^\0]*//;
334 delete $dbline{$i} if $dbline{$i} eq '';
338 $cmd =~ /^<\s*(.*)/ && do {
339 $pre = do action($1);
341 $cmd =~ /^>\s*(.*)/ && do {
342 $post = do action($1);
344 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
346 if ($dbline[$i] == 0) {
347 print OUT "Line $i may not have an action.\n";
349 $dbline{$i} =~ s/\0[^\0]*//;
350 $dbline{$i} .= "\0" . do action($3);
353 $cmd =~ /^n$/ && do {
357 $cmd =~ /^s$/ && do {
361 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
364 if ($dbline[$i] == 0) {
365 print OUT "Line $i not breakable.\n";
368 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
370 for ($i=0; $i <= $#stack; ) {
374 $cmd =~ /^r$/ && do {
375 $stack[$#stack] |= 2;
377 $cmd =~ /^T$/ && do {
378 local($p,$f,$l,$s,$h,$a,@a,@sub);
379 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
382 if (/^StB\000/ && length($_) == length($_main{'_main'})) {
383 $_ = sprintf("%s",$_);
387 s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
388 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
389 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
392 $w = $w ? '@ = ' : '$ = ';
393 $a = $h ? '(' . join(', ', @a) . ')' : '';
394 push(@sub, "$w&$s$a from file $f line $l\n");
397 for ($i=0; $i <= $#sub; $i++) {
402 $cmd =~ /^\/(.*)$/ && do {
404 $inpat =~ s:([^\\])/$:$1:;
406 eval '$inpat =~ m'."\n$inpat\n";
417 $start = 1 if ($start > $max);
418 last if ($start == $end);
419 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
421 print OUT "\032\032$filename:$start:0\n";
423 print OUT "$start:\t", $dbline[$start], "\n";
428 print OUT "/$pat/: not found\n" if ($start == $end);
430 $cmd =~ /^\?(.*)$/ && do {
432 $inpat =~ s:([^\\])\?$:$1:;
434 eval '$inpat =~ m'."\n$inpat\n";
445 $start = $max if ($start <= 0);
446 last if ($start == $end);
447 if ($dbline[$start] =~ m'."\n$pat\n".'i) {
449 print OUT "\032\032$filename:$start:0\n";
451 print OUT "$start:\t", $dbline[$start], "\n";
456 print OUT "?$pat?: not found\n" if ($start == $end);
458 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
459 pop(@hist) if length($cmd) > 1;
460 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
461 $cmd = $hist[$i] . "\n";
464 $cmd =~ /^!(.+)$/ && do {
466 pop(@hist) if length($cmd) > 1;
467 for ($i = $#hist; $i; --$i) {
468 last if $hist[$i] =~ $pat;
471 print OUT "No such command!\n\n";
474 $cmd = $hist[$i] . "\n";
477 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
478 $end = $2?($#hist-$2):0;
479 $hist = 0 if $hist < 0;
480 for ($i=$#hist; $i>$end; $i--) {
481 print OUT "$i: ",$hist[$i],"\n"
482 unless $hist[$i] =~ /^.?$/;
485 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
487 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
488 $alias{$k}="s~$k~$v~";
489 print OUT "$k = $v\n";
490 } elsif ($cmd =~ /^=\s*$/) {
491 foreach $k (sort keys(%alias)) {
492 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
493 print OUT "$k = $v\n";
495 print OUT "$k\t$alias{$k}\n";
501 $evalarg = $cmd; &eval;
505 $evalarg = $post; &eval;
508 ($@, $!, $[, $,, $/, $\) = @saved;
512 @saved = ($@, $!, $[, $,, $/, $\);
513 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
516 # The following takes its argument via $evalarg to preserve current @_
519 eval "$usercontext $evalarg; &DB'save";
525 while ($action =~ s/\\$//) {
542 push(@stack, $single);
544 $single |= 4 if $#stack == $deep;
547 $single |= pop(@stack);
552 $single |= pop(@stack);
557 $single = 1; # so it stops on first executable statement
559 $SIG{'INT'} = "DB'catch";
560 $deep = 100; # warning if stack gets this deep
568 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
574 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
575 do "$ENV{'LOGDIR'}/.perldb";
577 elsif (-f "$ENV{'HOME'}/.perldb") {
578 do "$ENV{'HOME'}/.perldb";