require 5.014; # For more reliable $@ after eval
# Needed for PrettyPrinter only:
# require 5.001; # Well, it coredumps anyway undef DB in 5.000 (not now)
# translate control chars to ^X - Randal Schwartz
# Modifications to print types by Peter Gordon v1.0
# Ilya Zakharevich -- patches after 5.001 (and some before ;-)
# Won't dump symbol tables and contents of debugged files by default
$winsize = 80 unless defined $winsize;
sub ASCII { return ord('A') == 65; }
$printUndef = 1 unless defined $printUndef;
$tick = "auto" unless defined $tick;
$unctrl = 'quote' unless defined $unctrl;
$dumpReused = 0 unless defined $dumpReused;
$bareStringify = 1 unless defined $bareStringify;
my $APC = chr utf8::unicode_to_native(0x9F);
my $backslash_c_question = (ASCII) ? '\177' : $APC;
(print "undef\n"), return unless defined $_[0];
(print &stringify($_[0]), "\n"), return unless ref $_[0];
dumpvar::unwrap($_[0], 0, $_[1]);
# This one is good for variable names:
for (my($dummy) = shift) {
return \$_ if ref \$_ eq "GLOB";
s/([\000-\037])/ '^' . chr(utf8::unicode_to_native(ord($1)^64))/eg;
s/ $backslash_c_question /^?/xg;
map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
if (eval { $string = _stringify(@_); 1 }) {
return "<< value could not be dumped: $@ >>";
(my $__, local $noticks) = @_;
return 'undef' unless defined $_ or not $printUndef;
return $_ . "" if ref \$_ eq 'GLOB';
$_ = &{'overload::StrVal'}($_)
if $bareStringify and ref $_
and %overload:: and defined &{'overload::StrVal'};
if (/[^[:^cntrl:]\n]/u) { # All controls but \n get '"'
} elsif ($unctrl eq 'unctrl') {
s/([[:^ascii:]])/'\\0x'.sprintf('%2X',ord($1))/eg
} elsif ($unctrl eq 'quote') {
s/([\"\\\$\@])/\\$1/g if $tick eq '"';
s/([\000-\037$backslash_c_question])/'\\c'._escaped_ord($1)/eg;
s/([[:^ascii:]])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
return ($noticks || /^\d+(\.\d*)?\Z/)
# Ensure a resulting \ is escaped to be \\
if ($chr eq $backslash_c_question) {
$chr = chr(utf8::unicode_to_native(ord($chr)^64));
my $tArrayDepth = $#{$_[0]} ;
$tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
unless $arrayDepth eq '' ;
$shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
if (!grep(ref $_, @{$_[0]})) {
$short = "0..$#{$_[0]} '" .
join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
return $short if length $short <= $compactDump;
my $short = &stringify($_[0], ref $_[0]);
if ($veryCompact && ref $_[0]
&& (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
my $end = "0..$#{$v} '" .
join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
} elsif ($veryCompact && ref $_[0]
&& (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
$short = $sp . "0..$#{$v} '" .
join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
unwrap($_[0],$_[1],$_[2]) if ref $_[0];
local($s) = shift ; # extra no of spaces
local($m) = shift ; # maximum recursion depth
local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
local($tHashDepth,$tArrayDepth) ;
# Check for reused addresses
$val = &{'overload::StrVal'}($v)
if %overload:: and defined &{'overload::StrVal'};
# Match type and address.
# Unblessed references will look like TYPE(0x...)
# Blessed references will look like Class=TYPE(0x...)
$val =~ s/^.*=//; # suppress the Class part, just keep TYPE(0x...)
$val =~ /([^\(]+) # Keep stuff that's
(0x[0-9a-f]+) # Save the address
$/x; # Should be at end now
if (!$dumpReused && defined $address) {
if ( $address{$address} > 1 ) {
print "${sp}-> REUSED_ADDRESS\n" ;
} elsif (ref \$v eq 'GLOB') {
# This is a raw glob. Special handling for that.
$address = "$v" . ""; # To avoid a bug with globs
if ( $address{$address} > 1 ) {
print "${sp}*DUMPED_GLOB*\n" ;
if (ref $v eq 'Regexp') {
# Reformat the regexp to look the standard way.
if ( $item_type eq 'HASH' ) {
# Hash ref or hash-based object.
my @sortKeys = sort keys(%$v) ;
$tHashDepth = $#sortKeys ;
$tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
unless $hashDepth eq '' ;
$more = "....\n" if $tHashDepth < $#sortKeys ;
$shortmore = ", ..." if $tHashDepth < $#sortKeys ;
$#sortKeys = $tHashDepth ;
if ($compactDump && !grep(ref $_, values %{$v})) {
# Next row core dumps during require from DB on 5.000, even with map {"_"}
# map {&stringify($_) . " => " . &stringify($v->{$_})}
# @sortKeys) . "'$shortmore";
push @keys, &stringify($_) . " => " . &stringify($v->{$_});
$short .= join ', ', @keys;
(print "$short\n"), return if length $short <= $compactDump;
print "$sp", &stringify($key), " => ";
DumpElem $value, $s, $m-1;
print "$sp empty hash\n" unless @sortKeys;
print "$sp$more" if defined $more ;
} elsif ( $item_type eq 'ARRAY' ) {
# Array ref or array-based object. Also: undef.
# See how big the array is.
$tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
if defined $arrayDepth && $arrayDepth ne '';
# Yep. Don't show it all.
$more = "....\n" if $tArrayDepth < $#{$v} ;
$shortmore = " ..." if $tArrayDepth < $#{$v} ;
if ($compactDump && !grep(ref $_, @{$v})) {
$short = $sp . "0..$#{$v} " .
map {exists $v->[$_] ? stringify $v->[$_] : "empty"} (0..$tArrayDepth)
$short = $sp . "empty array";
(print "$short\n"), return if length $short <= $compactDump;
#if ($compactDump && $short = ShortArray($v)) {
for $num (0 .. $tArrayDepth) {
if (defined $v->[$num]) {
DumpElem $v->[$num], $s, $m-1;
print "$sp empty array\n" unless @$v;
print "$sp$more" if defined $more ;
} elsif ( $item_type eq 'SCALAR' ) {
} elsif ( $item_type eq 'REF' ) {
return unless defined $$v;
} elsif ( $item_type eq 'CODE' ) {
# Code object or reference.
} elsif ( $item_type eq 'GLOB' ) {
# Glob object or reference.
print "$sp-> ",&stringify($$v,1),"\n";
dumpglob($s, "{$$v}", $$v, 1, $m-1);
} elsif (defined ($fileno = eval {fileno($v)})) {
print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
} elsif (ref \$v eq 'GLOB') {
dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
} elsif (defined ($fileno = eval {fileno(\$v)})) {
print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
print( (' ' x $s) . "<< value could not be dumped: $@ >>\n");
(my $var = $_[0]) =~ s/.//;
($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
($1 eq '!') ^ (eval { $var =~ /$2$3/ });
($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
$compactDump = shift if @_;
$compactDump = 6*80-1 if $compactDump and $compactDump < 2;
$veryCompact = shift if @_;
compactDump(1) if !$compactDump and $veryCompact;
if ($in eq 'unctrl' or $in eq 'quote') {
print "Unknown value for 'unctrl'.\n";
if (@_ and $_[0] eq '"') {
} elsif (@_ and $_[0] eq 'auto') {
} elsif (@_) { # Need to set
my ($off,$key, $val, $all, $m) = @_;
if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
print( (' ' x $off) . "\$", &unctrl($key), " = " );
DumpElem $entry, 3+$off, $m;
if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
print( (' ' x $off) . "\@$key = (\n" );
unwrap(\@entry,3+$off,$m) ;
print( (' ' x $off) . ")\n" );
if ($key ne "main::" && $key ne "DB::" && %entry
&& ($dumpPackages or $key !~ /::$/)
&& ($key !~ /^_</ or $dumpDBFiles)
&& !($package eq "dumpvar" and $key eq "stab")) {
print( (' ' x $off) . "\%$key = (\n" );
unwrap(\%entry,3+$off,$m) ;
print( (' ' x $off) . ")\n" );
if (defined ($fileno = eval{fileno(*entry)})) {
print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
my ($key, $val, $m, @vars) = @_;
return if @vars && !grep( matchlex($key, $_), @vars );
my $off = 0; # It reads better this way
if (UNIVERSAL::isa($val,'ARRAY')) {
print( (' ' x $off) . "$key = (\n" );
print( (' ' x $off) . ")\n" );
elsif (UNIVERSAL::isa($val,'HASH')) {
print( (' ' x $off) . "$key = (\n" );
print( (' ' x $off) . ")\n" );
elsif (UNIVERSAL::isa($val,'IO')) {
print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
# No lexical subroutines yet...
# elsif (UNIVERSAL::isa($val,'CODE')) {
print( (' ' x $off) . &unctrl($key), " = " );
DumpElem $$val, 3+$off, $m;
return if $skipCvGV; # Backdoor to avoid problems if XS broken...
$in = \&$in; # Hard reference...
eval {require Devel::Peek; 1} or return;
my $gv = Devel::Peek::CvGV($in) or return;
*$gv{PACKAGE} . '::' . *$gv{NAME};
$sub = $1 if $sub =~ /^\{\*(.*)\}$/;
my $subref = defined $1 ? \&$sub : \&$ini;
my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
|| (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
|| ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
$place = '???' unless defined $place;
$s = $sub unless defined $s;
print( (' ' x $off) . "&$s in $place\n" );
return undef unless %DB::sub;
while (($name, $loc) = each %DB::sub) {
my ($package,$m,@vars) = @_;
local(%address,$key,$val,$^W);
$package .= "::" unless $package =~ /::$/;
while ($package =~ /(\w+?::)/g){
local $CompleteTotal = 0;
while (($key,$val) = each(%stab)) {
next if @vars && !grep( matchvar($key, $_), @vars );
if ($package ne 'dumpvar' or $key ne 'stab')
and ref(\$val) eq 'GLOB';
dumpglob(0,$key, $val, 0, $m);
print "String space: $TotalStrings bytes in $Strings strings.\n";
$CompleteTotal += $TotalStrings;
print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";