source: trunk/essentials/dev-lang/perl/pod/perldebug.pod@ 3310

Last change on this file since 3310 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 35.9 KB
Line 
1=head1 NAME
2X<debug> X<debugger>
3
4perldebug - Perl debugging
5
6=head1 DESCRIPTION
7
8First of all, have you tried using the B<-w> switch?
9
10
11If you're new to the Perl debugger, you may prefer to read
12L<perldebtut>, which is a tutorial introduction to the debugger .
13
14=head1 The Perl Debugger
15
16If you invoke Perl with the B<-d> switch, your script runs under the
17Perl source debugger. This works like an interactive Perl
18environment, prompting for debugger commands that let you examine
19source code, set breakpoints, get stack backtraces, change the values of
20variables, etc. This is so convenient that you often fire up
21the debugger all by itself just to test out Perl constructs
22interactively to see what they do. For example:
23X<-d>
24
25 $ perl -d -e 42
26
27In Perl, the debugger is not a separate program the way it usually is in the
28typical compiled environment. Instead, the B<-d> flag tells the compiler
29to insert source information into the parse trees it's about to hand off
30to the interpreter. That means your code must first compile correctly
31for the debugger to work on it. Then when the interpreter starts up, it
32preloads a special Perl library file containing the debugger.
33
34The program will halt I<right before> the first run-time executable
35statement (but see below regarding compile-time statements) and ask you
36to enter a debugger command. Contrary to popular expectations, whenever
37the debugger halts and shows you a line of code, it always displays the
38line it's I<about> to execute, rather than the one it has just executed.
39
40Any command not recognized by the debugger is directly executed
41(C<eval>'d) as Perl code in the current package. (The debugger
42uses the DB package for keeping its own state information.)
43
44Note that the said C<eval> is bound by an implicit scope. As a
45result any newly introduced lexical variable or any modified
46capture buffer content is lost after the eval. The debugger is a
47nice environment to learn Perl, but if you interactively experiment using
48material which should be in the same scope, stuff it in one line.
49
50For any text entered at the debugger prompt, leading and trailing whitespace
51is first stripped before further processing. If a debugger command
52coincides with some function in your own program, merely precede the
53function with something that doesn't look like a debugger command, such
54as a leading C<;> or perhaps a C<+>, or by wrapping it with parentheses
55or braces.
56
57=head2 Debugger Commands
58
59The debugger understands the following commands:
60
61=over 12
62
63=item h
64X<debugger command, h>
65
66Prints out a summary help message
67
68=item h [command]
69
70Prints out a help message for the given debugger command.
71
72=item h h
73
74The special argument of C<h h> produces the entire help page, which is quite long.
75
76If the output of the C<h h> command (or any command, for that matter) scrolls
77past your screen, precede the command with a leading pipe symbol so
78that it's run through your pager, as in
79
80 DB> |h h
81
82You may change the pager which is used via C<o pager=...> command.
83
84
85=item p expr
86X<debugger command, p>
87
88Same as C<print {$DB::OUT} expr> in the current package. In particular,
89because this is just Perl's own C<print> function, this means that nested
90data structures and objects are not dumped, unlike with the C<x> command.
91
92The C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
93where STDOUT may be redirected to.
94
95=item x [maxdepth] expr
96X<debugger command, x>
97
98Evaluates its expression in list context and dumps out the result in a
99pretty-printed fashion. Nested data structures are printed out
100recursively, unlike the real C<print> function in Perl. When dumping
101hashes, you'll probably prefer 'x \%h' rather than 'x %h'.
102See L<Dumpvalue> if you'd like to do this yourself.
103
104The output format is governed by multiple options described under
105L<"Configurable Options">.
106
107If the C<maxdepth> is included, it must be a numeral I<N>; the value is
108dumped only I<N> levels deep, as if the C<dumpDepth> option had been
109temporarily set to I<N>.
110
111=item V [pkg [vars]]
112X<debugger command, V>
113
114Display all (or some) variables in package (defaulting to C<main>)
115using a data pretty-printer (hashes show their keys and values so
116you see what's what, control characters are made printable, etc.).
117Make sure you don't put the type specifier (like C<$>) there, just
118the symbol names, like this:
119
120 V DB filename line
121
122Use C<~pattern> and C<!pattern> for positive and negative regexes.
123
124This is similar to calling the C<x> command on each applicable var.
125
126=item X [vars]
127X<debugger command, X>
128
129Same as C<V currentpackage [vars]>.
130
131=item y [level [vars]]
132X<debugger command, y>
133
134Display all (or some) lexical variables (mnemonic: C<mY> variables)
135in the current scope or I<level> scopes higher. You can limit the
136variables that you see with I<vars> which works exactly as it does
137for the C<V> and C<X> commands. Requires the C<PadWalker> module
138version 0.08 or higher; will warn if this isn't installed. Output
139is pretty-printed in the same style as for C<V> and the format is
140controlled by the same options.
141
142=item T
143X<debugger command, T> X<backtrace> X<stack, backtrace>
144
145Produce a stack backtrace. See below for details on its output.
146
147=item s [expr]
148X<debugger command, s> X<step>
149
150Single step. Executes until the beginning of another
151statement, descending into subroutine calls. If an expression is
152supplied that includes function calls, it too will be single-stepped.
153
154=item n [expr]
155X<debugger command, n>
156
157Next. Executes over subroutine calls, until the beginning
158of the next statement. If an expression is supplied that includes
159function calls, those functions will be executed with stops before
160each statement.
161
162=item r
163X<debugger command, r>
164
165Continue until the return from the current subroutine.
166Dump the return value if the C<PrintRet> option is set (default).
167
168=item <CR>
169
170Repeat last C<n> or C<s> command.
171
172=item c [line|sub]
173X<debugger command, c>
174
175Continue, optionally inserting a one-time-only breakpoint
176at the specified line or subroutine.
177
178=item l
179X<debugger command, l>
180
181List next window of lines.
182
183=item l min+incr
184
185List C<incr+1> lines starting at C<min>.
186
187=item l min-max
188
189List lines C<min> through C<max>. C<l -> is synonymous to C<->.
190
191=item l line
192
193List a single line.
194
195=item l subname
196
197List first window of lines from subroutine. I<subname> may
198be a variable that contains a code reference.
199
200=item -
201X<debugger command, ->
202
203List previous window of lines.