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: perl5db.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 &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.
21 $console = "/dev/tty";
29 $console = "sys\$command";
33 open(IN, "<$console") || open(IN, "<&STDIN"); # so we don't dingle stdin
34 open(OUT,">$console") || open(OUT, ">&STDERR")
35 || open(OUT, ">&STDOUT"); # so we don't dongle stdout
39 $| = 1; # for real STDOUT
42 # Is Perl being run from Emacs?
43 $emacs = $main::ARGV[0] eq '-emacs';
44 shift(@main::ARGV) if $emacs;
46 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
47 print OUT "\nLoading DB routines from $header\n";
48 print OUT ("Emacs support ",
49 $emacs ? "enabled" : "available",
51 print OUT "\nEnter h for help.\n\n";
57 ($pkg, $filename, $line) = caller;
58 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
59 "package $pkg;"; # this won't let them modify, alas
60 local(*dbline) = "::_<$filename";
62 if (($stop,$action) = split(/\0/,$dbline{$line})) {
67 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
68 $dbline{$line} =~ s/;9($|\0)/$1/;
71 if ($single || $trace || $signal) {
73 print OUT "\032\032$filename:$line:0\n";
75 $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
76 $prefix .= "$sub($filename:";
77 if (length($prefix) > 30) {
78 print OUT "$prefix$line):\n$line:\t",$dbline[$line];
84 print OUT "$prefix$line$infix",$dbline[$line];
86 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
87 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
88 print OUT "$prefix$i$infix",$dbline[$i];
92 $evalarg = $action, &eval if $action;
93 if ($single || $signal) {
94 $evalarg = $pre, &eval if $pre;
95 print OUT $#stack . " levels deep in subroutine calls!\n"
99 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
103 $cmd eq '' && exit 0;
105 $cmd =~ s/\\$// && do {
110 $cmd =~ /^q$/ && exit 0;
111 $cmd =~ /^$/ && ($cmd = $laststep);
112 push(@hist,$cmd) if length($cmd) > 1;
113 ($i) = split(/\s+/,$cmd);
114 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
115 $cmd =~ /^h$/ && do {
119 n Next, steps over subroutine calls.
120 r Return from current subroutine.
121 c [line] Continue; optionally inserts a one-time-only breakpoint
122 at the specified line.
123 <CR> Repeat last n or s.
124 l min+incr List incr+1 lines starting at min.
125 l min-max List lines.
128 - List previous window.
129 w line List window around line.
130 l subname List subroutine.
131 f filename Switch to filename.
132 /pattern/ Search forwards for pattern; final / is optional.
133 ?pattern? Search backwards for pattern.
134 L List breakpoints and actions.
135 S List subroutine names.
138 Set breakpoint; line defaults to the current execution line;
139 condition breaks if it evaluates to true, defaults to \'1\'.
140 b subname [condition]
141 Set breakpoint at first line of subroutine.
142 d [line] Delete breakpoint.
143 D Delete all breakpoints.
145 Set an action to be done before the line is executed.
146 Sequence is: check for breakpoint, print line if necessary,
147 do action, prompt user if breakpoint or step, evaluate line.
148 A Delete all actions.
149 V [pkg [vars]] List some (default all) variables in package (default current).
150 X [vars] Same as \"V currentpackage [vars]\".
151 < command Define command before prompt.
152 > command Define command after prompt.
153 ! number Redo command (default previous command).
154 ! -number Redo number\'th to last command.
155 H -number Display last number commands (default all).
157 p expr Same as \"print DB::OUT expr\" in current package.
158 = [alias value] Define a command alias, or list current aliases.
159 command Execute as a perl statement in current package.
163 $cmd =~ /^t$/ && do {
165 print OUT "Trace = ".($trace?"on":"off")."\n";
167 $cmd =~ /^S$/ && do {
168 foreach $subname (sort(keys %sub)) {
169 print OUT $subname,"\n";
172 $cmd =~ s/^X\b/V $pkg/;
173 $cmd =~ /^V$/ && do {
175 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
176 local ($savout) = select(OUT);
178 @vars = split(' ',$2);
179 do 'dumpvar.pl' unless defined &main::dumpvar;
180 if (defined &main::dumpvar) {
181 &main::dumpvar($packname,@vars);
184 print DB::OUT "dumpvar.pl not available.\n";
188 $cmd =~ /^f\b\s*(.*)/ && do {
191 print OUT "The old f command is now the r command.\n";
192 print OUT "The new f command switches filenames.\n";
195 if (!defined $main::{'_<' . $file}) {
196 if (($try) = grep(m#^_<.*$file#, keys %main::)) {
197 $file = substr($try,2);
201 if (!defined $main::{'_<' . $file}) {
202 print OUT "There's no code here anything matching $file.\n";
205 elsif ($file ne $filename) {
206 *dbline = "::_<$file";
212 $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
214 $subname = "main::" . $subname unless $subname =~ /'|::/;
215 $subname = "main" . $subname if substr($subname,0,1)eq "'";
216 $subname = "main" . $subname if substr($subname,0,2)eq "::";
217 ($file,$subrange) = split(/:/,$sub{$subname});
218 if ($file ne $filename) {
219 *dbline = "::_<$file";
224 if (eval($subrange) < -$window) {
225 $subrange =~ s/-.*/+/;
227 $cmd = "l $subrange";
229 print OUT "Subroutine $1 not found.\n";
232 $cmd =~ /^w\b\s*(\d*)$/ && do {
236 $cmd = 'l ' . $start . '-' . ($start + $incr); };
237 $cmd =~ /^-$/ && do {
239 $cmd = 'l ' . ($start-$window*2) . '+'; };
240 $cmd =~ /^l$/ && do {
242 $cmd = 'l ' . $start . '-' . ($start + $incr); };
243 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
246 $incr = $window - 1 unless $incr;
247 $cmd = 'l ' . $start . '-' . ($start + $incr); };
248 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
249 $end = (!$2) ? $max : ($4 ? $4 : $2);
250 $end = $max if $end > $max;
252 $i = $line if $i eq '.';
255 print OUT "\032\032$filename:$i:0\n";
258 for (; $i <= $end; $i++) {
259 print OUT "$i:\t", $dbline[$i];
263 $start = $i; # remember in case they want more
264 $start = $max if $start > $max;
266 $cmd =~ /^D$/ && do {
267 print OUT "Deleting all breakpoints...\n";
268 for ($i = 1; $i <= $max ; $i++) {
269 if (defined $dbline{$i}) {
270 $dbline{$i} =~ s/^[^\0]+//;
271 if ($dbline{$i} =~ s/^\0?$//) {
277 $cmd =~ /^L$/ && do {
278 for ($i = 1; $i <= $max; $i++) {
279 if (defined $dbline{$i}) {
280 print OUT "$i:\t", $dbline[$i];
281 ($stop,$action) = split(/\0/, $dbline{$i});
282 print OUT " break if (", $stop, ")\n"
284 print OUT " action: ", $action, "\n"
290 $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
293 $subname = "${pkg}::" . $subname
294 unless $subname =~ /'|::/;
295 $subname = "main" . $subname if substr($subname,0,1) eq "'";
296 $subname = "main" . $subname if substr($subname,0,2) eq "::";
297 ($filename,$i) = split(/:/, $sub{$subname});
300 *dbline = "::_<$filename";
301 ++$i while $dbline[$i] == 0 && $i < $#dbline;
302 $dbline{$i} =~ s/^[^\0]*/$cond/;
304 print OUT "Subroutine $subname not found.\n";
307 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
310 if ($dbline[$i] == 0) {
311 print OUT "Line $i not breakable.\n";
313 $dbline{$i} =~ s/^[^\0]*/$cond/;
316 $cmd =~ /^d\b\s*(\d+)?/ && do {
318 $dbline{$i} =~ s/^[^\0]*//;
319 delete $dbline{$i} if $dbline{$i} eq '';
321 $cmd =~ /^A$/ && do {
322 for ($i = 1; $i <= $max ; $i++) {
323 if (defined $dbline{$i}) {
324 $dbline{$i} =~ s/\0[^\0]*//;
325 delete $dbline{$i} if $dbline{$i} eq '';
329 $cmd =~ /^<\s*(.*)/ && do {
332 $cmd =~ /^>\s*(.*)/ && do {
335 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
337 if ($dbline[$i] == 0) {
338 print OUT "Line $i may not have an action.\n";
340 $dbline{$i} =~ s/\0[^\0]*//;
341 $dbline{$i} .= "\0" . action($3);
344 $cmd =~ /^n$/ && do {
348 $cmd =~ /^s$/ && do {
352 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
355 if ($dbline[$i] == 0) {
356 print OUT "Line $i not breakable.\n";
359 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
361 for ($i=0; $i <= $#stack; ) {
365 $cmd =~ /^r$/ && do {
366 $stack[$#stack] |= 2;
368 $cmd =~ /^T$/ && do {
369 local($p,$f,$l,$s,$h,$a,@a,@sub);
370 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
376 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
377 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
378 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
381 $w = $w ? '@ = ' : '$ = ';
382 $a = $h ? '(' . join(', ', @a) . ')' : '';
383 push(@sub, "$w$s$a from file $f line $l\n");
386 for ($i=0; $i <= $#sub; $i++) {
391 $cmd =~ /^\/(.*)$/ && do {
393 $inpat =~ s:([^\\])/$:$1:;
395 eval '$inpat =~ m'."\a$inpat\a";
406 $start = 1 if ($start > $max);
407 last if ($start == $end);
408 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
410 print OUT "\032\032$filename:$start:0\n";
412 print OUT "$start:\t", $dbline[$start], "\n";
417 print OUT "/$pat/: not found\n" if ($start == $end);
419 $cmd =~ /^\?(.*)$/ && do {
421 $inpat =~ s:([^\\])\?$:$1:;
423 eval '$inpat =~ m'."\a$inpat\a";
434 $start = $max if ($start <= 0);
435 last if ($start == $end);
436 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
438 print OUT "\032\032$filename:$start:0\n";
440 print OUT "$start:\t", $dbline[$start], "\n";
445 print OUT "?$pat?: not found\n" if ($start == $end);
447 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
448 pop(@hist) if length($cmd) > 1;
449 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
450 $cmd = $hist[$i] . "\n";
453 $cmd =~ /^!(.+)$/ && do {
455 pop(@hist) if length($cmd) > 1;
456 for ($i = $#hist; $i; --$i) {
457 last if $hist[$i] =~ $pat;
460 print OUT "No such command!\n\n";
463 $cmd = $hist[$i] . "\n";
466 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
467 $end = $2?($#hist-$2):0;
468 $hist = 0 if $hist < 0;
469 for ($i=$#hist; $i>$end; $i--) {
470 print OUT "$i: ",$hist[$i],"\n"
471 unless $hist[$i] =~ /^.?$/;
474 $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
476 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
477 $alias{$k}="s~$k~$v~";
478 print OUT "$k = $v\n";
479 } elsif ($cmd =~ /^=\s*$/) {
480 foreach $k (sort keys(%alias)) {
481 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
482 print OUT "$k = $v\n";
484 print OUT "$k\t$alias{$k}\n";
490 $evalarg = $cmd; &eval;
494 $evalarg = $post; &eval;
497 ($@, $!, $,, $/, $\, $^W) = @saved;
502 @saved = ($@, $!, $,, $/, $\, $^W);
503 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
506 # The following takes its argument via $evalarg to preserve current @_
509 eval "$usercontext $evalarg; &DB::save";
515 while ($action =~ s/\\$//) {
532 push(@stack, $single);
534 $single |= 4 if $#stack == $deep;
537 $single |= pop(@stack);
542 $single |= pop(@stack);
547 $trace = $signal = $single = 0; # uninitialized warning suppression
550 $SIG{'INT'} = "DB::catch";
551 $deep = 100; # warning if stack gets this deep
559 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
565 elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
566 do "$ENV{'LOGDIR'}/$rcfile";
568 elsif (-f "$ENV{'HOME'}/$rcfile") {
569 do "$ENV{'HOME'}/$rcfile";