How do I get a stack trace using the ruby debugger?
Asked Answered
Y

3

19

I am trying to use the ruby debugger in a Rails app.

What command do I need to type at the (rdb:1) prompt in order to display a stack trace? I've tried backtrace, but it only lists the topmost frame.

Yaker answered 10/4, 2013 at 17:32 Comment(1)
Refer this for debugging in a rails app: #6909208Khmer
L
37

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

caller(0)  # Returns the stack trace, omitting 0 initial entry.
Leasehold answered 1/7, 2014 at 16:24 Comment(0)
D
8

That's a bug in debugger. If you are using Ruby >= 2.0, I suggest you use byebug. The issue is solved there since version 1.5.0. This is the bug report in the debugger repo and this is the workaround suggested there:

pp caller.drop_while {|e| e[/ruby-debug|\(eval\)|debugger|\<internal:prelude\>/] }
Darice answered 26/8, 2013 at 22:0 Comment(0)
K
1

Pry gem does have a plugin pry-stack_explorer can display stack

Example: Moving around between frames

[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

Further, it has lot of other features that are missing from ruby debugger. so I would suggest you try pry & its plugins

Kloof answered 10/4, 2013 at 18:39 Comment(1)
I've followed the steps at github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry but it doesn't help. show-stack just gets me an error.Yaker

© 2022 - 2024 — McMap. All rights reserved.