Created
March 25, 2014 19:37
-
-
Save kgrz/9769555 to your computer and use it in GitHub Desktop.
Ruby, trace, set_trace_func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ruby -r ./rb_trace_func.rb test.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100_000.times { |num| num.to_s } |
Makes sense. Also, GH gists won't send notifications for comments :/ Didn't see this earlier, sorry.
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
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.