Very quick rdebug primer for using the debugger in Mongrel.
<h1>Setting Up your Environment for Debugging</h1>
If you’re in textmate go to Bundles > Bundle Editor > Edit Snippits… Add a new snippit like so:
<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:
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|
|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|
|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.
||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.
|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’|
|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
|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
Without an argument, the command prints the current stack
If a thread number is given then we set the context for evaluating
|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 current line
list given lines
NOTE – to turn on autolist, use ‘set autolist’
|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.|
|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.
|show||Generic command for showing things about the debugger.
|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] [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] g[lobal]show global variables
v[ar] l[ocal]show local variables
|where||alias for backtrace||(see backtrace)|
One thought 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.