On 3/26/10, Yusuke Endoh <redmine / ruby-lang.org> wrote:
> Hmm, ok.  I reopen the ticket with change of the target to 2.0.
>
> But, what purpose is it for?  I can't think of no use case else
> debugging YARV.  Isn't current set_trace_func enough?

I can imagine this capability used to create a debugger. Debugging
your program's YARV instructions may not be so nice as debugging at
the source level, but I'd imagine it could occasionally be useful,
just as c programmers have cause to occasionally need to debug at the
assembly level.

I can also see this used to create a profiler or coverage tool. Finer
grained coverage or performance information would be a clear win over
current tools, which are limited to line-by-line data, since that's
what set_trace_func readily supports.

set_trace_func is nice, but the information provided by it is only
part of the picture. Consider this program:

require 'pp'

set_trace_func(proc{|*x| pp x })

a=1
b=2

if a<b
  p :c
end

On 1.8.7, this gives me:
["line", "stf.rb", 5, nil, #<Binding:0xb7594784>, false]
["line", "stf.rb", 6, nil, #<Binding:0xb7593744>, false]
["line", "stf.rb", 8, nil, #<Binding:0xb7592768>, false]
["c-call", "stf.rb", 8, :<, #<Binding:0xb759178c>, Fixnum]
["c-return", "stf.rb", 8, :<, #<Binding:0xb759079c>, Fixnum]
["line", "stf.rb", 8, nil, #<Binding:0xb758f7ac>, false]
["line", "stf.rb", 9, nil, #<Binding:0xb758e7d0>, false]
["c-call", "stf.rb", 9, :p, #<Binding:0xb758d7f4>, Kernel]
["c-call", "stf.rb", 9, :inspect, #<Binding:0xb758c818>, Symbol]
["c-return", "stf.rb", 9, :inspect, #<Binding:0xb758b800>, Symbol]
["c-call", "stf.rb", 9, :write, #<Binding:0xb758a84c>, IO]
:c["c-return", "stf.rb", 9, :write, #<Binding:0xb7589870>, IO]
["c-call", "stf.rb", 9, :write, #<Binding:0xb7588880>, IO]

["c-return", "stf.rb", 9, :write, #<Binding:0xb75878a4>, IO]
["c-return", "stf.rb", 9, :p, #<Binding:0xb75868c8>, Kernel]



There's some nice stuff in the set_trace_func events about when
methods are entered and exited, but it says nothing about assignments
to variables or whether or not the if statement was taken. (Unless
something for these has been added since the ruby versions I tested
this with.) (This could also be an argument in favor of expanding
set_trace_func to cover these kinds of events...)

Finally, a good reason to consider a feature like this is just because
it would be so cool. Clearly, this would be a very powerful (and
dangerous) meta-programming facility. The really far-out uses for
something like that are things no one can imagine yet. Who would have
thought that set_trace_func could be twisted into helping make the
Binding.of_caller hack work? This is a trivial example of the kind of
unexpected benefit from this kind of capability.