This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta: Fix typo
[perl5.git] / pod / perldebug.pod
... / ...
CommitLineData
1=head1 NAME
2X<debug> X<debugger>
3
4perldebug - Perl debugging
5
6=head1 DESCRIPTION
7
8First of all, have you tried using L<C<use strict;>|strict> and
9L<C<use warnings;>|warnings>?
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
14If you're looking for the nitty gritty details of how the debugger is
15I<implemented>, you may prefer to read L<perldebguts>.
16
17=head1 The Perl Debugger
18
19If you invoke Perl with the B<-d> switch, your script runs under the
20Perl source debugger. This works like an interactive Perl
21environment, prompting for debugger commands that let you examine
22source code, set breakpoints, get stack backtraces, change the values of
23variables, etc. This is so convenient that you often fire up
24the debugger all by itself just to test out Perl constructs
25interactively to see what they do. For example:
26X<-d>
27
28 $ perl -d -e 42
29
30In Perl, the debugger is not a separate program the way it usually is in the
31typical compiled environment. Instead, the B<-d> flag tells the compiler
32to insert source information into the parse trees it's about to hand off
33to the interpreter. That means your code must first compile correctly
34for the debugger to work on it. Then when the interpreter starts up, it
35preloads a special Perl library file containing the debugger.
36
37The program will halt I<right before> the first run-time executable
38statement (but see below regarding compile-time statements) and ask you
39to enter a debugger command. Contrary to popular expectations, whenever
40the debugger halts and shows you a line of code, it always displays the
41line it's I<about> to execute, rather than the one it has just executed.
42
43Any command not recognized by the debugger is directly executed
44(C<eval>'d) as Perl code in the current package. (The debugger
45uses the DB package for keeping its own state information.)
46
47Note that the said C<eval> is bound by an implicit scope. As a
48result any newly introduced lexical variable or any modified
49capture buffer content is lost after the eval. The debugger is a
50nice environment to learn Perl, but if you interactively experiment using
51material which should be in the same scope, stuff it in one line.
52
53For any text entered at the debugger prompt, leading and trailing whitespace
54is first stripped before further processing. If a debugger command
55coincides with some function in your own program, merely precede the
56function with something that doesn't look like a debugger command, such
57as a leading C<;> or perhaps a C<+>, or by wrapping it with parentheses
58or braces.
59
60=head2 Calling the Debugger
61
62There are several ways to call the debugger:
63
64=over 4
65
66=item perl -d program_name
67
68On the given program identified by C<program_name>.
69
70=item perl -d -e 0
71
72Interactively supply an arbitrary C<expression> using C<-e>.
73
74=item perl -d:ptkdb program_name
75
76Debug a given program via the C<Devel::ptkdb> GUI.
77
78=item perl -dt threaded_program_name
79
80Debug a given program using threads (experimental).
81
82=back
83
84=head2 Debugger Commands
85
86The interactive debugger understands the following commands:
87
88=over 12
89
90=item h
91X<debugger command, h>
92
93Prints out a summary help message
94
95=item h [command]
96
97Prints out a help message for the given debugger command.
98
99=item h h
100
101The special argument of C<h h> produces the entire help page, which is quite long.
102
103If the output of the C<h h> command (or any command, for that matter) scrolls
104past your screen, precede the command with a leading pipe symbol so
105that it's run through your pager, as in
106
107 DB> |h h
108
109You may change the pager which is used via C<o pager=...> command.
110
111=item p expr
112X<debugger command, p>
113
114Same as C<print {$DB::OUT} expr> in the current package. In particular,
115because this is just Perl's own C<print> function, this means that nested
116data structures and objects are not dumped, unlike with the C<x> command.
117
118The C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
119where STDOUT may be redirected to.
120
121=item x [maxdepth] expr
122X<debugger command, x>
123
124Evaluates its expression in list context and dumps out the result in a
125pretty-printed fashion. Nested data structures are printed out
126recursively, unlike the real C<print> function in Perl. When dumping
127hashes, you'll probably prefer 'x \%h' rather than 'x %h'.
128See L<Dumpvalue> if you'd like to do this yourself.
129
130The output format is governed by multiple options described under
131L</"Configurable Options">.
132
133If the C<maxdepth> is included, it must be a numeral I<N>; the value is
134dumped only I<N> levels deep, as if the C<dumpDepth> option had been
135temporarily set to I<N>.
136
137=item V [pkg [vars]]
138X<debugger command, V>
139
140Display all (or some) variables in package (defaulting to C<main>)
141using a data pretty-printer (hashes show their keys and values so
142you see what's what, control characters are made printable, etc.).
143Make sure you don't put the type specifier (like C<$>) there, just
144the symbol names, like this:
145
146 V DB filename line
147
148Use C<~pattern> and C<!pattern> for positive and negative regexes.
149
150This is similar to calling the C<x> command on each applicable var.
151
152=item X [vars]
153X<debugger command, X>
154
155Same as C<V currentpackage [vars]>.
156
157=item y [level [vars]]
158X<debugger command, y>
159
160Display all (or some) lexical variables (mnemonic: C<mY> variables)
161in the current scope or I<level> scopes higher. You can limit the
162variables that you see with I<vars> which works exactly as it does
163for the C<V> and C<X> commands. Requires the C<PadWalker> module
164version 0.08 or higher; will warn if this isn't installed. Output
165is pretty-printed in the same style as for C<V> and the format is
166controlled by the same options.
167
168=item T
169X<debugger command, T> X<backtrace> X<stack, backtrace>
170
171Produce a stack backtrace. See below for details on its output.
172
173=item s [expr]
174X<debugger command, s> X<step>
175
176Single step. Executes until the beginning of another
177statement, descending into subroutine calls. If an expression is
178supplied that includes function calls, it too will be single-stepped.
179
180=item n [expr]
181X<debugger command, n>
182
183Next. Executes over subroutine calls, until the beginning
184of the next statement. If an expression is supplied that includes
185function calls, those functions will be executed with stops before
186each statement.
187
188=item r
189X<debugger command, r>
190
191Continue until the return from the current subroutine.
192Dump the return value if the C<PrintRet> option is set (default).
193
194=item <CR>
195
196Repeat last C<n> or C<s> command.
197
198=item c [line|sub]
199X<debugger command, c>
200
201Continue, optionally inserting a one-time-only breakpoint
202at the specified line or subroutine.
203
204=item l
205X<debugger command, l>
206
207List next window of lines.
208
209=item l min+incr
210
211List C<incr+1> lines starting at C<min>.
212
213=item l min-max
214
215List lines C<min> through C<max>. C<l -> is synonymous to C<->.
216
217=item l line
218
219List a single line.
220
221=item l subname
222
223List first window of lines from subroutine. I<subname> may
224be a variable that contains a code reference.
225
226=item -
227X<debugger command, ->
228
229List previous window of lines.
230
231=item v [line]
232X<debugger command, v>
233
234View a few lines of code around the current line.
235
236=item .
237X<debugger command, .>
238
239Return the internal debugger pointer to the line last
240executed, and print out that line.
241
242=item f filename
243X<debugger command, f>
244
245Switch to viewing a different file or C<eval> statement. If I<filename>
246is not a full pathname found in the values of %INC, it is considered
247a regex.
248
249C<eval>ed strings (when accessible) are considered to be filenames:
250C<f (eval 7)> and C<f eval 7\b> access the body of the 7th C<eval>ed string
251(in the order of execution). The bodies of the currently executed C<eval>
252and of C<eval>ed strings that define subroutines are saved and thus
253accessible.
254
255=item /pattern/
256
257Search forwards for pattern (a Perl regex); final / is optional.
258The search is case-insensitive by default.
259
260=item ?pattern?
261
262Search backwards for pattern; final ? is optional.
263The search is case-insensitive by default.
264
265=item L [abw]
266X<debugger command, L>
267
268List (default all) actions, breakpoints and watch expressions
269
270=item S [[!]regex]
271X<debugger command, S>
272
273List subroutine names [not] matching the regex.
274
275=item t [n]
276X<debugger command, t>
277
278Toggle trace mode (see also the C<AutoTrace> option).
279Optional argument is the maximum number of levels to trace below
280the current one; anything deeper than that will be silent.
281
282=item t [n] expr
283X<debugger command, t>
284
285Trace through execution of C<expr>.
286Optional first argument is the maximum number of levels to trace below
287the current one; anything deeper than that will be silent.
288See L<perldebguts/"Frame Listing Output Examples"> for examples.
289
290=item b
291X<breakpoint>
292X<debugger command, b>
293
294Sets breakpoint on current line
295
296=item b [line] [condition]
297X<breakpoint>
298X<debugger command, b>
299
300Set a breakpoint before the given line. If a condition
301is specified, it's evaluated each time the statement is reached: a
302breakpoint is taken only if the condition is true. Breakpoints may
303only be set on lines that begin an executable statement. Conditions
304don't use C<if>:
305
306 b 237 $x > 30
307 b 237 ++$count237 < 11
308 b 33 /pattern/i
309
310If the line number is C<.>, sets a breakpoint on the current line:
311
312 b . $n > 100
313
314=item b [file]:[line] [condition]
315X<breakpoint>
316X<debugger command, b>
317
318Set a breakpoint before the given line in a (possibly different) file. If a
319condition is specified, it's evaluated each time the statement is reached: a
320breakpoint is taken only if the condition is true. Breakpoints may only be set
321on lines that begin an executable statement. Conditions don't use C<if>:
322
323 b lib/MyModule.pm:237 $x > 30
324 b /usr/lib/perl5/site_perl/CGI.pm:100 ++$count100 < 11
325
326=item b subname [condition]
327X<breakpoint>
328X<debugger command, b>
329
330Set a breakpoint before the first line of the named subroutine. I<subname> may
331be a variable containing a code reference (in this case I<condition>
332is not supported).
333
334=item b postpone subname [condition]
335X<breakpoint>
336X<debugger command, b>
337
338Set a breakpoint at first line of subroutine after it is compiled.
339
340=item b load filename
341X<breakpoint>
342X<debugger command, b>
343
344Set a breakpoint before the first executed line of the I<filename>,
345which should be a full pathname found amongst the %INC values.
346
347=item b compile subname
348X<breakpoint>
349X<debugger command, b>
350
351Sets a breakpoint before the first statement executed after the specified
352subroutine is compiled.
353
354=item B line
355X<breakpoint>
356X<debugger command, B>
357
358Delete a breakpoint from the specified I<line>.
359
360=item B *
361X<breakpoint>
362X<debugger command, B>
363
364Delete all installed breakpoints.
365
366=item disable [file]:[line]
367X<breakpoint>
368X<debugger command, disable>
369X<disable>
370
371Disable the breakpoint so it won't stop the execution of the program.
372Breakpoints are enabled by default and can be re-enabled using the C<enable>
373command.
374
375=item disable [line]
376X<breakpoint>
377X<debugger command, disable>
378X<disable>
379
380Disable the breakpoint so it won't stop the execution of the program.
381Breakpoints are enabled by default and can be re-enabled using the C<enable>
382command.
383
384This is done for a breakpoint in the current file.
385
386=item enable [file]:[line]
387X<breakpoint>
388X<debugger command, disable>
389X<disable>
390
391Enable the breakpoint so it will stop the execution of the program.
392
393=item enable [line]
394X<breakpoint>
395X<debugger command, disable>
396X<disable>
397
398Enable the breakpoint so it will stop the execution of the program.
399
400This is done for a breakpoint in the current file.
401
402=item a [line] command
403X<debugger command, a>
404
405Set an action to be done before the line is executed. If I<line> is
406omitted, set an action on the line about to be executed.
407The sequence of steps taken by the debugger is
408
409 1. check for a breakpoint at this line
410 2. print the line if necessary (tracing)
411 3. do any actions associated with that line
412 4. prompt user if at a breakpoint or in single-step
413 5. evaluate line
414
415For example, this will print out $foo every time line
41653 is passed:
417
418 a 53 print "DB FOUND $foo\n"
419
420=item A line
421X<debugger command, A>
422
423Delete an action from the specified line.
424
425=item A *
426X<debugger command, A>
427
428Delete all installed actions.
429
430=item w expr
431X<debugger command, w>
432
433Add a global watch-expression. Whenever a watched global changes the
434debugger will stop and display the old and new values.
435
436=item W expr
437X<debugger command, W>
438
439Delete watch-expression
440
441=item W *
442X<debugger command, W>
443
444Delete all watch-expressions.
445
446=item o
447X<debugger command, o>
448
449Display all options.
450
451=item o booloption ...
452X<debugger command, o>
453
454Set each listed Boolean option to the value C<1>.
455
456=item o anyoption? ...
457X<debugger command, o>
458
459Print out the value of one or more options.
460
461=item o option=value ...
462X<debugger command, o>
463
464Set the value of one or more options. If the value has internal
465whitespace, it should be quoted. For example, you could set C<o
466pager="less -MQeicsNfr"> to call B<less> with those specific options.
467You may use either single or double quotes, but if you do, you must
468escape any embedded instances of same sort of quote you began with,
469as well as any escaping any escapes that immediately precede that
470quote but which are not meant to escape the quote itself. In other
471words, you follow single-quoting rules irrespective of the quote;
472eg: C<o option='this isn\'t bad'> or C<o option="She said, \"Isn't
473it?\"">.
474
475For historical reasons, the C<=value> is optional, but defaults to
4761 only where it is safe to do so--that is, mostly for Boolean
477options. It is always better to assign a specific value using C<=>.
478The C<option> can be abbreviated, but for clarity probably should
479not be. Several options can be set together. See L</"Configurable Options">
480for a list of these.
481
482=item < ?
483X<< debugger command, < >>
484
485List out all pre-prompt Perl command actions.
486
487=item < [ command ]
488X<< debugger command, < >>
489
490Set an action (Perl command) to happen before every debugger prompt.
491A multi-line command may be entered by backslashing the newlines.
492
493=item < *
494X<< debugger command, < >>
495
496Delete all pre-prompt Perl command actions.
497
498=item << command
499X<< debugger command, << >>
500
501Add an action (Perl command) to happen before every debugger prompt.
502A multi-line command may be entered by backwhacking the newlines.
503
504=item > ?
505X<< debugger command, > >>
506
507List out post-prompt Perl command actions.
508
509=item > command
510X<< debugger command, > >>
511
512Set an action (Perl command) to happen after the prompt when you've
513just given a command to return to executing the script. A multi-line
514command may be entered by backslashing the newlines (we bet you
515couldn't have guessed this by now).
516
517=item > *
518X<< debugger command, > >>
519
520Delete all post-prompt Perl command actions.
521
522=item >> command
523X<<< debugger command, >> >>>
524
525Adds an action (Perl command) to happen after the prompt when you've
526just given a command to return to executing the script. A multi-line
527command may be entered by backslashing the newlines.
528
529=item { ?
530X<debugger command, {>
531
532List out pre-prompt debugger commands.
533
534=item { [ command ]
535
536Set an action (debugger command) to happen before every debugger prompt.
537A multi-line command may be entered in the customary fashion.
538
539Because this command is in some senses new, a warning is issued if
540you appear to have accidentally entered a block instead. If that's
541what you mean to do, write it as with C<;{ ... }> or even
542C<do { ... }>.
543
544=item { *
545X<debugger command, {>
546
547Delete all pre-prompt debugger commands.
548
549=item {{ command
550X<debugger command, {{>
551
552Add an action (debugger command) to happen before every debugger prompt.
553A multi-line command may be entered, if you can guess how: see above.
554
555=item ! number
556X<debugger command, !>
557
558Redo a previous command (defaults to the previous command).
559
560=item ! -number
561X<debugger command, !>
562
563Redo number'th previous command.
564
565=item ! pattern
566X<debugger command, !>
567
568Redo last command that started with pattern.
569See C<o recallCommand>, too.
570
571=item !! cmd
572X<debugger command, !!>
573
574Run cmd in a subprocess (reads from DB::IN, writes to DB::OUT) See
575C<o shellBang>, also. Note that the user's current shell (well,
576their C<$ENV{SHELL}> variable) will be used, which can interfere
577with proper interpretation of exit status or signal and coredump
578information.
579
580=item source file
581X<debugger command, source>
582
583Read and execute debugger commands from I<file>.
584I<file> may itself contain C<source> commands.
585
586=item H -number
587X<debugger command, H>
588
589Display last n commands. Only commands longer than one character are
590listed. If I<number> is omitted, list them all.
591
592=item q or ^D
593X<debugger command, q>
594X<debugger command, ^D>
595
596Quit. ("quit" doesn't work for this, unless you've made an alias)
597This is the only supported way to exit the debugger, though typing
598C<exit> twice might work.
599
600Set the C<inhibit_exit> option to 0 if you want to be able to step
601off the end the script. You may also need to set $finished to 0
602if you want to step through global destruction.
603
604=item R
605X<debugger command, R>
606
607Restart the debugger by C<exec()>ing a new session. We try to maintain
608your history across this, but internal settings and command-line options
609may be lost.
610
611The following setting are currently preserved: history, breakpoints,
612actions, debugger options, and the Perl command-line
613options B<-w>, B<-I>, and B<-e>.
614
615=item |dbcmd
616X<debugger command, |>
617
618Run the debugger command, piping DB::OUT into your current pager.
619
620=item ||dbcmd
621X<debugger command, ||>
622
623Same as C<|dbcmd> but DB::OUT is temporarily C<select>ed as well.
624
625=item = [alias value]
626X<debugger command, =>
627
628Define a command alias, like
629
630 = quit q
631
632or list current aliases.
633
634=item command
635
636Execute command as a Perl statement. A trailing semicolon will be
637supplied. If the Perl statement would otherwise be confused for a
638Perl debugger, use a leading semicolon, too.
639
640=item m expr
641X<debugger command, m>
642
643List which methods may be called on the result of the evaluated
644expression. The expression may evaluated to a reference to a
645blessed object, or to a package name.
646
647=item M
648X<debugger command, M>
649
650Display all loaded modules and their versions.
651
652=item man [manpage]
653X<debugger command, man>
654
655Despite its name, this calls your system's default documentation
656viewer on the given page, or on the viewer itself if I<manpage> is
657omitted. If that viewer is B<man>, the current C<Config> information
658is used to invoke B<man> using the proper MANPATH or S<B<-M>
659I<manpath>> option. Failed lookups of the form C<XXX> that match
660known manpages of the form I<perlXXX> will be retried. This lets
661you type C<man debug> or C<man op> from the debugger.
662
663On systems traditionally bereft of a usable B<man> command, the
664debugger invokes B<perldoc>. Occasionally this determination is
665incorrect due to recalcitrant vendors or rather more felicitously,
666to enterprising users. If you fall into either category, just
667manually set the $DB::doccmd variable to whatever viewer to view
668the Perl documentation on your system. This may be set in an rc
669file, or through direct assignment. We're still waiting for a
670working example of something along the lines of:
671
672 $DB::doccmd = 'netscape -remote http://something.here/';
673
674=back
675
676=head2 Configurable Options
677
678The debugger has numerous options settable using the C<o> command,
679either interactively or from the environment or an rc file.
680(./.perldb or ~/.perldb under Unix.)
681
682
683=over 12
684
685=item C<recallCommand>, C<ShellBang>
686X<debugger option, recallCommand>
687X<debugger option, ShellBang>
688
689The characters used to recall a command or spawn a shell. By
690default, both are set to C<!>, which is unfortunate.
691
692=item C<pager>
693X<debugger option, pager>
694
695Program to use for output of pager-piped commands (those beginning
696with a C<|> character.) By default, C<$ENV{PAGER}> will be used.
697Because the debugger uses your current terminal characteristics
698for bold and underlining, if the chosen pager does not pass escape
699sequences through unchanged, the output of some debugger commands
700will not be readable when sent through the pager.
701
702=item C<tkRunning>
703X<debugger option, tkRunning>
704
705Run Tk while prompting (with ReadLine).
706
707=item C<signalLevel>, C<warnLevel>, C<dieLevel>
708X<debugger option, signalLevel> X<debugger option, warnLevel>
709X<debugger option, dieLevel>
710
711Level of verbosity. By default, the debugger leaves your exceptions
712and warnings alone, because altering them can break correctly running
713programs. It will attempt to print a message when uncaught INT, BUS, or
714SEGV signals arrive. (But see the mention of signals in L</BUGS> below.)
715
716To disable this default safe mode, set these values to something higher
717than 0. At a level of 1, you get backtraces upon receiving any kind
718of warning (this is often annoying) or exception (this is
719often valuable). Unfortunately, the debugger cannot discern fatal
720exceptions from non-fatal ones. If C<dieLevel> is even 1, then your
721non-fatal exceptions are also traced and unceremoniously altered if they
722came from C<eval'ed> strings or from any kind of C<eval> within modules
723you're attempting to load. If C<dieLevel> is 2, the debugger doesn't
724care where they came from: It usurps your exception handler and prints
725out a trace, then modifies all exceptions with its own embellishments.
726This may perhaps be useful for some tracing purposes, but tends to hopelessly
727destroy any program that takes its exception handling seriously.
728
729=item C<AutoTrace>
730X<debugger option, AutoTrace>
731
732Trace mode (similar to C<t> command, but can be put into
733C<PERLDB_OPTS>).
734
735=item C<LineInfo>
736X<debugger option, LineInfo>
737
738File or pipe to print line number info to. If it is a pipe (say,
739C<|visual_perl_db>), then a short message is used. This is the
740mechanism used to interact with a slave editor or visual debugger,
741such as the special C<vi> or C<emacs> hooks, or the C<ddd> graphical
742debugger.
743
744=item C<inhibit_exit>
745X<debugger option, inhibit_exit>
746
747If 0, allows I<stepping off> the end of the script.
748
749=item C<PrintRet>
750X<debugger option, PrintRet>
751
752Print return value after C<r> command if set (default).
753
754=item C<ornaments>
755X<debugger option, ornaments>
756
757Affects screen appearance of the command line (see L<Term::ReadLine>).
758There is currently no way to disable these, which can render
759some output illegible on some displays, or with some pagers.
760This is considered a bug.
761
762=item C<frame>
763X<debugger option, frame>
764
765Affects the printing of messages upon entry and exit from subroutines. If
766C<frame & 2> is false, messages are printed on entry only. (Printing
767on exit might be useful if interspersed with other messages.)
768
769If C<frame & 4>, arguments to functions are printed, plus context
770and caller info. If C<frame & 8>, overloaded C<stringify> and
771C<tie>d C<FETCH> is enabled on the printed arguments. If C<frame
772& 16>, the return value from the subroutine is printed.
773
774The length at which the argument list is truncated is governed by the
775next option:
776
777=item C<maxTraceLen>
778X<debugger option, maxTraceLen>
779
780Length to truncate the argument list when the C<frame> option's
781bit 4 is set.
782
783=item C<windowSize>
784X<debugger option, windowSize>
785
786Change the size of code list window (default is 10 lines).
787
788=back
789
790The following options affect what happens with C<V>, C<X>, and C<x>
791commands:
792
793=over 12
794
795=item C<arrayDepth>, C<hashDepth>
796X<debugger option, arrayDepth> X<debugger option, hashDepth>
797
798Print only first N elements ('' for all).
799
800=item C<dumpDepth>
801X<debugger option, dumpDepth>
802
803Limit recursion depth to N levels when dumping structures.
804Negative values are interpreted as infinity. Default: infinity.
805
806=item C<compactDump>, C<veryCompact>
807X<debugger option, compactDump> X<debugger option, veryCompact>
808
809Change the style of array and hash output. If C<compactDump>, short array
810may be printed on one line.
811
812=item C<globPrint>
813X<debugger option, globPrint>
814
815Whether to print contents of globs.
816
817=item C<DumpDBFiles>
818X<debugger option, DumpDBFiles>
819
820Dump arrays holding debugged files.
821
822=item C<DumpPackages>
823X<debugger option, DumpPackages>
824
825Dump symbol tables of packages.
826
827=item C<DumpReused>
828X<debugger option, DumpReused>
829
830Dump contents of "reused" addresses.
831
832=item C<quote>, C<HighBit>, C<undefPrint>
833X<debugger option, quote> X<debugger option, HighBit>
834X<debugger option, undefPrint>
835
836Change the style of string dump. The default value for C<quote>
837is C<auto>; one can enable double-quotish or single-quotish format
838by setting it to C<"> or C<'>, respectively. By default, characters
839with their high bit set are printed verbatim.
840
841=item C<UsageOnly>
842X<debugger option, UsageOnly>
843
844Rudimentary per-package memory usage dump. Calculates total
845size of strings found in variables in the package. This does not
846include lexicals in a module's file scope, or lost in closures.
847
848=item C<HistFile>
849X<debugger option, history, HistFile>
850
851The path of the file from which the history (assuming a usable
852Term::ReadLine backend) will be read on the debugger's startup, and to which
853it will be saved on shutdown (for persistence across sessions). Similar in
854concept to Bash's C<.bash_history> file.
855
856=item C<HistSize>
857X<debugger option, history, HistSize>
858
859The count of the saved lines in the history (assuming C<HistFile> above).
860
861=back
862
863After the rc file is read, the debugger reads the C<$ENV{PERLDB_OPTS}>
864environment variable and parses this as the remainder of a "O ..."
865line as one might enter at the debugger prompt. You may place the
866initialization options C<TTY>, C<noTTY>, C<ReadLine>, and C<NonStop>
867there.
868
869If your rc file contains:
870
871 parse_options("NonStop=1 LineInfo=db.out AutoTrace");
872
873then your script will run without human intervention, putting trace
874information into the file I<db.out>. (If you interrupt it, you'd
875better reset C<LineInfo> to F</dev/tty> if you expect to see anything.)
876
877=over 12
878
879=item C<TTY>
880X<debugger option, TTY>
881
882The TTY to use for debugging I/O.
883
884=item C<noTTY>
885X<debugger option, noTTY>
886
887If set, the debugger goes into C<NonStop> mode and will not connect to a TTY. If
888interrupted (or if control goes to the debugger via explicit setting of
889$DB::signal or $DB::single from the Perl script), it connects to a TTY
890specified in the C<TTY> option at startup, or to a tty found at
891runtime using the C<Term::Rendezvous> module of your choice.
892
893This module should implement a method named C<new> that returns an object
894with two methods: C<IN> and C<OUT>. These should return filehandles to use
895for debugging input and output correspondingly. The C<new> method should
896inspect an argument containing the value of C<$ENV{PERLDB_NOTTY}> at
897startup, or C<"$ENV{HOME}/.perldbtty$$"> otherwise. This file is not
898inspected for proper ownership, so security hazards are theoretically
899possible.
900
901=item C<ReadLine>
902X<debugger option, ReadLine>
903
904If false, readline support in the debugger is disabled in order
905to debug applications that themselves use ReadLine.
906
907=item C<NonStop>
908X<debugger option, NonStop>
909
910If set, the debugger goes into non-interactive mode until interrupted, or
911programmatically by setting $DB::signal or $DB::single.
912
913=back
914
915Here's an example of using the C<$ENV{PERLDB_OPTS}> variable:
916
917 $ PERLDB_OPTS="NonStop frame=2" perl -d myprogram
918
919That will run the script B<myprogram> without human intervention,
920printing out the call tree with entry and exit points. Note that
921C<NonStop=1 frame=2> is equivalent to C<N f=2>, and that originally,
922options could be uniquely abbreviated by the first letter (modulo
923the C<Dump*> options). It is nevertheless recommended that you
924always spell them out in full for legibility and future compatibility.
925
926Other examples include
927
928 $ PERLDB_OPTS="NonStop LineInfo=listing frame=2" perl -d myprogram
929
930which runs script non-interactively, printing info on each entry
931into a subroutine and each executed line into the file named F<listing>.
932(If you interrupt it, you would better reset C<LineInfo> to something
933"interactive"!)
934
935Other examples include (using standard shell syntax to show environment
936variable settings):
937
938 $ ( PERLDB_OPTS="NonStop frame=1 AutoTrace LineInfo=tperl.out"
939 perl -d myprogram )
940
941which may be useful for debugging a program that uses C<Term::ReadLine>
942itself. Do not forget to detach your shell from the TTY in the window that
943corresponds to F</dev/ttyXX>, say, by issuing a command like
944
945 $ sleep 1000000
946
947See L<perldebguts/"Debugger Internals"> for details.
948
949=head2 Debugger Input/Output
950
951=over 8
952
953=item Prompt
954
955The debugger prompt is something like
956
957 DB<8>
958
959or even
960
961 DB<<17>>
962
963where that number is the command number, and which you'd use to
964access with the built-in B<csh>-like history mechanism. For example,
965C<!17> would repeat command number 17. The depth of the angle
966brackets indicates the nesting depth of the debugger. You could
967get more than one set of brackets, for example, if you'd already
968at a breakpoint and then printed the result of a function call that
969itself has a breakpoint, or you step into an expression via C<s/n/t
970expression> command.
971
972=item Multiline commands
973
974If you want to enter a multi-line command, such as a subroutine
975definition with several statements or a format, escape the newline
976that would normally end the debugger command with a backslash.
977Here's an example:
978
979 DB<1> for (1..4) { \
980 cont: print "ok\n"; \
981 cont: }
982 ok
983 ok
984 ok
985 ok
986
987Note that this business of escaping a newline is specific to interactive
988commands typed into the debugger.
989
990=item Stack backtrace
991X<backtrace> X<stack, backtrace>
992
993Here's an example of what a stack backtrace via C<T> command might
994look like:
995
996 $ = main::infested called from file 'Ambulation.pm' line 10
997 @ = Ambulation::legs(1, 2, 3, 4) called from file 'camel_flea'
998 line 7
999 $ = main::pests('bactrian', 4) called from file 'camel_flea'
1000 line 4
1001
1002The left-hand character up there indicates the context in which the
1003function was called, with C<$> and C<@> meaning scalar or list
1004contexts respectively, and C<.> meaning void context (which is
1005actually a sort of scalar context). The display above says
1006that you were in the function C<main::infested> when you ran the
1007stack dump, and that it was called in scalar context from line
100810 of the file I<Ambulation.pm>, but without any arguments at all,
1009meaning it was called as C<&infested>. The next stack frame shows
1010that the function C<Ambulation::legs> was called in list context
1011from the I<camel_flea> file with four arguments. The last stack
1012frame shows that C<main::pests> was called in scalar context,
1013also from I<camel_flea>, but from line 4.
1014
1015If you execute the C<T> command from inside an active C<use>
1016statement, the backtrace will contain both a C<require> frame and
1017an C<eval> frame.
1018
1019=item Line Listing Format
1020
1021This shows the sorts of output the C<l> command can produce:
1022
1023 DB<<13>> l
1024 101: @i{@i} = ();
1025 102:b @isa{@i,$pack} = ()
1026 103 if(exists $i{$prevpack} || exists $isa{$pack});
1027 104 }
1028 105
1029 106 next
1030 107==> if(exists $isa{$pack});
1031 108
1032 109:a if ($extra-- > 0) {
1033 110: %isa = ($pack,1);
1034
1035Breakable lines are marked with C<:>. Lines with breakpoints are
1036marked by C<b> and those with actions by C<a>. The line that's
1037about to be executed is marked by C<< ==> >>.
1038
1039Please be aware that code in debugger listings may not look the same
1040as your original source code. Line directives and external source
1041filters can alter the code before Perl sees it, causing code to move
1042from its original positions or take on entirely different forms.
1043
1044=item Frame listing
1045
1046When the C<frame> option is set, the debugger would print entered (and
1047optionally exited) subroutines in different styles. See L<perldebguts>
1048for incredibly long examples of these.
1049
1050=back
1051
1052=head2 Debugging Compile-Time Statements
1053
1054If you have compile-time executable statements (such as code within
1055BEGIN, UNITCHECK and CHECK blocks or C<use> statements), these will
1056I<not> be stopped by debugger, although C<require>s and INIT blocks
1057will, and compile-time statements can be traced with the C<AutoTrace>
1058option set in C<PERLDB_OPTS>). From your own Perl code, however, you
1059can transfer control back to the debugger using the following
1060statement, which is harmless if the debugger is not running:
1061
1062 $DB::single = 1;
1063
1064If you set C<$DB::single> to 2, it's equivalent to having
1065just typed the C<n> command, whereas a value of 1 means the C<s>
1066command. The C<$DB::trace> variable should be set to 1 to simulate
1067having typed the C<t> command.
1068
1069Another way to debug compile-time code is to start the debugger, set a
1070breakpoint on the I<load> of some module:
1071
1072 DB<7> b load f:/perllib/lib/Carp.pm
1073 Will stop on load of 'f:/perllib/lib/Carp.pm'.
1074
1075and then restart the debugger using the C<R> command (if possible). One can use C<b
1076compile subname> for the same purpose.
1077
1078=head2 Debugger Customization
1079
1080The debugger probably contains enough configuration hooks that you
1081won't ever have to modify it yourself. You may change the behaviour
1082of the debugger from within the debugger using its C<o> command, from
1083the command line via the C<PERLDB_OPTS> environment variable, and
1084from customization files.
1085
1086You can do some customization by setting up a F<.perldb> file, which
1087contains initialization code. For instance, you could make aliases
1088like these (the last one is one people expect to be there):
1089
1090 $DB::alias{'len'} = 's/^len(.*)/p length($1)/';
1091 $DB::alias{'stop'} = 's/^stop (at|in)/b/';
1092 $DB::alias{'ps'} = 's/^ps\b/p scalar /';
1093 $DB::alias{'quit'} = 's/^quit(\s*)/exit/';
1094
1095You can change options from F<.perldb> by using calls like this one;
1096
1097 parse_options("NonStop=1 LineInfo=db.out AutoTrace=1 frame=2");
1098
1099The code is executed in the package C<DB>. Note that F<.perldb> is
1100processed before processing C<PERLDB_OPTS>. If F<.perldb> defines the
1101subroutine C<afterinit>, that function is called after debugger
1102initialization ends. F<.perldb> may be contained in the current
1103directory, or in the home directory. Because this file is sourced
1104in by Perl and may contain arbitrary commands, for security reasons,
1105it must be owned by the superuser or the current user, and writable
1106by no one but its owner.
1107
1108You can mock TTY input to debugger by adding arbitrary commands to
1109@DB::typeahead. For example, your F<.perldb> file might contain:
1110
1111 sub afterinit { push @DB::typeahead, "b 4", "b 6"; }
1112
1113Which would attempt to set breakpoints on lines 4 and 6 immediately
1114after debugger initialization. Note that @DB::typeahead is not a supported
1115interface and is subject to change in future releases.
1116
1117If you want to modify the debugger, copy F<perl5db.pl> from the
1118Perl library to another name and hack it to your heart's content.
1119You'll then want to set your C<PERL5DB> environment variable to say
1120something like this:
1121
1122 BEGIN { require "myperl5db.pl" }
1123
1124As a last resort, you could also use C<PERL5DB> to customize the debugger
1125by directly setting internal variables or calling debugger functions.
1126
1127Note that any variables and functions that are not documented in
1128this document (or in L<perldebguts>) are considered for internal
1129use only, and as such are subject to change without notice.
1130
1131=head2 Readline Support / History in the Debugger
1132
1133As shipped, the only command-line history supplied is a simplistic one
1134that checks for leading exclamation points. However, if you install
1135the Term::ReadKey and Term::ReadLine modules from CPAN (such as
1136Term::ReadLine::Gnu, Term::ReadLine::Perl, ...) you will
1137have full editing capabilities much like those GNU I<readline>(3) provides.
1138Look for these in the F<modules/by-module/Term> directory on CPAN.
1139These do not support normal B<vi> command-line editing, however.
1140
1141A rudimentary command-line completion is also available, including
1142lexical variables in the current scope if the C<PadWalker> module
1143is installed.
1144
1145Without Readline support you may see the symbols "^[[A", "^[[C", "^[[B",
1146"^[[D"", "^H", ... when using the arrow keys and/or the backspace key.
1147
1148=head2 Editor Support for Debugging
1149
1150If you have the GNU's version of B<emacs> installed on your system,
1151it can interact with the Perl debugger to provide an integrated
1152software development environment reminiscent of its interactions
1153with C debuggers.
1154
1155Recent versions of Emacs come with a
1156start file for making B<emacs> act like a
1157syntax-directed editor that understands (some of) Perl's syntax.
1158See L<perlfaq3>.
1159
1160Users of B<vi> should also look into B<vim> and B<gvim>, the mousey
1161and windy version, for coloring of Perl keywords.
1162
1163Note that only perl can truly parse Perl, so all such CASE tools
1164fall somewhat short of the mark, especially if you don't program
1165your Perl as a C programmer might.
1166
1167=head2 The Perl Profiler
1168X<profile> X<profiling> X<profiler>
1169
1170If you wish to supply an alternative debugger for Perl to run,
1171invoke your script with a colon and a package argument given to the
1172B<-d> flag. Perl's alternative debuggers include a Perl profiler,
1173L<Devel::NYTProf>, which is available separately as a CPAN
1174distribution. To profile your Perl program in the file F<mycode.pl>,
1175just type:
1176
1177 $ perl -d:NYTProf mycode.pl
1178
1179When the script terminates the profiler will create a database of the
1180profile information that you can turn into reports using the profiler's
1181tools. See <perlperf> for details.
1182
1183=head1 Debugging Regular Expressions
1184X<regular expression, debugging>
1185X<regex, debugging> X<regexp, debugging>
1186
1187C<use re 'debug'> enables you to see the gory details of how the Perl
1188regular expression engine works. In order to understand this typically
1189voluminous output, one must not only have some idea about how regular
1190expression matching works in general, but also know how Perl's regular
1191expressions are internally compiled into an automaton. These matters
1192are explored in some detail in
1193L<perldebguts/"Debugging Regular Expressions">.
1194
1195=head1 Debugging Memory Usage
1196X<memory usage>
1197
1198Perl contains internal support for reporting its own memory usage,
1199but this is a fairly advanced concept that requires some understanding
1200of how memory allocation works.
1201See L<perldebguts/"Debugging Perl Memory Usage"> for the details.
1202
1203=head1 SEE ALSO
1204
1205You do have C<use strict> and C<use warnings> enabled, don't you?
1206
1207L<perldebtut>,
1208L<perldebguts>,
1209L<re>,
1210L<DB>,
1211L<Devel::NYTProf>,
1212L<Dumpvalue>,
1213and
1214L<perlrun>.
1215
1216When debugging a script that uses #! and is thus normally found in
1217$PATH, the -S option causes perl to search $PATH for it, so you don't
1218have to type the path or C<which $scriptname>.
1219
1220 $ perl -Sd foo.pl
1221
1222=head1 BUGS
1223
1224You cannot get stack frame information or in any fashion debug functions
1225that were not compiled by Perl, such as those from C or C++ extensions.
1226
1227If you alter your @_ arguments in a subroutine (such as with C<shift>
1228or C<pop>), the stack backtrace will not show the original values.
1229
1230The debugger does not currently work in conjunction with the B<-W>
1231command-line switch, because it itself is not free of warnings.
1232
1233If you're in a slow syscall (like C<wait>ing, C<accept>ing, or C<read>ing
1234from your keyboard or a socket) and haven't set up your own C<$SIG{INT}>
1235handler, then you won't be able to CTRL-C your way back to the debugger,
1236because the debugger's own C<$SIG{INT}> handler doesn't understand that
1237it needs to raise an exception to longjmp(3) out of slow syscalls.