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";
55 ($package, $filename, $line) = caller;
56 $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
57 "package $package;"; # this won't let them modify, alas
58 local(*dbline) = "::_<$filename";
60 if (($stop,$action) = split(/\0/,$dbline{$line})) {
65 $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
66 $dbline{$line} =~ s/;9($|\0)/$1/;
69 if ($single || $trace || $signal) {
71 print OUT "\032\032$filename:$line:0\n";
73 $prefix = $sub =~ /'|::/ ? "" : "${package}::";
74 $prefix .= "$sub($filename:";
75 if (length($prefix) > 30) {
76 print OUT "$prefix$line):\n$line:\t",$dbline[$line];
82 print OUT "$prefix$line$infix",$dbline[$line];
84 for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
85 last if $dbline[$i] =~ /^\s*(}|#|\n)/;
86 print OUT "$prefix$i$infix",$dbline[$i];
90 $evalarg = $action, &eval if $action;
91 if ($single || $signal) {
92 $evalarg = $pre, &eval if $pre;
93 print OUT $#stack . " levels deep in subroutine calls!\n"
97 while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) {
101 $cmd eq '' && exit 0;
103 $cmd =~ s/\\$// && do {
108 $cmd =~ /^q$/ && exit 0;
109 $cmd =~ /^$/ && ($cmd = $laststep);
110 push(@hist,$cmd) if length($cmd) > 1;
111 ($i) = split(/\s+/,$cmd);
112 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
113 $cmd =~ /^h$/ && do {
117 n Next, steps over subroutine calls.
118 r Return from current subroutine.
119 c [line] Continue; optionally inserts a one-time-only breakpoint
120 at the specified line.
121 <CR> Repeat last n or s.
122 l min+incr List incr+1 lines starting at min.
123 l min-max List lines.
126 - List previous window.
127 w line List window around line.
128 l subname List subroutine.
129 f filename Switch to filename.
130 /pattern/ Search forwards for pattern; final / is optional.
131 ?pattern? Search backwards for pattern.
132 L List breakpoints and actions.
133 S List subroutine names.
136 Set breakpoint; line defaults to the current execution line;
137 condition breaks if it evaluates to true, defaults to \'1\'.
138 b subname [condition]
139 Set breakpoint at first line of subroutine.
140 d [line] Delete breakpoint.
141 D Delete all breakpoints.
143 Set an action to be done before the line is executed.
144 Sequence is: check for breakpoint, print line if necessary,
145 do action, prompt user if breakpoint or step, evaluate line.
146 A Delete all actions.
147 V [pkg [vars]] List some (default all) variables in package (default current).
148 X [vars] Same as \"V currentpackage [vars]\".
149 < command Define command before prompt.
150 > command Define command after prompt.
151 ! number Redo command (default previous command).
152 ! -number Redo number\'th to last command.
153 H -number Display last number commands (default all).
155 p expr Same as \"print DB::OUT expr\" in current package.
156 = [alias value] Define a command alias, or list current aliases.
157 command Execute as a perl statement in current package.
161 $cmd =~ /^t$/ && do {
163 print OUT "Trace = ".($trace?"on":"off")."\n";
165 $cmd =~ /^S$/ && do {
166 foreach $subname (sort(keys %sub)) {
167 print OUT $subname,"\n";
170 $cmd =~ s/^X\b/V $package/;
171 $cmd =~ /^V$/ && do {
172 $cmd = "V $package"; };
173 $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
174 local ($savout) = select(OUT);
176 @vars = split(' ',$2);
177 do 'dumpvar.pl' unless defined &main::dumpvar;
178 if (defined &main::dumpvar) {
179 &main::dumpvar($packname,@vars);
182 print DB::OUT "dumpvar.pl not available.\n";
186 $cmd =~ /^f\b\s*(.*)/ && do {
189 print OUT "The old f command is now the r command.\n";
190 print OUT "The new f command switches filenames.\n";
193 if (!defined $main::{'_<' . $file}) {
194 if (($try) = grep(m#^_<.*$file#, keys %main::)) {
195 $file = substr($try,2);
199 if (!defined $main::{'_<' . $file}) {
200 print OUT "There's no code here anything matching $file.\n";
203 elsif ($file ne $filename) {
204 *dbline = "::_<$file";
210 $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
212 $subname = "main::" . $subname unless $subname =~ /'|::/;
213 $subname = "main" . $subname if substr($subname,0,1)eq "'";
214 $subname = "main" . $subname if substr($subname,0,2)eq "::";
215 ($file,$subrange) = split(/:/,$sub{$subname});
216 if ($file ne $filename) {
217 *dbline = "::_<$file";
222 if (eval($subrange) < -$window) {
223 $subrange =~ s/-.*/+/;
225 $cmd = "l $subrange";
227 print OUT "Subroutine $1 not found.\n";
230 $cmd =~ /^w\b\s*(\d*)$/ && do {
234 $cmd = 'l ' . $start . '-' . ($start + $incr); };
235 $cmd =~ /^-$/ && do {
237 $cmd = 'l ' . ($start-$window*2) . '+'; };
238 $cmd =~ /^l$/ && do {
240 $cmd = 'l ' . $start . '-' . ($start + $incr); };
241 $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
244 $incr = $window - 1 unless $incr;
245 $cmd = 'l ' . $start . '-' . ($start + $incr); };
246 $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
247 $end = (!$2) ? $max : ($4 ? $4 : $2);
248 $end = $max if $end > $max;
250 $i = $line if $i eq '.';
253 print OUT "\032\032$filename:$i:0\n";
256 for (; $i <= $end; $i++) {
257 print OUT "$i:\t", $dbline[$i];
261 $start = $i; # remember in case they want more
262 $start = $max if $start > $max;
264 $cmd =~ /^D$/ && do {
265 print OUT "Deleting all breakpoints...\n";
266 for ($i = 1; $i <= $max ; $i++) {
267 if (defined $dbline{$i}) {
268 $dbline{$i} =~ s/^[^\0]+//;
269 if ($dbline{$i} =~ s/^\0?$//) {
275 $cmd =~ /^L$/ && do {
276 for ($i = 1; $i <= $max; $i++) {
277 if (defined $dbline{$i}) {
278 print OUT "$i:\t", $dbline[$i];
279 ($stop,$action) = split(/\0/, $dbline{$i});
280 print OUT " break if (", $stop, ")\n"
282 print OUT " action: ", $action, "\n"
288 $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
291 $subname = "${package}::" . $subname
292 unless $subname =~ /'|::/;
293 $subname = "main" . $subname if substr($subname,0,1) eq "'";
294 $subname = "main" . $subname if substr($subname,0,2) eq "::";
295 ($filename,$i) = split(/:/, $sub{$subname});
298 *dbline = "::_<$filename";
299 ++$i while $dbline[$i] == 0 && $i < $#dbline;
300 $dbline{$i} =~ s/^[^\0]*/$cond/;
302 print OUT "Subroutine $subname not found.\n";
305 $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
308 if ($dbline[$i] == 0) {
309 print OUT "Line $i not breakable.\n";
311 $dbline{$i} =~ s/^[^\0]*/$cond/;
314 $cmd =~ /^d\b\s*(\d+)?/ && do {
316 $dbline{$i} =~ s/^[^\0]*//;
317 delete $dbline{$i} if $dbline{$i} eq '';
319 $cmd =~ /^A$/ && do {
320 for ($i = 1; $i <= $max ; $i++) {
321 if (defined $dbline{$i}) {
322 $dbline{$i} =~ s/\0[^\0]*//;
323 delete $dbline{$i} if $dbline{$i} eq '';
327 $cmd =~ /^<\s*(.*)/ && do {
330 $cmd =~ /^>\s*(.*)/ && do {
333 $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
335 if ($dbline[$i] == 0) {
336 print OUT "Line $i may not have an action.\n";
338 $dbline{$i} =~ s/\0[^\0]*//;
339 $dbline{$i} .= "\0" . action($3);
342 $cmd =~ /^n$/ && do {
346 $cmd =~ /^s$/ && do {
350 $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
353 if ($dbline[$i] == 0) {
354 print OUT "Line $i not breakable.\n";
357 $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
359 for ($i=0; $i <= $#stack; ) {
363 $cmd =~ /^r$/ && do {
364 $stack[$#stack] |= 2;
366 $cmd =~ /^T$/ && do {
367 local($p,$f,$l,$s,$h,$a,@a,@sub);
368 for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
374 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
375 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
376 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
379 $w = $w ? '@ = ' : '$ = ';
380 $a = $h ? '(' . join(', ', @a) . ')' : '';
381 push(@sub, "$w$s$a from file $f line $l\n");
384 for ($i=0; $i <= $#sub; $i++) {
389 $cmd =~ /^\/(.*)$/ && do {
391 $inpat =~ s:([^\\])/$:$1:;
393 eval '$inpat =~ m'."\a$inpat\a";
404 $start = 1 if ($start > $max);
405 last if ($start == $end);
406 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
408 print OUT "\032\032$filename:$start:0\n";
410 print OUT "$start:\t", $dbline[$start], "\n";
415 print OUT "/$pat/: not found\n" if ($start == $end);
417 $cmd =~ /^\?(.*)$/ && do {
419 $inpat =~ s:([^\\])\?$:$1:;
421 eval '$inpat =~ m'."\a$inpat\a";
432 $start = $max if ($start <= 0);
433 last if ($start == $end);
434 if ($dbline[$start] =~ m'."\a$pat\a".'i) {
436 print OUT "\032\032$filename:$start:0\n";
438 print OUT "$start:\t", $dbline[$start], "\n";
443 print OUT "?$pat?: not found\n" if ($start == $end);
445 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
446 pop(@hist) if length($cmd) > 1;
447 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
448 $cmd = $hist[$i] . "\n";
451 $cmd =~ /^!(.+)$/ && do {
453 pop(@hist) if length($cmd) > 1;
454 for ($i = $#hist; $i; --$i) {
455 last if $hist[$i] =~ $pat;
458 print OUT "No such command!\n\n";
461 $cmd = $hist[$i] . "\n";
464 $cmd =~ /^H\b\s*(-(\d+))?/ && do {
465 $end = $2?($#hist-$2):0;
466 $hist = 0 if $hist < 0;
467 for ($i=$#hist; $i>$end; $i--) {
468 print OUT "$i: ",$hist[$i],"\n"
469 unless $hist[$i] =~ /^.?$/;
472 $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
474 if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
475 $alias{$k}="s~$k~$v~";
476 print OUT "$k = $v\n";
477 } elsif ($cmd =~ /^=\s*$/) {
478 foreach $k (sort keys(%alias)) {
479 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
480 print OUT "$k = $v\n";
482 print OUT "$k\t$alias{$k}\n";
488 $evalarg = $cmd; &eval;
492 $evalarg = $post; &eval;
495 ($@, $!, $,, $/, $\) = @saved;
499 @saved = ($@, $!, $,, $/, $\, $^W);
500 $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
503 # The following takes its argument via $evalarg to preserve current @_
506 eval "$usercontext $evalarg; &DB::save";
512 while ($action =~ s/\\$//) {
529 push(@stack, $single);
531 $single |= 4 if $#stack == $deep;
534 $single |= pop(@stack);
539 $single |= pop(@stack);
544 $trace = $signal = $single = 0; # uninitialized warning suppression
547 $SIG{'INT'} = "DB::catch";
548 $deep = 100; # warning if stack gets this deep
556 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
562 elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
563 do "$ENV{'LOGDIR'}/$rcfile";
565 elsif (-f "$ENV{'HOME'}/$rcfile") {
566 do "$ENV{'HOME'}/$rcfile";