Skip to content

Instantly share code, notes, and snippets.

@kgrz
Created March 25, 2014 19:37
Show Gist options
  • Save kgrz/9769555 to your computer and use it in GitHub Desktop.
Save kgrz/9769555 to your computer and use it in GitHub Desktop.
Ruby, trace, set_trace_func
RubyVM::InstructionSequence.compile_option = {
trace_instruction: false
# This disables the `trace` VM instruction
}
count = lambda do
@count ||= 0
@count += 1
end
set_trace_func proc { |event, _, _, id, _, _|
if id == :to_s && event == "c-call"
count.call
end
}
at_exit do
puts "The method Fixnum#to_s was called #{@count} times"
end
ruby -r ./rb_trace_func.rb test.rb
100_000.times { |num| num.to_s }
@kgrz
Copy link
Author

kgrz commented Apr 3, 2014

FWIW, if I run the test/ruby/test_settracefunc.rb with the :trace_instruction set to false, it throws a bunch of errors mostly relating to some events that weren't generated. For example, line, c-return, and return don't get generated but c-call gets generated!

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