Step-by-step debugging and stack navigation for JRuby code.
Problem: the common tools byebug and pry-byebug are MRI-only.
Force JRuby to run in fully interpreted mode:
(otherwise next
would behave like step
)
# For JRuby >= 1.1.3 use "debug" flag, e.g.:
ruby --debug
# or
jruby --debug
# For older versions:
J-Djruby.reflection=true -J-Djruby.compile.mode=OFF
Examples how to do this:
- Add this line to your profile
.zshrc
,.bashrc
,.fishrc
, etc.
export JRUBY_OPTS="-Xcext.enabled=true --debug"
- Add an alias as shortcut, e.g.
alias jrdebug="JRUBY_OPTS='-Xcext.enabled=true --debug'"
- Use a
.jrubyrc
file in~/
or the workind directory (see Configuring JRuby)
There is a JRuby-based backend for ruby-debug: jruby-debug
- JRuby 1.5+ comes pre-bundled with the gem (for older versions do
gem install
), so just:
# require it
require "ruby-debug"
# or add it to your Gemfile
gem "ruby-debug"
# add breakpoint to source, e.g.
def something
debugger
buggy_method("foo")
end
- Navigation once breakpoint is hit:
# continue to next breakpoint
(rdb:1) c # short for "continue"
# next line (step over)
(rdb:1) n # short for "next"
# step into method
(rdb:1) s # short for "step"
# list current line
(rdb:1) l= # short for "list"
# help and more commands
(rdb:1) h= # short for "help"
- Make life easier: add a
~/.rdebugrc
file:
set autolist
set autoeval
set autoreload
- Useful links:
Add step, next and continue to pry for a simple debugger: pry-nav
Warning: project is no longer maintained (but works with JRuby)
- Set breakpoint:
# ensure you have pry loaded, e.g. in Gemfile:
gem 'pry'
gem 'pry-nav'
# then just invoke pry normally
def something
binding.pry # execution will stop here.
puts "Hello World" # run 'step' or 'next' in the console to move here.
end
- Navigate once breakpoint is hit:
# continue to next breakpoint
[1] pry(#<Sinatra::Api>)> continue
# next line (step over)
[1] pry(#<Sinatra::Api>)> next
# step into method
[1] pry(#<Sinatra::Api>)> step
# exit debugging session
[1] pry(#<Sinatra::Api>)> exit!
- Make life easier: add a
~/.pryrc
file:
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
I've also worked on another gem that adds the usual breakpoints and stepping to JRuby: pry-debugger-jruby, do check it out! (All feedback welcome 🎉)