1 require 5.002; # For (defined ref)
4 # Needed for PrettyPrinter only:
6 # require 5.001; # Well, it coredumps anyway undef DB in 5.000 (not now)
8 # translate control chars to ^X - Randal Schwartz
9 # Modifications to print types by Peter Gordon v1.0
11 # Ilya Zakharevich -- patches after 5.001 (and some before ;-)
13 # Won't dump symbol tables and contents of debugged files by default
15 $winsize = 80 unless defined $winsize;
21 $printUndef = 1 unless defined $printUndef;
22 $tick = "auto" unless defined $tick;
23 $unctrl = 'quote' unless defined $unctrl;
25 $dumpReused = 0 unless defined $dumpReused;
26 $bareStringify = 1 unless defined $bareStringify;
31 (print "undef\n"), return unless defined $_[0];
32 (print &stringify
($_[0]), "\n"), return unless ref $_[0];
33 dumpvar
::unwrap
($_[0],0);
36 # This one is good for variable names:
42 return \
$_ if ref \
$_ eq "GLOB";
43 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
48 local($_,$noticks) = @_;
52 return 'undef' unless defined $_ or not $printUndef;
53 return $_ . "" if ref \
$_ eq 'GLOB';
54 $_ = &{'overload::StrVal'}($_)
55 if $bareStringify and ref $_
56 and %overload:: and defined &{'overload::StrVal'};
58 if ($tick eq 'auto') {
59 if (/[\000-\011\013-\037\177]/) {
67 } elsif ($unctrl eq 'unctrl') {
69 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
70 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
72 } elsif ($unctrl eq 'quote') {
73 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
75 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
77 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
78 ($noticks || /^\d+(\.\d*)?\Z/)
84 my $tArrayDepth = $#{$_[0]} ;
85 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
86 unless $arrayDepth eq '' ;
88 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
89 if (!grep(ref $_, @
{$_[0]})) {
90 $short = "0..$#{$_[0]} '" .
91 join("' '", @
{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
92 return $short if length $short <= $compactDump;
98 my $short = &stringify
($_[0], ref $_[0]);
99 if ($veryCompact && ref $_[0]
100 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @
{$_[0]}) )) {
101 my $end = "0..$#{$v} '" .
102 join("' '", @
{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
103 } elsif ($veryCompact && ref $_[0]
104 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
106 $short = $sp . "0..$#{$v} '" .
107 join("' '", @
{$v}[0..$tArrayDepth]) . "'$shortmore";
115 return if $DB::signal
;
117 local($s) = shift ; # extra no of spaces
118 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
119 local($tHashDepth,$tArrayDepth) ;
124 # Check for reused addresses
127 $val = &{'overload::StrVal'}($v)
128 if %overload:: and defined &{'overload::StrVal'};
129 ($address) = $val =~ /(0x[0-9a-f]+)\)$/ ;
130 if (!$dumpReused && defined $address) {
131 $address{$address}++ ;
132 if ( $address{$address} > 1 ) {
133 print "${sp}-> REUSED_ADDRESS\n" ;
137 } elsif (ref \
$v eq 'GLOB') {
138 $address = "$v" . ""; # To avoid a bug with globs
139 $address{$address}++ ;
140 if ( $address{$address} > 1 ) {
141 print "${sp}*DUMPED_GLOB*\n" ;
146 if (ref $v eq 'Regexp') {
149 print "$sp-> qr/$re/\n";
153 if ( UNIVERSAL
::isa
($v, 'HASH') ) {
154 @sortKeys = sort keys(%$v) ;
156 $tHashDepth = $#sortKeys ;
157 $tHashDepth = $#sortKeys < $hashDepth-1 ?
$#sortKeys : $hashDepth-1
158 unless $hashDepth eq '' ;
159 $more = "....\n" if $tHashDepth < $#sortKeys ;
161 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
162 $#sortKeys = $tHashDepth ;
163 if ($compactDump && !grep(ref $_, values %{$v})) {
166 # Next row core dumps during require from DB on 5.000, even with map {"_"}
167 # map {&stringify($_) . " => " . &stringify($v->{$_})}
168 # @sortKeys) . "'$shortmore";
172 push @keys, &stringify
($_) . " => " . &stringify
($v->{$_});
174 $short .= join ', ', @keys;
175 $short .= $shortmore;
176 (print "$short\n"), return if length $short <= $compactDump;
178 for $key (@sortKeys) {
179 return if $DB::signal
;
180 $value = $ {$v}{$key} ;
181 print "$sp", &stringify
($key), " => ";
184 print "$sp empty hash\n" unless @sortKeys;
185 print "$sp$more" if defined $more ;
186 } elsif ( UNIVERSAL
::isa
($v, 'ARRAY') ) {
187 $tArrayDepth = $#{$v} ;
189 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
190 unless $arrayDepth eq '' ;
191 $more = "....\n" if $tArrayDepth < $#{$v} ;
193 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
194 if ($compactDump && !grep(ref $_, @
{$v})) {
196 $short = $sp . "0..$#{$v} " .
198 map {exists $v->[$_] ? stringify
$v->[$_] : "empty"} ($[..$tArrayDepth)
201 $short = $sp . "empty array";
203 (print "$short\n"), return if length $short <= $compactDump;
205 #if ($compactDump && $short = ShortArray($v)) {
209 for $num ($[ .. $tArrayDepth) {
210 return if $DB::signal
;
212 if (exists $v->[$num]) {
213 DumpElem
$v->[$num], $s;
215 print "empty slot\n";
218 print "$sp empty array\n" unless @
$v;
219 print "$sp$more" if defined $more ;
220 } elsif ( UNIVERSAL
::isa
($v, 'SCALAR') or ref $v eq 'REF' ) {
223 } elsif ( UNIVERSAL
::isa
($v, 'CODE') ) {
226 } elsif ( UNIVERSAL
::isa
($v, 'GLOB') ) {
227 print "$sp-> ",&stringify
($$v,1),"\n";
230 dumpglob
($s, "{$$v}", $$v, 1);
231 } elsif (defined ($fileno = fileno($v))) {
232 print( (' ' x
($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
234 } elsif (ref \
$v eq 'GLOB') {
236 dumpglob
($s, "{$v}", $v, 1) if $globPrint;
237 } elsif (defined ($fileno = fileno(\
$v))) {
238 print( (' ' x
$s) . "FileHandle({$v}) => fileno($fileno)\n" );
245 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
246 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
250 $compactDump = shift if @_;
251 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
256 $veryCompact = shift if @_;
257 compactDump
(1) if !$compactDump and $veryCompact;
264 if ($in eq 'unctrl' or $in eq 'quote') {
267 print "Unknown value for `unctrl'.\n";
274 if (@_ and $_[0] eq '"') {
277 } elsif (@_ and $_[0] eq 'auto') {
280 } elsif (@_) { # Need to set
288 return if $DB::signal
;
289 my ($off,$key, $val, $all) = @_;
290 local(*entry
) = $val;
292 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
293 print( (' ' x
$off) . "\$", &unctrl
($key), " = " );
294 DumpElem
$entry, 3+$off;
296 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
297 print( (' ' x
$off) . "\@$key = (\n" );
298 unwrap
(\
@entry,3+$off) ;
299 print( (' ' x
$off) . ")\n" );
301 if ($key ne "main::" && $key ne "DB::" && %entry
302 && ($dumpPackages or $key !~ /::$/)
303 && ($key !~ /^_</ or $dumpDBFiles)
304 && !($package eq "dumpvar" and $key eq "stab")) {
305 print( (' ' x
$off) . "\%$key = (\n" );
306 unwrap
(\
%entry,3+$off) ;
307 print( (' ' x
$off) . ")\n" );
309 if (defined ($fileno = fileno(*entry
))) {
310 print( (' ' x
$off) . "FileHandle($key) => fileno($fileno)\n" );
313 if (defined &entry
) {
319 sub CvGV_name_or_bust
{
321 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
322 $in = \
&$in; # Hard reference...
323 eval {require Devel
::Peek
; 1} or return;
324 my $gv = Devel
::Peek
::CvGV
($in) or return;
325 *$gv{PACKAGE
} . '::' . *$gv{NAME
};
332 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
333 my $subref = defined $1 ? \
&$sub : \
&$ini;
334 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
335 || (($s = CvGV_name_or_bust
($subref)) && $DB::sub{$s})
336 || ($subdump && ($s = findsubs
("$subref")) && $DB::sub{$s});
337 $place = '???' unless defined $place;
338 $s = $sub unless defined $s;
339 print( (' ' x
$off) . "&$s in $place\n" );
343 return undef unless %DB::sub;
344 my ($addr, $name, $loc);
345 while (($name, $loc) = each %DB::sub) {
347 $subs{"$addr"} = $name;
354 my ($package,@vars) = @_;
355 local(%address,$key,$val,$^W
);
356 $package .= "::" unless $package =~ /::$/;
358 while ($package =~ /(\w+?::)/g){
359 *stab
= $ {stab
}{$1};
361 local $TotalStrings = 0;
363 local $CompleteTotal = 0;
364 while (($key,$val) = each(%stab)) {
365 return if $DB::signal
;
366 next if @vars && !grep( matchvar
($key, $_), @vars );
368 globUsage
(\
$val, $key)
369 if ($package ne 'dumpvar' or $key ne 'stab')
370 and ref(\
$val) eq 'GLOB';
372 dumpglob
(0,$key, $val);
376 print "String space: $TotalStrings bytes in $Strings strings.\n";
377 $CompleteTotal += $TotalStrings;
378 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
383 my $size = length($_[0]);
384 $TotalStrings += $size;
389 sub arrayUsage
{ # array ref, name
391 map {$size += scalarUsage
($_)} @
{$_[0]};
393 print "\@$_[1] = $len item", ($len > 1 ?
"s" : ""),
394 " (data: $size bytes)\n"
396 $CompleteTotal += $size;
400 sub hashUsage
{ # hash ref, name
401 my @keys = keys %{$_[0]};
402 my @values = values %{$_[0]};
403 my $keys = arrayUsage \
@keys;
404 my $values = arrayUsage \
@values;
406 my $total = $keys + $values;
407 print "\%$_[1] = $len item", ($len > 1 ?
"s" : ""),
408 " (keys: $keys; values: $values; total: $total bytes)\n"
413 sub globUsage
{ # glob ref, name
414 local *name
= *{$_[0]};
416 $total += scalarUsage
$name if defined $name;
417 $total += arrayUsage \
@name, $_[1] if @name;
418 $total += hashUsage \
%name, $_[1] if %name and $_[1] ne "main::"
419 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
424 my ($package,@vars) = @_;
425 $package .= "::" unless $package =~ /::$/;
426 local *stab
= *{"main::"};
427 while ($package =~ /(\w+?::)/g){
428 *stab
= $ {stab
}{$1};
430 local $TotalStrings = 0;
431 local $CompleteTotal = 0;
433 while (($key,$val) = each(%stab)) {
434 next if @vars && !grep($key eq $_,@vars);
435 globUsage \
$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
437 print "String space: $TotalStrings.\n";
438 $CompleteTotal += $TotalStrings;
439 print "\nGrand total = $CompleteTotal bytes\n";