You are viewing the version of this documentation from Perl 5.43.1. This is a development version of Perl.

CONTENTS

NAME

perldebguts - Guts of Perl debugging

DESCRIPTION

This is not perldebug, 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.

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 perl -Dxxx command described in perlrun, which is usable only if a special Perl is built per the instructions in the INSTALL file in the Perl source tree.

For example, whenever you call Perl's built-in caller function from the package DB, the arguments that the corresponding stack frame was called with are copied to the @DB::args array. These mechanisms are enabled by calling Perl with the -d switch. Specifically, the following additional features are enabled (cf. "$^P" in perlvar):

Note that if &DB::sub needs external data for it to work, no subroutine call is possible without it. As an example, the standard debugger's &DB::sub depends on the $DB::deep variable (it defines how many levels of recursion deep into the debugger you can go before a mandatory break). If $DB::deep is not defined, subroutine calls are not possible, even though &DB::sub exists.

Writing Your Own Debugger

Environment Variables

The PERL5DB 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 <STDIN>}

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 &DB::sub is being compiled into the package DB through the use of the package directive.

When it starts, the debugger reads your rc file (./.perldb or ~/.perldb under Unix), which can set important options. (A subroutine (&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 o ... debugger command (q.v. in "Configurable Options" in perldebug).

Debugger Internal Variables

In addition to the file and subroutine-related variables mentioned above, the debugger also maintains various magical internal variables.

Debugger Customization Functions

Some functions are provided to simplify customization.

Note that any variables and functions that are not documented in this manpages (or in perldebug) are considered for internal use only, and as such are subject to change without notice.

Frame Listing Output Examples

The frame 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 option frame=2 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 PERLDB_OPTS environment variable to the value f=n N, and running perl -d -V from the command line. Examples using various values of n are shown to give you a feel for the difference between settings. Long though it may be, this is not a complete listing, but only excerpts.

  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
  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
  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
  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
  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
  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: ''

In all cases shown above, the line indentation shows the call tree. If bit 2 of frame 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.

Debugging Regular Expressions

There are two ways to enable debugging output for regular expressions.

If your perl is compiled with -DDEBUGGING, you may use the -Dr flag on the command line, and -Drv for more verbose information.

Otherwise, one can use re 'debug', which has effects at both compile time and run time. Since Perl 5.9.5, this pragma is lexically scoped.

Compile-time Output

The debugging output at compile time looks like this:

Compiling REx '[bc]d(ef*g)+h[ij]k$'
size 45 Got 364 bytes for offset annotations.
first at 1
rarest char g at 0
rarest char d at 0
   1: ANYOF[bc](12)
  12: EXACT <d>(14)
  14: CURLYX[0] {1,32767}(28)
  16:   OPEN1(18)
  18:     EXACT <e>(20)
  20:     STAR(23)
  21:       EXACT <f>(0)
  23:     EXACT <g>(25)
  25:   CLOSE1(27)
  27:   WHILEM[1/1](0)
  28: NOTHING(29)
  29: EXACT <h>(31)
  31: ANYOF[ij](42)
  42: EXACT <k>(44)
  44: EOL(45)
  45: END(0)
anchored 'de' at 1 floating 'gh' at 3..2147483647 (checking floating) 
      stclass 'ANYOF[bc]' minlen 7 
Offsets: [45]
	1[4] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 5[1]
	0[0] 12[1] 0[0] 6[1] 0[0] 7[1] 0[0] 9[1] 8[1] 0[0] 10[1] 0[0]
	11[1] 0[0] 12[0] 12[0] 13[1] 0[0] 14[4] 0[0] 0[0] 0[0] 0[0]
	0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 18[1] 0[0] 19[1] 20[0]  
Omitting $` $& $' support.

The first line shows the pre-compiled form of the regex. The second shows the size of the compiled form (in arbitrary units, usually 4-byte words) and the total number of bytes allocated for the offset/length table, usually 4+size*8. The next line shows the label id of the first node that does a match.

The

anchored 'de' at 1 floating 'gh' at 3..2147483647 (checking floating) 
      stclass 'ANYOF[bc]' minlen 7 

line (split into two lines above) contains optimizer information. In the example shown, the optimizer found that the match should contain a substring de at offset 1, plus substring gh at some offset between 3 and infinity. Moreover, when checking for these substrings (to abandon impossible matches quickly), Perl will check for the substring gh before checking for the substring de. The optimizer may also use the knowledge that the match starts (at the first id) with a character class, and no string shorter than 7 characters can possibly match.

The fields of interest which may appear in this line are

anchored STRING at POS
floating STRING at POS1..POS2

See above.

matching floating/anchored

Which substring to check first.

minlen

The minimal length of the match.

stclass TYPE

Type of first matching node.

noscan

Don't scan for the found substrings.

isall

Means that the optimizer information is all that the regular expression contains, and thus one does not need to enter the regex engine at all.

GPOS

Set if the pattern contains \G.

plus

Set if the pattern starts with a repeated char (as in x+y).

implicit

Set if the pattern starts with .*.

with eval

Set if the pattern contain eval-groups, such as (?{ code }) and (??{ code }).