# modified Perl debugger, to be run from Emacs in perldb-mode
# Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
# Johan Vromans -- upgrade to 4.0 pl 10
$header = '$RCSfile: perldb.pl,v $$Revision: 4.0.1.2 $$Date: 91/11/05 17:55:58 $';
# This file is automatically included if you do perl -d.
# It's probably not useful to include this yourself.
# Perl supplies the values for @line and %sub. It effectively inserts
# a do DB'DB(<linenum>); in front of every place that can
# have a breakpoint. It also inserts a do 'perldb.pl' before the first line.
# Revision 4.0.1.2 91/11/05 17:55:58 lwall
# patch11: perldb.pl modified to run within emacs in perldb-mode
# Revision 4.0.1.1 91/06/07 11:17:44 lwall
# patch4: added $^P variable to control calling of perldb routines
# patch4: debugger sometimes listed wrong number of lines for a statement
# Revision 4.0 91/03/20 01:25:50 lwall
# Revision 3.0.1.6 91/01/11 18:08:58 lwall
# patch42: @_ couldn't be accessed from debugger
# Revision 3.0.1.5 90/11/10 01:40:26 lwall
# patch38: the debugger wouldn't stop correctly or do action routines
# Revision 3.0.1.4 90/10/15 17:40:38 lwall
# patch29: the debugger now understands packages and evals
# patch29: scripts now run at almost full speed under the debugger
# patch29: more variables are settable from debugger
# Revision 3.0.1.3 90/08/09 04:00:58 lwall
# patch19: debugger now allows continuation lines
# patch19: debugger can now dump lists of variables
# patch19: debugger can now add aliases easily from prompt
# Revision 3.0.1.2 90/03/12 16:39:39 lwall
# patch13: perl -d didn't format stack traces of *foo right
# patch13: perl -d wiped out scalar return values of subroutines
# Revision 3.0.1.1 89/10/26 23:14:02 lwall
# patch1: RCS expanded an unintended $Header in lib/perldb.pl
# Revision 3.0 89/10/18 15:19:46 lwall
# Revision 2.0 88/06/05 00:09:45 root
open(IN
, "</dev/tty") || open(IN
, "<&STDIN"); # so we don't dingle stdin
open(OUT
,">/dev/tty") || open(OUT
, ">&STDOUT"); # so we don't dongle stdout
$| = 1; # for real STDOUT
# Is Perl being run from Emacs?
$emacs = $main'ARGV[$[] eq '-emacs
';
shift(@main'ARGV
) if $emacs;
$header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
print OUT
"\nLoading DB routines from $header\n";
print OUT
("Emacs support ",
$emacs ?
"enabled" : "available",
print OUT
"\nEnter h for help.\n\n";
($package, $filename, $line) = caller;
$usercontext = '($@, $!, $[, $,, $/, $\) = @saved;' .
"package $package;"; # this won't let them modify, alas
local($^P
) = 0; # don't debug our own evals
local(*dbline
) = "_<$filename";
if (($stop,$action) = split(/\0/,$dbline{$line})) {
$evalarg = "\$DB'signal |= do {$stop;}"; &eval;
$dbline{$line} =~ s/;9($|\0)/$1/;
if ($single || $trace || $signal) {
print OUT
"\032\032$filename:$line:0\n";
print OUT
"$package'" unless $sub =~ /'/;
print OUT
"$sub($filename:$line):\t",$dbline[$line];
for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
last if $dbline[$i] =~ /^\s*(}|#|\n)/;
print OUT
"$sub($filename:$i):\t",$dbline[$i];
$evalarg = $action, &eval if $action;
if ($single || $signal) {
$evalarg = $pre, &eval if $pre;
print OUT
$#stack . " levels deep in subroutine calls!\n"
while ((print OUT
" DB<", $#hist+1, "> "), $cmd=&gets
) {
$cmd =~ /^$/ && ($cmd = $laststep);
push(@hist,$cmd) if length($cmd) > 1;
($i) = split(/\s+/,$cmd);
eval "\$cmd =~ $alias{$i}", print OUT
$@
if $alias{$i};
n Next, steps over subroutine calls.
r Return from current subroutine.
c [line] Continue; optionally inserts a one-time-only breakpoint
l min+incr List incr+1 lines starting at min.
w line List window around line.
l subname List subroutine.
f filename Switch to filename.
/pattern/ Search forwards for pattern; final / is optional.
?pattern? Search backwards for pattern.
L List breakpoints and actions.
Set breakpoint; line defaults to the current execution line;
condition breaks if it evaluates to true, defaults to \'1\'.
Set breakpoint at first line of subroutine.
d [line] Delete breakpoint.
D Delete all breakpoints.
Set an action to be done before the line is executed.
Sequence is: check for breakpoint, print line if necessary,
do action, prompt user if breakpoint or step, evaluate line.
V [pkg [vars]] List some (default all) variables in package (default current).
X [vars] Same as \"V currentpackage [vars]\".
< command Define command before prompt.
> command Define command after prompt.
! number Redo command (default previous command).
! -number Redo number\'th to last command.
H -number Display last number commands (default all).
p expr Same as \"print DB'OUT expr\" in current package.
= [alias value] Define a command alias, or list current aliases.
command Execute as a perl statement in current package.
print OUT
"Trace = ".($trace?
"on":"off")."\n";
foreach $subname (sort(keys %sub)) {
$cmd =~ s/^X\b/V $package/;
$cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
do 'dumpvar.pl' unless defined &main
'dumpvar;
if (defined &main'dumpvar
) {
&main
'dumpvar($packname,@vars);
print DB'OUT
"dumpvar.pl not available.\n";
$cmd =~ /^f\b\s*(.*)/ && do {
print OUT
"The old f command is now the r command.\n";
print OUT
"The new f command switches filenames.\n";
if (!defined $_main{'_<' . $file}) {
if (($try) = grep(m
#^_<.*$file#, keys %_main)) {
if (!defined $_main{'_<' . $file}) {
print OUT
"There's no code here anything matching $file.\n";
elsif ($file ne $filename) {
$cmd =~ /^l\b\s*(['A-Za-z_]['\w]*)/ && do {
$subname = "main'" . $subname unless $subname =~ /'/;
$subname = "main" . $subname if substr($subname,0,1) eq "'";
($file,$subrange) = split(/:/,$sub{$subname});
if ($file ne $filename) {
if (eval($subrange) < -$window) {
print OUT
"Subroutine $1 not found.\n";
$cmd =~ /^w\b\s*(\d*)$/ && do {
$cmd = 'l ' . $start . '-' . ($start + $incr); };
$cmd = 'l ' . ($start-$window*2) . '+'; };
$cmd = 'l ' . $start . '-' . ($start + $incr); };
$cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
$incr = $window - 1 unless $incr;
$cmd = 'l ' . $start . '-' . ($start + $incr); };
$cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
$end = (!$2) ?
$max : ($4 ?
$4 : $2);
$end = $max if $end > $max;
print OUT
"\032\032$filename:$i:0\n";
for (; $i <= $end; $i++) {
print OUT
"$i:\t", $dbline[$i];
$start = $i; # remember in case they want more
$start = $max if $start > $max;
print OUT
"Deleting all breakpoints...\n";
for ($i = 1; $i <= $max ; $i++) {
if (defined $dbline{$i}) {
$dbline{$i} =~ s/^[^\0]+//;
if ($dbline{$i} =~ s/^\0?$//) {
for ($i = 1; $i <= $max; $i++) {
if (defined $dbline{$i}) {
print OUT
"$i:\t", $dbline[$i];
($stop,$action) = split(/\0/, $dbline{$i});
print OUT
" break if (", $stop, ")\n"
print OUT
" action: ", $action, "\n"
$cmd =~ /^b\b\s*(['A-Za-z_]['\w]*)\s*(.*)/ && do {
$subname = "$package'" . $subname unless $subname =~ /'/;
$subname = "main" . $subname if substr($subname,0,1) eq "'";
($filename,$i) = split(/[:-]/, $sub{$subname});
++$i while $dbline[$i] == 0 && $i < $#dbline;
$dbline{$i} =~ s/^[^\0]*/$cond/;
print OUT
"Subroutine $subname not found.\n";
$cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
print OUT
"Line $i not breakable.\n";
$dbline{$i} =~ s/^[^\0]*/$cond/;
$cmd =~ /^d\b\s*(\d+)?/ && do {
$dbline{$i} =~ s/^[^\0]*//;
delete $dbline{$i} if $dbline{$i} eq '';
for ($i = 1; $i <= $max ; $i++) {
if (defined $dbline{$i}) {
$dbline{$i} =~ s/\0[^\0]*//;
delete $dbline{$i} if $dbline{$i} eq '';
$cmd =~ /^<\s*(.*)/ && do {
$cmd =~ /^>\s*(.*)/ && do {
$cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
print OUT
"Line $i may not have an action.\n";
$dbline{$i} =~ s/\0[^\0]*//;
$dbline{$i} .= "\0" . do action
($3);
$cmd =~ /^c\b\s*(\d*)\s*$/ && do {
print OUT
"Line $i not breakable.\n";
$dbline{$i} =~ s/(\0|$)/;9$1/; # add one-time-only b.p.
for ($i=0; $i <= $#stack; ) {
local($p,$f,$l,$s,$h,$a,@a,@sub);
for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
if (/^StB\000/ && length($_) == length($_main{'_main'})) {
s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
$w = $w ?
'@ = ' : '$ = ';
$a = $h ?
'(' . join(', ', @a) . ')' : '';
push(@sub, "$w&$s$a from file $f line $l\n");
for ($i=0; $i <= $#sub; $i++) {
$cmd =~ /^\/(.*)$/ && do {
$inpat =~ s
:([^\\])/$:$1:;
eval '$inpat =~ m'."\n$inpat\n";
$start = 1 if ($start > $max);
last if ($start == $end);
if ($dbline[$start] =~ m'."\n$pat\n".'i) {
print OUT "\032\032$filename:$start:0\n";
print OUT "$start:\t", $dbline[$start], "\n";
print OUT
"/$pat/: not found\n" if ($start == $end);
$cmd =~ /^\?(.*)$/ && do {
$inpat =~ s
:([^\\])\?$:$1:;
eval '$inpat =~ m'."\n$inpat\n";
$start = $max if ($start <= 0);
last if ($start == $end);
if ($dbline[$start] =~ m'."\n$pat\n".'i) {
print OUT "\032\032$filename:$start:0\n";
print OUT "$start:\t", $dbline[$start], "\n";
print OUT
"?$pat?: not found\n" if ($start == $end);
$cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
pop(@hist) if length($cmd) > 1;
$i = ($1?
($#hist-($2?
$2:1)):($2?
$2:$#hist));
$cmd =~ /^!(.+)$/ && do {
pop(@hist) if length($cmd) > 1;
for ($i = $#hist; $i; --$i) {
last if $hist[$i] =~ $pat;
print OUT
"No such command!\n\n";
$cmd =~ /^H\b\s*(-(\d+))?/ && do {
for ($i=$#hist; $i>$end; $i--) {
print OUT
"$i: ",$hist[$i],"\n"
unless $hist[$i] =~ /^.?$/;
$cmd =~ s/^p( .*)?$/print DB'OUT$1/;
if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
} elsif ($cmd =~ /^=\s*$/) {
foreach $k (sort keys(%alias)) {
if (($v = $alias{$k}) =~ s
~^s\
~$k\
~(.*)\
~$~$1~) {
print OUT
"$k\t$alias{$k}\n";
($@
, $!, $[, $,, $/, $\
) = @saved;
@saved = ($@
, $!, $[, $,, $/, $\
);
$[ = 0; $, = ""; $/ = "\n"; $\
= "";
# The following takes its argument via $evalarg to preserve current @_
eval "$usercontext $evalarg; &DB'save";
while ($action =~ s/\\$//) {
$single |= 4 if $#stack == $deep;
$single = 1; # so it stops on first executable statement
$SIG{'INT'} = "DB'catch";
$deep = 100; # warning if stack gets this deep
s/(.*)/'$1'/ unless /^-?[\d.]+$/;
elsif (-f
"$ENV{'LOGDIR'}/.perldb") {
do "$ENV{'LOGDIR'}/.perldb";
elsif (-f
"$ENV{'HOME'}/.perldb") {
do "$ENV{'HOME'}/.perldb";