home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / perldb.pl < prev    next >
Perl Script  |  1991-06-14  |  16KB  |  550 lines

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