# Rails debugging - a (slightly) better approach?

**URL:** <https://www.ruby-forum.com/t/rails-debugging-a-slightly-better-approach/97150>\
**Category:** Rails\
**Created:** [July 5, 2006, 8:48am UTC](https://www.ruby-forum.com/t/rails-debugging-a-slightly-better-approach/97150 "2006-07-05T08:48:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fangheuschrecke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/58956e/32.png) [@fangheuschrecke](https://www.ruby-forum.com/u/fangheuschrecke)\
**Post date:** [July 5, 2006, 8:48am UTC](https://www.ruby-forum.com/t/rails-debugging-a-slightly-better-approach/97150/1 "2006-07-05T08:48:08Z")

</div>

Hi all,

First up hello everybody, I’m new to this forum.

I am also new to Ruby and Rails. One thing that struck me as very  
inconvenient for someone just learning the Rails ropes is that (apart  
from Arachno and Komodo, which at the moment I cannot afford - and don’t  
particularly like) there is no reasonable debugging support for Rails.

I know about breakpointer which - while useful - does not help me  
understand how things hang together. I had a look at the source code of  
the core ruby debugger (debug.rb) and tried to make sense of it.

Debug.rb uses set\_trace\_func to install a hook that gets executed on  
every line, call, return, def, etc. and runs a fair bit of conditional  
code on it (tracing the stack frames, checking for breakpoints, etc.).  
The reason that it is unuseably slow with rails is that there are a LOT  
of statements to process in this way until the debuggee code gets hit.

I played around with it and came up with a way of starting the debugger  
only just before it is required - similar to adding “breakpoint” calls  
to my code, I can now say “start\_debug”, which will then break into the  
core ruby debugger in the console where WEBrick is started.

Some things don’t work (and I think cannot work), such as traversing the  
call stack and evaluating expressions in the scope of higher-level stack  
frames. Apart from that, I can list the source code, step into and over  
method calls and inspect everything.

A problem is that after the section of code to be debugged, I also need  
a “stop\_debug” statement, which removes the trace func so things speed  
up again.

Code is available at:  
[http://www.muermann.org/ruby/mydebug.rb](http://www.muermann.org/ruby/mydebug.rb)

To give it a try, drop the file in the lib directory of your app and  
start the server with:  
Ruby -r lib/mydebug

Then, add start\_debug/stop\_debug commands to the code to be debugged.  
The performance between those calls will still be horrible, but at least  
the startup time and the rest of the app will not be affected.

Oh, start\_debug also serves as a breakpoint.

def list  
start\_debug  
… controller code …  
stop\_debug  
end

Sample debugging session:

MyDebug.rb  
Emacs support available.

=\> Booting WEBrick…  
=\> Rails application started on [http://0.0.0.0:3000](http://0.0.0.0:3000)  
=\> Ctrl-C to shutdown server; call with --help for options  
C:/work/ruby/workspace/workharder/script/…/config/…/app/controllers/ta  
sk\_controller.rb:8: u = User.find(session[:user].id)

(rdb:3) list  
[3, 12] in  
C:/work/ruby/workspace/workharder/script/…/config/…/app/controllers/ta  
sk\_controller.rb  
3 class TaskController \< UserController  
4 scaffold :task  
5  
6 def my\_tasks  
7 start\_debug  
=\> 8 u = User.find(session[:user].id)  
9  
10 @tasks = u.tasks  
11 stop\_debug  
12 @users = User.find(:all)

(rdb:3) up  
#2  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.12.1/lib/action\_controller/b  
ase.rb:910

(rdb:3) down  
#1  
C:/work/ruby/workspace/workharder/script/…/config/…/app/controllers/ta  
sk\_controller.rb:8:in `my\_tasks’

(rdb:3) next  
C:/work/ruby/workspace/workharder/script/…/config/…/app/controllers/ta  
sk\_controller.rb:10: @tasks = u.tasks

(rdb:3) list  
[5, 14] in  
C:/work/ruby/workspace/workharder/script/…/config/…/app/controllers/ta  
sk\_controller.rb  
5  
6 def my\_tasks  
7 start\_debug  
8 u = User.find(session[:user].id)  
9  
=\> 10 @tasks = u.tasks  
11 stop\_debug  
12 @users = User.find(:all)  
13 end  
14

(rdb:3) u  
u  
#\<User:0x5b01e20 @attributes={…}\>  
(rdb:3)

---

<div class="post-metadata">

**Author:** ![fangheuschrecke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/58956e/32.png) [@fangheuschrecke](https://www.ruby-forum.com/u/fangheuschrecke)\
**Post date:** [July 5, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/rails-debugging-a-slightly-better-approach/97150/2 "2006-07-05T17:53:00Z")

</div>

On Jul 4, 2006, at 11:46 PM, Max M. wrote:

> Hi all,
> 
> First up hello everybody, I’m new to this forum.
> 
> I am also new to Ruby and Rails. One thing that struck me as very  
> inconvenient for someone just learning the Rails ropes is that (apart  
> from Arachno and Komodo, which at the moment I cannot afford - and  
> don’t  
> particularly like) there is no reasonable debugging support for Rails.

Hey Max-

```
This is very cool. Great idea to only run set_trace_func when you

```

need it. I played with this a little last night but I will be  
watching how you progress with this. Pretty cool

-Ezra

---

<div class="post-metadata">

**Author:** ![fangheuschrecke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/58956e/32.png) [@fangheuschrecke](https://www.ruby-forum.com/u/fangheuschrecke)\
**Post date:** [July 8, 2006, 10:57pm UTC](https://www.ruby-forum.com/t/rails-debugging-a-slightly-better-approach/97150/3 "2006-07-08T22:57:51Z")

</div>

On Wednesday, July 05, 2006, at 4:46 PM, Max M. wrote:

> Code is available at:  
> [http://www.muermann.org/ruby/mydebug.rb](http://www.muermann.org/ruby/mydebug.rb)

This, my friend, is awesome. Thank you.

B.A.
