Max Muermann
2006-Jul-05 06:46 UTC
[Rails] Rails debugging - a (slightly) better approach?
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 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 => 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)
Ezra Zygmuntowicz
2006-Jul-05 15:50 UTC
[Rails] Rails debugging - a (slightly) better approach?
On Jul 4, 2006, at 11:46 PM, Max Muermann 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. ><snip> 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
On Wednesday, July 05, 2006, at 4:46 PM, Max Muermann wrote:>Code is available at: >http://www.muermann.org/ruby/mydebug.rbThis, my friend, is awesome. Thank you. B.A. -- Posted with http://DevLists.com. Sign up and save your mailbox.