ActiveBlog

How to debug Rails apps deployed on Stackato
by Eric Promislow

Eric Promislow, August 30, 2011

It's easy to debug Rails apps deployed on Stackato, for both versions 2 and 3 of Rails (and possibly Rails 1, although we haven't tested it).  It just requires adding some temporary code to your application.

First, you may want to download Komodo 7 Alpha and get access to Stackato Beta.

Follow the steps below:

Step 1:

Copy the full rubylib directory from <komodo-install>/lib/support/dbgp into vendor/. Do not preload this directory with a directive in application.rb -- Komodo will load it on demand.

Step 2:

Add the following contents to the file lib/jit_debug.rb

module JitDebug
  protected
  def invoke_debugger_if_needed(debugInfo)
    if (defined? ::Debugger) &&

        defined? ::Debugger::DBGP_CommandProcessor
      return
    end
    if debugInfo =~ /^(.*):(\d+)$/
      host = $1
      port = $2
    else
      host = 'localhost'
      port = 9000
    end
    deleteFirst = false
    dbgpParentDir = File::join(

                      File::dirname(
                        File::dirname(__FILE__)),
                          'vendor', 'rubylib')
    if ! $:.include?(dbgpParentDir)
      $:.unshift(dbgpParentDir)
      deleteFirst = true
    end
    ENV['RUBYDB_OPTS']=
        "remoteport=#{host}:#{port} logfile=stderr"
    begin
      require 'rdbgp'
    rescue LoadError
    ensure
      $:.shift if deleteFirst
    end
  end

  def self.included?(base)
    base.send :helper_method, :invoke_debugger_if_needed
  end
end

Step 3:

Include the module in the application_controller.rb file:

class ApplicationController < ActionController::Base
  #...
  include JitDebug
end

Step 4:

Add code that will load the debugger on demand to each controller you're considering debugging:

class MyController < ApplicationController
  before_filter :check_debugger
  #... controller methods

  private
  def check_debugger
    return if !(debugInfo = params[:debug])
    invoke_debugger_if_needed(debugInfo)
  end
end

Now you can debug pages on demand by adding a parameter to the URL that matches the system Komodo is running on. For example, suppose you're running Komodo on a machine with IP address 192.168.69.33 and
Komodo is listening for debug connections on port 9000 (the default). You can then trigger the debugger by modifying the URL by adding a debug parameter:

http://movies.stackato.mysite.com/movies/2?debug=192.168.69.23:9000

This might first stop in Rails library code, which usually isn't interesting. Simply set breakpoints in your controller, model, and erb view files, and press Continue to reach one.

Please note:

Once the Ruby debugger has been loaded, it can't be detached. This means your apps will continue to run more slowly. The only way to get back to non-debugger mode is by ending the session (best by pressing the "Stop Debugging" button in Komodo, and restarting the server).

Note that the names "check_debugger", "debug", "JitDebug", etc. must all be changed if they conflict with your Ruby code.

Again, you can try this out on Komodo 7 Alpha and Stackato Beta.

Subscribe to ActiveState Blogs by Email

Share this post:

About the Author: RSS

Eric Promislow is a senior developer who's worked on Komodo since the very beginning. He has a M.Sc. in Computing Science from Queen's University and a B.Sc. in Biophysics from the University of Ontario. Before joining ActiveState, he helped create the OmniMark text-processing language.