Skip to content

Instantly share code, notes, and snippets.

@jmartelletti
Created July 26, 2014 14:42
Show Gist options
  • Save jmartelletti/760048d23cc9547016b0 to your computer and use it in GitHub Desktop.
Save jmartelletti/760048d23cc9547016b0 to your computer and use it in GitHub Desktop.
require "binding_of_caller"
calls = []
stats = {}
trace = TracePoint.new(:call, :class, :return) do |tp|
next if tp.path =~ /\/var\/lib\/gems\/2\.0\.0/
next if tp.path =~ /\/usr\/lib\/ruby\/2\.0\.0/
next if tp.path =~ /\(erb\)/
stats[tp.defined_class] ||= {}
stats[tp.defined_class][tp.method_id] ||= 0
stats[tp.defined_class][tp.method_id] += 1
binding = tp.binding
bind = eval("self", binding)
caller = eval("binding.of_caller(3)", binding)
caller_obj = eval("self", caller)
#puts bind.inspect
# event", tracepoint_attr_event, 0);
# rb_define_method(rb_cTracePoint, "lineno", tracepoint_attr_lineno, 0);
# rb_define_method(rb_cTracePoint, "path", tracepoint_attr_path, 0);
# rb_define_method(rb_cTracePoint, "method_id", tracepoint_attr_method_id, 0);
# rb_define_method(rb_cTracePoint, "defined_class", tracepoint_attr_defined_class, 0);
# rb_define_method(rb_cTracePoint, "binding", tracepoint_attr_binding, 0);
# rb_define_method(rb_cTracePoint, "self", tracepoint_attr_self, 0);
# rb_define_method(rb_cTracePoint, "return_value", tracepoint_attr_return_value, 0);
# rb_define_method(rb_cTracePoint, "raised_exception"
calls << {
binding: bind,
event: tp.event,
lineno: tp.lineno,
path: tp.path,
method_id: tp.method_id,
klass: tp.defined_class,
caller: caller_obj
#return_value: tp.return_value,
#raised_exception: tp.raised_exception
}
end
trace.enable
config.after(:suite) {
File.open("calls.dot", "w") do |f|
f.write "digraph \"calls\" {\n"
f.write "graph[ratio=1,center=1];\n"
calls.each do |tp|
next if tp[:path] =~ /\(eval\)/
next if tp[:path] =~ /\/spec\//
next if tp[:path] =~ /\/home\/james\/\.gem\/ruby\/2\.0\.0/
if tp[:event] == :call || tp[:event] == :return #|| tp[:event] == :line
#puts "#{tp[:event]} #{tp[:klass]}.#{tp[:method_id]} (#{tp[:path]}:#{tp[:lineno]}) binding:#{tp[:binding]}"
#puts " caller:#{tp[:caller]} "
f.write "\"#{tp[:caller]}\" -> \"#{tp[:binding]}\" [ label = \"#{tp[:method_id]}\" ];\n"
end
end
f.write "}"
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment