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