perldoc under OS/2
[p5sagit/p5-mst-13.2.git] / pod / perldebug.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3perldebug - Perl debugging
4
5=head1 DESCRIPTION
6
7First of all, have you tried using the B<-w> switch?
8
4e1d3b43 9=head1 The Perl Debugger
10
26f28346 11"As soon as we started programming, we found to our
12surprise that it wasn't as easy to get programs right
13as we had thought. Debugging had to be discovered.
14I can remember the exact instant when I realized that
15a large part of my life from then on was going to be
16spent in finding mistakes in my own programs."
17--Maurice Wilkes, 1949
18
4e1d3b43 19If you invoke Perl with the B<-d> switch, your script runs under the
20Perl source debugger. This works like an interactive Perl
21environment, prompting for debugger commands that let you examine
68dc0745 22source code, set breakpoints, get stack backtraces, change the values of
4e1d3b43 23variables, etc. This is so convenient that you often fire up
54310121 24the debugger all by itself just to test out Perl constructs
4e1d3b43 25interactively to see what they do. For example:
26
27 perl -d -e 42
28
29In Perl, the debugger is not a separate program as it usually is in the
30typical compiled environment. Instead, the B<-d> flag tells the compiler
31to insert source information into the parse trees it's about to hand off
32to the interpreter. That means your code must first compile correctly
33for the debugger to work on it. Then when the interpreter starts up, it
54310121 34preloads a Perl library file containing the debugger itself.
4e1d3b43 35
36The program will halt I<right before> the first run-time executable
37statement (but see below regarding compile-time statements) and ask you
38to enter a debugger command. Contrary to popular expectations, whenever
39the debugger halts and shows you a line of code, it always displays the
40line it's I<about> to execute, rather than the one it has just executed.
41
42Any command not recognized by the debugger is directly executed
43(C<eval>'d) as Perl code in the current package. (The debugger uses the
44DB package for its own state information.)
45
46Leading white space before a command would cause the debugger to think
47it's I<NOT> a debugger command but for Perl, so be careful not to do
48that.
49
50=head2 Debugger Commands
51
52The debugger understands the following commands:
a0d0e21e 53
54=over 12
55
4e1d3b43 56=item h [command]
57
54310121 58Prints out a help message.
4e1d3b43 59
60If you supply another debugger command as an argument to the C<h> command,
61it prints out the description for just that command. The special
62argument of C<h h> produces a more compact help listing, designed to fit
63together on one screen.
64
65If the output the C<h> command (or any command, for that matter) scrolls
66past your screen, either precede the command with a leading pipe symbol so
67it's run through your pager, as in
68
69 DB> |h
70
e7ea3e70 71You may change the pager which is used via C<O pager=...> command.
72
4e1d3b43 73=item p expr
74
36477c24 75Same as C<print {$DB::OUT} expr> in the current package. In particular,
5f05dabc 76because this is just Perl's own B<print> function, this means that nested
4e1d3b43 77data structures and objects are not dumped, unlike with the C<x> command.
78
e7ea3e70 79The C<DB::OUT> filehandle is opened to F</dev/tty>, regardless of
80where STDOUT may be redirected to.
81
4e1d3b43 82=item x expr
83
54310121 84Evaluates its expression in list context and dumps out the result
4e1d3b43 85in a pretty-printed fashion. Nested data structures are printed out
86recursively, unlike the C<print> function.
87
36477c24 88The details of printout are governed by multiple C<O>ptions.
89
4e1d3b43 90=item V [pkg [vars]]
91
92Display all (or some) variables in package (defaulting to the C<main>
93package) using a data pretty-printer (hashes show their keys and values so
94you see what's what, control characters are made printable, etc.). Make
95sure you don't put the type specifier (like C<$>) there, just the symbol
96names, like this:
97
98 V DB filename line
99
100Use C<~pattern> and C<!pattern> for positive and negative regexps.
a0d0e21e 101
4e1d3b43 102Nested data structures are printed out in a legible fashion, unlike
103the C<print> function.
104
36477c24 105The details of printout are governed by multiple C<O>ptions.
106
4e1d3b43 107=item X [vars]
108
109Same as C<V currentpackage [vars]>.
a0d0e21e 110
111=item T
112
68dc0745 113Produce a stack backtrace. See below for details on its output.
a0d0e21e 114
4e1d3b43 115=item s [expr]
a0d0e21e 116
117Single step. Executes until it reaches the beginning of another
4e1d3b43 118statement, descending into subroutine calls. If an expression is
119supplied that includes function calls, it too will be single-stepped.
a0d0e21e 120
e7ea3e70 121=item n [expr]
a0d0e21e 122
123Next. Executes over subroutine calls, until it reaches the beginning
774d564b 124of the next statement. If an expression is supplied that includes
125function calls, those functions will be executed with stops before
126each statement.
a0d0e21e 127
184e9718 128=item E<lt>CRE<gt>
a0d0e21e 129
4e1d3b43 130Repeat last C<n> or C<s> command.
a0d0e21e 131
36477c24 132=item c [line|sub]
a0d0e21e 133
4e1d3b43 134Continue, optionally inserting a one-time-only breakpoint
36477c24 135at the specified line or subroutine.
a0d0e21e 136
4e1d3b43 137=item l
a0d0e21e 138
4e1d3b43 139List next window of lines.
a0d0e21e 140
141=item l min+incr
142
4e1d3b43 143List C<incr+1> lines starting at C<min>.
a0d0e21e 144
145=item l min-max
146
774d564b 147List lines C<min> through C<max>. C<l -> is synonymous to C<->.
a0d0e21e 148
149=item l line
150
4e1d3b43 151List a single line.
a0d0e21e 152
4e1d3b43 153=item l subname
a0d0e21e 154
4e1d3b43 155List first window of lines from subroutine.
a0d0e21e 156
157=item -
158
4e1d3b43 159List previous window of lines.
a0d0e21e 160
4e1d3b43 161=item w [line]
a0d0e21e 162
4e1d3b43 163List window (a few lines) around the current line.
a0d0e21e 164
4e1d3b43 165=item .
a0d0e21e 166
4e1d3b43 167Return debugger pointer to the last-executed line and
168print it out.
169
170=item f filename
171
774d564b 172Switch to viewing a different file or eval statement. If C<filename>
e7ea3e70 173is not a full filename as found in values of %INC, it is considered as
174a regexp.
a0d0e21e 175
176=item /pattern/
177
4e1d3b43 178Search forwards for pattern; final / is optional.
a0d0e21e 179
180=item ?pattern?
181
4e1d3b43 182Search backwards for pattern; final ? is optional.
a0d0e21e 183
184=item L
185
36477c24 186List all breakpoints and actions.
a0d0e21e 187
4e1d3b43 188=item S [[!]pattern]
a0d0e21e 189
4e1d3b43 190List subroutine names [not] matching pattern.
a0d0e21e 191
192=item t
193
36477c24 194Toggle trace mode (see also C<AutoTrace> C<O>ption).
4e1d3b43 195
196=item t expr
197
198Trace through execution of expr. For example:
199
200 $ perl -de 42
201 Stack dump during die enabled outside of evals.
a0d0e21e 202
4e1d3b43 203 Loading DB routines from perl5db.pl patch level 0.94
204 Emacs support available.
205
206 Enter h or `h h' for help.
207
208 main::(-e:1): 0
209 DB<1> sub foo { 14 }
210
211 DB<2> sub bar { 3 }
212
213 DB<3> t print foo() * bar()
214 main::((eval 172):3): print foo() + bar();
215 main::foo((eval 168):2):
216 main::bar((eval 170):2):
217 42
36477c24 218
219or, with the C<O>ption C<frame=2> set,
220
221 DB<4> O f=2
222 frame = '2'
223 DB<5> t print foo() * bar()
224 3: foo() * bar()
225 entering main::foo
226 2: sub foo { 14 };
227 exited main::foo
228 entering main::bar
229 2: sub bar { 3 };
230 exited main::bar
231 42
4e1d3b43 232
233=item b [line] [condition]
a0d0e21e 234
235Set a breakpoint. If line is omitted, sets a breakpoint on the line
4e1d3b43 236that is about to be executed. If a condition is specified, it's
a0d0e21e 237evaluated each time the statement is reached and a breakpoint is taken
5f05dabc 238only if the condition is true. Breakpoints may be set on only lines
4e1d3b43 239that begin an executable statement. Conditions don't use B<if>:
a0d0e21e 240
241 b 237 $x > 30
36477c24 242 b 237 ++$count237 < 11
a0d0e21e 243 b 33 /pattern/i
244
4e1d3b43 245=item b subname [condition]
a0d0e21e 246
4e1d3b43 247Set a breakpoint at the first line of the named subroutine.
a0d0e21e 248
36477c24 249=item b postpone subname [condition]
250
251Set breakpoint at first line of subroutine after it is compiled.
252
253=item b load filename
254
774d564b 255Set breakpoint at the first executed line of the file. Filename should
e7ea3e70 256be a full name as found in values of %INC.
257
258=item b compile subname
259
260Sets breakpoint at the first statement executed after the subroutine
261is compiled.
36477c24 262
4e1d3b43 263=item d [line]
a0d0e21e 264
4e1d3b43 265Delete a breakpoint at the specified line. If line is omitted, deletes
266the breakpoint on the line that is about to be executed.
a0d0e21e 267
268=item D
269
4e1d3b43 270Delete all installed breakpoints.
271
272=item a [line] command
273
274Set an action to be done before the line is executed.
275The sequence of steps taken by the debugger is
276
8ebc5c01 277 1. check for a breakpoint at this line
278 2. print the line if necessary (tracing)
279 3. do any actions associated with that line
280 4. prompt user if at a breakpoint or in single-step
281 5. evaluate line
a0d0e21e 282
4e1d3b43 283For example, this will print out C<$foo> every time line
28453 is passed:
a0d0e21e 285
4e1d3b43 286 a 53 print "DB FOUND $foo\n"
a0d0e21e 287
288=item A
289
4e1d3b43 290Delete all installed actions.
291
292=item O [opt[=val]] [opt"val"] [opt?]...
293
294Set or query values of options. val defaults to 1. opt can
295be abbreviated. Several options can be listed.
296
297=over 12
298
e7ea3e70 299=item C<recallCommand>, C<ShellBang>
4e1d3b43 300
301The characters used to recall command or spawn shell. By
302default, these are both set to C<!>.
303
e7ea3e70 304=item C<pager>
4e1d3b43 305
306Program to use for output of pager-piped commands (those
307beginning with a C<|> character.) By default,
308C<$ENV{PAGER}> will be used.
309
e7ea3e70 310=item C<tkRunning>
36477c24 311
312Run Tk while prompting (with ReadLine).
313
e7ea3e70 314=item C<signalLevel>, C<warnLevel>, C<dieLevel>
315
774d564b 316Level of verbosity. By default the debugger is in a sane verbose mode,
e7ea3e70 317thus it will print backtraces on all the warnings and die-messages
318which are going to be printed out, and will print a message when
54310121 319interesting uncaught signals arrive.
36477c24 320
774d564b 321To disable this behaviour, set these values to 0. If C<dieLevel> is 2,
e7ea3e70 322then the messages which will be caught by surrounding C<eval> are also
323printed.
36477c24 324
e7ea3e70 325=item C<AutoTrace>
36477c24 326
e7ea3e70 327Trace mode (similar to C<t> command, but can be put into
328C<PERLDB_OPTS>).
36477c24 329
e7ea3e70 330=item C<LineInfo>
36477c24 331
e7ea3e70 332File or pipe to print line number info to. If it is a pipe (say,
333C<|visual_perl_db>), then a short, "emacs like" message is used.
36477c24 334
335=item C<inhibit_exit>
336
337If 0, allows I<stepping off> the end of the script.
338
54310121 339=item C<PrintRet>
36477c24 340
341affects printing of return value after C<r> command.
342
28d1fb14 343=item C<ornaments>
344
3e3baf6d 345affects screen appearance of the command line (see L<Term::ReadLine>).
28d1fb14 346
54310121 347=item C<frame>
36477c24 348
349affects printing messages on entry and exit from subroutines. If
350C<frame & 2> is false, messages are printed on entry only. (Printing
5f05dabc 351on exit may be useful if inter(di)spersed with other messages.)
36477c24 352
353If C<frame & 4>, arguments to functions are printed as well as the
774d564b 354context and caller info. If C<frame & 8>, overloaded C<stringify> and
28d1fb14 355C<tie>d C<FETCH> are enabled on the printed arguments. If C<frame &
35616>, the return value from the subroutine is printed as well.
357
358The length at which the argument list is truncated is governed by the
359next option:
e7ea3e70 360
361=item C<maxTraceLen>
362
363length at which the argument list is truncated when C<frame> option's
364bit 4 is set.
36477c24 365
4e1d3b43 366=back
367
368The following options affect what happens with C<V>, C<X>, and C<x>
369commands:
370
371=over 12
372
e7ea3e70 373=item C<arrayDepth>, C<hashDepth>
4e1d3b43 374
375Print only first N elements ('' for all).
376
e7ea3e70 377=item C<compactDump>, C<veryCompact>
4e1d3b43 378
774d564b 379Change style of array and hash dump. If C<compactDump>, short array
e7ea3e70 380may be printed on one line.
4e1d3b43 381
e7ea3e70 382=item C<globPrint>
4e1d3b43 383
384Whether to print contents of globs.
385
e7ea3e70 386=item C<DumpDBFiles>
4e1d3b43 387
388Dump arrays holding debugged files.
389
e7ea3e70 390=item C<DumpPackages>
4e1d3b43 391
392Dump symbol tables of packages.
393
e7ea3e70 394=item C<quote>, C<HighBit>, C<undefPrint>
395
774d564b 396Change style of string dump. Default value of C<quote> is C<auto>, one
e7ea3e70 397can enable either double-quotish dump, or single-quotish by setting it
774d564b 398to C<"> or C<'>. By default, characters with high bit set are printed
e7ea3e70 399I<as is>.
400
54310121 401=item C<UsageOnly>
4e1d3b43 402
774d564b 403I<very> rudimentally per-package memory usage dump. Calculates total
e7ea3e70 404size of strings in variables in the package.
4e1d3b43 405
36477c24 406=back
4e1d3b43 407
36477c24 408During startup options are initialized from C<$ENV{PERLDB_OPTS}>.
409You can put additional initialization options C<TTY>, C<noTTY>,
410C<ReadLine>, and C<NonStop> there.
411
412Example rc file:
4e1d3b43 413
e7ea3e70 414 &parse_options("NonStop=1 LineInfo=db.out AutoTrace");
4e1d3b43 415
36477c24 416The script will run without human intervention, putting trace information
417into the file I<db.out>. (If you interrupt it, you would better reset
418C<LineInfo> to something "interactive"!)
4e1d3b43 419
36477c24 420=over 12
4e1d3b43 421
36477c24 422=item C<TTY>
4e1d3b43 423
36477c24 424The TTY to use for debugging I/O.
425
36477c24 426=item C<noTTY>
427
774d564b 428If set, goes in C<NonStop> mode, and would not connect to a TTY. If
36477c24 429interrupt (or if control goes to debugger via explicit setting of
430$DB::signal or $DB::single from the Perl script), connects to a TTY
431specified by the C<TTY> option at startup, or to a TTY found at
432runtime using C<Term::Rendezvous> module of your choice.
433
434This module should implement a method C<new> which returns an object
435with two methods: C<IN> and C<OUT>, returning two filehandles to use
774d564b 436for debugging input and output correspondingly. Method C<new> may
36477c24 437inspect an argument which is a value of C<$ENV{PERLDB_NOTTY}> at
438startup, or is C<"/tmp/perldbtty$$"> otherwise.
439
440=item C<ReadLine>
441
442If false, readline support in debugger is disabled, so you can debug
443ReadLine applications.
444
445=item C<NonStop>
446
54310121 447If set, debugger goes into noninteractive mode until interrupted, or
36477c24 448programmatically by setting $DB::signal or $DB::single.
449
450=back
451
452Here's an example of using the C<$ENV{PERLDB_OPTS}> variable:
4e1d3b43 453
e7ea3e70 454 $ PERLDB_OPTS="N f=2" perl -d myprogram
4e1d3b43 455
456will run the script C<myprogram> without human intervention, printing
457out the call tree with entry and exit points. Note that C<N f=2> is
774d564b 458equivalent to C<NonStop=1 frame=2>. Note also that at the moment when
4e1d3b43 459this documentation was written all the options to the debugger could
36477c24 460be uniquely abbreviated by the first letter (with exception of
461C<Dump*> options).
4e1d3b43 462
36477c24 463Other examples may include
a0d0e21e 464
e7ea3e70 465 $ PERLDB_OPTS="N f A L=listing" perl -d myprogram
a0d0e21e 466
54310121 467- runs script noninteractively, printing info on each entry into a
36477c24 468subroutine and each executed line into the file F<listing>. (If you
469interrupt it, you would better reset C<LineInfo> to something
470"interactive"!)
471
472
e7ea3e70 473 $ env "PERLDB_OPTS=R=0 TTY=/dev/ttyc" perl -d myprogram
36477c24 474
475may be useful for debugging a program which uses C<Term::ReadLine>
774d564b 476itself. Do not forget detach shell from the TTY in the window which
36477c24 477corresponds to F</dev/ttyc>, say, by issuing a command like
478
e7ea3e70 479 $ sleep 1000000
36477c24 480
481See L<"Debugger Internals"> below for more details.
482
483=item E<lt> [ command ]
484
485Set an action (Perl command) to happen before every debugger prompt.
4a6725af 486A multi-line command may be entered by backslashing the newlines. If
36477c24 487C<command> is missing, resets the list of actions.
488
489=item E<lt>E<lt> command
490
491Add an action (Perl command) to happen before every debugger prompt.
4a6725af 492A multi-line command may be entered by backslashing the newlines.
a0d0e21e 493
184e9718 494=item E<gt> command
a0d0e21e 495
36477c24 496Set an action (Perl command) to happen after the prompt when you've
4a6725af 497just given a command to return to executing the script. A multi-line
36477c24 498command may be entered by backslashing the newlines. If C<command> is
499missing, resets the list of actions.
500
501=item E<gt>E<gt> command
502
503Adds an action (Perl command) to happen after the prompt when you've
4a6725af 504just given a command to return to executing the script. A multi-line
36477c24 505command may be entered by backslashing the newlines.
506
507=item { [ command ]
508
509Set an action (debugger command) to happen before every debugger prompt.
4a6725af 510A multi-line command may be entered by backslashing the newlines. If
36477c24 511C<command> is missing, resets the list of actions.
512
513=item {{ command
514
515Add an action (debugger command) to happen before every debugger prompt.
4a6725af 516A multi-line command may be entered by backslashing the newlines.
a0d0e21e 517
4e1d3b43 518=item ! number
a0d0e21e 519
4e1d3b43 520Redo a previous command (default previous command).
a0d0e21e 521
4e1d3b43 522=item ! -number
a0d0e21e 523
4e1d3b43 524Redo number'th-to-last command.
a0d0e21e 525
4e1d3b43 526=item ! pattern
a0d0e21e 527
4e1d3b43 528Redo last command that started with pattern.
529See C<O recallCommand>, too.
a0d0e21e 530
4e1d3b43 531=item !! cmd
a0d0e21e 532
4e1d3b43 533Run cmd in a subprocess (reads from DB::IN, writes to DB::OUT)
534See C<O shellBang> too.
a0d0e21e 535
536=item H -number
537
538Display last n commands. Only commands longer than one character are
539listed. If number is omitted, lists them all.
540
541=item q or ^D
542
36477c24 543Quit. ("quit" doesn't work for this.) This is the only supported way
544to exit the debugger, though typing C<exit> twice may do it too.
545
546Set an C<O>ption C<inhibit_exit> to 0 if you want to be able to I<step
774d564b 547off> the end the script. You may also need to set C<$finished> to 0 at
36477c24 548some moment if you want to step through global destruction.
a0d0e21e 549
4e1d3b43 550=item R
551
552Restart the debugger by B<exec>ing a new session. It tries to maintain
553your history across this, but internal settings and command line options
554may be lost.
555
5f05dabc 556Currently the following setting are preserved: history, breakpoints,
54310121 557actions, debugger C<O>ptions, and the following command line
5f05dabc 558options: B<-w>, B<-I>, and B<-e>.
36477c24 559
4e1d3b43 560=item |dbcmd
561
562Run debugger command, piping DB::OUT to current pager.
563
564=item ||dbcmd
565
566Same as C<|dbcmd> but DB::OUT is temporarily B<select>ed as well.
567Often used with commands that would otherwise produce long
568output, such as
569
570 |V main
571
572=item = [alias value]
573
e7ea3e70 574Define a command alias, like
575
576 = quit q
577
578or list current aliases.
4e1d3b43 579
a0d0e21e 580=item command
581
582Execute command as a Perl statement. A missing semicolon will be
583supplied.
584
e7ea3e70 585=item m expr
a0d0e21e 586
e7ea3e70 587The expression is evaluated, and the methods which may be applied to
588the result are listed.
589
590=item m package
591
592The methods which may be applied to objects in the C<package> are listed.
a0d0e21e 593
594=back
595
e7ea3e70 596=head2 Debugger input/output
597
598=over 8
599
600=item Prompt
601
4e1d3b43 602The debugger prompt is something like
603
604 DB<8>
605
606or even
607
608 DB<<17>>
609
610where that number is the command number, which you'd use to access with
54310121 611the builtin B<csh>-like history mechanism, e.g., C<!17> would repeat
4e1d3b43 612command number 17. The number of angle brackets indicates the depth of
613the debugger. You could get more than one set of brackets, for example, if
614you'd already at a breakpoint and then printed out the result of a
36477c24 615function call that itself also has a breakpoint, or you step into an
616expression via C<s/n/t expression> command.
4e1d3b43 617
54310121 618=item Multiline commands
e7ea3e70 619
4a6725af 620If you want to enter a multi-line command, such as a subroutine
e7ea3e70 621definition with several statements, or a format, you may escape the
622newline that would normally end the debugger command with a backslash.
623Here's an example:
a0d0e21e 624
4e1d3b43 625 DB<1> for (1..4) { \
626 cont: print "ok\n"; \
627 cont: }
628 ok
629 ok
630 ok
631 ok
632
633Note that this business of escaping a newline is specific to interactive
634commands typed into the debugger.
635
e7ea3e70 636=item Stack backtrace
637
68dc0745 638Here's an example of what a stack backtrace via C<T> command might
e7ea3e70 639look like:
4e1d3b43 640
641 $ = main::infested called from file `Ambulation.pm' line 10
642 @ = Ambulation::legs(1, 2, 3, 4) called from file `camel_flea' line 7
643 $ = main::pests('bactrian', 4) called from file `camel_flea' line 4
644
645The left-hand character up there tells whether the function was called
646in a scalar or list context (we bet you can tell which is which). What
647that says is that you were in the function C<main::infested> when you ran
648the stack dump, and that it was called in a scalar context from line 10
649of the file I<Ambulation.pm>, but without any arguments at all, meaning
650it was called as C<&infested>. The next stack frame shows that the
651function C<Ambulation::legs> was called in a list context from the
652I<camel_flea> file with four arguments. The last stack frame shows that
653C<main::pests> was called in a scalar context, also from I<camel_flea>,
654but from line 4.
655
e7ea3e70 656Note that if you execute C<T> command from inside an active C<use>
657statement, the backtrace will contain both C<L<perlfunc/require>>
658frame and an C<L<perlfunc/eval EXPR>>) frame.
659
660=item Listing
661
662Listing given via different flavors of C<l> command looks like this:
663
664 DB<<13>> l
665 101: @i{@i} = ();
666 102:b @isa{@i,$pack} = ()
667 103 if(exists $i{$prevpack} || exists $isa{$pack});
668 104 }
669 105
670 106 next
671 107==> if(exists $isa{$pack});
672 108
673 109:a if ($extra-- > 0) {
674 110: %isa = ($pack,1);
675
676Note that the breakable lines are marked with C<:>, lines with
677breakpoints are marked by C<b>, with actions by C<a>, and the
678next executed line is marked by C<==E<gt>>.
679
680=item Frame listing
681
682When C<frame> option is set, debugger would print entered (and
683optionally exited) subroutines in different styles.
684
54310121 685What follows is the start of the listing of
e7ea3e70 686
28d1fb14 687 env "PERLDB_OPTS=f=n N" perl -d -V
688
689for different values of C<n>:
e7ea3e70 690
691=over 4
692
693=item 1
694
695 entering main::BEGIN
696 entering Config::BEGIN
697 Package lib/Exporter.pm.
698 Package lib/Carp.pm.
699 Package lib/Config.pm.
700 entering Config::TIEHASH
701 entering Exporter::import
702 entering Exporter::export
703 entering Config::myconfig
704 entering Config::FETCH
705 entering Config::FETCH
706 entering Config::FETCH
707 entering Config::FETCH
708
709=item 2
710
711 entering main::BEGIN
712 entering Config::BEGIN
713 Package lib/Exporter.pm.
714 Package lib/Carp.pm.
715 exited Config::BEGIN
716 Package lib/Config.pm.
717 entering Config::TIEHASH
718 exited Config::TIEHASH
719 entering Exporter::import
720 entering Exporter::export
721 exited Exporter::export
722 exited Exporter::import
723 exited main::BEGIN
724 entering Config::myconfig
725 entering Config::FETCH
726 exited Config::FETCH
727 entering Config::FETCH
728 exited Config::FETCH
729 entering Config::FETCH
730
731=item 4
732
733 in $=main::BEGIN() from /dev/nul:0
734 in $=Config::BEGIN() from lib/Config.pm:2
735 Package lib/Exporter.pm.
736 Package lib/Carp.pm.
737 Package lib/Config.pm.
738 in $=Config::TIEHASH('Config') from lib/Config.pm:644
739 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/nul:0
740 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from li
741 in @=Config::myconfig() from /dev/nul:0
742 in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
743 in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
744 in $=Config::FETCH(ref(Config), 'PATCHLEVEL') from lib/Config.pm:574
745 in $=Config::FETCH(ref(Config), 'SUBVERSION') from lib/Config.pm:574
746 in $=Config::FETCH(ref(Config), 'osname') from lib/Config.pm:574
747 in $=Config::FETCH(ref(Config), 'osvers') from lib/Config.pm:574
748
749=item 6
750
751 in $=main::BEGIN() from /dev/nul:0
752 in $=Config::BEGIN() from lib/Config.pm:2
753 Package lib/Exporter.pm.
754 Package lib/Carp.pm.
755 out $=Config::BEGIN() from lib/Config.pm:0
756 Package lib/Config.pm.
757 in $=Config::TIEHASH('Config') from lib/Config.pm:644
758 out $=Config::TIEHASH('Config') from lib/Config.pm:644
759 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/nul:0
760 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
761 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
762 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/nul:0
763 out $=main::BEGIN() from /dev/nul:0
764 in @=Config::myconfig() from /dev/nul:0
765 in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
766 out $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
767 in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
768 out $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
769 in $=Config::FETCH(ref(Config), 'PATCHLEVEL') from lib/Config.pm:574
770 out $=Config::FETCH(ref(Config), 'PATCHLEVEL') from lib/Config.pm:574
771 in $=Config::FETCH(ref(Config), 'SUBVERSION') from lib/Config.pm:574
772
773=item 14
774
775 in $=main::BEGIN() from /dev/nul:0
776 in $=Config::BEGIN() from lib/Config.pm:2
777 Package lib/Exporter.pm.
778 Package lib/Carp.pm.
779 out $=Config::BEGIN() from lib/Config.pm:0
780 Package lib/Config.pm.
781 in $=Config::TIEHASH('Config') from lib/Config.pm:644
782 out $=Config::TIEHASH('Config') from lib/Config.pm:644
783 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/nul:0
784 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
785 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
786 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/nul:0
787 out $=main::BEGIN() from /dev/nul:0
788 in @=Config::myconfig() from /dev/nul:0
789 in $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
790 out $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
791 in $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
792 out $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
793
28d1fb14 794=item 30
795
796 in $=CODE(0x15eca4)() from /dev/null:0
797 in $=CODE(0x182528)() from lib/Config.pm:2
798 Package lib/Exporter.pm.
799 out $=CODE(0x182528)() from lib/Config.pm:0
800 scalar context return from CODE(0x182528): undef
801 Package lib/Config.pm.
802 in $=Config::TIEHASH('Config') from lib/Config.pm:628
803 out $=Config::TIEHASH('Config') from lib/Config.pm:628
804 scalar context return from Config::TIEHASH: empty hash
805 in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
806 in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
807 out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
808 scalar context return from Exporter::export: ''
809 out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
810 scalar context return from Exporter::import: ''
811
812
e7ea3e70 813=back
814
815In all the cases indentation of lines shows the call tree, if bit 2 of
816C<frame> is set, then a line is printed on exit from a subroutine as
817well, if bit 4 is set, then the arguments are printed as well as the
818caller info, if bit 8 is set, the arguments are printed even if they
28d1fb14 819are tied or references, if bit 16 is set, the return value is printed
820as well.
e7ea3e70 821
822When a package is compiled, a line like this
823
824 Package lib/Carp.pm.
825
826is printed with proper indentation.
827
828=back
829
830=head2 Debugging compile-time statements
831
4e1d3b43 832If you have any compile-time executable statements (code within a BEGIN
833block or a C<use> statement), these will C<NOT> be stopped by debugger,
36477c24 834although C<require>s will (and compile-time statements can be traced
54310121 835with C<AutoTrace> option set in C<PERLDB_OPTS>). From your own Perl
36477c24 836code, however, you can
4e1d3b43 837transfer control back to the debugger using the following statement,
838which is harmless if the debugger is not running:
a0d0e21e 839
840 $DB::single = 1;
841
4e1d3b43 842If you set C<$DB::single> to the value 2, it's equivalent to having
843just typed the C<n> command, whereas a value of 1 means the C<s>
844command. The C<$DB::trace> variable should be set to 1 to simulate
845having typed the C<t> command.
846
e7ea3e70 847Another way to debug compile-time code is to start debugger, set a
848breakpoint on I<load> of some module thusly
849
850 DB<7> b load f:/perllib/lib/Carp.pm
851 Will stop on load of `f:/perllib/lib/Carp.pm'.
852
774d564b 853and restart debugger by C<R> command (if possible). One can use C<b
e7ea3e70 854compile subname> for the same purpose.
855
4e1d3b43 856=head2 Debugger Customization
a0d0e21e 857
36477c24 858Most probably you not want to modify the debugger, it contains enough
774d564b 859hooks to satisfy most needs. You may change the behaviour of debugger
36477c24 860from the debugger itself, using C<O>ptions, from the command line via
861C<PERLDB_OPTS> environment variable, and from I<customization files>.
a0d0e21e 862
863You can do some customization by setting up a F<.perldb> file which
864contains initialization code. For instance, you could make aliases
4e1d3b43 865like these (the last one is one people expect to be there):
a0d0e21e 866
4e1d3b43 867 $DB::alias{'len'} = 's/^len(.*)/p length($1)/';
a0d0e21e 868 $DB::alias{'stop'} = 's/^stop (at|in)/b/';
4e1d3b43 869 $DB::alias{'ps'} = 's/^ps\b/p scalar /';
870 $DB::alias{'quit'} = 's/^quit(\s*)/exit\$/';
871
36477c24 872One changes options from F<.perldb> file via calls like this one;
873
874 parse_options("NonStop=1 LineInfo=db.out AutoTrace=1 frame=2");
875
774d564b 876(the code is executed in the package C<DB>). Note that F<.perldb> is
877processed before processing C<PERLDB_OPTS>. If F<.perldb> defines the
36477c24 878subroutine C<afterinit>, it is called after all the debugger
774d564b 879initialization ends. F<.perldb> may be contained in the current
36477c24 880directory, or in the C<LOGDIR>/C<HOME> directory.
881
882If you want to modify the debugger, copy F<perl5db.pl> from the Perl
883library to another name and modify it as necessary. You'll also want
884to set your C<PERL5DB> environment variable to say something like this:
885
886 BEGIN { require "myperl5db.pl" }
887
888As the last resort, one can use C<PERL5DB> to customize debugger by
889directly setting internal variables or calling debugger functions.
890
4e1d3b43 891=head2 Readline Support
892
893As shipped, the only command line history supplied is a simplistic one
894that checks for leading exclamation points. However, if you install
895the Term::ReadKey and Term::ReadLine modules from CPAN, you will
896have full editing capabilities much like GNU I<readline>(3) provides.
897Look for these in the F<modules/by-module/Term> directory on CPAN.
898
54310121 899A rudimentary command line completion is also available.
e7ea3e70 900Unfortunately, the names of lexical variables are not available for
901completion.
902
4e1d3b43 903=head2 Editor Support for Debugging
904
905If you have GNU B<emacs> installed on your system, it can interact with
906the Perl debugger to provide an integrated software development
907environment reminiscent of its interactions with C debuggers.
908
909Perl is also delivered with a start file for making B<emacs> act like a
910syntax-directed editor that understands (some of) Perl's syntax. Look in
911the I<emacs> directory of the Perl source distribution.
912
913(Historically, a similar setup for interacting with B<vi> and the
914X11 window system had also been available, but at the time of this
915writing, no debugger support for B<vi> currently exists.)
916
917=head2 The Perl Profiler
918
919If you wish to supply an alternative debugger for Perl to run, just
920invoke your script with a colon and a package argument given to the B<-d>
921flag. One of the most popular alternative debuggers for Perl is
922B<DProf>, the Perl profiler. As of this writing, B<DProf> is not
923included with the standard Perl distribution, but it is expected to
924be included soon, for certain values of "soon".
925
926Meanwhile, you can fetch the Devel::Dprof module from CPAN. Assuming
927it's properly installed on your system, to profile your Perl program in
928the file F<mycode.pl>, just type:
929
930 perl -d:DProf mycode.pl
931
932When the script terminates the profiler will dump the profile information
933to a file called F<tmon.out>. A tool like B<dprofpp> (also supplied with
934the Devel::DProf package) can be used to interpret the information which is
935in that profile.
936
36477c24 937=head2 Debugger support in perl
4e1d3b43 938
e7ea3e70 939When you call the B<caller> function (see L<perlfunc/caller>) from the
940package DB, Perl sets the array @DB::args to contain the arguments the
54310121 941corresponding stack frame was called with.
4e1d3b43 942
36477c24 943If perl is run with B<-d> option, the following additional features
944are enabled:
a0d0e21e 945
36477c24 946=over
4e1d3b43 947
36477c24 948=item *
4e1d3b43 949
36477c24 950Perl inserts the contents of C<$ENV{PERL5DB}> (or C<BEGIN {require
951'perl5db.pl'}> if not present) before the first line of the
952application.
4e1d3b43 953
36477c24 954=item *
4e1d3b43 955
36477c24 956The array C<@{"_<$filename"}> is the line-by-line contents of
774d564b 957$filename for all the compiled files. Same for C<eval>ed strings which
958contain subroutines, or which are currently executed. The C<$filename>
36477c24 959for C<eval>ed strings looks like C<(eval 34)>.
4e1d3b43 960
36477c24 961=item *
4e1d3b43 962
36477c24 963The hash C<%{"_<$filename"}> contains breakpoints and action (it is
964keyed by line number), and individual entries are settable (as opposed
774d564b 965to the whole hash). Only true/false is important to Perl, though the
36477c24 966values used by F<perl5db.pl> have the form
774d564b 967C<"$break_condition\0$action">. Values are magical in numeric context:
36477c24 968they are zeros if the line is not breakable.
4e1d3b43 969
36477c24 970Same for evaluated strings which contain subroutines, or which are
774d564b 971currently executed. The C<$filename> for C<eval>ed strings looks like
36477c24 972C<(eval 34)>.
4e1d3b43 973
36477c24 974=item *
4e1d3b43 975
774d564b 976The scalar C<${"_<$filename"}> contains C<"_<$filename">. Same for
36477c24 977evaluated strings which contain subroutines, or which are currently
774d564b 978executed. The C<$filename> for C<eval>ed strings looks like C<(eval
36477c24 97934)>.
4e1d3b43 980
36477c24 981=item *
4e1d3b43 982
36477c24 983After each C<require>d file is compiled, but before it is executed,
984C<DB::postponed(*{"_<$filename"})> is called (if subroutine
774d564b 985C<DB::postponed> exists). Here the $filename is the expanded name of
36477c24 986the C<require>d file (as found in values of C<%INC>).
4e1d3b43 987
36477c24 988=item *
4e1d3b43 989
36477c24 990After each subroutine C<subname> is compiled existence of
774d564b 991C<$DB::postponed{subname}> is checked. If this key exists,
36477c24 992C<DB::postponed(subname)> is called (if subroutine C<DB::postponed>
993exists).
4e1d3b43 994
36477c24 995=item *
4e1d3b43 996
36477c24 997A hash C<%DB::sub> is maintained, with keys being subroutine names,
774d564b 998values having the form C<filename:startline-endline>. C<filename> has
36477c24 999the form C<(eval 31)> for subroutines defined inside C<eval>s.
4e1d3b43 1000
36477c24 1001=item *
1002
5f05dabc 1003When execution of the application reaches a place that can have
1004a breakpoint, a call to C<DB::DB()> is performed if any one of
1005variables $DB::trace, $DB::single, or $DB::signal is true. (Note that
36477c24 1006these variables are not C<local>izable.) This feature is disabled when
1007the control is inside C<DB::DB()> or functions called from it (unless
e7ea3e70 1008C<$^D & (1E<lt>E<lt>30)>).
36477c24 1009
1010=item *
1011
5f05dabc 1012When execution of the application reaches a subroutine call, a call
36477c24 1013to C<&DB::sub>(I<args>) is performed instead, with C<$DB::sub> being
1014the name of the called subroutine. (Unless the subroutine is compiled
1015in the package C<DB>.)
4e1d3b43 1016
1017=back
a0d0e21e 1018
36477c24 1019Note that no subroutine call is possible until C<&DB::sub> is defined
e7ea3e70 1020(for subroutines outside of package C<DB>). (This restriction is
1021recently lifted.)
1022
1023(In fact, for the standard debugger the same is true if C<$DB::deep>
1024(how many levels of recursion deep into the debugger you can go before
1025a mandatory break) is not defined.)
1026
1027With the recent updates the minimal possible debugger consists of one
1028line
1029
1030 sub DB::DB {}
1031
1032which is quite handy as contents of C<PERL5DB> environment
1033variable:
1034
1035 env "PERL5DB=sub DB::DB {}" perl -d your-script
1036
1037Another (a little bit more useful) minimal debugger can be created
1038with the only line being
1039
1040 sub DB::DB {print ++$i; scalar <STDIN>}
1041
1042This debugger would print the sequential number of encountered
1043statement, and would wait for your C<CR> to continue.
1044
1045The following debugger is quite functional:
1046
54310121 1047 {
1048 package DB;
1049 sub DB {}
e7ea3e70 1050 sub sub {print ++$i, " $sub\n"; &$sub}
1051 }
1052
1053It prints the sequential number of subroutine call and the name of the
774d564b 1054called subroutine. Note that C<&DB::sub> should be compiled into the
e7ea3e70 1055package C<DB>.
36477c24 1056
1057=head2 Debugger Internals
1058
1059At the start, the debugger reads your rc file (F<./.perldb> or
54310121 1060F<~/.perldb> under Unix), which can set important options. This file may
36477c24 1061define a subroutine C<&afterinit> to be executed after the debugger is
1062initialized.
1063
5f05dabc 1064After the rc file is read, the debugger reads environment variable
36477c24 1065PERLDB_OPTS and parses it as a rest of C<O ...> line in debugger prompt.
1066
1067It also maintains magical internal variables, such as C<@DB::dbline>,
1068C<%DB::dbline>, which are aliases for C<@{"::_<current_file"}>
774d564b 1069C<%{"::_<current_file"}>. Here C<current_file> is the currently
36477c24 1070selected (with the debugger's C<f> command, or by flow of execution)
1071file.
1072
774d564b 1073Some functions are provided to simplify customization. See L<"Debugger
1074Customization"> for description of C<DB::parse_options(string)>. The
36477c24 1075function C<DB::dump_trace(skip[, count])> skips the specified number
1076of frames, and returns an array containing info about the caller
774d564b 1077frames (all if C<count> is missing). Each entry is a hash with keys
36477c24 1078C<context> (C<$> or C<@>), C<sub> (subroutine name, or info about
5f05dabc 1079eval), C<args> (C<undef> or a reference to an array), C<file>, and
36477c24 1080C<line>.
1081
54310121 1082The function C<DB::print_trace(FH, skip[, count[, short]])> prints
774d564b 1083formatted info about caller frames. The last two functions may be
36477c24 1084convenient as arguments to C<E<lt>>, C<E<lt>E<lt>> commands.
1085
a0d0e21e 1086=head2 Other resources
1087
1088You did try the B<-w> switch, didn't you?
1089
1090=head1 BUGS
1091
4e1d3b43 1092You cannot get the stack frame information or otherwise debug functions
1093that were not compiled by Perl, such as C or C++ extensions.
a0d0e21e 1094
4e1d3b43 1095If you alter your @_ arguments in a subroutine (such as with B<shift>
68dc0745 1096or B<pop>, the stack backtrace will not show the original values.