Devel::Peek(3pm) Perl Programmers Reference Guide Devel::Peek(3pm)
NAME
Devel::Peek - A data debugging tool for the XS programmer
SYNOPSIS
use Devel::Peek;
Dump( $a );
Dump( $a, 5 );
DumpArray( 5, $a, $b, ... );
mstat "Point 5";
use Devel::Peek ':opd=st';
DESCRIPTION
Devel::Peek contains functions which allows raw Perl datatypes to be manipulated from a Perl script. This is used by those who do XS
programming to check that the data they are sending from C to Perl looks as they think it should look. The trick, then, is to know what
the raw datatype is supposed to look like when it gets to Perl. This document offers some tips and hints to describe good and bad raw
data.
It is very possible that this document will fall far short of being useful to the casual reader. The reader is expected to understand the
material in the first few sections of perlguts.
Devel::Peek supplies a "Dump()" function which can dump a raw Perl datatype, and "mstat("marker")" function to report on memory usage (if
perl is compiled with corresponding option). The function DeadCode() provides statistics on the data "frozen" into inactive "CV".
Devel::Peek also supplies "SvREFCNT()", "SvREFCNT_inc()", and "SvREFCNT_dec()" which can query, increment, and decrement reference counts
on SVs. This document will take a passive, and safe, approach to data debugging and for that it will describe only the "Dump()" function.
Function "DumpArray()" allows dumping of multiple values (useful when you need to analyze returns of functions).
The global variable $Devel::Peek::pv_limit can be set to limit the number of character printed in various string values. Setting it to 0
means no limit.
If "use Devel::Peek" directive has a ":opd=FLAGS" argument, this switches on debugging of opcode dispatch. "FLAGS" should be a combination
of "s", "t", and "P" (see -D flags in perlrun). ":opd" is a shortcut for ":opd=st".
Runtime debugging
"CvGV($cv)" return one of the globs associated to a subroutine reference $cv.
debug_flags() returns a string representation of $^D (similar to what is allowed for -D flag). When called with a numeric argument, sets
$^D to the corresponding value. When called with an argument of the form "flags-flags", set on/off bits of $^D corresponding to letters
before/after "-". (The returned value is for $^D before the modification.)
runops_debug() returns true if the current opcode dispatcher is the debugging one. When called with an argument, switches to debugging or
non-debugging dispatcher depending on the argument (active for newly-entered subs/etc only). (The returned value is for the dispatcher
before the modification.)
Memory footprint debugging
When perl is compiled with support for memory footprint debugging (default with Perl's malloc()), Devel::Peek provides an access to this
API.
Use mstat() function to emit a memory state statistic to the terminal. For more information on the format of output of mstat() see "Using
$ENV{PERL_DEBUG_MSTATS}" in perldebguts.
Three additional functions allow access to this statistic from Perl. First, use "mstats_fillhash(%hash)" to get the information contained
in the output of mstat() into %hash. The field of this hash are
minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree
Two additional fields "free", "used" contain array references which provide per-bucket count of free and used chunks. Two other fields
"mem_size", "available_size" contain array references which provide the information about the allocated size and usable size of chunks in
each bucket. Again, see "Using $ENV{PERL_DEBUG_MSTATS}" in perldebguts for details.
Keep in mind that only the first several "odd-numbered" buckets are used, so the information on size of the "odd-numbered" buckets which
are not used is probably meaningless.
The information in
mem_size available_size minbucket nbuckets
is the property of a particular build of perl, and does not depend on the current process. If you do not provide the optional argument to
the functions mstats_fillhash(), fill_mstats(), mstats2hash(), then the information in fields "mem_size", "available_size" is not updated.
"fill_mstats($buf)" is a much cheaper call (both speedwise and memory-wise) which collects the statistic into $buf in machine-readable
form. At a later moment you may need to call "mstats2hash($buf, %hash)" to use this information to fill %hash.
All three APIs "fill_mstats($buf)", "mstats_fillhash(%hash)", and "mstats2hash($buf, %hash)" are designed to allocate no memory if used the
second time on the same $buf and/or %hash.
So, if you want to collect memory info in a cycle, you may call
$#buf = 999;
fill_mstats($_) for @buf;
mstats_fillhash(%report, 1); # Static info too
foreach (@buf) {
# Do something...
fill_mstats $_; # Collect statistic
}
foreach (@buf) {
mstats2hash($_, %report); # Preserve static info
# Do something with %report
}
EXAMPLES
The following examples don't attempt to show everything as that would be a monumental task, and, frankly, we don't want this manpage to be
an internals document for Perl. The examples do demonstrate some basics of the raw Perl datatypes, and should suffice to get most
determined people on their way. There are no guidewires or safety nets, nor blazed trails, so be prepared to travel alone from this point
and on and, if at all possible, don't fall into the quicksand (it's bad for business).
Oh, one final bit of advice: take perlguts with you. When you return we expect to see it well-thumbed.
A simple scalar string
Let's begin by looking a simple scalar which is holding a string.
use Devel::Peek;
$a = "hello";
Dump $a;
The output:
SV = PVIV(0xbc288)
REFCNT = 1
FLAGS = (POK,pPOK)
IV = 0
PV = 0xb2048 "hello"