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