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;
48 local($_,$noticks) = @_;
52 return 'undef' unless defined $_ or not $printUndef;
53 return $_ . "" if ref \$_ eq 'GLOB';
54 $_ = &{'overload::StrVal'}($_)
55 if $bareStringify and ref $_
56 and %overload:: and defined &{'overload::StrVal'};
58 if ($tick eq 'auto') {
59 if (/[\000-\011\013-\037\177]/) {
67 } elsif ($unctrl eq 'unctrl') {
69 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
70 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
72 } elsif ($unctrl eq 'quote') {
73 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
75 s/([\000-\037\177])/'\\c'.chr(ord($1)^64)/eg;
77 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
78 ($noticks || /^\d+(\.\d*)?\Z/)
84 my $tArrayDepth = $#{$_[0]} ;
85 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
86 unless $arrayDepth eq '' ;
88 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
89 if (!grep(ref $_, @{$_[0]})) {
90 $short = "0..$#{$_[0]} '" .
91 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
92 return $short if length $short <= $compactDump;
98 my $short = &stringify($_[0], ref $_[0]);
99 if ($veryCompact && ref $_[0]
100 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
101 my $end = "0..$#{$v} '" .
102 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
103 } elsif ($veryCompact && ref $_[0]
104 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
106 $short = $sp . "0..$#{$v} '" .
107 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
115 return if $DB::signal;
117 local($s) = shift ; # extra no of spaces
118 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
119 local($tHashDepth,$tArrayDepth) ;
124 # Check for reused addresses
127 $val = &{'overload::StrVal'}($v)
128 if %overload:: and defined &{'overload::StrVal'};
129 ($address) = $val =~ /(0x[0-9a-f]+)\)$/ ;
130 if (!$dumpReused && defined $address) {
131 $address{$address}++ ;
132 if ( $address{$address} > 1 ) {
133 print "${sp}-> REUSED_ADDRESS\n" ;
137 } elsif (ref \$v eq 'GLOB') {
138 $address = "$v" . ""; # To avoid a bug with globs
139 $address{$address}++ ;
140 if ( $address{$address} > 1 ) {
141 print "${sp}*DUMPED_GLOB*\n" ;
146 if (ref $v eq 'Regexp') {
149 print "$sp-> qr/$re/\n";
153 if ( UNIVERSAL::isa($v, 'HASH') ) {
154 @sortKeys = sort keys(%$v) ;
156 $tHashDepth = $#sortKeys ;
157 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
158 unless $hashDepth eq '' ;
159 $more = "....\n" if $tHashDepth < $#sortKeys ;
161 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
162 $#sortKeys = $tHashDepth ;
163 if ($compactDump && !grep(ref $_, values %{$v})) {
166 # Next row core dumps during require from DB on 5.000, even with map {"_"}
167 # map {&stringify($_) . " => " . &stringify($v->{$_})}
168 # @sortKeys) . "'$shortmore";
172 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
174 $short .= join ', ', @keys;
175 $short .= $shortmore;
176 (print "$short\n"), return if length $short <= $compactDump;
178 for $key (@sortKeys) {
179 return if $DB::signal;
180 $value = $ {$v}{$key} ;
181 print "$sp", &stringify($key), " => ";
184 print "$sp empty hash\n" unless @sortKeys;
185 print "$sp$more" if defined $more ;
186 } elsif ( UNIVERSAL::isa($v, 'ARRAY') ) {
187 $tArrayDepth = $#{$v} ;
189 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
190 unless $arrayDepth eq '' ;
191 $more = "....\n" if $tArrayDepth < $#{$v} ;
193 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
194 if ($compactDump && !grep(ref $_, @{$v})) {
196 $short = $sp . "0..$#{$v} " .
198 map {stringify $_} @{$v}[0..$tArrayDepth])
201 $short = $sp . "empty array";
203 (print "$short\n"), return if length $short <= $compactDump;
205 #if ($compactDump && $short = ShortArray($v)) {
209 for $num ($[ .. $tArrayDepth) {
210 return if $DB::signal;
212 DumpElem $v->[$num], $s;
214 print "$sp empty array\n" unless @$v;
215 print "$sp$more" if defined $more ;
216 } elsif ( UNIVERSAL::isa($v, 'SCALAR') or ref $v eq 'REF' ) {
219 } elsif ( UNIVERSAL::isa($v, 'CODE') ) {
222 } elsif ( UNIVERSAL::isa($v, 'GLOB') ) {
223 print "$sp-> ",&stringify($$v,1),"\n";
226 dumpglob($s, "{$$v}", $$v, 1);
227 } elsif (defined ($fileno = fileno($v))) {
228 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
230 } elsif (ref \$v eq 'GLOB') {
232 dumpglob($s, "{$v}", $v, 1) if $globPrint;
233 } elsif (defined ($fileno = fileno(\$v))) {
234 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
241 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
242 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
246 $compactDump = shift if @_;
247 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
252 $veryCompact = shift if @_;
253 compactDump(1) if !$compactDump and $veryCompact;
260 if ($in eq 'unctrl' or $in eq 'quote') {
263 print "Unknown value for `unctrl'.\n";
270 if (@_ and $_[0] eq '"') {
273 } elsif (@_ and $_[0] eq 'auto') {
276 } elsif (@_) { # Need to set
284 return if $DB::signal;
285 my ($off,$key, $val, $all) = @_;
286 local(*entry) = $val;
288 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
289 print( (' ' x $off) . "\$", &unctrl($key), " = " );
290 DumpElem $entry, 3+$off;
292 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
293 print( (' ' x $off) . "\@$key = (\n" );
294 unwrap(\@entry,3+$off) ;
295 print( (' ' x $off) . ")\n" );
297 if ($key ne "main::" && $key ne "DB::" && %entry
298 && ($dumpPackages or $key !~ /::$/)
299 && ($key !~ /^_</ or $dumpDBFiles)
300 && !($package eq "dumpvar" and $key eq "stab")) {
301 print( (' ' x $off) . "\%$key = (\n" );
302 unwrap(\%entry,3+$off) ;
303 print( (' ' x $off) . ")\n" );
305 if (defined ($fileno = fileno(*entry))) {
306 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
309 if (defined &entry) {
315 sub CvGV_name_or_bust {
317 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
318 $in = \&$in; # Hard reference...
319 eval {require Devel::Peek; 1} or return;
320 my $gv = Devel::Peek::CvGV($in) or return;
321 *$gv{PACKAGE} . '::' . *$gv{NAME};
328 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
329 my $subref = defined $1 ? \&$sub : \&$ini;
330 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
331 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
332 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
333 $place = '???' unless defined $place;
334 $s = $sub unless defined $s;
335 print( (' ' x $off) . "&$s in $place\n" );
339 return undef unless %DB::sub;
340 my ($addr, $name, $loc);
341 while (($name, $loc) = each %DB::sub) {
343 $subs{"$addr"} = $name;
350 my ($package,@vars) = @_;
351 local(%address,$key,$val,$^W);
352 $package .= "::" unless $package =~ /::$/;
354 while ($package =~ /(\w+?::)/g){
355 *stab = $ {stab}{$1};
357 local $TotalStrings = 0;
359 local $CompleteTotal = 0;
360 while (($key,$val) = each(%stab)) {
361 return if $DB::signal;
362 next if @vars && !grep( matchvar($key, $_), @vars );
364 globUsage(\$val, $key) unless $package eq 'dumpvar' and $key eq 'stab';
366 dumpglob(0,$key, $val);
370 print "String space: $TotalStrings bytes in $Strings strings.\n";
371 $CompleteTotal += $TotalStrings;
372 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
377 my $size = length($_[0]);
378 $TotalStrings += $size;
383 sub arrayUsage { # array ref, name
385 map {$size += scalarUsage($_)} @{$_[0]};
387 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
388 " (data: $size bytes)\n"
390 $CompleteTotal += $size;
394 sub hashUsage { # hash ref, name
395 my @keys = keys %{$_[0]};
396 my @values = values %{$_[0]};
397 my $keys = arrayUsage \@keys;
398 my $values = arrayUsage \@values;
400 my $total = $keys + $values;
401 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
402 " (keys: $keys; values: $values; total: $total bytes)\n"
407 sub globUsage { # glob ref, name
408 local *name = *{$_[0]};
410 $total += scalarUsage $name if defined $name;
411 $total += arrayUsage \@name, $_[1] if @name;
412 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
413 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
418 my ($package,@vars) = @_;
419 $package .= "::" unless $package =~ /::$/;
420 local *stab = *{"main::"};
421 while ($package =~ /(\w+?::)/g){
422 *stab = $ {stab}{$1};
424 local $TotalStrings = 0;
425 local $CompleteTotal = 0;
427 while (($key,$val) = each(%stab)) {
428 next if @vars && !grep($key eq $_,@vars);
429 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
431 print "String space: $TotalStrings.\n";
432 $CompleteTotal += $TotalStrings;
433 print "\nGrand total = $CompleteTotal bytes\n";