xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/pod/perldebug.pod (revision 0:68f95e015346)
1*0Sstevel@tonic-gate=head1 NAME
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateperldebug - Perl debugging
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 DESCRIPTION
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateFirst of all, have you tried using the B<-w> switch?
8*0Sstevel@tonic-gate
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gateIf you're new to the Perl debugger, you may prefer to read
11*0Sstevel@tonic-gateL<perldebtut>, which is a tutorial introduction to the debugger .
12*0Sstevel@tonic-gate
13*0Sstevel@tonic-gate=head1 The Perl Debugger
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gateIf you invoke Perl with the B<-d> switch, your script runs under the
16*0Sstevel@tonic-gatePerl source debugger.  This works like an interactive Perl
17*0Sstevel@tonic-gateenvironment, prompting for debugger commands that let you examine
18*0Sstevel@tonic-gatesource code, set breakpoints, get stack backtraces, change the values of
19*0Sstevel@tonic-gatevariables, etc.  This is so convenient that you often fire up
20*0Sstevel@tonic-gatethe debugger all by itself just to test out Perl constructs
21*0Sstevel@tonic-gateinteractively to see what they do.  For example:
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate    $ perl -d -e 42
24*0Sstevel@tonic-gate
25*0Sstevel@tonic-gateIn Perl, the debugger is not a separate program the way it usually is in the
26*0Sstevel@tonic-gatetypical compiled environment.  Instead, the B<-d> flag tells the compiler
27*0Sstevel@tonic-gateto insert source information into the parse trees it's about to hand off
28*0Sstevel@tonic-gateto the interpreter.  That means your code must first compile correctly
29*0Sstevel@tonic-gatefor the debugger to work on it.  Then when the interpreter starts up, it
30*0Sstevel@tonic-gatepreloads a special Perl library file containing the debugger.
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gateThe program will halt I<right before> the first run-time executable
33*0Sstevel@tonic-gatestatement (but see below regarding compile-time statements) and ask you
34*0Sstevel@tonic-gateto enter a debugger command.  Contrary to popular expectations, whenever
35*0Sstevel@tonic-gatethe debugger halts and shows you a line of code, it always displays the
36*0Sstevel@tonic-gateline it's I<about> to execute, rather than the one it has just executed.
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gateAny command not recognized by the debugger is directly executed
39*0Sstevel@tonic-gate(C<eval>'d) as Perl code in the current package.  (The debugger
40*0Sstevel@tonic-gateuses the DB package for keeping its own state information.)
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gateNote that the said C<eval> is bound by an implicit scope. As a
43*0Sstevel@tonic-gateresult any newly introduced lexical variable or any modified
44*0Sstevel@tonic-gatecapture buffer content is lost after the eval. The debugger is a
45*0Sstevel@tonic-gatenice environment to learn Perl, but if you interactively experiment using
46*0Sstevel@tonic-gatematerial which should be in the same scope, stuff it in one line.
47*0Sstevel@tonic-gate
48*0Sstevel@tonic-gateFor any text entered at the debugger prompt, leading and trailing whitespace
49*0Sstevel@tonic-gateis first stripped before further processing.  If a debugger command
50*0Sstevel@tonic-gatecoincides with some function in your own program, merely precede the
51*0Sstevel@tonic-gatefunction with something that doesn't look like a debugger command, such
52*0Sstevel@tonic-gateas a leading C<;> or perhaps a C<+>, or by wrapping it with parentheses
53*0Sstevel@tonic-gateor braces.
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate=head2 Debugger Commands
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gateThe debugger understands the following commands:
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate=over 12
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate=item h
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gatePrints out a summary help message
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate=item h [command]
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gatePrints out a help message for the given debugger command.
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate=item h h
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gateThe special argument of C<h h> produces the entire help page, which is quite long.
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gateIf the output of the C<h h> command (or any command, for that matter) scrolls
74*0Sstevel@tonic-gatepast your screen, precede the command with a leading pipe symbol so
75*0Sstevel@tonic-gatethat it's run through your pager, as in
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate    DB> |h h
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gateYou may change the pager which is used via C<o pager=...> command.
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate=item p expr
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gateSame as C<print {$DB::OUT} expr> in the current package.  In particular,
85*0Sstevel@tonic-gatebecause this is just Perl's own C<print> function, this means that nested
86*0Sstevel@tonic-gatedata structures and objects are not dumped, unlike with the C<x> command.
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gateThe C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
89*0Sstevel@tonic-gatewhere STDOUT may be redirected to.
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate=item x [maxdepth] expr
92*0Sstevel@tonic-gate
93*0Sstevel@tonic-gateEvaluates its expression in list context and dumps out the result in a
94*0Sstevel@tonic-gatepretty-printed fashion.  Nested data structures are printed out
95*0Sstevel@tonic-gaterecursively, unlike the real C<print> function in Perl.  When dumping
96*0Sstevel@tonic-gatehashes, you'll probably prefer 'x \%h' rather than 'x %h'.
97*0Sstevel@tonic-gateSee L<Dumpvalue> if you'd like to do this yourself.
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gateThe output format is governed by multiple options described under
100*0Sstevel@tonic-gateL<"Configurable Options">.
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gateIf the C<maxdepth> is included, it must be a numeral I<N>; the value is
103*0Sstevel@tonic-gatedumped only I<N> levels deep, as if the C<dumpDepth> option had been
104*0Sstevel@tonic-gatetemporarily set to I<N>.
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate=item V [pkg [vars]]
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gateDisplay all (or some) variables in package (defaulting to C<main>)
109*0Sstevel@tonic-gateusing a data pretty-printer (hashes show their keys and values so
110*0Sstevel@tonic-gateyou see what's what, control characters are made printable, etc.).
111*0Sstevel@tonic-gateMake sure you don't put the type specifier (like C<$>) there, just
112*0Sstevel@tonic-gatethe symbol names, like this:
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gate    V DB filename line
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gateUse C<~pattern> and C<!pattern> for positive and negative regexes.
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gateThis is similar to calling the C<x> command on each applicable var.
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate=item X [vars]
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gateSame as C<V currentpackage [vars]>.
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate=item y [level [vars]]
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gateDisplay all (or some) lexical variables (mnemonic: C<mY> variables)
127*0Sstevel@tonic-gatein the current scope or I<level> scopes higher.  You can limit the
128*0Sstevel@tonic-gatevariables that you see with I<vars> which works exactly as it does
129*0Sstevel@tonic-gatefor the C<V> and C<X> commands.  Requires the C<PadWalker> module
130*0Sstevel@tonic-gateversion 0.08 or higher; will warn if this isn't installed.  Output
131*0Sstevel@tonic-gateis pretty-printed in the same style as for C<V> and the format is
132*0Sstevel@tonic-gatecontrolled by the same options.
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate=item T
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gateProduce a stack backtrace.  See below for details on its output.
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate=item s [expr]
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gateSingle step.  Executes until the beginning of another
141*0Sstevel@tonic-gatestatement, descending into subroutine calls.  If an expression is
142*0Sstevel@tonic-gatesupplied that includes function calls, it too will be single-stepped.
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate=item n [expr]
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gateNext.  Executes over subroutine calls, until the beginning
147*0Sstevel@tonic-gateof the next statement.  If an expression is supplied that includes
148*0Sstevel@tonic-gatefunction calls, those functions will be executed with stops before
149*0Sstevel@tonic-gateeach statement.
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate=item r
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gateContinue until the return from the current subroutine.
154*0Sstevel@tonic-gateDump the return value if the C<PrintRet> option is set (default).
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate=item <CR>
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gateRepeat last C<n> or C<s> command.
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate=item c [line|sub]
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gateContinue, optionally inserting a one-time-only breakpoint
163*0Sstevel@tonic-gateat the specified line or subroutine.
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate=item l
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gateList next window of lines.
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate=item l min+incr
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gateList C<incr+1> lines starting at C<min>.
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate=item l min-max
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gateList lines C<min> through C<max>.  C<l -> is synonymous to C<->.
176*0Sstevel@tonic-gate
177*0Sstevel@tonic-gate=item l line
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gateList a single line.
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate=item l subname
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gateList first window of lines from subroutine.  I<subname> may
184*0Sstevel@tonic-gatebe a variable that contains a code reference.
185*0Sstevel@tonic-gate
186*0Sstevel@tonic-gate=item -
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gateList previous window of lines.
189*0Sstevel@tonic-gate
190*0Sstevel@tonic-gate=item v [line]
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gateView a few lines of code around the current line.
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate=item .
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gateReturn the internal debugger pointer to the line last
197*0Sstevel@tonic-gateexecuted, and print out that line.
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate=item f filename
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gateSwitch to viewing a different file or C<eval> statement.  If I<filename>
202*0Sstevel@tonic-gateis not a full pathname found in the values of %INC, it is considered
203*0Sstevel@tonic-gatea regex.
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gateC<eval>ed strings (when accessible) are considered to be filenames:
206*0Sstevel@tonic-gateC<f (eval 7)> and C<f eval 7\b> access the body of the 7th C<eval>ed string
207*0Sstevel@tonic-gate(in the order of execution).  The bodies of the currently executed C<eval>
208*0Sstevel@tonic-gateand of C<eval>ed strings that define subroutines are saved and thus
209*0Sstevel@tonic-gateaccessible.
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate=item /pattern/
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gateSearch forwards for pattern (a Perl regex); final / is optional.
214*0Sstevel@tonic-gateThe search is case-insensitive by default.
215*0Sstevel@tonic-gate
216*0Sstevel@tonic-gate=item ?pattern?
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gateSearch backwards for pattern; final ? is optional.
219*0Sstevel@tonic-gateThe search is case-insensitive by default.
220*0Sstevel@tonic-gate
221*0Sstevel@tonic-gate=item L [abw]
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gateList (default all) actions, breakpoints and watch expressions
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate=item S [[!]regex]
226*0Sstevel@tonic-gate
227*0Sstevel@tonic-gateList subroutine names [not] matching the regex.
228*0Sstevel@tonic-gate
229*0Sstevel@tonic-gate=item t
230*0Sstevel@tonic-gate
231*0Sstevel@tonic-gateToggle trace mode (see also the C<AutoTrace> option).
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate=item t expr
234*0Sstevel@tonic-gate
235*0Sstevel@tonic-gateTrace through execution of C<expr>.
236*0Sstevel@tonic-gateSee L<perldebguts/"Frame Listing Output Examples"> for examples.
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate=item b
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gateSets breakpoint on current line
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate=item b [line] [condition]
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gateSet a breakpoint before the given line.  If a condition
245*0Sstevel@tonic-gateis specified, it's evaluated each time the statement is reached: a
246*0Sstevel@tonic-gatebreakpoint is taken only if the condition is true.  Breakpoints may
247*0Sstevel@tonic-gateonly be set on lines that begin an executable statement.  Conditions
248*0Sstevel@tonic-gatedon't use C<if>:
249*0Sstevel@tonic-gate
250*0Sstevel@tonic-gate    b 237 $x > 30
251*0Sstevel@tonic-gate    b 237 ++$count237 < 11
252*0Sstevel@tonic-gate    b 33 /pattern/i
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate=item b subname [condition]
255*0Sstevel@tonic-gate
256*0Sstevel@tonic-gateSet a breakpoint before the first line of the named subroutine.  I<subname> may
257*0Sstevel@tonic-gatebe a variable containing a code reference (in this case I<condition>
258*0Sstevel@tonic-gateis not supported).
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate=item b postpone subname [condition]
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gateSet a breakpoint at first line of subroutine after it is compiled.
263*0Sstevel@tonic-gate
264*0Sstevel@tonic-gate=item b load filename
265*0Sstevel@tonic-gate
266*0Sstevel@tonic-gateSet a breakpoint before the first executed line of the I<filename>,
267*0Sstevel@tonic-gatewhich should be a full pathname found amongst the %INC values.
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate=item b compile subname
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gateSets a breakpoint before the first statement executed after the specified
272*0Sstevel@tonic-gatesubroutine is compiled.
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate=item B line
275*0Sstevel@tonic-gate
276*0Sstevel@tonic-gateDelete a breakpoint from the specified I<line>.
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate=item B *
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gateDelete all installed breakpoints.
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate=item a [line] command
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gateSet an action to be done before the line is executed.  If I<line> is
285*0Sstevel@tonic-gateomitted, set an action on the line about to be executed.
286*0Sstevel@tonic-gateThe sequence of steps taken by the debugger is
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate  1. check for a breakpoint at this line
289*0Sstevel@tonic-gate  2. print the line if necessary (tracing)
290*0Sstevel@tonic-gate  3. do any actions associated with that line
291*0Sstevel@tonic-gate  4. prompt user if at a breakpoint or in single-step
292*0Sstevel@tonic-gate  5. evaluate line
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gateFor example, this will print out $foo every time line
295*0Sstevel@tonic-gate53 is passed:
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate    a 53 print "DB FOUND $foo\n"
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gate=item A line
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gateDelete an action from the specified line.
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate=item A *
304*0Sstevel@tonic-gate
305*0Sstevel@tonic-gateDelete all installed actions.
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate=item w expr
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gateAdd a global watch-expression.  We hope you know what one of these
310*0Sstevel@tonic-gateis, because they're supposed to be obvious.
311*0Sstevel@tonic-gate
312*0Sstevel@tonic-gate=item W expr
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gateDelete watch-expression
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate=item W *
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gateDelete all watch-expressions.
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate=item o
321*0Sstevel@tonic-gate
322*0Sstevel@tonic-gateDisplay all options
323*0Sstevel@tonic-gate
324*0Sstevel@tonic-gate=item o booloption ...
325*0Sstevel@tonic-gate
326*0Sstevel@tonic-gateSet each listed Boolean option to the value C<1>.
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate=item o anyoption? ...
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gatePrint out the value of one or more options.
331*0Sstevel@tonic-gate
332*0Sstevel@tonic-gate=item o option=value ...
333*0Sstevel@tonic-gate
334*0Sstevel@tonic-gateSet the value of one or more options.  If the value has internal
335*0Sstevel@tonic-gatewhitespace, it should be quoted.  For example, you could set C<o
336*0Sstevel@tonic-gatepager="less -MQeicsNfr"> to call B<less> with those specific options.
337*0Sstevel@tonic-gateYou may use either single or double quotes, but if you do, you must
338*0Sstevel@tonic-gateescape any embedded instances of same sort of quote you began with,
339*0Sstevel@tonic-gateas well as any escaping any escapes that immediately precede that
340*0Sstevel@tonic-gatequote but which are not meant to escape the quote itself.  In other
341*0Sstevel@tonic-gatewords, you follow single-quoting rules irrespective of the quote;
342*0Sstevel@tonic-gateeg: C<o option='this isn\'t bad'> or C<o option="She said, \"Isn't
343*0Sstevel@tonic-gateit?\"">.
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gateFor historical reasons, the C<=value> is optional, but defaults to
346*0Sstevel@tonic-gate1 only where it is safe to do so--that is, mostly for Boolean
347*0Sstevel@tonic-gateoptions.  It is always better to assign a specific value using C<=>.
348*0Sstevel@tonic-gateThe C<option> can be abbreviated, but for clarity probably should
349*0Sstevel@tonic-gatenot be.  Several options can be set together.  See L<"Configurable Options">
350*0Sstevel@tonic-gatefor a list of these.
351*0Sstevel@tonic-gate
352*0Sstevel@tonic-gate=item < ?
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gateList out all pre-prompt Perl command actions.
355*0Sstevel@tonic-gate
356*0Sstevel@tonic-gate=item < [ command ]
357*0Sstevel@tonic-gate
358*0Sstevel@tonic-gateSet an action (Perl command) to happen before every debugger prompt.
359*0Sstevel@tonic-gateA multi-line command may be entered by backslashing the newlines.
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate=item < *
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gateDelete all pre-prompt Perl command actions.
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate=item << command
366*0Sstevel@tonic-gate
367*0Sstevel@tonic-gateAdd an action (Perl command) to happen before every debugger prompt.
368*0Sstevel@tonic-gateA multi-line command may be entered by backwhacking the newlines.
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate=item > ?
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gateList out post-prompt Perl command actions.
373*0Sstevel@tonic-gate
374*0Sstevel@tonic-gate=item > command
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gateSet an action (Perl command) to happen after the prompt when you've
377*0Sstevel@tonic-gatejust given a command to return to executing the script.  A multi-line
378*0Sstevel@tonic-gatecommand may be entered by backslashing the newlines (we bet you
379*0Sstevel@tonic-gatecouldn't've guessed this by now).
380*0Sstevel@tonic-gate
381*0Sstevel@tonic-gate=item > *
382*0Sstevel@tonic-gate
383*0Sstevel@tonic-gateDelete all post-prompt Perl command actions.
384*0Sstevel@tonic-gate
385*0Sstevel@tonic-gate=item >> command
386*0Sstevel@tonic-gate
387*0Sstevel@tonic-gateAdds an action (Perl command) to happen after the prompt when you've
388*0Sstevel@tonic-gatejust given a command to return to executing the script.  A multi-line
389*0Sstevel@tonic-gatecommand may be entered by backslashing the newlines.
390*0Sstevel@tonic-gate
391*0Sstevel@tonic-gate=item { ?
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gateList out pre-prompt debugger commands.
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gate=item { [ command ]
396*0Sstevel@tonic-gate
397*0Sstevel@tonic-gateSet an action (debugger command) to happen before every debugger prompt.
398*0Sstevel@tonic-gateA multi-line command may be entered in the customary fashion.
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gateBecause this command is in some senses new, a warning is issued if
401*0Sstevel@tonic-gateyou appear to have accidentally entered a block instead.  If that's
402*0Sstevel@tonic-gatewhat you mean to do, write it as with C<;{ ... }> or even
403*0Sstevel@tonic-gateC<do { ... }>.
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate=item { *
406*0Sstevel@tonic-gate
407*0Sstevel@tonic-gateDelete all pre-prompt debugger commands.
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gate=item {{ command
410*0Sstevel@tonic-gate
411*0Sstevel@tonic-gateAdd an action (debugger command) to happen before every debugger prompt.
412*0Sstevel@tonic-gateA multi-line command may be entered, if you can guess how: see above.
413*0Sstevel@tonic-gate
414*0Sstevel@tonic-gate=item ! number
415*0Sstevel@tonic-gate
416*0Sstevel@tonic-gateRedo a previous command (defaults to the previous command).
417*0Sstevel@tonic-gate
418*0Sstevel@tonic-gate=item ! -number
419*0Sstevel@tonic-gate
420*0Sstevel@tonic-gateRedo number'th previous command.
421*0Sstevel@tonic-gate
422*0Sstevel@tonic-gate=item ! pattern
423*0Sstevel@tonic-gate
424*0Sstevel@tonic-gateRedo last command that started with pattern.
425*0Sstevel@tonic-gateSee C<o recallCommand>, too.
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate=item !! cmd
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gateRun cmd in a subprocess (reads from DB::IN, writes to DB::OUT) See
430*0Sstevel@tonic-gateC<o shellBang>, also.  Note that the user's current shell (well,
431*0Sstevel@tonic-gatetheir C<$ENV{SHELL}> variable) will be used, which can interfere
432*0Sstevel@tonic-gatewith proper interpretation of exit status or signal and coredump
433*0Sstevel@tonic-gateinformation.
434*0Sstevel@tonic-gate
435*0Sstevel@tonic-gate=item source file
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gateRead and execute debugger commands from I<file>.
438*0Sstevel@tonic-gateI<file> may itself contain C<source> commands.
439*0Sstevel@tonic-gate
440*0Sstevel@tonic-gate=item H -number
441*0Sstevel@tonic-gate
442*0Sstevel@tonic-gateDisplay last n commands.  Only commands longer than one character are
443*0Sstevel@tonic-gatelisted.  If I<number> is omitted, list them all.
444*0Sstevel@tonic-gate
445*0Sstevel@tonic-gate=item q or ^D
446*0Sstevel@tonic-gate
447*0Sstevel@tonic-gateQuit.  ("quit" doesn't work for this, unless you've made an alias)
448*0Sstevel@tonic-gateThis is the only supported way to exit the debugger, though typing
449*0Sstevel@tonic-gateC<exit> twice might work.
450*0Sstevel@tonic-gate
451*0Sstevel@tonic-gateSet the C<inhibit_exit> option to 0 if you want to be able to step
452*0Sstevel@tonic-gateoff the end the script.  You may also need to set $finished to 0
453*0Sstevel@tonic-gateif you want to step through global destruction.
454*0Sstevel@tonic-gate
455*0Sstevel@tonic-gate=item R
456*0Sstevel@tonic-gate
457*0Sstevel@tonic-gateRestart the debugger by C<exec()>ing a new session.  We try to maintain
458*0Sstevel@tonic-gateyour history across this, but internal settings and command-line options
459*0Sstevel@tonic-gatemay be lost.
460*0Sstevel@tonic-gate
461*0Sstevel@tonic-gateThe following setting are currently preserved: history, breakpoints,
462*0Sstevel@tonic-gateactions, debugger options, and the Perl command-line
463*0Sstevel@tonic-gateoptions B<-w>, B<-I>, and B<-e>.
464*0Sstevel@tonic-gate
465*0Sstevel@tonic-gate=item |dbcmd
466*0Sstevel@tonic-gate
467*0Sstevel@tonic-gateRun the debugger command, piping DB::OUT into your current pager.
468*0Sstevel@tonic-gate
469*0Sstevel@tonic-gate=item ||dbcmd
470*0Sstevel@tonic-gate
471*0Sstevel@tonic-gateSame as C<|dbcmd> but DB::OUT is temporarily C<select>ed as well.
472*0Sstevel@tonic-gate
473*0Sstevel@tonic-gate=item = [alias value]
474*0Sstevel@tonic-gate
475*0Sstevel@tonic-gateDefine a command alias, like
476*0Sstevel@tonic-gate
477*0Sstevel@tonic-gate    = quit q
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gateor list current aliases.
480*0Sstevel@tonic-gate
481*0Sstevel@tonic-gate=item command
482*0Sstevel@tonic-gate
483*0Sstevel@tonic-gateExecute command as a Perl statement.  A trailing semicolon will be
484*0Sstevel@tonic-gatesupplied.  If the Perl statement would otherwise be confused for a
485*0Sstevel@tonic-gatePerl debugger, use a leading semicolon, too.
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate=item m expr
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gateList which methods may be called on the result of the evaluated
490*0Sstevel@tonic-gateexpression.  The expression may evaluated to a reference to a
491*0Sstevel@tonic-gateblessed object, or to a package name.
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gate=item M
494*0Sstevel@tonic-gate
495*0Sstevel@tonic-gateDisplays all loaded modules and their versions
496*0Sstevel@tonic-gate
497*0Sstevel@tonic-gate
498*0Sstevel@tonic-gate=item man [manpage]
499*0Sstevel@tonic-gate
500*0Sstevel@tonic-gateDespite its name, this calls your system's default documentation
501*0Sstevel@tonic-gateviewer on the given page, or on the viewer itself if I<manpage> is
502*0Sstevel@tonic-gateomitted.  If that viewer is B<man>, the current C<Config> information
503*0Sstevel@tonic-gateis used to invoke B<man> using the proper MANPATH or S<B<-M>
504*0Sstevel@tonic-gateI<manpath>> option.  Failed lookups of the form C<XXX> that match
505*0Sstevel@tonic-gateknown manpages of the form I<perlXXX> will be retried.  This lets
506*0Sstevel@tonic-gateyou type C<man debug> or C<man op> from the debugger.
507*0Sstevel@tonic-gate
508*0Sstevel@tonic-gateOn systems traditionally bereft of a usable B<man> command, the
509*0Sstevel@tonic-gatedebugger invokes B<perldoc>.  Occasionally this determination is
510*0Sstevel@tonic-gateincorrect due to recalcitrant vendors or rather more felicitously,
511*0Sstevel@tonic-gateto enterprising users.  If you fall into either category, just
512*0Sstevel@tonic-gatemanually set the $DB::doccmd variable to whatever viewer to view
513*0Sstevel@tonic-gatethe Perl documentation on your system.  This may be set in an rc
514*0Sstevel@tonic-gatefile, or through direct assignment.  We're still waiting for a
515*0Sstevel@tonic-gateworking example of something along the lines of:
516*0Sstevel@tonic-gate
517*0Sstevel@tonic-gate    $DB::doccmd = 'netscape -remote http://something.here/';
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate=back
520*0Sstevel@tonic-gate
521*0Sstevel@tonic-gate=head2 Configurable Options
522*0Sstevel@tonic-gate
523*0Sstevel@tonic-gateThe debugger has numerous options settable using the C<o> command,
524*0Sstevel@tonic-gateeither interactively or from the environment or an rc file.
525*0Sstevel@tonic-gate(./.perldb or ~/.perldb under Unix.)
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate
528*0Sstevel@tonic-gate=over 12
529*0Sstevel@tonic-gate
530*0Sstevel@tonic-gate=item C<recallCommand>, C<ShellBang>
531*0Sstevel@tonic-gate
532*0Sstevel@tonic-gateThe characters used to recall command or spawn shell.  By
533*0Sstevel@tonic-gatedefault, both are set to C<!>, which is unfortunate.
534*0Sstevel@tonic-gate
535*0Sstevel@tonic-gate=item C<pager>
536*0Sstevel@tonic-gate
537*0Sstevel@tonic-gateProgram to use for output of pager-piped commands (those beginning
538*0Sstevel@tonic-gatewith a C<|> character.)  By default, C<$ENV{PAGER}> will be used.
539*0Sstevel@tonic-gateBecause the debugger uses your current terminal characteristics
540*0Sstevel@tonic-gatefor bold and underlining, if the chosen pager does not pass escape
541*0Sstevel@tonic-gatesequences through unchanged, the output of some debugger commands
542*0Sstevel@tonic-gatewill not be readable when sent through the pager.
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate=item C<tkRunning>
545*0Sstevel@tonic-gate
546*0Sstevel@tonic-gateRun Tk while prompting (with ReadLine).
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate=item C<signalLevel>, C<warnLevel>, C<dieLevel>
549*0Sstevel@tonic-gate
550*0Sstevel@tonic-gateLevel of verbosity.  By default, the debugger leaves your exceptions
551*0Sstevel@tonic-gateand warnings alone, because altering them can break correctly running
552*0Sstevel@tonic-gateprograms.  It will attempt to print a message when uncaught INT, BUS, or
553*0Sstevel@tonic-gateSEGV signals arrive.  (But see the mention of signals in L<BUGS> below.)
554*0Sstevel@tonic-gate
555*0Sstevel@tonic-gateTo disable this default safe mode, set these values to something higher
556*0Sstevel@tonic-gatethan 0.  At a level of 1, you get backtraces upon receiving any kind
557*0Sstevel@tonic-gateof warning (this is often annoying) or exception (this is
558*0Sstevel@tonic-gateoften valuable).  Unfortunately, the debugger cannot discern fatal
559*0Sstevel@tonic-gateexceptions from non-fatal ones.  If C<dieLevel> is even 1, then your
560*0Sstevel@tonic-gatenon-fatal exceptions are also traced and unceremoniously altered if they
561*0Sstevel@tonic-gatecame from C<eval'd> strings or from any kind of C<eval> within modules
562*0Sstevel@tonic-gateyou're attempting to load.  If C<dieLevel> is 2, the debugger doesn't
563*0Sstevel@tonic-gatecare where they came from:  It usurps your exception handler and prints
564*0Sstevel@tonic-gateout a trace, then modifies all exceptions with its own embellishments.
565*0Sstevel@tonic-gateThis may perhaps be useful for some tracing purposes, but tends to hopelessly
566*0Sstevel@tonic-gatedestroy any program that takes its exception handling seriously.
567*0Sstevel@tonic-gate
568*0Sstevel@tonic-gate=item C<AutoTrace>
569*0Sstevel@tonic-gate
570*0Sstevel@tonic-gateTrace mode (similar to C<t> command, but can be put into
571*0Sstevel@tonic-gateC<PERLDB_OPTS>).
572*0Sstevel@tonic-gate
573*0Sstevel@tonic-gate=item C<LineInfo>
574*0Sstevel@tonic-gate
575*0Sstevel@tonic-gateFile or pipe to print line number info to.  If it is a pipe (say,
576*0Sstevel@tonic-gateC<|visual_perl_db>), then a short message is used.  This is the
577*0Sstevel@tonic-gatemechanism used to interact with a slave editor or visual debugger,
578*0Sstevel@tonic-gatesuch as the special C<vi> or C<emacs> hooks, or the C<ddd> graphical
579*0Sstevel@tonic-gatedebugger.
580*0Sstevel@tonic-gate
581*0Sstevel@tonic-gate=item C<inhibit_exit>
582*0Sstevel@tonic-gate
583*0Sstevel@tonic-gateIf 0, allows I<stepping off> the end of the script.
584*0Sstevel@tonic-gate
585*0Sstevel@tonic-gate=item C<PrintRet>
586*0Sstevel@tonic-gate
587*0Sstevel@tonic-gatePrint return value after C<r> command if set (default).
588*0Sstevel@tonic-gate
589*0Sstevel@tonic-gate=item C<ornaments>
590*0Sstevel@tonic-gate
591*0Sstevel@tonic-gateAffects screen appearance of the command line (see L<Term::ReadLine>).
592*0Sstevel@tonic-gateThere is currently no way to disable these, which can render
593*0Sstevel@tonic-gatesome output illegible on some displays, or with some pagers.
594*0Sstevel@tonic-gateThis is considered a bug.
595*0Sstevel@tonic-gate
596*0Sstevel@tonic-gate=item C<frame>
597*0Sstevel@tonic-gate
598*0Sstevel@tonic-gateAffects the printing of messages upon entry and exit from subroutines.  If
599*0Sstevel@tonic-gateC<frame & 2> is false, messages are printed on entry only. (Printing
600*0Sstevel@tonic-gateon exit might be useful if interspersed with other messages.)
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gateIf C<frame & 4>, arguments to functions are printed, plus context
603*0Sstevel@tonic-gateand caller info.  If C<frame & 8>, overloaded C<stringify> and
604*0Sstevel@tonic-gateC<tie>d C<FETCH> is enabled on the printed arguments.  If C<frame
605*0Sstevel@tonic-gate& 16>, the return value from the subroutine is printed.
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gateThe length at which the argument list is truncated is governed by the
608*0Sstevel@tonic-gatenext option:
609*0Sstevel@tonic-gate
610*0Sstevel@tonic-gate=item C<maxTraceLen>
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gateLength to truncate the argument list when the C<frame> option's
613*0Sstevel@tonic-gatebit 4 is set.
614*0Sstevel@tonic-gate
615*0Sstevel@tonic-gate=item C<windowSize>
616*0Sstevel@tonic-gate
617*0Sstevel@tonic-gateChange the size of code list window (default is 10 lines).
618*0Sstevel@tonic-gate
619*0Sstevel@tonic-gate=back
620*0Sstevel@tonic-gate
621*0Sstevel@tonic-gateThe following options affect what happens with C<V>, C<X>, and C<x>
622*0Sstevel@tonic-gatecommands:
623*0Sstevel@tonic-gate
624*0Sstevel@tonic-gate=over 12
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gate=item C<arrayDepth>, C<hashDepth>
627*0Sstevel@tonic-gate
628*0Sstevel@tonic-gatePrint only first N elements ('' for all).
629*0Sstevel@tonic-gate
630*0Sstevel@tonic-gate=item C<dumpDepth>
631*0Sstevel@tonic-gate
632*0Sstevel@tonic-gateLimit recursion depth to N levels when dumping structures.
633*0Sstevel@tonic-gateNegative values are interpreted as infinity.  Default: infinity.
634*0Sstevel@tonic-gate
635*0Sstevel@tonic-gate=item C<compactDump>, C<veryCompact>
636*0Sstevel@tonic-gate
637*0Sstevel@tonic-gateChange the style of array and hash output.  If C<compactDump>, short array
638*0Sstevel@tonic-gatemay be printed on one line.
639*0Sstevel@tonic-gate
640*0Sstevel@tonic-gate=item C<globPrint>
641*0Sstevel@tonic-gate
642*0Sstevel@tonic-gateWhether to print contents of globs.
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate=item C<DumpDBFiles>
645*0Sstevel@tonic-gate
646*0Sstevel@tonic-gateDump arrays holding debugged files.
647*0Sstevel@tonic-gate
648*0Sstevel@tonic-gate=item C<DumpPackages>
649*0Sstevel@tonic-gate
650*0Sstevel@tonic-gateDump symbol tables of packages.
651*0Sstevel@tonic-gate
652*0Sstevel@tonic-gate=item C<DumpReused>
653*0Sstevel@tonic-gate
654*0Sstevel@tonic-gateDump contents of "reused" addresses.
655*0Sstevel@tonic-gate
656*0Sstevel@tonic-gate=item C<quote>, C<HighBit>, C<undefPrint>
657*0Sstevel@tonic-gate
658*0Sstevel@tonic-gateChange the style of string dump.  The default value for C<quote>
659*0Sstevel@tonic-gateis C<auto>; one can enable double-quotish or single-quotish format
660*0Sstevel@tonic-gateby setting it to C<"> or C<'>, respectively.  By default, characters
661*0Sstevel@tonic-gatewith their high bit set are printed verbatim.
662*0Sstevel@tonic-gate
663*0Sstevel@tonic-gate=item C<UsageOnly>
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gateRudimentary per-package memory usage dump.  Calculates total
666*0Sstevel@tonic-gatesize of strings found in variables in the package.  This does not
667*0Sstevel@tonic-gateinclude lexicals in a module's file scope, or lost in closures.
668*0Sstevel@tonic-gate
669*0Sstevel@tonic-gate=back
670*0Sstevel@tonic-gate
671*0Sstevel@tonic-gateAfter the rc file is read, the debugger reads the C<$ENV{PERLDB_OPTS}>
672*0Sstevel@tonic-gateenvironment variable and parses this as the remainder of a `O ...'
673*0Sstevel@tonic-gateline as one might enter at the debugger prompt.  You may place the
674*0Sstevel@tonic-gateinitialization options C<TTY>, C<noTTY>, C<ReadLine>, and C<NonStop>
675*0Sstevel@tonic-gatethere.
676*0Sstevel@tonic-gate
677*0Sstevel@tonic-gateIf your rc file contains:
678*0Sstevel@tonic-gate
679*0Sstevel@tonic-gate  parse_options("NonStop=1 LineInfo=db.out AutoTrace");
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gatethen your script will run without human intervention, putting trace
682*0Sstevel@tonic-gateinformation into the file I<db.out>.  (If you interrupt it, you'd
683*0Sstevel@tonic-gatebetter reset C<LineInfo> to F</dev/tty> if you expect to see anything.)
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gate=over 12
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate=item C<TTY>
688*0Sstevel@tonic-gate
689*0Sstevel@tonic-gateThe TTY to use for debugging I/O.
690*0Sstevel@tonic-gate
691*0Sstevel@tonic-gate=item C<noTTY>
692*0Sstevel@tonic-gate
693*0Sstevel@tonic-gateIf set, the debugger goes into C<NonStop> mode and will not connect to a TTY.  If
694*0Sstevel@tonic-gateinterrupted (or if control goes to the debugger via explicit setting of
695*0Sstevel@tonic-gate$DB::signal or $DB::single from the Perl script), it connects to a TTY
696*0Sstevel@tonic-gatespecified in the C<TTY> option at startup, or to a tty found at
697*0Sstevel@tonic-gateruntime using the C<Term::Rendezvous> module of your choice.
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gateThis module should implement a method named C<new> that returns an object
700*0Sstevel@tonic-gatewith two methods: C<IN> and C<OUT>.  These should return filehandles to use
701*0Sstevel@tonic-gatefor debugging input and output correspondingly.  The C<new> method should
702*0Sstevel@tonic-gateinspect an argument containing the value of C<$ENV{PERLDB_NOTTY}> at
703*0Sstevel@tonic-gatestartup, or C<".perldbtty$$"> otherwise.  This file is not
704*0Sstevel@tonic-gateinspected for proper ownership, so security hazards are theoretically
705*0Sstevel@tonic-gatepossible.
706*0Sstevel@tonic-gate
707*0Sstevel@tonic-gate=item C<ReadLine>
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gateIf false, readline support in the debugger is disabled in order
710*0Sstevel@tonic-gateto debug applications that themselves use ReadLine.
711*0Sstevel@tonic-gate
712*0Sstevel@tonic-gate=item C<NonStop>
713*0Sstevel@tonic-gate
714*0Sstevel@tonic-gateIf set, the debugger goes into non-interactive mode until interrupted, or
715*0Sstevel@tonic-gateprogrammatically by setting $DB::signal or $DB::single.
716*0Sstevel@tonic-gate
717*0Sstevel@tonic-gate=back
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gateHere's an example of using the C<$ENV{PERLDB_OPTS}> variable:
720*0Sstevel@tonic-gate
721*0Sstevel@tonic-gate    $ PERLDB_OPTS="NonStop frame=2" perl -d myprogram
722*0Sstevel@tonic-gate
723*0Sstevel@tonic-gateThat will run the script B<myprogram> without human intervention,
724*0Sstevel@tonic-gateprinting out the call tree with entry and exit points.  Note that
725*0Sstevel@tonic-gateC<NonStop=1 frame=2> is equivalent to C<N f=2>, and that originally,
726*0Sstevel@tonic-gateoptions could be uniquely abbreviated by the first letter (modulo
727*0Sstevel@tonic-gatethe C<Dump*> options).  It is nevertheless recommended that you
728*0Sstevel@tonic-gatealways spell them out in full for legibility and future compatibility.
729*0Sstevel@tonic-gate
730*0Sstevel@tonic-gateOther examples include
731*0Sstevel@tonic-gate
732*0Sstevel@tonic-gate    $ PERLDB_OPTS="NonStop LineInfo=listing frame=2" perl -d myprogram
733*0Sstevel@tonic-gate
734*0Sstevel@tonic-gatewhich runs script non-interactively, printing info on each entry
735*0Sstevel@tonic-gateinto a subroutine and each executed line into the file named F<listing>.
736*0Sstevel@tonic-gate(If you interrupt it, you would better reset C<LineInfo> to something
737*0Sstevel@tonic-gate"interactive"!)
738*0Sstevel@tonic-gate
739*0Sstevel@tonic-gateOther examples include (using standard shell syntax to show environment
740*0Sstevel@tonic-gatevariable settings):
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gate  $ ( PERLDB_OPTS="NonStop frame=1 AutoTrace LineInfo=tperl.out"
743*0Sstevel@tonic-gate      perl -d myprogram )
744*0Sstevel@tonic-gate
745*0Sstevel@tonic-gatewhich may be useful for debugging a program that uses C<Term::ReadLine>
746*0Sstevel@tonic-gateitself.  Do not forget to detach your shell from the TTY in the window that
747*0Sstevel@tonic-gatecorresponds to F</dev/ttyXX>, say, by issuing a command like
748*0Sstevel@tonic-gate
749*0Sstevel@tonic-gate  $ sleep 1000000
750*0Sstevel@tonic-gate
751*0Sstevel@tonic-gateSee L<perldebguts/"Debugger Internals"> for details.
752*0Sstevel@tonic-gate
753*0Sstevel@tonic-gate=head2 Debugger input/output
754*0Sstevel@tonic-gate
755*0Sstevel@tonic-gate=over 8
756*0Sstevel@tonic-gate
757*0Sstevel@tonic-gate=item Prompt
758*0Sstevel@tonic-gate
759*0Sstevel@tonic-gateThe debugger prompt is something like
760*0Sstevel@tonic-gate
761*0Sstevel@tonic-gate    DB<8>
762*0Sstevel@tonic-gate
763*0Sstevel@tonic-gateor even
764*0Sstevel@tonic-gate
765*0Sstevel@tonic-gate    DB<<17>>
766*0Sstevel@tonic-gate
767*0Sstevel@tonic-gatewhere that number is the command number, and which you'd use to
768*0Sstevel@tonic-gateaccess with the built-in B<csh>-like history mechanism.  For example,
769*0Sstevel@tonic-gateC<!17> would repeat command number 17.  The depth of the angle
770*0Sstevel@tonic-gatebrackets indicates the nesting depth of the debugger.  You could
771*0Sstevel@tonic-gateget more than one set of brackets, for example, if you'd already
772*0Sstevel@tonic-gateat a breakpoint and then printed the result of a function call that
773*0Sstevel@tonic-gateitself has a breakpoint, or you step into an expression via C<s/n/t
774*0Sstevel@tonic-gateexpression> command.
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate=item Multiline commands
777*0Sstevel@tonic-gate
778*0Sstevel@tonic-gateIf you want to enter a multi-line command, such as a subroutine
779*0Sstevel@tonic-gatedefinition with several statements or a format, escape the newline
780*0Sstevel@tonic-gatethat would normally end the debugger command with a backslash.
781*0Sstevel@tonic-gateHere's an example:
782*0Sstevel@tonic-gate
783*0Sstevel@tonic-gate      DB<1> for (1..4) {         \
784*0Sstevel@tonic-gate      cont:     print "ok\n";   \
785*0Sstevel@tonic-gate      cont: }
786*0Sstevel@tonic-gate      ok
787*0Sstevel@tonic-gate      ok
788*0Sstevel@tonic-gate      ok
789*0Sstevel@tonic-gate      ok
790*0Sstevel@tonic-gate
791*0Sstevel@tonic-gateNote that this business of escaping a newline is specific to interactive
792*0Sstevel@tonic-gatecommands typed into the debugger.
793*0Sstevel@tonic-gate
794*0Sstevel@tonic-gate=item Stack backtrace
795*0Sstevel@tonic-gate
796*0Sstevel@tonic-gateHere's an example of what a stack backtrace via C<T> command might
797*0Sstevel@tonic-gatelook like:
798*0Sstevel@tonic-gate
799*0Sstevel@tonic-gate    $ = main::infested called from file `Ambulation.pm' line 10
800*0Sstevel@tonic-gate    @ = Ambulation::legs(1, 2, 3, 4) called from file `camel_flea' line 7
801*0Sstevel@tonic-gate    $ = main::pests('bactrian', 4) called from file `camel_flea' line 4
802*0Sstevel@tonic-gate
803*0Sstevel@tonic-gateThe left-hand character up there indicates the context in which the
804*0Sstevel@tonic-gatefunction was called, with C<$> and C<@> meaning scalar or list
805*0Sstevel@tonic-gatecontexts respectively, and C<.> meaning void context (which is
806*0Sstevel@tonic-gateactually a sort of scalar context).  The display above says
807*0Sstevel@tonic-gatethat you were in the function C<main::infested> when you ran the
808*0Sstevel@tonic-gatestack dump, and that it was called in scalar context from line
809*0Sstevel@tonic-gate10 of the file I<Ambulation.pm>, but without any arguments at all,
810*0Sstevel@tonic-gatemeaning it was called as C<&infested>.  The next stack frame shows
811*0Sstevel@tonic-gatethat the function C<Ambulation::legs> was called in list context
812*0Sstevel@tonic-gatefrom the I<camel_flea> file with four arguments.  The last stack
813*0Sstevel@tonic-gateframe shows that C<main::pests> was called in scalar context,
814*0Sstevel@tonic-gatealso from I<camel_flea>, but from line 4.
815*0Sstevel@tonic-gate
816*0Sstevel@tonic-gateIf you execute the C<T> command from inside an active C<use>
817*0Sstevel@tonic-gatestatement, the backtrace will contain both a C<require> frame and
818*0Sstevel@tonic-gatean C<eval>) frame.
819*0Sstevel@tonic-gate
820*0Sstevel@tonic-gate=item Line Listing Format
821*0Sstevel@tonic-gate
822*0Sstevel@tonic-gateThis shows the sorts of output the C<l> command can produce:
823*0Sstevel@tonic-gate
824*0Sstevel@tonic-gate    DB<<13>> l
825*0Sstevel@tonic-gate  101:                @i{@i} = ();
826*0Sstevel@tonic-gate  102:b               @isa{@i,$pack} = ()
827*0Sstevel@tonic-gate  103                     if(exists $i{$prevpack} || exists $isa{$pack});
828*0Sstevel@tonic-gate  104             }
829*0Sstevel@tonic-gate  105
830*0Sstevel@tonic-gate  106             next
831*0Sstevel@tonic-gate  107==>              if(exists $isa{$pack});
832*0Sstevel@tonic-gate  108
833*0Sstevel@tonic-gate  109:a           if ($extra-- > 0) {
834*0Sstevel@tonic-gate  110:                %isa = ($pack,1);
835*0Sstevel@tonic-gate
836*0Sstevel@tonic-gateBreakable lines are marked with C<:>.  Lines with breakpoints are
837*0Sstevel@tonic-gatemarked by C<b> and those with actions by C<a>.  The line that's
838*0Sstevel@tonic-gateabout to be executed is marked by C<< ==> >>.
839*0Sstevel@tonic-gate
840*0Sstevel@tonic-gatePlease be aware that code in debugger listings may not look the same
841*0Sstevel@tonic-gateas your original source code.  Line directives and external source
842*0Sstevel@tonic-gatefilters can alter the code before Perl sees it, causing code to move
843*0Sstevel@tonic-gatefrom its original positions or take on entirely different forms.
844*0Sstevel@tonic-gate
845*0Sstevel@tonic-gate=item Frame listing
846*0Sstevel@tonic-gate
847*0Sstevel@tonic-gateWhen the C<frame> option is set, the debugger would print entered (and
848*0Sstevel@tonic-gateoptionally exited) subroutines in different styles.  See L<perldebguts>
849*0Sstevel@tonic-gatefor incredibly long examples of these.
850*0Sstevel@tonic-gate
851*0Sstevel@tonic-gate=back
852*0Sstevel@tonic-gate
853*0Sstevel@tonic-gate=head2 Debugging compile-time statements
854*0Sstevel@tonic-gate
855*0Sstevel@tonic-gateIf you have compile-time executable statements (such as code within
856*0Sstevel@tonic-gateBEGIN and CHECK blocks or C<use> statements), these will I<not> be
857*0Sstevel@tonic-gatestopped by debugger, although C<require>s and INIT blocks will, and
858*0Sstevel@tonic-gatecompile-time statements can be traced with C<AutoTrace> option set
859*0Sstevel@tonic-gatein C<PERLDB_OPTS>).  From your own Perl code, however, you can
860*0Sstevel@tonic-gatetransfer control back to the debugger using the following statement,
861*0Sstevel@tonic-gatewhich is harmless if the debugger is not running:
862*0Sstevel@tonic-gate
863*0Sstevel@tonic-gate    $DB::single = 1;
864*0Sstevel@tonic-gate
865*0Sstevel@tonic-gateIf you set C<$DB::single> to 2, it's equivalent to having
866*0Sstevel@tonic-gatejust typed the C<n> command, whereas a value of 1 means the C<s>
867*0Sstevel@tonic-gatecommand.  The C<$DB::trace>  variable should be set to 1 to simulate
868*0Sstevel@tonic-gatehaving typed the C<t> command.
869*0Sstevel@tonic-gate
870*0Sstevel@tonic-gateAnother way to debug compile-time code is to start the debugger, set a
871*0Sstevel@tonic-gatebreakpoint on the I<load> of some module:
872*0Sstevel@tonic-gate
873*0Sstevel@tonic-gate    DB<7> b load f:/perllib/lib/Carp.pm
874*0Sstevel@tonic-gate  Will stop on load of `f:/perllib/lib/Carp.pm'.
875*0Sstevel@tonic-gate
876*0Sstevel@tonic-gateand then restart the debugger using the C<R> command (if possible).  One can use C<b
877*0Sstevel@tonic-gatecompile subname> for the same purpose.
878*0Sstevel@tonic-gate
879*0Sstevel@tonic-gate=head2 Debugger Customization
880*0Sstevel@tonic-gate
881*0Sstevel@tonic-gateThe debugger probably contains enough configuration hooks that you
882*0Sstevel@tonic-gatewon't ever have to modify it yourself.  You may change the behaviour
883*0Sstevel@tonic-gateof debugger from within the debugger using its C<o> command, from
884*0Sstevel@tonic-gatethe command line via the C<PERLDB_OPTS> environment variable, and
885*0Sstevel@tonic-gatefrom customization files.
886*0Sstevel@tonic-gate
887*0Sstevel@tonic-gateYou can do some customization by setting up a F<.perldb> file, which
888*0Sstevel@tonic-gatecontains initialization code.  For instance, you could make aliases
889*0Sstevel@tonic-gatelike these (the last one is one people expect to be there):
890*0Sstevel@tonic-gate
891*0Sstevel@tonic-gate    $DB::alias{'len'}  = 's/^len(.*)/p length($1)/';
892*0Sstevel@tonic-gate    $DB::alias{'stop'} = 's/^stop (at|in)/b/';
893*0Sstevel@tonic-gate    $DB::alias{'ps'}   = 's/^ps\b/p scalar /';
894*0Sstevel@tonic-gate    $DB::alias{'quit'} = 's/^quit(\s*)/exit/';
895*0Sstevel@tonic-gate
896*0Sstevel@tonic-gateYou can change options from F<.perldb> by using calls like this one;
897*0Sstevel@tonic-gate
898*0Sstevel@tonic-gate    parse_options("NonStop=1 LineInfo=db.out AutoTrace=1 frame=2");
899*0Sstevel@tonic-gate
900*0Sstevel@tonic-gateThe code is executed in the package C<DB>.  Note that F<.perldb> is
901*0Sstevel@tonic-gateprocessed before processing C<PERLDB_OPTS>.  If F<.perldb> defines the
902*0Sstevel@tonic-gatesubroutine C<afterinit>, that function is called after debugger
903*0Sstevel@tonic-gateinitialization ends.  F<.perldb> may be contained in the current
904*0Sstevel@tonic-gatedirectory, or in the home directory.  Because this file is sourced
905*0Sstevel@tonic-gatein by Perl and may contain arbitrary commands, for security reasons,
906*0Sstevel@tonic-gateit must be owned by the superuser or the current user, and writable
907*0Sstevel@tonic-gateby no one but its owner.
908*0Sstevel@tonic-gate
909*0Sstevel@tonic-gateYou can mock TTY input to debugger by adding arbitrary commands to
910*0Sstevel@tonic-gate@DB::typeahead. For example, your F<.perldb> file might contain:
911*0Sstevel@tonic-gate
912*0Sstevel@tonic-gate    sub afterinit { push @DB::typeahead, "b 4", "b 6"; }
913*0Sstevel@tonic-gate
914*0Sstevel@tonic-gateWhich would attempt to set breakpoints on lines 4 and 6 immediately
915*0Sstevel@tonic-gateafter debugger initilization. Note that @DB::typeahead is not a supported
916*0Sstevel@tonic-gateinterface and is subject to change in future releases.
917*0Sstevel@tonic-gate
918*0Sstevel@tonic-gateIf you want to modify the debugger, copy F<perl5db.pl> from the
919*0Sstevel@tonic-gatePerl library to another name and hack it to your heart's content.
920*0Sstevel@tonic-gateYou'll then want to set your C<PERL5DB> environment variable to say
921*0Sstevel@tonic-gatesomething like this:
922*0Sstevel@tonic-gate
923*0Sstevel@tonic-gate    BEGIN { require "myperl5db.pl" }
924*0Sstevel@tonic-gate
925*0Sstevel@tonic-gateAs a last resort, you could also use C<PERL5DB> to customize the debugger
926*0Sstevel@tonic-gateby directly setting internal variables or calling debugger functions.
927*0Sstevel@tonic-gate
928*0Sstevel@tonic-gateNote that any variables and functions that are not documented in
929*0Sstevel@tonic-gatethis document (or in L<perldebguts>) are considered for internal
930*0Sstevel@tonic-gateuse only, and as such are subject to change without notice.
931*0Sstevel@tonic-gate
932*0Sstevel@tonic-gate=head2 Readline Support
933*0Sstevel@tonic-gate
934*0Sstevel@tonic-gateAs shipped, the only command-line history supplied is a simplistic one
935*0Sstevel@tonic-gatethat checks for leading exclamation points.  However, if you install
936*0Sstevel@tonic-gatethe Term::ReadKey and Term::ReadLine modules from CPAN, you will
937*0Sstevel@tonic-gatehave full editing capabilities much like GNU I<readline>(3) provides.
938*0Sstevel@tonic-gateLook for these in the F<modules/by-module/Term> directory on CPAN.
939*0Sstevel@tonic-gateThese do not support normal B<vi> command-line editing, however.
940*0Sstevel@tonic-gate
941*0Sstevel@tonic-gateA rudimentary command-line completion is also available.
942*0Sstevel@tonic-gateUnfortunately, the names of lexical variables are not available for
943*0Sstevel@tonic-gatecompletion.
944*0Sstevel@tonic-gate
945*0Sstevel@tonic-gate=head2 Editor Support for Debugging
946*0Sstevel@tonic-gate
947*0Sstevel@tonic-gateIf you have the FSF's version of B<emacs> installed on your system,
948*0Sstevel@tonic-gateit can interact with the Perl debugger to provide an integrated
949*0Sstevel@tonic-gatesoftware development environment reminiscent of its interactions
950*0Sstevel@tonic-gatewith C debuggers.
951*0Sstevel@tonic-gate
952*0Sstevel@tonic-gatePerl comes with a start file for making B<emacs> act like a
953*0Sstevel@tonic-gatesyntax-directed editor that understands (some of) Perl's syntax.
954*0Sstevel@tonic-gateLook in the I<emacs> directory of the Perl source distribution.
955*0Sstevel@tonic-gate
956*0Sstevel@tonic-gateA similar setup by Tom Christiansen for interacting with any
957*0Sstevel@tonic-gatevendor-shipped B<vi> and the X11 window system is also available.
958*0Sstevel@tonic-gateThis works similarly to the integrated multiwindow support that
959*0Sstevel@tonic-gateB<emacs> provides, where the debugger drives the editor.  At the
960*0Sstevel@tonic-gatetime of this writing, however, that tool's eventual location in the
961*0Sstevel@tonic-gatePerl distribution was uncertain.
962*0Sstevel@tonic-gate
963*0Sstevel@tonic-gateUsers of B<vi> should also look into B<vim> and B<gvim>, the mousey
964*0Sstevel@tonic-gateand windy version, for coloring of Perl keywords.
965*0Sstevel@tonic-gate
966*0Sstevel@tonic-gateNote that only perl can truly parse Perl, so all such CASE tools
967*0Sstevel@tonic-gatefall somewhat short of the mark, especially if you don't program
968*0Sstevel@tonic-gateyour Perl as a C programmer might.
969*0Sstevel@tonic-gate
970*0Sstevel@tonic-gate=head2 The Perl Profiler
971*0Sstevel@tonic-gate
972*0Sstevel@tonic-gateIf you wish to supply an alternative debugger for Perl to run, just
973*0Sstevel@tonic-gateinvoke your script with a colon and a package argument given to the
974*0Sstevel@tonic-gateB<-d> flag.  The most popular alternative debuggers for Perl is the
975*0Sstevel@tonic-gatePerl profiler.  Devel::DProf is now included with the standard Perl
976*0Sstevel@tonic-gatedistribution.  To profile your Perl program in the file F<mycode.pl>,
977*0Sstevel@tonic-gatejust type:
978*0Sstevel@tonic-gate
979*0Sstevel@tonic-gate    $ perl -d:DProf mycode.pl
980*0Sstevel@tonic-gate
981*0Sstevel@tonic-gateWhen the script terminates the profiler will dump the profile
982*0Sstevel@tonic-gateinformation to a file called F<tmon.out>.  A tool like B<dprofpp>,
983*0Sstevel@tonic-gatealso supplied with the standard Perl distribution, can be used to
984*0Sstevel@tonic-gateinterpret the information in that profile.
985*0Sstevel@tonic-gate
986*0Sstevel@tonic-gate=head1 Debugging regular expressions
987*0Sstevel@tonic-gate
988*0Sstevel@tonic-gateC<use re 'debug'> enables you to see the gory details of how the Perl
989*0Sstevel@tonic-gateregular expression engine works. In order to understand this typically
990*0Sstevel@tonic-gatevoluminous output, one must not only have some idea about how regular
991*0Sstevel@tonic-gateexpression matching works in general, but also know how Perl's regular
992*0Sstevel@tonic-gateexpressions are internally compiled into an automaton. These matters
993*0Sstevel@tonic-gateare explored in some detail in
994*0Sstevel@tonic-gateL<perldebguts/"Debugging regular expressions">.
995*0Sstevel@tonic-gate
996*0Sstevel@tonic-gate=head1 Debugging memory usage
997*0Sstevel@tonic-gate
998*0Sstevel@tonic-gatePerl contains internal support for reporting its own memory usage,
999*0Sstevel@tonic-gatebut this is a fairly advanced concept that requires some understanding
1000*0Sstevel@tonic-gateof how memory allocation works.
1001*0Sstevel@tonic-gateSee L<perldebguts/"Debugging Perl memory usage"> for the details.
1002*0Sstevel@tonic-gate
1003*0Sstevel@tonic-gate=head1 SEE ALSO
1004*0Sstevel@tonic-gate
1005*0Sstevel@tonic-gateYou did try the B<-w> switch, didn't you?
1006*0Sstevel@tonic-gate
1007*0Sstevel@tonic-gateL<perldebtut>,
1008*0Sstevel@tonic-gateL<perldebguts>,
1009*0Sstevel@tonic-gateL<re>,
1010*0Sstevel@tonic-gateL<DB>,
1011*0Sstevel@tonic-gateL<Devel::DProf>,
1012*0Sstevel@tonic-gateL<dprofpp>,
1013*0Sstevel@tonic-gateL<Dumpvalue>,
1014*0Sstevel@tonic-gateand
1015*0Sstevel@tonic-gateL<perlrun>.
1016*0Sstevel@tonic-gate
1017*0Sstevel@tonic-gate=head1 BUGS
1018*0Sstevel@tonic-gate
1019*0Sstevel@tonic-gateYou cannot get stack frame information or in any fashion debug functions
1020*0Sstevel@tonic-gatethat were not compiled by Perl, such as those from C or C++ extensions.
1021*0Sstevel@tonic-gate
1022*0Sstevel@tonic-gateIf you alter your @_ arguments in a subroutine (such as with C<shift>
1023*0Sstevel@tonic-gateor C<pop>), the stack backtrace will not show the original values.
1024*0Sstevel@tonic-gate
1025*0Sstevel@tonic-gateThe debugger does not currently work in conjunction with the B<-W>
1026*0Sstevel@tonic-gatecommand-line switch, because it itself is not free of warnings.
1027*0Sstevel@tonic-gate
1028*0Sstevel@tonic-gateIf you're in a slow syscall (like C<wait>ing, C<accept>ing, or C<read>ing
1029*0Sstevel@tonic-gatefrom your keyboard or a socket) and haven't set up your own C<$SIG{INT}>
1030*0Sstevel@tonic-gatehandler, then you won't be able to CTRL-C your way back to the debugger,
1031*0Sstevel@tonic-gatebecause the debugger's own C<$SIG{INT}> handler doesn't understand that
1032*0Sstevel@tonic-gateit needs to raise an exception to longjmp(3) out of slow syscalls.
1033