title: How to get a backtrace from a SystemStackError: stack level too deep?
body: <p>Often I get hard to debug infinite recursions when coding ruby. Is there a way to get a backtrace out of a <code>SystemStackError</code> to find out, where exactly the infinite loop occurs?</p>
  
  <h2>Example</h2>
  
  <p>Given some methods <code>foo</code>, <code>bar</code> and <code>baz</code> which call each other in a loop:</p>
  
  <pre><code>def foo
    bar
  end
  
  def bar
    baz
  end
  
  def baz
    foo
  end
  
  foo
  </code></pre>
  
  <p>When I run this code, I just get the message <code>test.rb:6: stack level too deep (SystemStackError)</code>. 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 <code>foo</code>, <code>bar</code> and <code>baz</code>, like this:</p>
  
  <pre><code>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'
    [...]
  </code></pre>
  
  <p>Is there any way to accomplish this?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As you may see from the answer below, Rubinius can do it. Unfortunately some <a href="http://github.com/rubinius/rubinius/issues/1692">rubinius bugs</a> prevent me from using it with the software I'd like to debug. So to be precise the question is:</p>
  
  <p><strong>How do I get a backtrace with MRI (the default ruby) 1.9?</strong></p>