=head1 NAME
perldebguts - Guts of Perl debugging
=head1 DESCRIPTION
This is not the perldebug(1) manpage, which tells you how to use
the debugger. This manpage describes low-level details concerning
the debugger's internals, which range from difficult to impossible
to understand for anyone who isn't incredibly intimate with Perl's guts.
Caveat lector.
=head1 Debugger Internals
Perl has special debugging hooks at compile-time and run-time used
to create debugging environments. These hooks are not to be confused
with the I command described in L, which is
usable only if a special Perl is built per the instructions in the
F podpage in the Perl source tree.
For example, whenever you call Perl's built-in C function
from the package C, the arguments that the corresponding stack
frame was called with are copied to the C<@DB::args> array. These
mechanisms are enabled by calling Perl with the B<-d> switch.
Specifically, the following additional features are enabled
(cf. L):
=over 4
=item *
Perl inserts the contents of C<$ENV{PERL5DB}> (or C if not present) before the first line of your program.
=item *
Each array C<@{"_<$filename"}> holds the lines of $filename for a
file compiled by Perl. The same is also true for Ced strings
that contain subroutines, or which are currently being executed.
The $filename for Ced strings looks like C<(eval 34)>.
Code assertions in regexes look like C<(re_eval 19)>.
Values in this array are magical in numeric context: they compare
equal to zero only if the line is not breakable.
=item *
Each hash C<%{"_<$filename"}> contains breakpoints and actions keyed
by line number. Individual entries (as opposed to the whole hash)
are settable. Perl only cares about Boolean true here, although
the values used by F have the form
C<"$break_condition\0$action">.
The same holds for evaluated strings that contain subroutines, or
which are currently being executed. The $filename for Ced strings
looks like C<(eval 34)> or C<(re_eval 19)>.
=item *
Each scalar C<${"_<$filename"}> contains C<"_<$filename">. This is
also the case for evaluated strings that contain subroutines, or
which are currently being executed. The $filename for Ced
strings looks like C<(eval 34)> or C<(re_eval 19)>.
=item *
After each Cd file is compiled, but before it is executed,
C is called if the subroutine
C exists. Here, the $filename is the expanded name of
the Cd file, as found in the values of %INC.
=item *
After each subroutine C is compiled, the existence of
C<$DB::postponed{subname}> is checked. If this key exists,
C is called if the C subroutine
also exists.
=item *
A hash C<%DB::sub> is maintained, whose keys are subroutine names
and whose values have the form C.
C has the form C<(eval 34)> for subroutines defined inside
Cs, or C<(re_eval 19)> for those within regex code assertions.
=item *
When the execution of your program reaches a point that can hold a
breakpoint, the C subroutine is called if any of the variables
C<$DB::trace>, C<$DB::single>, or C<$DB::signal> is true. These variables
are not Cizable. This feature is disabled when executing
inside C, including functions called from it
unless C<< $^D & (1<<30) >> is true.
=item *
When execution of the program reaches a subroutine call, a call to
C<&DB::sub>(I) is made instead, with C<$DB::sub> holding the
name of the called subroutine. (This doesn't happen if the subroutine
was compiled in the C package.)
=back
Note that if C<&DB::sub> needs external data for it to work, no
subroutine call is possible without it. As an example, the standard
debugger's C<&DB::sub> depends on the C<$DB::deep> variable
(it defines how many levels of recursion deep into the debugger you can go
before a mandatory break). If C<$DB::deep> is not defined, subroutine
calls are not possible, even though C<&DB::sub> exists.
=head2 Writing Your Own Debugger
=head3 Environment Variables
The C environment variable can be used to define a debugger.
For example, the minimal "working" debugger (it actually doesn't do anything)
consists of one line:
sub DB::DB {}
It can easily be defined like this:
$ PERL5DB="sub DB::DB {}" perl -d your-script
Another brief debugger, slightly more useful, can be created
with only the line:
sub DB::DB {print ++$i; scalar }
This debugger prints a number which increments for each statement
encountered and waits for you to hit a newline before continuing
to the next statement.
The following debugger is actually useful:
{
package DB;
sub DB {}
sub sub {print ++$i, " $sub\n"; &$sub}
}
It prints the sequence number of each subroutine call and the name of the
called subroutine. Note that C<&DB::sub> is being compiled into the
package C through the use of the C directive.
When it starts, the debugger reads your rc file (F<./.perldb> or
F<~/.perldb> under Unix), which can set important options.
(A subroutine (C<&afterinit>) can be defined here as well; it is executed
after the debugger completes its own initialization.)
After the rc file is read, the debugger reads the PERLDB_OPTS
environment variable and uses it to set debugger options. The
contents of this variable are treated as if they were the argument
of an C debugger command (q.v. in L).
=head3 Debugger internal variables
In addition to the file and subroutine-related variables mentioned above,
the debugger also maintains various magical internal variables.
=over 4
=item *
C<@DB::dbline> is an alias for C<@{"::_, which
holds the lines of the currently-selected file (compiled by Perl), either
explicitly chosen with the debugger's C command, or implicitly by flow
of execution.
Values in this array are magical in numeric context: they compare
equal to zero only if the line is not breakable.
=item *
C<%DB::dbline>, is an alias for C<%{"::_, which
contains breakpoints and actions keyed by line number in
the currently-selected file, either explicitly chosen with the
debugger's C command, or implicitly by flow of execution.
As previously noted, individual entries (as opposed to the whole hash)
are settable. Perl only cares about Boolean true here, although
the values used by F have the form
C<"$break_condition\0$action">.
=back
=head3 Debugger customization functions
Some functions are provided to simplify customization.
=over 4
=item *
See L for a description of options parsed by
C.
=item *
C skips the specified number of frames
and returns a list containing information about the calling frames (all
of them, if C is missing). Each entry is reference to a hash
with keys C (either C<.>, C<$>, or C<@>), C (subroutine
name, or info about C), C (C or a reference to
an array), C, and C.
=item *
C prints
formatted info about caller frames. The last two functions may be
convenient as arguments to C<< < >>, C<< << >> commands.
=back
Note that any variables and functions that are not documented in
this manpages (or in L) are considered for internal
use only, and as such are subject to change without notice.
=head1 Frame Listing Output Examples
The C option can be used to control the output of frame
information. For example, contrast this expression trace:
$ perl -de 42
Stack dump during die enabled outside of evals.
Loading DB routines from perl5db.pl patch level 0.94
Emacs support available.
Enter h or `h h' for help.
main::(-e:1): 0
DB<1> sub foo { 14 }
DB<2> sub bar { 3 }
DB<3> t print foo() * bar()
main::((eval 172):3): print foo() + bar();
main::foo((eval 168):2):
main::bar((eval 170):2):
42
with this one, once the Cption C has been set:
DB<4> o f=2
frame = '2'
DB<5> t print foo() * bar()
3: foo() * bar()
entering main::foo
2: sub foo { 14 };
exited main::foo
entering main::bar
2: sub bar { 3 };
exited main::bar
42
By way of demonstration, we present below a laborious listing
resulting from setting your C environment variable to
the value C, and running I from the command line.
Examples use various values of C are shown to give you a feel
for the difference between settings. Long those it may be, this
is not a complete listing, but only excerpts.
=over 4
=item 1
entering main::BEGIN
entering Config::BEGIN
Package lib/Exporter.pm.
Package lib/Carp.pm.
Package lib/Config.pm.
entering Config::TIEHASH
entering Exporter::import
entering Exporter::export
entering Config::myconfig
entering Config::FETCH
entering Config::FETCH
entering Config::FETCH
entering Config::FETCH
=item 2
entering main::BEGIN
entering Config::BEGIN
Package lib/Exporter.pm.
Package lib/Carp.pm.
exited Config::BEGIN
Package lib/Config.pm.
entering Config::TIEHASH
exited Config::TIEHASH
entering Exporter::import
entering Exporter::export
exited Exporter::export
exited Exporter::import
exited main::BEGIN
entering Config::myconfig
entering Config::FETCH
exited Config::FETCH
entering Config::FETCH
exited Config::FETCH
entering Config::FETCH
=item 3
in $=main::BEGIN() from /dev/null:0
in $=Config::BEGIN() from lib/Config.pm:2
Package lib/Exporter.pm.
Package lib/Carp.pm.
Package lib/Config.pm.
in $=Config::TIEHASH('Config') from lib/Config.pm:644
in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from li
in @=Config::myconfig() from /dev/null:0
in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'osname') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'osvers') from lib/Config.pm:574
=item 4
in $=main::BEGIN() from /dev/null:0
in $=Config::BEGIN() from lib/Config.pm:2
Package lib/Exporter.pm.
Package lib/Carp.pm.
out $=Config::BEGIN() from lib/Config.pm:0
Package lib/Config.pm.
in $=Config::TIEHASH('Config') from lib/Config.pm:644
out $=Config::TIEHASH('Config') from lib/Config.pm:644
in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/
out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
out $=main::BEGIN() from /dev/null:0
in @=Config::myconfig() from /dev/null:0
in $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
out $=Config::FETCH(ref(Config), 'package') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
out $=Config::FETCH(ref(Config), 'baserev') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
out $=Config::FETCH(ref(Config), 'PERL_VERSION') from lib/Config.pm:574
in $=Config::FETCH(ref(Config), 'PERL_SUBVERSION') from lib/Config.pm:574
=item 5
in $=main::BEGIN() from /dev/null:0
in $=Config::BEGIN() from lib/Config.pm:2
Package lib/Exporter.pm.
Package lib/Carp.pm.
out $=Config::BEGIN() from lib/Config.pm:0
Package lib/Config.pm.
in $=Config::TIEHASH('Config') from lib/Config.pm:644
out $=Config::TIEHASH('Config') from lib/Config.pm:644
in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/E
out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
out $=main::BEGIN() from /dev/null:0
in @=Config::myconfig() from /dev/null:0
in $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
out $=Config::FETCH('Config=HASH(0x1aa444)', 'package') from lib/Config.pm:574
in $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
out $=Config::FETCH('Config=HASH(0x1aa444)', 'baserev') from lib/Config.pm:574
=item 6
in $=CODE(0x15eca4)() from /dev/null:0
in $=CODE(0x182528)() from lib/Config.pm:2
Package lib/Exporter.pm.
out $=CODE(0x182528)() from lib/Config.pm:0
scalar context return from CODE(0x182528): undef
Package lib/Config.pm.
in $=Config::TIEHASH('Config') from lib/Config.pm:628
out $=Config::TIEHASH('Config') from lib/Config.pm:628
scalar context return from Config::TIEHASH: empty hash
in $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
in $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
out $=Exporter::export('Config', 'main', 'myconfig', 'config_vars') from lib/Exporter.pm:171
scalar context return from Exporter::export: ''
out $=Exporter::import('Config', 'myconfig', 'config_vars') from /dev/null:0
scalar context return from Exporter::import: ''
=back
In all cases shown above, the line indentation shows the call tree.
If bit 2 of C is set, a line is printed on exit from a
subroutine as well. If bit 4 is set, the arguments are printed
along with the caller info. If bit 8 is set, the arguments are
printed even if they are tied or references. If bit 16 is set, the
return value is printed, too.
When a package is compiled, a line like this
Package lib/Carp.pm.
is printed with proper indentation.
=head1 Debugging regular expressions
There are two ways to enable debugging output for regular expressions.
If your perl is compiled with C<-DDEBUGGING>, you may use the
B<-Dr> flag on the command line.
Otherwise, one can C