You can attach GDB to a Ruby process and get all sorts of information. Call with gdb /path/to/ruby <PID>
, then running call rb_backtrace()
from within gdb
will get you a stack trace and you can raise exceptions with call rb_raise(rb_eException, "Testing...")
.
Neat trick, see here for the original source.
I should add that in Ruby 1.9.3 with Apple’s GDB 6.3.50 the cast to (int)
isn’t necessary in the call to rb_raise
shown on the page above.
Also, once in gdb
, you can use the following functions to help out.
define redirect_stdout call rb_eval_string("$_old_stdout, $stdout = $stdout, File.open('/tmp/ruby-debug.' + Process.pid.to_s, 'a'); $stdout.sync = true") end define ruby_eval call(rb_p(rb_eval_string_protect($arg0,(int*)0))) end
You can paste these into ~/.gdbinit
in order to always have them available. Once defined, you can run redirect_stdout
to have output redirected into a tmp file. Then you can run Ruby code with ruby_eval('Kernel.caller')
, etc. to get access to objects, local variables, etc.
Part of this information comes from here.
Some more neat GDB tricks are here.