Using The Perl Debugger - Acts Of Madness (Page 5 of 6 )
The Perl debugger also allows you to define actions - code that is executed when a specific line is reached. This comes in handy when you need to try a piece of code with different variable values, or print notification before specific lines are executed.
Actions are set with the "a" command, which needs a line number and a line of code to be executed before the debugger executes the specified line. Consider the following example, which demonstrates:
DB<7> l 24-44
24==> foreach $line (@file)
25 {
26 # split on record delimiter
27: @record = split(":", $line);
28
29 # print username if UID >= 500
30: if ($record[2] >= 500)
31 {
32: print "$record[0]($record[2])\n";
33: $count++
34 }
35 }
DB<10> b 30
DB<11> a 30 print "Checking UID $record[2]\n"
In this case, when the debugger reaches line 30, it will automatically halt (because I set a breakpoint) and also print the value of the second element of the array $record (because I told it to via the "a" command).
DB<21> c
Checking UID 3
main::(passwd.pl:30): if ($record[2] >= 500)
main::(passwd.pl:31): {
DB<21> c
Checking UID 534
all(534)
main::(passwd.pl:30): if ($record[2] >= 500)
main::(passwd.pl:31): {
Checking UID 516
...
Actions can be deleted with the "A" command, which can be supplied with either a line number
DB<12> A 27
or the * wildcard.
DB<13> A *
Deleting all actions...
* Following The Trail
As you might have guessed from my frequent use of this command in some of the previous examples, the "L" command provides a list of all currently-set breakpoints, actions and watch-expressions.
DB<4> L
passwd.pl:
27: @record = split(":", $line);
break if (1)
action: print $line
Watch-expressions:
@record
You can also obtain a list of previously-entered debugger commands with the "H" command, which displays a command history,
DB<7> H
6: b 11
5: A *
4: B *
3: l 19-25
2: v 18
1: l 12
and even repeat previous commands with the "!" command.
DB<7> ! 4
B *
Deleting all breakpoints...
You can obtain a stack trace with the "T" command, as in the following
example:
DB<5> T
. = DBI::_setup_driver('DBD::mysql') called from file `/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/DBI.pm' line 629 $ = DBI::install_driver('DBI', 'mysql') called from file `/usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/DBI.pm' line 497 $ = DBI::connect('DBI', 'DBI:mysql:database=db198;host=localhost', 'root', 'secret', ref(HASH)) called from file `mailer.pl' line 16
This is fairly easy to decipher, so long as you start with the last line first. In the example above, it's clear to see that the script called the
DBI::connect() function, which called DBI::install_driver(), which in turn invoked DBI::_setup_driver() from the DBI.pm module. This stack trace is useful to evaluate the control flow within your program and the libraries it interacts with.
You can also customize the debugger with the "o" command, which lets you alter debugger options. Try typing it at the debugger prompt and you should see something like this:
DB<1> o
hashDepth = 'N/A'
arrayDepth = 'N/A'
CommandSet = '580'
dumpDepth = 'N/A'
DumpDBFiles = 'N/A'
DumpPackages = 'N/A'
DumpReused = 'N/A'
compactDump = 'N/A'
veryCompact = 'N/A'
quote = 'N/A'
HighBit = 'N/A'
undefPrint = 'N/A'
globPrint = 'N/A'
PrintRet = '1'
UsageOnly = 'N/A'
frame = '0'
AutoTrace = '0'
TTY = '/dev/tty'
noTTY = 'N/A'
ReadLine = '1'
NonStop = '0'
LineInfo = '/dev/tty'
maxTraceLen = '400'
recallCommand = '!'
ShellBang = '!'
pager = '|/usr/bin/less -isr'
tkRunning = 'N/A'
ornaments = 'us,ue,md,me'
signalLevel = '1'
warnLevel = '1'
dieLevel = '1'
inhibit_exit = '1'
ImmediateStop = 'N/A'
bareStringify = 'N/A'
CreateTTY = '3'
RemotePort = 'N/A'
windowSize = '10'
As you may have guessed, these are all internal debugger options which can be customized as per your requirements. To alter a particular options, simply use the "o" command followed by the option name and its value, as
below:
DB<4> o windowSize=15
windowSize = '15'
Here's a brief list of the more interesting options available for
customization:
"RecallCommand" - the command used to repeat previous commands ("!) by default);
"pager" - the program to use when paging through long screens of output;
"hashDepth" - the depth to which hashes are displayed;
"arrayDepth" - the depth to which arrays are displayed;
"dumpDepth" - the depth to which structures are displayed;
"inhibit_exit" - continue debugger session even after the script has ended;
"windowSize" - number of lines of code to display with "v" and "l" commands;
Next: Test Drive >>
More Perl Articles
More By icarus, (c) Melonfire