This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't depend on threads to do a watchdog when testing threads
[perl5.git] / lib / dumpvar.pl
CommitLineData
d338d6fe 1require 5.002; # For (defined ref)
2package dumpvar;
3
4# Needed for PrettyPrinter only:
5
6# require 5.001; # Well, it coredumps anyway undef DB in 5.000 (not now)
7
8# translate control chars to ^X - Randal Schwartz
9# Modifications to print types by Peter Gordon v1.0
10
11# Ilya Zakharevich -- patches after 5.001 (and some before ;-)
12
13# Won't dump symbol tables and contents of debugged files by default
14
15$winsize = 80 unless defined $winsize;
16
17
18# Defaults
19
20# $globPrint = 1;
21$printUndef = 1 unless defined $printUndef;
54d04a52 22$tick = "auto" unless defined $tick;
d338d6fe 23$unctrl = 'quote' unless defined $unctrl;
54d04a52 24$subdump = 1;
22fae026 25$dumpReused = 0 unless defined $dumpReused;
ee239bfe 26$bareStringify = 1 unless defined $bareStringify;
d338d6fe 27
28sub main::dumpValue {
29 local %address;
b2391ea8 30 local $^W=0;
d338d6fe 31 (print "undef\n"), return unless defined $_[0];
32 (print &stringify($_[0]), "\n"), return unless ref $_[0];
f338af47
IZ
33 push @_, -1 if @_ == 1;
34 dumpvar::unwrap($_[0], 0, $_[1]);
d338d6fe 35}
36
37# This one is good for variable names:
38
39sub unctrl {
40 local($_) = @_;
41 local($v) ;
42
43 return \$_ if ref \$_ eq "GLOB";
2f3efc97
JH
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.
47 } else {
48 s/([\001-\037\177])/'^'.pack('c',ord($1)^64)/eg;
49 }
d338d6fe 50 $_;
51}
52
f8543d02
A
53sub uniescape {
54 join("",
55 map { $_ > 255 ? sprintf("\\x{%04X}", $_) : chr($_) }
56 unpack("U*", $_[0]));
57}
58
d338d6fe 59sub stringify {
60 local($_,$noticks) = @_;
61 local($v) ;
54d04a52 62 my $tick = $tick;
d338d6fe 63
64 return 'undef' unless defined $_ or not $printUndef;
65 return $_ . "" if ref \$_ eq 'GLOB';
ee239bfe
IZ
66 $_ = &{'overload::StrVal'}($_)
67 if $bareStringify and ref $_
475342a6 68 and %overload:: and defined &{'overload::StrVal'};
ee239bfe 69
54d04a52 70 if ($tick eq 'auto') {
2f3efc97
JH
71 if (ord('A') == 193) {
72 if (/[\000-\011]/ or /[\013-\024\31-\037\177]/) {
73 $tick = '"';
74 } else {
75 $tick = "'";
76 }
77 } else {
78 if (/[\000-\011\013-\037\177]/) {
79 $tick = '"';
80 } else {
81 $tick = "'";
82 }
83 }
54d04a52 84 }
d338d6fe 85 if ($tick eq "'") {
86 s/([\'\\])/\\$1/g;
87 } elsif ($unctrl eq 'unctrl') {
88 s/([\"\\])/\\$1/g ;
89 s/([\000-\037\177])/'^'.pack('c',ord($1)^64)/eg;
f8543d02 90 # uniescape?
d338d6fe 91 s/([\200-\377])/'\\0x'.sprintf('%2X',ord($1))/eg
92 if $quoteHighBit;
93 } elsif ($unctrl eq 'quote') {
94 s/([\"\\\$\@])/\\$1/g if $tick eq '"';
95 s/\033/\\e/g;
2f3efc97
JH
96 if (ord('A') == 193) { # EBCDIC.
97 s/([\000-\037\177])/'\\c'.chr(193)/eg; # Unfinished.
98 } else {
99 s/([\000-\037\177])/'\\c'._escaped_ord($1)/eg;
100 }
d338d6fe 101 }
f8543d02 102 $_ = uniescape($_);
d338d6fe 103 s/([\200-\377])/'\\'.sprintf('%3o',ord($1))/eg if $quoteHighBit;
104 ($noticks || /^\d+(\.\d*)?\Z/)
105 ? $_
106 : $tick . $_ . $tick;
107}
108
bd6f3d36
MS
109# Ensure a resulting \ is escaped to be \\
110sub _escaped_ord {
111 my $chr = shift;
112 $chr = chr(ord($chr)^64);
113 $chr =~ s{\\}{\\\\}g;
114 return $chr;
115}
116
d338d6fe 117sub ShortArray {
118 my $tArrayDepth = $#{$_[0]} ;
119 $tArrayDepth = $#{$_[0]} < $arrayDepth-1 ? $#{$_[0]} : $arrayDepth-1
120 unless $arrayDepth eq '' ;
121 my $shortmore = "";
122 $shortmore = " ..." if $tArrayDepth < $#{$_[0]} ;
123 if (!grep(ref $_, @{$_[0]})) {
124 $short = "0..$#{$_[0]} '" .
125 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
126 return $short if length $short <= $compactDump;
127 }
128 undef;
129}
130
131sub DumpElem {
132 my $short = &stringify($_[0], ref $_[0]);
133 if ($veryCompact && ref $_[0]
134 && (ref $_[0] eq 'ARRAY' and !grep(ref $_, @{$_[0]}) )) {
135 my $end = "0..$#{$v} '" .
136 join("' '", @{$_[0]}[0..$tArrayDepth]) . "'$shortmore";
137 } elsif ($veryCompact && ref $_[0]
138 && (ref $_[0] eq 'HASH') and !grep(ref $_, values %{$_[0]})) {
139 my $end = 1;
140 $short = $sp . "0..$#{$v} '" .
141 join("' '", @{$v}[0..$tArrayDepth]) . "'$shortmore";
142 } else {
143 print "$short\n";
9bcb75ad 144 unwrap($_[0],$_[1],$_[2]) if ref $_[0];
d338d6fe 145 }
146}
147
148sub unwrap {
149 return if $DB::signal;
150 local($v) = shift ;
151 local($s) = shift ; # extra no of spaces
d03c2a1b
MJD
152 local($m) = shift ; # maximum recursion depth
153 return if $m == 0;
ee239bfe 154 local(%v,@v,$sp,$value,$key,@sortKeys,$more,$shortmore,$short) ;
d338d6fe 155 local($tHashDepth,$tArrayDepth) ;
156
157 $sp = " " x $s ;
158 $s += 3 ;
159
160 # Check for reused addresses
161 if (ref $v) {
ee239bfe
IZ
162 my $val = $v;
163 $val = &{'overload::StrVal'}($v)
475342a6 164 if %overload:: and defined &{'overload::StrVal'};
9bcb75ad
JM
165 # Match type and address.
166 # Unblessed references will look like TYPE(0x...)
167 # Blessed references will look like Class=TYPE(0x...)
6b1b4e01 168 $val =~ s/^.*=//; # suppress the Class part, just keep TYPE(0x...)
9bcb75ad
JM
169 ($item_type, $address) =
170 $val =~ /([^\(]+) # Keep stuff that's
171 # not an open paren
172 \( # Skip open paren
173 (0x[0-9a-f]+) # Save the address
174 \) # Skip close paren
175 $/x; # Should be at end now
176
22fae026 177 if (!$dumpReused && defined $address) {
d338d6fe 178 $address{$address}++ ;
179 if ( $address{$address} > 1 ) {
180 print "${sp}-> REUSED_ADDRESS\n" ;
181 return ;
182 }
183 }
184 } elsif (ref \$v eq 'GLOB') {
9bcb75ad 185 # This is a raw glob. Special handling for that.
d338d6fe 186 $address = "$v" . ""; # To avoid a bug with globs
187 $address{$address}++ ;
188 if ( $address{$address} > 1 ) {
189 print "${sp}*DUMPED_GLOB*\n" ;
190 return ;
191 }
192 }
193
7894fbab 194 if (ref $v eq 'Regexp') {
9bcb75ad 195 # Reformat the regexp to look the standard way.
7894fbab
GS
196 my $re = "$v";
197 $re =~ s,/,\\/,g;
198 print "$sp-> qr/$re/\n";
199 return;
200 }
201
9bcb75ad
JM
202 if ( $item_type eq 'HASH' ) {
203 # Hash ref or hash-based object.
204 my @sortKeys = sort keys(%$v) ;
d338d6fe 205 undef $more ;
206 $tHashDepth = $#sortKeys ;
207 $tHashDepth = $#sortKeys < $hashDepth-1 ? $#sortKeys : $hashDepth-1
208 unless $hashDepth eq '' ;
209 $more = "....\n" if $tHashDepth < $#sortKeys ;
210 $shortmore = "";
211 $shortmore = ", ..." if $tHashDepth < $#sortKeys ;
212 $#sortKeys = $tHashDepth ;
213 if ($compactDump && !grep(ref $_, values %{$v})) {
214 #$short = $sp .
215 # (join ', ',
216# Next row core dumps during require from DB on 5.000, even with map {"_"}
217 # map {&stringify($_) . " => " . &stringify($v->{$_})}
218 # @sortKeys) . "'$shortmore";
219 $short = $sp;
220 my @keys;
221 for (@sortKeys) {
222 push @keys, &stringify($_) . " => " . &stringify($v->{$_});
223 }
224 $short .= join ', ', @keys;
225 $short .= $shortmore;
226 (print "$short\n"), return if length $short <= $compactDump;
227 }
228 for $key (@sortKeys) {
229 return if $DB::signal;
230 $value = $ {$v}{$key} ;
231 print "$sp", &stringify($key), " => ";
d03c2a1b 232 DumpElem $value, $s, $m-1;
d338d6fe 233 }
234 print "$sp empty hash\n" unless @sortKeys;
235 print "$sp$more" if defined $more ;
9bcb75ad
JM
236 } elsif ( $item_type eq 'ARRAY' ) {
237 # Array ref or array-based object. Also: undef.
238 # See how big the array is.
d338d6fe 239 $tArrayDepth = $#{$v} ;
240 undef $more ;
9bcb75ad 241 # Bigger than the max?
d338d6fe 242 $tArrayDepth = $#{$v} < $arrayDepth-1 ? $#{$v} : $arrayDepth-1
600d99fa 243 if defined $arrayDepth && $arrayDepth ne '';
9bcb75ad 244 # Yep. Don't show it all.
d338d6fe 245 $more = "....\n" if $tArrayDepth < $#{$v} ;
246 $shortmore = "";
247 $shortmore = " ..." if $tArrayDepth < $#{$v} ;
9bcb75ad 248
d338d6fe 249 if ($compactDump && !grep(ref $_, @{$v})) {
250 if ($#$v >= 0) {
54d04a52
IZ
251 $short = $sp . "0..$#{$v} " .
252 join(" ",
859172fe 253 map {exists $v->[$_] ? stringify $v->[$_] : "empty"} (0..$tArrayDepth)
d9182636 254 ) . "$shortmore";
d338d6fe 255 } else {
256 $short = $sp . "empty array";
257 }
258 (print "$short\n"), return if length $short <= $compactDump;
259 }
260 #if ($compactDump && $short = ShortArray($v)) {
261 # print "$short\n";
262 # return;
263 #}
859172fe 264 for $num (0 .. $tArrayDepth) {
d338d6fe 265 return if $DB::signal;
266 print "$sp$num ";
d9182636 267 if (exists $v->[$num]) {
9bcb75ad
JM
268 if (defined $v->[$num]) {
269 DumpElem $v->[$num], $s, $m-1;
270 }
271 else {
272 print "undef\n";
273 }
d9182636
GS
274 } else {
275 print "empty slot\n";
276 }
d338d6fe 277 }
278 print "$sp empty array\n" unless @$v;
279 print "$sp$more" if defined $more ;
9bcb75ad
JM
280 } elsif ( $item_type eq 'SCALAR' ) {
281 unless (defined $$v) {
282 print "$sp-> undef\n";
283 return;
284 }
d338d6fe 285 print "$sp-> ";
d03c2a1b 286 DumpElem $$v, $s, $m-1;
9bcb75ad
JM
287 } elsif ( $item_type eq 'REF' ) {
288 print "$sp-> $$v\n";
289 return unless defined $$v;
290 unwrap($$v, $s+3, $m-1);
291 } elsif ( $item_type eq 'CODE' ) {
292 # Code object or reference.
54d04a52
IZ
293 print "$sp-> ";
294 dumpsub (0, $v);
9bcb75ad
JM
295 } elsif ( $item_type eq 'GLOB' ) {
296 # Glob object or reference.
d338d6fe 297 print "$sp-> ",&stringify($$v,1),"\n";
298 if ($globPrint) {
299 $s += 3;
3a4b996c 300 dumpglob($s, "{$$v}", $$v, 1, $m-1);
bef1d284 301 } elsif (defined ($fileno = eval {fileno($v)})) {
d338d6fe 302 print( (' ' x ($s+3)) . "FileHandle({$$v}) => fileno($fileno)\n" );
303 }
304 } elsif (ref \$v eq 'GLOB') {
9bcb75ad 305 # Raw glob (again?)
d338d6fe 306 if ($globPrint) {
3a4b996c 307 dumpglob($s, "{$v}", $v, 1, $m-1) if $globPrint;
bef1d284 308 } elsif (defined ($fileno = eval {fileno(\$v)})) {
d338d6fe 309 print( (' ' x $s) . "FileHandle({$v}) => fileno($fileno)\n" );
310 }
311 }
312}
313
a7b657ee
PS
314sub matchlex {
315 (my $var = $_[0]) =~ s/.//;
316 $var eq $_[1] or
317 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
318 ($1 eq '!') ^ (eval { $var =~ /$2$3/ });
319}
320
d338d6fe 321sub matchvar {
322 $_[0] eq $_[1] or
b2391ea8 323 ($_[1] =~ /^([!~])(.)([\x00-\xff]*)/) and
324 ($1 eq '!') ^ (eval {($_[2] . "::" . $_[0]) =~ /$2$3/});
d338d6fe 325}
326
327sub compactDump {
328 $compactDump = shift if @_;
329 $compactDump = 6*80-1 if $compactDump and $compactDump < 2;
330 $compactDump;
331}
332
333sub veryCompact {
334 $veryCompact = shift if @_;
335 compactDump(1) if !$compactDump and $veryCompact;
336 $veryCompact;
337}
338
339sub unctrlSet {
340 if (@_) {
341 my $in = shift;
342 if ($in eq 'unctrl' or $in eq 'quote') {
343 $unctrl = $in;
344 } else {
1f874cb6 345 print "Unknown value for 'unctrl'.\n";
d338d6fe 346 }
347 }
348 $unctrl;
349}
350
351sub quote {
352 if (@_ and $_[0] eq '"') {
353 $tick = '"';
354 $unctrl = 'quote';
54d04a52
IZ
355 } elsif (@_ and $_[0] eq 'auto') {
356 $tick = 'auto';
357 $unctrl = 'quote';
d338d6fe 358 } elsif (@_) { # Need to set
359 $tick = "'";
360 $unctrl = 'unctrl';
361 }
362 $tick;
363}
364
365sub dumpglob {
366 return if $DB::signal;
3a4b996c 367 my ($off,$key, $val, $all, $m) = @_;
d338d6fe 368 local(*entry) = $val;
369 my $fileno;
54d04a52 370 if (($key !~ /^_</ or $dumpDBFiles) and defined $entry) {
d338d6fe 371 print( (' ' x $off) . "\$", &unctrl($key), " = " );
3a4b996c 372 DumpElem $entry, 3+$off, $m;
d338d6fe 373 }
475342a6 374 if (($key !~ /^_</ or $dumpDBFiles) and @entry) {
d338d6fe 375 print( (' ' x $off) . "\@$key = (\n" );
3a4b996c 376 unwrap(\@entry,3+$off,$m) ;
d338d6fe 377 print( (' ' x $off) . ")\n" );
378 }
475342a6 379 if ($key ne "main::" && $key ne "DB::" && %entry
d338d6fe 380 && ($dumpPackages or $key !~ /::$/)
54d04a52 381 && ($key !~ /^_</ or $dumpDBFiles)
d338d6fe 382 && !($package eq "dumpvar" and $key eq "stab")) {
383 print( (' ' x $off) . "\%$key = (\n" );
3a4b996c 384 unwrap(\%entry,3+$off,$m) ;
d338d6fe 385 print( (' ' x $off) . ")\n" );
386 }
bef1d284 387 if (defined ($fileno = eval{fileno(*entry)})) {
d338d6fe 388 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
389 }
390 if ($all) {
391 if (defined &entry) {
54d04a52 392 dumpsub($off, $key);
d338d6fe 393 }
394 }
395}
396
a7b657ee
PS
397sub dumplex {
398 return if $DB::signal;
399 my ($key, $val, $m, @vars) = @_;
400 return if @vars && !grep( matchlex($key, $_), @vars );
401 local %address;
402 my $off = 0; # It reads better this way
403 my $fileno;
404 if (UNIVERSAL::isa($val,'ARRAY')) {
405 print( (' ' x $off) . "$key = (\n" );
406 unwrap($val,3+$off,$m) ;
407 print( (' ' x $off) . ")\n" );
408 }
409 elsif (UNIVERSAL::isa($val,'HASH')) {
410 print( (' ' x $off) . "$key = (\n" );
411 unwrap($val,3+$off,$m) ;
412 print( (' ' x $off) . ")\n" );
413 }
414 elsif (UNIVERSAL::isa($val,'IO')) {
415 print( (' ' x $off) . "FileHandle($key) => fileno($fileno)\n" );
416 }
417 # No lexical subroutines yet...
418 # elsif (UNIVERSAL::isa($val,'CODE')) {
419 # dumpsub($off, $$val);
420 # }
421 else {
422 print( (' ' x $off) . &unctrl($key), " = " );
423 DumpElem $$val, 3+$off, $m;
424 }
425}
426
83ee9e09
GS
427sub CvGV_name_or_bust {
428 my $in = shift;
429 return if $skipCvGV; # Backdoor to avoid problems if XS broken...
430 $in = \&$in; # Hard reference...
431 eval {require Devel::Peek; 1} or return;
432 my $gv = Devel::Peek::CvGV($in) or return;
433 *$gv{PACKAGE} . '::' . *$gv{NAME};
434}
435
54d04a52
IZ
436sub dumpsub {
437 my ($off,$sub) = @_;
83ee9e09
GS
438 my $ini = $sub;
439 my $s;
54d04a52 440 $sub = $1 if $sub =~ /^\{\*(.*)\}$/;
83ee9e09
GS
441 my $subref = defined $1 ? \&$sub : \&$ini;
442 my $place = $DB::sub{$sub} || (($s = $subs{"$subref"}) && $DB::sub{$s})
443 || (($s = CvGV_name_or_bust($subref)) && $DB::sub{$s})
444 || ($subdump && ($s = findsubs("$subref")) && $DB::sub{$s});
54d04a52 445 $place = '???' unless defined $place;
83ee9e09
GS
446 $s = $sub unless defined $s;
447 print( (' ' x $off) . "&$s in $place\n" );
54d04a52
IZ
448}
449
450sub findsubs {
475342a6 451 return undef unless %DB::sub;
54d04a52
IZ
452 my ($addr, $name, $loc);
453 while (($name, $loc) = each %DB::sub) {
454 $addr = \&$name;
455 $subs{"$addr"} = $name;
456 }
457 $subdump = 0;
458 $subs{ shift() };
459}
460
d338d6fe 461sub main::dumpvar {
3a4b996c 462 my ($package,$m,@vars) = @_;
b2391ea8 463 local(%address,$key,$val,$^W);
d338d6fe 464 $package .= "::" unless $package =~ /::$/;
465 *stab = *{"main::"};
466 while ($package =~ /(\w+?::)/g){
467 *stab = $ {stab}{$1};
468 }
469 local $TotalStrings = 0;
470 local $Strings = 0;
471 local $CompleteTotal = 0;
472 while (($key,$val) = each(%stab)) {
473 return if $DB::signal;
474 next if @vars && !grep( matchvar($key, $_), @vars );
475 if ($usageOnly) {
4c82ae22
GS
476 globUsage(\$val, $key)
477 if ($package ne 'dumpvar' or $key ne 'stab')
478 and ref(\$val) eq 'GLOB';
d338d6fe 479 } else {
3a4b996c 480 dumpglob(0,$key, $val, 0, $m);
d338d6fe 481 }
482 }
483 if ($usageOnly) {
484 print "String space: $TotalStrings bytes in $Strings strings.\n";
485 $CompleteTotal += $TotalStrings;
486 print "Grand total = $CompleteTotal bytes (1 level deep) + overhead.\n";
487 }
488}
489
490sub scalarUsage {
491 my $size = length($_[0]);
492 $TotalStrings += $size;
493 $Strings++;
494 $size;
495}
496
497sub arrayUsage { # array ref, name
498 my $size = 0;
499 map {$size += scalarUsage($_)} @{$_[0]};
500 my $len = @{$_[0]};
501 print "\@$_[1] = $len item", ($len > 1 ? "s" : ""),
502 " (data: $size bytes)\n"
503 if defined $_[1];
504 $CompleteTotal += $size;
505 $size;
506}
507
508sub hashUsage { # hash ref, name
509 my @keys = keys %{$_[0]};
510 my @values = values %{$_[0]};
511 my $keys = arrayUsage \@keys;
512 my $values = arrayUsage \@values;
513 my $len = @keys;
514 my $total = $keys + $values;
515 print "\%$_[1] = $len item", ($len > 1 ? "s" : ""),
516 " (keys: $keys; values: $values; total: $total bytes)\n"
517 if defined $_[1];
518 $total;
519}
520
521sub globUsage { # glob ref, name
522 local *name = *{$_[0]};
523 $total = 0;
524 $total += scalarUsage $name if defined $name;
475342a6
GS
525 $total += arrayUsage \@name, $_[1] if @name;
526 $total += hashUsage \%name, $_[1] if %name and $_[1] ne "main::"
d338d6fe 527 and $_[1] ne "DB::"; #and !($package eq "dumpvar" and $key eq "stab"));
528 $total;
529}
530
531sub packageUsage {
532 my ($package,@vars) = @_;
533 $package .= "::" unless $package =~ /::$/;
534 local *stab = *{"main::"};
535 while ($package =~ /(\w+?::)/g){
536 *stab = $ {stab}{$1};
537 }
538 local $TotalStrings = 0;
539 local $CompleteTotal = 0;
540 my ($key,$val);
541 while (($key,$val) = each(%stab)) {
542 next if @vars && !grep($key eq $_,@vars);
543 globUsage \$val, $key unless $package eq 'dumpvar' and $key eq 'stab';
544 }
545 print "String space: $TotalStrings.\n";
546 $CompleteTotal += $TotalStrings;
547 print "\nGrand total = $CompleteTotal bytes\n";
548}
549
5501;
551