如何从 SystemStackError: stack level too deep 获取回溯? [英] How to get a backtrace from a SystemStackError: stack level too deep?

查看:38
本文介绍了如何从 SystemStackError: stack level too deep 获取回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编写 ruby​​ 代码时,我经常很难调试无限递归.有没有办法从 SystemStackError 中获取回溯以找出无限循环发生的确切位置?

Often I get hard to debug infinite recursions when coding ruby. Is there a way to get a backtrace out of a SystemStackError to find out, where exactly the infinite loop occurs?

给定一些方法 foobarbaz,它们在循环中相互调用:

Given some methods foo, bar and baz which call each other in a loop:

def foo
  bar
end

def bar
  baz
end

def baz
  foo
end

foo

当我运行此代码时,我只收到消息 test.rb:6: stack level too deep (SystemStackError).至少获取堆栈的最后 100 行会很有用,所以我可以立即看到这是 foobarbaz,像这样:

When I run this code, I just get the message test.rb:6: stack level too deep (SystemStackError). It would be useful to get at least the last 100 lines of the stack, so I could immediately see this is a loop between foo, bar and baz, like this:

test.rb:6: stack level too deep (SystemStackError)
  test.rb:2:in `foo'
  test.rb:10:in `baz'
  test.rb:6:in `bar'
  test.rb:2:in `foo'
  test.rb:10:in `baz'
  test.rb:6:in `bar'
  test.rb:2:in `foo'
  [...]

有没有办法做到这一点?

Is there any way to accomplish this?

正如您从下面的答案中看到的那样,Rubinius 可以做到.不幸的是,一些rubinius 错误阻止我将它与我想调试的软件一起使用.所以准确地说,问题是:

As you may see from the answer below, Rubinius can do it. Unfortunately some rubinius bugs prevent me from using it with the software I'd like to debug. So to be precise the question is:

如何使用 MRI(默认 ruby​​)1.9 获得回溯?

推荐答案

显然这被跟踪为 feature6216 并在 Ruby 2.2 中修复.

Apparently this was tracked as feature 6216 and fixed in Ruby 2.2.

$ ruby system-stack-error.rb
system-stack-error.rb:6:in `bar': stack level too deep (SystemStackError)
        from system-stack-error.rb:2:in `foo'
        from system-stack-error.rb:10:in `baz'
        from system-stack-error.rb:6:in `bar'
        from system-stack-error.rb:2:in `foo'
        from system-stack-error.rb:10:in `baz'
        from system-stack-error.rb:6:in `bar'
        from system-stack-error.rb:2:in `foo'
        from system-stack-error.rb:10:in `baz'
         ... 10067 levels...
        from system-stack-error.rb:10:in `baz'
        from system-stack-error.rb:6:in `bar'
        from system-stack-error.rb:2:in `foo'
        from system-stack-error.rb:13:in `<main>'

这篇关于如何从 SystemStackError: stack level too deep 获取回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆