peek.html

来自「perl教程」· HTML 代码 · 共 533 行 · 第 1/2 页

HTML
533
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Devel::Peek - A data debugging tool for the XS programmer</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Devel::Peek - A data debugging tool for the XS programmer</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#runtime_debugging">Runtime debugging</a></li>
		<li><a href="#memory_footprint_debugging">Memory footprint debugging</a></li>
	</ul>

	<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>
</p>
<h1><a name="name">NAME</a></h1>
<p>Devel::Peek - A data debugging tool for the XS programmer</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">Dump</span><span class="operator">(</span> <span class="variable">$a</span> <span class="operator">);</span>
        <span class="variable">Dump</span><span class="operator">(</span> <span class="variable">$a</span><span class="operator">,</span> <span class="number">5</span> <span class="operator">);</span>
        <span class="variable">DumpArray</span><span class="operator">(</span> <span class="number">5</span><span class="operator">,</span> <span class="variable">$a</span><span class="operator">,</span> <span class="variable">$b</span><span class="operator">,</span> <span class="operator">...</span> <span class="operator">);</span>
        <span class="variable">mstat</span> <span class="string">"Point 5"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span> <span class="string">':opd=st'</span><span class="operator">;</span>
</pre>
<p>
</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(&quot;marker&quot;)</code> function to report on memory usage
(if perl is compiled with corresponding option).  The function
<code>DeadCode()</code> provides statistics on the data &quot;frozen&quot; 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.</p>
<p>Function <code>DumpArray()</code> allows dumping of multiple values (useful when you
need to analyze 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>If <code>use Devel::Peek</code> directive has a <code>:opd=FLAGS</code> argument,
this switches on debugging of opcode dispatch.  <code>FLAGS</code> should be a
combination of <a href="../../lib/Pod/perlguts.html#item_s"><code>s</code></a>, <code>t</code>, and <code>P</code> (see <strong>-D</strong> flags in <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>).
<code>:opd</code> is a shortcut for <code>:opd=st</code>.</p>
<p>
</p>
<h2><a name="runtime_debugging">Runtime debugging</a></h2>
<p><code>CvGV($cv)</code> return one of the globs associated to a subroutine reference $cv.</p>
<p><code>debug_flags()</code> returns a string representation of <a href="../../lib/Pod/perlvar.html#item___d"><code>$^D</code></a> (similar to
what is allowed for <strong>-D</strong> flag).  When called with a numeric argument,
sets $^D to the corresponding value.  When called with an argument of
the form <code>&quot;flags-flags&quot;</code>, set on/off bits of <a href="../../lib/Pod/perlvar.html#item___d"><code>$^D</code></a> corresponding to
letters before/after <code>-</code>.  (The returned value is for <a href="../../lib/Pod/perlvar.html#item___d"><code>$^D</code></a> before
the modification.)</p>
<p><code>runops_debug()</code> returns true if the current <em>opcode dispatcher</em> 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.)</p>
<p>
</p>
<h2><a name="memory_footprint_debugging">Memory footprint debugging</a></h2>
<p>When perl is compiled with support for memory footprint debugging
(default with Perl's malloc()), Devel::Peek provides an access to this API.</p>
<p>Use <code>mstat()</code> function to emit a memory state statistic to the terminal.
For more information on the format of output of <code>mstat()</code> see
<a href="../../lib/Pod/perldebguts.html#using_c__env_perl_debug_mstats__">Using <code>$ENV{PERL_DEBUG_MSTATS}</code> in the perldebguts manpage</a>.</p>
<p>Three additional functions allow access to this statistic from Perl.
First, use <code>mstats_fillhash(%hash)</code> to get the information contained
in the output of <code>mstat()</code> into %hash. The field of this hash are</p>
<pre>
  minbucket nbuckets sbrk_good sbrk_slack sbrked_remains sbrks start_slack
  topbucket topbucket_ev topbucket_odd total total_chain total_sbrk totfree</pre>
<p>Two additional fields <code>free</code>, <code>used</code> contain array references which
provide per-bucket count of free and used chunks.  Two other fields
<code>mem_size</code>, <code>available_size</code> contain array references which provide
the information about the allocated size and usable size of chunks in
each bucket.  Again, see <a href="../../lib/Pod/perldebguts.html#using_c__env_perl_debug_mstats__">Using <code>$ENV{PERL_DEBUG_MSTATS}</code> in the perldebguts manpage</a>
for details.</p>
<p>Keep in mind that only the first several &quot;odd-numbered&quot; buckets are
used, so the information on size of the &quot;odd-numbered&quot; buckets which are
not used is probably meaningless.</p>
<p>The information in</p>
<pre>
 mem_size available_size minbucket nbuckets</pre>
<p>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 <code>mem_size</code>, <code>available_size</code> is not
updated.</p>
<p><code>fill_mstats($buf)</code> 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
<code>mstats2hash($buf, %hash)</code> to use this information to fill %hash.</p>
<p>All three APIs <code>fill_mstats($buf)</code>, <code>mstats_fillhash(%hash)</code>, and
<code>mstats2hash($buf, %hash)</code> are designed to allocate no memory if used
<em>the second time</em> on the same $buf and/or %hash.</p>
<p>So, if you want to collect memory info in a cycle, you may call</p>
<pre>
  <span class="variable">$#buf</span> <span class="operator">=</span> <span class="number">999</span><span class="operator">;</span>
  <span class="variable">fill_mstats</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span> <span class="keyword">for</span> <span class="variable">@buf</span><span class="operator">;</span>
  <span class="variable">mstats_fillhash</span><span class="operator">(</span><span class="variable">%report</span><span class="operator">,</span> <span class="number">1</span><span class="operator">);</span>          <span class="comment"># Static info too</span>
</pre>
<pre>
  <span class="keyword">foreach</span> <span class="operator">(</span><span class="variable">@buf</span><span class="operator">)</span> <span class="operator">{</span>
    <span class="comment"># Do something...</span>
    <span class="variable">fill_mstats</span> <span class="variable">$_</span><span class="operator">;</span>                     <span class="comment"># Collect statistic</span>
  <span class="operator">}</span>
  <span class="keyword">foreach</span> <span class="operator">(</span><span class="variable">@buf</span><span class="operator">)</span> <span class="operator">{</span>
    <span class="variable">mstats2hash</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">,</span> <span class="variable">%report</span><span class="operator">);</span>           <span class="comment"># Preserve static info</span>
    <span class="comment"># Do something with %report</span>
  <span class="operator">}</span>
</pre>
<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>
</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>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="string">"hello"</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = PVIV(0xbc288)
          REFCNT = 1
          FLAGS = (POK,pPOK)
          IV = 0
          PV = 0xb2048 &quot;hello&quot;\0
          CUR = 5
          LEN = 6</pre>
<p>This says <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> 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>
</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>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="number">42</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = IV(0xbc818)
          REFCNT = 1
          FLAGS = (IOK,pIOK)
          IV = 42</pre>
<p>This says <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> 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>
</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>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="number">42</span><span class="operator">;</span>
        <span class="variable">$b</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$a</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$a</span><span class="operator">;</span>
</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 <a href="../../lib/Pod/perlvar.html#item__b"><code>$b</code></a>
instead of <a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a>.</p>
<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>
        <span class="keyword">use</span> <span class="variable">Devel::Peek</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="number">42</span><span class="operator">;</span>
        <span class="variable">$b</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$a</span><span class="operator">;</span>
        <span class="variable">Dump</span> <span class="variable">$b</span><span class="operator">;</span>
</pre>
<p>The output:</p>
<pre>
        SV = RV(0xf041c)
          REFCNT = 1
          FLAGS = (ROK)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?