开发者

What do I put in my Ruby (JRuby) code to break into the debugger?

开发者 https://www.devze.com 2022-12-24 22:57 出处:网络
I\'m familiarizing myself with some JRuby code, and I\'d like to be able to place a breakpoint in the code and run (as usual) from the command-line, having it break into the debugger when it gets to t

I'm familiarizing myself with some JRuby code, and I'd like to be able to place a breakpoint in the code and run (as usual) from the command-line, having it break into the debugger when it gets to that point. Is there something I can put in my code to force JRuby to break into the debugger?

I've tried running jruby -r debug foo.rb (instead of the usual jruby foo.rb), and then setting a breakpoint with b bar.py:98, and then continuing. But the debugger stops every time there's an exception, and there seem to be a lot of them before it gets to t开发者_如何学运维he line of code I'm interested in. I'd like to be able to put the "break-into-debugger" line(s) in my code and run jruby foo.rb and have the first place the debugger stops be at that line.

(i.e. I'm looking for the Ruby/JRuby equivalent of import pdb;pdb.set_trace() in Python.)


You could try Netbeans Ruby IDE, it has JRuby interpreter and debugging tools embedded and you can debug visually in the IDE directly.
If using an IDE is not an option for you, just install de debug gem into your JRuby distro and use it via debugger command:

  1. Manually download the ruby-debug-base-0.10.3.1-java.gem from debug-commons to a local directory.
  2. Install the Gem into your JRuby Gem repository:
    jruby -S gem install -l ruby-debug-base-0.10.3.1-java.gem
  3. Install ruby-debug gem:
    jruby -S gem install --ignore-dependencies ruby-debug

The debugger command should work now.

# test.rb
require 'rubygems'
require 'ruby-debug'
debugger
# run like this:
jruby --debug -S rdebug test.rb

More information on Netbeans wiki, rdebug wiki and JRuby wiki


require 'rubygems'
require 'ruby-debug'
debugger


I am using JRuby 1.7.3 (1.9.3p385) on Windows 7.

Contrary to what people say, there does not seem to be a version of ruby-debug preinstalled with JRuby. However, I installed it by running gem install ruby-debug. I start the debugger in my code like this:

require 'rubygems'
require 'ruby-debug'
debugger

You should call the debugger method everywhere that you want to have a breakpoint.

I ran the code like this:

jruby --debug program.rb

The --debug option is optional, but without it you get a warning that "tracing (e.g. set_trace_func) will not capture all events without --debug flag".

Another answer on here used the -S rdebug option on the command line. That option is not necessary for debugging and it makes the debugger start at the very beginning of your program. Just call the debugger method to start the debugger when you need it; don't use that option.

Even without the ruby-debug gem, it seems like there is still some kind of basic debugging built in to JRuby via debug.rb. You can simply write load 'debug.rb' in a file to start a debugger going. It prints out some junky messages ("Debug.rb" and "Emacs support available.") but it seems to work.

0

精彩评论

暂无评论...
验证码 换一张
取 消