In Perl, the debugger is not a separate program as it usually is in the typical compiled environment. Instead, the -d flag tells the compiler to insert source information into the parse trees it's about to hand off to the interpreter. That means your code must first compile correctly for the debugger to work on it. Then when the interpreter starts up, it pre-loads a Perl library file containing the debugger itself.
The program will halt right before the first run-time executable statement (but see below regarding compile-time statements) and ask you to enter a debugger command. Contrary to popular expectations, whenever the debugger halts and shows you a line of code, it always displays the line it's about to execute, rather than the one it has just executed.
Any command not recognized by the debugger is directly executed
(eval
'd) as Perl code in the current package. (The debugger uses the
DB package for its own state information.)
Leading white space before a command would cause the debugger to think it's NOT a debugger command but for Perl, so be careful not to do that.
If you supply another debugger command as an argument to the h command, it prints out the description for just that command. The special argument of h h produces a more compact help listing, designed to fit together on one screen.
If the output the h command (or any command, for that matter) scrolls past your screen, either precede the command with a leading pipe symbol so it's run through your pager, as in
print DB::OUT expr
in the current package. In particular,
since this is just Perl's own print function, this means that nested
data structures and objects are not dumped, unlike with the
x
command.
print
function.
main
package) using a data pretty-printer (hashes show their keys and values so
you see what's what, control characters are made printable, etc.). Make
sure you don't put the type specifier (like $
) there, just the symbol
names, like this:
Use ~pattern
and !pattern
for positive and negative regexps.
Nested data structures are printed out in a legible fashion, unlike
the print
function.
incr+1
lines starting at min
.
min
through max
.
$foo
every time line53 is passed:
|
character.) By default,
$ENV{PAGER}
will be used.
PrintRet
affects printing of return value after r
command, The option frame
affects printing messages on entry and exit
from subroutines. If frame
is 1, messages are printed on entry only;
if it's set to more than that, they'll will be printed on exit as well,
which may be useful if interdispersed with other messages.
During startup options are initialized from $ENV{PERLDB_OPTS}.
You can put additional initialization options
TTY
,
noTTY
,
ReadLine
, and
NonStop
there. Here's an example of using
the $ENV{PERLDB_OPTS}
variable:
will run the script myprogram
without human intervention, printing
out the call tree with entry and exit points. Note that N f=2
is
equivalent to
NonStop=1 frame=2
. Note also that at the moment when
this documentation was written all the options to the debugger could
be uniquely abbreviated by the first letter.
See ``Debugger Internals'' below for more details.
print DB::OUT expr
. The DB::OUT filehandle is opened to
/dev/tty, regardless of where STDOUT may be redirected to.
or even
DB<<17>>
where that number is the command number, which you'd use to access with
the built-in csh-like history mechanism, e.g. !17
would repeat
command number 17. The number of angle brackets indicates the depth of
the debugger. You could get more than one set of brackets, for example, if
you'd already at a breakpoint and then printed out the result of a
function call that itself also has a breakpoint.
If you want to enter a multi-line command, such as a subroutine definition with several statements, you may escape the newline that would normally end the debugger command with a backslash. Here's an example:
Note that this business of escaping a newline is specific to interactive commands typed into the debugger.
Here's an example of what a stack backtrace might look like:
The left-hand character up there tells whether the function was called
in a scalar or list context (we bet you can tell which is which). What
that says is that you were in the function main::infested
when you ran
the stack dump, and that it was called in a scalar context from line 10
of the file Ambulation.pm, but without any arguments at all, meaning
it was called as &infested
. The next stack frame shows that the
function Ambulation::legs
was called in a list context from the
camel_flea file with four arguments. The last stack frame shows that
main::pests
was called in a scalar context, also from camel_flea,
but from line 4.
If you have any compile-time executable statements (code within a BEGIN
block or a use
statement), these will NOT
be stopped by debugger,
although require
s will. From your own Perl code, however, you can
transfer control back to the debugger using the following statement,
which is harmless if the debugger is not running:
If you set $DB::single
to the value 2, it's equivalent to having
just typed the
n
command, whereas a value of 1 means the
s
command. The $DB::trace
variable should be set to 1 to simulate
having typed the
t
command.
You can do some customization by setting up a .perldb file which contains initialization code. For instance, you could make aliases like these (the last one is one people expect to be there):
Perl is also delivered with a start file for making emacs act like a syntax-directed editor that understands (some of) Perl's syntax. Look in the emacs directory of the Perl source distribution.
(Historically, a similar setup for interacting with vi and the X11 window system had also been available, but at the time of this writing, no debugger support for vi currently exists.)
Meanwhile, you can fetch the Devel::Dprof module from CPAN. Assuming it's properly installed on your system, to profile your Perl program in the file mycode.pl, just type:
When the script terminates the profiler will dump the profile information to a file called tmon.out. A tool like dprofpp (also supplied with the Devel::DProf package) can be used to interpret the information which is in that profile.
@DB::args
array to contain the arguments that stack frame was called
with. It also maintains other magical internal variables, such as
@DB::dbline
, an array of the source code lines for the currently
selected (with the debugger's
f
command) file. Perl effectively
inserts a call to the function DB::DB
(linenum) in front of every
place that can have a breakpoint. Instead of a subroutine call it calls
DB::sub
setting $DB::sub
being the called subroutine. It also
inserts a BEGIN {require 'perl5db.pl'}
before the first line.
Note that no subroutine call is possible until &DB::sub
is defined
(for subroutines defined outside this file). In fact, the same is
true if $DB::deep
(how many levels of recursion deep into the
debugger you are) is not defined.
At the start, the debugger reads your rc file (./.perldb or
~/.perldb under UNIX), which can set important options. This file may
define a subroutine &afterinit
to be executed after the debugger is
initialized.
After the rc file is read, the debugger reads environment variable PERLDB_OPTS and parses it as a rest of O ... line in debugger prompt.
The following options can only be specified at startup. To set them in
your rc file, call &parse_options(``optionName=new_value'')
.
Term::Rendezvous
. Current variant is to have the name of TTY in this
file.
Example rc file:
The script will run without human intervention, putting trace information into the file db.out. (If you interrupt it, you would better reset LineInfo to something ``interactive''!)
You cannot get the stack frame information or otherwise debug functions that were not compiled by Perl, such as C or C++ extensions.
If you alter your @_ arguments in a subroutine (such as with shift or pop, the stack backtrace will not show the original values.