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;
28 (print "undef\n"), return unless defined $_[0];
29 (print &stringify($_[0]), "\n"), return unless ref $_[0];
30 dumpvar::unwrap($_[0],0);
33 # This one is good for variable names:
39 return \$_ if ref \$_ eq "GLOB";
40 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
45 local($_,$noticks) = @_;
49 return 'undef' unless defined $_ or not $printUndef;
50 return $_ . "" if ref \$_ eq 'GLOB';
51 if ($tick eq 'auto') {
52 if (/[\000-\011\013-\037\177]/) {
60 } elsif ($unctrl eq 'unctrl') {
62 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
63 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
65 } elsif ($unctrl eq 'quote') {
66 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
68 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
70 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
71 ($noticks || /^\d+(\.\d*)?\Z/)
77 my $tArrayDepth = $#{$_[0]} ;
78 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
79 unless $arrayDepth eq '' ;
81 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
82 if (!grep(ref $_, @{$_[0]})) {
83 $short = "0..$#{$_[0]} '" .
84 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
85 return $short if length $short <= $compactDump;
91 my $short = &stringify($_[0], ref $_[0]);
92 if ($veryCompact && ref $_[0]
93 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
94 my $end = "0..$#{$v} '" .
95 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
96 } elsif ($veryCompact && ref $_[0]
97 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
99 $short = $sp . "0..$#{$v} '" .
100 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
108 return if $DB::signal;
110 local($s) = shift ; # extra no of spaces
111 local(%v,@v,$sp,$value,$key,$type,@sortKeys,$more,$shortmore,$short) ;
112 local($tHashDepth,$tArrayDepth) ;
117 # Check for reused addresses
119 ($address) = $v =~ /(0x[0-9a-f]+)/ ;
120 if (defined $address) {
121 ($type) = $v =~ /=(.*?)\(/ ;
122 $address{$address}++ ;
123 if ( $address{$address} > 1 ) {
124 print "${sp}-> REUSED_ADDRESS\n" ;
128 } elsif (ref \$v eq 'GLOB') {
129 $address = "$v" . ""; # To avoid a bug with globs
130 $address{$address}++ ;
131 if ( $address{$address} > 1 ) {
132 print "${sp}*DUMPED_GLOB*\n" ;
137 if ( ref $v eq 'HASH' or $type eq 'HASH') {
138 @sortKeys = sort keys(%$v) ;
140 $tHashDepth = $#sortKeys ;
141 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
142 unless $hashDepth eq '' ;
143 $more = "....\n" if $tHashDepth < $#sortKeys ;
145 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
146 $#sortKeys = $tHashDepth ;
147 if ($compactDump && !grep(ref $_, values %{$v})) {
150 # Next row core dumps during require from DB on 5.000, even with map {"_"}
151 # map {&stringify($_) . " => " . &stringify($v->{$_})}
152 # @sortKeys) . "'$shortmore";
156 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
158 $short .= join ', ', @keys;
159 $short .= $shortmore;
160 (print "$short\n"), return if length $short <= $compactDump;
162 for $key (@sortKeys) {
163 return if $DB::signal;
164 $value = $ {$v}{$key} ;
165 print "$sp", &stringify($key), " => ";
168 print "$sp empty hash\n" unless @sortKeys;
169 print "$sp$more" if defined $more ;
170 } elsif ( ref $v eq 'ARRAY' or $type eq 'ARRAY') {
171 $tArrayDepth = $#{$v} ;
173 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
174 unless $arrayDepth eq '' ;
175 $more = "....\n" if $tArrayDepth < $#{$v} ;
177 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
178 if ($compactDump && !grep(ref $_, @{$v})) {
180 $short = $sp . "0..$#{$v} " .
182 map {stringify $_} @{$v}[0..$tArrayDepth])
185 $short = $sp . "empty array";
187 (print "$short\n"), return if length $short <= $compactDump;
189 #if ($compactDump && $short = ShortArray($v)) {
193 for $num ($[ .. $tArrayDepth) {
194 return if $DB::signal;
196 DumpElem $v->[$num], $s;
198 print "$sp empty array\n" unless @$v;
199 print "$sp$more" if defined $more ;
200 } elsif ( ref $v eq 'SCALAR' or ref $v eq 'REF' or $type eq 'SCALAR' ) {
203 } elsif ( ref $v eq 'CODE' or $type eq 'CODE' ) {
206 } elsif (ref $v eq 'GLOB') {
207 print "$sp-> ",&stringify($$v,1),"\n";
210 dumpglob($s, "{$$v}", $$v, 1);
211 } elsif (defined ($fileno = fileno($v))) {
212 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
214 } elsif (ref \$v eq 'GLOB') {
216 dumpglob($s, "{$v}", $v, 1) if $globPrint;
217 } elsif (defined ($fileno = fileno(\$v))) {
218 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
225 ($_[1] =~ /^([!~])(.)/) and
226 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$'/});
230 $compactDump = shift if @_;
231 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
236 $veryCompact = shift if @_;
237 compactDump(1) if !$compactDump and $veryCompact;
244 if ($in eq 'unctrl' or $in eq 'quote') {
247 print "Unknown value for `unctrl'.\n";
254 if (@_ and $_[0] eq '"') {
257 } elsif (@_ and $_[0] eq 'auto') {
260 } elsif (@_) { # Need to set
268 return if $DB::signal;
269 my ($off,$key, $val, $all) = @_;
270 local(*entry) = $val;
272 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
273 print( (' ' x $off) . "\$", &unctrl($key), " = " );
274 DumpElem $entry, 3+$off;
276 if (($key !~ /^_</ or $dumpDBFiles) and defined @entry) {
277 print( (' ' x $off) . "\@$key = (\n" );
278 unwrap(\@entry,3+$off) ;
279 print( (' ' x $off) . ")\n" );
281 if ($key ne "main::" && $key ne "DB::" && defined %entry
282 && ($dumpPackages or $key !~ /::$/)
283 && ($key !~ /^_</ or $dumpDBFiles)
284 && !($package eq "dumpvar" and $key eq "stab")) {
285 print( (' ' x $off) . "\%$key = (\n" );
286 unwrap(\%entry,3+$off) ;
287 print( (' ' x $off) . ")\n" );
289 if (defined ($fileno = fileno(*entry))) {
290 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
293 if (defined &entry) {
301 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
303 my $place = $DB::sub{$sub} || (($sub = $subs{"$subref"}) && $DB::sub{$sub})
304 || ($subdump && ($sub = findsubs("$subref")) && $DB::sub{$sub});
305 $place = '???' unless defined $place;
306 print( (' ' x $off) . "&$sub in $place\n" );
310 return undef unless defined %DB::sub;
311 my ($addr, $name, $loc);
312 while (($name, $loc) = each %DB::sub) {
314 $subs{"$addr"} = $name;
321 my ($package,@vars) = @_;
322 local(%address,$key,$val);
323 $package .= "::" unless $package =~ /::$/;
325 while ($package =~ /(\w+?::)/g){
326 *stab = $ {stab}{$1};
328 local $TotalStrings = 0;
330 local $CompleteTotal = 0;
331 while (($key,$val) = each(%stab)) {
332 return if $DB::signal;
333 next if @vars && !grep( matchvar($key, $_), @vars );
335 globUsage(\$val, $key) unless $package eq 'dumpvar' and $key eq 'stab';
337 dumpglob(0,$key, $val);
341 print "String space: $TotalStrings bytes in $Strings strings.\n";
342 $CompleteTotal += $TotalStrings;
343 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
348 my $size = length($_[0]);
349 $TotalStrings += $size;
354 sub arrayUsage { # array ref, name
356 map {$size += scalarUsage($_)} @{$_[0]};
358 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
359 " (data: $size bytes)\n"
361 $CompleteTotal += $size;
365 sub hashUsage { # hash ref, name
366 my @keys = keys %{$_[0]};
367 my @values = values %{$_[0]};
368 my $keys = arrayUsage \@keys;
369 my $values = arrayUsage \@values;
371 my $total = $keys + $values;
372 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
373 " (keys: $keys; values: $values; total: $total bytes)\n"
378 sub globUsage { # glob ref, name
379 local *name = *{$_[0]};
381 $total += scalarUsage $name if defined $name;
382 $total += arrayUsage \@name, $_[1] if defined @name;
383 $total += hashUsage \%name, $_[1] if defined %name and $_[1] ne "main::"
384 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
389 my ($package,@vars) = @_;
390 $package .= "::" unless $package =~ /::$/;
391 local *stab = *{"main::"};
392 while ($package =~ /(\w+?::)/g){
393 *stab = $ {stab}{$1};
395 local $TotalStrings = 0;
396 local $CompleteTotal = 0;
398 while (($key,$val) = each(%stab)) {
399 next if @vars && !grep($key eq $_,@vars);
400 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
402 print "String space: $TotalStrings.\n";
403 $CompleteTotal += $TotalStrings;
404 print "\nGrand total = $CompleteTotal bytes\n";