Commit | Line | Data |
a687059c |
1 | package DB; |
2 | |
76854fea |
3 | $header = '$Header: perldb.pl,v 3.0.1.4 90/10/15 17:40:38 lwall Locked $'; |
a687059c |
4 | # |
5 | # This file is automatically included if you do perl -d. |
6 | # It's probably not useful to include this yourself. |
7 | # |
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. |
11 | # |
12 | # $Log: perldb.pl,v $ |
76854fea |
13 | # Revision 3.0.1.4 90/10/15 17:40:38 lwall |
14 | # patch29: added caller |
15 | # patch29: the debugger now understands packages and evals |
16 | # patch29: scripts now run at almost full speed under the debugger |
17 | # patch29: more variables are settable from debugger |
18 | # |
33b78306 |
19 | # Revision 3.0.1.3 90/08/09 04:00:58 lwall |
20 | # patch19: debugger now allows continuation lines |
21 | # patch19: debugger can now dump lists of variables |
22 | # patch19: debugger can now add aliases easily from prompt |
23 | # |
79a0689e |
24 | # Revision 3.0.1.2 90/03/12 16:39:39 lwall |
25 | # patch13: perl -d didn't format stack traces of *foo right |
26 | # patch13: perl -d wiped out scalar return values of subroutines |
27 | # |
03a14243 |
28 | # Revision 3.0.1.1 89/10/26 23:14:02 lwall |
29 | # patch1: RCS expanded an unintended $Header in lib/perldb.pl |
30 | # |
a687059c |
31 | # Revision 3.0 89/10/18 15:19:46 lwall |
32 | # 3.0 baseline |
33 | # |
34 | # Revision 2.0 88/06/05 00:09:45 root |
35 | # Baseline version 2.0. |
36 | # |
37 | # |
38 | |
76854fea |
39 | open(IN, "</dev/tty") || open(IN, "<&STDIN"); # so we don't dingle stdin |
40 | open(OUT,">/dev/tty") || open(OUT, ">&STDOUT"); # so we don't dongle stdout |
a687059c |
41 | select(OUT); |
42 | $| = 1; # for DB'OUT |
43 | select(STDOUT); |
44 | $| = 1; # for real STDOUT |
76854fea |
45 | $sub = ''; |
a687059c |
46 | |
03a14243 |
47 | $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/; |
76854fea |
48 | print OUT "\nLoading DB routines from $header\n\nEnter h for help.\n\n"; |
a687059c |
49 | |
50 | sub DB { |
76854fea |
51 | &save; |
52 | ($package, $filename, $line) = caller; |
53 | $usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' . |
54 | "package $package;"; # this won't let them modify, alas |
55 | local(*dbline) = "_<$filename"; |
56 | $max = $#dbline; |
57 | if (($stop,$action) = split(/\0/,$dbline{$line})) { |
a687059c |
58 | if ($stop eq '1') { |
59 | $signal |= 1; |
60 | } |
61 | else { |
76854fea |
62 | $signal |= &eval($stop); |
63 | $dbline{$line} =~ s/;9($|\0)/$1/; |
a687059c |
64 | } |
65 | } |
66 | if ($single || $trace || $signal) { |
76854fea |
67 | print OUT "$package'" unless $sub =~ /'/; |
68 | print OUT "$sub($filename:$line):\t",$dbline[$line]; |
69 | for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) { |
70 | last if $dbline[$i] =~ /^\s*(}|#|\n)/; |
71 | print OUT "$sub($filename:$i):\t",$dbline[$i]; |
a687059c |
72 | } |
73 | } |
76854fea |
74 | &eval($action) if $action; |
a687059c |
75 | if ($single || $signal) { |
76854fea |
76 | &eval($pre) if $pre; |
a687059c |
77 | print OUT $#stack . " levels deep in subroutine calls!\n" |
78 | if $single & 4; |
79 | $start = $line; |
76854fea |
80 | while ((print OUT " DB<", $#hist+1, "> "), $cmd=&gets) { |
a687059c |
81 | $single = 0; |
82 | $signal = 0; |
83 | $cmd eq '' && exit 0; |
84 | chop($cmd); |
33b78306 |
85 | $cmd =~ s/\\$// && do { |
86 | print OUT " cont: "; |
76854fea |
87 | $cmd .= &gets; |
33b78306 |
88 | redo; |
89 | }; |
a687059c |
90 | $cmd =~ /^q$/ && exit 0; |
91 | $cmd =~ /^$/ && ($cmd = $laststep); |
92 | push(@hist,$cmd) if length($cmd) > 1; |
93 | ($i) = split(/\s+/,$cmd); |
94 | eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i}; |
95 | $cmd =~ /^h$/ && do { |
96 | print OUT " |
97 | T Stack trace. |
98 | s Single step. |
99 | n Next, steps over subroutine calls. |
76854fea |
100 | r Return from current subroutine. |
a687059c |
101 | c [line] Continue; optionally inserts a one-time-only breakpoint |
102 | at the specified line. |
103 | <CR> Repeat last n or s. |
104 | l min+incr List incr+1 lines starting at min. |
105 | l min-max List lines. |
106 | l line List line; |
107 | l List next window. |
108 | - List previous window. |
109 | w line List window around line. |
110 | l subname List subroutine. |
76854fea |
111 | f filename Switch to filename. |
a687059c |
112 | /pattern/ Search forwards for pattern; final / is optional. |
113 | ?pattern? Search backwards for pattern. |
114 | L List breakpoints and actions. |
115 | S List subroutine names. |
116 | t Toggle trace mode. |
117 | b [line] [condition] |
118 | Set breakpoint; line defaults to the current execution line; |
119 | condition breaks if it evaluates to true, defaults to \'1\'. |
120 | b subname [condition] |
121 | Set breakpoint at first line of subroutine. |
122 | d [line] Delete breakpoint. |
123 | D Delete all breakpoints. |
124 | a [line] command |
125 | Set an action to be done before the line is executed. |
126 | Sequence is: check for breakpoint, print line if necessary, |
127 | do action, prompt user if breakpoint or step, evaluate line. |
128 | A Delete all actions. |
76854fea |
129 | V [pkg [vars]] List some (default all) variables in package (default current). |
130 | X [vars] Same as \"V currentpackage [vars]\". |
a687059c |
131 | < command Define command before prompt. |
132 | > command Define command after prompt. |
133 | ! number Redo command (default previous command). |
134 | ! -number Redo number\'th to last command. |
135 | H -number Display last number commands (default all). |
136 | q or ^D Quit. |
76854fea |
137 | p expr Same as \"print DB'OUT expr\" in current package. |
33b78306 |
138 | = [alias value] Define a command alias, or list current aliases. |
76854fea |
139 | command Execute as a perl statement in current package. |
a687059c |
140 | |
141 | "; |
142 | next; }; |
143 | $cmd =~ /^t$/ && do { |
144 | $trace = !$trace; |
145 | print OUT "Trace = ".($trace?"on":"off")."\n"; |
146 | next; }; |
147 | $cmd =~ /^S$/ && do { |
148 | foreach $subname (sort(keys %sub)) { |
76854fea |
149 | print OUT $subname,"\n"; |
a687059c |
150 | } |
151 | next; }; |
76854fea |
152 | $cmd =~ s/^X\b/V $package/; |
a687059c |
153 | $cmd =~ /^V$/ && do { |
76854fea |
154 | $cmd = 'V $package'; }; |
155 | $cmd =~ /^V\s*(\S+)\s*(.*)/ && do { |
a687059c |
156 | $packname = $1; |
33b78306 |
157 | @vars = split(' ',$2); |
a687059c |
158 | do 'dumpvar.pl' unless defined &main'dumpvar; |
159 | if (defined &main'dumpvar) { |
33b78306 |
160 | &main'dumpvar($packname,@vars); |
a687059c |
161 | } |
162 | else { |
163 | print DB'OUT "dumpvar.pl not available.\n"; |
164 | } |
165 | next; }; |
76854fea |
166 | $cmd =~ /^f\s*(.*)/ && do { |
167 | $file = $1; |
168 | if (!$file) { |
169 | print OUT "The old f command is now the r command.\n"; |
170 | print OUT "The new f command switches filenames.\n"; |
171 | next; |
172 | } |
173 | if (!defined $_main{'_<' . $file}) { |
174 | if (($try) = grep(m#^_<.*$file#, keys %_main)) { |
175 | $file = substr($try,2); |
176 | print "\n$file:\n"; |
177 | } |
178 | } |
179 | if (!defined $_main{'_<' . $file}) { |
180 | print OUT "There's no code here anything matching $file.\n"; |
181 | next; |
182 | } |
183 | elsif ($file ne $filename) { |
184 | *dbline = "_<$file"; |
185 | $max = $#dbline; |
186 | $filename = $file; |
187 | $start = 1; |
188 | $cmd = "l"; |
189 | } }; |
a687059c |
190 | $cmd =~ /^l\s*(['A-Za-z_]['\w]*)/ && do { |
191 | $subname = $1; |
192 | $subname = "main'" . $subname unless $subname =~ /'/; |
76854fea |
193 | $subname = "main" . $subname if substr($subname,0,1) eq "'"; |
194 | ($file,$subrange) = split(/:/,$sub{$subname}); |
195 | if ($file ne $filename) { |
196 | *dbline = "_<$file"; |
197 | $max = $#dbline; |
198 | $filename = $file; |
199 | } |
a687059c |
200 | if ($subrange) { |
201 | if (eval($subrange) < -$window) { |
202 | $subrange =~ s/-.*/+/; |
203 | } |
204 | $cmd = "l $subrange"; |
205 | } else { |
206 | print OUT "Subroutine $1 not found.\n"; |
207 | next; |
208 | } }; |
209 | $cmd =~ /^w\s*(\d*)$/ && do { |
210 | $incr = $window - 1; |
211 | $start = $1 if $1; |
212 | $start -= $preview; |
213 | $cmd = 'l ' . $start . '-' . ($start + $incr); }; |
214 | $cmd =~ /^-$/ && do { |
215 | $incr = $window - 1; |
216 | $cmd = 'l ' . ($start-$window*2) . '+'; }; |
217 | $cmd =~ /^l$/ && do { |
218 | $incr = $window - 1; |
219 | $cmd = 'l ' . $start . '-' . ($start + $incr); }; |
220 | $cmd =~ /^l\s*(\d*)\+(\d*)$/ && do { |
221 | $start = $1 if $1; |
222 | $incr = $2; |
223 | $incr = $window - 1 unless $incr; |
224 | $cmd = 'l ' . $start . '-' . ($start + $incr); }; |
225 | $cmd =~ /^l\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do { |
226 | $end = (!$2) ? $max : ($4 ? $4 : $2); |
227 | $end = $max if $end > $max; |
228 | $i = $2; |
229 | $i = $line if $i eq '.'; |
230 | $i = 1 if $i < 1; |
231 | for (; $i <= $end; $i++) { |
76854fea |
232 | print OUT "$i:\t", $dbline[$i]; |
a687059c |
233 | last if $signal; |
234 | } |
235 | $start = $i; # remember in case they want more |
236 | $start = $max if $start > $max; |
237 | next; }; |
238 | $cmd =~ /^D$/ && do { |
239 | print OUT "Deleting all breakpoints...\n"; |
240 | for ($i = 1; $i <= $max ; $i++) { |
76854fea |
241 | if (defined $dbline{$i}) { |
242 | $dbline{$i} =~ s/^[^\0]+//; |
243 | if ($dbline{$i} =~ s/^\0?$//) { |
244 | delete $dbline{$i}; |
245 | } |
246 | } |
a687059c |
247 | } |
248 | next; }; |
249 | $cmd =~ /^L$/ && do { |
250 | for ($i = 1; $i <= $max; $i++) { |
76854fea |
251 | if (defined $dbline{$i}) { |
252 | print OUT "$i:\t", $dbline[$i]; |
253 | ($stop,$action) = split(/\0/, $dbline{$i}); |
254 | print OUT " break if (", $stop, ")\n" |
255 | if $stop; |
256 | print OUT " action: ", $action, "\n" |
257 | if $action; |
a687059c |
258 | last if $signal; |
259 | } |
260 | } |
261 | next; }; |
262 | $cmd =~ /^b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do { |
263 | $subname = $1; |
76854fea |
264 | $cond = $2 || '1'; |
265 | $subname = "$package'" . $subname unless $subname =~ /'/; |
266 | $subname = "main" . $subname if substr($subname,0,1) eq "'"; |
267 | ($filename,$i) = split(/[:-]/, $sub{$subname}); |
a687059c |
268 | if ($i) { |
76854fea |
269 | *dbline = "_<$filename"; |
270 | ++$i while $dbline[$i] == 0 && $i < $#dbline; |
271 | $dbline{$i} =~ s/^[^\0]*/$cond/; |
a687059c |
272 | } else { |
76854fea |
273 | print OUT "Subroutine $subname not found.\n"; |
a687059c |
274 | } |
275 | next; }; |
276 | $cmd =~ /^b\s*(\d*)\s*(.*)/ && do { |
277 | $i = ($1?$1:$line); |
76854fea |
278 | $cond = $2 || '1'; |
279 | if ($dbline[$i] == 0) { |
a687059c |
280 | print OUT "Line $i not breakable.\n"; |
281 | } else { |
76854fea |
282 | $dbline{$i} =~ s/^[^\0]*/$cond/; |
a687059c |
283 | } |
284 | next; }; |
285 | $cmd =~ /^d\s*(\d+)?/ && do { |
286 | $i = ($1?$1:$line); |
76854fea |
287 | $dbline{$i} =~ s/^[^\0]*//; |
288 | delete $dbline{$i} if $dbline{$i} eq ''; |
a687059c |
289 | next; }; |
290 | $cmd =~ /^A$/ && do { |
291 | for ($i = 1; $i <= $max ; $i++) { |
76854fea |
292 | if (defined $dbline{$i}) { |
293 | $dbline{$i} =~ s/\0[^\0]*//; |
294 | delete $dbline{$i} if $dbline{$i} eq ''; |
295 | } |
a687059c |
296 | } |
297 | next; }; |
298 | $cmd =~ /^<\s*(.*)/ && do { |
299 | $pre = do action($1); |
300 | next; }; |
301 | $cmd =~ /^>\s*(.*)/ && do { |
302 | $post = do action($1); |
303 | next; }; |
304 | $cmd =~ /^a\s*(\d+)(\s+(.*))?/ && do { |
305 | $i = $1; |
76854fea |
306 | if ($dbline[$i] == 0) { |
a687059c |
307 | print OUT "Line $i may not have an action.\n"; |
308 | } else { |
76854fea |
309 | $dbline{$i} =~ s/\0[^\0]*//; |
310 | $dbline .= "\0" . do action($3); |
a687059c |
311 | } |
312 | next; }; |
313 | $cmd =~ /^n$/ && do { |
314 | $single = 2; |
315 | $laststep = $cmd; |
316 | last; }; |
317 | $cmd =~ /^s$/ && do { |
318 | $single = 1; |
319 | $laststep = $cmd; |
320 | last; }; |
321 | $cmd =~ /^c\s*(\d*)\s*$/ && do { |
322 | $i = $1; |
323 | if ($i) { |
76854fea |
324 | if ($dbline[$i] == 0) { |
a687059c |
325 | print OUT "Line $i not breakable.\n"; |
326 | next; |
327 | } |
76854fea |
328 | $dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p. |
a687059c |
329 | } |
330 | for ($i=0; $i <= $#stack; ) { |
331 | $stack[$i++] &= ~1; |
332 | } |
333 | last; }; |
76854fea |
334 | $cmd =~ /^r$/ && do { |
a687059c |
335 | $stack[$#stack] |= 2; |
336 | last; }; |
337 | $cmd =~ /^T$/ && do { |
76854fea |
338 | local($p,$f,$l,$s,$h,$a,@a,@sub); |
339 | for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) { |
340 | @a = @args; |
341 | for (@a) { |
342 | if (/^StB\000/ && length($_) == length($_main{'_main'})) { |
343 | $_ = sprintf("%s",$_); |
344 | } |
345 | else { |
346 | s/'/\\'/g; |
347 | s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/; |
348 | s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg; |
349 | s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg; |
350 | } |
351 | } |
352 | $w = $w ? '@ = ' : '$ = '; |
353 | $a = $h ? '(' . join(', ', @a) . ')' : ''; |
354 | push(@sub, "$w&$s$a from file $f line $l\n"); |
355 | last if $signal; |
356 | } |
357 | for ($i=0; $i <= $#sub; $i++) { |
a687059c |
358 | last if $signal; |
76854fea |
359 | print OUT $sub[$i]; |
a687059c |
360 | } |
361 | next; }; |
362 | $cmd =~ /^\/(.*)$/ && do { |
363 | $inpat = $1; |
364 | $inpat =~ s:([^\\])/$:$1:; |
365 | if ($inpat ne "") { |
366 | eval '$inpat =~ m'."\n$inpat\n"; |
367 | if ($@ ne "") { |
368 | print OUT "$@"; |
369 | next; |
370 | } |
371 | $pat = $inpat; |
372 | } |
373 | $end = $start; |
374 | eval ' |
375 | for (;;) { |
376 | ++$start; |
377 | $start = 1 if ($start > $max); |
378 | last if ($start == $end); |
76854fea |
379 | if ($dbline[$start] =~ m'."\n$pat\n".'i) { |
380 | print OUT "$start:\t", $dbline[$start], "\n"; |
a687059c |
381 | last; |
382 | } |
383 | } '; |
384 | print OUT "/$pat/: not found\n" if ($start == $end); |
385 | next; }; |
386 | $cmd =~ /^\?(.*)$/ && do { |
387 | $inpat = $1; |
388 | $inpat =~ s:([^\\])\?$:$1:; |
389 | if ($inpat ne "") { |
390 | eval '$inpat =~ m'."\n$inpat\n"; |
391 | if ($@ ne "") { |
392 | print OUT "$@"; |
393 | next; |
394 | } |
395 | $pat = $inpat; |
396 | } |
397 | $end = $start; |
398 | eval ' |
399 | for (;;) { |
400 | --$start; |
401 | $start = $max if ($start <= 0); |
402 | last if ($start == $end); |
76854fea |
403 | if ($dbline[$start] =~ m'."\n$pat\n".'i) { |
404 | print OUT "$start:\t", $dbline[$start], "\n"; |
a687059c |
405 | last; |
406 | } |
407 | } '; |
408 | print OUT "?$pat?: not found\n" if ($start == $end); |
409 | next; }; |
410 | $cmd =~ /^!+\s*(-)?(\d+)?$/ && do { |
411 | pop(@hist) if length($cmd) > 1; |
412 | $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist)); |
413 | $cmd = $hist[$i] . "\n"; |
414 | print OUT $cmd; |
415 | redo; }; |
416 | $cmd =~ /^!(.+)$/ && do { |
417 | $pat = "^$1"; |
418 | pop(@hist) if length($cmd) > 1; |
419 | for ($i = $#hist; $i; --$i) { |
420 | last if $hist[$i] =~ $pat; |
421 | } |
422 | if (!$i) { |
423 | print OUT "No such command!\n\n"; |
424 | next; |
425 | } |
426 | $cmd = $hist[$i] . "\n"; |
427 | print OUT $cmd; |
428 | redo; }; |
429 | $cmd =~ /^H\s*(-(\d+))?/ && do { |
430 | $end = $2?($#hist-$2):0; |
431 | $hist = 0 if $hist < 0; |
432 | for ($i=$#hist; $i>$end; $i--) { |
433 | print OUT "$i: ",$hist[$i],"\n" |
434 | unless $hist[$i] =~ /^.?$/; |
435 | }; |
436 | next; }; |
437 | $cmd =~ s/^p( .*)?$/print DB'OUT$1/; |
33b78306 |
438 | $cmd =~ /^=/ && do { |
439 | if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) { |
440 | $alias{$k}="s~$k~$v~"; |
441 | print OUT "$k = $v\n"; |
442 | } elsif ($cmd =~ /^=\s*$/) { |
443 | foreach $k (sort keys(%alias)) { |
444 | if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) { |
445 | print OUT "$k = $v\n"; |
446 | } else { |
447 | print OUT "$k\t$alias{$k}\n"; |
448 | }; |
449 | }; |
450 | }; |
451 | next; }; |
76854fea |
452 | &eval($cmd); |
453 | print OUT "\n"; |
a687059c |
454 | } |
455 | if ($post) { |
76854fea |
456 | &eval($post); |
a687059c |
457 | } |
458 | } |
76854fea |
459 | ($@, $!, $[, $,, $/, $\) = @saved; |
460 | } |
461 | |
462 | sub save { |
463 | @saved = ($@, $!, $[, $,, $/, $\); |
464 | $[ = 0; $, = ""; $/ = "\n"; $\ = ""; |
465 | } |
466 | |
467 | sub eval { |
468 | eval "$usercontext $_[0]; &DB'save"; |
469 | print OUT $@; |
a687059c |
470 | } |
471 | |
472 | sub action { |
473 | local($action) = @_; |
474 | while ($action =~ s/\\$//) { |
475 | print OUT "+ "; |
76854fea |
476 | $action .= &gets; |
a687059c |
477 | } |
478 | $action; |
479 | } |
480 | |
76854fea |
481 | sub gets { |
482 | local($.); |
483 | <IN>; |
484 | } |
485 | |
a687059c |
486 | sub catch { |
487 | $signal = 1; |
488 | } |
489 | |
490 | sub sub { |
491 | push(@stack, $single); |
492 | $single &= 1; |
493 | $single |= 4 if $#stack == $deep; |
a687059c |
494 | if (wantarray) { |
495 | @i = &$sub; |
79a0689e |
496 | $single |= pop(@stack); |
497 | @i; |
a687059c |
498 | } |
499 | else { |
500 | $i = &$sub; |
79a0689e |
501 | $single |= pop(@stack); |
502 | $i; |
a687059c |
503 | } |
a687059c |
504 | } |
505 | |
506 | $single = 1; # so it stops on first executable statement |
a687059c |
507 | @hist = ('?'); |
508 | $SIG{'INT'} = "DB'catch"; |
509 | $deep = 100; # warning if stack gets this deep |
510 | $window = 10; |
511 | $preview = 3; |
512 | |
513 | @stack = (0); |
76854fea |
514 | @ARGS = @ARGV; |
a687059c |
515 | for (@args) { |
516 | s/'/\\'/g; |
517 | s/(.*)/'$1'/ unless /^-?[\d.]+$/; |
518 | } |
a687059c |
519 | |
520 | if (-f '.perldb') { |
521 | do './.perldb'; |
522 | } |
523 | elsif (-f "$ENV{'LOGDIR'}/.perldb") { |
524 | do "$ENV{'LOGDIR'}/.perldb"; |
525 | } |
526 | elsif (-f "$ENV{'HOME'}/.perldb") { |
527 | do "$ENV{'HOME'}/.perldb"; |
528 | } |
529 | |
530 | 1; |