# Tracing method definition in core and stdlibs?

**URL:** <https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448>\
**Category:** Ruby\
**Created:** [March 20, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448 "2009-03-20T14:00:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 20, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/1 "2009-03-20T14:00:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 20, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/2 "2009-03-20T17:26:37Z")

</div>

On 20.03.2009 13:56, John W. wrote:

> ```
> end
> 
> ```
> 
> 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

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 20, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/3 "2009-03-20T17:45:58Z")

</div>

On Fri, Mar 20, 2009 at 12:22 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > def method_added(x)
> > 
> > ```
> > 
> > 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?

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.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 20, 2009, 7:49pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/4 "2009-03-20T19:49:45Z")

</div>

On Fri, Mar 20, 2009 at 12:42 PM, John W.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 21, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/5 "2009-03-21T14:19:18Z")

</div>

On Sat, Mar 21, 2009 at 6:17 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [March 21, 2009, 11:20am UTC](https://www.ruby-forum.com/t/tracing-method-definition-in-core-and-stdlibs/163448/6 "2009-03-21T11:20:42Z")

</div>

On 20.03.2009 19:45, John W. wrote:

> 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
