1 # loaded from vm_trace.c
3 # Document-class: TracePoint
5 # A class that provides the functionality of Kernel#set_trace_func in a
6 # nice Object-Oriented API.
10 # We can use TracePoint to gather information specifically for exceptions:
12 # trace = TracePoint.new(:raise) do |tp|
13 # p [tp.lineno, tp.event, tp.raised_exception]
15 # #=> #<TracePoint:disabled>
21 # #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
25 # If you don't specify the type of events you want to listen for,
26 # TracePoint will include all available events.
28 # *Note* do not depend on current event set, as this list is subject to
29 # change. Instead, it is recommended you specify the type of events you
32 # To filter what is traced, you can pass any of the following as +events+:
34 # +:line+:: execute an expression or statement on a new line
35 # +:class+:: start a class or module definition
36 # +:end+:: finish a class or module definition
37 # +:call+:: call a Ruby method
38 # +:return+:: return from a Ruby method
39 # +:c_call+:: call a C-language routine
40 # +:c_return+:: return from a C-language routine
41 # +:raise+:: raise an exception
42 # +:b_call+:: event hook at block entry
43 # +:b_return+:: event hook at block ending
44 # +:a_call+:: event hook at all calls (+call+, +b_call+, and +c_call+)
45 # +:a_return+:: event hook at all returns (+return+, +b_return+, and +c_return+)
46 # +:thread_begin+:: event hook at thread beginning
47 # +:thread_end+:: event hook at thread ending
48 # +:fiber_switch+:: event hook at fiber switch
49 # +:script_compiled+:: new Ruby code compiled (with +eval+, +load+ or +require+)
53 # TracePoint.new(*events) { |obj| block } -> obj
55 # Returns a new TracePoint object, not enabled by default.
57 # Next, in order to activate the trace, you must use TracePoint#enable
59 # trace = TracePoint.new(:call) do |tp|
60 # p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
62 # #=> #<TracePoint:disabled>
67 # puts "Hello, TracePoint!"
69 # # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
72 # When you want to deactivate the trace, you must use TracePoint#disable
76 # See TracePoint@Events for possible events and more information.
78 # A block must be given, otherwise an ArgumentError is raised.
80 # If the trace method isn't included in the given events filter, a
81 # RuntimeError is raised.
83 # TracePoint.trace(:line) do |tp|
84 # p tp.raised_exception
86 # #=> RuntimeError: 'raised_exception' not supported by this event
88 # If the trace method is called outside block, a RuntimeError is raised.
90 # TracePoint.trace(:line) do |tp|
93 # $tp.lineno #=> access from outside (RuntimeError)
95 # Access from other threads is also forbidden.
98 Primitive.tracepoint_new_s(events)
102 # trace.inspect -> string
104 # Return a string containing a human-readable TracePoint
107 Primitive.tracepoint_inspect
111 # TracePoint.stat -> obj
113 # Returns internal information of TracePoint.
115 # The contents of the returned value are implementation specific.
116 # It may be changed in future.
118 # This method is only for debugging TracePoint itself.
120 Primitive.tracepoint_stat_s
124 # TracePoint.trace(*events) { |obj| block } -> obj
126 # A convenience method for TracePoint.new, that activates the trace
129 # trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
130 # #=> #<TracePoint:enabled>
132 # trace.enabled? #=> true
134 def self.trace(*events)
135 Primitive.tracepoint_trace_s(events)
139 # TracePoint.allow_reentry
141 # In general, while a TracePoint callback is running,
142 # other registered callbacks are not called to avoid
143 # confusion by reentrance.
144 # This method allows the reentrance in a given block.
145 # This method should be used carefully, otherwise the callback
146 # can be easily called infinitely.
148 # If this method is called when the reentrance is already allowed,
149 # it raises a RuntimeError.
150 def self.allow_reentry
151 Primitive.tracepoint_allow_reentry
155 # trace.enable(target: nil, target_line: nil, target_thread: nil) -> true or false
156 # trace.enable(target: nil, target_line: nil, target_thread: nil) { block } -> obj
158 # Activates the trace.
160 # Returns +true+ if trace was enabled.
161 # Returns +false+ if trace was disabled.
163 # trace.enabled? #=> false
164 # trace.enable #=> false (previous state)
166 # trace.enabled? #=> true
167 # trace.enable #=> true (previous state)
168 # # trace is still enabled
170 # If a block is given, the trace will only be enabled within the scope of the
178 # # only enabled for this block
184 # +target+, +target_line+ and +target_thread+ parameters are used to
185 # limit tracing only to specified code objects. +target+ should be a
186 # code object for which RubyVM::InstructionSequence.of will return
187 # an instruction sequence.
189 # t = TracePoint.new(:line) { |tp| p tp }
199 # t.enable(target: method(:m1))
202 # # prints #<TracePoint:line test.rb:4 in `m1'>
206 # Note: You cannot access event hooks within the +enable+ block.
208 # trace.enable { p tp.lineno }
209 # #=> RuntimeError: access from outside
211 def enable(target: nil, target_line: nil, target_thread: nil)
212 Primitive.tracepoint_enable_m(target, target_line, target_thread)
216 # trace.disable -> true or false
217 # trace.disable { block } -> obj
219 # Deactivates the trace
221 # Return true if trace was enabled.
222 # Return false if trace was disabled.
224 # trace.enabled? #=> true
225 # trace.disable #=> true (previous status)
226 # trace.enabled? #=> false
227 # trace.disable #=> false
229 # If a block is given, the trace will only be disable within the scope of the
237 # # only disabled for this block
243 # Note: You cannot access event hooks within the block.
245 # trace.disable { p tp.lineno }
246 # #=> RuntimeError: access from outside
248 Primitive.tracepoint_disable_m
252 # trace.enabled? -> true or false
254 # The current status of the trace
256 Primitive.tracepoint_enabled_p
261 # See TracePoint@Events for more information.
263 Primitive.tracepoint_attr_event
266 # Line number of the event
268 Primitive.tracepoint_attr_lineno
271 # Path of the file being run
273 Primitive.tracepoint_attr_path
276 # Return the parameters definition of the method or block that the
277 # current hook belongs to. Format is the same as for Method#parameters
279 Primitive.tracepoint_attr_parameters
282 # Return the name at the definition of the method being called
284 Primitive.tracepoint_attr_method_id
287 # Return the called name of the method being called
289 Primitive.tracepoint_attr_callee_id
292 # Return class or module of the method being called.
294 # class C; def foo; end; end
295 # trace = TracePoint.new(:call) do |tp|
296 # p tp.defined_class #=> C
301 # If method is defined by a module, then that module is returned.
303 # module M; def foo; end; end
304 # class C; include M; end;
305 # trace = TracePoint.new(:call) do |tp|
306 # p tp.defined_class #=> M
311 # <b>Note:</b> #defined_class returns singleton class.
313 # 6th block parameter of Kernel#set_trace_func passes original class
314 # of attached by singleton class.
316 # <b>This is a difference between Kernel#set_trace_func and TracePoint.</b>
318 # class C; def self.foo; end; end
319 # trace = TracePoint.new(:call) do |tp|
320 # p tp.defined_class #=> #<Class:C>
325 Primitive.tracepoint_attr_defined_class
328 # Return the generated binding object from event.
330 # Note that for +c_call+ and +c_return+ events, the binding returned is the
331 # binding of the nearest Ruby method calling the C method, since C methods
332 # themselves do not have bindings.
334 Primitive.tracepoint_attr_binding
337 # Return the trace object during event
339 # Same as the following, except it returns the correct object (the method
340 # receiver) for +c_call+ and +c_return+ events:
342 # trace.binding.eval('self')
344 Primitive.tracepoint_attr_self
347 # Return value from +:return+, +c_return+, and +b_return+ event
349 Primitive.tracepoint_attr_return_value
352 # Value from exception raised on the +:raise+ event
354 Primitive.tracepoint_attr_raised_exception
357 # Compiled source code (String) on *eval methods on the +:script_compiled+ event.
358 # If loaded from a file, it will return nil.
360 Primitive.tracepoint_attr_eval_script
363 # Compiled instruction sequence represented by a RubyVM::InstructionSequence instance
364 # on the +:script_compiled+ event.
366 # Note that this method is MRI specific.
367 def instruction_sequence
368 Primitive.tracepoint_attr_instruction_sequence