Tracing method definition in core and stdlibs?

J

John Wells

Hi guys,

I'm interested in tracing method definition in core and stdlibs.
However, I'm not sure how to hook in the process at such an early
stage.

For example, if I put the following code in trace.rb:

[Class, Module].each do |n|
n.class_eval do |clz|
def method_added(x)
puts "Method==#{x}"
end
end
end


And then call

$ ruby -rtrace.rb -e 'puts "test"'

I only see two "Method==" outputted...both "Method==method_added", and
both obviously in response to this code itself.

Is there a way to hook in earlier?

Thanks,
John
 
R

Robert Klemme

I'm interested in tracing method definition in core and stdlibs.
However, I'm not sure how to hook in the process at such an early
stage.

For example, if I put the following code in trace.rb:

[Class, Module].each do |n|
n.class_eval do |clz|
def method_added(x)
puts "Method==#{x}"
end
end
end


And then call

$ ruby -rtrace.rb -e 'puts "test"'

I only see two "Method==" outputted...both "Method==method_added", and
both obviously in response to this code itself.

Is there a way to hook in earlier?

Probably not as many methods of the standard library are define in C
code and I doubt that will trigger a method_added.

Btw, what do you need that for?

Cheers

robert
 
J

John Wells

I'm interested in tracing method definition in core and stdlibs.
However, I'm not sure how to hook in the process at such an early
stage.

For example, if I put the following code in trace.rb:

[Class, Module].each do |n|
=A0 n.class_eval do |clz|
=A0 =A0 def method_added(x)
=A0 =A0 =A0 puts "Method=3D=3D#{x}"
=A0 =A0 end
=A0 end
=A0end


And then call

$ ruby -rtrace.rb -e 'puts "test"'

I only see two "Method=3D=3D" outputted...both "Method=3D=3Dmethod_added= ", and
both obviously in response to this code itself.

Is there a way to hook in earlier?

Probably not as many methods of the standard library are define in C code
and I doubt that will trigger a method_added.

Btw, what do you need that for?

Yes, but I'd expect to see a good bit that were ruby too, correct?

I'd like to create a utility method that would allow me to easily see
everywhere a method (file name, line number) is defined...I have it
working for scripts I write myself, but only going forward from that
script scope...not back through the libs. So I'd like to hook in
early...before any ruby files are loaded at all.
 
J

John Wells

Yes, but I'd expect to see a good bit that were ruby too, correct?

I'd like to create a utility method that would allow me to easily see
everywhere a method (file name, line number) is defined...I have it
working for scripts I write myself, but only going forward from that
script scope...not back through the libs. So I'd like to hook in
early...before any ruby files are loaded at all.


Is there any way to accomplish this in 1.8.x?

Thanks,
John
 
R

Robert Klemme

Is there any way to accomplish this in 1.8.x?

Lack of responses to me indicates that you probably need to hack Ruby's
C core to achieve what you want. If you just want to see method
definitions you can as well get Ruby sources and search through them.

Kind regards

robert
 
J

John Wells

Lack of responses to me indicates that you probably need to hack Ruby's C
core to achieve what you want. =A0If you just want to see method definiti= ons
you can as well get Ruby sources and search through them.

Thanks Robert. I wasn't thinking properly about it. I believe you were
correct in saying most everything loaded before doing a simple puts is
just C code...the more I thought about it (and after running strace to
verify), it seems that no external ruby libs are loaded up by the
interpreter assuming that your code doesn't do any specific requires.
So, the code I showed gave no results correctly...but if you change it
to:

$ ruby -rtrace.rb -e 'require "net/http"'

You get the expected results. So, in lieu of having rubinius, I think
that's the best you can expect. Everything else you'd have to dive
into the C for. And honestly, I don't really care about the C...I'm
more interested in being able to debug libraries and gems that are
more promiscuous than they should be.

I appreciate your help and guidance.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top