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:
40 for (my($dummy) = shift) {
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;
56 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
61 (my $__, local $noticks) = @_;
66 return 'undef' unless defined $_ or not $printUndef;
67 return $_ . "" if ref \$_ eq 'GLOB';
68 $_ = &{'overload::StrVal'}($_)
69 if $bareStringify and ref $_
70 and %overload:: and defined &{'overload::StrVal'};
72 if ($tick eq 'auto') {
73 if (ord('A') == 193) {
74 if (/[\000-\011]/ or /[\013-\024\31-\037\177]/) {
80 if (/[\000-\011\013-\037\177]/) {
89 } elsif ($unctrl eq 'unctrl') {
91 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
93 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
95 } elsif ($unctrl eq 'quote') {
96 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
98 if (ord('A') == 193) { # EBCDIC.
99 s/([\000-\037\177])/'\\c'.chr(193)/eg; # Unfinished.
101 s/([\000-\037\177])/'\\c'._escaped_ord($1)/eg;
105 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
106 return ($noticks || /^\d+(\.\d*)?\Z/)
108 : $tick . $_ . $tick;
112 # Ensure a resulting \ is escaped to be \\
115 $chr = chr(ord($chr)^64);
116 $chr =~ s{\\}{\\\\}g;
121 my $tArrayDepth = $#{$_[0]} ;
122 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
123 unless $arrayDepth eq '' ;
125 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
126 if (!grep(ref $_, @{$_[0]})) {
127 $short = "0..$#{$_[0]} '" .
128 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
129 return $short if length $short <= $compactDump;
135 my $short = &stringify($_[0], ref $_[0]);
136 if ($veryCompact && ref $_[0]
137 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
138 my $end = "0..$#{$v} '" .
139 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
140 } elsif ($veryCompact && ref $_[0]
141 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
143 $short = $sp . "0..$#{$v} '" .
144 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
147 unwrap($_[0],$_[1],$_[2]) if ref $_[0];
152 return if $DB::signal;
154 local($s) = shift ; # extra no of spaces
155 local($m) = shift ; # maximum recursion depth
157 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
158 local($tHashDepth,$tArrayDepth) ;
163 # Check for reused addresses
166 $val = &{'overload::StrVal'}($v)
167 if %overload:: and defined &{'overload::StrVal'};
168 # Match type and address.
169 # Unblessed references will look like TYPE(0x...)
170 # Blessed references will look like Class=TYPE(0x...)
171 $val =~ s/^.*=//; # suppress the Class part, just keep TYPE(0x...)
172 ($item_type, $address) =
173 $val =~ /([^\(]+) # Keep stuff that's
176 (0x[0-9a-f]+) # Save the address
177 \) # Skip close paren
178 $/x; # Should be at end now
180 if (!$dumpReused && defined $address) {
181 $address{$address}++ ;
182 if ( $address{$address} > 1 ) {
183 print "${sp}-> REUSED_ADDRESS\n" ;
187 } elsif (ref \$v eq 'GLOB') {
188 # This is a raw glob. Special handling for that.
189 $address = "$v" . ""; # To avoid a bug with globs
190 $address{$address}++ ;
191 if ( $address{$address} > 1 ) {
192 print "${sp}*DUMPED_GLOB*\n" ;
197 if (ref $v eq 'Regexp') {
198 # Reformat the regexp to look the standard way.
201 print "$sp-> qr/$re/\n";
205 if ( $item_type eq 'HASH' ) {
206 # Hash ref or hash-based object.
207 my @sortKeys = sort keys(%$v) ;
209 $tHashDepth = $#sortKeys ;
210 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
211 unless $hashDepth eq '' ;
212 $more = "....\n" if $tHashDepth < $#sortKeys ;
214 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
215 $#sortKeys = $tHashDepth ;
216 if ($compactDump && !grep(ref $_, values %{$v})) {
219 # Next row core dumps during require from DB on 5.000, even with map {"_"}
220 # map {&stringify($_) . " => " . &stringify($v->{$_})}
221 # @sortKeys) . "'$shortmore";
225 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
227 $short .= join ', ', @keys;
228 $short .= $shortmore;
229 (print "$short\n"), return if length $short <= $compactDump;
231 for $key (@sortKeys) {
232 return if $DB::signal;
233 $value = $ {$v}{$key} ;
234 print "$sp", &stringify($key), " => ";
235 DumpElem $value, $s, $m-1;
237 print "$sp empty hash\n" unless @sortKeys;
238 print "$sp$more" if defined $more ;
239 } elsif ( $item_type eq 'ARRAY' ) {
240 # Array ref or array-based object. Also: undef.
241 # See how big the array is.
242 $tArrayDepth = $#{$v} ;
244 # Bigger than the max?
245 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
246 if defined $arrayDepth && $arrayDepth ne '';
247 # Yep. Don't show it all.
248 $more = "....\n" if $tArrayDepth < $#{$v} ;
250 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
252 if ($compactDump && !grep(ref $_, @{$v})) {
254 $short = $sp . "0..$#{$v} " .
256 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} (0..$tArrayDepth)
259 $short = $sp . "empty array";
261 (print "$short\n"), return if length $short <= $compactDump;
263 #if ($compactDump && $short = ShortArray($v)) {
267 for $num (0 .. $tArrayDepth) {
268 return if $DB::signal;
270 if (exists $v->[$num]) {
271 if (defined $v->[$num]) {
272 DumpElem $v->[$num], $s, $m-1;
278 print "empty slot\n";
281 print "$sp empty array\n" unless @$v;
282 print "$sp$more" if defined $more ;
283 } elsif ( $item_type eq 'SCALAR' ) {
284 unless (defined $$v) {
285 print "$sp-> undef\n";
289 DumpElem $$v, $s, $m-1;
290 } elsif ( $item_type eq 'REF' ) {
292 return unless defined $$v;
293 unwrap($$v, $s+3, $m-1);
294 } elsif ( $item_type eq 'CODE' ) {
295 # Code object or reference.
298 } elsif ( $item_type eq 'GLOB' ) {
299 # Glob object or reference.
300 print "$sp-> ",&stringify($$v,1),"\n";
303 dumpglob($s, "{$$v}", $$v, 1, $m-1);
304 } elsif (defined ($fileno = eval {fileno($v)})) {
305 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
307 } elsif (ref \$v eq 'GLOB') {
310 dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
311 } elsif (defined ($fileno = eval {fileno(\$v)})) {
312 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
318 (my $var = $_[0]) =~ s/.//;
320 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
321 ($1 eq '!') ^ (eval { $var =~ /$2$3/ });
326 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
327 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
331 $compactDump = shift if @_;
332 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
337 $veryCompact = shift if @_;
338 compactDump(1) if !$compactDump and $veryCompact;
345 if ($in eq 'unctrl' or $in eq 'quote') {
348 print "Unknown value for 'unctrl'.\n";
355 if (@_ and $_[0] eq '"') {
358 } elsif (@_ and $_[0] eq 'auto') {
361 } elsif (@_) { # Need to set
369 return if $DB::signal;
370 my ($off,$key, $val, $all, $m) = @_;
371 local(*entry) = $val;
373 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
374 print( (' ' x $off) . "\$", &unctrl($key), " = " );
375 DumpElem $entry, 3+$off, $m;
377 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
378 print( (' ' x $off) . "\@$key = (\n" );
379 unwrap(\@entry,3+$off,$m) ;
380 print( (' ' x $off) . ")\n" );
382 if ($key ne "main::" && $key ne "DB::" && %entry
383 && ($dumpPackages or $key !~ /::$/)
384 && ($key !~ /^_</ or $dumpDBFiles)
385 && !($package eq "dumpvar" and $key eq "stab")) {
386 print( (' ' x $off) . "\%$key = (\n" );
387 unwrap(\%entry,3+$off,$m) ;
388 print( (' ' x $off) . ")\n" );
390 if (defined ($fileno = eval{fileno(*entry)})) {
391 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
394 if (defined &entry) {
401 return if $DB::signal;
402 my ($key, $val, $m, @vars) = @_;
403 return if @vars && !grep( matchlex($key, $_), @vars );
405 my $off = 0; # It reads better this way
407 if (UNIVERSAL::isa($val,'ARRAY')) {
408 print( (' ' x $off) . "$key = (\n" );
409 unwrap($val,3+$off,$m) ;
410 print( (' ' x $off) . ")\n" );
412 elsif (UNIVERSAL::isa($val,'HASH')) {
413 print( (' ' x $off) . "$key = (\n" );
414 unwrap($val,3+$off,$m) ;
415 print( (' ' x $off) . ")\n" );
417 elsif (UNIVERSAL::isa($val,'IO')) {
418 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
420 # No lexical subroutines yet...
421 # elsif (UNIVERSAL::isa($val,'CODE')) {
422 # dumpsub($off, $$val);
425 print( (' ' x $off) . &unctrl($key), " = " );
426 DumpElem $$val, 3+$off, $m;
430 sub CvGV_name_or_bust {
432 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
433 $in = \&$in; # Hard reference...
434 eval {require Devel::Peek; 1} or return;
435 my $gv = Devel::Peek::CvGV($in) or return;
436 *$gv{PACKAGE} . '::' . *$gv{NAME};
443 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
444 my $subref = defined $1 ? \&$sub : \&$ini;
445 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
446 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
447 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
448 $place = '???' unless defined $place;
449 $s = $sub unless defined $s;
450 print( (' ' x $off) . "&$s in $place\n" );
454 return undef unless %DB::sub;
455 my ($addr, $name, $loc);
456 while (($name, $loc) = each %DB::sub) {
458 $subs{"$addr"} = $name;
465 my ($package,$m,@vars) = @_;
466 local(%address,$key,$val,$^W);
467 $package .= "::" unless $package =~ /::$/;
469 while ($package =~ /(\w+?::)/g){
470 *stab = $ {stab}{$1};
472 local $TotalStrings = 0;
474 local $CompleteTotal = 0;
475 while (($key,$val) = each(%stab)) {
476 return if $DB::signal;
477 next if @vars && !grep( matchvar($key, $_), @vars );
479 globUsage(\$val, $key)
480 if ($package ne 'dumpvar' or $key ne 'stab')
481 and ref(\$val) eq 'GLOB';
483 dumpglob(0,$key, $val, 0, $m);
487 print "String space: $TotalStrings bytes in $Strings strings.\n";
488 $CompleteTotal += $TotalStrings;
489 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
494 my $size = length($_[0]);
495 $TotalStrings += $size;
500 sub arrayUsage { # array ref, name
502 map {$size += scalarUsage($_)} @{$_[0]};
504 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
505 " (data: $size bytes)\n"
507 $CompleteTotal += $size;
511 sub hashUsage { # hash ref, name
512 my @keys = keys %{$_[0]};
513 my @values = values %{$_[0]};
514 my $keys = arrayUsage \@keys;
515 my $values = arrayUsage \@values;
517 my $total = $keys + $values;
518 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
519 " (keys: $keys; values: $values; total: $total bytes)\n"
524 sub globUsage { # glob ref, name
525 local *name = *{$_[0]};
527 $total += scalarUsage $name if defined $name;
528 $total += arrayUsage \@name, $_[1] if @name;
529 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
530 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
535 my ($package,@vars) = @_;
536 $package .= "::" unless $package =~ /::$/;
537 local *stab = *{"main::"};
538 while ($package =~ /(\w+?::)/g){
539 *stab = $ {stab}{$1};
541 local $TotalStrings = 0;
542 local $CompleteTotal = 0;
544 while (($key,$val) = each(%stab)) {
545 next if @vars && !grep($key eq $_,@vars);
546 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
548 print "String space: $TotalStrings.\n";
549 $CompleteTotal += $TotalStrings;
550 print "\nGrand total = $CompleteTotal bytes\n";