Cross-language debugging: from Ruby to Java and back

Until we bring the full support for two ways debugging - from Ruby to Java and vice versa; the following "trick" might be used to debug Ruby and Java at once in the NetBeans 6.5 (you might get 6.5 build here). It might be used to debug standard JRuby applications/scripts, JRuby's Java extension or JRuby's internals itself. I'm using it for some time during development of JRuby Fast Debugger extension.

The trick is to start actually two debugging sessions, one for Ruby part and second for Java part (JVM under which JRuby runtime runs). Sessions then automatically activate themselves when the breakpoint for their language is hit.


Except the Ruby IDE, we need to have Java support installed if it is not already the case. This can be find out by looking e.g. into the New Project Wizard. If the Java category is there, we have all we need. If not see the How do I install the Java support into the Ruby NetBeans IDE? FAQ.

So how to utilize it

  1. Open a (J)Ruby project utilizing some Java classes
  2. Go to its properties -> Run category and set JVM Arguments to -Xdebug -Xrunjdwp:transport=dt_socket,address=7000,server=y,suspend=y like on the screenshot (for Rails project the field has to be edited through properties file manually).

    JVM Arguments
  3. Then set the breakpoints in the Ruby code and Java code
  4. Start to debug the (J)Ruby project: Menu | Run | Debug Project
  5. Connect to the JRuby's JVM session: Menu | Debug | Attach Debugger, enter the port 7000, confirm the dialog. (Note that this need to be done within 15 seconds after the Ruby debugger was triggered (previous step). If that's not enough for you, you might increase the Ruby debugger timeout)

    Attach Debugger

That's it. As can be seen on the following screenshot, after I do a step in the Ruby code, Java debugging session take over the control and I'm debugging the Java code called from Ruby code with the full power of NetBeans Java debugger. Once I'm done with Java side I'll just press F5 (Continue) and Ruby debugging session takes back the control and I can continue with the debugging of Ruby part.
Note that if you have JRuby sources you might just set the breakpoint somewhere in the JRuby interpreter's guts and take a look what going around in there.


Clearly not fully-fledged cross-language debugging. Since sessions live their life independently, actions like Step Into, Step Over and similar do not work in the cross-language context. We have to simulate Step Into and similar actions by putting breakpoints on appropriate places. Stacktrace is not shared, so we get two, one for each language, instead of one with nicely mixed frames from both languages. But still, I find it pretty handy during JRuby Fast Debugger development.
If you do not want to repeatedly delete and add the long string from the step 2, you might create a configuration as might be seen in the properties screenshot (JRuby Debug in Configuration combobox).

To watch the progress on fully-fledge cross-language debugging, you might CC yourself to this RFE in the Issuezilla.


Good you blogged about this, the feature has been pretty well hidden :)

Posted by emononen on October 21, 2008 at 04:39 AM PDT #

This is a HUGE deal!!! Many of us are really looking forward to this being feature complete. Excellent work!

Posted by Mario Aquino on October 29, 2008 at 06:50 AM PDT #

Erno> sure, such feature should not be for free! ;)

Mario> I'm not sure how much time I'll have for a debugger into the next release, but at least I could make all the above automatic (i.e. starting both session and doing 'Java Debugger Attach') which would help as well, until the fully-fledged solution is available.

Posted by Martin Krauskopf on October 29, 2008 at 06:22 PM PDT #

Thanks for nice description of how to do this. I've been meaning to write it up, but never have time.

I've been using this in various forms since late July including while developing the ruby debugger support in V3. This meant debugging a debugger (written in ruby and java) that is debugging ruby code that is running in java -- cool and crazy at the same time.

Posted by Peter Williams on November 06, 2008 at 05:44 AM PST #

Peter> yup, exactly my mine use-case, debugging the debugger :)

Posted by Martin Krauskopf on November 06, 2008 at 06:46 AM PST #

Do you think this approach would work trying to debug live code on an appserver (ie glassfish)?

Posted by Jay on March 13, 2009 at 03:04 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Martin Krauskopf


« December 2016