home *** CD-ROM | disk | FTP | other *** search
-
- <HTML>
- <HEAD>
- <TITLE>Devel::Peek - A data debugging tool for the XS programmer</TITLE>
- <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
- <LINK REV="made" HREF="mailto:">
- </HEAD>
-
- <BODY>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> Devel::Peek - A data debugging tool for the XS programmer</P></STRONG>
- </TD></TR>
- </TABLE>
-
- <A NAME="__index__"></A>
- <!-- INDEX BEGIN -->
-
- <UL>
-
- <LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
-
- <LI><A HREF="#synopsis">SYNOPSIS</A></LI>
- <LI><A HREF="#description">DESCRIPTION</A></LI>
- <LI><A HREF="#examples">EXAMPLES</A></LI>
- <UL>
-
- <LI><A HREF="#a simple scalar string">A simple scalar string</A></LI>
- <LI><A HREF="#a simple scalar number">A simple scalar number</A></LI>
- <LI><A HREF="#a simple scalar with an extra reference">A simple scalar with an extra reference</A></LI>
- <LI><A HREF="#a reference to a simple scalar">A reference to a simple scalar</A></LI>
- <LI><A HREF="#a reference to an array">A reference to an array</A></LI>
- <LI><A HREF="#a reference to a hash">A reference to a hash</A></LI>
- <LI><A HREF="#dumping a large array or hash">Dumping a large array or hash</A></LI>
- <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>
- <LI><A HREF="#a reference to a subroutine">A reference to a subroutine</A></LI>
- </UL>
-
- <LI><A HREF="#exports">EXPORTS</A></LI>
- <LI><A HREF="#bugs">BUGS</A></LI>
- <LI><A HREF="#author">AUTHOR</A></LI>
- <LI><A HREF="#see also">SEE ALSO</A></LI>
- </UL>
- <!-- INDEX END -->
-
- <HR>
- <P>
- <H1><A NAME="name">NAME</A></H1>
- <P>Devel::Peek - A data debugging tool for the XS programmer</P>
- <P>
- <HR>
- <H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
- <UL>
- <LI>Linux</LI>
- <LI>Solaris</LI>
- <LI>Windows</LI>
- </UL>
- <HR>
- <H1><A NAME="synopsis">SYNOPSIS</A></H1>
- <PRE>
- use Devel::Peek;
- Dump( $a );
- Dump( $a, 5 );
- DumpArray( 5, $a, $b, ... );
- mstat "Point 5";</PRE>
- <P>
- <HR>
- <H1><A NAME="description">DESCRIPTION</A></H1>
- <P>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.</P>
- <P>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 <A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>.</P>
- <P>Devel::Peek supplies a <CODE>Dump()</CODE> function which can dump a raw Perl
- datatype, and <CODE>mstat("marker")</CODE> function to report on memory usage
- (if perl is compiled with corresponding option). The function
- <CODE>DeadCode()</CODE> provides statistics on the data ``frozen'' into inactive
- <CODE>CV</CODE>. Devel::Peek also supplies <CODE>SvREFCNT()</CODE>, <CODE>SvREFCNT_inc()</CODE>, and
- <CODE>SvREFCNT_dec()</CODE> 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 <CODE>Dump()</CODE>
- function. For format of output of <CODE>mstats()</CODE> see
- <A HREF="../../lib/Pod/perldebug.html#using c<$env{perl_debug_mstats}>">Using <CODE>$ENV{PERL_DEBUG_MSTATS}</CODE> in the perldebug manpage</A>.</P>
- <P>Function <CODE>DumpArray()</CODE> allows dumping of multiple values (useful when you
- need to analize returns of functions).</P>
- <P>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.</P>
- <P>
- <HR>
- <H1><A NAME="examples">EXAMPLES</A></H1>
- <P>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).</P>
- <P>Oh, one final bit of advice: take <A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A> with you. When you return we
- expect to see it well-thumbed.</P>
- <P>
- <H2><A NAME="a simple scalar string">A simple scalar string</A></H2>
- <P>Let's begin by looking a simple scalar which is holding a string.</P>
- <PRE>
- use Devel::Peek;
- $a = "hello";
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = PVIV(0xbc288)
- REFCNT = 1
- FLAGS = (POK,pPOK)
- IV = 0
- PV = 0xb2048 "hello"\0
- CUR = 5
- LEN = 6</PRE>
- <P>This says <CODE>$a</CODE> is an SV, a scalar. The scalar is a PVIV, a string.
- Its reference count is 1. It has the <CODE>POK</CODE> flag set, meaning its
- current PV field is valid. Because POK is set we look at the PV item
- to see what is in the scalar. The \0 at the end indicate that this
- PV is properly NUL-terminated.
- If the FLAGS had been IOK we would look
- at the IV item. CUR indicates the number of characters in the PV.
- LEN indicates the number of bytes requested for the PV (one more than
- CUR, in this case, because LEN includes an extra byte for the
- end-of-string marker).</P>
- <P>
- <H2><A NAME="a simple scalar number">A simple scalar number</A></H2>
- <P>If the scalar contains a number the raw SV will be leaner.</P>
- <PRE>
- use Devel::Peek;
- $a = 42;
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = IV(0xbc818)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 42</PRE>
- <P>This says <CODE>$a</CODE> is an SV, a scalar. The scalar is an IV, a number. Its
- reference count is 1. It has the <CODE>IOK</CODE> flag set, meaning it is currently
- being evaluated as a number. Because IOK is set we look at the IV item to
- see what is in the scalar.</P>
- <P>
- <H2><A NAME="a simple scalar with an extra reference">A simple scalar with an extra reference</A></H2>
- <P>If the scalar from the previous example had an extra reference:</P>
- <PRE>
- use Devel::Peek;
- $a = 42;
- $b = \$a;
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = IV(0xbe860)
- REFCNT = 2
- FLAGS = (IOK,pIOK)
- IV = 42</PRE>
- <P>Notice that this example differs from the previous example only in its
- reference count. Compare this to the next example, where we dump <CODE>$b</CODE>
- instead of <CODE>$a</CODE>.</P>
- <P>
- <H2><A NAME="a reference to a simple scalar">A reference to a simple scalar</A></H2>
- <P>This shows what a reference looks like when it references a simple scalar.</P>
- <PRE>
- use Devel::Peek;
- $a = 42;
- $b = \$a;
- Dump $b;</PRE>
- <P>The output:</P>
- <PRE>
- SV = RV(0xf041c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xbab08
- SV = IV(0xbe860)
- REFCNT = 2
- FLAGS = (IOK,pIOK)
- IV = 42</PRE>
- <P>Starting from the top, this says <CODE>$b</CODE> is an SV. The scalar is an RV, a
- reference. It has the <CODE>ROK</CODE> flag set, meaning it is a reference. Because
- ROK is set we have an RV item rather than an IV or PV. Notice that Dump
- follows the reference and shows us what <CODE>$b</CODE> was referencing. We see the
- same <CODE>$a</CODE> that we found in the previous example.</P>
- <P>Note that the value of <CODE>RV</CODE> coincides with the numbers we see when we
- stringify $b. The addresses inside <CODE>RV()</CODE> and <CODE>IV()</CODE> are addresses of
- <CODE>X***</CODE> structure which holds the current state of an <A HREF="../../lib/Pod/perlguts.html#item_SV"><CODE>SV</CODE></A>. This
- address may change during lifetime of an SV.</P>
- <P>
- <H2><A NAME="a reference to an array">A reference to an array</A></H2>
- <P>This shows what a reference to an array looks like.</P>
- <PRE>
- use Devel::Peek;
- $a = [42];
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = RV(0xf041c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xb2850
- SV = PVAV(0xbd448)
- REFCNT = 1
- FLAGS = ()
- IV = 0
- NV = 0
- ARRAY = 0xb2048
- ALLOC = 0xb2048
- FILL = 0
- MAX = 0
- ARYLEN = 0x0
- FLAGS = (REAL)
- Elt No. 0 0xb5658
- SV = IV(0xbe860)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 42</PRE>
- <P>This says <CODE>$a</CODE> is an SV and that it is an RV. That RV points to
- another SV which is a PVAV, an array. The array has one element,
- element zero, which is another SV. The field <CODE>FILL</CODE> above indicates
- the last element in the array, similar to <CODE>$#$a</CODE>.</P>
- <P>If <CODE>$a</CODE> pointed to an array of two elements then we would see the
- following.</P>
- <PRE>
- use Devel::Peek 'Dump';
- $a = [42,24];
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = RV(0xf041c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xb2850
- SV = PVAV(0xbd448)
- REFCNT = 1
- FLAGS = ()
- IV = 0
- NV = 0
- ARRAY = 0xb2048
- ALLOC = 0xb2048
- FILL = 0
- MAX = 0
- ARYLEN = 0x0
- FLAGS = (REAL)
- Elt No. 0 0xb5658
- SV = IV(0xbe860)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 42
- Elt No. 1 0xb5680
- SV = IV(0xbe818)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 24</PRE>
- <P>Note that <CODE>Dump</CODE> will not report <EM>all</EM> the elements in the array,
- only several first (depending on how deep it already went into the
- report tree).</P>
- <P>
- <H2><A NAME="a reference to a hash">A reference to a hash</A></H2>
- <P>The following shows the raw form of a reference to a hash.</P>
- <PRE>
- use Devel::Peek;
- $a = {hello=>42};
- Dump $a;</PRE>
- <P>The output:</P>
- <PRE>
- SV = RV(0xf041c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xb2850
- SV = PVHV(0xbd448)
- REFCNT = 1
- FLAGS = ()
- NV = 0
- ARRAY = 0xbd748
- KEYS = 1
- FILL = 1
- MAX = 7
- RITER = -1
- EITER = 0x0
- Elt "hello" => 0xbaaf0
- SV = IV(0xbe860)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 42</PRE>
- <P>This shows <CODE>$a</CODE> is a reference pointing to an SV. That SV is a PVHV, a
- hash. Fields RITER and EITER are used by <A HREF="../../lib/Pod/perlfunc.html#item_each"><CODE>each</CODE></A>.</P>
- <P>
- <H2><A NAME="dumping a large array or hash">Dumping a large array or hash</A></H2>
- <P>The <CODE>Dump()</CODE> function, by default, dumps up to 4 elements from a
- toplevel array or hash. This number can be increased by supplying a
- second argument to the function.</P>
- <PRE>
- use Devel::Peek;
- $a = [10,11,12,13,14];
- Dump $a;</PRE>
- <P>Notice that <CODE>Dump()</CODE> prints only elements 10 through 13 in the above code.
- The following code will print all of the elements.</P>
- <PRE>
- use Devel::Peek 'Dump';
- $a = [10,11,12,13,14];
- Dump $a, 5;</PRE>
- <P>
- <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>
- <P>This is what you really need to know as an XS programmer, of course. When
- an XSUB returns a pointer to a C structure that pointer is stored in an SV
- and a reference to that SV is placed on the XSUB stack. So the output from
- an XSUB which uses something like the T_PTROBJ map might look something like
- this:</P>
- <PRE>
- SV = RV(0xf381c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xb8ad8
- SV = PVMG(0xbb3c8)
- REFCNT = 1
- FLAGS = (OBJECT,IOK,pIOK)
- IV = 729160
- NV = 0
- PV = 0
- STASH = 0xc1d10 "CookBookB::Opaque"</PRE>
- <P>This shows that we have an SV which is an RV. That RV points at another
- SV. In this case that second SV is a PVMG, a blessed scalar. Because it is
- blessed it has the <CODE>OBJECT</CODE> flag set. Note that an SV which holds a C
- pointer also has the <CODE>IOK</CODE> flag set. The <CODE>STASH</CODE> is set to the package
- name which this SV was blessed into.</P>
- <P>The output from an XSUB which uses something like the T_PTRREF map, which
- doesn't bless the object, might look something like this:</P>
- <PRE>
- SV = RV(0xf381c)
- REFCNT = 1
- FLAGS = (ROK)
- RV = 0xb8ad8
- SV = PVMG(0xbb3c8)
- REFCNT = 1
- FLAGS = (IOK,pIOK)
- IV = 729160
- NV = 0
- PV = 0</PRE>
- <P>
- <H2><A NAME="a reference to a subroutine">A reference to a subroutine</A></H2>
- <P>Looks like this:</P>
- <PRE>
- SV = RV(0x798ec)
- REFCNT = 1
- FLAGS = (TEMP,ROK)
- RV = 0x1d453c
- SV = PVCV(0x1c768c)
- REFCNT = 2
- FLAGS = ()
- IV = 0
- NV = 0
- COMP_STASH = 0x31068 "main"
- START = 0xb20e0
- ROOT = 0xbece0
- XSUB = 0x0
- XSUBANY = 0
- GVGV::GV = 0x1d44e8 "MY" :: "top_targets"
- FILE = "(eval 5)"
- DEPTH = 0
- PADLIST = 0x1c9338</PRE>
- <P>This shows that</P>
- <UL>
- <LI>
- the subroutine is not an XSUB (since <CODE>START</CODE> and <CODE>ROOT</CODE> are
- non-zero, and <CODE>XSUB</CODE> is zero);
- <P></P>
- <LI>
- that it was compiled in the package <CODE>main</CODE>;
- <P></P>
- <LI>
- under the name <CODE>MY::top_targets</CODE>;
- <P></P>
- <LI>
- inside a 5th eval in the program;
- <P></P>
- <LI>
- it is not currently executed (see <CODE>DEPTH</CODE>);
- <P></P>
- <LI>
- it has no prototype (<CODE>PROTOTYPE</CODE> field is missing).
- <P></P></UL>
- <P>
- <HR>
- <H1><A NAME="exports">EXPORTS</A></H1>
- <P><CODE>Dump</CODE>, <CODE>mstat</CODE>, <CODE>DeadCode</CODE>, <CODE>DumpArray</CODE>, <CODE>DumpWithOP</CODE> and
- <CODE>DumpProg</CODE> by default. Additionally available <CODE>SvREFCNT</CODE>,
- <CODE>SvREFCNT_inc</CODE> and <CODE>SvREFCNT_dec</CODE>.</P>
- <P>
- <HR>
- <H1><A NAME="bugs">BUGS</A></H1>
- <P>Readers have been known to skip important parts of <A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>, causing much
- frustration for all.</P>
- <P>
- <HR>
- <H1><A NAME="author">AUTHOR</A></H1>
- <P>Ilya Zakharevich <A HREF="mailto:ilya@math.ohio-state.edu">ilya@math.ohio-state.edu</A></P>
- <P>Copyright (c) 1995-98 Ilya Zakharevich. All rights reserved.
- This program is free software; you can redistribute it and/or
- modify it under the same terms as Perl itself.</P>
- <P>Author of this software makes no claim whatsoever about suitability,
- reliability, edability, editability or usability of this product, and
- should not be kept liable for any damage resulting from the use of
- it. If you can use it, you are in luck, if not, I should not be kept
- responsible. Keep a handy copy of your backup tape at hand.</P>
- <P>
- <HR>
- <H1><A NAME="see also">SEE ALSO</A></H1>
- <P><A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>, and <A HREF="../../lib/Pod/perlguts.html">the perlguts manpage</A>, again.</P>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> Devel::Peek - A data debugging tool for the XS programmer</P></STRONG>
- </TD></TR>
- </TABLE>
-
- </BODY>
-
- </HTML>
-