如何使用ruby调试器获取堆栈跟踪?

spi*_*epf 9 ruby debugging ruby-on-rails backtrace

我试图在Rails应用程序中使用ruby调试器.

我需要在(rdb:1)提示符下键入什么命令才能显示堆栈跟踪?我试过了backtrace,但它只列出了最顶层的框架.

kon*_*yak 24

http://apidock.com/ruby/Kernel/caller

caller(0)  # Returns the stack trace, omitting 0 initial entry.
Run Code Online (Sandbox Code Playgroud)

  • `put caller(0)`用于更人性化的输出 (8认同)

dei*_*vid 5

这是一个错误debugger.如果你使用Ruby> = 2.0,我建议你使用byebug.从版本1.5.0开始,问题就解决了.debuggerrepo中的错误报告,这是在那里建议的解决方法:

pp caller.drop_while {|e| e[/ruby-debug|\(eval\)|debugger|\<internal:prelude\>/] }
Run Code Online (Sandbox Code Playgroud)


Cur*_*ind 1

Pry gem 确实有一个pry-stack_explorer可以显示堆栈的插件

示例:在帧之间移动

[8] pry(J)> show-stack

Showing all accessible frames in stack:
--
=> #0 [method]  c <Object#c()>
   #1 [block]   block in b <Object#b()>
   #2 [method]  b <Object#b()>
   #3 [method]  alphabet <Object#alphabet(y)>
   #4 [class]   <class:J>
   #5 [block]   block in <main>
   #6 [eval]    <main>
   #7 [top]     <main>
[9] pry(J)> frame 3

Frame number: 3/7
Frame type: method

From: /Users/john/ruby/projects/pry-stack_explorer/examples/example.rb @ line 10 in Object#alphabet:

     5:
     6: require 'pry-stack_explorer'
     7:
     8: def alphabet(y)
     9:   x = 20
 => 10:   b
    11: end
    12:
    13: def b
    14:   x = 30
    15:   proc {
[10] pry(J)> x
=> 20
Run Code Online (Sandbox Code Playgroud)

此外,它还具有 ruby​​ 调试器所缺少的许多其他功能。所以我建议你尝试pry一下它的插件