Basic Debugger Commands

The debugger is a feathre of Perl that lets you step through your program one command at a time, looking for errors. On this page we'll look at seven basic commands that will get you started.

Command Action
w where are we? -- print the lines around the current point
n next: perform the next statement
s step: like n, but step into the code of subroutines
b break: set a breakpoint at a subroutine or a line number
c continue: execute the program until a breakpoint is reached or the program is finished
p print a value
q quit the debugger

Suppose we have the following program:

#!/usr/bin/perl -w
use strict;

my (@rainbow) =
("red", "orange", "yellow");

for (my $color = 0; $color < @rainbow; $color = $color+1) {
print_color($color);
}

sub print_color {
my ($c) = @_;
print "From $rainbow[$c] to $rainbow[$c+1]...\n";
}

(which you can download as colors.pl). When we run it, it gives a warning message:
C:\biol591 perl colors.pl
From red to orange...
From orange to yellow...
Use of uninitialized value in concatenation (.) at colors.pl line 13.
From yellow to ...

For this short program we might be able to spot the problem by examining the text; but it's also a good opportunity to look at the debugger. We call up the debugger by using the -d flag on the perl command:
C:\biol591 perl -d colors.pl
Perl will give us some not-very-meaningful messages, then print the first line or two of the program and stop, waiting for our input:
Default die handler restored.

Loading DB routines from perl5db.pl version 1.07
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(colors.pl:4): my (@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");

If we want to see a little more of the program we can use the w command:
  DB<1> w
1 #!/usr/bin/perl -w
2: use strict;
3
4==> my (@rainbow) =
5 ("red", "orange", "yellow");
6
7: for (my $color = 0; $color < @rainbow; $color = $color+1) {
8: print_color($color);
9 }
10

We can execute the current statment (line 4, in this example) and go on to the next with the n command:
  DB<1> n
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
and just keep going like that if we want:
  DB<1> n
main::(colors.pl:8): print_color($color);
DB<1>
n
From red to orange...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1
> n
main::(colors.pl:8): print_color($color);
DB<1>
n
From orange to yellow...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1>
n
main::(colors.pl:8): print_color($color);
DB<1>
n
Use of uninitialized value in concatenation (.) at colors.pl line 13, line 8.
From yellow to ...
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {

To leave the program we use the q command:
  DB<1> q
What we saw in our first attempt was that the warning occurred inside of theprint_color routine. We can see what happens inside the routine with the s command. Here's how we might do that: call up the debugger as before, and usen to advance to the place where we first call the print_color routine:
C:\biol591 perl -d colors.pl
Default die handler restored.
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(colors.pl:4): my (@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");
DB<1>
n
main::(colors.pl:7): for (my $color = 0; $color < @rainbow; $color = $color+1) {
DB<1>
n
main::(colors.pl:8): print_color($color);
Then we type s. Perl stops inside print_color and shows us the first statement of the routine:
  DB<1> s
main::print_color(colors.pl:12): my ($c) = @_;
You'll often find yourself doing an nright after s has stopped at a routine's first statement, so that you can examine the values sent to the routine. In the current example, after $c is set we can print it out with the pcommand:
  DB<1> n
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<1>
p $c
0
We're pretty sure that the warning occurs for a larger value of$c, though. We can skip quickly through large parts of the program by setting a breakpoint with the b command and continuing with thec command. When Perl receives the c command, it executes the program without a pause -- until it reaches a breakpoint. At the breakpoint Perl pauses and gives control back to us.

Here we'll set a breakpoint at the print_color routine and continue until we're about to execute the offending command:

  DB<2> b print_color
DB<3> c
From red to orange...
main::print_color(colors.pl:12): my ($c) = @_;
DB<3>
c
From orange to yellow...
main::print_color(colors.pl:12): my ($c) = @_;
DB<3>
c
Use of uninitialized value in concatenation (.) at colors.pl line 13, line 9.
From yellow to ...
Debugged program terminated. Use q to quit or R to restart,
use O inhibit_exit to avoid stopping after program termination,
h q, h R or h O to get additional info.

Oops! We went too far. Let's quit:
DB<3> q
...and try again.
C:\biol591 perl -d colors.pl
Default die handler restored.
Loading DB routines from perl5db.pl version 1.07
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(colors.pl:4): my(@rainbow) =
main::(colors.pl:5): ("red", "orange", "yellow");
This time we'll set a breakpoint directly at line 13, which is where the problem seems to be, and continue exactly three times, until we're about to perform the offending command:
  DB<1> b 13
DB<2> c
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<2>
c
From red to orange...
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";
DB<2>
c
From orange to yellow...
main::print_color(colors.pl:13): print "From $rainbow[$c] to $rainbow[$c+1]...\n";

Here we can look at $c, $c+1, and the size of the @rainbowarray:
  DB<2> p $c
2
DB<3>
p $c+1
3
DB<4>
p scalar @rainbox
0
Hmmmm... the @rainbow array ought to have more than zero items! Oh -- we misspelled rainbow. Let's try that again:
  DB<5> p scalar @rainbow
3
Well, if @rainbow has three items, they ought to be$rainbow[0],$rainbow[1], and $rainbow[2]. So the problem may be in trying to print $rainbow[$c+1], i.e. $rainbow[3]. We can test that by printing $rainbow[$c], which should be O.K.:
  DB<6> p $rainbow[$c]
yellow
and $rainbow[$c+1], which should give us a warning:
  DB<7> p $rainbow[$c+1]
Use of uninitialized value in print at
 (eval 10)[/usr/lib/perl5/5.6.0/perl5db.pl:1510] line 2,
line 10.
Sure enough, that's the problem. Fixing the problem is another matter -- it depends on what the program was intended to do. The debugger has helped us find the error, though, and now it's time to thank it and quit:
  DB<8> q