Ruby Debugger

Advertisements


It doesn't matter how easy a language is to use, it usually contains some bugs if it is more than a few lines long. To help deal with bugs, the standard distribution of Ruby includes a debugger.

In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.

Usage Syntax:

Here is the usage syntax to use ruby debugger:

$ ruby -r debug filename[, ...]

Ruby Debugger Commands:

Here is a complete list of commands, which you can use while debugging your program. Here, it is not necessary to use complete keyword to give a command, part given inside [...] is option.

SNCommand with Description
1b[reak] [< file| class>:]< line| method>
Sets breakpoint to some position. Breakpoint is a place where you want to pause program execution for debugging purpose.
2wat[ch] expression
Sets watchpoints
3cat[ch] (exception|off)
Sets catchpoint to an exception.
4b[reak]
Displays breakpoints and watchpoints
5del[ete] [n]
Deletes breakpoints
6disp[lay] expression
Displays value of expression
7undisp[lay] [ n]
Removes display of n
8c[ont]
Continues execution
9s[tep] [ n]
Executes next n lines stepping into methods
10n[ext] [ n]
Executes next n lines stepping over methods
11w[here]
Displays stack frame
12f[rame]
Synonym for where
13l[ist][<-| n- m>]
Displays source lines from n to m
14up [ n]
Moves up n levels in the stack frame
15down [ n]
Moves down n levels in the stack frame
16fin[ish]
Finishes execution of the current method
17tr[ace] [on|off]
Toggles trace mode on and off
18q[uit]
Exits debugger
19v[ar] g[lobal]
Displays global variables
20v[ar] l[ocal]
Displays local variables
21v[ar] i[instance] object
Displays instance variables of object
22v[ar] c[onst] object
Displays constants of object
23m[ethod] i[instance] object
Displays instance methods of object
24m[ethod] class| module
Displays instance methods of the class or module
25th[read] l[ist]
Displays threads
26th[read] c[ur[rent]]
Displays current thread
27th[read] n
Stops specified thread
28th[read] stop >
Synonym for th[read] n
29th[read] c[ur[rent]] n>
Synonym for th[read] n
30th[read] resume >
Resumes thread n
31p expression
Evaluates the expression
32h[elp]
Displays help message
33everything else
Evaluates.

Example:

Consider the following file hello.rb, which needs to be debugged:

#!/usr/bin/env ruby
class Hello
   def initialize( hello )
      @hello = hello
   end
   def hello
      @hello
   end
end

salute = Hello.new( "Hello, Mac!" )
puts salute.hello

Here is one interactive session captured. Given commands are written in bold:

[root@ruby]# ruby -r debug hello.rb
Debug.rb
Emacs support available.

hello.rb:3:class Hello
(rdb:1) v l
  salute => nil
(rdb:1) b 10
Set breakpoint 1 at hello.rb:10
(rdb:1) c
Hello, Mac!
[root@ruby]#


Advertisements
Advertisements