perldebguts.html
来自「perl教程」· HTML 代码 · 共 928 行 · 第 1/4 页
HTML
928 行
<?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>perldebguts - Guts of Perl debugging</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>perldebguts - Guts of Perl debugging</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#debugger_internals">Debugger Internals</a></li>
<ul>
<li><a href="#writing_your_own_debugger">Writing Your Own Debugger</a></li>
<ul>
<li><a href="#environment_variables">Environment Variables</a></li>
<li><a href="#debugger_internal_variables">Debugger internal variables</a></li>
<li><a href="#debugger_customization_functions">Debugger customization functions</a></li>
</ul>
</ul>
<li><a href="#frame_listing_output_examples">Frame Listing Output Examples</a></li>
<li><a href="#debugging_regular_expressions">Debugging regular expressions</a></li>
<ul>
<li><a href="#compiletime_output">Compile-time output</a></li>
<li><a href="#types_of_nodes">Types of nodes</a></li>
<li><a href="#runtime_output">Run-time output</a></li>
</ul>
<li><a href="#debugging_perl_memory_usage">Debugging Perl memory usage</a></li>
<ul>
<li><a href="#using__env_perl_debug_mstats_">Using <code>$ENV{PERL_DEBUG_MSTATS}</code></a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perldebguts - Guts of Perl debugging</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This is not the <code>perldebug(1)</code> manpage, which tells you how to use
the debugger. This manpage describes low-level details concerning
the debugger's internals, which range from difficult to impossible
to understand for anyone who isn't incredibly intimate with Perl's guts.
Caveat lector.</p>
<p>
</p>
<hr />
<h1><a name="debugger_internals">Debugger Internals</a></h1>
<p>Perl has special debugging hooks at compile-time and run-time used
to create debugging environments. These hooks are not to be confused
with the <em>perl -Dxxx</em> command described in <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>, which is
usable only if a special Perl is built per the instructions in the
<em>INSTALL</em> podpage in the Perl source tree.</p>
<p>For example, whenever you call Perl's built-in <a href="../../lib/Pod/perlfunc.html#item_caller"><code>caller</code></a> function
from the package <code>DB</code>, the arguments that the corresponding stack
frame was called with are copied to the <code>@DB::args</code> array. These
mechanisms are enabled by calling Perl with the <strong>-d</strong> switch.
Specifically, the following additional features are enabled
(cf. <a href="../../lib/Pod/perlvar.html#__p">$^P in the perlvar manpage</a>):</p>
<ul>
<li>
<p>Perl inserts the contents of <code>$ENV{PERL5DB}</code> (or <code>BEGIN {require
'perl5db.pl'}</code> if not present) before the first line of your program.</p>
</li>
<li>
<p>Each array <code>@{"_<$filename"}</code> holds the lines of $filename for a
file compiled by Perl. The same is also true for <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>ed strings
that contain subroutines, or which are currently being executed.
The $filename for <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>ed strings looks like <code>(eval 34)</code>.
Code assertions in regexes look like <code>(re_eval 19)</code>.</p>
<p>Values in this array are magical in numeric context: they compare
equal to zero only if the line is not breakable.</p>
</li>
<li>
<p>Each hash <code>%{"_<$filename"}</code> contains breakpoints and actions keyed
by line number. Individual entries (as opposed to the whole hash)
are settable. Perl only cares about Boolean true here, although
the values used by <em>perl5db.pl</em> have the form
<code>"$break_condition\0$action"</code>.</p>
<p>The same holds for evaluated strings that contain subroutines, or
which are currently being executed. The $filename for <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>ed strings
looks like <code>(eval 34)</code> or <code>(re_eval 19)</code>.</p>
</li>
<li>
<p>Each scalar <code>${"_<$filename"}</code> contains <code>"_<$filename"</code>. This is
also the case for evaluated strings that contain subroutines, or
which are currently being executed. The $filename for <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>ed
strings looks like <code>(eval 34)</code> or <code>(re_eval 19)</code>.</p>
</li>
<li>
<p>After each <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>d file is compiled, but before it is executed,
<code>DB::postponed(*{"_<$filename"})</code> is called if the subroutine
<code>DB::postponed</code> exists. Here, the $filename is the expanded name of
the <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>d file, as found in the values of %INC.</p>
</li>
<li>
<p>After each subroutine <code>subname</code> is compiled, the existence of
<code>$DB::postponed{subname}</code> is checked. If this key exists,
<code>DB::postponed(subname)</code> is called if the <code>DB::postponed</code> subroutine
also exists.</p>
</li>
<li>
<p>A hash <code>%DB::sub</code> is maintained, whose keys are subroutine names
and whose values have the form <code>filename:startline-endline</code>.
<code>filename</code> has the form <code>(eval 34)</code> for subroutines defined inside
<a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>s, or <code>(re_eval 19)</code> for those within regex code assertions.</p>
</li>
<li>
<p>When the execution of your program reaches a point that can hold a
breakpoint, the <code>DB::DB()</code> subroutine is called if any of the variables
<code>$DB::trace</code>, <code>$DB::single</code>, or <code>$DB::signal</code> is true. These variables
are not <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>izable. This feature is disabled when executing
inside <code>DB::DB()</code>, including functions called from it
unless <a href="../../lib/Pod/perlvar.html#item___d"><code>$^D & (1<<30)</code></a> is true.</p>
</li>
<li>
<p>When execution of the program reaches a subroutine call, a call to
<code>&DB::sub</code>(<em>args</em>) is made instead, with <code>$DB::sub</code> holding the
name of the called subroutine. (This doesn't happen if the subroutine
was compiled in the <code>DB</code> package.)</p>
</li>
</ul>
<p>Note that if <code>&DB::sub</code> needs external data for it to work, no
subroutine call is possible without it. As an example, the standard
debugger's <code>&DB::sub</code> depends on the <code>$DB::deep</code> variable
(it defines how many levels of recursion deep into the debugger you can go
before a mandatory break). If <code>$DB::deep</code> is not defined, subroutine
calls are not possible, even though <code>&DB::sub</code> exists.</p>
<p>
</p>
<h2><a name="writing_your_own_debugger">Writing Your Own Debugger</a></h2>
<p>
</p>
<h3><a name="environment_variables">Environment Variables</a></h3>
<p>The <a href="../../lib/Pod/perlrun.html#item_perl5db"><code>PERL5DB</code></a> environment variable can be used to define a debugger.
For example, the minimal "working" debugger (it actually doesn't do anything)
consists of one line:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> DB::DB </span><span class="operator">{}</span>
</pre>
<p>It can easily be defined like this:</p>
<pre>
<span class="operator">$ </span><span class="variable">PERL5DB</span><span class="operator">=</span><span class="string">"sub DB::DB {}"</span> <span class="variable">perl</span> <span class="keyword">-d</span> <span class="variable">your</span><span class="operator">-</span><span class="variable">script</span>
</pre>
<p>Another brief debugger, slightly more useful, can be created
with only the line:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> DB::DB </span><span class="operator">{</span><span class="keyword">print</span> <span class="operator">++</span><span class="variable">$i</span><span class="operator">;</span> <span class="keyword">scalar</span> <span class="operator"><</span><span class="variable">STDIN</span><span class="operator">>}</span>
</pre>
<p>This debugger prints a number which increments for each statement
encountered and waits for you to hit a newline before continuing
to the next statement.</p>
<p>The following debugger is actually useful:</p>
<pre>
<span class="operator">{</span>
<span class="keyword">package</span> <span class="variable">DB</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> DB </span><span class="operator">{}</span>
<span class="keyword">sub</span><span class="variable"> sub </span><span class="operator">{</span><span class="keyword">print</span> <span class="operator">++</span><span class="variable">$i</span><span class="operator">,</span> <span class="string">" $sub\n"</span><span class="operator">;</span> <span class="operator">&</span><span class="variable">$sub</span><span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>It prints the sequence number of each subroutine call and the name of the
called subroutine. Note that <code>&DB::sub</code> is being compiled into the
package <code>DB</code> through the use of the <a href="../../lib/Pod/perlfunc.html#item_package"><code>package</code></a> directive.</p>
<p>When it starts, the debugger reads your rc file (<em>./.perldb</em> or
<em>~/.perldb</em> under Unix), which can set important options.
(A subroutine (<code>&afterinit</code>) can be defined here as well; it is executed
after the debugger completes its own initialization.)</p>
<p>After the rc file is read, the debugger reads the PERLDB_OPTS
environment variable and uses it to set debugger options. The
contents of this variable are treated as if they were the argument
of an <code>o ...</code> debugger command (q.v. in <a href="../../lib/Pod/perldebug.html#options">Options in the perldebug manpage</a>).</p>
<p>
</p>
<h3><a name="debugger_internal_variables_in_addition_to_the_file_and_subroutinerelated_variables_mentioned_above__the_debugger_also_maintains_various_magical_internal_variables_">Debugger internal variables
In addition to the file and subroutine-related variables mentioned above,
the debugger also maintains various magical internal variables.</a></h3>
<ul>
<li>
<p><code>@DB::dbline</code> is an alias for <code>@{"::_<current_file"}</code>, which
holds the lines of the currently-selected file (compiled by Perl), either
explicitly chosen with the debugger's <a href="../../lib/Pod/perlguts.html#item_f"><code>f</code></a> command, or implicitly by flow
of execution.</p>
<p>Values in this array are magical in numeric context: they compare
equal to zero only if the line is not breakable.</p>
</li>
<li>
<p><code>%DB::dbline</code>, is an alias for <code>%{"::_<current_file"}</code>, which
contains breakpoints and actions keyed by line number in
the currently-selected file, either explicitly chosen with the
debugger's <a href="../../lib/Pod/perlguts.html#item_f"><code>f</code></a> command, or implicitly by flow of execution.</p>
<p>As previously noted, individual entries (as opposed to the whole hash)
are settable. Perl only cares about Boolean true here, although
the values used by <em>perl5db.pl</em> have the form
<code>"$break_condition\0$action"</code>.</p>
</li>
</ul>
<p>
</p>
<h3><a name="debugger_customization_functions">Debugger customization functions</a></h3>
<p>Some functions are provided to simplify customization.</p>
<ul>
<li>
<p>See <a href="../../lib/Pod/perldebug.html#options">Options in the perldebug manpage</a> for description of options parsed by
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?