2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml">
5 <title>Devel::Peek - A data debugging tool for the XS programmer
</title>
6 <meta http-equiv=
"content-type" content=
"text/html; charset=utf-8" />
7 <link rev=
"made" href=
"mailto:" />
10 <body style=
"background-color: white">
11 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
12 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
13 <big><strong><span class=
"block"> Devel::Peek - A data debugging tool for the XS programmer
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
22 <li><a href=
"#name">NAME
</a></li>
23 <li><a href=
"#synopsis">SYNOPSIS
</a></li>
24 <li><a href=
"#description">DESCRIPTION
</a></li>
27 <li><a href=
"#runtime_debugging">Runtime debugging
</a></li>
28 <li><a href=
"#memory_footprint_debugging">Memory footprint debugging
</a></li>
31 <li><a href=
"#examples">EXAMPLES
</a></li>
34 <li><a href=
"#a_simple_scalar_string">A simple scalar string
</a></li>
35 <li><a href=
"#a_simple_scalar_number">A simple scalar number
</a></li>
36 <li><a href=
"#a_simple_scalar_with_an_extra_reference">A simple scalar with an extra reference
</a></li>
37 <li><a href=
"#a_reference_to_a_simple_scalar">A reference to a simple scalar
</a></li>
38 <li><a href=
"#a_reference_to_an_array">A reference to an array
</a></li>
39 <li><a href=
"#a_reference_to_a_hash">A reference to a hash
</a></li>
40 <li><a href=
"#dumping_a_large_array_or_hash">Dumping a large array or hash
</a></li>
41 <li><a href=
"#a_reference_to_an_sv_which_holds_a_c_pointer">A reference to an SV which holds a C pointer
</a></li>
42 <li><a href=
"#a_reference_to_a_subroutine">A reference to a subroutine
</a></li>
45 <li><a href=
"#exports">EXPORTS
</a></li>
46 <li><a href=
"#bugs">BUGS
</a></li>
47 <li><a href=
"#author">AUTHOR
</a></li>
48 <li><a href=
"#see_also">SEE ALSO
</a></li>
55 <h1><a name=
"name">NAME
</a></h1>
56 <p>Devel::Peek - A data debugging tool for the XS programmer
</p>
60 <h1><a name=
"synopsis">SYNOPSIS
</a></h1>
65 DumpArray(
5, $a, $b, ... );
66 mstat
"Point
5";
</pre>
68 use Devel::Peek ':opd=st';
</pre>
72 <h1><a name=
"description">DESCRIPTION
</a></h1>
73 <p>Devel::Peek contains functions which allows raw Perl datatypes to be
74 manipulated from a Perl script. This is used by those who do XS programming
75 to check that the data they are sending from C to Perl looks as they think
76 it should look. The trick, then, is to know what the raw datatype is
77 supposed to look like when it gets to Perl. This document offers some tips
78 and hints to describe good and bad raw data.
</p>
79 <p>It is very possible that this document will fall far short of being useful
80 to the casual reader. The reader is expected to understand the material in
81 the first few sections of
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage
</a>.
</p>
82 <p>Devel::Peek supplies a
<code>Dump()
</code> function which can dump a raw Perl
83 datatype, and
<code>mstat(
"marker
")
</code> function to report on memory usage
84 (if perl is compiled with corresponding option). The function
85 <code>DeadCode()
</code> provides statistics on the data ``frozen'' into inactive
86 <code>CV
</code>. Devel::Peek also supplies
<code>SvREFCNT()
</code>,
<code>SvREFCNT_inc()
</code>, and
87 <code>SvREFCNT_dec()
</code> which can query, increment, and decrement reference
88 counts on SVs. This document will take a passive, and safe, approach
89 to data debugging and for that it will describe only the
<code>Dump()
</code>
91 <p>Function
<code>DumpArray()
</code> allows dumping of multiple values (useful when you
92 need to analyze returns of functions).
</p>
93 <p>The global variable $Devel::Peek::pv_limit can be set to limit the
94 number of character printed in various string values. Setting it to
0
96 <p>If
<code>use Devel::Peek
</code> directive has a
<code>:opd=FLAGS
</code> argument,
97 this switches on debugging of opcode dispatch.
<code>FLAGS
</code> should be a
98 combination of
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html#item_s"><code>s
</code></a>,
<code>t
</code>, and
<code>P
</code> (see
<strong>-D
</strong> flags in
<a href=
"file://C|\msysgit\mingw\html/pod/perlrun.html">the perlrun manpage
</a>).
99 <code>:opd
</code> is a shortcut for
<code>:opd=st
</code>.
</p>
102 <h2><a name=
"runtime_debugging">Runtime debugging
</a></h2>
103 <p><code>CvGV($cv)
</code> return one of the globs associated to a subroutine reference $cv.
</p>
104 <p><code>debug_flags()
</code> returns a string representation of
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item___d"><code>$^D
</code></a> (similar to
105 what is allowed for
<strong>-D
</strong> flag). When called with a numeric argument,
106 sets $^D to the corresponding value. When called with an argument of
107 the form
<code>"flags-flags
"</code>, set on/off bits of
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item___d"><code>$^D
</code></a> corresponding to
108 letters before/after
<code>-
</code>. (The returned value is for
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item___d"><code>$^D
</code></a> before
109 the modification.)
</p>
110 <p><code>runops_debug()
</code> returns true if the current
<em>opcode dispatcher
</em> is the
111 debugging one. When called with an argument, switches to debugging or
112 non-debugging dispatcher depending on the argument (active for
113 newly-entered subs/etc only). (The returned value is for the dispatcher before the modification.)
</p>
116 <h2><a name=
"memory_footprint_debugging">Memory footprint debugging
</a></h2>
117 <p>When perl is compiled with support for memory footprint debugging
118 (default with Perl's malloc()), Devel::Peek provides an access to this API.
</p>
119 <p>Use
<code>mstat()
</code> function to emit a memory state statistic to the terminal.
120 For more information on the format of output of
<code>mstat()
</code> see
121 <a href=
"file://C|\msysgit\mingw\html/pod/perldebguts.html#using_c__env_perl_debug_mstats__">Using
<code>$ENV{PERL_DEBUG_MSTATS}
</code> in the perldebguts manpage
</a>.
</p>
122 <p>Three additional functions allow access to this statistic from Perl.
123 First, use
<code>mstats_fillhash(%hash)
</code> to get the information contained
124 in the output of
<code>mstat()
</code> into %hash. The field of this hash are
</p>
126 minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
127 topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
</pre>
128 <p>Two additional fields
<code>free
</code>,
<code>used
</code> contain array references which
129 provide per-bucket count of free and used chunks. Two other fields
130 <code>mem_size
</code>,
<code>available_size
</code> contain array references which provide
131 the information about the allocated size and usable size of chunks in
132 each bucket. Again, see
<a href=
"file://C|\msysgit\mingw\html/pod/perldebguts.html#using_c__env_perl_debug_mstats__">Using
<code>$ENV{PERL_DEBUG_MSTATS}
</code> in the perldebguts manpage
</a>
134 <p>Keep in mind that only the first several ``odd-numbered'' buckets are
135 used, so the information on size of the ``odd-numbered'' buckets which are
136 not used is probably meaningless.
</p>
137 <p>The information in
</p>
139 mem_size available_size minbucket nbuckets
</pre>
140 <p>is the property of a particular build of perl, and does not depend on
141 the current process. If you do not provide the optional argument to
142 the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then
143 the information in fields
<code>mem_size
</code>,
<code>available_size
</code> is not
145 <p><code>fill_mstats($buf)
</code> is a much cheaper call (both speedwise and
146 memory-wise) which collects the statistic into $buf in
147 machine-readable form. At a later moment you may need to call
148 <code>mstats2hash($buf, %hash)
</code> to use this information to fill %hash.
</p>
149 <p>All three APIs
<code>fill_mstats($buf)
</code>,
<code>mstats_fillhash(%hash)
</code>, and
150 <code>mstats2hash($buf, %hash)
</code> are designed to allocate no memory if used
151 <em>the second time
</em> on the same $buf and/or %hash.
</p>
152 <p>So, if you want to collect memory info in a cycle, you may call
</p>
155 fill_mstats($_) for @buf;
156 mstats_fillhash(%report,
1); # Static info too
</pre>
160 fill_mstats $_; # Collect statistic
163 mstats2hash($_, %report); # Preserve static info
164 # Do something with %report
169 <h1><a name=
"examples">EXAMPLES
</a></h1>
170 <p>The following examples don't attempt to show everything as that would be a
171 monumental task, and, frankly, we don't want this manpage to be an internals
172 document for Perl. The examples do demonstrate some basics of the raw Perl
173 datatypes, and should suffice to get most determined people on their way.
174 There are no guidewires or safety nets, nor blazed trails, so be prepared to
175 travel alone from this point and on and, if at all possible, don't fall into
176 the quicksand (it's bad for business).
</p>
177 <p>Oh, one final bit of advice: take
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage
</a> with you. When you return we
178 expect to see it well-thumbed.
</p>
181 <h2><a name=
"a_simple_scalar_string">A simple scalar string
</a></h2>
182 <p>Let's begin by looking a simple scalar which is holding a string.
</p>
185 $a =
"hello
";
193 PV =
0xb2048 "hello
"\
0
196 <p>This says
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> is an SV, a scalar. The scalar is a PVIV, a string.
197 Its reference count is
1. It has the
<code>POK
</code> flag set, meaning its
198 current PV field is valid. Because POK is set we look at the PV item
199 to see what is in the scalar. The \
0 at the end indicate that this
200 PV is properly NUL-terminated.
201 If the FLAGS had been IOK we would look
202 at the IV item. CUR indicates the number of characters in the PV.
203 LEN indicates the number of bytes requested for the PV (one more than
204 CUR, in this case, because LEN includes an extra byte for the
205 end-of-string marker).
</p>
208 <h2><a name=
"a_simple_scalar_number">A simple scalar number
</a></h2>
209 <p>If the scalar contains a number the raw SV will be leaner.
</p>
220 <p>This says
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> is an SV, a scalar. The scalar is an IV, a number. Its
221 reference count is
1. It has the
<code>IOK
</code> flag set, meaning it is currently
222 being evaluated as a number. Because IOK is set we look at the IV item to
223 see what is in the scalar.
</p>
226 <h2><a name=
"a_simple_scalar_with_an_extra_reference">A simple scalar with an extra reference
</a></h2>
227 <p>If the scalar from the previous example had an extra reference:
</p>
239 <p>Notice that this example differs from the previous example only in its
240 reference count. Compare this to the next example, where we dump
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__b"><code>$b
</code></a>
241 instead of
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a>.
</p>
244 <h2><a name=
"a_reference_to_a_simple_scalar">A reference to a simple scalar
</a></h2>
245 <p>This shows what a reference looks like when it references a simple scalar.
</p>
261 <p>Starting from the top, this says
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__b"><code>$b
</code></a> is an SV. The scalar is an RV, a
262 reference. It has the
<code>ROK
</code> flag set, meaning it is a reference. Because
263 ROK is set we have an RV item rather than an IV or PV. Notice that Dump
264 follows the reference and shows us what
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__b"><code>$b
</code></a> was referencing. We see the
265 same
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> that we found in the previous example.
</p>
266 <p>Note that the value of
<code>RV
</code> coincides with the numbers we see when we
267 stringify $b. The addresses inside
<code>RV()
</code> and
<code>IV()
</code> are addresses of
268 <code>X***
</code> structure which holds the current state of an
<code>SV
</code>. This
269 address may change during lifetime of an SV.
</p>
272 <h2><a name=
"a_reference_to_an_array">A reference to an array
</a></h2>
273 <p>This shows what a reference to an array looks like.
</p>
300 <p>This says
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> is an SV and that it is an RV. That RV points to
301 another SV which is a PVAV, an array. The array has one element,
302 element zero, which is another SV. The field
<code>FILL
</code> above indicates
303 the last element in the array, similar to
<code>$#$a
</code>.
</p>
304 <p>If
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> pointed to an array of two elements then we would see the
307 use Devel::Peek 'Dump';
337 <p>Note that
<code>Dump
</code> will not report
<em>all
</em> the elements in the array,
338 only several first (depending on how deep it already went into the
342 <h2><a name=
"a_reference_to_a_hash">A reference to a hash
</a></h2>
343 <p>The following shows the raw form of a reference to a hash.
</p>
350 SV = RV(
0x8177858) at
0x816a618
354 SV = PVHV(
0x8167768) at
0x814fc10
359 ARRAY =
0x816c5b8 (
0:
7,
1:
1)
360 hash quality =
100.0%
366 Elt
"hello
" HASH =
0xc8fd181b
367 SV = IV(
0x816c030) at
0x814fcf4
371 <p>This shows
<a href=
"file://C|\msysgit\mingw\html/pod/perlvar.html#item__a"><code>$a
</code></a> is a reference pointing to an SV. That SV is a PVHV, a
372 hash. Fields RITER and EITER are used by
<code>each
</code>.
</p>
373 <p>The ``quality'' of a hash is defined as the total number of comparisons needed
374 to access every element once, relative to the expected number needed for a
375 random hash. The value can go over
100%.
</p>
376 <p>The total number of comparisons is equal to the sum of the squares of the
377 number of entries in each bucket. For a random hash of
<code><n
</code>> keys into
378 <code><k
</code>> buckets, the expected value is:
</p>
383 <h2><a name=
"dumping_a_large_array_or_hash">Dumping a large array or hash
</a></h2>
384 <p>The
<code>Dump()
</code> function, by default, dumps up to
4 elements from a
385 toplevel array or hash. This number can be increased by supplying a
386 second argument to the function.
</p>
389 $a = [
10,
11,
12,
13,
14];
391 <p>Notice that
<code>Dump()
</code> prints only elements
10 through
13 in the above code.
392 The following code will print all of the elements.
</p>
394 use Devel::Peek 'Dump';
395 $a = [
10,
11,
12,
13,
14];
399 <h2><a name=
"a_reference_to_an_sv_which_holds_a_c_pointer">A reference to an SV which holds a C pointer
</a></h2>
400 <p>This is what you really need to know as an XS programmer, of course. When
401 an XSUB returns a pointer to a C structure that pointer is stored in an SV
402 and a reference to that SV is placed on the XSUB stack. So the output from
403 an XSUB which uses something like the T_PTROBJ map might look something like
412 FLAGS = (OBJECT,IOK,pIOK)
416 STASH =
0xc1d10 "CookBookB::Opaque
"</pre>
417 <p>This shows that we have an SV which is an RV. That RV points at another
418 SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
419 blessed it has the
<code>OBJECT
</code> flag set. Note that an SV which holds a C
420 pointer also has the
<code>IOK
</code> flag set. The
<code>STASH
</code> is set to the package
421 name which this SV was blessed into.
</p>
422 <p>The output from an XSUB which uses something like the T_PTRREF map, which
423 doesn't bless the object, might look something like this:
</p>
437 <h2><a name=
"a_reference_to_a_subroutine">A reference to a subroutine
</a></h2>
438 <p>Looks like this:
</p>
449 COMP_STASH =
0x31068 "main
"
454 GVGV::GV =
0x1d44e8 "MY
" ::
"top_targets
"
455 FILE =
"(eval
5)
"
457 PADLIST =
0x1c9338</pre>
458 <p>This shows that
</p>
461 <p>the subroutine is not an XSUB (since
<code>START
</code> and
<code>ROOT
</code> are
462 non-zero, and
<code>XSUB
</code> is zero);
</p>
465 <p>that it was compiled in the package
<code>main
</code>;
</p>
468 <p>under the name
<code>MY::top_targets
</code>;
</p>
471 <p>inside a
5th eval in the program;
</p>
474 <p>it is not currently executed (see
<code>DEPTH
</code>);
</p>
477 <p>it has no prototype (
<code>PROTOTYPE
</code> field is missing).
</p>
483 <h1><a name=
"exports">EXPORTS
</a></h1>
484 <p><code>Dump
</code>,
<code>mstat
</code>,
<code>DeadCode
</code>,
<code>DumpArray
</code>,
<code>DumpWithOP
</code> and
485 <code>DumpProg
</code>,
<code>fill_mstats
</code>,
<code>mstats_fillhash
</code>,
<code>mstats2hash
</code> by
486 default. Additionally available
<code>SvREFCNT
</code>,
<code>SvREFCNT_inc
</code> and
487 <code>SvREFCNT_dec
</code>.
</p>
491 <h1><a name=
"bugs">BUGS
</a></h1>
492 <p>Readers have been known to skip important parts of
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage
</a>, causing much
493 frustration for all.
</p>
497 <h1><a name=
"author">AUTHOR
</a></h1>
498 <p>Ilya Zakharevich
<a href=
"mailto:ilya@math.ohio-state.edu">ilya@math.ohio-state.edu
</a></p>
499 <p>Copyright (c)
1995-
98 Ilya Zakharevich. All rights reserved.
500 This program is free software; you can redistribute it and/or
501 modify it under the same terms as Perl itself.
</p>
502 <p>Author of this software makes no claim whatsoever about suitability,
503 reliability, edability, editability or usability of this product, and
504 should not be kept liable for any damage resulting from the use of
505 it. If you can use it, you are in luck, if not, I should not be kept
506 responsible. Keep a handy copy of your backup tape at hand.
</p>
510 <h1><a name=
"see_also">SEE ALSO
</a></h1>
511 <p><a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage
</a>, and
<a href=
"file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage
</a>, again.
</p>
512 <table border=
"0" width=
"100%" cellspacing=
"0" cellpadding=
"3">
513 <tr><td class=
"block" style=
"background-color: #cccccc" valign=
"middle">
514 <big><strong><span class=
"block"> Devel::Peek - A data debugging tool for the XS programmer
</span></strong></big>