1 # Devel::Peek - A data debugging tool for the XS programmer
2 # The documentation is after the __END__
6 $VERSION = $VERSION = 0.95;
11 @ISA = qw(Exporter DynaLoader);
12 @EXPORT = qw(Dump mstat DeadCode DumpArray DumpWithOP DumpProg);
13 @EXPORT_OK = qw(SvREFCNT SvREFCNT_inc SvREFCNT_dec);
14 %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]);
16 bootstrap Devel::Peek;
18 sub DumpWithOP ($;$) {
19 local($Devel::Peek::dump_ops)=1;
20 my $depth = @_ > 1 ? $_[1] : 4 ;
29 Devel::Peek - A data debugging tool for the XS programmer
36 DumpArray( 5, $a, $b, ... );
41 Devel::Peek contains functions which allows raw Perl datatypes to be
42 manipulated from a Perl script. This is used by those who do XS programming
43 to check that the data they are sending from C to Perl looks as they think
44 it should look. The trick, then, is to know what the raw datatype is
45 supposed to look like when it gets to Perl. This document offers some tips
46 and hints to describe good and bad raw data.
48 It is very possible that this document will fall far short of being useful
49 to the casual reader. The reader is expected to understand the material in
50 the first few sections of L<perlguts>.
52 Devel::Peek supplies a C<Dump()> function which can dump a raw Perl
53 datatype, and C<mstat("marker")> function to report on memory usage
54 (if perl is compiled with corresponding option). The function
55 DeadCode() provides statistics on the data "frozen" into inactive
56 C<CV>. Devel::Peek also supplies C<SvREFCNT()>, C<SvREFCNT_inc()>, and
57 C<SvREFCNT_dec()> which can query, increment, and decrement reference
58 counts on SVs. This document will take a passive, and safe, approach
59 to data debugging and for that it will describe only the C<Dump()>
60 function. For format of output of mstats() see
61 L<perldebug/Using C<$ENV{PERL_DEBUG_MSTATS}>>.
63 Function C<DumpArray()> allows dumping of multiple values (useful when you
64 need to analize returns of functions).
66 The global variable $Devel::Peek::pv_limit can be set to limit the
67 number of character printed in various string values. Setting it to 0
72 The following examples don't attempt to show everything as that would be a
73 monumental task, and, frankly, we don't want this manpage to be an internals
74 document for Perl. The examples do demonstrate some basics of the raw Perl
75 datatypes, and should suffice to get most determined people on their way.
76 There are no guidewires or safety nets, nor blazed trails, so be prepared to
77 travel alone from this point and on and, if at all possible, don't fall into
78 the quicksand (it's bad for business).
80 Oh, one final bit of advice: take L<perlguts> with you. When you return we
81 expect to see it well-thumbed.
83 =head2 A simple scalar string
85 Let's begin by looking a simple scalar which is holding a string.
97 PV = 0xb2048 "hello"\0
101 This says C<$a> is an SV, a scalar. The scalar is a PVIV, a string.
102 Its reference count is 1. It has the C<POK> flag set, meaning its
103 current PV field is valid. Because POK is set we look at the PV item
104 to see what is in the scalar. The \0 at the end indicate that this
105 PV is properly NUL-terminated.
106 If the FLAGS had been IOK we would look
107 at the IV item. CUR indicates the number of characters in the PV.
108 LEN indicates the number of bytes requested for the PV (one more than
109 CUR, in this case, because LEN includes an extra byte for the
110 end-of-string marker).
112 =head2 A simple scalar number
114 If the scalar contains a number the raw SV will be leaner.
127 This says C<$a> is an SV, a scalar. The scalar is an IV, a number. Its
128 reference count is 1. It has the C<IOK> flag set, meaning it is currently
129 being evaluated as a number. Because IOK is set we look at the IV item to
130 see what is in the scalar.
132 =head2 A simple scalar with an extra reference
134 If the scalar from the previous example had an extra reference:
148 Notice that this example differs from the previous example only in its
149 reference count. Compare this to the next example, where we dump C<$b>
152 =head2 A reference to a simple scalar
154 This shows what a reference looks like when it references a simple scalar.
172 Starting from the top, this says C<$b> is an SV. The scalar is an RV, a
173 reference. It has the C<ROK> flag set, meaning it is a reference. Because
174 ROK is set we have an RV item rather than an IV or PV. Notice that Dump
175 follows the reference and shows us what C<$b> was referencing. We see the
176 same C<$a> that we found in the previous example.
178 Note that the value of C<RV> coincides with the numbers we see when we
179 stringify $b. The addresses inside RV() and IV() are addresses of
180 C<X***> structure which holds the current state of an C<SV>. This
181 address may change during lifetime of an SV.
183 =head2 A reference to an array
185 This shows what a reference to an array looks like.
214 This says C<$a> is an SV and that it is an RV. That RV points to
215 another SV which is a PVAV, an array. The array has one element,
216 element zero, which is another SV. The field C<FILL> above indicates
217 the last element in the array, similar to C<$#$a>.
219 If C<$a> pointed to an array of two elements then we would see the
222 use Devel::Peek 'Dump';
254 Note that C<Dump> will not report I<all> the elements in the array,
255 only several first (depending on how deep it already went into the
258 =head2 A reference to a hash
260 The following shows the raw form of a reference to a hash.
282 Elt "hello" => 0xbaaf0
288 This shows C<$a> is a reference pointing to an SV. That SV is a PVHV, a
289 hash. Fields RITER and EITER are used by C<L<each>>.
291 =head2 Dumping a large array or hash
293 The C<Dump()> function, by default, dumps up to 4 elements from a
294 toplevel array or hash. This number can be increased by supplying a
295 second argument to the function.
298 $a = [10,11,12,13,14];
301 Notice that C<Dump()> prints only elements 10 through 13 in the above code.
302 The following code will print all of the elements.
304 use Devel::Peek 'Dump';
305 $a = [10,11,12,13,14];
308 =head2 A reference to an SV which holds a C pointer
310 This is what you really need to know as an XS programmer, of course. When
311 an XSUB returns a pointer to a C structure that pointer is stored in an SV
312 and a reference to that SV is placed on the XSUB stack. So the output from
313 an XSUB which uses something like the T_PTROBJ map might look something like
322 FLAGS = (OBJECT,IOK,pIOK)
326 STASH = 0xc1d10 "CookBookB::Opaque"
328 This shows that we have an SV which is an RV. That RV points at another
329 SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
330 blessed it has the C<OBJECT> flag set. Note that an SV which holds a C
331 pointer also has the C<IOK> flag set. The C<STASH> is set to the package
332 name which this SV was blessed into.
334 The output from an XSUB which uses something like the T_PTRREF map, which
335 doesn't bless the object, might look something like this:
348 =head2 A reference to a subroutine
361 COMP_STASH = 0x31068 "main"
366 GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
376 the subroutine is not an XSUB (since C<START> and C<ROOT> are
377 non-zero, and C<XSUB> is zero);
381 that it was compiled in the package C<main>;
385 under the name C<MY::top_targets>;
389 inside a 5th eval in the program;
393 it is not currently executed (see C<DEPTH>);
397 it has no prototype (C<PROTOTYPE> field is missing).
403 C<Dump>, C<mstat>, C<DeadCode>, C<DumpArray>, C<DumpWithOP> and
404 C<DumpProg> by default. Additionally available C<SvREFCNT>,
405 C<SvREFCNT_inc> and C<SvREFCNT_dec>.
409 Readers have been known to skip important parts of L<perlguts>, causing much
414 Ilya Zakharevich ilya@math.ohio-state.edu
416 Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
417 This program is free software; you can redistribute it and/or
418 modify it under the same terms as Perl itself.
420 Author of this software makes no claim whatsoever about suitability,
421 reliability, edability, editability or usability of this product, and
422 should not be kept liable for any damage resulting from the use of
423 it. If you can use it, you are in luck, if not, I should not be kept
424 responsible. Keep a handy copy of your backup tape at hand.
428 L<perlguts>, and L<perlguts>, again.