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