在ruby中调试第三方gem的最佳方法

时间:2021-02-22 20:42:57

Since there may be a lot of Ghost Methods inside a ruby gem, I don't think it is a good idea to study the inner mechanism of a ruby gem just by reading its source code statically. Is there a way to attach the source file of a third-part gem to a running ruby process for debugging so that I can set break point and see how things work dynamically ?
BTW,I've tried to navigate to the source file of a third-part gem in RubyMine by clicking on the context menu "Go To->Implementations" of the 'require' statement or other symbol of an third-part gem( require 'watir' for example ), without success. Is it normal for an IDE of a dynamic typing language such as Ruby to fail a symbol navigation?

由于ruby gem中可能有很多Ghost方法,所以我不认为仅仅通过静态读取源代码来研究ruby gem的内部机制是一个好主意。是否有一种方法可以将第三部分gem的源文件附加到正在运行的ruby进程中以进行调试,以便我可以设置断点并查看事物如何动态工作?顺便说一句,我试着在RubyMine的一个第三方gem的源文件中点击“Go to ->实现”的“需要”语句或第三部分gem的其他符号(例如,需要“watir”),但没有成功。动态类型语言(如Ruby)的IDE失败符号导航是正常的吗?

3 个解决方案

#1


38  

I would love to know if there's a better way to do this, but how I usually do it is:

我很想知道是否有更好的方法,但我通常是这样做的:

  1. Add the ruby-debug gem to your Gemfile (or ruby-debug19 if you're on Ruby 1.9.2)
  2. 将Ruby -debug gem添加到Gemfile中(或者Ruby -debug19,如果您使用的是Ruby 1.9.2)
  3. Find the Gem by doing bundle show gemname. I'm on a Mac so I usually pipe this to pbcopy so it gets copied to my clipboard. bundle show rails | pbcopy
  4. 通过捆绑显示gemname找到Gem。我用的是Mac电脑,所以我通常把这个连接到pbcopy,这样它就会被复制到我的剪贴板上。捆绑显示rails | pbcopy
  5. Open the gem directory in your favorite editor. mvim /path/to/gem/directory
  6. 在您喜欢的编辑器中打开gem目录。mvim /道路/ /珠宝/目录
  7. Navigate to the file and line where you want to put the breakpoint* and insert debugger above the line in question.
  8. 导航到要放置断点*的文件和行,并将调试器插入到有问题的行之上。
  9. Reload page, run test, or do whatever you would to get the Gem file to execute
  10. 重载页面,运行测试,或者做任何你想做的事情来执行Gem文件
  11. When execution stops at debugger, you can inspect variables (p variable_name), and move line by line with the ruby debugger commands.
  12. 当执行在调试器停止时,您可以检查变量(p variable_name),并使用ruby调试器命令逐行移动。

*Knowing where to put the breakpoint can take some understanding of the code, but you should start in lib/gemname.rb

知道在哪里放置断点可以理解代码,但是应该从lib/gemname.rb开始

#2


9  

I would avoid editing the Gem files as suggested in the currently accepted answer. Instead, put the debugger command in one of your app files and use the break command to set a breakpoint in the gem. I'm using rvm with a gemset so here is how I do it:

我将避免编辑Gem文件,如当前所接受的答案所建议的那样。相反,将调试器命令放在一个应用程序文件中,并使用break命令在gem中设置断点。我使用的是一个gemset的rvm所以这里是我怎么做的:

break /Users/chris/.rvm/gems/ruby-1.9.3-p125@<gemset>/gems/<gem_name>-<gem-version>/<path_to_file>:<line_number>

打破/用户/ chris / .rvm /珠宝/ ruby-1.9.3-p125@ < gemset > /珠宝/ < gem_name > - < gem-version > / < path_to_file >:< line_number >

#3


0  

In languages that change the code at runtime, like Ruby, is hard to accurately predict 100% the origins of symbols, methods etc.

在运行时更改代码的语言中,如Ruby,很难100%准确地预测符号、方法等的来源。

I deal with a lot of third party gems that need source code analysis and I found that the best tool for this task is Netbeas + it's Ruby and Rails plugins.

我处理了许多需要源代码分析的第三方gem,我发现这个任务最好的工具是Netbeas +它的Ruby和Rails插件。

  • allows good navigation in the dependent gems source code (unlike other alternatives)
  • 允许在相关的gems源代码中进行良好的导航(与其他替代方案不同)
  • visual breakpoints and debugging that actually work (with trace and all) **
  • 实际工作的可视断点和调试(跟踪和所有)**

** it has some glitches with method calls in yielded code blocks (like each {}) but I learned to deal with those

**它有一些方法调用的小故障(比如每个{}),但是我学会了处理这些问题。

What I usually do is set breakpoints and analyse the code at runtime.

我通常做的是设置断点并在运行时分析代码。

#1


38  

I would love to know if there's a better way to do this, but how I usually do it is:

我很想知道是否有更好的方法,但我通常是这样做的:

  1. Add the ruby-debug gem to your Gemfile (or ruby-debug19 if you're on Ruby 1.9.2)
  2. 将Ruby -debug gem添加到Gemfile中(或者Ruby -debug19,如果您使用的是Ruby 1.9.2)
  3. Find the Gem by doing bundle show gemname. I'm on a Mac so I usually pipe this to pbcopy so it gets copied to my clipboard. bundle show rails | pbcopy
  4. 通过捆绑显示gemname找到Gem。我用的是Mac电脑,所以我通常把这个连接到pbcopy,这样它就会被复制到我的剪贴板上。捆绑显示rails | pbcopy
  5. Open the gem directory in your favorite editor. mvim /path/to/gem/directory
  6. 在您喜欢的编辑器中打开gem目录。mvim /道路/ /珠宝/目录
  7. Navigate to the file and line where you want to put the breakpoint* and insert debugger above the line in question.
  8. 导航到要放置断点*的文件和行,并将调试器插入到有问题的行之上。
  9. Reload page, run test, or do whatever you would to get the Gem file to execute
  10. 重载页面,运行测试,或者做任何你想做的事情来执行Gem文件
  11. When execution stops at debugger, you can inspect variables (p variable_name), and move line by line with the ruby debugger commands.
  12. 当执行在调试器停止时,您可以检查变量(p variable_name),并使用ruby调试器命令逐行移动。

*Knowing where to put the breakpoint can take some understanding of the code, but you should start in lib/gemname.rb

知道在哪里放置断点可以理解代码,但是应该从lib/gemname.rb开始

#2


9  

I would avoid editing the Gem files as suggested in the currently accepted answer. Instead, put the debugger command in one of your app files and use the break command to set a breakpoint in the gem. I'm using rvm with a gemset so here is how I do it:

我将避免编辑Gem文件,如当前所接受的答案所建议的那样。相反,将调试器命令放在一个应用程序文件中,并使用break命令在gem中设置断点。我使用的是一个gemset的rvm所以这里是我怎么做的:

break /Users/chris/.rvm/gems/ruby-1.9.3-p125@<gemset>/gems/<gem_name>-<gem-version>/<path_to_file>:<line_number>

打破/用户/ chris / .rvm /珠宝/ ruby-1.9.3-p125@ < gemset > /珠宝/ < gem_name > - < gem-version > / < path_to_file >:< line_number >

#3


0  

In languages that change the code at runtime, like Ruby, is hard to accurately predict 100% the origins of symbols, methods etc.

在运行时更改代码的语言中,如Ruby,很难100%准确地预测符号、方法等的来源。

I deal with a lot of third party gems that need source code analysis and I found that the best tool for this task is Netbeas + it's Ruby and Rails plugins.

我处理了许多需要源代码分析的第三方gem,我发现这个任务最好的工具是Netbeas +它的Ruby和Rails插件。

  • allows good navigation in the dependent gems source code (unlike other alternatives)
  • 允许在相关的gems源代码中进行良好的导航(与其他替代方案不同)
  • visual breakpoints and debugging that actually work (with trace and all) **
  • 实际工作的可视断点和调试(跟踪和所有)**

** it has some glitches with method calls in yielded code blocks (like each {}) but I learned to deal with those

**它有一些方法调用的小故障(比如每个{}),但是我学会了处理这些问题。

What I usually do is set breakpoints and analyse the code at runtime.

我通常做的是设置断点并在运行时分析代码。