3

使用这个最小的 ruby​​ 代码:

require 'debug'
puts

在一个名为的文件中,例如 script.rb

如果我像这样启动它:ruby -rdebug script.rb

然后按l调试提示,我得到了列表,如预期的那样

如果我改为正常运行它ruby script.rb

按下时l我得到:

(rdb:1) l
[-3, 6] in script.rb
No sourcefile available for script.rb

错误消息充其量似乎具有误导性:工作目录没有改变,文件肯定还在那里!

我无法找到有关此行为的文档(我在 jruby 和 mri 上都尝试过,并且行为相同)

我知道“调试器”和“撬”,但它们服务于不同的用例:

我已经习惯了其他带有内置调试模块的脚本语言,它可以让我在代码中的任何位置放置一条语句,以便将我放入调试 shell、检查代码、变量等等……内置它的好处是它随处可用,无需为它设置环境,甚至当我在不是我自己的机器上时

我显然可以通过始终调用解释器-rdebug并手动设置断点来解决这个问题,但我发现这比替代方法更有效

4

2 回答 2

2

After looking into the debug source code, I found a workaround and a fix:

the workaround can be:

trace on
next
trace off
list

this will let you get the listing without restarting the interpreter with -rdebug, with the disadvantage that you'll get some otherwise unwanted output from the tracing, and you'll be able to see it only after moving by one statement

for the fix: the problem is that the SCRIPT_LINES__ Hash lacks a value for the current file... apparently it's only set inside tracer.rb

I've changed line 161, and changed the Hash with a subclass that tracks where []= has been called from, and I wasn't able to dig up the actual code that does the work when stepping into a function that comes from a different file

Also: I haven't found a single person yet who actively uses this module (I asked both on #ruby, #jruby and #pry on freenode), and together with the fact that it uses a function that is now obsolete it leads me to be a bit pessimistic about the maintenance state of this module

nonetheless, I submitted a pull request for the fix (it's actually quite dumb and simple, but to do otherwise I'd need a deeper understanding of this module, and possibly to refactor some parts of it... but if this module isn't actively maintaned, I'm not sure that's a good thing to put effort on)

于 2013-04-09T20:45:31.020 回答
1

我怀疑 Ruby 解释器无法在没有调试模块中的组件的情况下加载源文件。因此,没有-rdebug,就无法访​​问源文件。我同意这是一个误导性错误。“未加载调试功能”可能会更好。

于 2013-04-08T01:57:28.600 回答