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.
18 # Is Perl being run from Emacs?
19 $emacs = ((defined $main::ARGV[0]) and ($main::ARGV[0] eq '-emacs'));
20 shift(@main::ARGV) if $emacs;
22 #require Term::ReadLine;
27 $console = "/dev/tty";
35 $console = "sys\$command";
40 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
44 $console = "/dev/con";
48 open(IN, "<$console") || open(IN, "<&STDIN"); # so we don't dingle stdin
49 open(OUT,">$console") || open(OUT, ">&STDERR")
50 || open(OUT, ">&STDOUT"); # so we don't dongle stdout
54 $| = 1; # for real STDOUT
57 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
58 print OUT "\nLoading DB routines from $header\n";
59 print OUT ("Emacs support ",
60 $emacs ? "enabled" : "available",
62 print OUT "\nEnter h for help.\n\n";
68 ($pkg, $filename, $line) = caller;
69 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
70 "package $pkg;"; # this won't let them modify, alas
71 local(*dbline) = "::_<$filename";
73 if (($stop,$action) = split(/\0/,$dbline{$line})) {
78 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
79 $dbline{$line} =~ s/;9($|\0)/$1/;
82 if ($single || $trace || $signal) {
84 print OUT "\032\032$filename:$line:0\n";
86 $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
87 $prefix .= "$sub($filename:";
88 if (length($prefix) > 30) {
89 print OUT "$prefix$line):\n$line:\t",$dbline[$line];
95 print OUT "$prefix$line$infix",$dbline[$line];
97 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
98 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
99 print OUT "$prefix$i$infix",$dbline[$i];
103 $evalarg = $action, &eval if $action;
104 if ($single || $signal) {
105 $evalarg = $pre, &eval if $pre;
106 print OUT $#stack . " levels deep in subroutine calls!\n"
110 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
114 $cmd eq '' && exit 0;
116 $cmd =~ s/\\$// && do {
121 $cmd =~ /^q$/ && exit 0;
122 $cmd =~ /^$/ && ($cmd = $laststep);
123 push(@hist,$cmd) if length($cmd) > 1;
124 ($i) = split(/\s+/,$cmd);
125 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
126 $cmd =~ /^h$/ && do {
130 n Next, steps over subroutine calls.
131 r Return from current subroutine.
132 c [line] Continue; optionally inserts a one-time-only breakpoint
133 at the specified line.
134 <CR> Repeat last n or s.
135 l min+incr List incr+1 lines starting at min.
136 l min-max List lines.
139 - List previous window.
140 w line List window around line.
141 l subname List subroutine.
142 f filename Switch to filename.
143 /pattern/ Search forwards for pattern; final / is optional.
144 ?pattern? Search backwards for pattern.
145 L List breakpoints and actions.
146 S List subroutine names.
149 Set breakpoint; line defaults to the current execution line;
150 condition breaks if it evaluates to true, defaults to \'1\'.
151 b subname [condition]
152 Set breakpoint at first line of subroutine.
153 d [line] Delete breakpoint.
154 D Delete all breakpoints.
156 Set an action to be done before the line is executed.
157 Sequence is: check for breakpoint, print line if necessary,
158 do action, prompt user if breakpoint or step, evaluate line.
159 A Delete all actions.
160 V [pkg [vars]] List some (default all) variables in package (default current).
161 X [vars] Same as \"V currentpackage [vars]\".
162 < command Define command before prompt.
163 > command Define command after prompt.
164 ! number Redo command (default previous command).
165 ! -number Redo number\'th to last command.
166 H -number Display last number commands (default all).
168 p expr Same as \"print DB::OUT expr\" in current package.
169 = [alias value] Define a command alias, or list current aliases.
170 command Execute as a perl statement in current package.
174 $cmd =~ /^t$/ && do {
176 print OUT "Trace = ".($trace?"on":"off")."\n";
178 $cmd =~ /^S$/ && do {
179 foreach $subname (sort(keys %sub)) {
180 print OUT $subname,"\n";
183 $cmd =~ s/^X\b/V $pkg/;
184 $cmd =~ /^V$/ && do {
186 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
187 local ($savout) = select(OUT);
189 @vars = split(' ',$2);
190 do 'dumpvar.pl' unless defined &main::dumpvar;
191 if (defined &main::dumpvar) {
192 &main::dumpvar($packname,@vars);
195 print DB::OUT "dumpvar.pl not available.\n";
199 $cmd =~ /^f\b\s*(.*)/ && do {
202 print OUT "The old f command is now the r command.\n";
203 print OUT "The new f command switches filenames.\n";
206 if (!defined $main::{'_<' . $file}) {
207 if (($try) = grep(m#^_<.*$file#, keys %main::)) {
208 $file = substr($try,2);
212 if (!defined $main::{'_<' . $file}) {
213 print OUT "There's no code here anything matching $file.\n";
216 elsif ($file ne $filename) {
217 *dbline = "::_<$file";
223 $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
225 $subname = "main::" . $subname unless $subname =~ /'|::/;
226 $subname = "main" . $subname if substr($subname,0,1)eq "'";
227 $subname = "main" . $subname if substr($subname,0,2)eq "::";
228 # VMS filespecs may (usually do) contain ':', so don't use split
229 ($file,$subrange) = $sub{$subname} =~ /(.*):(.*)/;
230 if ($file ne $filename) {
231 *dbline = "::_<$file";
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 = "${pkg}::" . $subname
306 unless $subname =~ /'|::/;
307 $subname = "main" . $subname if substr($subname,0,1) eq "'";
308 $subname = "main" . $subname if substr($subname,0,2) eq "::";
309 # VMS filespecs may (usually do) contain ':', so don't use split
310 ($filename,$i) = $sub{$subname} =~ /(.*):(.*)/;
313 *dbline = "::_<$filename";
314 ++$i while $dbline[$i] == 0 && $i < $#dbline;
315 $dbline{$i} =~ s/^[^\0]*/$cond/;
317 print OUT "Subroutine $subname not found.\n";
320 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
323 if ($dbline[$i] == 0) {
324 print OUT "Line $i not breakable.\n";
326 $dbline{$i} =~ s/^[^\0]*/$cond/;
329 $cmd =~ /^d\b\s*(\d+)?/ && do {
331 $dbline{$i} =~ s/^[^\0]*//;
332 delete $dbline{$i} if $dbline{$i} eq '';
334 $cmd =~ /^A$/ && do {
335 for ($i = 1; $i <= $max ; $i++) {
336 if (defined $dbline{$i}) {
337 $dbline{$i} =~ s/\0[^\0]*//;
338 delete $dbline{$i} if $dbline{$i} eq '';
342 $cmd =~ /^<\s*(.*)/ && do {
345 $cmd =~ /^>\s*(.*)/ && do {
348 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
350 if ($dbline[$i] == 0) {
351 print OUT "Line $i may not have an action.\n";
353 $dbline{$i} =~ s/\0[^\0]*//;
354 $dbline{$i} .= "\0" . action($3);
357 $cmd =~ /^n$/ && do {
361 $cmd =~ /^s$/ && do {
365 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
368 if ($dbline[$i] == 0) {
369 print OUT "Line $i not breakable.\n";
372 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
374 for ($i=0; $i <= $#stack; ) {
378 $cmd =~ /^r$/ && do {
379 $stack[$#stack] |= 2;
381 $cmd =~ /^T$/ && do {
382 local($p,$f,$l,$s,$h,$a,@a,@sub);
383 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
389 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
390 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
391 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
394 $w = $w ? '@ = ' : '$ = ';
395 $a = $h ? '(' . join(', ', @a) . ')' : '';
396 push(@sub, "$w$s$a from file $f line $l\n");
399 for ($i=0; $i <= $#sub; $i++) {
404 $cmd =~ /^\/(.*)$/ && do {
406 $inpat =~ s:([^\\])/$:$1:;
408 eval '$inpat =~ m'."\a$inpat\a";
419 $start = 1 if ($start > $max);
420 last if ($start == $end);
421 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
423 print OUT "\032\032$filename:$start:0\n";
425 print OUT "$start:\t", $dbline[$start], "\n";
430 print OUT "/$pat/: not found\n" if ($start == $end);
432 $cmd =~ /^\?(.*)$/ && do {
434 $inpat =~ s:([^\\])\?$:$1:;
436 eval '$inpat =~ m'."\a$inpat\a";
447 $start = $max if ($start <= 0);
448 last if ($start == $end);
449 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
451 print OUT "\032\032$filename:$start:0\n";
453 print OUT "$start:\t", $dbline[$start], "\n";
458 print OUT "?$pat?: not found\n" if ($start == $end);
460 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
461 pop(@hist) if length($cmd) > 1;
462 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
463 $cmd = $hist[$i] . "\n";
466 $cmd =~ /^!(.+)$/ && do {
468 pop(@hist) if length($cmd) > 1;
469 for ($i = $#hist; $i; --$i) {
470 last if $hist[$i] =~ $pat;
473 print OUT "No such command!\n\n";
476 $cmd = $hist[$i] . "\n";
479 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
480 $end = $2?($#hist-$2):0;
481 $hist = 0 if $hist < 0;
482 for ($i=$#hist; $i>$end; $i--) {
483 print OUT "$i: ",$hist[$i],"\n"
484 unless $hist[$i] =~ /^.?$/;
487 $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
489 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
490 $alias{$k}="s~$k~$v~";
491 print OUT "$k = $v\n";
492 } elsif ($cmd =~ /^=\s*$/) {
493 foreach $k (sort keys(%alias)) {
494 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
495 print OUT "$k = $v\n";
497 print OUT "$k\t$alias{$k}\n";
503 $evalarg = $cmd; &eval;
507 $evalarg = $post; &eval;
510 ($@, $!, $,, $/, $\, $^W) = @saved;
515 @saved = ($@, $!, $,, $/, $\, $^W);
516 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
519 # The following takes its argument via $evalarg to preserve current @_
522 eval "$usercontext $evalarg; &DB::save";
528 while ($action =~ s/\\$//) {
545 push(@stack, $single);
547 $single |= 4 if $#stack == $deep;
550 $single |= pop(@stack);
555 $single |= pop(@stack);
560 $trace = $signal = $single = 0; # uninitialized warning suppression
563 $SIG{'INT'} = "DB::catch";
564 $deep = 100; # warning if stack gets this deep
572 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
578 elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
579 do "$ENV{'LOGDIR'}/$rcfile";
581 elsif (-f "$ENV{'HOME'}/$rcfile") {
582 do "$ENV{'HOME'}/$rcfile";