📄 ch30.htm
字号:
number as less than the <TT><I><FONT FACE="Courier">startLine</FONT></I></TT>
number. To list the source file lines from 20 through 41, try
this command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">l 20-41</FONT></TT>
</BLOCKQUOTE>
<P>
To look at the 10 lines preceding the current command, use the
<TT><FONT FACE="Courier">-</FONT></TT> command. For example, if
you are at line 23, typing in the <TT><FONT FACE="Courier">-</FONT></TT>
command will show lines 13 to 22.
<P>
The <TT><FONT FACE="Courier">w</FONT></TT> command will print
a window of lines of code around the current line by printing
three lines before the current line (or the line number you specify
after it as an option to the <TT><FONT FACE="Courier">w</FONT></TT>
command), followed by the code at the current line, and up to
the next six lines of code. For example, the command
<BLOCKQUOTE>
<TT><FONT FACE="Courier">w 5 </FONT></TT>
</BLOCKQUOTE>
<P>
will print line numbers 2 through 11.
<H2><A NAME="SteppingThroughCode"><B><FONT SIZE=5 COLOR=#FF0000>Stepping
Through Code</FONT></B></A></H2>
<P>
You can step through the source code with the <TT><FONT FACE="Courier">s</FONT></TT>
command. Pressing the <TT><FONT FACE="Courier">s</FONT></TT> key
will successively walk you through each line of the code as it
executes, as shown in Figure 30.3.
<P>
<A HREF="f30-3.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f30-3.gif" ><B>Figure 30.3 : </B><I>Using the step function.</I></A>
<P>
The <TT><FONT FACE="Courier">s</FONT></TT> command steps to the
next line of code. If the line of code is in a module whose code
is accessible, you'll go to it and stop. To step over a function,
use the <TT><FONT FACE="Courier">n</FONT></TT> command, which
goes to the next instruction at the same scope.
<P>
The <TT><FONT FACE="Courier">c</FONT></TT> command executes the
debugged program in continuous mode. That is, the program will
run until either a breakpoint is hit or the program terminates.
To run in continuous mode, simply type <TT><FONT FACE="Courier">c</FONT></TT>
at the debug prompt with no parameters. The <TT><FONT FACE="Courier">c</FONT></TT>
command can also be used to execute from the current line to another
known line number:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">c newLineNumber</FONT></TT>
</BLOCKQUOTE>
<P>
For example, if you are at line 7, to execute from the current
line number (7) to line number 12, you would issue the command
<TT><FONT FACE="Courier">c 12</FONT></TT>.
<H2><A NAME="LookingatValues"><B><FONT SIZE=5 COLOR=#FF0000>Looking
at Values</FONT></B></A></H2>
<P>
To see the values of certain variables in the program, use the
<TT><FONT FACE="Courier">V</FONT></TT> command. Used by itself,
<TT><FONT FACE="Courier">V</FONT></TT> lists all the variables
in scope at this time. Here's the syntax:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">V [<I>package</I> [<I>variable</I>]]</FONT></TT>
</BLOCKQUOTE>
<P>
To look at values in your program, you'll want to look at the
<TT><FONT FACE="Courier">main</FONT></TT> package. For example,
to print the value of <TT><FONT FACE="Courier">$reply</FONT></TT>,
use this command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">V main reply<BR>
$reply = '1'</FONT></TT>
</BLOCKQUOTE>
<P>
Note that the dollar sign before the variable specified to <TT><FONT FACE="Courier">V</FONT></TT>
is not supplied. Therefore, if you specify the command <TT><FONT FACE="Courier">V
main $reply</FONT></TT>, you are actually asking for the value
of <TT><FONT FACE="Courier">$$reply</FONT></TT> and not <TT><FONT FACE="Courier">$reply</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">trace</FONT></TT> option is available
with the <TT><FONT FACE="Courier">t</FONT></TT> toggle command.
Issuing <TT><FONT FACE="Courier">trace</FONT></TT> once turns
it on, and issuing it again turns it off. See Figure 30.4 for
a sample use of the <TT><FONT FACE="Courier">trace</FONT></TT>
command on Listing 30.2. In this example, <TT><FONT FACE="Courier">trace</FONT></TT>
is turned on, and then the <TT><FONT FACE="Courier">c</FONT></TT>
command is issued to run the debugger continuously. In <TT><FONT FACE="Courier">trace</FONT></TT>
mode, the debugger prints out each line of code that executes.
<P>
<A HREF="f30-4.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f30-4.gif" ><B>Figure 30.4 : </B><I>Using the trace command with breakpoints.</I></A>
<P>
The <TT><FONT FACE="Courier">X</FONT></TT> command is helpful
when displaying values of variables in the current package. Remember
that the <TT><FONT FACE="Courier">main</FONT></TT> package is
the default package for a Perl script. Issued by itself with no
options, the <TT><FONT FACE="Courier">X</FONT></TT> command displays
all the variables in the current package. Avoid issuing the <TT><FONT FACE="Courier">X</FONT></TT>
command by itself because it can generate a very long listing
of all the variables in the <TT><FONT FACE="Courier">main</FONT></TT>
package.
<P>
To see the value of a particular variable instead of all the variables,
type the name of the variable after the <TT><FONT FACE="Courier">X</FONT></TT>
command. For example, the following command
<BLOCKQUOTE>
<TT><FONT FACE="Courier">X <I>fileNumber</I> </FONT></TT>
</BLOCKQUOTE>
<P>
will print the value of the <TT><I><FONT FACE="Courier">fileNumber</FONT></I></TT>
variable in the current package. If you have array variables and
scalar variables with the same name in the same package, the <TT><FONT FACE="Courier">X</FONT></TT>
command will display the values of both these variables. For example,
if you have a scalar variable called <TT><FONT FACE="Courier">names</FONT></TT>
and an array called <TT><FONT FACE="Courier">names</FONT></TT>,
the <TT><FONT FACE="Courier">X</FONT></TT> command will show the
values of both variables:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">DB<3><B> X names<BR>
</B>$names = "kamran"<BR>
@names = (<BR>
"kamran"<BR>
"joe"<BR>
"donald"<BR>
)</FONT></TT>
</BLOCKQUOTE>
<H2><A NAME="Breakpoints"><B><FONT SIZE=5 COLOR=#FF0000>Breakpoints</FONT></B></A>
</H2>
<P>
You can place breakpoints at suspect locations in your code and
run the program until one of the specified breakpoints is hit.
Breakpoints can be specified to be hit as soon as the line of
code is about to be executed.
<P>
The <TT><FONT FACE="Courier">c</FONT></TT> command is used to
step forward until either the program stops or a specified breakpoint
is hit. To specify a breakpoint at the current line, use the <TT><FONT FACE="Courier">b</FONT></TT>
command without any parameters. To specify a specific line, use
the command of the form:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">b <I>linenumber</I></FONT></TT>
</BLOCKQUOTE>
<P>
Usually, you use <TT><FONT FACE="Courier">trace</FONT></TT> statements
to see statements between the current execution point and a breakpoint
(refer to Figure 30.4). The program is run in continuous mode
with the <TT><FONT FACE="Courier">c</FONT></TT> command until
it hits a breakpoint. There is a breakpoint in Listing 30.1 that
causes the debugger to stop. The <TT><FONT FACE="Courier">L</FONT></TT>
command is issued in the example to list the breakpoints in the
system.
<P>
Breakpoints can also be specified to occur at the first executable
line of code within a subroutine. Simply use the <TT><FONT FACE="Courier">b</FONT></TT>
command with the name of the subroutine as the first parameter.
For example, to break at the first line of code in the <TT><FONT FACE="Courier">xyc</FONT></TT>
subroutine, try this command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">b xyc</FONT></TT>
</BLOCKQUOTE>
<P>
You can also ask the debugger to look at a condition when a line
is hit with a breakpoint tag on it. If the breakpoint is specified
at a line and the condition is true, the debugger stops; otherwise,
it keeps on going. For example, if you want the debugger to stop
in <TT><FONT FACE="Courier">xyc</FONT></TT> only when the global
<TT><FONT FACE="Courier">$reply</FONT></TT> is <TT><FONT FACE="Courier">1</FONT></TT>,
use this command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">b xyc ($reply == '1')</FONT></TT>
</BLOCKQUOTE>
<P>
To list all breakpoints defined during a debug session, use the
<TT><FONT FACE="Courier">L</FONT></TT> command. If you issue unconditional
breakpoints, you'll see breakpoints listed as this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">break if (1)</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">L</FONT></TT> command will also list
up to the last five executed lines of the program.
<P>
To remove a breakpoint, use the <TT><FONT FACE="Courier">d</FONT></TT>
command and specify the line number to delete. To remove all breakpoints,
use the <TT><FONT FACE="Courier">D</FONT></TT> command. For example,
to delete a breakpoint at line 12, you would issue the command
<TT><FONT FACE="Courier">d 12</FONT></TT>.
<P>
The <TT><FONT FACE="Courier">DB</FONT></TT> package uses the following
sequence to hit breakpoints and evaluate code on each line of
executable code:
<OL>
<LI>Checks to see whether the breakpoint is defined at this line
number. If there is no breakpoint defined for this line, it starts
to process the next line. If there is a break-<BR>
point at this line, the debugger prepares to stop. If the condition
for the defined breakpoint is true, the debugger stops execution
and presents a prompt to the user.
<LI>Checks to see whether the line of code is printable. If so,
it prints the entire line of code (including code spanning multiple
lines).
<LI>Checks to see whether there are any actions defined for this
line and performs these actions. (An <I>action</I> is a set of
Perl commands to be executed.)
<LI>Checks to see whether the stop was due to a breakpoint. If
the condition for the breakpoint is true and a breakpoint has
been marked in this location, the debugger stops and presents
a prompt for user interaction.
<LI>Evaluates the line and gets ready to execute it. Gets user
input if the user is stopping; otherwise, it executes the line
and returns to item 1 in order to process the next line.
</OL>
<H2><A NAME="Actions"><B><FONT SIZE=5 COLOR=#FF0000>Actions</FONT></B></A>
</H2>
<P>
You can specify actions to take when a certain line of code is
executed. This step is very important when you want to print out
values as the program executes (see Figure 30.5). Notice how the
value of <TT><FONT FACE="Courier">reply</FONT></TT> is printed
out when line 73 is reached. The action is defined with this statement:
<P>
<A HREF="f30-5.gif" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/f30-5.gif" ><B>Figure 30.5 : </B><I>Using actions in the debugger.</I></A>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">a 73 print "I am on line 73 and
reply is $reply"</FONT></TT>
</BLOCKQUOTE>
<P>
Notice that you did not have to terminate the action command with
a semicolon. You need to use semicolons only if you have more
than one statement for an action. If you forget to supply the
terminating semicolon, the debugger will supply it for you. In
any event, try to keep actions simple and short. Don't write lengthy
actions unless absolutely necessary; otherwise, you'll slow down
the debugger and clutter up the output on your terminal.
<P>
Actions are not limited to displaying values. For instance, you
can use an action to reset a variable to a known value while in
a loop, using a statement like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">a 73 $reply = 1; print "forced reply
to 1\n";</FONT></TT>
</BLOCKQUOTE>
<P>
To execute statements within the debugged program's space, simply
type the command at the prompt. For example, to explicitly create
and set the value of <TT><FONT FACE="Courier">$kw</FONT></TT>
to <TT><FONT FACE="Courier">2</FONT></TT> in the code, use the
following commands at the <TT><FONT FACE="Courier">DB<></FONT></TT>
prompt:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">DB<1> $kw = 2<BR>
<I>... nothing is printed here ...<BR>
</I>DB<1> print $kw<BR>
2<BR>
DB<1> V main kw<BR>
$kw = '2'</FONT></TT>
</BLOCKQUOTE>
<P>
In this example, the variable <TT><FONT FACE="Courier">$kw</FONT></TT>
is created and defined in the program environment. You cannot
modify the source code in the original program, but you can add
items to the name space.
<P>
In some cases, your program may have redirected its output to
<TT><FONT FACE="Courier">STDOUT</FONT></TT> and therefore whatever
it is printing will not be shown on the console. To evaluate an
expression and print its value out to the console regardless of
how <TT><FONT FACE="Courier">STDOUT</FONT></TT> is redirected,
you can use the <TT><FONT FACE="Courier">p</FONT></TT> command.
The <TT><FONT FACE="Courier">p</FONT></TT> command evaluates an
expression in the current program's environment and prints it
out to the debugger console. Basically, the <TT><FONT FACE="Courier">print</FONT></TT>
command prints the output to wherever <TT><FONT FACE="Courier">STDOUT</FONT></TT>
is redirected, whereas the <TT><FONT FACE="Courier">p</FONT></TT>
command is equivalent to the following <TT><FONT FACE="Courier">print</FONT></TT>
command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">print DB::OUT </FONT></TT>
</BLOCKQUOTE>
<P>
The command above forces output from a <TT><FONT FACE="Courier">print</FONT></TT>
command to where the <TT><FONT FACE="Courier">DB::</FONT></TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -