3 $header = '$Header: perldb.pl,v 3.0.1.1 89/10/26 23:14:02 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.1.1 89/10/26 23:14:02 lwall
14 # patch1: RCS expanded an unintended $Header in lib/perldb.pl
16 # Revision 3.0 89/10/18 15:19:46 lwall
19 # Revision 2.0 88/06/05 00:09:45 root
20 # Baseline version 2.0.
24 open(IN,"/dev/tty"); # so we don't dingle stdin
25 open(OUT,">/dev/tty"); # so we don't dongle stdout
29 $| = 1; # for real STDOUT
31 $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
32 print OUT "\nLoading DB from $header\n\n";
35 local($. ,$@, $!, $[, $,, $/, $\);
36 $[ = 0; $, = ""; $/ = "\n"; $\ = "";
44 $DB'signal |= eval $DB'stop[$DB'line]; print DB'OUT $@;
45 $DB'stop[$DB'line] =~ s/;9$//;
48 if ($single || $trace || $signal) {
49 print OUT "$sub($line):\t",$line[$line];
50 for ($i = $line + 1; $i <= $max && $line[$i] == 0; ++$i) {
51 last if $line[$i] =~ /^\s*(}|#|\n)/;
52 print OUT "$sub($i):\t",$line[$i];
57 eval $DB'action[$DB'line]; print DB'OUT $@;
59 if ($single || $signal) {
62 eval $DB'pre; print DB'OUT $@;
64 print OUT $#stack . " levels deep in subroutine calls!\n"
67 while ((print OUT " DB<", $#hist+1, "> "), $cmd=<IN>) {
72 $cmd =~ /^q$/ && exit 0;
73 $cmd =~ /^$/ && ($cmd = $laststep);
74 push(@hist,$cmd) if length($cmd) > 1;
75 ($i) = split(/\s+/,$cmd);
76 eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
81 n Next, steps over subroutine calls.
82 f Finish current subroutine.
83 c [line] Continue; optionally inserts a one-time-only breakpoint
84 at the specified line.
85 <CR> Repeat last n or s.
86 l min+incr List incr+1 lines starting at min.
90 - List previous window.
91 w line List window around line.
92 l subname List subroutine.
93 /pattern/ Search forwards for pattern; final / is optional.
94 ?pattern? Search backwards for pattern.
95 L List breakpoints and actions.
96 S List subroutine names.
99 Set breakpoint; line defaults to the current execution line;
100 condition breaks if it evaluates to true, defaults to \'1\'.
101 b subname [condition]
102 Set breakpoint at first line of subroutine.
103 d [line] Delete breakpoint.
104 D Delete all breakpoints.
106 Set an action to be done before the line is executed.
107 Sequence is: check for breakpoint, print line if necessary,
108 do action, prompt user if breakpoint or step, evaluate line.
109 A Delete all actions.
110 V package List all variables and values in package (default main).
111 < command Define command before prompt.
112 > command Define command after prompt.
113 ! number Redo command (default previous command).
114 ! -number Redo number\'th to last command.
115 H -number Display last number commands (default all).
117 p expr Same as \"package main; print DB'OUT expr\".
118 command Execute as a perl statement.
122 $cmd =~ /^t$/ && do {
124 print OUT "Trace = ".($trace?"on":"off")."\n";
126 $cmd =~ /^S$/ && do {
127 foreach $subname (sort(keys %sub)) {
128 if ($subname =~ /^main'(.*)/) {
132 print OUT $subname,"\n";
136 $cmd =~ /^V$/ && do {
138 $cmd =~ /^V\s*(['A-Za-z_]['\w]*)$/ && do {
140 do 'dumpvar.pl' unless defined &main'dumpvar;
141 if (defined &main'dumpvar) {
142 &main'dumpvar($packname);
145 print DB'OUT "dumpvar.pl not available.\n";
148 $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do {
150 $subname = "main'" . $subname unless $subname =~ /'/;
151 $subrange = $sub{$subname};
153 if (eval($subrange) < -$window) {
154 $subrange =~ s/-.*/+/;
156 $cmd = "l $subrange";
158 print OUT "Subroutine $1 not found.\n";
161 $cmd =~ /^w\s*(\d*)$/ && do {
165 $cmd = 'l ' . $start . '-' . ($start + $incr); };
166 $cmd =~ /^-$/ && do {
168 $cmd = 'l ' . ($start-$window*2) . '+'; };
169 $cmd =~ /^l$/ && do {
171 $cmd = 'l ' . $start . '-' . ($start + $incr); };
172 $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do {
175 $incr = $window - 1 unless $incr;
176 $cmd = 'l ' . $start . '-' . ($start + $incr); };
177 $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
178 $end = (!$2) ? $max : ($4 ? $4 : $2);
179 $end = $max if $end > $max;
181 $i = $line if $i eq '.';
183 for (; $i <= $end; $i++) {
184 print OUT "$i:\t", $line[$i];
187 $start = $i; # remember in case they want more
188 $start = $max if $start > $max;
190 $cmd =~ /^D$/ && do {
191 print OUT "Deleting all breakpoints...\n";
192 for ($i = 1; $i <= $max ; $i++) {
196 $cmd =~ /^L$/ && do {
197 for ($i = 1; $i <= $max; $i++) {
198 if ($stop[$i] || $action[$i]) {
199 print OUT "$i:\t", $line[$i];
200 print OUT " break if (", $stop[$i], ")\n"
202 print OUT " action: ", $action[$i], "\n"
208 $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
210 $subname = "main'" . $subname unless $subname =~ /'/;
211 ($i) = split(/-/, $sub{$subname});
213 ++$i while $line[$i] == 0 && $i < $#line;
214 $stop[$i] = $2 ? $2 : 1;
216 print OUT "Subroutine $1 not found.\n";
219 $cmd =~ /^b\s*(\d*)\s*(.*)/ && do {
221 if ($line[$i] == 0) {
222 print OUT "Line $i not breakable.\n";
224 $stop[$i] = $2 ? $2 : 1;
227 $cmd =~ /^d\s*(\d+)?/ && do {
231 $cmd =~ /^A$/ && do {
232 for ($i = 1; $i <= $max ; $i++) {
236 $cmd =~ /^<\s*(.*)/ && do {
237 $pre = do action($1);
239 $cmd =~ /^>\s*(.*)/ && do {
240 $post = do action($1);
242 $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do {
244 if ($line[$i] == 0) {
245 print OUT "Line $i may not have an action.\n";
247 $action[$i] = do action($3);
250 $cmd =~ /^n$/ && do {
254 $cmd =~ /^s$/ && do {
258 $cmd =~ /^c\s*(\d*)\s*$/ && do {
261 if ($line[$i] == 0) {
262 print OUT "Line $i not breakable.\n";
265 $stop[$i] .= ";9"; # add one-time-only b.p.
267 for ($i=0; $i <= $#stack; ) {
271 $cmd =~ /^f$/ && do {
272 $stack[$#stack] |= 2;
274 $cmd =~ /^T$/ && do {
275 for ($i=0; $i <= $#sub; ) {
276 print OUT $sub[$i++], "\n";
280 $cmd =~ /^\/(.*)$/ && do {
282 $inpat =~ s:([^\\])/$:$1:;
284 eval '$inpat =~ m'."\n$inpat\n";
295 $start = 1 if ($start > $max);
296 last if ($start == $end);
297 if ($line[$start] =~ m'."\n$pat\n".'i) {
298 print OUT "$start:\t", $line[$start], "\n";
302 print OUT "/$pat/: not found\n" if ($start == $end);
304 $cmd =~ /^\?(.*)$/ && do {
306 $inpat =~ s:([^\\])\?$:$1:;
308 eval '$inpat =~ m'."\n$inpat\n";
319 $start = $max if ($start <= 0);
320 last if ($start == $end);
321 if ($line[$start] =~ m'."\n$pat\n".'i) {
322 print OUT "$start:\t", $line[$start], "\n";
326 print OUT "?$pat?: not found\n" if ($start == $end);
328 $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
329 pop(@hist) if length($cmd) > 1;
330 $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
331 $cmd = $hist[$i] . "\n";
334 $cmd =~ /^!(.+)$/ && do {
336 pop(@hist) if length($cmd) > 1;
337 for ($i = $#hist; $i; --$i) {
338 last if $hist[$i] =~ $pat;
341 print OUT "No such command!\n\n";
344 $cmd = $hist[$i] . "\n";
347 $cmd =~ /^H\s*(-(\d+))?/ && do {
348 $end = $2?($#hist-$2):0;
349 $hist = 0 if $hist < 0;
350 for ($i=$#hist; $i>$end; $i--) {
351 print OUT "$i: ",$hist[$i],"\n"
352 unless $hist[$i] =~ /^.?$/;
355 $cmd =~ s/^p( .*)?$/print DB'OUT$1/;
364 eval $DB'post; print DB'OUT $@;
371 while ($action =~ s/\\$//) {
383 push(@stack, $single);
385 $single |= 4 if $#stack == $deep;
388 if (/^Stab/ && length($_) == length($_main{'_main'})) {
389 $_ = sprintf("%s",$_);
394 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
397 push(@sub, $sub . '(' . join(', ', @args) . ') from ' . $line);
406 $single |= pop(@stack);
410 $single = 1; # so it stops on first executable statement
413 $SIG{'INT'} = "DB'catch";
414 $deep = 100; # warning if stack gets this deep
422 s/(.*)/'$1'/ unless /^-?[\d.]+$/;
424 push(@sub, 'main(' . join(', ', @args) . ")" );
430 elsif (-f "$ENV{'LOGDIR'}/.perldb") {
431 do "$ENV{'LOGDIR'}/.perldb";
433 elsif (-f "$ENV{'HOME'}/.perldb") {
434 do "$ENV{'HOME'}/.perldb";