source: trunk/essentials/dev-lang/perl/pod/perldebguts.pod@ 3609

Last change on this file since 3609 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 31.2 KB
Line 
1=head1 NAME
2
3perldebguts - Guts of Perl debugging
4
5=head1 DESCRIPTION
6
7This is not the perldebug(1) manpage, which tells you how to use
8the debugger. This manpage describes low-level details concerning
9the debugger's internals, which range from difficult to impossible
10to understand for anyone who isn't incredibly intimate with Perl's guts.
11Caveat lector.
12
13=head1 Debugger Internals
14
15Perl has special debugging hooks at compile-time and run-time used
16to create debugging environments. These hooks are not to be confused
17with the I<perl -Dxxx> command described in L<perlrun>, which is
18usable only if a special Perl is built per the instructions in the
19F<INSTALL> podpage in the Perl source tree.
20
21For example, whenever you call Perl's built-in C<caller> function
22from the package C<DB>, the arguments that the corresponding stack
23frame was called with are copied to the C<@DB::args> array. These
24mechanisms are enabled by calling Perl with the B<-d> switch.
25Specifically, the following additional features are enabled
26(cf. L<perlvar/$^P>):
27
28=over 4
29
30=item *
31
32Perl inserts the contents of C<$ENV{PERL5DB}> (or C<BEGIN {require
33'perl5db.pl'}> if not present) before the first line of your program.
34
35=item *
36
37Each array C<@{"_<$filename"}> holds the lines of $filename for a
38file compiled by Perl. The same is also true for C<eval>ed strings
39that contain subroutines, or which are currently being executed.
40The $filename for C<eval>ed strings looks like C<(eval 34)>.
41Code assertions in regexes look like C<(re_eval 19)>.
42
43Values in this array are magical in numeric context: they compare
44equal to zero only if the line is not breakable.
45
46=item *
47
48Each hash C<%{"_<$filename"}> contains breakpoints and actions keyed
49by line number. Individual entries (as opposed to the whole hash)
50are settable. Perl only cares about Boolean true here, although
51the values used by F<perl5db.pl> have the form
52C<"$break_condition\0$action">.
53
54The same holds for evaluated strings that contain subroutines, or
55which are currently being executed. The $filename for C<eval>ed strings
56looks like C<(eval 34)> or C<(re_eval 19)>.
57
58=item *
59
60Each scalar C<${"_<$filename"}> contains C<"_<$filename">. This is
61also the case for evaluated strings that contain subroutines, or
62which are currently being executed. The $filename for C<eval>ed
63strings looks like C<(eval 34)> or C<(re_eval 19)>.
64
65=item *
66
67After each C<require>d file is compiled, but before it is executed,
68C<DB::postponed(*{"_<$filename"})> is called if the subroutine
69C<DB::postponed> exists. Here, the $filename is the expanded name of
70the C<require>d file, as found in the values of %INC.
71
72=item *
73
74After each subroutine C<subname> is compiled, the existence of
75C<$DB::postponed{subname}> is checked. If this key exists,
76C<DB::postponed(subname)> is called if the C<DB::postponed> subroutine
77also exists.
78
79=item *
80
81A hash C<%DB::sub> is maintained, whose keys are subroutine names
82and whose values have the form C<filename:startline-endline>.
83C<filename> has the form C<(eval 34)> for subroutines defined inside
84C<eval>s, or C<(re_eval 19)> for those within regex code assertions.
85
86=item *
87
88When the execution of your program reaches a point that can hold a
89breakpoint, the C<DB::DB()> subroutine is called if any of the variables
90C<$DB::trace>, C<$DB::single>, or C<$DB::signal> is true. These variables
91are not C<local>izable. This feature is disabled when executing
92inside C<DB::DB()>, including functions called from it
93unless C<< $^D & (1<<30) >> is true.
94
95=item *
96
97When execution of the program reaches a subroutine call, a call to
98C<&DB::sub>(I<args>) is made instead, with C<$DB::sub> holding the
99name of the called subroutine. (This doesn't happen if the subroutine
100was compiled in the C<DB> package.)
101
102=back
103
104Note that if C<&DB::sub> needs external data for it to work, no
105subroutine call is possible without it. As an example, the standard
106debugger's C<&DB::sub> depends on the C<$DB::deep> variable
107(it defines how many levels of recursion deep into the debugger you can go
108before a mandatory break). If C<$DB::deep> is not defined, subroutine
109calls are not possible, even though C<&DB::sub> exists.
110
111=head2 Writing Your Own Debugger
112
113=head3 Environment Variables
114
115The C<PERL5DB> environment variable can be used to define a debugger.
116For example, the minimal "working" debugger (it actually doesn't do anything)
117consists of one line:
118
119 sub DB::DB {}
120
121It can easily be defined like this:
122
123 $ PERL5DB="sub DB::DB {}" perl -d your-script
124
125Another brief debugger, slightly more useful, can be created
126with only the line:
127
128 sub DB::DB {print ++$i; scalar <STDIN>}
129
130This debugger prints a number which increments for each statement
131encountered and waits for you to hit a newline before continuing
132to the next statement.
133
134The following debugger is actually useful:
135
136 {
137 package DB;
138 sub DB {}
139 sub sub {print ++$i, " $sub\n"; &$sub}
140 }
141
142It prints the sequence number of each subroutine call and the name of the
143called subroutine. Note that C<&DB::sub> is being compiled into the
144package C<DB> through the use of the C<package> directive.
145
146When it starts, the debugger reads your rc file (F<./.perldb> or
147F<~/.perldb> under Unix), which can set important options.
148(A subroutine (C<&afterinit>) can be defined here as well; it is executed
149after the debugger completes its own initialization.)
150
151After the rc file is read, the debugger reads the PERLDB_OPTS
152environment variable and uses it to set debugger options. The
153contents of this variable are treated as if they were the argument
154of an C<o ...> debugger command (q.v. in L<perldebug/Options>).
155
156=head3 Debugger internal variables
157In addition to the file and subroutine-related variables mentioned above,
158the debugger also maintains various magical internal variables.
159
160=over 4