arrow_backward Back to blog

Debugging Rails Applications With ruby-debug

I worked with GNU C/C++ for a long time before I met Ruby. The two languages are very different; they have their own strengths and weaknesses in different application scenarios. But there is one nice thing that they have in common: they both have great debuggers for their developers who are hitting their heads on their keyboards trying to find out what has gone wrong with their applications.

My associate, Yong Zhi, wrote a great blog post on how to debug a Ruby application, a standalone script, or a full-stacked Rails application with GDB, GNU Project Debugger. Within the Ruby community there is always more than one option you can choose from. As many of you know, ruby-debug provides almost the same directives with GDB, so, for the Ruby developers who have used GDB before, there will not be a steep learning curve. What I want to share with you today is how I make my life easier with help from ruby-debug.

The following steps are used to get ruby-debug working with a Rails 3 application.

Step 1: Install ruby-debug

Open Gemfile, add the following lines to it:

and run bundle install to install the gem.

Step 2: Start the server for development

Step 3: Find the bug

For the line you where you want to setup a breakpoint, just place a ‘debugger’, whether it’s in a view, a controller or a model.

Send another request after setting up the breakpoints; the server will stop at the first breakpoint it runs into, waiting for the further instructions:

With the prompt, you can get a list of directives with “help”, or “help backtrace” for the usage of specific directive. Usually, I’ll use ‘l’, short for ‘list’, to take a look at where the application stops:

After that, check out the value of the variable with ‘p’, short for ‘print’,

After that, you can continue to the next line with ‘n’, short for ‘next’,

Or, you can use ‘c’, short for ‘continue’, to get to the next breakpoint. If there are not any breakpoints left, the reponse will be sent back to the client and the request-response loop is over.

If you want to know how you got where you are, just call ‘bt’, short for ‘backtrace’,

If you want to step into a function call you may have interest in, you can call ‘s’, short for ‘step’,

To step out of the function call you just stepped into, use ‘fin’, short for ‘finish’,

To quit the debugger, type ‘q’, short for ‘quit’,

Those are most of the ruby-debug directives that I find myself using daily. There’s one more feature of ruby-debug that can bring great help for the debugging: irb session.

You can open as many irb sessions as you want, you can change the value of the variable to try it out.

There are lots of other great features in ruby-debug I’m not covering today. Here are some resources to get you started with ruby-debug very quickly:

  1. Debugging Rails Application
  2. Debugging with ruby-debug
  3. ruby-debug cheat sheet
  4. ruby-debug with Passenger

New Project Request