Cross-language debugging: from Ruby to Java and back
By Martin Krauskopf on Oct 21, 2008
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
- Open a (J)Ruby project utilizing some Java classes
- 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
- Then set the breakpoints in the Ruby code and Java code
- Start to debug the (J)Ruby project: Menu | Run | Debug Project
- 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)
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.