[ruby/etc] bump up to 1.3.1
[ruby-80x24.org.git] / trace_point.rb
blob85ebac9aa74a91c83ab546695a9e7b26ebb8e187
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.
8 # == Example
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]
14 #           end
15 #           #=> #<TracePoint:disabled>
17 #           trace.enable
18 #           #=> false
20 #           0 / 0
21 #           #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
23 # == Events
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
30 # want to use.
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+)
51 class TracePoint
52   # call-seq:
53   #     TracePoint.new(*events) { |obj| block }     -> obj
54   #
55   # Returns a new TracePoint object, not enabled by default.
56   #
57   # Next, in order to activate the trace, you must use TracePoint#enable
58   #
59   #     trace = TracePoint.new(:call) do |tp|
60   #         p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
61   #     end
62   #     #=> #<TracePoint:disabled>
63   #
64   #     trace.enable
65   #     #=> false
66   #
67   #     puts "Hello, TracePoint!"
68   #     # ...
69   #     # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
70   #     # ...
71   #
72   # When you want to deactivate the trace, you must use TracePoint#disable
73   #
74   #     trace.disable
75   #
76   # See TracePoint@Events for possible events and more information.
77   #
78   # A block must be given, otherwise an ArgumentError is raised.
79   #
80   # If the trace method isn't included in the given events filter, a
81   # RuntimeError is raised.
82   #
83   #     TracePoint.trace(:line) do |tp|
84   #         p tp.raised_exception
85   #     end
86   #     #=> RuntimeError: 'raised_exception' not supported by this event
87   #
88   # If the trace method is called outside block, a RuntimeError is raised.
89   #
90   #      TracePoint.trace(:line) do |tp|
91   #        $tp = tp
92   #      end
93   #      $tp.lineno #=> access from outside (RuntimeError)
94   #
95   # Access from other threads is also forbidden.
96   #
97   def self.new(*events)
98     Primitive.tracepoint_new_s(events)
99   end
101   #  call-seq:
102   #    trace.inspect  -> string
103   #
104   #  Return a string containing a human-readable TracePoint
105   #  status.
106   def inspect
107     Primitive.tracepoint_inspect
108   end
110   # call-seq:
111   #     TracePoint.stat -> obj
112   #
113   #  Returns internal information of TracePoint.
114   #
115   #  The contents of the returned value are implementation specific.
116   #  It may be changed in future.
117   #
118   #  This method is only for debugging TracePoint itself.
119   def self.stat
120     Primitive.tracepoint_stat_s
121   end
123   # call-seq:
124   #        TracePoint.trace(*events) { |obj| block }    -> obj
125   #
126   # A convenience method for TracePoint.new, that activates the trace
127   # automatically.
128   #
129   #         trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
130   #         #=> #<TracePoint:enabled>
131   #
132   #         trace.enabled? #=> true
133   #
134   def self.trace(*events)
135     Primitive.tracepoint_trace_s(events)
136   end
138   # call-seq:
139   #   TracePoint.allow_reentry
140   #
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.
147   #
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
152   end
154   # call-seq:
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
157   #
158   # Activates the trace.
159   #
160   # Returns +true+ if trace was enabled.
161   # Returns +false+ if trace was disabled.
162   #
163   #   trace.enabled?  #=> false
164   #   trace.enable    #=> false (previous state)
165   #                   #   trace is enabled
166   #   trace.enabled?  #=> true
167   #   trace.enable    #=> true (previous state)
168   #                   #   trace is still enabled
169   #
170   # If a block is given, the trace will only be enabled within the scope of the
171   # block.
172   #
173   #    trace.enabled?
174   #    #=> false
175   #
176   #    trace.enable do
177   #      trace.enabled?
178   #      # only enabled for this block
179   #    end
180   #
181   #    trace.enabled?
182   #    #=> false
183   #
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.
188   #
189   #    t = TracePoint.new(:line) { |tp| p tp }
190   #
191   #    def m1
192   #      p 1
193   #    end
194   #
195   #    def m2
196   #      p 2
197   #    end
198   #
199   #    t.enable(target: method(:m1))
200   #
201   #    m1
202   #    # prints #<TracePoint:line test.rb:4 in `m1'>
203   #    m2
204   #    # prints nothing
205   #
206   # Note: You cannot access event hooks within the +enable+ block.
207   #
208   #    trace.enable { p tp.lineno }
209   #    #=> RuntimeError: access from outside
210   #
211   def enable(target: nil, target_line: nil, target_thread: nil)
212     Primitive.tracepoint_enable_m(target, target_line, target_thread)
213   end
215   # call-seq:
216   #     trace.disable           -> true or false
217   #     trace.disable { block } -> obj
218   #
219   # Deactivates the trace
220   #
221   # Return true if trace was enabled.
222   # Return false if trace was disabled.
223   #
224   #     trace.enabled?  #=> true
225   #     trace.disable   #=> true (previous status)
226   #     trace.enabled?  #=> false
227   #     trace.disable   #=> false
228   #
229   # If a block is given, the trace will only be disable within the scope of the
230   # block.
231   #
232   #     trace.enabled?
233   #     #=> true
234   #
235   #     trace.disable do
236   #         trace.enabled?
237   #         # only disabled for this block
238   #     end
239   #
240   #     trace.enabled?
241   #     #=> true
242   #
243   # Note: You cannot access event hooks within the block.
244   #
245   #     trace.disable { p tp.lineno }
246   #     #=> RuntimeError: access from outside
247   def disable
248     Primitive.tracepoint_disable_m
249   end
251   # call-seq:
252   #     trace.enabled?      -> true or false
253   #
254   # The current status of the trace
255   def enabled?
256     Primitive.tracepoint_enabled_p
257   end
259   # Type of event
260   #
261   # See TracePoint@Events for more information.
262   def event
263     Primitive.tracepoint_attr_event
264   end
266   # Line number of the event
267   def lineno
268     Primitive.tracepoint_attr_lineno
269   end
271   # Path of the file being run
272   def path
273     Primitive.tracepoint_attr_path
274   end
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
278   def parameters
279     Primitive.tracepoint_attr_parameters
280   end
282   # Return the name at the definition of the method being called
283   def method_id
284     Primitive.tracepoint_attr_method_id
285   end
287   # Return the called name of the method being called
288   def callee_id
289     Primitive.tracepoint_attr_callee_id
290   end
292   # Return class or module of the method being called.
293   #
294   #     class C; def foo; end; end
295   #     trace = TracePoint.new(:call) do |tp|
296   #       p tp.defined_class #=> C
297   #     end.enable do
298   #       C.new.foo
299   #     end
300   #
301   # If method is defined by a module, then that module is returned.
302   #
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
307   #     end.enable do
308   #       C.new.foo
309   #     end
310   #
311   # <b>Note:</b> #defined_class returns singleton class.
312   #
313   # 6th block parameter of Kernel#set_trace_func passes original class
314   # of attached by singleton class.
315   #
316   # <b>This is a difference between Kernel#set_trace_func and TracePoint.</b>
317   #
318   #     class C; def self.foo; end; end
319   #     trace = TracePoint.new(:call) do |tp|
320   #       p tp.defined_class #=> #<Class:C>
321   #     end.enable do
322   #       C.foo
323   #     end
324   def defined_class
325     Primitive.tracepoint_attr_defined_class
326   end
328   # Return the generated binding object from event.
329   #
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.
333   def binding
334     Primitive.tracepoint_attr_binding
335   end
337   # Return the trace object during event
338   #
339   # Same as the following, except it returns the correct object (the method
340   # receiver) for +c_call+ and +c_return+ events:
341   #
342   #   trace.binding.eval('self')
343   def self
344     Primitive.tracepoint_attr_self
345   end
347   #  Return value from +:return+, +c_return+, and +b_return+ event
348   def return_value
349     Primitive.tracepoint_attr_return_value
350   end
352   # Value from exception raised on the +:raise+ event
353   def raised_exception
354     Primitive.tracepoint_attr_raised_exception
355   end
357   # Compiled source code (String) on *eval methods on the +:script_compiled+ event.
358   # If loaded from a file, it will return nil.
359   def eval_script
360     Primitive.tracepoint_attr_eval_script
361   end
363   # Compiled instruction sequence represented by a RubyVM::InstructionSequence instance
364   # on the +:script_compiled+ event.
365   #
366   # Note that this method is MRI specific.
367   def instruction_sequence
368     Primitive.tracepoint_attr_instruction_sequence
369   end