Skip to content

Instantly share code, notes, and snippets.

@klappradla
Last active March 22, 2024 12:10
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save klappradla/69029a982ade44e20e124c29b1c00541 to your computer and use it in GitHub Desktop.
Save klappradla/69029a982ade44e20e124c29b1c00541 to your computer and use it in GitHub Desktop.
Debugging in JRuby

Debugging in JRuby

Step-by-step debugging and stack navigation for JRuby code.

Problem: the common tools byebug and pry-byebug are MRI-only.

Prerequisites

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:

  1. Add this line to your profile .zshrc, .bashrc, .fishrc, etc.
export JRUBY_OPTS="-Xcext.enabled=true --debug"
  1. Add an alias as shortcut, e.g.
alias jrdebug="JRUBY_OPTS='-Xcext.enabled=true --debug'"
  1. Use a .jrubyrc file in ~/ or the workind directory (see Configuring JRuby)

ruby-debug

There is a JRuby-based backend for ruby-debug: jruby-debug

  1. 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
  1. 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"
  1. Make life easier: add a ~/.rdebugrc file:
set autolist
set autoeval
set autoreload
  1. Useful links:

pry-nav

Add step, next and continue to pry for a simple debugger: pry-nav

Warning: project is no longer maintained (but works with JRuby)

  1. 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
  1. 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!
  1. 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'
@ivoanjo
Copy link

ivoanjo commented Apr 6, 2018

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 🎉)

@jrochkind
Copy link

@ivoanjo seems to have moved the project to https://gitlab.com/ivoanjo/pry-debugger-jruby

@mjobin-mdsol
Copy link

as far as I know cext.enabled=true is no more, thus this document is outdated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment