home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / PERL30X.ZIP / PERLDB.PL < prev    next >
Perl Script  |  1991-01-14  |  15KB  |  534 lines

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