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;
49 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
54 local($_,$noticks) = @_;
58 return 'undef' unless defined $_ or not $printUndef;
59 return $_ . "" if ref \$_ eq 'GLOB';
60 $_ = &{'overload::StrVal'}($_)
61 if $bareStringify and ref $_
62 and %overload:: and defined &{'overload::StrVal'};
64 if ($tick eq 'auto') {
65 if (/[\000-\011\013-\037\177]/) {
73 } elsif ($unctrl eq 'unctrl') {
75 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
77 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
79 } elsif ($unctrl eq 'quote') {
80 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
82 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
85 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
86 ($noticks || /^\d+(\.\d*)?\Z/)
92 my $tArrayDepth = $#{$_[0]} ;
93 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
94 unless $arrayDepth eq '' ;
96 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
97 if (!grep(ref $_, @{$_[0]})) {
98 $short = "0..$#{$_[0]} '" .
99 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
100 return $short if length $short <= $compactDump;
106 my $short = &stringify($_[0], ref $_[0]);
107 if ($veryCompact && ref $_[0]
108 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
109 my $end = "0..$#{$v} '" .
110 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
111 } elsif ($veryCompact && ref $_[0]
112 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
114 $short = $sp . "0..$#{$v} '" .
115 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
123 return if $DB::signal;
125 local($s) = shift ; # extra no of spaces
126 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
127 local($tHashDepth,$tArrayDepth) ;
132 # Check for reused addresses
135 $val = &{'overload::StrVal'}($v)
136 if %overload:: and defined &{'overload::StrVal'};
137 ($address) = $val =~ /(0x[0-9a-f]+)\)$/ ;
138 if (!$dumpReused && defined $address) {
139 $address{$address}++ ;
140 if ( $address{$address} > 1 ) {
141 print "${sp}-> REUSED_ADDRESS\n" ;
145 } elsif (ref \$v eq 'GLOB') {
146 $address = "$v" . ""; # To avoid a bug with globs
147 $address{$address}++ ;
148 if ( $address{$address} > 1 ) {
149 print "${sp}*DUMPED_GLOB*\n" ;
154 if (ref $v eq 'Regexp') {
157 print "$sp-> qr/$re/\n";
161 if ( UNIVERSAL::isa($v, 'HASH') ) {
162 @sortKeys = sort keys(%$v) ;
164 $tHashDepth = $#sortKeys ;
165 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
166 unless $hashDepth eq '' ;
167 $more = "....\n" if $tHashDepth < $#sortKeys ;
169 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
170 $#sortKeys = $tHashDepth ;
171 if ($compactDump && !grep(ref $_, values %{$v})) {
174 # Next row core dumps during require from DB on 5.000, even with map {"_"}
175 # map {&stringify($_) . " => " . &stringify($v->{$_})}
176 # @sortKeys) . "'$shortmore";
180 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
182 $short .= join ', ', @keys;
183 $short .= $shortmore;
184 (print "$short\n"), return if length $short <= $compactDump;
186 for $key (@sortKeys) {
187 return if $DB::signal;
188 $value = $ {$v}{$key} ;
189 print "$sp", &stringify($key), " => ";
192 print "$sp empty hash\n" unless @sortKeys;
193 print "$sp$more" if defined $more ;
194 } elsif ( UNIVERSAL::isa($v, 'ARRAY') ) {
195 $tArrayDepth = $#{$v} ;
197 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
198 if defined $arrayDepth && $arrayDepth ne '';
199 $more = "....\n" if $tArrayDepth < $#{$v} ;
201 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
202 if ($compactDump && !grep(ref $_, @{$v})) {
204 $short = $sp . "0..$#{$v} " .
206 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} ($[..$tArrayDepth)
209 $short = $sp . "empty array";
211 (print "$short\n"), return if length $short <= $compactDump;
213 #if ($compactDump && $short = ShortArray($v)) {
217 for $num ($[ .. $tArrayDepth) {
218 return if $DB::signal;
220 if (exists $v->[$num]) {
221 DumpElem $v->[$num], $s;
223 print "empty slot\n";
226 print "$sp empty array\n" unless @$v;
227 print "$sp$more" if defined $more ;
228 } elsif ( UNIVERSAL::isa($v, 'SCALAR') or ref $v eq 'REF' ) {
231 } elsif ( UNIVERSAL::isa($v, 'CODE') ) {
234 } elsif ( UNIVERSAL::isa($v, 'GLOB') ) {
235 print "$sp-> ",&stringify($$v,1),"\n";
238 dumpglob($s, "{$$v}", $$v, 1);
239 } elsif (defined ($fileno = fileno($v))) {
240 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
242 } elsif (ref \$v eq 'GLOB') {
244 dumpglob($s, "{$v}", $v, 1) if $globPrint;
245 } elsif (defined ($fileno = fileno(\$v))) {
246 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
253 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
254 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
258 $compactDump = shift if @_;
259 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
264 $veryCompact = shift if @_;
265 compactDump(1) if !$compactDump and $veryCompact;
272 if ($in eq 'unctrl' or $in eq 'quote') {
275 print "Unknown value for `unctrl'.\n";
282 if (@_ and $_[0] eq '"') {
285 } elsif (@_ and $_[0] eq 'auto') {
288 } elsif (@_) { # Need to set
296 return if $DB::signal;
297 my ($off,$key, $val, $all) = @_;
298 local(*entry) = $val;
300 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
301 print( (' ' x $off) . "\$", &unctrl($key), " = " );
302 DumpElem $entry, 3+$off;
304 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
305 print( (' ' x $off) . "\@$key = (\n" );
306 unwrap(\@entry,3+$off) ;
307 print( (' ' x $off) . ")\n" );
309 if ($key ne "main::" && $key ne "DB::" && %entry
310 && ($dumpPackages or $key !~ /::$/)
311 && ($key !~ /^_</ or $dumpDBFiles)
312 && !($package eq "dumpvar" and $key eq "stab")) {
313 print( (' ' x $off) . "\%$key = (\n" );
314 unwrap(\%entry,3+$off) ;
315 print( (' ' x $off) . ")\n" );
317 if (defined ($fileno = fileno(*entry))) {
318 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
321 if (defined &entry) {
327 sub CvGV_name_or_bust {
329 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
330 $in = \&$in; # Hard reference...
331 eval {require Devel::Peek; 1} or return;
332 my $gv = Devel::Peek::CvGV($in) or return;
333 *$gv{PACKAGE} . '::' . *$gv{NAME};
340 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
341 my $subref = defined $1 ? \&$sub : \&$ini;
342 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
343 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
344 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
345 $place = '???' unless defined $place;
346 $s = $sub unless defined $s;
347 print( (' ' x $off) . "&$s in $place\n" );
351 return undef unless %DB::sub;
352 my ($addr, $name, $loc);
353 while (($name, $loc) = each %DB::sub) {
355 $subs{"$addr"} = $name;
362 my ($package,@vars) = @_;
363 local(%address,$key,$val,$^W);
364 $package .= "::" unless $package =~ /::$/;
366 while ($package =~ /(\w+?::)/g){
367 *stab = $ {stab}{$1};
369 local $TotalStrings = 0;
371 local $CompleteTotal = 0;
372 while (($key,$val) = each(%stab)) {
373 return if $DB::signal;
374 next if @vars && !grep( matchvar($key, $_), @vars );
376 globUsage(\$val, $key)
377 if ($package ne 'dumpvar' or $key ne 'stab')
378 and ref(\$val) eq 'GLOB';
380 dumpglob(0,$key, $val);
384 print "String space: $TotalStrings bytes in $Strings strings.\n";
385 $CompleteTotal += $TotalStrings;
386 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
391 my $size = length($_[0]);
392 $TotalStrings += $size;
397 sub arrayUsage { # array ref, name
399 map {$size += scalarUsage($_)} @{$_[0]};
401 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
402 " (data: $size bytes)\n"
404 $CompleteTotal += $size;
408 sub hashUsage { # hash ref, name
409 my @keys = keys %{$_[0]};
410 my @values = values %{$_[0]};
411 my $keys = arrayUsage \@keys;
412 my $values = arrayUsage \@values;
414 my $total = $keys + $values;
415 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
416 " (keys: $keys; values: $values; total: $total bytes)\n"
421 sub globUsage { # glob ref, name
422 local *name = *{$_[0]};
424 $total += scalarUsage $name if defined $name;
425 $total += arrayUsage \@name, $_[1] if @name;
426 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
427 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
432 my ($package,@vars) = @_;
433 $package .= "::" unless $package =~ /::$/;
434 local *stab = *{"main::"};
435 while ($package =~ /(\w+?::)/g){
436 *stab = $ {stab}{$1};
438 local $TotalStrings = 0;
439 local $CompleteTotal = 0;
441 while (($key,$val) = each(%stab)) {
442 next if @vars && !grep($key eq $_,@vars);
443 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
445 print "String space: $TotalStrings.\n";
446 $CompleteTotal += $TotalStrings;
447 print "\nGrand total = $CompleteTotal bytes\n";