| 1 | =head1 NAME
|
|---|
| 2 | X<debug> X<debugger>
|
|---|
| 3 |
|
|---|
| 4 | perldebug - Perl debugging
|
|---|
| 5 |
|
|---|
| 6 | =head1 DESCRIPTION
|
|---|
| 7 |
|
|---|
| 8 | First of all, have you tried using the B<-w> switch?
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 | If you're new to the Perl debugger, you may prefer to read
|
|---|
| 12 | L<perldebtut>, which is a tutorial introduction to the debugger .
|
|---|
| 13 |
|
|---|
| 14 | =head1 The Perl Debugger
|
|---|
| 15 |
|
|---|
| 16 | If you invoke Perl with the B<-d> switch, your script runs under the
|
|---|
| 17 | Perl source debugger. This works like an interactive Perl
|
|---|
| 18 | environment, prompting for debugger commands that let you examine
|
|---|
| 19 | source code, set breakpoints, get stack backtraces, change the values of
|
|---|
| 20 | variables, etc. This is so convenient that you often fire up
|
|---|
| 21 | the debugger all by itself just to test out Perl constructs
|
|---|
| 22 | interactively to see what they do. For example:
|
|---|
| 23 | X<-d>
|
|---|
| 24 |
|
|---|
| 25 | $ perl -d -e 42
|
|---|
| 26 |
|
|---|
| 27 | In Perl, the debugger is not a separate program the way it usually is in the
|
|---|
| 28 | typical compiled environment. Instead, the B<-d> flag tells the compiler
|
|---|
| 29 | to insert source information into the parse trees it's about to hand off
|
|---|
| 30 | to the interpreter. That means your code must first compile correctly
|
|---|
| 31 | for the debugger to work on it. Then when the interpreter starts up, it
|
|---|
| 32 | preloads a special Perl library file containing the debugger.
|
|---|
| 33 |
|
|---|
| 34 | The program will halt I<right before> the first run-time executable
|
|---|
| 35 | statement (but see below regarding compile-time statements) and ask you
|
|---|
| 36 | to enter a debugger command. Contrary to popular expectations, whenever
|
|---|
| 37 | the debugger halts and shows you a line of code, it always displays the
|
|---|
| 38 | line it's I<about> to execute, rather than the one it has just executed.
|
|---|
| 39 |
|
|---|
| 40 | Any command not recognized by the debugger is directly executed
|
|---|
| 41 | (C<eval>'d) as Perl code in the current package. (The debugger
|
|---|
| 42 | uses the DB package for keeping its own state information.)
|
|---|
| 43 |
|
|---|
| 44 | Note that the said C<eval> is bound by an implicit scope. As a
|
|---|
| 45 | result any newly introduced lexical variable or any modified
|
|---|
| 46 | capture buffer content is lost after the eval. The debugger is a
|
|---|
| 47 | nice environment to learn Perl, but if you interactively experiment using
|
|---|
| 48 | material which should be in the same scope, stuff it in one line.
|
|---|
| 49 |
|
|---|
| 50 | For any text entered at the debugger prompt, leading and trailing whitespace
|
|---|
| 51 | is first stripped before further processing. If a debugger command
|
|---|
| 52 | coincides with some function in your own program, merely precede the
|
|---|
| 53 | function with something that doesn't look like a debugger command, such
|
|---|
| 54 | as a leading C<;> or perhaps a C<+>, or by wrapping it with parentheses
|
|---|
| 55 | or braces.
|
|---|
| 56 |
|
|---|
| 57 | =head2 Debugger Commands
|
|---|
| 58 |
|
|---|
| 59 | The debugger understands the following commands:
|
|---|
| 60 |
|
|---|
| 61 | =over 12
|
|---|
| 62 |
|
|---|
| 63 | =item h
|
|---|
| 64 | X<debugger command, h>
|
|---|
| 65 |
|
|---|
| 66 | Prints out a summary help message
|
|---|
| 67 |
|
|---|
| 68 | =item h [command]
|
|---|
| 69 |
|
|---|
| 70 | Prints out a help message for the given debugger command.
|
|---|
| 71 |
|
|---|
| 72 | =item h h
|
|---|
| 73 |
|
|---|
| 74 | The special argument of C<h h> produces the entire help page, which is quite long.
|
|---|
| 75 |
|
|---|
| 76 | If the output of the C<h h> command (or any command, for that matter) scrolls
|
|---|
| 77 | past your screen, precede the command with a leading pipe symbol so
|
|---|
| 78 | that it's run through your pager, as in
|
|---|
| 79 |
|
|---|
| 80 | DB> |h h
|
|---|
| 81 |
|
|---|
| 82 | You may change the pager which is used via C<o pager=...> command.
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | =item p expr
|
|---|
| 86 | X<debugger command, p>
|
|---|
| 87 |
|
|---|
| 88 | Same as C<print {$DB::OUT} expr> in the current package. In particular,
|
|---|
| 89 | because this is just Perl's own C<print> function, this means that nested
|
|---|
| 90 | data structures and objects are not dumped, unlike with the C<x> command.
|
|---|
| 91 |
|
|---|
| 92 | The C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
|
|---|
| 93 | where STDOUT may be redirected to.
|
|---|
| 94 |
|
|---|
| 95 | =item x [maxdepth] expr
|
|---|
| 96 | X<debugger command, x>
|
|---|
| 97 |
|
|---|
| 98 | Evaluates its expression in list context and dumps out the result in a
|
|---|
| 99 | pretty-printed fashion. Nested data structures are printed out
|
|---|
| 100 | recursively, unlike the real C<print> function in Perl. When dumping
|
|---|
| 101 | hashes, you'll probably prefer 'x \%h' rather than 'x %h'.
|
|---|
| 102 | See L<Dumpvalue> if you'd like to do this yourself.
|
|---|
| 103 |
|
|---|
| 104 | The output format is governed by multiple options described under
|
|---|
| 105 | L<"Configurable Options">.
|
|---|
| 106 |
|
|---|
| 107 | If the C<maxdepth> is included, it must be a numeral I<N>; the value is
|
|---|
| 108 | dumped only I<N> levels deep, as if the C<dumpDepth> option had been
|
|---|
| 109 | temporarily set to I<N>.
|
|---|
| 110 |
|
|---|
| 111 | =item V [pkg [vars]]
|
|---|
| 112 | X<debugger command, V>
|
|---|
| 113 |
|
|---|
| 114 | Display all (or some) variables in package (defaulting to C<main>)
|
|---|
| 115 | using a data pretty-printer (hashes show their keys and values so
|
|---|
| 116 | you see what's what, control characters are made printable, etc.).
|
|---|
| 117 | Make sure you don't put the type specifier (like C<$>) there, just
|
|---|
| 118 | the symbol names, like this:
|
|---|
| 119 |
|
|---|
| 120 | V DB filename line
|
|---|
| 121 |
|
|---|
| 122 | Use C<~pattern> and C<!pattern> for positive and negative regexes.
|
|---|
| 123 |
|
|---|
| 124 | This is similar to calling the C<x> command on each applicable var.
|
|---|
| 125 |
|
|---|
| 126 | =item X [vars]
|
|---|
| 127 | X<debugger command, X>
|
|---|
| 128 |
|
|---|
| 129 | Same as C<V currentpackage [vars]>.
|
|---|
| 130 |
|
|---|
| 131 | =item y [level [vars]]
|
|---|
| 132 | X<debugger command, y>
|
|---|
| 133 |
|
|---|
| 134 | Display all (or some) lexical variables (mnemonic: C<mY> variables)
|
|---|
| 135 | in the current scope or I<level> scopes higher. You can limit the
|
|---|
| 136 | variables that you see with I<vars> which works exactly as it does
|
|---|
| 137 | for the C<V> and C<X> commands. Requires the C<PadWalker> module
|
|---|
| 138 | version 0.08 or higher; will warn if this isn't installed. Output
|
|---|
| 139 | is pretty-printed in the same style as for C<V> and the format is
|
|---|
| 140 | controlled by the same options.
|
|---|
| 141 |
|
|---|
| 142 | =item T
|
|---|
| 143 | X<debugger command, T> X<backtrace> X<stack, backtrace>
|
|---|
| 144 |
|
|---|
| 145 | Produce a stack backtrace. See below for details on its output.
|
|---|
| 146 |
|
|---|
| 147 | =item s [expr]
|
|---|
| 148 | X<debugger command, s> X<step>
|
|---|
| 149 |
|
|---|
| 150 | Single step. Executes until the beginning of another
|
|---|
| 151 | statement, descending into subroutine calls. If an expression is
|
|---|
| 152 | supplied that includes function calls, it too will be single-stepped.
|
|---|
| 153 |
|
|---|
| 154 | =item n [expr]
|
|---|
| 155 | X<debugger command, n>
|
|---|
| 156 |
|
|---|
| 157 | Next. Executes over subroutine calls, until the beginning
|
|---|
| 158 | of the next statement. If an expression is supplied that includes
|
|---|
| 159 | function calls, those functions will be executed with stops before
|
|---|
| 160 | each statement.
|
|---|
| 161 |
|
|---|
| 162 | =item r
|
|---|
| 163 | X<debugger command, r>
|
|---|
| 164 |
|
|---|
| 165 | Continue until the return from the current subroutine.
|
|---|
| 166 | Dump the return value if the C<PrintRet> option is set (default).
|
|---|
| 167 |
|
|---|
| 168 | =item <CR>
|
|---|
| 169 |
|
|---|
| 170 | Repeat last C<n> or C<s> command.
|
|---|
| 171 |
|
|---|
| 172 | =item c [line|sub]
|
|---|
| 173 | X<debugger command, c>
|
|---|
| 174 |
|
|---|
| 175 | Continue, optionally inserting a one-time-only breakpoint
|
|---|
| 176 | at the specified line or subroutine.
|
|---|
| 177 |
|
|---|
| 178 | =item l
|
|---|
| 179 | X<debugger command, l>
|
|---|
| 180 |
|
|---|
| 181 | List next window of lines.
|
|---|
| 182 |
|
|---|
| 183 | =item l min+incr
|
|---|
| 184 |
|
|---|
| 185 | List C<incr+1> lines starting at C<min>.
|
|---|
| 186 |
|
|---|
| 187 | =item l min-max
|
|---|
| 188 |
|
|---|
| 189 | List lines C<min> through C<max>. C<l -> is synonymous to C<->.
|
|---|
| 190 |
|
|---|
| 191 | =item l line
|
|---|
| 192 |
|
|---|
| 193 | List a single line.
|
|---|
| 194 |
|
|---|
| 195 | =item l subname
|
|---|
| 196 |
|
|---|
| 197 | List first window of lines from subroutine. I<subname> may
|
|---|
| 198 | be a variable that contains a code reference.
|
|---|
| 199 |
|
|---|
| 200 | =item -
|
|---|
| 201 | X<debugger command, ->
|
|---|
| 202 |
|
|---|
| 203 | List previous window of lines.
|
|---|
|
|---|