1 # Devel::Peek - A data debugging tool for the XS programmer
2 # The documentation is after the __END__
6 # Underscore to allow older Perls to access older version from CPAN
8 $XS_VERSION = $VERSION;
9 $VERSION = eval $VERSION;
15 @EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
16 fill_mstats mstats_fillhash mstats2hash runops_debug debug_flags);
17 @EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec CvGV);
18 %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
20 XSLoader::load 'Devel::Peek';
24 my $ops_rx = qr/^:opd(=[stP]*)?\b/;
25 my @db = grep m/$ops_rx/, @_;
26 @_ = grep !m/$ops_rx/, @_;
28 die "Too many :opd options" if @db > 1;
30 my $flags = ($db[0] =~ m/$ops_rx/ and $1);
31 $flags = 'st' unless defined $flags;
33 $f |= 2 if $flags =~ /s/;
34 $f |= 8 if $flags =~ /t/;
35 $f |= 64 if $flags =~ /P/;
39 goto &Exporter::import;
42 sub DumpWithOP ($;$) {
43 local($Devel::Peek::dump_ops)=1;
44 my $depth = @_ > 1 ? $_[1] : 4 ;
48 $D_flags = 'psltocPmfrxuLHXDSTR';
50 sub debug_flags (;$) {
52 for my $i (0 .. length($D_flags)-1) {
53 $out .= substr $D_flags, $i, 1 if $^D & (1<<$i);
57 if (defined $arg and $arg =~ /\D/) {
58 die "unknown flags in debug_flags()" if $arg =~ /[^-$D_flags]/;
59 my ($on,$off) = split /-/, "$arg-";
61 $num |= (1<<index($D_flags, $_)) for split //, $on;
62 $num &= ~(1<<index($D_flags, $_)) for split //, $off;
64 $^D = $num if defined $arg;
73 Devel::Peek - A data debugging tool for the XS programmer
80 DumpArray( 5, $a, $b, ... );
83 use Devel::Peek ':opd=st';
87 Devel::Peek contains functions which allows raw Perl datatypes to be
88 manipulated from a Perl script. This is used by those who do XS programming
89 to check that the data they are sending from C to Perl looks as they think
90 it should look. The trick, then, is to know what the raw datatype is
91 supposed to look like when it gets to Perl. This document offers some tips
92 and hints to describe good and bad raw data.
94 It is very possible that this document will fall far short of being useful
95 to the casual reader. The reader is expected to understand the material in
96 the first few sections of L<perlguts>.
98 Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
99 datatype, and C<mstat("marker")> function to report on memory usage
100 (if perl is compiled with corresponding option). The function
101 DeadCode() provides statistics on the data "frozen" into inactive
102 C<CV>. Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, and
103 C<SvREFCNT_dec()> which can query, increment, and decrement 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 Function C<DumpArray()> allows dumping of multiple values (useful when you
109 need to analyze returns of functions).
111 The global variable $Devel::Peek::pv_limit can be set to limit the
112 number of character printed in various string values. Setting it to 0
115 If C<use Devel::Peek> directive has a C<:opd=FLAGS> argument,
116 this switches on debugging of opcode dispatch. C<FLAGS> should be a
117 combination of C<s>, C<t>, and C<P> (see B<-D> flags in L<perlrun>).
118 C<:opd> is a shortcut for C<:opd=st>.
120 =head2 Runtime debugging
122 C<CvGV($cv)> return one of the globs associated to a subroutine reference $cv.
124 debug_flags() returns a string representation of C<$^D> (similar to
125 what is allowed for B<-D> flag). When called with a numeric argument,
126 sets $^D to the corresponding value. When called with an argument of
127 the form C<"flags-flags">, set on/off bits of C<$^D> corresponding to
128 letters before/after C<->. (The returned value is for C<$^D> before
131 runops_debug() returns true if the current I<opcode dispatcher> is the
132 debugging one. When called with an argument, switches to debugging or
133 non-debugging dispatcher depending on the argument (active for
134 newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.)
136 =head2 Memory footprint debugging
138 When perl is compiled with support for memory footprint debugging
139 (default with Perl's malloc()), Devel::Peek provides an access to this API.
141 Use mstat() function to emit a memory state statistic to the terminal.
142 For more information on the format of output of mstat() see
143 L<perldebguts/Using C<$ENV{PERL_DEBUG_MSTATS}>>.
145 Three additional functions allow access to this statistic from Perl.
146 First, use C<mstats_fillhash(%hash)> to get the information contained
147 in the output of mstat() into %hash. The field of this hash are
149 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
150 topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
152 Two additional fields C<free>, C<used> contain array references which
153 provide per-bucket count of free and used chunks. Two other fields
154 C<mem_size>, C<available_size> contain array references which provide
155 the information about the allocated size and usable size of chunks in
156 each bucket. Again, see L<perldebguts/Using C<$ENV{PERL_DEBUG_MSTATS}>>
159 Keep in mind that only the first several "odd-numbered" buckets are
160 used, so the information on size of the "odd-numbered" buckets which are
161 not used is probably meaningless.
165 mem_size available_size minbucket nbuckets
167 is the property of a particular build of perl, and does not depend on
168 the current process. If you do not provide the optional argument to
169 the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
170 the information in fields C<mem_size>, C<available_size> is not
173 C<fill_mstats($buf)> is a much cheaper call (both speedwise and
174 memory-wise) which collects the statistic into $buf in
175 machine-readable form. At a later moment you may need to call
176 C<mstats2hash($buf, %hash)> to use this information to fill %hash.
178 All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
179 C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
180 I<the second time> on the same $buf and/or %hash.
182 So, if you want to collect memory info in a cycle, you may call
185 fill_mstats($_) for @buf;
186 mstats_fillhash(%report, 1); # Static info too
190 fill_mstats $_; # Collect statistic
193 mstats2hash($_, %report); # Preserve static info
194 # Do something with %report
199 The following examples don't attempt to show everything as that would be a
200 monumental task, and, frankly, we don't want this manpage to be an internals
201 document for Perl. The examples do demonstrate some basics of the raw Perl
202 datatypes, and should suffice to get most determined people on their way.
203 There are no guidewires or safety nets, nor blazed trails, so be prepared to
204 travel alone from this point and on and, if at all possible, don't fall into
205 the quicksand (it's bad for business).
207 Oh, one final bit of advice: take L<perlguts> with you. When you return we
208 expect to see it well-thumbed.
210 =head2 A simple scalar string
212 Let's begin by looking a simple scalar which is holding a string.
224 PV = 0xb2048 "hello"\0
228 This says C<$a> is an SV, a scalar. The scalar is a PVIV, a string.
229 Its reference count is 1. It has the C<POK> flag set, meaning its
230 current PV field is valid. Because POK is set we look at the PV item
231 to see what is in the scalar. The \0 at the end indicate that this
232 PV is properly NUL-terminated.
233 If the FLAGS had been IOK we would look
234 at the IV item. CUR indicates the number of characters in the PV.
235 LEN indicates the number of bytes requested for the PV (one more than
236 CUR, in this case, because LEN includes an extra byte for the
237 end-of-string marker).
239 =head2 A simple scalar number
241 If the scalar contains a number the raw SV will be leaner.
254 This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
255 reference count is 1. It has the C<IOK> flag set, meaning it is currently
256 being evaluated as a number. Because IOK is set we look at the IV item to
257 see what is in the scalar.
259 =head2 A simple scalar with an extra reference
261 If the scalar from the previous example had an extra reference:
275 Notice that this example differs from the previous example only in its
276 reference count. Compare this to the next example, where we dump C<$b>
279 =head2 A reference to a simple scalar
281 This shows what a reference looks like when it references a simple scalar.
299 Starting from the top, this says C<$b> is an SV. The scalar is an RV, a
300 reference. It has the C<ROK> flag set, meaning it is a reference. Because
301 ROK is set we have an RV item rather than an IV or PV. Notice that Dump
302 follows the reference and shows us what C<$b> was referencing. We see the
303 same C<$a> that we found in the previous example.
305 Note that the value of C<RV> coincides with the numbers we see when we
306 stringify $b. The addresses inside RV() and IV() are addresses of
307 C<X***> structure which holds the current state of an C<SV>. This
308 address may change during lifetime of an SV.
310 =head2 A reference to an array
312 This shows what a reference to an array looks like.
341 This says C<$a> is an SV and that it is an RV. That RV points to
342 another SV which is a PVAV, an array. The array has one element,
343 element zero, which is another SV. The field C<FILL> above indicates
344 the last element in the array, similar to C<$#$a>.
346 If C<$a> pointed to an array of two elements then we would see the
349 use Devel::Peek 'Dump';
381 Note that C<Dump> will not report I<all> the elements in the array,
382 only several first (depending on how deep it already went into the
385 =head2 A reference to a hash
387 The following shows the raw form of a reference to a hash.
409 Elt "hello" => 0xbaaf0
415 This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
416 hash. Fields RITER and EITER are used by C<L<each>>.
418 =head2 Dumping a large array or hash
420 The C<Dump()> function, by default, dumps up to 4 elements from a
421 toplevel array or hash. This number can be increased by supplying a
422 second argument to the function.
425 $a = [10,11,12,13,14];
428 Notice that C<Dump()> prints only elements 10 through 13 in the above code.
429 The following code will print all of the elements.
431 use Devel::Peek 'Dump';
432 $a = [10,11,12,13,14];
435 =head2 A reference to an SV which holds a C pointer
437 This is what you really need to know as an XS programmer, of course. When
438 an XSUB returns a pointer to a C structure that pointer is stored in an SV
439 and a reference to that SV is placed on the XSUB stack. So the output from
440 an XSUB which uses something like the T_PTROBJ map might look something like
449 FLAGS = (OBJECT,IOK,pIOK)
453 STASH = 0xc1d10 "CookBookB::Opaque"
455 This shows that we have an SV which is an RV. That RV points at another
456 SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
457 blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
458 pointer also has the C<IOK> flag set. The C<STASH> is set to the package
459 name which this SV was blessed into.
461 The output from an XSUB which uses something like the T_PTRREF map, which
462 doesn't bless the object, might look something like this:
475 =head2 A reference to a subroutine
488 COMP_STASH = 0x31068 "main"
493 GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
504 the subroutine is not an XSUB (since C<START> and C<ROOT> are
505 non-zero, and C<XSUB> is zero);
509 that it was compiled in the package C<main>;
513 under the name C<MY::top_targets>;
517 inside a 5th eval in the program;
521 it is not currently executed (see C<DEPTH>);
525 it has no prototype (C<PROTOTYPE> field is missing).
531 C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
532 C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
533 default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
538 Readers have been known to skip important parts of L<perlguts>, causing much
543 Ilya Zakharevich ilya@math.ohio-state.edu
545 Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
546 This program is free software; you can redistribute it and/or
547 modify it under the same terms as Perl itself.
549 Author of this software makes no claim whatsoever about suitability,
550 reliability, edability, editability or usability of this product, and
551 should not be kept liable for any damage resulting from the use of
552 it. If you can use it, you are in luck, if not, I should not be kept
553 responsible. Keep a handy copy of your backup tape at hand.
557 L<perlguts>, and L<perlguts>, again.