In Ruby, there is a class that can help you keep track of some activities such as method call, class declaration, exception, … And that class is TracePoint. It is quite useful when you want to track and call a particular function, you can see how that function is called, what it returns.
Create a TracePoint
Here is an example that creates a TracePoint to track function calls from a method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <span class="token keyword">class</span> <span class="token class-name">Foo</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">bar</span></span> yaa mod <span class="token keyword">end</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">yaa</span></span> mod <span class="token keyword">end</span> <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">mod</span></span> <span class="token keyword">end</span> <span class="token keyword">end</span> trace <span class="token operator">=</span> <span class="token constant">TracePoint</span> <span class="token punctuation">.</span> <span class="token keyword">new</span> <span class="token punctuation">(</span> <span class="token symbol">:call</span> <span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span> tp <span class="token operator">|</span> puts <span class="token string">" <span class="token interpolation"><span class="token delimiter tag">#{</span> tp <span class="token punctuation">.</span> defined_class <span class="token delimiter tag">}</span></span> # <span class="token interpolation"><span class="token delimiter tag">#{</span> tp <span class="token punctuation">.</span> method_id <span class="token delimiter tag">}</span></span> "</span> <span class="token keyword">end</span> trace <span class="token punctuation">.</span> enable <span class="token constant">Foo</span> <span class="token punctuation">.</span> <span class="token keyword">new</span> <span class="token punctuation">.</span> bar trace <span class="token punctuation">.</span> disable <span class="token comment"># Foo#bar</span> <span class="token comment"># Foo#yaa</span> <span class="token comment"># Foo#mod</span> <span class="token comment"># Foo#mod</span> |
With event :call
you can keep track of the times Ruby calls any method. So you can above that we have trace.enable
and trace.disable
to ensure that our trace
will not run all the time every time a certain method calls, but only limited to a certain range. .
Trackable events
The table below lists the events that TracePoint supports (Ruby 2.5.0):
Name of the event | Description |
---|---|
line | Run the code on a new line |
class | At the beginning of defining a Class or Module |
end | At the end of defining a Class or Module |
call | Call a function in Ruby |
return | Returned from a function in Ruby |
c_call | Call a function written in C |
c_return | Returns from a function written in C |
raise | When an exception is fired |
b_call | Start a block |
b_return | End a block |
thread_begin | When starting a thread |
thread_end | At the end of a thread |
fiber_switch | when switching context |
Fiber is a class in Ruby that makes code that can be stopped and resumed.
Available methods
The table below lists the methods that TracePoint supports (Ruby 2.5.0):
Method name | Description |
---|---|
binding | Returns the binding object created with the event |
defined_class | Returns the Class or Module on which the method is being called |
disable | Disable TracePoint |
enable | Activate TracePoint |
enabled? | Check the running status of TracePoint |
event | Name of the event that is happening |
inspect | Returns a string containing the TracePoint state |
lineno | Number of lines in which the event occurred |
method_id | The method name is called |
path | The path of the file is running |
raised_exception | The value returned from the exception of the event :raise |
return_value | Returns values from events :return :c_return :b_return :c_return :b_return |
self | Returns the object that created the event |
Draw flow to call the function with TracePoint
I wrote a gem to model a function in ruby, with the example at the beginning of this article we will have:
An example of a Fibonacci numbering function:
Code:
1 2 3 4 5 6 | <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">fibonacci</span></span> n <span class="token keyword">return</span> n <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token number">0.</span> <span class="token number">.1</span> <span class="token punctuation">)</span> <span class="token punctuation">.</span> <span class="token keyword">include</span> <span class="token operator">?</span> n fibonacci <span class="token punctuation">(</span> n <span class="token operator">-</span> <span class="token number">1</span> <span class="token punctuation">)</span> <span class="token operator">+</span> fibonacci <span class="token punctuation">(</span> n <span class="token operator">-</span> <span class="token number">2</span> <span class="token punctuation">)</span> <span class="token keyword">end</span> |
Conclude
Hope this article can help you understand more about TracePoint
in Ruby.
Gem I wrote