2019-11-07 18:22:08 +09:00
|
|
|
# loaded from vm_trace.c
|
|
|
|
|
|
|
|
# A class that provides the functionality of Kernel#set_trace_func in a
|
2024-12-13 05:44:15 +08:00
|
|
|
# well-structured Object-Oriented API.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# == Example
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Use TracePoint to gather information specifically for exceptions:
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace = TracePoint.new(:raise) do |tp|
|
|
|
|
# p [tp.lineno, tp.event, tp.raised_exception]
|
|
|
|
# end
|
|
|
|
# #=> #<TracePoint:disabled>
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enable #=> false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# 0 / 0
|
|
|
|
# #=> [5, :raise, #<ZeroDivisionError: divided by 0>]
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# == Events
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If you don't specify the types of events you want to listen for,
|
2019-11-07 18:22:08 +09:00
|
|
|
# TracePoint will include all available events.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# *Note:* Do not depend on the current event set, as this list is subject to
|
|
|
|
# change. Instead, it is recommended to specify the types of events you
|
2019-11-07 18:22:08 +09:00
|
|
|
# want to use.
|
|
|
|
#
|
|
|
|
# To filter what is traced, you can pass any of the following as +events+:
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# +: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+, and +c_call+).
|
|
|
|
# +:a_return+:: Event hook at all returns (+return+, +b_return+, and +c_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+, +load+, or +require+).
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
class TracePoint
|
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# TracePoint.new(*events) { |tp| block } -> tp
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# Returns a new TracePoint object, not enabled by default.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# To activate the TracePoint object, use TracePoint#enable:
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace = TracePoint.new(:call) do |tp|
|
|
|
|
# p [tp.lineno, tp.defined_class, tp.method_id, tp.event]
|
|
|
|
# end
|
|
|
|
# #=> #<TracePoint:disabled>
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enable #=> false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# puts "Hello, TracePoint!"
|
|
|
|
# # ...
|
|
|
|
# # [48, IRB::Notifier::AbstractNotifier, :printf, :call]
|
|
|
|
# # ...
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# To deactivate the trace, use TracePoint#disable.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.disable
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# See TracePoint@Events for possible events and more information.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# A block must be given; otherwise, an ArgumentError is raised.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# If the trace method isn't included in the given events filter, a
|
|
|
|
# RuntimeError is raised.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# TracePoint.trace(:line) do |tp|
|
|
|
|
# p tp.raised_exception
|
|
|
|
# end
|
|
|
|
# #=> RuntimeError: 'raised_exception' not supported by this event
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If the trace method is called outside a block, a RuntimeError is raised.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# TracePoint.trace(:line) do |tp|
|
|
|
|
# $tp = tp
|
|
|
|
# end
|
|
|
|
# $tp.lineno #=> access from outside (RuntimeError)
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# Access from other threads is also forbidden.
|
|
|
|
#
|
|
|
|
def self.new(*events)
|
2024-03-27 07:29:38 +09:00
|
|
|
Primitive.attr! :use_block
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_new_s(events)
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# call-seq:
|
|
|
|
# trace.inspect -> string
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns a string containing a human-readable TracePoint
|
|
|
|
# status.
|
2019-11-07 18:22:08 +09:00
|
|
|
def inspect
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_inspect
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# TracePoint.stat -> obj
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns internal information of TracePoint.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# The contents of the returned value are implementation-specific
|
|
|
|
# and may change in the future.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# This method is only for debugging TracePoint itself.
|
2019-12-04 21:02:21 -05:00
|
|
|
def self.stat
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_stat_s
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# TracePoint.trace(*events) { |tp| block } -> obj
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# A convenience method for TracePoint.new that activates the trace
|
2021-09-25 17:28:48 +02:00
|
|
|
# automatically.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace = TracePoint.trace(:call) { |tp| [tp.lineno, tp.event] }
|
|
|
|
# #=> #<TracePoint:enabled>
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> true
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
def self.trace(*events)
|
2024-03-27 07:29:38 +09:00
|
|
|
Primitive.attr! :use_block
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_trace_s(events)
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2021-12-09 03:50:17 +09:00
|
|
|
# call-seq:
|
2021-12-30 20:52:42 +02:00
|
|
|
# TracePoint.allow_reentry { block }
|
2021-12-09 03:50:17 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Generally, while a TracePoint callback is running,
|
2021-12-09 03:50:17 +09:00
|
|
|
# other registered callbacks are not called to avoid
|
2024-12-13 05:44:15 +08:00
|
|
|
# confusion from reentrance.
|
|
|
|
# This method allows reentrance within a given block.
|
|
|
|
# Use this method carefully to avoid infinite callback invocation.
|
2021-12-09 03:50:17 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If called when reentrance is already allowed,
|
2021-12-09 03:50:17 +09:00
|
|
|
# it raises a RuntimeError.
|
2021-12-30 20:52:42 +02:00
|
|
|
#
|
|
|
|
# <b>Example:</b>
|
|
|
|
#
|
|
|
|
# # Without reentry
|
|
|
|
# # ---------------
|
|
|
|
#
|
|
|
|
# line_handler = TracePoint.new(:line) do |tp|
|
2024-12-13 05:44:15 +08:00
|
|
|
# next if tp.path != __FILE__ # Only works in this file
|
2021-12-30 20:52:42 +02:00
|
|
|
# puts "Line handler"
|
|
|
|
# binding.eval("class C; end")
|
|
|
|
# end.enable
|
|
|
|
#
|
|
|
|
# class_handler = TracePoint.new(:class) do |tp|
|
|
|
|
# puts "Class handler"
|
|
|
|
# end.enable
|
|
|
|
#
|
|
|
|
# class B
|
|
|
|
# end
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# # This script will print "Class handler" only once: when inside the :line
|
|
|
|
# # handler, all other handlers are ignored.
|
2021-12-30 20:52:42 +02:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# # With reentry
|
|
|
|
# # ------------
|
|
|
|
#
|
|
|
|
# line_handler = TracePoint.new(:line) do |tp|
|
2024-12-13 05:44:15 +08:00
|
|
|
# next if tp.path != __FILE__ # Only works in this file
|
|
|
|
# next if (__LINE__..__LINE__+3).cover?(tp.lineno) # Prevent infinite calls
|
2021-12-30 20:52:42 +02:00
|
|
|
# puts "Line handler"
|
|
|
|
# TracePoint.allow_reentry { binding.eval("class C; end") }
|
|
|
|
# end.enable
|
|
|
|
#
|
|
|
|
# class_handler = TracePoint.new(:class) do |tp|
|
|
|
|
# puts "Class handler"
|
|
|
|
# end.enable
|
|
|
|
#
|
|
|
|
# class B
|
|
|
|
# end
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# # This will print "Class handler" twice: inside the allow_reentry block in the :line
|
2021-12-30 20:52:42 +02:00
|
|
|
# # handler, other handlers are enabled.
|
|
|
|
#
|
|
|
|
# Note that the example shows the principal effect of the method, but its
|
2024-12-13 05:44:15 +08:00
|
|
|
# practical usage is for debugging libraries that sometimes require other libraries'
|
|
|
|
# hooks to not be affected by the debugger being inside trace point handling.
|
|
|
|
# Precautions should be taken against infinite recursion in this case
|
|
|
|
# (note that we needed to filter out calls by itself from the :line handler,
|
|
|
|
# otherwise it would call itself infinitely).
|
2021-12-30 20:52:42 +02:00
|
|
|
#
|
2021-12-09 03:50:17 +09:00
|
|
|
def self.allow_reentry
|
2024-03-27 07:29:38 +09:00
|
|
|
Primitive.attr! :use_block
|
2021-12-09 03:50:17 +09:00
|
|
|
Primitive.tracepoint_allow_reentry
|
|
|
|
end
|
|
|
|
|
2019-11-07 18:22:08 +09:00
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enable(target: nil, target_line: nil, target_thread: nil) -> true or false
|
|
|
|
# trace.enable(target: nil, target_line: nil, target_thread: :default) { block } -> obj
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# Activates the trace.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns +true+ if the trace was enabled.
|
|
|
|
# Returns +false+ if the trace was disabled.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# trace.enabled? #=> false
|
|
|
|
# trace.enable #=> false (previous state)
|
|
|
|
# # trace is enabled
|
|
|
|
# trace.enabled? #=> true
|
|
|
|
# trace.enable #=> true (previous state)
|
|
|
|
# # trace is still enabled
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If a block is given, the trace will only be enabled during the block execution.
|
2022-02-08 08:55:49 -08:00
|
|
|
# If target and target_line are both nil, then target_thread will default
|
2021-12-27 12:52:04 -08:00
|
|
|
# to the current thread if a block is given.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enable do
|
|
|
|
# trace.enabled?
|
|
|
|
# # Only enabled for this block and thread
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# The +target+, +target_line+, and +target_thread+ parameters are used to
|
|
|
|
# limit tracing to specified code objects. +target+ should be a
|
2019-11-07 18:22:08 +09:00
|
|
|
# code object for which RubyVM::InstructionSequence.of will return
|
|
|
|
# an instruction sequence.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# t = TracePoint.new(:line) { |tp| p tp }
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# def m1
|
|
|
|
# p 1
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# def m2
|
|
|
|
# p 2
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# t.enable(target: method(:m1))
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# m1
|
|
|
|
# # Prints #<TracePoint:line test.rb:4 in `m1'>
|
|
|
|
# m2
|
|
|
|
# # Prints nothing
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# Note: You cannot access event hooks within the +enable+ block.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enable { p tp.lineno }
|
|
|
|
# #=> RuntimeError: access from outside
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2021-12-27 14:08:59 -08:00
|
|
|
def enable(target: nil, target_line: nil, target_thread: :default)
|
2024-03-27 07:29:38 +09:00
|
|
|
Primitive.attr! :use_block
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_enable_m(target, target_line, target_thread)
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.disable -> true or false
|
|
|
|
# trace.disable { block } -> obj
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Deactivates the trace.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns +true+ if the trace was enabled.
|
|
|
|
# Returns +false+ if the trace was disabled.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> true
|
|
|
|
# trace.disable #=> true (previous status)
|
|
|
|
# trace.enabled? #=> false
|
|
|
|
# trace.disable #=> false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If a block is given, the trace will only be disabled within the scope of the
|
2019-11-07 18:22:08 +09:00
|
|
|
# block.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> true
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.disable do
|
|
|
|
# trace.enabled?
|
|
|
|
# # Only disabled for this block
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? #=> true
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# Note: You cannot access event hooks within the block.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.disable { p tp.lineno }
|
|
|
|
# #=> RuntimeError: access from outside
|
|
|
|
#
|
2019-11-07 18:22:08 +09:00
|
|
|
def disable
|
2024-03-27 07:29:38 +09:00
|
|
|
Primitive.attr! :use_block
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_disable_m
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
# call-seq:
|
2024-12-13 05:44:15 +08:00
|
|
|
# trace.enabled? -> true or false
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the current status of the trace.
|
2019-11-07 18:22:08 +09:00
|
|
|
def enabled?
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_enabled_p
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the type of event.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# See TracePoint@Events for more information.
|
|
|
|
def event
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_event
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the line number of the event.
|
2019-11-07 18:22:08 +09:00
|
|
|
def lineno
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_lineno
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the path of the file being executed.
|
2019-11-07 18:22:08 +09:00
|
|
|
def path
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_path
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the parameter definitions of the method or block that the
|
|
|
|
# current hook belongs to. The format is the same as for Method#parameters.
|
2019-11-07 18:22:08 +09:00
|
|
|
def parameters
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_parameters
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the name at the definition of the method being called.
|
2019-11-07 18:22:08 +09:00
|
|
|
def method_id
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_method_id
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the called name of the method being called.
|
2019-11-07 18:22:08 +09:00
|
|
|
def callee_id
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_callee_id
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the class or module of the method being called.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# class C; def foo; end; end
|
|
|
|
# trace = TracePoint.new(:call) do |tp|
|
|
|
|
# p tp.defined_class #=> C
|
|
|
|
# end.enable do
|
|
|
|
# C.new.foo
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# If the method is defined by a module, then that module is returned.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# module M; def foo; end; end
|
|
|
|
# class C; include M; end
|
|
|
|
# trace = TracePoint.new(:call) do |tp|
|
|
|
|
# p tp.defined_class #=> M
|
|
|
|
# end.enable do
|
|
|
|
# C.new.foo
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# <b>Note:</b> #defined_class returns the singleton class.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# The 6th block parameter of Kernel#set_trace_func passes the original class
|
|
|
|
# attached by the singleton class.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
|
|
|
# <b>This is a difference between Kernel#set_trace_func and TracePoint.</b>
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# class C; def self.foo; end; end
|
|
|
|
# trace = TracePoint.new(:call) do |tp|
|
|
|
|
# p tp.defined_class #=> #<Class:C>
|
|
|
|
# end.enable do
|
|
|
|
# C.foo
|
|
|
|
# end
|
2019-11-07 18:22:08 +09:00
|
|
|
def defined_class
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_defined_class
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the generated binding object from the event.
|
2021-04-26 15:21:52 -07:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Note that for +:c_call+ and +:c_return+ events, the method returns
|
2023-02-15 21:30:58 +02:00
|
|
|
# +nil+, since C methods themselves do not have bindings.
|
2019-11-07 18:22:08 +09:00
|
|
|
def binding
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_binding
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the trace object during the event.
|
2019-11-07 18:22:08 +09:00
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Similar to the following, but it returns the correct object (the method
|
2023-12-14 23:01:48 +02:00
|
|
|
# receiver) for +:c_call+ and +:c_return+ events:
|
2021-04-26 15:21:52 -07:00
|
|
|
#
|
|
|
|
# trace.binding.eval('self')
|
2019-11-07 18:22:08 +09:00
|
|
|
def self
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_self
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the return value from +:return+, +:c_return+, and +:b_return+ events.
|
2019-11-07 18:22:08 +09:00
|
|
|
def return_value
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_return_value
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the exception raised on the +:raise+ event or rescued on the +:rescue+ event.
|
2019-11-07 18:22:08 +09:00
|
|
|
def raised_exception
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_raised_exception
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the compiled source code (String) from eval methods on the +:script_compiled+ event.
|
|
|
|
# If loaded from a file, it returns +nil+.
|
2019-11-07 18:22:08 +09:00
|
|
|
def eval_script
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_eval_script
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
|
2024-12-13 05:44:15 +08:00
|
|
|
# Returns the compiled instruction sequence represented by a RubyVM::InstructionSequence instance
|
2019-11-07 18:22:08 +09:00
|
|
|
# on the +:script_compiled+ event.
|
|
|
|
#
|
2024-12-13 05:44:15 +08:00
|
|
|
# Note that this method is CRuby-specific.
|
2019-11-07 18:22:08 +09:00
|
|
|
def instruction_sequence
|
2020-05-31 15:52:32 +09:00
|
|
|
Primitive.tracepoint_attr_instruction_sequence
|
2019-11-07 18:22:08 +09:00
|
|
|
end
|
|
|
|
end
|