next up previous contents
Next: Creating Command Aliases Up: Stepping Through Your Script Previous: Examples: Using the n

Using Breakpoints

Breakpoints are used to tell the debugger where to stop execution of your script. After the execution is stopped, the debugger prompts you to enter a debugger command. For example, you might want to set a breakpoint on a line that assigns a complicated expression to a variable. This allows you to check any variables used in the expression before it is executed.

The following listing demonstrates the different breakpoint commands you can use.

1:      sub a {

2:          my($foo) = @_;

3:

4:          print("This is function a. Foo is $foo.\n");

5:      }

6:

7:      a(10);

8:      a(5);

When the script is first loaded into the debugger, the current execution line is 7. Using the c command causes the entire program to be executed. A transcript of the debugging session might look like this:

main::(16lst04.pl:7):   a(10);

  DB<1> c

This is function a. Foo is 10.

This is function a. Foo is 5.

You can force the debugger to stop each time that a() is invoked by using the b a command. This lets you examine the @_ parameter array before the function is started.

For example:

main::(16lst04.pl:7):   a(10);

  DB<1> b a

  DB<2> c

main::a(16lst04.pl:2):      my($foo) = @_;

  DB<3> p @_

10

  DB<4> c

This is function a. Foo is 10.

main::a(16lst04.pl:2):      my($foo) = @_;

  DB<4> p @_

5

  DB<5> c

This is function a. Foo is 5.

HINT The p command, used in this example, is shorthand for the statement print("@_\n");. You can use the p command to print any variable.

You can also create conditional breakpoints. For example, you could tell the debugger to stop inside a() only if $foo is equal to 5 using the command b 4 $foo == 5. In this instance, you can't use b a $foo == 5 because $foo is a local variable. When the debugger stops just before executing a function, the parameter array is initialized but not any of the local variables. A debugging session using conditional breakpoints might look like this:

main::(16lst04.pl:7):   a(10);

  DB<1> b 4 $foo == 5

  DB<2> L

4:          print("This is function a. Foo is $foo.\n");

  break if ($foo == 5)

  DB<2> c

This is function a. Foo is 10.

main::a(16lst04.pl:4):      print("This is function a. Foo is $foo.\n");

  DB<2> c

This is function a. Foo is 5.

The debugger did not stop during the first call to a() because $foo was equal to 10. On the second call, $foo is set to 5 which causes the debugger to stop.

The L debugger command is used to display all breakpoints and their conditions. If you don't specify any conditions, a default condition of 1 is supplied. Because 1 is always true, this creates an unconditional breakpoint. If you had created an unconditional breakpoint on line 7, the L command would display the following:

4:          print("This is function a. Foo is $foo.\n");

  break if ($foo == 10)

7:      a(10);

  break if (1)

The d command is used to delete or remove breakpoints. Issuing the commands d 4 and then L would result in this display:

7:      a(10);

  break if (1)

If you want to delete all the breakpoints at once, use the D command.


next up previous contents
Next: Creating Command Aliases Up: Stepping Through Your Script Previous: Examples: Using the n
dave@cs.cf.ac.uk