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 push @_, -1 if @_ == 1;
34 dumpvar::unwrap($_[0], 0, $_[1]);
37 # This one is good for variable names:
43 return \$_ if ref \$_ eq "GLOB";
44 if (ord('A') == 193) { # EBCDIC.
45 # EBCDIC has no concept of "\cA" or "A" being related
46 # to each other by a linear/boolean mapping.
48 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
55 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
60 local($_,$noticks) = @_;
64 return 'undef' unless defined $_ or not $printUndef;
65 return $_ . "" if ref \$_ eq 'GLOB';
66 $_ = &{'overload::StrVal'}($_)
67 if $bareStringify and ref $_
68 and %overload:: and defined &{'overload::StrVal'};
70 if ($tick eq 'auto') {
71 if (ord('A') == 193) {
72 if (/[\000-\011]/ or /[\013-\024\31-\037\177]/) {
78 if (/[\000-\011\013-\037\177]/) {
87 } elsif ($unctrl eq 'unctrl') {
89 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
91 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
93 } elsif ($unctrl eq 'quote') {
94 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
96 if (ord('A') == 193) { # EBCDIC.
97 s/([\000-\037\177])/'\\c'.chr(193)/eg; # Unfinished.
99 s/([\000-\037\177])/'\\c'._escaped_ord($1)/eg;
103 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
104 ($noticks || /^\d+(\.\d*)?\Z/)
106 : $tick . $_ . $tick;
109 # Ensure a resulting \ is escaped to be \\
112 $chr = chr(ord($chr)^64);
113 $chr =~ s{\\}{\\\\}g;
118 my $tArrayDepth = $#{$_[0]} ;
119 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
120 unless $arrayDepth eq '' ;
122 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
123 if (!grep(ref $_, @{$_[0]})) {
124 $short = "0..$#{$_[0]} '" .
125 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
126 return $short if length $short <= $compactDump;
132 my $short = &stringify($_[0], ref $_[0]);
133 if ($veryCompact && ref $_[0]
134 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
135 my $end = "0..$#{$v} '" .
136 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
137 } elsif ($veryCompact && ref $_[0]
138 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
140 $short = $sp . "0..$#{$v} '" .
141 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
144 unwrap($_[0],$_[1],$_[2]) if ref $_[0];
149 return if $DB::signal;
151 local($s) = shift ; # extra no of spaces
152 local($m) = shift ; # maximum recursion depth
154 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
155 local($tHashDepth,$tArrayDepth) ;
160 # Check for reused addresses
163 $val = &{'overload::StrVal'}($v)
164 if %overload:: and defined &{'overload::StrVal'};
165 # Match type and address.
166 # Unblessed references will look like TYPE(0x...)
167 # Blessed references will look like Class=TYPE(0x...)
168 $val =~ s/^.*=//; # suppress the Class part, just keep TYPE(0x...)
169 ($item_type, $address) =
170 $val =~ /([^\(]+) # Keep stuff that's
173 (0x[0-9a-f]+) # Save the address
174 \) # Skip close paren
175 $/x; # Should be at end now
177 if (!$dumpReused && defined $address) {
178 $address{$address}++ ;
179 if ( $address{$address} > 1 ) {
180 print "${sp}-> REUSED_ADDRESS\n" ;
184 } elsif (ref \$v eq 'GLOB') {
185 # This is a raw glob. Special handling for that.
186 $address = "$v" . ""; # To avoid a bug with globs
187 $address{$address}++ ;
188 if ( $address{$address} > 1 ) {
189 print "${sp}*DUMPED_GLOB*\n" ;
194 if (ref $v eq 'Regexp') {
195 # Reformat the regexp to look the standard way.
198 print "$sp-> qr/$re/\n";
202 if ( $item_type eq 'HASH' ) {
203 # Hash ref or hash-based object.
204 my @sortKeys = sort keys(%$v) ;
206 $tHashDepth = $#sortKeys ;
207 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
208 unless $hashDepth eq '' ;
209 $more = "....\n" if $tHashDepth < $#sortKeys ;
211 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
212 $#sortKeys = $tHashDepth ;
213 if ($compactDump && !grep(ref $_, values %{$v})) {
216 # Next row core dumps during require from DB on 5.000, even with map {"_"}
217 # map {&stringify($_) . " => " . &stringify($v->{$_})}
218 # @sortKeys) . "'$shortmore";
222 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
224 $short .= join ', ', @keys;
225 $short .= $shortmore;
226 (print "$short\n"), return if length $short <= $compactDump;
228 for $key (@sortKeys) {
229 return if $DB::signal;
230 $value = $ {$v}{$key} ;
231 print "$sp", &stringify($key), " => ";
232 DumpElem $value, $s, $m-1;
234 print "$sp empty hash\n" unless @sortKeys;
235 print "$sp$more" if defined $more ;
236 } elsif ( $item_type eq 'ARRAY' ) {
237 # Array ref or array-based object. Also: undef.
238 # See how big the array is.
239 $tArrayDepth = $#{$v} ;
241 # Bigger than the max?
242 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
243 if defined $arrayDepth && $arrayDepth ne '';
244 # Yep. Don't show it all.
245 $more = "....\n" if $tArrayDepth < $#{$v} ;
247 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
249 if ($compactDump && !grep(ref $_, @{$v})) {
251 $short = $sp . "0..$#{$v} " .
253 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} (0..$tArrayDepth)
256 $short = $sp . "empty array";
258 (print "$short\n"), return if length $short <= $compactDump;
260 #if ($compactDump && $short = ShortArray($v)) {
264 for $num (0 .. $tArrayDepth) {
265 return if $DB::signal;
267 if (exists $v->[$num]) {
268 if (defined $v->[$num]) {
269 DumpElem $v->[$num], $s, $m-1;
275 print "empty slot\n";
278 print "$sp empty array\n" unless @$v;
279 print "$sp$more" if defined $more ;
280 } elsif ( $item_type eq 'SCALAR' ) {
281 unless (defined $$v) {
282 print "$sp-> undef\n";
286 DumpElem $$v, $s, $m-1;
287 } elsif ( $item_type eq 'REF' ) {
289 return unless defined $$v;
290 unwrap($$v, $s+3, $m-1);
291 } elsif ( $item_type eq 'CODE' ) {
292 # Code object or reference.
295 } elsif ( $item_type eq 'GLOB' ) {
296 # Glob object or reference.
297 print "$sp-> ",&stringify($$v,1),"\n";
300 dumpglob($s, "{$$v}", $$v, 1, $m-1);
301 } elsif (defined ($fileno = eval {fileno($v)})) {
302 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
304 } elsif (ref \$v eq 'GLOB') {
307 dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
308 } elsif (defined ($fileno = eval {fileno(\$v)})) {
309 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
315 (my $var = $_[0]) =~ s/.//;
317 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
318 ($1 eq '!') ^ (eval { $var =~ /$2$3/ });
323 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
324 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
328 $compactDump = shift if @_;
329 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
334 $veryCompact = shift if @_;
335 compactDump(1) if !$compactDump and $veryCompact;
342 if ($in eq 'unctrl' or $in eq 'quote') {
345 print "Unknown value for `unctrl'.\n";
352 if (@_ and $_[0] eq '"') {
355 } elsif (@_ and $_[0] eq 'auto') {
358 } elsif (@_) { # Need to set
366 return if $DB::signal;
367 my ($off,$key, $val, $all, $m) = @_;
368 local(*entry) = $val;
370 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
371 print( (' ' x $off) . "\$", &unctrl($key), " = " );
372 DumpElem $entry, 3+$off, $m;
374 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
375 print( (' ' x $off) . "\@$key = (\n" );
376 unwrap(\@entry,3+$off,$m) ;
377 print( (' ' x $off) . ")\n" );
379 if ($key ne "main::" && $key ne "DB::" && %entry
380 && ($dumpPackages or $key !~ /::$/)
381 && ($key !~ /^_</ or $dumpDBFiles)
382 && !($package eq "dumpvar" and $key eq "stab")) {
383 print( (' ' x $off) . "\%$key = (\n" );
384 unwrap(\%entry,3+$off,$m) ;
385 print( (' ' x $off) . ")\n" );
387 if (defined ($fileno = eval{fileno(*entry)})) {
388 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
391 if (defined &entry) {
398 return if $DB::signal;
399 my ($key, $val, $m, @vars) = @_;
400 return if @vars && !grep( matchlex($key, $_), @vars );
402 my $off = 0; # It reads better this way
404 if (UNIVERSAL::isa($val,'ARRAY')) {
405 print( (' ' x $off) . "$key = (\n" );
406 unwrap($val,3+$off,$m) ;
407 print( (' ' x $off) . ")\n" );
409 elsif (UNIVERSAL::isa($val,'HASH')) {
410 print( (' ' x $off) . "$key = (\n" );
411 unwrap($val,3+$off,$m) ;
412 print( (' ' x $off) . ")\n" );
414 elsif (UNIVERSAL::isa($val,'IO')) {
415 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
417 # No lexical subroutines yet...
418 # elsif (UNIVERSAL::isa($val,'CODE')) {
419 # dumpsub($off, $$val);
422 print( (' ' x $off) . &unctrl($key), " = " );
423 DumpElem $$val, 3+$off, $m;
427 sub CvGV_name_or_bust {
429 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
430 $in = \&$in; # Hard reference...
431 eval {require Devel::Peek; 1} or return;
432 my $gv = Devel::Peek::CvGV($in) or return;
433 *$gv{PACKAGE} . '::' . *$gv{NAME};
440 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
441 my $subref = defined $1 ? \&$sub : \&$ini;
442 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
443 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
444 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
445 $place = '???' unless defined $place;
446 $s = $sub unless defined $s;
447 print( (' ' x $off) . "&$s in $place\n" );
451 return undef unless %DB::sub;
452 my ($addr, $name, $loc);
453 while (($name, $loc) = each %DB::sub) {
455 $subs{"$addr"} = $name;
462 my ($package,$m,@vars) = @_;
463 local(%address,$key,$val,$^W);
464 $package .= "::" unless $package =~ /::$/;
466 while ($package =~ /(\w+?::)/g){
467 *stab = $ {stab}{$1};
469 local $TotalStrings = 0;
471 local $CompleteTotal = 0;
472 while (($key,$val) = each(%stab)) {
473 return if $DB::signal;
474 next if @vars && !grep( matchvar($key, $_), @vars );
476 globUsage(\$val, $key)
477 if ($package ne 'dumpvar' or $key ne 'stab')
478 and ref(\$val) eq 'GLOB';
480 dumpglob(0,$key, $val, 0, $m);
484 print "String space: $TotalStrings bytes in $Strings strings.\n";
485 $CompleteTotal += $TotalStrings;
486 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
491 my $size = length($_[0]);
492 $TotalStrings += $size;
497 sub arrayUsage { # array ref, name
499 map {$size += scalarUsage($_)} @{$_[0]};
501 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
502 " (data: $size bytes)\n"
504 $CompleteTotal += $size;
508 sub hashUsage { # hash ref, name
509 my @keys = keys %{$_[0]};
510 my @values = values %{$_[0]};
511 my $keys = arrayUsage \@keys;
512 my $values = arrayUsage \@values;
514 my $total = $keys + $values;
515 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
516 " (keys: $keys; values: $values; total: $total bytes)\n"
521 sub globUsage { # glob ref, name
522 local *name = *{$_[0]};
524 $total += scalarUsage $name if defined $name;
525 $total += arrayUsage \@name, $_[1] if @name;
526 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
527 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
532 my ($package,@vars) = @_;
533 $package .= "::" unless $package =~ /::$/;
534 local *stab = *{"main::"};
535 while ($package =~ /(\w+?::)/g){
536 *stab = $ {stab}{$1};
538 local $TotalStrings = 0;
539 local $CompleteTotal = 0;
541 while (($key,$val) = each(%stab)) {
542 next if @vars && !grep($key eq $_,@vars);
543 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
545 print "String space: $TotalStrings.\n";
546 $CompleteTotal += $TotalStrings;
547 print "\nGrand total = $CompleteTotal bytes\n";