1 # Devel::Peek - A data debugging tool for the XS programmer
2 # The documentation is after the __END__
7 $XS_VERSION = $VERSION;
8 $VERSION = eval $VERSION;
14 @EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
15 fill_mstats mstats_fillhash mstats2hash runops_debug debug_flags);
16 @EXPORT_OK = qw(SvREFCNT CvGV);
17 %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
23 my $ops_rx = qr/^:opd(=[stP]*)?\b/;
24 my @db = grep m/$ops_rx/, @_;
25 @_ = grep !m/$ops_rx/, @_;
27 die "Too many :opd options" if @db > 1;
29 my $flags = ($db[0] =~ m/$ops_rx/ and $1);
30 $flags = 'st' unless defined $flags;
32 $f |= 2 if $flags =~ /s/;
33 $f |= 8 if $flags =~ /t/;
34 $f |= 64 if $flags =~ /P/;
38 goto &Exporter::import;
41 sub DumpWithOP ($;$) {
42 local($Devel::Peek::dump_ops)=1;
43 my $depth = @_ > 1 ? $_[1] : 4 ;
47 $D_flags = 'psltocPmfrxuLHXDSTR';
49 sub debug_flags (;$) {
51 for my $i (0 .. length($D_flags)-1) {
52 $out .= substr $D_flags, $i, 1 if $^D & (1<<$i);
56 if (defined $arg and $arg =~ /\D/) {
57 die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/;
58 my ($on,$off) = split /-/, "$arg-";
60 $num |= (1<<index($D_flags, $_)) for split //, $on;
61 $num &= ~(1<<index($D_flags, $_)) for split //, $off;
63 $^D = $num if defined $arg;
72 Devel::Peek - A data debugging tool for the XS programmer
81 DumpArray( 5, $a, $b, ... );
84 use Devel::Peek ':opd=st';
88 Devel::Peek contains functions which allows raw Perl datatypes to be
89 manipulated from a Perl script. This is used by those who do XS programming
90 to check that the data they are sending from C to Perl looks as they think
91 it should look. The trick, then, is to know what the raw datatype is
92 supposed to look like when it gets to Perl. This document offers some tips
93 and hints to describe good and bad raw data.
95 It is very possible that this document will fall far short of being useful
96 to the casual reader. The reader is expected to understand the material in
97 the first few sections of L<perlguts>.
99 Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
100 datatype, and C<mstat("marker")> function to report on memory usage
101 (if perl is compiled with corresponding option). The function
102 DeadCode() provides statistics on the data "frozen" into inactive
103 C<CV>. Devel::Peek also supplies C<SvREFCNT()> which can query reference
104 counts on SVs. This document will take a passive, and safe, approach
105 to data debugging and for that it will describe only the C<Dump()>
108 The C<Dump()> function takes one or two arguments: something to dump, and
109 an optional limit for recursion and array elements (default is 4). The
110 first 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>
112 works, as does C<Dump $foo>. And C<Dump pos> will call C<pos> in rvalue
113 context, whereas C<Dump ${\pos}> will call it in lvalue context.
115 Function C<DumpArray()> allows dumping of multiple values (useful when you
116 need to analyze returns of functions).
118 The global variable $Devel::Peek::pv_limit can be set to limit the
119 number of character printed in various string values. Setting it to 0
122 If C<use Devel::Peek> directive has a C<:opd=FLAGS> argument,
123 this switches on debugging of opcode dispatch. C<FLAGS> should be a
124 combination of C<s>, C<t>, and C<P> (see B<-D> flags in L<perlrun>).
125 C<:opd> is a shortcut for C<:opd=st>.
127 =head2 Runtime debugging
129 C<CvGV($cv)> return one of the globs associated to a subroutine reference $cv.
131 debug_flags() returns a string representation of C<$^D> (similar to
132 what is allowed for B<-D> flag). When called with a numeric argument,
133 sets $^D to the corresponding value. When called with an argument of
134 the form C<"flags-flags">, set on/off bits of C<$^D> corresponding to
135 letters before/after C<->. (The returned value is for C<$^D> before
138 runops_debug() returns true if the current I<opcode dispatcher> is the
139 debugging one. When called with an argument, switches to debugging or
140 non-debugging dispatcher depending on the argument (active for
141 newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.)
143 =head2 Memory footprint debugging
145 When perl is compiled with support for memory footprint debugging
146 (default with Perl's malloc()), Devel::Peek provides an access to this API.
148 Use mstat() function to emit a memory state statistic to the terminal.
149 For more information on the format of output of mstat() see
150 L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>.
152 Three additional functions allow access to this statistic from Perl.
153 First, use C<mstats_fillhash(%hash)> to get the information contained
154 in the output of mstat() into %hash. The field of this hash are
156 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks
157 start_slack topbucket topbucket_ev topbucket_odd total total_chain
160 Two additional fields C<free>, C<used> contain array references which
161 provide per-bucket count of free and used chunks. Two other fields
162 C<mem_size>, C<available_size> contain array references which provide
163 the information about the allocated size and usable size of chunks in
164 each bucket. Again, see L<perldebguts/Using $ENV{PERL_DEBUG_MSTATS}>
168 Keep in mind that only the first several "odd-numbered" buckets are
169 used, so the information on size of the "odd-numbered" buckets which are
170 not used is probably meaningless.
174 mem_size available_size minbucket nbuckets
176 is the property of a particular build of perl, and does not depend on
177 the current process. If you do not provide the optional argument to
178 the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
179 the information in fields C<mem_size>, C<available_size> is not
182 C<fill_mstats($buf)> is a much cheaper call (both speedwise and
183 memory-wise) which collects the statistic into $buf in
184 machine-readable form. At a later moment you may need to call
185 C<mstats2hash($buf, %hash)> to use this information to fill %hash.
187 All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
188 C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
189 I<the second time> on the same $buf and/or %hash.
191 So, if you want to collect memory info in a cycle, you may call
194 fill_mstats($_) for @buf;
195 mstats_fillhash(%report, 1); # Static info too
199 fill_mstats $_; # Collect statistic
202 mstats2hash($_, %report); # Preserve static info
203 # Do something with %report
208 The following examples don't attempt to show everything as that would be a
209 monumental task, and, frankly, we don't want this manpage to be an internals
210 document for Perl. The examples do demonstrate some basics of the raw Perl
211 datatypes, and should suffice to get most determined people on their way.
212 There are no guidewires or safety nets, nor blazed trails, so be prepared to
213 travel alone from this point and on and, if at all possible, don't fall into
214 the quicksand (it's bad for business).
216 Oh, one final bit of advice: take L<perlguts> with you. When you return we
217 expect to see it well-thumbed.
219 =head2 A simple scalar string
221 Let's begin by looking a simple scalar which is holding a string.
224 $a = 42; $a = "hello";
229 SV = PVIV(0xbc288) at 0xbe9a8
233 PV = 0xb2048 "hello"\0
237 This says C<$a> is an SV, a scalar. The scalar type is a PVIV, which is
238 capable of holding an integer (IV) and/or a string (PV) value. The scalar's
239 head is allocated at address 0xbe9a8, while the body is at 0xbc288.
240 Its reference count is 1. It has the C<POK> flag set, meaning its
241 current PV field is valid. Because POK is set we look at the PV item
242 to see what is in the scalar. The \0 at the end indicate that this
243 PV is properly NUL-terminated.
244 Note that the IV field still contains its old numeric value, but because
245 FLAGS doesn't have IOK set, we must ignore the IV item.
246 CUR indicates the number of characters in the PV. LEN indicates the
247 number of bytes allocated for the PV (at least one more than CUR, because
248 LEN includes an extra byte for the end-of-string marker, then usually
249 rounded up to some efficient allocation unit).
251 =head2 A simple scalar number
253 If the scalar contains a number the raw SV will be leaner.
261 SV = IV(0xbc818) at 0xbe9a8
266 This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
267 reference count is 1. It has the C<IOK> flag set, meaning it is currently
268 being evaluated as a number. Because IOK is set we look at the IV item to
269 see what is in the scalar.
271 =head2 A simple scalar with an extra reference
273 If the scalar from the previous example had an extra reference:
282 SV = IV(0xbe860) at 0xbe9a8
287 Notice that this example differs from the previous example only in its
288 reference count. Compare this to the next example, where we dump C<$b>
291 =head2 A reference to a simple scalar
293 This shows what a reference looks like when it references a simple scalar.
302 SV = IV(0xf041c) at 0xbe9a0
306 SV = IV(0xbe860) at 0xbe9a8
311 Starting from the top, this says C<$b> is an SV. The scalar is an IV,
312 which is capable of holding an integer or reference value.
313 It has the C<ROK> flag set, meaning it is a reference (rather than an
314 integer or string). Notice that Dump
315 follows the reference and shows us what C<$b> was referencing. We see the
316 same C<$a> that we found in the previous example.
318 Note that the value of C<RV> coincides with the numbers we see when we
319 stringify $b. The addresses inside IV() are addresses of
320 C<X***> structures which hold the current state of an C<SV>. This
321 address may change during lifetime of an SV.
323 =head2 A reference to an array
325 This shows what a reference to an array looks like.
333 SV = IV(0xc85998) at 0xc859a8
337 SV = PVAV(0xc71e10) at 0xc70de8
346 SV = IV(0xc70f88) at 0xc70f98
351 This says C<$a> is a reference (ROK), which points to
352 another SV which is a PVAV, an array. The array has one element,
353 element zero, which is another SV. The field C<FILL> above indicates
354 the last element in the array, similar to C<$#$a>.
356 If C<$a> pointed to an array of two elements then we would see the
359 use Devel::Peek 'Dump';
365 SV = IV(0x158c998) at 0x158c9a8
369 SV = PVAV(0x1578e10) at 0x1577de8
378 SV = IV(0x1577f88) at 0x1577f98
383 SV = IV(0x158be88) at 0x158be98
388 Note that C<Dump> will not report I<all> the elements in the array,
389 only several first (depending on how deep it already went into the
392 =head2 A reference to a hash
394 The following shows the raw form of a reference to a hash.
402 SV = IV(0x8177858) at 0x816a618
406 SV = PVHV(0x8167768) at 0x814fc10
409 ARRAY = 0x816c5b8 (0:7, 1:1)
410 hash quality = 100.0%
416 Elt "hello" HASH = 0xc8fd181b
417 SV = IV(0x816c030) at 0x814fcf4
422 This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
423 hash. Fields RITER and EITER are used by C<L<perlfunc/each>>.
425 The "quality" of a hash is defined as the total number of comparisons needed
426 to access every element once, relative to the expected number needed for a
427 random hash. The value can go over 100%.
429 The total number of comparisons is equal to the sum of the squares of the
430 number of entries in each bucket. For a random hash of C<<n>> keys into
431 C<<k>> buckets, the expected value is:
435 =head2 Dumping a large array or hash
437 The C<Dump()> function, by default, dumps up to 4 elements from a
438 toplevel array or hash. This number can be increased by supplying a
439 second argument to the function.
442 $a = [10,11,12,13,14];
445 Notice that C<Dump()> prints only elements 10 through 13 in the above code.
446 The following code will print all of the elements.
448 use Devel::Peek 'Dump';
449 $a = [10,11,12,13,14];
452 =head2 A reference to an SV which holds a C pointer
454 This is what you really need to know as an XS programmer, of course. When
455 an XSUB returns a pointer to a C structure that pointer is stored in an SV
456 and a reference to that SV is placed on the XSUB stack. So the output from
457 an XSUB which uses something like the T_PTROBJ map might look something like
460 SV = IV(0xf381c) at 0xc859a8
464 SV = PVMG(0xbb3c8) at 0xc859a0
466 FLAGS = (OBJECT,IOK,pIOK)
470 STASH = 0xc1d10 "CookBookB::Opaque"
472 This shows that we have an SV which is a reference, which points at another
473 SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
474 blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
475 pointer also has the C<IOK> flag set. The C<STASH> is set to the package
476 name which this SV was blessed into.
478 The output from an XSUB which uses something like the T_PTRREF map, which
479 doesn't bless the object, might look something like this:
481 SV = IV(0xf381c) at 0xc859a8
485 SV = PVMG(0xbb3c8) at 0xc859a0
492 =head2 A reference to a subroutine
496 SV = IV(0x24d2dd8) at 0x24d2de8
500 SV = PVCV(0x24e5798) at 0x24e79d8
503 COMP_STASH = 0x22c9c50 "main"
504 START = 0x22eed60 ===> 0
506 GVGV::GV = 0x22de9d8 "MY" :: "top_targets"
512 PADNAME = 0x22e9ec0(0x22eed00) PAD = 0x22e9ea8(0x22eecd0)
513 OUTSIDE = 0x22c9fb0 (MAIN)
522 the subroutine is not an XSUB (since C<START> and C<ROOT> are
523 non-zero, and C<XSUB> is not listed, and is thus null);
527 that it was compiled in the package C<main>;
531 under the name C<MY::top_targets>;
535 inside a 5th eval in the program;
539 it is not currently executed (see C<DEPTH>);
543 it has no prototype (C<PROTOTYPE> field is missing).
549 C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
550 C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
551 default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
556 Readers have been known to skip important parts of L<perlguts>, causing much
561 Ilya Zakharevich ilya@math.ohio-state.edu
563 Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
564 This program is free software; you can redistribute it and/or
565 modify it under the same terms as Perl itself.
567 Author of this software makes no claim whatsoever about suitability,
568 reliability, edability, editability or usability of this product, and
569 should not be kept liable for any damage resulting from the use of
570 it. If you can use it, you are in luck, if not, I should not be kept
571 responsible. Keep a handy copy of your backup tape at hand.
575 L<perlguts>, and L<perlguts>, again.