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