home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl501m.zip / lib / perl5db.pl < prev    next >
Perl Script  |  1995-03-16  |  16KB  |  573 lines

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