This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op/.stat.t: fix race condition
[perl5.git] / ext / Devel-Peek / Peek.pm
CommitLineData
3967c732
JD
1# Devel::Peek - A data debugging tool for the XS programmer
2# The documentation is after the __END__
3
4package Devel::Peek;
5
1ed44841 6$VERSION = '1.18';
105cd853
YST
7$XS_VERSION = $VERSION;
8$VERSION = eval $VERSION;
3967c732
JD
9
10require Exporter;
da4061d3 11require XSLoader;
3967c732 12
9426adcd 13@ISA = qw(Exporter);
d1424c31 14@EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
bd16a5f0 15 fill_mstats mstats_fillhash mstats2hash runops_debug debug_flags);
da1929e7 16@EXPORT_OK = qw(SvREFCNT CvGV);
3967c732
JD
17%EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
18
da4061d3 19XSLoader::load();
3967c732 20
1045810a
IZ
21sub import {
22 my $c = shift;
23 my $ops_rx = qr/^:opd(=[stP]*)?\b/;
24 my @db = grep m/$ops_rx/, @_;
25 @_ = grep !m/$ops_rx/, @_;
26 if (@db) {
27 die "Too many :opd options" if @db > 1;
28 runops_debug(1);
29 my $flags = ($db[0] =~ m/$ops_rx/ and $1);
30 $flags = 'st' unless defined $flags;
31 my $f = 0;
32 $f |= 2 if $flags =~ /s/;
33 $f |= 8 if $flags =~ /t/;
34 $f |= 64 if $flags =~ /P/;
35 $^D |= $f if $f;
36 }
37 unshift @_, $c;
38 goto &Exporter::import;
39}
40
3967c732
JD
41sub DumpWithOP ($;$) {
42 local($Devel::Peek::dump_ops)=1;
43 my $depth = @_ > 1 ? $_[1] : 4 ;
44 Dump($_[0],$depth);
45}
46
bd16a5f0
IZ
47$D_flags = 'psltocPmfrxuLHXDSTR';
48
49sub debug_flags (;$) {
50 my $out = "";
51 for my $i (0 .. length($D_flags)-1) {
52 $out .= substr $D_flags, $i, 1 if $^D & (1<<$i);
53 }
54 my $arg = shift;
55 my $num = $arg;
56 if (defined $arg and $arg =~ /\D/) {
57 die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/;
58 my ($on,$off) = split /-/, "$arg-";
59 $num = $^D;
60 $num |= (1<<index($D_flags, $_)) for split //, $on;
61 $num &= ~(1<<index($D_flags, $_)) for split //, $off;
62 }
63 $^D = $num if defined $arg;
64 $out
65}
66
3967c732
JD
671;
68__END__
69
70=head1 NAME
71
72Devel::Peek - A data debugging tool for the XS programmer
73
74=head1 SYNOPSIS
75
76 use Devel::Peek;
77 Dump( $a );
78 Dump( $a, 5 );
40091657
FC
79 Dump( @a );
80 Dump( %h );
3967c732
JD
81 DumpArray( 5, $a, $b, ... );
82 mstat "Point 5";
83
1045810a
IZ
84 use Devel::Peek ':opd=st';
85
3967c732
JD
86=head1 DESCRIPTION
87
88Devel::Peek contains functions which allows raw Perl datatypes to be
89manipulated from a Perl script. This is used by those who do XS programming
90to check that the data they are sending from C to Perl looks as they think
91it should look. The trick, then, is to know what the raw datatype is
92supposed to look like when it gets to Perl. This document offers some tips
93and hints to describe good and bad raw data.
94
95It is very possible that this document will fall far short of being useful
96to the casual reader. The reader is expected to understand the material in
97the first few sections of L<perlguts>.
98
99Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
100datatype, and C<mstat("marker")> function to report on memory usage
101(if perl is compiled with corresponding option). The function
102DeadCode() provides statistics on the data "frozen" into inactive
da1929e7 103C<CV>. Devel::Peek also supplies C<SvREFCNT()> which can query reference
3967c732
JD
104counts on SVs. This document will take a passive, and safe, approach
105to data debugging and for that it will describe only the C<Dump()>
d1424c31 106function.
3967c732 107
40091657
FC
108The C<Dump()> function takes one or two arguments: something to dump, and
109an optional limit for recursion and array elements (default is 4). The
110first argument is evaluted in rvalue scalar context, with exceptions for
111@array and %hash, which dump the array or hash itself. So C<Dump @array>
112works, as does C<Dump $foo>. And C<Dump pos> will call C<pos> in rvalue
113context, whereas C<Dump ${\pos}> will call it in lvalue context.
114
3967c732 115Function C<DumpArray()> allows dumping of multiple values (useful when you
076c2fc0 116need to analyze returns of functions).
3967c732
JD
117
118The global variable $Devel::Peek::pv_limit can be set to limit the
119number of character printed in various string values. Setting it to 0
120means no limit.
121
1045810a
IZ
122If C<use Devel::Peek> directive has a C<:opd=FLAGS> argument,
123this switches on debugging of opcode dispatch. C<FLAGS> should be a
124combination of C<s>, C<t>, and C<P> (see B<-D> flags in L<perlrun>).
125C<:opd> is a shortcut for C<:opd=st>.
126
bd16a5f0
IZ
127=head2 Runtime debugging
128
129C<CvGV($cv)> return one of the globs associated to a subroutine reference $cv.
130
131debug_flags() returns a string representation of C<$^D> (similar to
132what is allowed for B<-D> flag). When called with a numeric argument,
133sets $^D to the corresponding value. When called with an argument of
134the form C<"flags-flags">, set on/off bits of C<$^D> corresponding to
135letters before/after C<->. (The returned value is for C<$^D> before
136the modification.)
137
138runops_debug() returns true if the current I<opcode dispatcher> is the
139debugging one. When called with an argument, switches to debugging or
140non-debugging dispatcher depending on the argument (active for
141newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.)
142
d1424c31
IZ
143=head2 Memory footprint debugging
144
145When perl is compiled with support for memory footprint debugging
146(default with Perl's malloc()), Devel::Peek provides an access to this API.
147
148Use mstat() function to emit a memory state statistic to the terminal.
149For more information on the format of output of mstat() see
9704a6c6 150L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>.
d1424c31
IZ
151
152Three additional functions allow access to this statistic from Perl.
153First, use C<mstats_fillhash(%hash)> to get the information contained
154in the output of mstat() into %hash. The field of this hash are
155
555bd962
BG
156 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks
157 start_slack topbucket topbucket_ev topbucket_odd total total_chain
158 total_sbrk totfree
d1424c31
IZ
159
160Two additional fields C<free>, C<used> contain array references which
161provide per-bucket count of free and used chunks. Two other fields
162C<mem_size>, C<available_size> contain array references which provide
163the information about the allocated size and usable size of chunks in
9704a6c6 164each bucket. Again, see L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>
d1424c31
IZ
165for details.
166
b1e4fe07 167
d1424c31
IZ
168Keep in mind that only the first several "odd-numbered" buckets are
169used, so the information on size of the "odd-numbered" buckets which are
170not used is probably meaningless.
171
172The information in
173
174 mem_size available_size minbucket nbuckets
175
176is the property of a particular build of perl, and does not depend on
177the current process. If you do not provide the optional argument to
178the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
179the information in fields C<mem_size>, C<available_size> is not
180updated.
181
182C<fill_mstats($buf)> is a much cheaper call (both speedwise and
183memory-wise) which collects the statistic into $buf in
184machine-readable form. At a later moment you may need to call
185C<mstats2hash($buf, %hash)> to use this information to fill %hash.
186
187All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
188C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
189I<the second time> on the same $buf and/or %hash.
190
191So, if you want to collect memory info in a cycle, you may call
192
193 $#buf = 999;
194 fill_mstats($_) for @buf;
195 mstats_fillhash(%report, 1); # Static info too
196
197 foreach (@buf) {
198 # Do something...
199 fill_mstats $_; # Collect statistic
200 }
201 foreach (@buf) {
202 mstats2hash($_, %report); # Preserve static info
203 # Do something with %report
204 }
205
3967c732
JD
206=head1 EXAMPLES
207
208The following examples don't attempt to show everything as that would be a
209monumental task, and, frankly, we don't want this manpage to be an internals
210document for Perl. The examples do demonstrate some basics of the raw Perl
211datatypes, and should suffice to get most determined people on their way.
212There are no guidewires or safety nets, nor blazed trails, so be prepared to
213travel alone from this point and on and, if at all possible, don't fall into
214the quicksand (it's bad for business).
215
216Oh, one final bit of advice: take L<perlguts> with you. When you return we
217expect to see it well-thumbed.
218
219=head2 A simple scalar string
220
221Let's begin by looking a simple scalar which is holding a string.
222
a423dfdd 223 use Devel::Peek;
f64eb7e3 224 $a = 42; $a = "hello";
3967c732
JD
225 Dump $a;
226
227The output:
228
f64eb7e3 229 SV = PVIV(0xbc288) at 0xbe9a8
3967c732
JD
230 REFCNT = 1
231 FLAGS = (POK,pPOK)
f64eb7e3 232 IV = 42
3967c732
JD
233 PV = 0xb2048 "hello"\0
234 CUR = 5
f64eb7e3 235 LEN = 8
3967c732 236
f64eb7e3
DM
237This says C<$a> is an SV, a scalar. The scalar type is a PVIV, which is
238capable of holding an integer (IV) and/or a string (PV) value. The scalar's
239head is allocated at address 0xbe9a8, while the body is at 0xbc288.
3967c732
JD
240Its reference count is 1. It has the C<POK> flag set, meaning its
241current PV field is valid. Because POK is set we look at the PV item
242to see what is in the scalar. The \0 at the end indicate that this
243PV is properly NUL-terminated.
f64eb7e3
DM
244Note that the IV field still contains its old numeric value, but because
245FLAGS doesn't have IOK set, we must ignore the IV item.
246CUR indicates the number of characters in the PV. LEN indicates the
247number of bytes allocated for the PV (at least one more than CUR, because
248LEN includes an extra byte for the end-of-string marker, then usually
249rounded up to some efficient allocation unit).
3967c732
JD
250
251=head2 A simple scalar number
252
253If the scalar contains a number the raw SV will be leaner.
254
a423dfdd 255 use Devel::Peek;
3967c732
JD
256 $a = 42;
257 Dump $a;
258
259The output:
260
f64eb7e3 261 SV = IV(0xbc818) at 0xbe9a8
3967c732
JD
262 REFCNT = 1
263 FLAGS = (IOK,pIOK)
264 IV = 42
265
266This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
267reference count is 1. It has the C<IOK> flag set, meaning it is currently
268being evaluated as a number. Because IOK is set we look at the IV item to
269see what is in the scalar.
270
271=head2 A simple scalar with an extra reference
272
273If the scalar from the previous example had an extra reference:
274
a423dfdd 275 use Devel::Peek;
3967c732
JD
276 $a = 42;
277 $b = \$a;
278 Dump $a;
279
280The output:
281
f64eb7e3 282 SV = IV(0xbe860) at 0xbe9a8
3967c732
JD
283 REFCNT = 2
284 FLAGS = (IOK,pIOK)
285 IV = 42
286
287Notice that this example differs from the previous example only in its
288reference count. Compare this to the next example, where we dump C<$b>
289instead of C<$a>.
290
291=head2 A reference to a simple scalar
292
293This shows what a reference looks like when it references a simple scalar.
294
a423dfdd 295 use Devel::Peek;
3967c732
JD
296 $a = 42;
297 $b = \$a;
298 Dump $b;
299
300The output:
301
f64eb7e3 302 SV = IV(0xf041c) at 0xbe9a0
3967c732
JD
303 REFCNT = 1
304 FLAGS = (ROK)
305 RV = 0xbab08
f64eb7e3
DM
306 SV = IV(0xbe860) at 0xbe9a8
307 REFCNT = 2
308 FLAGS = (IOK,pIOK)
309 IV = 42
310
311Starting from the top, this says C<$b> is an SV. The scalar is an IV,
312which is capable of holding an integer or reference value.
313It has the C<ROK> flag set, meaning it is a reference (rather than an
314integer or string). Notice that Dump
3967c732
JD
315follows the reference and shows us what C<$b> was referencing. We see the
316same C<$a> that we found in the previous example.
317
318Note that the value of C<RV> coincides with the numbers we see when we
f64eb7e3
DM
319stringify $b. The addresses inside IV() are addresses of
320C<X***> structures which hold the current state of an C<SV>. This
3967c732
JD
321address may change during lifetime of an SV.
322
323=head2 A reference to an array
324
325This shows what a reference to an array looks like.
326
a423dfdd 327 use Devel::Peek;
3967c732
JD
328 $a = [42];
329 Dump $a;
330
331The output:
332
f64eb7e3 333 SV = IV(0xc85998) at 0xc859a8
3967c732
JD
334 REFCNT = 1
335 FLAGS = (ROK)
f64eb7e3
DM
336 RV = 0xc70de8
337 SV = PVAV(0xc71e10) at 0xc70de8
338 REFCNT = 1
339 FLAGS = ()
340 ARRAY = 0xc7e820
341 FILL = 0
342 MAX = 0
343 ARYLEN = 0x0
344 FLAGS = (REAL)
345 Elt No. 0
346 SV = IV(0xc70f88) at 0xc70f98
347 REFCNT = 1
348 FLAGS = (IOK,pIOK)
349 IV = 42
350
351This says C<$a> is a reference (ROK), which points to
3967c732
JD
352another SV which is a PVAV, an array. The array has one element,
353element zero, which is another SV. The field C<FILL> above indicates
354the last element in the array, similar to C<$#$a>.
355
356If C<$a> pointed to an array of two elements then we would see the
357following.
358
359 use Devel::Peek 'Dump';
360 $a = [42,24];
361 Dump $a;
362
363The output:
364
f64eb7e3 365 SV = IV(0x158c998) at 0x158c9a8
3967c732
JD
366 REFCNT = 1
367 FLAGS = (ROK)
f64eb7e3
DM
368 RV = 0x1577de8
369 SV = PVAV(0x1578e10) at 0x1577de8
370 REFCNT = 1
371 FLAGS = ()
372 ARRAY = 0x1585820
373 FILL = 1
374 MAX = 1
375 ARYLEN = 0x0
376 FLAGS = (REAL)
377 Elt No. 0
378 SV = IV(0x1577f88) at 0x1577f98
379 REFCNT = 1
380 FLAGS = (IOK,pIOK)
381 IV = 42
382 Elt No. 1
383 SV = IV(0x158be88) at 0x158be98
384 REFCNT = 1
385 FLAGS = (IOK,pIOK)
386 IV = 24
3967c732
JD
387
388Note that C<Dump> will not report I<all> the elements in the array,
389only several first (depending on how deep it already went into the
390report tree).
391
392=head2 A reference to a hash
393
394The following shows the raw form of a reference to a hash.
395
a423dfdd 396 use Devel::Peek;
3967c732
JD
397 $a = {hello=>42};
398 Dump $a;
399
400The output:
401
f64eb7e3 402 SV = IV(0x8177858) at 0x816a618
d5889220
T
403 REFCNT = 1
404 FLAGS = (ROK)
405 RV = 0x814fc10
406 SV = PVHV(0x8167768) at 0x814fc10
407 REFCNT = 1
408 FLAGS = (SHAREKEYS)
d5889220
T
409 ARRAY = 0x816c5b8 (0:7, 1:1)
410 hash quality = 100.0%
411 KEYS = 1
412 FILL = 1
413 MAX = 7
414 RITER = -1
415 EITER = 0x0
416 Elt "hello" HASH = 0xc8fd181b
417 SV = IV(0x816c030) at 0x814fcf4
418 REFCNT = 1
419 FLAGS = (IOK,pIOK)
420 IV = 42
3967c732
JD
421
422This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
b1e4fe07 423hash. Fields RITER and EITER are used by C<L<perlfunc/each>>.
3967c732 424
d5889220
T
425The "quality" of a hash is defined as the total number of comparisons needed
426to access every element once, relative to the expected number needed for a
427random hash. The value can go over 100%.
428
429The total number of comparisons is equal to the sum of the squares of the
430number of entries in each bucket. For a random hash of C<<n>> keys into
431C<<k>> buckets, the expected value is:
432
433 n + n(n-1)/2k
434
3967c732
JD
435=head2 Dumping a large array or hash
436
437The C<Dump()> function, by default, dumps up to 4 elements from a
438toplevel array or hash. This number can be increased by supplying a
439second argument to the function.
440
a423dfdd 441 use Devel::Peek;
3967c732
JD
442 $a = [10,11,12,13,14];
443 Dump $a;
444
445Notice that C<Dump()> prints only elements 10 through 13 in the above code.
446The following code will print all of the elements.
447
448 use Devel::Peek 'Dump';
449 $a = [10,11,12,13,14];
450 Dump $a, 5;
451
452=head2 A reference to an SV which holds a C pointer
453
454This is what you really need to know as an XS programmer, of course. When
455an XSUB returns a pointer to a C structure that pointer is stored in an SV
456and a reference to that SV is placed on the XSUB stack. So the output from
457an XSUB which uses something like the T_PTROBJ map might look something like
458this:
459
f64eb7e3 460 SV = IV(0xf381c) at 0xc859a8
3967c732
JD
461 REFCNT = 1
462 FLAGS = (ROK)
463 RV = 0xb8ad8
f64eb7e3
DM
464 SV = PVMG(0xbb3c8) at 0xc859a0
465 REFCNT = 1
466 FLAGS = (OBJECT,IOK,pIOK)
467 IV = 729160
468 NV = 0
469 PV = 0
470 STASH = 0xc1d10 "CookBookB::Opaque"
471
472This shows that we have an SV which is a reference, which points at another
3967c732
JD
473SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
474blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
475pointer also has the C<IOK> flag set. The C<STASH> is set to the package
476name which this SV was blessed into.
477
478The output from an XSUB which uses something like the T_PTRREF map, which
479doesn't bless the object, might look something like this:
480
f64eb7e3 481 SV = IV(0xf381c) at 0xc859a8
3967c732
JD
482 REFCNT = 1
483 FLAGS = (ROK)
484 RV = 0xb8ad8
f64eb7e3
DM
485 SV = PVMG(0xbb3c8) at 0xc859a0
486 REFCNT = 1
487 FLAGS = (IOK,pIOK)
488 IV = 729160
489 NV = 0
490 PV = 0
3967c732
JD
491
492=head2 A reference to a subroutine
493
494Looks like this:
495
f64eb7e3 496 SV = IV(0x24d2dd8) at 0x24d2de8
3967c732
JD
497 REFCNT = 1
498 FLAGS = (TEMP,ROK)
f64eb7e3
DM
499 RV = 0x24e79d8
500 SV = PVCV(0x24e5798) at 0x24e79d8
501 REFCNT = 2
502 FLAGS = ()
503 COMP_STASH = 0x22c9c50 "main"
504 START = 0x22eed60 ===> 0
505 ROOT = 0x22ee490
506 GVGV::GV = 0x22de9d8 "MY" :: "top_targets"
507 FILE = "(eval 5)"
508 DEPTH = 0
509 FLAGS = 0x0
510 OUTSIDE_SEQ = 93
511 PADLIST = 0x22e9ed8
512 PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0)
513 OUTSIDE = 0x22c9fb0 (MAIN)
514
3967c732
JD
515
516This shows that
517
bbc7dcd2 518=over 4
3967c732 519
a45bd81d 520=item *
3967c732
JD
521
522the subroutine is not an XSUB (since C<START> and C<ROOT> are
f64eb7e3 523non-zero, and C<XSUB> is not listed, and is thus null);
3967c732 524
a45bd81d 525=item *
3967c732
JD
526
527that it was compiled in the package C<main>;
528
a45bd81d 529=item *
3967c732
JD
530
531under the name C<MY::top_targets>;
532
a45bd81d 533=item *
3967c732
JD
534
535inside a 5th eval in the program;
536
a45bd81d 537=item *
3967c732
JD
538
539it is not currently executed (see C<DEPTH>);
540
a45bd81d 541=item *
3967c732
JD
542
543it has no prototype (C<PROTOTYPE> field is missing).
544
a45bd81d 545=back
3967c732
JD
546
547=head1 EXPORTS
548
549C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
7c6ca602 550C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
d1424c31
IZ
551default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
552C<SvREFCNT_dec>.
3967c732
JD
553
554=head1 BUGS
555
556Readers have been known to skip important parts of L<perlguts>, causing much
557frustration for all.
558
559=head1 AUTHOR
560
561Ilya Zakharevich ilya@math.ohio-state.edu
562
563Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
564This program is free software; you can redistribute it and/or
565modify it under the same terms as Perl itself.
566
567Author of this software makes no claim whatsoever about suitability,
568reliability, edability, editability or usability of this product, and
569should not be kept liable for any damage resulting from the use of
570it. If you can use it, you are in luck, if not, I should not be kept
571responsible. Keep a handy copy of your backup tape at hand.
572
573=head1 SEE ALSO
574
575L<perlguts>, and L<perlguts>, again.
576
577=cut