Tuesday, July 5, 2011

Debugging in perl

To see the warning present in your perl script run your  script with -w as as follows:

perl -w  karthik.pl

It will help to find out the error in your script.
Next fine way of finding the errors is stepping through the program line by line:
For that you can run the script with  -d option.

perl -d inheritance.pl
These options will be handy for that:
s        -  for moving through the program step by  step.Even when it moves into a function it moves line by line.It is very slow maybe helpful to see how the code is executed.

n       -  it executes the next statement in the script .Although all function calls are executed it does not follow the execution path inside a function.It helps to move quicker than the s option.

c     -  it executes entire script untill it encounters a break point before the script ends.
          you can also use this option to continue untill the line number after c.
          c 40   - makes the debugger to execute untill line number 40.

No command(enter)  - makes it to execute previous n or s command previously executed.
example:
  DB<1> s
main::(perl_inheritance.pl:24): $a = new A;
  DB<1> s
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
  DB<1> s
main::(perl_inheritance.pl:26): $b=new B;
  DB<1> n
main::(perl_inheritance.pl:27): $c=new C;
Here as you may see when you use s option the debugger moves into the new method
but when use the n option it does not go into the execution path of the method and moves next line after the method.

  DB<1> c
C_Method
Method (in B)
B_Method
A_Methos
Method (in B)
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.
when you use c option it simply ends as we have not set any breakpoints


Seeing the trace how the program gets executed:

For that you have to turn on the trace mode:
typing t and pressing enter turns on the trace and typing t again and pressing Enter turns off the trace.
  DB<1> t
Trace = on
  DB<1> c
C::(perl_inheritance.pl:15):    @ISA = qw(B);
C::(perl_inheritance.pl:16):    sub C_Method{
main::(perl_inheritance.pl:24): $a = new A;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:26): $b=new B;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:27): $c=new C;
A::new(perl_inheritance.pl:2):  sub new { bless {}, shift };
A::new(perl_inheritance.pl:3):  sub A_Method {
main::(perl_inheritance.pl:28): $c->C_Method();
C::C_Method(perl_inheritance.pl:17):
17:     print "C_Method\n";
C_Method
main::(perl_inheritance.pl:29): $c->Method();
C::Method(perl_inheritance.pl:20):      print "Method (in B)\n";
Method (in B)
main::(perl_inheritance.pl:30): $c->B_Method();
B::B_Method(perl_inheritance.pl:9):
9:      print "B_Method\n";
B_Method
main::(perl_inheritance.pl:31): $c->A_Method();
A::A_Method(perl_inheritance.pl:4):
4:      print "A_Methos\n";
A_Methos
main::(perl_inheritance.pl:32): $c->Method();
C::Method(perl_inheritance.pl:20):      print "Method (in B)\n";
Method (in B)
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.
  DB<1>

Setting  the breakpoint:

b   - sets the  breakpoint
b followed by the line no sets the breakpoint in that lineno.
  DB<1> b 27
  DB<2> c
main::(perl_inheritance.pl:27): $c=new C;

As you may see i set the breakpoint at line no 27 and use option c it stops at the lineno 27.

D  - using this option deletes all the breakpoints set.you can also delete individual breakpoints using option d  followed by lineno where breakpoint is set.

For setting action:

Action is used when you want to print any  variable or string before a line no:
It can be done using the following syntax:
a  10 print("$count");

By this $count value is printed before line no.
G:\perl_programs>perl -d perl_inheritance.pl
Loading DB routines from perl5db.pl version 1.33
Editor support available.
Enter h or `h h' for help, or `perldoc perldebug' for more help.
B::(perl_inheritance.pl:7):     @ISA=qw(A);
B::(perl_inheritance.pl:8):     sub B_Method {
  DB<1> a 27 print "hello here is the action"."\n"
  DB<2> c
hello here is the action
C_Method
Method (in B)
B_Method
A_Methos
Method (in B)
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.
hello here is the action
A  - deletes all actions.

Options for displaying information:
L -  lists all breakpoints and actions.

l  - prints part of the script
    l  - prints 10 lines of script
    l  <lineno>  - prints the particular line in the script.
    l   5+4  - lists 4 lines starting with line  5
    l   4-8     - lists lines from 4 to 8

S  - prints all function names defined including those in external scripts

T  - prints the stack trace.The trace of the entire program how it gets executed.

V  -  prints the variables defined from all packages and modules loaded

X  -  list variables only in the current package

Finally
q   - can be used to quit from the debugger


No comments:

Post a Comment