Unraveling Code with the Debugger
by Daniel Allen
|
Debugger
Next, I fired up the debugger on the file: perl -d view
... which didn't work, because the program uses taint mode. (If you try, you'll see the warning, "-T" is on the #! line, it must also be used on the command line at view line 1.) That was easy to fix by running perl -dT view.
If you try that, you'll see the debugger interface:
Loading DB routines from perl5db.pl version 1.28
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(view:38): my $query = new CGI;
DB<1>
The major elements here are: an introductory message; the package, filename, and line number; the command the debugger is about to run; and a prompt showing your command-history number.
Press c (for continue) and Enter. The program will run through to completion, finishing with:
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>
Press R to restart the program ... which normally would work, but you can't restart programs with taint mode enabled. No matter. Restart the debugger with perl -dT view again.
Next, press l for a code listing with line numbers. The default is ten lines of code. You'll notice an arrow by the line prepared to execute next. Some lines have a trailing colon (:), which means that you can interrupt execution at that line. Pressing l again will show the next ten lines, and so on. l can also handle listing a range (such as l 1-40) or a function (such as l TWiki::initialize). You can move the current display back to the execution point with a period (.) or back ten lines with minus (-).
Start running the program, slowly. Press n, for next, and the display will change to show the subsequent line:
DB<8> n
main::(view:40): my $thePathInfo = $query->path_info();
DB<8>
n will always bring you to the next executable line. This command is used frequently enough that you can press Enter to repeat it. If you try this, on the fifth execution you'll see that multiline commands break up nicely on the screen:
DB<1>
main::(view:45): my( $topic, $webName, $scriptUrlPath, $userName ) =
main::(view:46): TWiki::initialize( $thePathInfo, $theRemoteUser,
main::(view:47): $theTopic, $theUrl, $query );
DB<1>
Only the first of those lines is breakable, however. Another n would skip ahead to line 49; but don't do that yet.
The next feature is code evaluation. At the debugger prompt, you may enter any Perl code to execute in the current context. This can include modifying the running program, such as defining functions or variables.
You can also run functions or examine data using normal Perl syntax. There is a special command to evaluate and dump expressions in a list context, x. Try it:
DB<1> x $query->url;
0 'http://localhost/view'
DB<2>
Notice that the command history number increments from 1 to 2, because this is the first nontrivial command you've executed. Reexecute any prior command with the shortcut !number.
The 0 signifies the array index of each element, which is useful with lists. x will also do the right thing for data structures. Try viewing your CGI object:
DB<2> x $query
0 CGI=HASH(0x8aef668)
'.charset' => 'ISO-8859-1'
'.fieldnames' => HASH(0x8a83034)
empty hash
'.parameters' => ARRAY(0x8b32aa4)
empty array
'.path_info' => ''
'escape' => 1
For deep data, you might prefer restricting the depth walking to two levels: x 2 $query .
Prev [1] [2] [3] Next