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 }
@patshaughnessy
Copy link

Hi Kashyap - just took a quick look at this and yes, it's a bit odd. I still see the trace YARV instructions being generated by the compiler, even when trace_instruction is set to false. When I have time I'll do some C-level debugging to understand what's going on. It might be we misunderstand the way compile_options work, or maybe there's a bug in MRI.

@kgrz
Copy link
Author

kgrz commented Apr 2, 2014

Makes sense. Also, GH gists won't send notifications for comments :/ Didn't see this earlier, sorry.

@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