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