3 $header = '$Header: perldb.pl,v 3.0 89/10/18 15:19:46 lwall Locked $';
5 # This file is automatically included if you do perl -d.
6 # It's probably not useful to include this yourself.
8 # Perl supplies the values for @line and %sub. It effectively inserts
9 # a do DB'DB(<linenum>); in front of every place that can
10 # have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
13 # Revision 3.0 89/10/18 15:19:46 lwall
16 # Revision 2.0 88/06/05 00:09:45 root
17 # Baseline version 2.0.
21 open(IN,"/dev/tty"); # so we don't dingle stdin
22 open(OUT,">/dev/tty"); # so we don't dongle stdout
26 $| = 1; # for real STDOUT
28 $header =~ s/\$Header: perldb.pl,v 3.0 89/10/18 15:19:46 lwall Locked $/$1$2/;
29 print OUT "\nLoading DB from $header\n\n";
32 local($. ,$@, $!, $[, $,, $/, $\);
33 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
41 $DB'signal |= eval $DB'stop[$DB'line]; print DB'OUT $@;
42 $DB'stop[$DB'line] =~ s/;9$//;
45 if ($single || $trace || $signal) {
46 print OUT "$sub($line):\t",$line[$line];
47 for ($i = $line + 1; $i <= $max && $line[$i] == 0; ++$i) {
48 last if $line[$i] =~ /^\s*(}|#|\n)/;
49 print OUT "$sub($i):\t",$line[$i];
54 eval $DB'action[$DB'line]; print DB'OUT $@;
56 if ($single || $signal) {
59 eval $DB'pre; print DB'OUT $@;
61 print OUT $#stack . " levels deep in subroutine calls!\n"
64 while ((print OUT " DB<", $#hist+1, "> "), $cmd=<IN>) {
69 $cmd =~ /^q$/ && exit 0;
70 $cmd =~ /^$/ && ($cmd = $laststep);
71 push(@hist,$cmd) if length($cmd) > 1;
72 ($i) = split(/\s+/,$cmd);
73 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
78 n Next, steps over subroutine calls.
79 f Finish current subroutine.
80 c [line] Continue; optionally inserts a one-time-only breakpoint
81 at the specified line.
82 <CR> Repeat last n or s.
83 l min+incr List incr+1 lines starting at min.
87 - List previous window.
88 w line List window around line.
89 l subname List subroutine.
90 /pattern/ Search forwards for pattern; final / is optional.
91 ?pattern? Search backwards for pattern.
92 L List breakpoints and actions.
93 S List subroutine names.
96 Set breakpoint; line defaults to the current execution line;
97 condition breaks if it evaluates to true, defaults to \'1\'.
99 Set breakpoint at first line of subroutine.
100 d [line] Delete breakpoint.
101 D Delete all breakpoints.
103 Set an action to be done before the line is executed.
104 Sequence is: check for breakpoint, print line if necessary,
105 do action, prompt user if breakpoint or step, evaluate line.
106 A Delete all actions.
107 V package List all variables and values in package (default main).
108 < command Define command before prompt.
109 > command Define command after prompt.
110 ! number Redo command (default previous command).
111 ! -number Redo number\'th to last command.
112 H -number Display last number commands (default all).
114 p expr Same as \"package main; print DB'OUT expr\".
115 command Execute as a perl statement.
119 $cmd =~ /^t$/ && do {
121 print OUT "Trace = ".($trace?"on":"off")."\n";
123 $cmd =~ /^S$/ && do {
124 foreach $subname (sort(keys %sub)) {
125 if ($subname =~ /^main'(.*)/) {
129 print OUT $subname,"\n";
133 $cmd =~ /^V$/ && do {
135 $cmd =~ /^V\s*(['A-Za-z_]['\w]*)$/ && do {
137 do 'dumpvar.pl' unless defined &main'dumpvar;
138 if (defined &main'dumpvar) {
139 &main'dumpvar($packname);
142 print DB'OUT "dumpvar.pl not available.\n";
145 $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
147 $subname = "main'" . $subname unless $subname =~ /'/;
148 $subrange = $sub{$subname};
150 if (eval($subrange) < -$window) {
151 $subrange =~ s/-.*/+/;
153 $cmd = "l $subrange";
155 print OUT "Subroutine $1 not found.\n";
158 $cmd =~ /^w\s*(\d*)$/ && do {
162 $cmd = 'l ' . $start . '-' . ($start + $incr); };
163 $cmd =~ /^-$/ && do {
165 $cmd = 'l ' . ($start-$window*2) . '+'; };
166 $cmd =~ /^l$/ && do {
168 $cmd = 'l ' . $start . '-' . ($start + $incr); };
169 $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
172 $incr = $window - 1 unless $incr;
173 $cmd = 'l ' . $start . '-' . ($start + $incr); };
174 $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
175 $end = (!$2) ? $max : ($4 ? $4 : $2);
176 $end = $max if $end > $max;
178 $i = $line if $i eq '.';
180 for (; $i <= $end; $i++) {
181 print OUT "$i:\t", $line[$i];
184 $start = $i; # remember in case they want more
185 $start = $max if $start > $max;
187 $cmd =~ /^D$/ && do {
188 print OUT "Deleting all breakpoints...\n";
189 for ($i = 1; $i <= $max ; $i++) {
193 $cmd =~ /^L$/ && do {
194 for ($i = 1; $i <= $max; $i++) {
195 if ($stop[$i] || $action[$i]) {
196 print OUT "$i:\t", $line[$i];
197 print OUT " break if (", $stop[$i], ")\n"
199 print OUT " action: ", $action[$i], "\n"
205 $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
207 $subname = "main'" . $subname unless $subname =~ /'/;
208 ($i) = split(/-/, $sub{$subname});
210 ++$i while $line[$i] == 0 && $i < $#line;
211 $stop[$i] = $2 ? $2 : 1;
213 print OUT "Subroutine $1 not found.\n";
216 $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
218 if ($line[$i] == 0) {
219 print OUT "Line $i not breakable.\n";
221 $stop[$i] = $2 ? $2 : 1;
224 $cmd =~ /^d\s*(\d+)?/ && do {
228 $cmd =~ /^A$/ && do {
229 for ($i = 1; $i <= $max ; $i++) {
233 $cmd =~ /^<\s*(.*)/ && do {
234 $pre = do action($1);
236 $cmd =~ /^>\s*(.*)/ && do {
237 $post = do action($1);
239 $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
241 if ($line[$i] == 0) {
242 print OUT "Line $i may not have an action.\n";
244 $action[$i] = do action($3);
247 $cmd =~ /^n$/ && do {
251 $cmd =~ /^s$/ && do {
255 $cmd =~ /^c\s*(\d*)\s*$/ && do {
258 if ($line[$i] == 0) {
259 print OUT "Line $i not breakable.\n";
262 $stop[$i] .= ";9"; # add one-time-only b.p.
264 for ($i=0; $i <= $#stack; ) {
268 $cmd =~ /^f$/ && do {
269 $stack[$#stack] |= 2;
271 $cmd =~ /^T$/ && do {
272 for ($i=0; $i <= $#sub; ) {
273 print OUT $sub[$i++], "\n";
277 $cmd =~ /^\/(.*)$/ && do {
279 $inpat =~ s:([^\\])/$:$1:;
281 eval '$inpat =~ m'."\n$inpat\n";
292 $start = 1 if ($start > $max);
293 last if ($start == $end);
294 if ($line[$start] =~ m'."\n$pat\n".'i) {
295 print OUT "$start:\t", $line[$start], "\n";
299 print OUT "/$pat/: not found\n" if ($start == $end);
301 $cmd =~ /^\?(.*)$/ && do {
303 $inpat =~ s:([^\\])\?$:$1:;
305 eval '$inpat =~ m'."\n$inpat\n";
316 $start = $max if ($start <= 0);
317 last if ($start == $end);
318 if ($line[$start] =~ m'."\n$pat\n".'i) {
319 print OUT "$start:\t", $line[$start], "\n";
323 print OUT "?$pat?: not found\n" if ($start == $end);
325 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
326 pop(@hist) if length($cmd) > 1;
327 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
328 $cmd = $hist[$i] . "\n";
331 $cmd =~ /^!(.+)$/ && do {
333 pop(@hist) if length($cmd) > 1;
334 for ($i = $#hist; $i; --$i) {
335 last if $hist[$i] =~ $pat;
338 print OUT "No such command!\n\n";
341 $cmd = $hist[$i] . "\n";
344 $cmd =~ /^H\s*(-(\d+))?/ && do {
345 $end = $2?($#hist-$2):0;
346 $hist = 0 if $hist < 0;
347 for ($i=$#hist; $i>$end; $i--) {
348 print OUT "$i: ",$hist[$i],"\n"
349 unless $hist[$i] =~ /^.?$/;
352 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
361 eval $DB'post; print DB'OUT $@;
368 while ($action =~ s/\\$//) {
380 push(@stack, $single);
382 $single |= 4 if $#stack == $deep;
385 if (/^Stab/ && length($_) == length($_main{'_main'})) {
386 $_ = sprintf("%s",$_);
391 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
394 push(@sub, $sub . '(' . join(', ', @args) . ') from ' . $line);
403 $single |= pop(@stack);
407 $single = 1; # so it stops on first executable statement
410 $SIG{'INT'} = "DB'catch";
411 $deep = 100; # warning if stack gets this deep
419 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
421 push(@sub, 'main(' . join(', ', @args) . ")" );
427 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
428 do "$ENV{'LOGDIR'}/.perldb";
430 elsif (-f "$ENV{'HOME'}/.perldb") {
431 do "$ENV{'HOME'}/.perldb";