How do you view a sample of the call stack in ruby?

I’m investigating different optimization techniques, and I came across this post Analyzing Code for Efficiency? by someone who believes that sampling the call stack is more effective than using a profiler. The basic idea is that if you take a view of the call stack, you see where your application is most likely to be spending most of its time, and then optimize there.

It is certainly interesting, and he is obviously an expert on this, but I don’t know how to view the call stack in ruby. In debugger I can say “info stack” but only seems to show one line.

EDIT: I saw this comment by Mike Dunlavey: “I would just like to point out that if you run under the debugger, interrupt it manually, and display the call stack…”

I’m just not sure how to interrupt it manually and dipslay the call stack.

Ruby OOP terminology: How do you call “devise” and “:trackable” in this class?

Here is a Ruby ActiveRecord class: class User < ActiveRecord::Base has_many :user_tokens devise :trackable, :confirmable I have two Ruby terminology questions about it: How do you call :confirmab

How to view call stack with dtrace

How to view call stack, return value and arguments of the simply program below, with dtrace /** Trival code **/ #include <stdio.h> int foo (int *a, int *b) { *a = *b; *b = 4; return 0; } int ma

How do you document sample programs?

Let’s say you have to [write from scratch, rewrite, refactor] a sample program illustrating how to do something quite specific with some middleware/SDK/library, or maybe just some programming techniqu

How do you decide which aliased method to call in Ruby?

In the Ruby API I notice a lot of methods are aliased. For instance to iterate over the lines in a String I can call str.lines or str.each_line. How do you decide which one to use? Are some alias name

sample code for overflow in stack

Hello Can you give me a sample code for overflow in stack with c or .net ? and say me how do you resolve this bug. Thank you

How do you sample groups in a data.table

How would you use data.table to efficiently take a sample of rows for groups in a data frame? > DT = data.table(a=sample(1:2), b=sample(1:1000,20)) > DT a b 1: 2 562 2: 1 183 3: 2 180 4: 1 874

When Debugging in In Visual Studio, how do it view the function call heiracrcy/stack in the IDE?

In VB.net, I am aware that you can use a stack trace to get the function call heirarchy that lead to an exception. However, I also know that if you are debugging and an exception is generated, you can

looking for a program for linux to view the call stack of my program [closed]

looking for a native linux program that lets you view the call stack.

How do you monitor your Ruby programming productivity?

Ruby coders: How do you monitor your productivity? I’m curious to know what you use to keep track of how much you do and how well you do it.

How do you kill a PTY.spawn call in Ruby?

If I run a command like this, using ruby’s pty class, how do I kill it if I find a certain input string? cmd = appcfg.py update cdn PTY.spawn(#{cmd} 2>&1) do | input, output, pid | begin in

Answers

You can throw an exception at any time, and then look at the [email protected] predefined variable, which returns an array of backtrace data. E.g. put this in foo.rb:

begin                                                                        
  raise 'foo'                                                                
rescue                                                                       
  puts [email protected]                                                                    
end  

Then run it:

$ ruby foo.rb 
foo.rb:2:in `<main>'

Just put

puts caller

anywhere in the code. If you don’t like its format, it’s an array of strings, so you can do some regex manipulation for a desired output.

How about sending signal to the ruby process, and creating a handler for the signal which dumps all stacks?

From http://le-huy.blogspot.com/2012/04/dump-backtrace-of-all-threads-in-ruby.html we have this example :

require 'pp'

def backtrace_for_all_threads(signame)
  File.open("/tmp/ruby_backtrace_#{Process.pid}.txt","a") do |f|
      f.puts "--- got signal #{signame}, dump backtrace for all threads at #{Time.now}"
      if Thread.current.respond_to?(:backtrace)
        Thread.list.each do |t|
          f.puts t.inspect
          PP.pp(t.backtrace.delete_if {|frame| frame =~ /^#{File.expand_path(__FILE__)}/},
               f) # remove frames resulting from calling this method
        end
      else
          PP.pp(caller.delete_if {|frame| frame =~ /^#{File.expand_path(__FILE__)}/},
               f) # remove frames resulting from calling this method
      end
  end
end

Signal.trap(29) do
  backtrace_for_all_threads("INFO")
end

Then we need to send the signal to the appropriate process :

ps afxw | grep ruby
kill -29 <pid>
ls -l /tmp/ruby*
vi /tmp/ruby_backtrace_...

Repeat the signal at appropriate sampling time.