Categories
Programming

Ruby Debugger Primer

Very quick rdebug primer for using the debugger in Mongrel.

<h1>Setting Up your Environment for Debugging</h1>

gem install ruby-debug

If you’re in textmate go to Bundles > Bundle Editor > Edit Snippits… Add a new snippit like so:
[IMAGE:X]

<h1>How to Debug</h1>

1) Set a breakpoint in your code. Do this by typing “debug” followed by tab in TextMate. If setup correctly, TextMate will replace your debug statement with this:

require “rubygems”; require “ruby-debug”; debugger

2) Fire up mongrel (script/server -p 3000)

3) Run your application in the browser. At the breakpoint, Mongrel will stop (this will mean your browser will “hang” while it is loading the page). At this point, you can debug.

Use <strong>p</strong> to see the value of any variable (actually you’ll probably want <strong>pp</strong> for “pretty print”).

Use <strong>c</strong> to <strong>continue</strong> (go back to running code).

Use <strong>l =</strong> to see the line you are on.

Command Alias Usage What it does
help Type ‘help ‘ for help on a specific command
backtrace bt Print the entire stack frame. Each frame is numbered, the most recent frame is 0. frame number can be referred to in the “frame” command;
“up” and “down” add or subtract respectively to frame numbers shown.
The position of the current frame is marked with ->.
break b b[reak] file:line [if expr]
b[reak] class(.|#)method [if expr]
set breakpoint to some position, (optionally) if expr == true
catch cat cat[ch] Intercept when there would otherwise be is no handler for it.
condition Condition breakpoint-number expression Specify breakpoint number N to break only if COND is true.
N is an integer and COND is an expression to be evaluated whenever
breakpoint N is reached. If the empty string is used, the condition is removed.
continue c or cont c [ nnn] run until program ends, hits a breakpoint or reaches line nnn
delete del del [ nnn…] delete some or all breakpoints
disable Disable some things.

A disabled item is not forgotten, but has no effect until reenabled.
Use the “enable” command to have it take effect again.

List of disable subcommands:

disable breakpoints – Disable some breakpoints
disable display – Disable some display expressions when program stops

display disp disp or disp add expression into display expression list (when used with expression); or display expression list when no expression is specified
down down[count] move to lower frame
edit Edit specified file.

With no argument, edits file containing most recent line listed.
Editing targets can also be specified in this:
FILE:LINENUM, to edit at that line in that file,

enable Enable some things.
This is used to cancel the effect of the “disable” command.

List of enable subcommands:

enable breakpoints – Enable specified breakpoints
enable display – Enable some expressions to be displayed when program stops
eval e (also alias for p) e evaluate expression and print its value. * NOTE – to turn on autoeval, use ‘set autoeval’
exit see quit
finish fin fin [frame-number] Execute until selected stack frame returns. If no frame number is given, we run until the currently selected frame
returns. The currently selected frame starts out the most-recent
frame or 0 if no frame positioning (e.g “up”, “down” or “frame”) has
been performed. If a frame number is given we run until that frame
returns.
frame f f frame-number [thread thread-number] Move the current frame to the specified frame number.

A negative number indicates position from the other end. So
‘frame -1’ moves to the oldest frame, and ‘frame 0’ moves to
the newest frame.

Without an argument, the command prints the current stack
frame. Since the current position is redisplayed, it may trigger a
resyncronization if there is a front end also watching over
things.

If a thread number is given then we set the context for evaluating
expressions to that frame of that thread.

help help Type ‘help ‘ for help on a specific command.
info Generic command for showing things about the program being debugged.

List of info subcommands:

info args – Argument variables of current stack frame
info breakpoints – Status of user-settable breakpoints
info catch – Exceptions that can be caught in the current stack frame
info display – Expressions to display when program stops
info file – Info about a particular file read in
info files – File names and timestamps of files read in
info global_variables – Global variables
info instance_variables – Instance variables of the current stack frame
info line – Line number and file name of current position in source file
info locals – Local variables of the current stack frame
info program – Execution status of the program
info stack – Backtrace of the stack
info thread – List info about thread NUM
info threads – information of currently-known threads
info variables – Local and instance variables of the current stack frame
irb starts an Interactive Ruby (IRB) session.
list l l or l – or l = or l nn-mm l
list forward
l –
list backward
l =
list current line
l nn-mm
list given lines

NOTE – to turn on autolist, use ‘set autolist’

method m m i
m iv
m
show methods of object (m i); show instance variables of object (m iv); show instance methods of class or module
next n[+-]?[ nnn] step over once or nnn times, ‘+’ forces to move to another line. ‘-‘ is the opposite of ‘+’ and disables the force_stepping setting.
p see eval
pp pp expression evaluate expression and pretty-print its value
ps ps expression evaluate expression, an array, sort, and columnize its value
putl putl expression evaluate expression, an array, and columnize its value
quit exit q [unconditionally] Normally we prompt before exiting. However if the parameter
“unconditionally” is given, we stop without asking further questions
reload r forces source code reloading
restart restart|R [args] Restart the program. This is is a re-exec – all debugger state
is lost. If command arguments are passed those are used.
save save [FILE] Saves current debugger state to FILE as a script file.
This includes breakpoints, catchpoints, display expressions and some settings.
If no filename is given, we will fabricate one.

Use the ‘source’ command in another debug session to restore them.

set Modifies parts of the ruby-debug environment. Boolean values take
on, off, 1 or 0.
You can see these environment settings with the “show” command.


List of set subcommands:

set annotate – Set annotation level
set args – Set argument list to give program being debugged when it is started
set autoeval – Evaluate every unrecognized command
set autolist – Execute ‘list’ command on every breakpoint
set autoirb – Invoke IRB on every stop
set autoreload – Reload source code when changed
set basename – Report file basename only showing file names
set callstyle – Set how you want call parameters displayed
set debuggertesting – Used when testing the debugger
set forcestep – Make sure ‘next/step’ commands always move to a new line
set fullpath – Display full file names in frames
set history – Generic command for setting command history parameters
set keep-frame-bindings – Save frame binding on each call
set linetrace+ – Set line execution tracing to show different lines
set linetrace – Set line execution tracing
set listsize – Set number of source lines to list by default
set trace – Display stack trace when ‘eval’ raises exception
set width – Number of characters the debugger thinks are in a line

show Generic command for showing things about the debugger.


List of show subcommands:

show annotate – Show annotation level
show args – Show argument list to give program being debugged when it is started
show autoeval – Show if unrecognized command are evaluated
show autolist – Show if ‘list’ commands is run on breakpoints
show autoirb – Show if IRB is invoked on debugger stops
show autoreload – Show if source code is reloaded when changed
show basename – Show if basename used in reporting files
show callstyle – Show paramater style used showing call frames
show commands – Show the history of commands you typed
show forcestep – Show if sure ‘next/step’ forces move to a new line
show fullpath – Show if full file names are displayed in frames
show history – Generic command for showing command history parameters
show keep-frame-bindings – Save frame binding on each call
show linetrace – Show line execution tracing
show linetrace+ – Show if consecutive lines should be different are shown in tracing
show listsize – Show number of source lines to list by default
show port – Show server port
show post-mortem – Show whether we go into post-mortem debugging on an uncaught exception
show trace – Show if a stack trace is displayed when ‘eval’ raises exception
show version – Show what version of the debugger this is
show width – Show the number of characters the debugger thinks are in a line

source source FILE executes a file containing debugger commands
step s[+-]?[ nnn] step (into methods) once or nnn times; ‘+’ forces to move to another line.; ‘-‘ is the opposite of ‘+’ and disables the force_stepping setting.
thread h[read] l[ist]list all threads

th[read] stop stop thread nnn

th[read] resume resume thread nnn

th[read] [sw[itch]] switch thread context to nnn

th[read] [cur[rent]]show current thread

tmate tm tm n opens a current file in TextMate. It uses n-th frame if arg (n) is specifed.
trace tr[ace] (on|off)set trace mode of current thread
tr[ace] (on|off) allset trace mode of all threads
tr[ace] var(iable) VARNAME [stop|nostop]set trace variable on VARNAME
undisplay undisp[ nnn] Cancel some expressions to be displayed when program stops.
Arguments are the code numbers of the expressions to stop displaying.
No argument means cancel all automatic-display expressions.
“delete display” has the same effect as this command.
Do “info display” to see current list of code numbers.
up up[count] move to higher frame
var v v[ar] cl[ass] show class variables of self

v[ar] c[onst] show constants of object

v[ar] g[lobal]show global variables

v[ar] i[nstance] show instance variables of object

v[ar] l[ocal]show local variables

where alias for backtrace (see backtrace)

2 replies on “Ruby Debugger Primer”

man this post is from 11 years ago and the formatting is all messed up (and the images are missing) and you still like it! I will try to fix the formatting so this page looks a little better.

Leave a Reply

OR LOG IN WITH




Your email address will not be published. Required fields are marked *