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