Perl Debugging Tools – Perl Debugger Commands

August 11th, 2009 by admin | Filed under PERL Debugging

A quick summary of the commands provided by Perl Debugger and how to go about with debugging perl scripts.

Related Terms – Perl Debugger Tool, Debugging Perl Cgi Scripts, Perl Ide Debugger, Perl Debug Command Line, Perl Debugger Unix, Perl Debugger Commands, Perl Tk Debugger, Perl Debug Module

warn LIST

Concatenates LIST, and prints result to STDERR. Program flow then continues uninterupted. If LIST is empty, uses the default message “Warning: Something’s wrong” if last element of LIST does not end in a newline, appends “at [filename], line [line#]“

die LIST

Like warn, prints the concatenated list to STDERR. Program then exits, with the current value of $! as its exit code. –($! is an error code in numeric context, and the corresponding error message in string context) Like warn, will print the file name and line number (and input line number, if applicable), if the last element of LIST does not end with a newline.

Special Tokens

In your internal error messages, it’s often helpful to know what line (and sometimes what file) program execution was at when the message was printed. Two tokens: __FILE__ and __LINE__ Hold the filename and linenumber, respectively. Note: These are NOT variables, so do not try to interpolate them!!

Data::Dumper

http://www.perldoc.com/perl5.6/lib/Data/Dumper.html

Very useful function for seeing exactly what is in a complex structure. To use, first import the module: use Data::Dumper; Then, call Dumper with a reference to the structure you want to investigate, and print the results: print Dumper(\%bigHash);

Dumper return value – Dumper returns a pretty-printed string. The string contains the entire expanded contents of the data structure. Any internal data structure is also expanded.

$VAR1 = {
'Lalli' => [
100,
95,
86
],
'Smith' => [
87,
92,
100
]
};

Useful Perl Debugger Command Line Switches

  • –Know it. Use it. Love it.
  • –Simply check your program for compilation errors, without bothering to actually run the program.
  • –Very useful for large programs, or when machine resources are tight (for example, your CGI homework next week)
  • –Evaluate one line of perl code, and return
  • –ex:
  • perl -e ‘$a = 42; print “\$a = $a\n”;’

The Perl Debugger

Command-line, interactive debugging program, written (of course) in Perl. Start the debugger by supplying the -d switch: perl -d myfile.pl This isn’t actually a separate program. Instead, -d inserts debugger library code, then executes your program as normal, halting before first run-time executable statement.

now what? The debugger halts just before the first run-time executable statement, and prints that statement to the screen, giving the prompt: DB<1> It then waits for you to type a debugger command or a perl statement to be evaluated Maybe most important command is h h –compact listing of debugger commands Also simply h –expanded listing of debugger commands precede any command with | to page the output –(so, give |h instead of just h)

Debugger commands

Stepping and Running

  • s – Single-step through the program. Execute one statement at a time. If statement is a function call, go into that function and halt at the first statement
  • –(equiv of MSVC’s “Step Into” command)
  • n – Single-step on same level. Execute one statement at a time. If statement is a function call, evaluate the function, and continue at next statement
  • –(equiv of MSVC’s “Step Over” command)
  • – repeat the last s or n command
  • r – run until the end of the currently executing subroutine, display the return value, and halt at next line after subroutine call.

Breakpoints

  • b LINE CONDITION – set a breakpoint at line number LINE if CONDITION is true. Debugger flow will halt only if CONDITION is true when it reaches LINE. Omit CONDITION to always halt. Omit LINE to set the breakpoint at the current about-to-be-executed line.
  • b SUBNAME CONDITION – set a breakpoint at the first line of the subroutine SUBNAME. Flow halts only if CONDITION is true when SUBNAME is called. Omit CONDITION to always halt.
  • d LINE – delete the breakpoint at line number LINE. Omit LINE to delete the breakpoint at the current about-to-be-executed line.
  • D – delete all breakpoints (careful! there is no undelete!)
  • L – list all breakpoints and their associated actions
  • c – continue execution (ie, run all statements) until the next breakpoint
  • c LINE – set a one-time breakpoint at LINE, run to that LINE, and remove the breakpoint.

Tracing

  • T – Excute a stack backtrace. This shows the sequence of function calls that took your program to the current point of execution
  • –(ie, “foo was called by bar at line 35; bar was called by baz at line 453; baz was called by …”)
  • t – toggle Trace Mode. If on, every line of the program is printed as it is evaluated
  • W EXPR – add EXPR as a global watch expression. Program will halt whenever value of EXPR changes.
  • W – delete all watch expressions (careful! There is no undelete!!)

Display

  • p EXPR – print EXPR. Literally. This is perl’s print function, with output redirected to the Debugger console
  • x EXPR – pretty-print EXPR, expanding nested structures in the same manner as Data::Dumper
  • V PKG – do an x on each ‘global’ variable in the package PKG
  • X – do a V on the currently-executing package

Locating Code

  • l – list the next few lines. Several optional arguments:
  • –l LINE – list line number LINE
  • –l SUBNAME – list first few lines of SUBNAME
  • –l MIN+INCR – list INCR+1 lines, starting at line number MIN
  • –l MIN-MAX – list lines MIN through MAX
  • - – list the previous few lines.
  • w LINE – list a ‘window’ surrounding line LINE, or current line if omitted.
  • . – return internal debugger pointer to the current line.
  • /PATTERN/ – search forward for PATTERN
  • ?PATTERN? – search backward for PATTERN
  • S PATTERN – list subroutine names matching PATTERN, or all subroutines if PATTERN omitted.

Actions & Command execution

  • a LINE COMMAND – execute COMMAND every time LINE is evaluated. Omit LINE for current line, omit COMMAND to delete the command
  • A – delete all actions
  • < EXPR - evaluate perl expression before every debugger prompt
  • –<< EXPR - add another expression
  • –< ? - list all expressions
  • –< - delete all expressions
  • > EXPR – same as <, but execute expression after the debugger prompt. (associated commands likewise)
  • { COMMAND – specify a debugger command to be executed before each prompt (as opposed to a perl expression)
  • –{{ COMMAND – add an additional command
  • –{ ? – list all commands
  • –{ – delete all commands
  • ! – repeat the previous command.
  • ! NUMBER – repeat the NUMBER’th command
  • ! -NUMBER – repeat the NUMBER’th-from-last command

Miscellaneous Commands

  • q – quit the debugger
  • ^D – quit the debugger
  • R – restart the debugger, with breakpoints, actions, etc preserved
  • = ALIAS VALUE – define a new debugger comand VALUE named ALIAS
  • = ALIAS – print the value of ALIAS
  • = – list all current aliases

References for more help with Perl Debugger

  1. Camel, chapter 20
  2. perldoc perldebug

Related Posts:

tag_iconTags: | | | | | | |

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a comment.

To leave a comment, please fill in the fields below.

Powered by Yahoo! Answers