我在Ruby(JRuby)代码中放入什么来打入调试器?

时间:2021-02-16 17:09:14

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?

我正熟悉一些JRuby代码,我希望能够在代码中放置一个断点并从命令行运行(像往常一样),让它在到达那一点时进入调试器。我可以在代码中添加一些东西来强制JRuby进入调试器吗?

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 the 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.

我试过运行jruby -r debug foo.rb(而不是通常的jruby foo.rb),然后用b bar.py:98设置断点,然后继续。但是每次出现异常时调试器都会停止,并且在它到达我感兴趣的代码行之前似乎有很多它们。我希望能够将“break-into-debugger”放入在我的代码中的行并运行jruby foo.rb并使调试器停在该行的第一位。

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

(即我在Python中寻找Ruby / JRuby等效的import pdb; pdb.set_trace()。)

3 个解决方案

#1


5  

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:

您可以尝试Netbeans Ruby IDE,它嵌入了JRuby解释器和调试工具,您可以直接在IDE中进行可视化调试。如果使用IDE不是您的选择,只需将de debug gem安装到JRuby发行版中并通过debugger命令使用它:

  1. Manually download the ruby-debug-base-0.10.3.1-java.gem from debug-commons to a local directory.
  2. 手动将debug-commons中的ruby-debug-base-0.10.3.1-java.gem下载到本地目录。

  3. Install the Gem into your JRuby Gem repository:
    jruby -S gem install -l ruby-debug-base-0.10.3.1-java.gem
  4. 将Gem安装到JRuby Gem存储库中:jruby -S gem install -l ruby​​-debug-base-0.10.3.1-java.gem

  5. Install ruby-debug gem:
    jruby -S gem install --ignore-dependencies ruby-debug
  6. 安装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

有关Netbeans wiki,rdebug wiki和JRuby wiki的更多信息

#2


1  

require 'rubygems'
require 'ruby-debug'
debugger

#3


1  

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

我在Windows 7上使用JRuby 1.7.3(1.9.3p385)。

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:

与人们所说的相反,似乎没有预装JRuby的ruby-debug版本。但是,我通过运行gem install ruby​​-debug安装它。我在我的代码中启动调试器,如下所示:

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".

--debug选项是可选的,但如果没有它,您会收到一条警告:“跟踪(例如set_trace_func)不会捕获所有没有--debug标志的事件”。

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.

这里的另一个答案在命令行上使用了-S rdebug选项。调试不需要该选项,它使调试器在程序的最开始时启动。只需调用调试器方法即可在需要时启动调试器;不要使用该选项。

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.

即使没有ruby-debug gem,似乎还是通过debug.rb在JRuby中内置了一些基本调试。您可以简单地在文件中加载'debug.rb'来启动调试器。它打印出一些垃圾消息(“Debug.rb”和“Emacs支持可用。”),但它似乎工作。

#1


5  

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:

您可以尝试Netbeans Ruby IDE,它嵌入了JRuby解释器和调试工具,您可以直接在IDE中进行可视化调试。如果使用IDE不是您的选择,只需将de debug gem安装到JRuby发行版中并通过debugger命令使用它:

  1. Manually download the ruby-debug-base-0.10.3.1-java.gem from debug-commons to a local directory.
  2. 手动将debug-commons中的ruby-debug-base-0.10.3.1-java.gem下载到本地目录。

  3. Install the Gem into your JRuby Gem repository:
    jruby -S gem install -l ruby-debug-base-0.10.3.1-java.gem
  4. 将Gem安装到JRuby Gem存储库中:jruby -S gem install -l ruby​​-debug-base-0.10.3.1-java.gem

  5. Install ruby-debug gem:
    jruby -S gem install --ignore-dependencies ruby-debug
  6. 安装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

有关Netbeans wiki,rdebug wiki和JRuby wiki的更多信息

#2


1  

require 'rubygems'
require 'ruby-debug'
debugger

#3


1  

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

我在Windows 7上使用JRuby 1.7.3(1.9.3p385)。

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:

与人们所说的相反,似乎没有预装JRuby的ruby-debug版本。但是,我通过运行gem install ruby​​-debug安装它。我在我的代码中启动调试器,如下所示:

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".

--debug选项是可选的,但如果没有它,您会收到一条警告:“跟踪(例如set_trace_func)不会捕获所有没有--debug标志的事件”。

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.

这里的另一个答案在命令行上使用了-S rdebug选项。调试不需要该选项,它使调试器在程序的最开始时启动。只需调用调试器方法即可在需要时启动调试器;不要使用该选项。

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.

即使没有ruby-debug gem,似乎还是通过debug.rb在JRuby中内置了一些基本调试。您可以简单地在文件中加载'debug.rb'来启动调试器。它打印出一些垃圾消息(“Debug.rb”和“Emacs支持可用。”),但它似乎工作。