I’m writing a Ruby C extension, and frequently need to run GDB or Valgrind to find bugs. For debugging my specs, I use this:
namespace :spec do
RSPEC_CMD = [ 'ruby', '-S', 'rspec', '-Ilib:ext', SPECDIR ]
desc "Run specs under GDB."
task :gdb => [ :compile ] do |task|
cmd = [ 'gdb' ] + GDB_OPTIONS
cmd += [ '--args' ]
cmd += RSPEC_CMD
run( *cmd )
end
desc "Run specs under Valgrind."
task :valgrind => [ :compile ] do |task|
cmd = [ 'valgrind' ] + VALGRIND_OPTIONS
cmd += RSPEC_CMD
run( *cmd )
end
end
Unfortunately, that won’t work for IRB, since IRB is a Ruby script and not an executable.
I imagine the answer is extremely trivial, but I don’t know enough shell magic to figure it out on my own.
A caveat is that I need to be able to use IRB (enter commands), not just run IRB.
So what’s the command to run IRB within GDB (or Valgrind)?
Looking at the /usr/bin/irb I have here (using
which irb), it just has the shebang#!/usr/bin/ruby1.8in the first line (ie. in this case, /usr/bin/ruby1.8 is the actual executable file run). So you would give that to gdb:And then you could specify /usr/bin/irb as an argument for Ruby, giving you the irb prompt, thus:
Also, I find this one-liner works for me: