1PERLDEBUG(1)           Perl Programmers Reference Guide           PERLDEBUG(1)
2
3
4

NAME

6       perldebug - Perl debugging
7

DESCRIPTION

9       First of all, have you tried using "use strict;" and "use warnings;"?
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
14       If you're looking for the nitty gritty details of how the debugger is
15       implemented, you may prefer to read perldebguts.
16
17       For in-depth technical usage details, see perl5db.pl, the documentation
18       of the debugger itself.
19

The Perl Debugger

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

Debugging Regular Expressions

901       "use re 'debug'" enables you to see the gory details of how the Perl
902       regular expression engine works. In order to understand this typically
903       voluminous output, one must not only have some idea about how regular
904       expression matching works in general, but also know how Perl's regular
905       expressions are internally compiled into an automaton. These matters
906       are explored in some detail in "Debugging Regular Expressions" in
907       perldebguts.
908

Debugging Memory Usage

910       Perl contains internal support for reporting its own memory usage, but
911       this is a fairly advanced concept that requires some understanding of
912       how memory allocation works.  See "Debugging Perl Memory Usage" in
913       perldebguts for the details.
914

SEE ALSO

916       You do have "use strict" and "use warnings" enabled, don't you?
917
918       perldebtut, perldebguts, perl5db.pl, re, DB, Devel::NYTProf, Dumpvalue,
919       and perlrun.
920
921       When debugging a script that uses #! and is thus normally found in
922       $PATH, the -S option causes perl to search $PATH for it, so you don't
923       have to type the path or "which $scriptname".
924
925         $ perl -Sd foo.pl
926

BUGS

928       You cannot get stack frame information or in any fashion debug
929       functions that were not compiled by Perl, such as those from C or C++
930       extensions.
931
932       If you alter your @_ arguments in a subroutine (such as with "shift" or
933       "pop"), the stack backtrace will not show the original values.
934
935       The debugger does not currently work in conjunction with the -W
936       command-line switch, because it itself is not free of warnings.
937
938       If you're in a slow syscall (like "wait"ing, "accept"ing, or "read"ing
939       from your keyboard or a socket) and haven't set up your own $SIG{INT}
940       handler, then you won't be able to CTRL-C your way back to the
941       debugger, because the debugger's own $SIG{INT} handler doesn't
942       understand that it needs to raise an exception to longjmp(3) out of
943       slow syscalls.
944
945
946
947perl v5.34.1                      2022-03-15                      PERLDEBUG(1)
Impressum