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
13 @EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg
14 fill_mstats mstats_fillhash mstats2hash);
15 @EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec CvGV);
16 %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
18 XSLoader::load 'Devel::Peek';
20 sub DumpWithOP ($;$) {
21 local($Devel::Peek::dump_ops)=1;
22 my $depth = @_ > 1 ? $_[1] : 4 ;
31 Devel::Peek - A data debugging tool for the XS programmer
38 DumpArray( 5, $a, $b, ... );
43 Devel::Peek contains functions which allows raw Perl datatypes to be
44 manipulated from a Perl script. This is used by those who do XS programming
45 to check that the data they are sending from C to Perl looks as they think
46 it should look. The trick, then, is to know what the raw datatype is
47 supposed to look like when it gets to Perl. This document offers some tips
48 and hints to describe good and bad raw data.
50 It is very possible that this document will fall far short of being useful
51 to the casual reader. The reader is expected to understand the material in
52 the first few sections of L<perlguts>.
54 Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
55 datatype, and C<mstat("marker")> function to report on memory usage
56 (if perl is compiled with corresponding option). The function
57 DeadCode() provides statistics on the data "frozen" into inactive
58 C<CV>. Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, and
59 C<SvREFCNT_dec()> which can query, increment, and decrement reference
60 counts on SVs. This document will take a passive, and safe, approach
61 to data debugging and for that it will describe only the C<Dump()>
64 Function C<DumpArray()> allows dumping of multiple values (useful when you
65 need to analyze returns of functions).
67 The global variable $Devel::Peek::pv_limit can be set to limit the
68 number of character printed in various string values. Setting it to 0
71 =head2 Memory footprint debugging
73 When perl is compiled with support for memory footprint debugging
74 (default with Perl's malloc()), Devel::Peek provides an access to this API.
76 Use mstat() function to emit a memory state statistic to the terminal.
77 For more information on the format of output of mstat() see
78 L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>.
80 Three additional functions allow access to this statistic from Perl.
81 First, use C<mstats_fillhash(%hash)> to get the information contained
82 in the output of mstat() into %hash. The field of this hash are
84 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
85 topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
87 Two additional fields C<free>, C<used> contain array references which
88 provide per-bucket count of free and used chunks. Two other fields
89 C<mem_size>, C<available_size> contain array references which provide
90 the information about the allocated size and usable size of chunks in
91 each bucket. Again, see L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>
94 Keep in mind that only the first several "odd-numbered" buckets are
95 used, so the information on size of the "odd-numbered" buckets which are
96 not used is probably meaningless.
100 mem_size available_size minbucket nbuckets
102 is the property of a particular build of perl, and does not depend on
103 the current process. If you do not provide the optional argument to
104 the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
105 the information in fields C<mem_size>, C<available_size> is not
108 C<fill_mstats($buf)> is a much cheaper call (both speedwise and
109 memory-wise) which collects the statistic into $buf in
110 machine-readable form. At a later moment you may need to call
111 C<mstats2hash($buf, %hash)> to use this information to fill %hash.
113 All three APIs C<fill_mstats($buf)>, C<mstats_fillhash(%hash)>, and
114 C<mstats2hash($buf, %hash)> are designed to allocate no memory if used
115 I<the second time> on the same $buf and/or %hash.
117 So, if you want to collect memory info in a cycle, you may call
120 fill_mstats($_) for @buf;
121 mstats_fillhash(%report, 1); # Static info too
125 fill_mstats $_; # Collect statistic
128 mstats2hash($_, %report); # Preserve static info
129 # Do something with %report
134 The following examples don't attempt to show everything as that would be a
135 monumental task, and, frankly, we don't want this manpage to be an internals
136 document for Perl. The examples do demonstrate some basics of the raw Perl
137 datatypes, and should suffice to get most determined people on their way.
138 There are no guidewires or safety nets, nor blazed trails, so be prepared to
139 travel alone from this point and on and, if at all possible, don't fall into
140 the quicksand (it's bad for business).
142 Oh, one final bit of advice: take L<perlguts> with you. When you return we
143 expect to see it well-thumbed.
145 =head2 A simple scalar string
147 Let's begin by looking a simple scalar which is holding a string.
159 PV = 0xb2048 "hello"\0
163 This says C<$a> is an SV, a scalar. The scalar is a PVIV, a string.
164 Its reference count is 1. It has the C<POK> flag set, meaning its
165 current PV field is valid. Because POK is set we look at the PV item
166 to see what is in the scalar. The \0 at the end indicate that this
167 PV is properly NUL-terminated.
168 If the FLAGS had been IOK we would look
169 at the IV item. CUR indicates the number of characters in the PV.
170 LEN indicates the number of bytes requested for the PV (one more than
171 CUR, in this case, because LEN includes an extra byte for the
172 end-of-string marker).
174 =head2 A simple scalar number
176 If the scalar contains a number the raw SV will be leaner.
189 This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
190 reference count is 1. It has the C<IOK> flag set, meaning it is currently
191 being evaluated as a number. Because IOK is set we look at the IV item to
192 see what is in the scalar.
194 =head2 A simple scalar with an extra reference
196 If the scalar from the previous example had an extra reference:
210 Notice that this example differs from the previous example only in its
211 reference count. Compare this to the next example, where we dump C<$b>
214 =head2 A reference to a simple scalar
216 This shows what a reference looks like when it references a simple scalar.
234 Starting from the top, this says C<$b> is an SV. The scalar is an RV, a
235 reference. It has the C<ROK> flag set, meaning it is a reference. Because
236 ROK is set we have an RV item rather than an IV or PV. Notice that Dump
237 follows the reference and shows us what C<$b> was referencing. We see the
238 same C<$a> that we found in the previous example.
240 Note that the value of C<RV> coincides with the numbers we see when we
241 stringify $b. The addresses inside RV() and IV() are addresses of
242 C<X***> structure which holds the current state of an C<SV>. This
243 address may change during lifetime of an SV.
245 =head2 A reference to an array
247 This shows what a reference to an array looks like.
276 This says C<$a> is an SV and that it is an RV. That RV points to
277 another SV which is a PVAV, an array. The array has one element,
278 element zero, which is another SV. The field C<FILL> above indicates
279 the last element in the array, similar to C<$#$a>.
281 If C<$a> pointed to an array of two elements then we would see the
284 use Devel::Peek 'Dump';
316 Note that C<Dump> will not report I<all> the elements in the array,
317 only several first (depending on how deep it already went into the
320 =head2 A reference to a hash
322 The following shows the raw form of a reference to a hash.
344 Elt "hello" => 0xbaaf0
350 This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
351 hash. Fields RITER and EITER are used by C<L<each>>.
353 =head2 Dumping a large array or hash
355 The C<Dump()> function, by default, dumps up to 4 elements from a
356 toplevel array or hash. This number can be increased by supplying a
357 second argument to the function.
360 $a = [10,11,12,13,14];
363 Notice that C<Dump()> prints only elements 10 through 13 in the above code.
364 The following code will print all of the elements.
366 use Devel::Peek 'Dump';
367 $a = [10,11,12,13,14];
370 =head2 A reference to an SV which holds a C pointer
372 This is what you really need to know as an XS programmer, of course. When
373 an XSUB returns a pointer to a C structure that pointer is stored in an SV
374 and a reference to that SV is placed on the XSUB stack. So the output from
375 an XSUB which uses something like the T_PTROBJ map might look something like
384 FLAGS = (OBJECT,IOK,pIOK)
388 STASH = 0xc1d10 "CookBookB::Opaque"
390 This shows that we have an SV which is an RV. That RV points at another
391 SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
392 blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
393 pointer also has the C<IOK> flag set. The C<STASH> is set to the package
394 name which this SV was blessed into.
396 The output from an XSUB which uses something like the T_PTRREF map, which
397 doesn't bless the object, might look something like this:
410 =head2 A reference to a subroutine
423 COMP_STASH = 0x31068 "main"
428 GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
439 the subroutine is not an XSUB (since C<START> and C<ROOT> are
440 non-zero, and C<XSUB> is zero);
444 that it was compiled in the package C<main>;
448 under the name C<MY::top_targets>;
452 inside a 5th eval in the program;
456 it is not currently executed (see C<DEPTH>);
460 it has no prototype (C<PROTOTYPE> field is missing).
466 C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
467 C<DumpProg>, C<fill_mstats>, C<mstats_fillhash>, C<mstats2hash> by
468 default. Additionally available C<SvREFCNT>, C<SvREFCNT_inc> and
473 Readers have been known to skip important parts of L<perlguts>, causing much
478 Ilya Zakharevich ilya@math.ohio-state.edu
480 Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
481 This program is free software; you can redistribute it and/or
482 modify it under the same terms as Perl itself.
484 Author of this software makes no claim whatsoever about suitability,
485 reliability, edability, editability or usability of this product, and
486 should not be kept liable for any damage resulting from the use of
487 it. If you can use it, you are in luck, if not, I should not be kept
488 responsible. Keep a handy copy of your backup tape at hand.
492 L<perlguts>, and L<perlguts>, again.