Skip to content

Instantly share code, notes, and snippets.

@toch
Last active September 8, 2015 14:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save toch/82fd93ca449500d21f00 to your computer and use it in GitHub Desktop.
Save toch/82fd93ca449500d21f00 to your computer and use it in GitHub Desktop.
Decorate any method in Ruby to get its call stack
def trace_calls_on
scope = {}
trace = TracePoint.new(:call, :line) do |tp|
case tp.event
when :call then puts "#{tp.path}:#{tp.lineno} #{tp.defined_class}::#{tp.method_id} " \
"called from #{scope[:path]}:#{scope[:lineno]} #{scope[:class]}::#{scope[:method_id]}"
when :line then scope = {
event: :line,
lineno: tp.lineno,
path: tp.path,
class: tp.defined_class,
method_id: tp.method_id
}
end
end
trace.enable
yield
trace.disable
end
require 'open-uri'
trace_calls_on do
open('http://google.com', proxy: nil)
end
@toch
Copy link
Author

toch commented Sep 8, 2015

I've wrapped the gist into a nice gem with pretty output https://rubygems.org/gems/trace_calls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment