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 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
50 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
55 local($_,$noticks) = @_;
59 return 'undef' unless defined $_ or not $printUndef;
60 return $_ . "" if ref \$_ eq 'GLOB';
61 $_ = &{'overload::StrVal'}($_)
62 if $bareStringify and ref $_
63 and %overload:: and defined &{'overload::StrVal'};
65 if ($tick eq 'auto') {
66 if (/[\000-\011\013-\037\177]/) {
74 } elsif ($unctrl eq 'unctrl') {
76 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
78 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
80 } elsif ($unctrl eq 'quote') {
81 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
83 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
86 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
87 ($noticks || /^\d+(\.\d*)?\Z/)
93 my $tArrayDepth = $#{$_[0]} ;
94 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
95 unless $arrayDepth eq '' ;
97 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
98 if (!grep(ref $_, @{$_[0]})) {
99 $short = "0..$#{$_[0]} '" .
100 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
101 return $short if length $short <= $compactDump;
107 my $short = &stringify($_[0], ref $_[0]);
108 if ($veryCompact && ref $_[0]
109 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
110 my $end = "0..$#{$v} '" .
111 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
112 } elsif ($veryCompact && ref $_[0]
113 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
115 $short = $sp . "0..$#{$v} '" .
116 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
119 unwrap($_[0],$_[1],$_[2]) if ref $_[0];
124 return if $DB::signal;
126 local($s) = shift ; # extra no of spaces
127 local($m) = shift ; # maximum recursion depth
129 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
130 local($tHashDepth,$tArrayDepth) ;
135 # Check for reused addresses
138 $val = &{'overload::StrVal'}($v)
139 if %overload:: and defined &{'overload::StrVal'};
140 # Match type and address.
141 # Unblessed references will look like TYPE(0x...)
142 # Blessed references will look like Class=TYPE(0x...)
143 ($start_part, $val) = split /=/,$val;
144 $val = $start_part unless defined $val;
145 ($item_type, $address) =
146 $val =~ /([^\(]+) # Keep stuff that's
149 (0x[0-9a-f]+) # Save the address
150 \) # Skip close paren
151 $/x; # Should be at end now
153 if (!$dumpReused && defined $address) {
154 $address{$address}++ ;
155 if ( $address{$address} > 1 ) {
156 print "${sp}-> REUSED_ADDRESS\n" ;
160 } elsif (ref \$v eq 'GLOB') {
161 # This is a raw glob. Special handling for that.
162 $address = "$v" . ""; # To avoid a bug with globs
163 $address{$address}++ ;
164 if ( $address{$address} > 1 ) {
165 print "${sp}*DUMPED_GLOB*\n" ;
170 if (ref $v eq 'Regexp') {
171 # Reformat the regexp to look the standard way.
174 print "$sp-> qr/$re/\n";
178 if ( $item_type eq 'HASH' ) {
179 # Hash ref or hash-based object.
180 my @sortKeys = sort keys(%$v) ;
182 $tHashDepth = $#sortKeys ;
183 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
184 unless $hashDepth eq '' ;
185 $more = "....\n" if $tHashDepth < $#sortKeys ;
187 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
188 $#sortKeys = $tHashDepth ;
189 if ($compactDump && !grep(ref $_, values %{$v})) {
192 # Next row core dumps during require from DB on 5.000, even with map {"_"}
193 # map {&stringify($_) . " => " . &stringify($v->{$_})}
194 # @sortKeys) . "'$shortmore";
198 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
200 $short .= join ', ', @keys;
201 $short .= $shortmore;
202 (print "$short\n"), return if length $short <= $compactDump;
204 for $key (@sortKeys) {
205 return if $DB::signal;
206 $value = $ {$v}{$key} ;
207 print "$sp", &stringify($key), " => ";
208 DumpElem $value, $s, $m-1;
210 print "$sp empty hash\n" unless @sortKeys;
211 print "$sp$more" if defined $more ;
212 } elsif ( $item_type eq 'ARRAY' ) {
213 # Array ref or array-based object. Also: undef.
214 # See how big the array is.
215 $tArrayDepth = $#{$v} ;
217 # Bigger than the max?
218 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
219 if defined $arrayDepth && $arrayDepth ne '';
220 # Yep. Don't show it all.
221 $more = "....\n" if $tArrayDepth < $#{$v} ;
223 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
225 if ($compactDump && !grep(ref $_, @{$v})) {
227 $short = $sp . "0..$#{$v} " .
229 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} ($[..$tArrayDepth)
232 $short = $sp . "empty array";
234 (print "$short\n"), return if length $short <= $compactDump;
236 #if ($compactDump && $short = ShortArray($v)) {
240 for $num ($[ .. $tArrayDepth) {
241 return if $DB::signal;
243 if (exists $v->[$num]) {
244 if (defined $v->[$num]) {
245 DumpElem $v->[$num], $s, $m-1;
251 print "empty slot\n";
254 print "$sp empty array\n" unless @$v;
255 print "$sp$more" if defined $more ;
256 } elsif ( $item_type eq 'SCALAR' ) {
257 unless (defined $$v) {
258 print "$sp-> undef\n";
262 DumpElem $$v, $s, $m-1;
263 } elsif ( $item_type eq 'REF' ) {
265 return unless defined $$v;
266 unwrap($$v, $s+3, $m-1);
267 } elsif ( $item_type eq 'CODE' ) {
268 # Code object or reference.
271 } elsif ( $item_type eq 'GLOB' ) {
272 # Glob object or reference.
273 print "$sp-> ",&stringify($$v,1),"\n";
276 dumpglob($s, "{$$v}", $$v, 1, $m-1);
277 } elsif (defined ($fileno = fileno($v))) {
278 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
280 } elsif (ref \$v eq 'GLOB') {
283 dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
284 } elsif (defined ($fileno = fileno(\$v))) {
285 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
291 (my $var = $_[0]) =~ s/.//;
293 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
294 ($1 eq '!') ^ (eval { $var =~ /$2$3/ });
299 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
300 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
304 $compactDump = shift if @_;
305 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
310 $veryCompact = shift if @_;
311 compactDump(1) if !$compactDump and $veryCompact;
318 if ($in eq 'unctrl' or $in eq 'quote') {
321 print "Unknown value for `unctrl'.\n";
328 if (@_ and $_[0] eq '"') {
331 } elsif (@_ and $_[0] eq 'auto') {
334 } elsif (@_) { # Need to set
342 return if $DB::signal;
343 my ($off,$key, $val, $all, $m) = @_;
344 local(*entry) = $val;
346 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
347 print( (' ' x $off) . "\$", &unctrl($key), " = " );
348 DumpElem $entry, 3+$off, $m;
350 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
351 print( (' ' x $off) . "\@$key = (\n" );
352 unwrap(\@entry,3+$off,$m) ;
353 print( (' ' x $off) . ")\n" );
355 if ($key ne "main::" && $key ne "DB::" && %entry
356 && ($dumpPackages or $key !~ /::$/)
357 && ($key !~ /^_</ or $dumpDBFiles)
358 && !($package eq "dumpvar" and $key eq "stab")) {
359 print( (' ' x $off) . "\%$key = (\n" );
360 unwrap(\%entry,3+$off,$m) ;
361 print( (' ' x $off) . ")\n" );
363 if (defined ($fileno = fileno(*entry))) {
364 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
367 if (defined &entry) {
374 return if $DB::signal;
375 my ($key, $val, $m, @vars) = @_;
376 return if @vars && !grep( matchlex($key, $_), @vars );
378 my $off = 0; # It reads better this way
380 if (UNIVERSAL::isa($val,'ARRAY')) {
381 print( (' ' x $off) . "$key = (\n" );
382 unwrap($val,3+$off,$m) ;
383 print( (' ' x $off) . ")\n" );
385 elsif (UNIVERSAL::isa($val,'HASH')) {
386 print( (' ' x $off) . "$key = (\n" );
387 unwrap($val,3+$off,$m) ;
388 print( (' ' x $off) . ")\n" );
390 elsif (UNIVERSAL::isa($val,'IO')) {
391 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
393 # No lexical subroutines yet...
394 # elsif (UNIVERSAL::isa($val,'CODE')) {
395 # dumpsub($off, $$val);
398 print( (' ' x $off) . &unctrl($key), " = " );
399 DumpElem $$val, 3+$off, $m;
403 sub CvGV_name_or_bust {
405 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
406 $in = \&$in; # Hard reference...
407 eval {require Devel::Peek; 1} or return;
408 my $gv = Devel::Peek::CvGV($in) or return;
409 *$gv{PACKAGE} . '::' . *$gv{NAME};
416 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
417 my $subref = defined $1 ? \&$sub : \&$ini;
418 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
419 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
420 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
421 $place = '???' unless defined $place;
422 $s = $sub unless defined $s;
423 print( (' ' x $off) . "&$s in $place\n" );
427 return undef unless %DB::sub;
428 my ($addr, $name, $loc);
429 while (($name, $loc) = each %DB::sub) {
431 $subs{"$addr"} = $name;
438 my ($package,$m,@vars) = @_;
439 local(%address,$key,$val,$^W);
440 $package .= "::" unless $package =~ /::$/;
442 while ($package =~ /(\w+?::)/g){
443 *stab = $ {stab}{$1};
445 local $TotalStrings = 0;
447 local $CompleteTotal = 0;
448 while (($key,$val) = each(%stab)) {
449 return if $DB::signal;
450 next if @vars && !grep( matchvar($key, $_), @vars );
452 globUsage(\$val, $key)
453 if ($package ne 'dumpvar' or $key ne 'stab')
454 and ref(\$val) eq 'GLOB';
456 dumpglob(0,$key, $val, 0, $m);
460 print "String space: $TotalStrings bytes in $Strings strings.\n";
461 $CompleteTotal += $TotalStrings;
462 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
467 my $size = length($_[0]);
468 $TotalStrings += $size;
473 sub arrayUsage { # array ref, name
475 map {$size += scalarUsage($_)} @{$_[0]};
477 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
478 " (data: $size bytes)\n"
480 $CompleteTotal += $size;
484 sub hashUsage { # hash ref, name
485 my @keys = keys %{$_[0]};
486 my @values = values %{$_[0]};
487 my $keys = arrayUsage \@keys;
488 my $values = arrayUsage \@values;
490 my $total = $keys + $values;
491 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
492 " (keys: $keys; values: $values; total: $total bytes)\n"
497 sub globUsage { # glob ref, name
498 local *name = *{$_[0]};
500 $total += scalarUsage $name if defined $name;
501 $total += arrayUsage \@name, $_[1] if @name;
502 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
503 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
508 my ($package,@vars) = @_;
509 $package .= "::" unless $package =~ /::$/;
510 local *stab = *{"main::"};
511 while ($package =~ /(\w+?::)/g){
512 *stab = $ {stab}{$1};
514 local $TotalStrings = 0;
515 local $CompleteTotal = 0;
517 while (($key,$val) = each(%stab)) {
518 next if @vars && !grep($key eq $_,@vars);
519 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
521 print "String space: $TotalStrings.\n";
522 $CompleteTotal += $TotalStrings;
523 print "\nGrand total = $CompleteTotal bytes\n";