perldebug.html
来自「perl教程」· HTML 代码 · 共 1,321 行 · 第 1/4 页
HTML
1,321 行
options could be uniquely abbreviated by the first letter (modulo
the <code>Dump*</code> options). It is nevertheless recommended that you
always spell them out in full for legibility and future compatibility.</p>
<p>Other examples include</p>
<pre>
$ PERLDB_OPTS="NonStop LineInfo=listing frame=2" perl -d myprogram</pre>
<p>which runs script non-interactively, printing info on each entry
into a subroutine and each executed line into the file named <em>listing</em>.
(If you interrupt it, you would better reset <a href="#item_lineinfo"><code>LineInfo</code></a> to something
"interactive"!)</p>
<p>Other examples include (using standard shell syntax to show environment
variable settings):</p>
<pre>
$ ( PERLDB_OPTS="NonStop frame=1 AutoTrace LineInfo=tperl.out"
perl -d myprogram )</pre>
<p>which may be useful for debugging a program that uses <code>Term::ReadLine</code>
itself. Do not forget to detach your shell from the TTY in the window that
corresponds to <em>/dev/ttyXX</em>, say, by issuing a command like</p>
<pre>
$ sleep 1000000</pre>
<p>See <a href="../../lib/Pod/perldebguts.html#debugger_internals">Debugger Internals in the perldebguts manpage</a> for details.</p>
<p>
</p>
<h2><a name="debugger_input_output">Debugger input/output</a></h2>
<dl>
<dt><strong><a name="item_prompt">Prompt</a></strong>
<dd>
<p>The debugger prompt is something like</p>
</dd>
<dd>
<pre>
DB<8></pre>
</dd>
<dd>
<p>or even</p>
</dd>
<dd>
<pre>
DB<<17>></pre>
</dd>
<dd>
<p>where that number is the command number, and which you'd use to
access with the built-in <strong>csh</strong>-like history mechanism. For example,
<code>!17</code> would repeat command number 17. The depth of the angle
brackets indicates the nesting depth of the debugger. You could
get more than one set of brackets, for example, if you'd already
at a breakpoint and then printed the result of a function call that
itself has a breakpoint, or you step into an expression via <code>s/n/t
expression</code> command.</p>
</dd>
</li>
<dt><strong><a name="item_multiline_commands">Multiline commands</a></strong>
<dd>
<p>If you want to enter a multi-line command, such as a subroutine
definition with several statements or a format, escape the newline
that would normally end the debugger command with a backslash.
Here's an example:</p>
</dd>
<dd>
<pre>
<span class="variable">DB</span><span class="operator"><</span><span class="number">1</span><span class="operator">></span> <span class="keyword">for</span> <span class="operator">(</span><span class="number">1</span><span class="operator">..</span><span class="number">4</span><span class="operator">)</span> <span class="operator">{</span> <span class="operator">\</span>
<span class="variable">cont</span><span class="operator">:</span> <span class="keyword">print</span> <span class="string">"ok\n"</span><span class="operator">;</span> <span class="operator">\</span>
<span class="variable">cont</span><span class="operator">:</span> <span class="operator">}</span>
<span class="variable">ok</span>
<span class="variable">ok</span>
<span class="variable">ok</span>
<span class="variable">ok</span>
</pre>
</dd>
<dd>
<p>Note that this business of escaping a newline is specific to interactive
commands typed into the debugger.</p>
</dd>
</li>
<dt><strong><a name="item_stack_backtrace">Stack backtrace</a></strong>
<dd>
<p>Here's an example of what a stack backtrace via <a href="#item_t"><code>T</code></a> command might
look like:</p>
</dd>
<dd>
<pre>
$ = main::infested called from file `Ambulation.pm' line 10
@ = Ambulation::legs(1, 2, 3, 4) called from file `camel_flea' line 7
$ = main::pests('bactrian', 4) called from file `camel_flea' line 4</pre>
</dd>
<dd>
<p>The left-hand character up there indicates the context in which the
function was called, with <code>$</code> and <code>@</code> meaning scalar or list
contexts respectively, and <a href="#item__2e"><code>.</code></a> meaning void context (which is
actually a sort of scalar context). The display above says
that you were in the function <code>main::infested</code> when you ran the
stack dump, and that it was called in scalar context from line
10 of the file <em>Ambulation.pm</em>, but without any arguments at all,
meaning it was called as <code>&infested</code>. The next stack frame shows
that the function <code>Ambulation::legs</code> was called in list context
from the <em>camel_flea</em> file with four arguments. The last stack
frame shows that <code>main::pests</code> was called in scalar context,
also from <em>camel_flea</em>, but from line 4.</p>
</dd>
<dd>
<p>If you execute the <a href="#item_t"><code>T</code></a> command from inside an active <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>
statement, the backtrace will contain both a <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> frame and
an <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>) frame.</p>
</dd>
</li>
<dt><strong><a name="item_line_listing_format">Line Listing Format</a></strong>
<dd>
<p>This shows the sorts of output the <a href="#item_l"><code>l</code></a> command can produce:</p>
</dd>
<dd>
<pre>
<span class="variable">DB</span><span class="operator"><<</span><span class="number">13</span><span class="operator">>></span> <span class="variable">l</span>
<span class="number">101</span><span class="operator">:</span> <span class="variable">@i</span><span class="operator">{</span><span class="variable">@i</span><span class="operator">}</span> <span class="operator">=</span> <span class="operator">();</span>
<span class="number">102</span><span class="operator">:</span><span class="variable">b</span> <span class="variable">@isa</span><span class="operator">{</span><span class="variable">@i</span><span class="operator">,</span><span class="variable">$pack</span><span class="operator">}</span> <span class="operator">=</span> <span class="operator">()</span>
<span class="number">103</span> <span class="keyword">if</span><span class="operator">(</span><span class="keyword">exists</span> <span class="variable">$i</span><span class="operator">{</span><span class="variable">$prevpack</span><span class="operator">}</span> <span class="operator">||</span> <span class="keyword">exists</span> <span class="variable">$isa</span><span class="operator">{</span><span class="variable">$pack</span><span class="operator">}</span><span class="operator">);</span>
<span class="number">104</span> <span class="operator">}</span>
<span class="number">105</span>
<span class="number">106</span> <span class="keyword">next</span>
<span class="number">107</span><span class="operator">==></span> <span class="keyword">if</span><span class="operator">(</span><span class="keyword">exists</span> <span class="variable">$isa</span><span class="operator">{</span><span class="variable">$pack</span><span class="operator">}</span><span class="operator">);</span>
<span class="number">108</span>
<span class="number">109</span><span class="operator">:</span><span class="variable">a</span> <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$extra</span><span class="operator">--</span> <span class="operator">></span> <span class="number">0</span><span class="operator">)</span> <span class="operator">{</span>
<span class="number">110</span><span class="operator">:</span> <span class="variable">%isa</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$pack</span><span class="operator">,</span><span class="number">1</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Breakable lines are marked with <code>:</code>. Lines with breakpoints are
marked by <a href="#item_b"><code>b</code></a> and those with actions by <code>a</code>. The line that's
about to be executed is marked by <code>==></code>.</p>
</dd>
<dd>
<p>Please be aware that code in debugger listings may not look the same
as your original source code. Line directives and external source
filters can alter the code before Perl sees it, causing code to move
from its original positions or take on entirely different forms.</p>
</dd>
</li>
<dt><strong><a name="item_frame_listing">Frame listing</a></strong>
<dd>
<p>When the <a href="#item_frame"><code>frame</code></a> option is set, the debugger would print entered (and
optionally exited) subroutines in different styles. See <a href="../../lib/Pod/perldebguts.html">the perldebguts manpage</a>
for incredibly long examples of these.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="debugging_compiletime_statements">Debugging compile-time statements</a></h2>
<p>If you have compile-time executable statements (such as code within
BEGIN and CHECK blocks or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statements), these will <em>not</em> be
stopped by debugger, although <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>s and INIT blocks will, and
compile-time statements can be traced with <a href="#item_autotrace"><code>AutoTrace</code></a> option set
in <code>PERLDB_OPTS</code>). From your own Perl code, however, you can
transfer control back to the debugger using the following statement,
which is harmless if the debugger is not running:</p>
<pre>
<span class="variable">$DB::single</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>If you set <code>$DB::single</code> to 2, it's equivalent to having
just typed the <a href="../../lib/Pod/perlguts.html#item_n"><code>n</code></a> command, whereas a value of 1 means the <a href="../../lib/Pod/perlguts.html#item_s"><code>s</code></a>
command. The <code>$DB::trace</code> variable should be set to 1 to simulate
having typed the <a href="#item_t"><code>t</code></a> command.</p>
<p>Another way to debug compile-time code is to start the debugger, set a
breakpoint on the <em>load</em> of some module:</p>
<pre>
DB<7> b load f:/perllib/lib/Carp.pm
Will stop on load of `f:/perllib/lib/Carp.pm'.</pre>
<p>and then restart the debugger using the <a href="#item_r"><code>R</code></a> command (if possible). One can use <a href="#item_b_compile_subname"><code>b
compile subname</code></a> for the same purpose.</p>
<p>
</p>
<h2><a name="debugger_customization">Debugger Customization</a></h2>
<p>The debugger probably contains enough configuration hooks that you
won't ever have to modify it yourself. You may change the behaviour
of debugger from within the debugger using its <a href="#item_o"><code>o</code></a> command, from
the command line via the <code>PERLDB_OPTS</code> environment variable, and
from customization files.</p>
<p>You can do some customization by setting up a <em>.perldb</em> file, which
contains initialization code. For instance, you could make aliases
like these (the last one is one people expect to be there):</p>
<pre>
<span class="variable">$DB::alias</span><span class="operator">{</span><span class="string">'len'</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'s/^len(.*)/p length($1)/'</span><span class="operator">;</span>
<span class="variable">$DB::alias</span><span class="operator">{</span><span class="string">'stop'</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'s/^stop (at|in)/b/'</span><span class="operator">;</span>
<span class="variable">$DB::alias</span><span class="operator">{</span><span class="string">'ps'</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'s/^ps\b/p scalar /'</span><span class="operator">;</span>
<span class="variable">$DB::alias</span><span class="operator">{</span><span class="string">'quit'</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">'s/^quit(\s*)/exit/'</span><span class="operator">;</span>
</pre>
<p>You can change options from <em>.perldb</em> by using calls like this one;</p>
<pre>
<span class="variable">parse_options</span><span class="operator">(</span><span class="string">"NonStop=1 LineInfo=db.out AutoTrace=1 frame=2"</span><span class="operator">);</span>
</pre>
<p>The code is executed in the package <code>DB</code>. Note that <em>.perldb</em> is
processed before processing <code>PERLDB_OPTS</code>. If <em>.perldb</em> defines the
subroutine <code>afterinit</code>, that function is called after debugger
initialization ends. <em>.perldb</em> may be contained in the current
directory, or in the home directory. Because this file is sourced
in by Perl and may contain arbitrary commands, for security reasons,
it must be owned by the superuser or the current user, and writable
by no one but its owner.</p>
<p>You can mock TTY input to debugger by adding arbitrary commands to
@DB::typeahead. For example, your <em>.perldb</em> file might contain:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> afterinit </span><span class="operator">{</span> <span class="keyword">push</span> <span class="variable">@DB::typeahead</span><span class="operator">,</span> <span class="string">"b 4"</span><span class="operator">,</span> <span class="string">"b 6"</span><span class="operator">;</span> <span class="operator">}</span>
</pre>
<p>Which would attempt to set breakpoints on lines 4 and 6 immediately
after debugger initialization. Note that @DB::typeahead is not a supported
interface and is subject to change in future releases.</p>
<p>If you want to modify the debugger, copy <em>perl5db.pl</em> from the
Perl library to another name and hack it to your heart's content.
You'll then want to set your <a href="../../lib/Pod/perlrun.html#item_perl5db"><code>PERL5DB</code></a> environment variable to say
something like this:</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="string">"myperl5db.pl"</span> <span class="operator">}</span>
</pre>
<p>As a last resort, you could also use <a href="../../lib/Pod/perlrun.html#item_perl5db"><code>PERL5DB</code></a> to customize the debugger
by directly setting internal variables or calling debugger functions.</p>
<p>Note that any variables and functions that are not documented in
this document (or in <a href="../../lib/Pod/perldebguts.html">the perldebguts manpage</a>) are considered for internal
use only, and as such are subject to change without notice.</p>
<p>
</p>
<h2><a name="readline_support">Readline Support</a></h2>
<p>As shipped, the only command-line history supplied is a simplistic one
that checks for leading exclamation points. However, if you install
the Term::ReadKey and Term::ReadLine modules from CPAN, you will
have full editing capabilities much like GNU <em>readline</em>(3) provides.
Look for these in the <em>modules/by-module/Term</em> directory on CPAN.
These do not support normal <strong>vi</strong> command-line editing, however.</p>
<p>A rudimentary command-line completion is also available.
Unfortunately, the names of lexical variables are not available for
completion.</p>
<p>
</p>
<h2><a name="editor_support_for_debugging">Editor Support for Debugging</a></h2>
<p>If you have the FSF's version of <strong>emacs</strong> installed on your system,
it can interact with the Perl debugger to provide an integrated
software development environment reminiscent of its interactions
with C debuggers.</p>
<p>Perl comes with a start file for making <strong>emacs</strong> act like a
syntax-directed editor that understands (some of) Perl's syntax.
Look in the <em>emacs</em> directory of the Perl source distribution.</p>
<p>A similar setup by Tom Christiansen for interacting with any
vendor-shipped <strong>vi</strong> and the X11 window system is also available.
This works similarly to the integrated multiwindow support that
<strong>emacs</strong> provides, where the debugger drives the editor. At the
time of this writing, however, that tool's eventual location in the
Perl distribution was uncertain.</p>
<p>Users of <strong>vi</strong> should also look into <strong>vim</strong> and <strong>gvim</strong>, the mousey
and windy version, for coloring of Perl keywords.</p>
<p>Note that only perl can truly parse Perl, so all such CASE tools
fall somewhat short of the mark, especially if you don't program
your Perl as a C programmer might.</p>
<p>
</p>
<h2><a name="the_perl_profiler">The Perl Profiler</a></h2>
<p>If you wish to supply an alternative debugger for Perl to run, just
invoke your script with a colon and a package argument given to the
<strong>-d</strong> flag. The most popular alternative debuggers for Perl is the
Perl profiler. Devel::DProf is now included with the standard Perl
distribution. To profile your Perl program in the file <em>mycode.pl</em>,
just type:</p>
<pre>
$ perl -d:DProf mycode.pl</pre>
<p>When the script terminates the profiler will dump the profile
information to a file called <em>tmon.out</em>. A tool like <strong>dprofpp</strong>,
also supplied with the standard Perl distribution, can be used to
interpret the information in that profile.</p>
<p>
</p>
<hr />
<h1><a name="debugging_regular_expressions">Debugging regular expressions</a></h1>
<p><code>use re 'debug'</code> enables you to see the gory details of how the Perl
regular expression engine works. In order to understand this typically
voluminous output, one must not only have some idea about how regular
expression matching works in general, but also know how Perl's regular
expressions are internally compiled into an automaton. These matters
are explored in some detail in
<a href="../../lib/Pod/perldebguts.html#debugging_regular_expressions">Debugging regular expressions in the perldebguts manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="debugging_memory_usage">Debugging memory usage</a></h1>
<p>Perl contains internal support for reporting its own memory usage,
but this is a fairly advanced concept that requires some understanding
of how memory allocation works.
See <a href="../../lib/Pod/perldebguts.html#debugging_perl_memory_usage">Debugging Perl memory usage in the perldebguts manpage</a> for the details.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>You did try the <strong>-w</strong> switch, didn't you?</p>
<p><a href="../../lib/Pod/perldebtut.html">the perldebtut manpage</a>,
<a href="../../lib/Pod/perldebguts.html">the perldebguts manpage</a>,
<a href="../../lib/re.html">the re manpage</a>,
<a href="../../lib/DB.html">the DB manpage</a>,
<a href="../../lib/Devel/DProf.html">the Devel::DProf manpage</a>,
<em>dprofpp</em>,
<a href="../../lib/Dumpvalue.html">the Dumpvalue manpage</a>,
and
<a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>.</p>
<p>When debugging a script that uses #! and is thus normally found in
$PATH, the -S option causes perl to search $PATH for it, so you don't
have to type the path or <code>which $scriptname</code>.</p>
<pre>
$ perl -Sd foo.pl</pre>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>You cannot get stack frame information or in any fashion debug functions
that were not compiled by Perl, such as those from C or C++ extensions.</p>
<p>If you alter your @_ arguments in a subroutine (such as with <a href="../../lib/Pod/perlfunc.html#item_shift"><code>shift</code></a>
or <a href="../../lib/Pod/perlfunc.html#item_pop"><code>pop</code></a>), the stack backtrace will not show the original values.</p>
<p>The debugger does not currently work in conjunction with the <strong>-W</strong>
command-line switch, because it itself is not free of warnings.</p>
<p>If you're in a slow syscall (like <a href="../../lib/Pod/perlfunc.html#item_wait"><code>wait</code></a>ing, <a href="../../lib/Pod/perlfunc.html#item_accept"><code>accept</code></a>ing, or <a href="../../lib/Pod/perlfunc.html#item_read"><code>read</code></a>ing
from your keyboard or a socket) and haven't set up your own <code>$SIG{INT}</code>
handler, then you won't be able to CTRL-C your way back to the debugger,
because the debugger's own <code>$SIG{INT}</code> handler doesn't understand that
it needs to raise an exception to <code>longjmp(3)</code> out of slow syscalls.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?