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