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;
29 (print "undef\n"), return unless defined $_[0];
30 (print &stringify($_[0]), "\n"), return unless ref $_[0];
31 dumpvar::unwrap($_[0],0);
34 # This one is good for variable names:
40 return \$_ if ref \$_ eq "GLOB";
41 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
46 local($_,$noticks) = @_;
50 return 'undef' unless defined $_ or not $printUndef;
51 return $_ . "" if ref \$_ eq 'GLOB';
52 if ($tick eq 'auto') {
53 if (/[\000-\011\013-\037\177]/) {
61 } elsif ($unctrl eq 'unctrl') {
63 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
64 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
66 } elsif ($unctrl eq 'quote') {
67 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
69 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
71 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
72 ($noticks || /^\d+(\.\d*)?\Z/)
78 my $tArrayDepth = $#{$_[0]} ;
79 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
80 unless $arrayDepth eq '' ;
82 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
83 if (!grep(ref $_, @{$_[0]})) {
84 $short = "0..$#{$_[0]} '" .
85 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
86 return $short if length $short <= $compactDump;
92 my $short = &stringify($_[0], ref $_[0]);
93 if ($veryCompact && ref $_[0]
94 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
95 my $end = "0..$#{$v} '" .
96 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
97 } elsif ($veryCompact && ref $_[0]
98 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
100 $short = $sp . "0..$#{$v} '" .
101 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
109 return if $DB::signal;
111 local($s) = shift ; # extra no of spaces
112 local(%v,@v,$sp,$value,$key,$type,@sortKeys,$more,$shortmore,$short) ;
113 local($tHashDepth,$tArrayDepth) ;
118 # Check for reused addresses
120 ($address) = $v =~ /(0x[0-9a-f]+)/ ;
121 if (defined $address) {
122 ($type) = $v =~ /=(.*?)\(/ ;
123 $address{$address}++ ;
124 if ( $address{$address} > 1 ) {
125 print "${sp}-> REUSED_ADDRESS\n" ;
129 } elsif (ref \$v eq 'GLOB') {
130 $address = "$v" . ""; # To avoid a bug with globs
131 $address{$address}++ ;
132 if ( $address{$address} > 1 ) {
133 print "${sp}*DUMPED_GLOB*\n" ;
138 if ( ref $v eq 'HASH' or $type eq 'HASH') {
139 @sortKeys = sort keys(%$v) ;
141 $tHashDepth = $#sortKeys ;
142 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
143 unless $hashDepth eq '' ;
144 $more = "....\n" if $tHashDepth < $#sortKeys ;
146 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
147 $#sortKeys = $tHashDepth ;
148 if ($compactDump && !grep(ref $_, values %{$v})) {
151 # Next row core dumps during require from DB on 5.000, even with map {"_"}
152 # map {&stringify($_) . " => " . &stringify($v->{$_})}
153 # @sortKeys) . "'$shortmore";
157 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
159 $short .= join ', ', @keys;
160 $short .= $shortmore;
161 (print "$short\n"), return if length $short <= $compactDump;
163 for $key (@sortKeys) {
164 return if $DB::signal;
165 $value = $ {$v}{$key} ;
166 print "$sp", &stringify($key), " => ";
169 print "$sp empty hash\n" unless @sortKeys;
170 print "$sp$more" if defined $more ;
171 } elsif ( ref $v eq 'ARRAY' or $type eq 'ARRAY') {
172 $tArrayDepth = $#{$v} ;
174 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
175 unless $arrayDepth eq '' ;
176 $more = "....\n" if $tArrayDepth < $#{$v} ;
178 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
179 if ($compactDump && !grep(ref $_, @{$v})) {
181 $short = $sp . "0..$#{$v} " .
183 map {stringify $_} @{$v}[0..$tArrayDepth])
186 $short = $sp . "empty array";
188 (print "$short\n"), return if length $short <= $compactDump;
190 #if ($compactDump && $short = ShortArray($v)) {
194 for $num ($[ .. $tArrayDepth) {
195 return if $DB::signal;
197 DumpElem $v->[$num], $s;
199 print "$sp empty array\n" unless @$v;
200 print "$sp$more" if defined $more ;
201 } elsif ( ref $v eq 'SCALAR' or ref $v eq 'REF' or $type eq 'SCALAR' ) {
204 } elsif ( ref $v eq 'CODE' or $type eq 'CODE' ) {
207 } elsif (ref $v eq 'GLOB') {
208 print "$sp-> ",&stringify($$v,1),"\n";
211 dumpglob($s, "{$$v}", $$v, 1);
212 } elsif (defined ($fileno = fileno($v))) {
213 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
215 } elsif (ref \$v eq 'GLOB') {
217 dumpglob($s, "{$v}", $v, 1) if $globPrint;
218 } elsif (defined ($fileno = fileno(\$v))) {
219 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
226 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
227 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
231 $compactDump = shift if @_;
232 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
237 $veryCompact = shift if @_;
238 compactDump(1) if !$compactDump and $veryCompact;
245 if ($in eq 'unctrl' or $in eq 'quote') {
248 print "Unknown value for `unctrl'.\n";
255 if (@_ and $_[0] eq '"') {
258 } elsif (@_ and $_[0] eq 'auto') {
261 } elsif (@_) { # Need to set
269 return if $DB::signal;
270 my ($off,$key, $val, $all) = @_;
271 local(*entry) = $val;
273 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
274 print( (' ' x $off) . "\$", &unctrl($key), " = " );
275 DumpElem $entry, 3+$off;
277 if (($key !~ /^_</ or $dumpDBFiles) and defined @entry) {
278 print( (' ' x $off) . "\@$key = (\n" );
279 unwrap(\@entry,3+$off) ;
280 print( (' ' x $off) . ")\n" );
282 if ($key ne "main::" && $key ne "DB::" && defined %entry
283 && ($dumpPackages or $key !~ /::$/)
284 && ($key !~ /^_</ or $dumpDBFiles)
285 && !($package eq "dumpvar" and $key eq "stab")) {
286 print( (' ' x $off) . "\%$key = (\n" );
287 unwrap(\%entry,3+$off) ;
288 print( (' ' x $off) . ")\n" );
290 if (defined ($fileno = fileno(*entry))) {
291 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
294 if (defined &entry) {
302 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
304 my $place = $DB::sub{$sub} || (($sub = $subs{"$subref"}) && $DB::sub{$sub})
305 || ($subdump && ($sub = findsubs("$subref")) && $DB::sub{$sub});
306 $place = '???' unless defined $place;
307 print( (' ' x $off) . "&$sub in $place\n" );
311 return undef unless defined %DB::sub;
312 my ($addr, $name, $loc);
313 while (($name, $loc) = each %DB::sub) {
315 $subs{"$addr"} = $name;
322 my ($package,@vars) = @_;
323 local(%address,$key,$val,$^W);
324 $package .= "::" unless $package =~ /::$/;
326 while ($package =~ /(\w+?::)/g){
327 *stab = $ {stab}{$1};
329 local $TotalStrings = 0;
331 local $CompleteTotal = 0;
332 while (($key,$val) = each(%stab)) {
333 return if $DB::signal;
334 next if @vars && !grep( matchvar($key, $_), @vars );
336 globUsage(\$val, $key) unless $package eq 'dumpvar' and $key eq 'stab';
338 dumpglob(0,$key, $val);
342 print "String space: $TotalStrings bytes in $Strings strings.\n";
343 $CompleteTotal += $TotalStrings;
344 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
349 my $size = length($_[0]);
350 $TotalStrings += $size;
355 sub arrayUsage { # array ref, name
357 map {$size += scalarUsage($_)} @{$_[0]};
359 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
360 " (data: $size bytes)\n"
362 $CompleteTotal += $size;
366 sub hashUsage { # hash ref, name
367 my @keys = keys %{$_[0]};
368 my @values = values %{$_[0]};
369 my $keys = arrayUsage \@keys;
370 my $values = arrayUsage \@values;
372 my $total = $keys + $values;
373 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
374 " (keys: $keys; values: $values; total: $total bytes)\n"
379 sub globUsage { # glob ref, name
380 local *name = *{$_[0]};
382 $total += scalarUsage $name if defined $name;
383 $total += arrayUsage \@name, $_[1] if defined @name;
384 $total += hashUsage \%name, $_[1] if defined %name and $_[1] ne "main::"
385 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
390 my ($package,@vars) = @_;
391 $package .= "::" unless $package =~ /::$/;
392 local *stab = *{"main::"};
393 while ($package =~ /(\w+?::)/g){
394 *stab = $ {stab}{$1};
396 local $TotalStrings = 0;
397 local $CompleteTotal = 0;
399 while (($key,$val) = each(%stab)) {
400 next if @vars && !grep($key eq $_,@vars);
401 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
403 print "String space: $TotalStrings.\n";
404 $CompleteTotal += $TotalStrings;
405 print "\nGrand total = $CompleteTotal bytes\n";