On this page
Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
TracePoint
A class that provides the functionality of Kernel#set_trace_func in a nice Object-Oriented API.
Example
We can use TracePoint to gather information specifically for exceptions:
trace = TracePoint.new(:raise) do |tp| p [tp.lineno, tp.event, tp.raised_exception] end #=> #<TracePoint:disabled> trace.enable #=> false 0 / 0 #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
Events
If you don’t specify the type of events you want to listen for, TracePoint will include all available events.
Note do not depend on current event set, as this list is subject to change. Instead, it is recommended you specify the type of events you want to use.
To filter what is traced, you can pass any of the following as events:
:line-
execute an expression or statement on a new line
:class-
start a class or module definition
:end-
finish a class or module definition
:call-
call a Ruby method
:return-
return from a Ruby method
:c_call-
call a C-language routine
:c_return-
return from a C-language routine
:raise-
raise an exception
:rescue-
rescue an exception
:b_call-
event hook at block entry
:b_return-
event hook at block ending
:a_call-
event hook at all calls (
call,b_call, andc_call) :a_return-
event hook at all returns (
return,b_return, andc_return) :thread_begin-
event hook at thread beginning
:thread_end-
event hook at thread ending
:fiber_switch-
event hook at fiber switch
:script_compiled-
new Ruby code compiled (with
eval,loadorrequire)
Class methods 4
Instance methods 17
Ruby version changes 4
-
3.3
-
TracePointsupportsrescueevent. When the raised exception was rescued, theTracePointwill fire the hook.rescueevent only supports Ruby-levelrescue. [Feature #19572]
-
-
3.2
-
TracePoint#bindingnow returnsnilforc_call/c_returnTracePoints. [Bug #18487] -
TracePoint#enabletarget_threadkeyword argument now defaults to the current thread if a block is given andtargetandtarget_linekeyword arguments are not passed. [Bug #16889]
-
-
3.1
-
TracePoint.allow_reentryis added to allow reenter whileTracePointcallback. [Feature #15912]
-