perl5db.html
来自「perl教程」· HTML 代码 · 共 1,157 行 · 第 1/5 页
HTML
1,157 行
<p>As you may recall from reading <code>perlfunc</code>, the <a href="../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> operator makes a
temporary copy of a variable in the current scope. When the scope ends, the
old copy is restored. This is often used in the debugger to handle the
automatic stacking of variables during recursive calls:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> foo </span><span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$some_global</span><span class="operator">++;</span>
</pre>
<pre>
<span class="comment"># Do some stuff, then ...</span>
<span class="keyword">return</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>What happens is that on entry to the subroutine, <code>$some_global</code> is localized,
then altered. When the subroutine returns, Perl automatically undoes the
localization, restoring the previous value. Voila, automatic stack management.</p>
<p>The debugger uses this trick a <em>lot</em>. Of particular note is <code>DB::eval</code>,
which lets the debugger get control inside of <a href="#item_eval"><code>eval</code></a>'ed code. The debugger
localizes a saved copy of <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a> inside the subroutine, which allows it to
keep <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a> safe until it <code>DB::eval</code> returns, at which point the previous
value of <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a> is restored. This makes it simple (well, <em>simpler</em>) to keep
track of <a href="../lib/Pod/perlvar.html#item___"><code>$@</code></a> inside <a href="#item_eval"><code>eval</code></a>s which <a href="#item_eval"><code>eval</code></a> other <code>eval's</code>.</p>
<p>In any case, watch for this pattern. It occurs fairly often.</p>
<p>
</p>
<h2><a name="the___trick">The <code>^</code> trick</a></h2>
<p>This is used to cleverly reverse the sense of a logical test depending on
the value of an auxiliary variable. For instance, the debugger's <code>S</code>
(search for subroutines by pattern) allows you to negate the pattern
like this:</p>
<pre>
# Find all non-'foo' subs:
S !/foo/</pre>
<p>Boolean algebra states that the truth table for XOR looks like this:</p>
<ul>
<li><strong><a name="item_0__5e_0__3d_0">0 ^ 0 = 0</a></strong>
<p>(! not present and no match) --> false, don't print</p>
</li>
<li><strong><a name="item_0__5e_1__3d_1">0 ^ 1 = 1</a></strong>
<p>(! not present and matches) --> true, print</p>
</li>
<li><strong><a name="item_1__5e_0__3d_1">1 ^ 0 = 1</a></strong>
<p>(! present and no match) --> true, print</p>
</li>
<li><strong><a name="item_1__5e_1__3d_0">1 ^ 1 = 0</a></strong>
<p>(! present and matches) --> false, don't print</p>
</li>
</ul>
<p>As you can see, the first pair applies when <code>!</code> isn't supplied, and
the second pair applies when it is. The XOR simply allows us to
compact a more complicated if-then-elseif-else into a more elegant
(but perhaps overly clever) single test. After all, it needed this
explanation...</p>
<p>
</p>
<h2><a name="flags__flags__flags">FLAGS, FLAGS, FLAGS</a></h2>
<p>There is a certain C programming legacy in the debugger. Some variables,
such as <a href="#item__single"><code>$single</code></a>, <a href="#item__trace"><code>$trace</code></a>, and <code>$frame</code>, have <em>magical</em> values composed
of 1, 2, 4, etc. (powers of 2) OR'ed together. This allows several pieces
of state to be stored independently in a single scalar.</p>
<p>A test like</p>
<pre>
if ($scalar & 4) ...</pre>
<p>is checking to see if the appropriate bit is on. Since each bit can be
"addressed" independently in this way, <code>$scalar</code> is acting sort of like
an array of bits. Obviously, since the contents of <code>$scalar</code> are just a
bit-pattern, we can save and restore it easily (it will just look like
a number).</p>
<p>The problem, is of course, that this tends to leave magic numbers scattered
all over your program whenever a bit is set, cleared, or checked. So why do
it?</p>
<ul>
<li>
<p>First, doing an arithmetical or bitwise operation on a scalar is
just about the fastest thing you can do in Perl: <code>use constant</code> actually
creates a subroutine call, and array and hash lookups are much slower. Is
this over-optimization at the expense of readability? Possibly, but the
debugger accesses these variables a <em>lot</em>. Any rewrite of the code will
probably have to benchmark alternate implementations and see which is the
best balance of readability and speed, and then document how it actually
works.</p>
</li>
<li>
<p>Second, it's very easy to serialize a scalar number. This is done in
the restart code; the debugger state variables are saved in <a href="../lib/Pod/perlvar.html#item__env"><code>%ENV</code></a> and then
restored when the debugger is restarted. Having them be just numbers makes
this trivial.</p>
</li>
<li>
<p>Third, some of these variables are being shared with the Perl core
smack in the middle of the interpreter's execution loop. It's much faster for
a C program (like the interpreter) to check a bit in a scalar than to access
several different variables (or a Perl array).</p>
</li>
</ul>
<p>
</p>
<h2><a name="what_are_those_xxx_comments_for">What are those <code>XXX</code> comments for?</a></h2>
<p>Any comment containing <code>XXX</code> means that the comment is either somewhat
speculative - it's not exactly clear what a given variable or chunk of
code is doing, or that it is incomplete - the basics may be clear, but the
subtleties are not completely documented.</p>
<p>Send in a patch if you can clear up, fill out, or clarify an <code>XXX</code>.</p>
<p>
</p>
<hr />
<h1><a name="data_structures_maintained_by_core">DATA STRUCTURES MAINTAINED BY CORE</a></h1>
<p>There are a number of special data structures provided to the debugger by
the Perl interpreter.</p>
<p>The array <code>@{$main::{'_<'.$filename}}</code> (aliased locally to <code>@dbline</code> via glob
assignment) contains the text from <a href="#item__filename"><code>$filename</code></a>, with each element
corresponding to a single line of <a href="#item__filename"><code>$filename</code></a>.</p>
<p>The hash <code>%{'_<'.$filename}</code> (aliased locally to <code>%dbline</code> via glob
assignment) contains breakpoints and actions. The keys are line numbers;
you can set individual values, but not the whole hash. The Perl interpreter
uses this hash to determine where breakpoints have been set. Any true value is
considered to be a breakpoint; <code>perl5db.pl</code> uses <code>$break_condition\0$action</code>.
Values are magical in numeric context: 1 if the line is breakable, 0 if not.</p>
<p>The scalar <code>${"_<$filename"}</code> simply contains the string <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="#item_eval"><code>eval</code></a>ed strings looks
like <code>(eval 34)</code> or <code>(re_eval 19)</code>.</p>
<p>
</p>
<hr />
<h1><a name="debugger_startup">DEBUGGER STARTUP</a></h1>
<p>When <code>perl5db.pl</code> starts, it reads an rcfile (<code>perl5db.ini</code> for
non-interactive sessions, <code>.perldb</code> for interactive ones) that can set a number
of options. In addition, this file may define a subroutine <code>&afterinit</code>
that will be executed (in the debugger's context) after the debugger has
initialized itself.</p>
<p>Next, it checks the <code>PERLDB_OPTS</code> environment variable and treats its
contents as the argument of a <a href="../lib/Pod/perlguts.html#item_o"><code>o</code></a> command in the debugger.</p>
<p>
</p>
<h2><a name="startuponly_options">STARTUP-ONLY OPTIONS</a></h2>
<p>The following options can only be specified at startup.
To set them in your rcfile, add a call to
<code>&parse_options("optionName=new_value")</code>.</p>
<ul>
<li><strong><a name="item_tty">TTY</a></strong>
<p>the TTY to use for debugging i/o.</p>
</li>
<li><strong><a name="item_notty">noTTY</a></strong>
<p>if set, goes in NonStop mode. On interrupt, if TTY is not set,
uses the value of noTTY or <em>$HOME/.perldbtty$$</em> to find TTY using
Term::Rendezvous. Current variant is to have the name of TTY in this
file.</p>
</li>
<li><strong><a name="item_readline">ReadLine</a></strong>
<p>If false, a dummy ReadLine is used, so you can debug
ReadLine applications.</p>
</li>
<li><strong><a name="item_nonstop">NonStop</a></strong>
<p>if true, no i/o is performed until interrupt.</p>
</li>
<li><strong><a name="item_lineinfo">LineInfo</a></strong>
<p>file or pipe to print line number info to. If it is a
pipe, a short "emacs like" message is used.</p>
</li>
<li><strong><a name="item_remoteport">RemotePort</a></strong>
<p>host:port to connect to on remote host for remote debugging.</p>
</li>
</ul>
<p>
</p>
<h3><a name="sample_rcfile">SAMPLE RCFILE</a></h3>
<pre>
<span class="operator">&</span><span class="variable">parse_options</span><span class="operator">(</span><span class="string">"NonStop=1 LineInfo=db.out"</span><span class="operator">);</span>
<span class="keyword">sub</span><span class="variable"> afterinit </span><span class="operator">{</span> <span class="variable">$trace</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span> <span class="operator">}</span>
</pre>
<p>The script will run without human intervention, putting trace
information into <code>db.out</code>. (If you interrupt it, you had better
reset <a href="#item_lineinfo"><code>LineInfo</code></a> to something <em>interactive</em>!)</p>
<p>
</p>
<hr />
<h1><a name="internals_description">INTERNALS DESCRIPTION</a></h1>
<p>
</p>
<h2><a name="debugger_interface_variables">DEBUGGER INTERFACE VARIABLES</a></h2>
<p>Perl supplies the values for <code>%sub</code>. It effectively inserts
a <code>&DB::DB();</code> in front of each place that can have a
breakpoint. At each subroutine call, it calls <code>&DB::sub</code> with
<code>$DB::sub</code> set to the called subroutine. It also inserts a <code>BEGIN
{require 'perl5db.pl'}</code> before the first line.</p>
<p>After each <a href="../lib/Pod/perlfunc.html#item_require"><code>require</code></a>d file is compiled, but before it is executed, a
call to <code>&DB::postponed($main::{'_<'.$filename})</code> is done. <a href="#item__filename"><code>$filename</code></a>
is the expanded name of the <a href="../lib/Pod/perlfunc.html#item_require"><code>require</code></a>d file (as found via <a href="../lib/Pod/perlvar.html#item__inc"><code>%INC</code></a>).</p>
<p>
</p>
<h3><a name="important_internal_variables">IMPORTANT INTERNAL VARIABLES</a></h3>
<p>
</p>
<h4><a name="_createtty"><a href="#item__createtty"><code>$CreateTTY</code></a></a></h4>
<p>Used to control when the debugger will attempt to acquire another TTY to be
used for input.</p>
<ul>
<li><strong><a name="item_fork">1 - on <code>fork()</code></a></strong>
<li><strong><a name="item_2__2d_debugger_is_started_inside_debugger">2 - debugger is started inside debugger</a></strong>
<li><strong><a name="item_4__2d_on_startup">4 - on startup</a></strong>
</ul>
<p>
</p>
<h4><a name="_doret"><code>$doret</code></a></h4>
<p>The value -2 indicates that no return value should be printed.
Any other positive value causes <code>DB::sub</code> to print return values.</p>
<p>
</p>
<h4><a name="_evalarg"><a href="#item__evalarg"><code>$evalarg</code></a></a></h4>
<p>The item to be eval'ed by <code>DB::eval</code>. Used to prevent messing with the current
contents of <a href="../lib/Pod/perlvar.html#item___"><code>@_</code></a> when <code>DB::eval</code> is called.</p>
<p>
</p>
<h4><a name="_frame"><code>$frame</code></a></h4>
<p>Determines what messages (if any) will get printed when a subroutine (or eval)
is entered or exited.</p>
<ul>
<li><strong><a name="item_0__2d_no_enter_2fexit_messages">0 - No enter/exit messages</a></strong>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?