TracePoint

A TracePoint is a Binding with the addition of event information. Among other things, it functions very well as the join-point for Event-based AOP.

Usage

  TracePoint.trace { |tp|
    puts "#{tp.self.class}\t#{tp.called}\t#{tp.event}\t#{tp.return?}\t#{tp.back == tp.bind}"
  }

  1 + 1

produces

  Class   trace   return     true    false
  Object          line       false   false
  Fixnum  +       c-call     false   false
  Fixnum  +       c-return   false   false

Notes

You can’t subclass Binding, so we delegate (which is better anyway).

Methods
Constants
EVENT_MAP = { :all => ['call', 'c-call', 'return', 'c-return', 'line', 'class', 'end', 'raise'], :before => ['call', 'c-call'], :after => ['return', 'c-return'], :call => ['call'], :return => ['return'], :ccall => ['c-call'], :creturn => ['c-return'], :line => ['line'], :class => ['class'], :end => ['end'], :raise => ['raise']
  methods for working with events
Attributes
[RW] back_binding — instance ——————-
[RW] binding — instance ——————-
[RW] event — instance ——————-
Public Class methods
active()
# File lib/facets/more/tracepoint.rb, line 62
    def active ; @@active ; end
active=(x)
# File lib/facets/more/tracepoint.rb, line 64
    def active=(x)
      @@active = x ? true : false
      unless @@active
        set_trace_func nil
      end
    end
new( event, method, bind, back_binding=bind )

Until Ruby has a built-in way to get the name of the calling method that information must be passed into the TracePoint.

# File lib/facets/more/tracepoint.rb, line 97
  def initialize( event, method, bind, back_binding=bind )
    @event = event
    @method = method
    @binding = bind
    @back_binding = back_binding
  end
trace( {|| ...}

Trace execution using a TracePoint.

# File lib/facets/more/tracepoint.rb, line 72
    def trace # :yield:
      if active
        bb_stack = []
        set_trace_func proc{ |e, f, l, m, b, k|
          #(p e, f, l, m, b, k, @@bb_stack; puts "---") if $DEBUG
          if ['call','c-call','class'].include?(e)
            bb_stack << b
          elsif ['return','c-return','end'].include?(e)
            bb = bb_stack.pop
          end
          b = bb if ! b    # this sucks!
          tp = TracePoint.new(e,m,b,bb)
          yield(tp)
        }
      end
    end
Public Instance methods
===(e)

For use in case conditions

# File lib/facets/more/tracepoint.rb, line 149
  def ===(e)
    EVENT_MAP[e].include?(@event)
  end
back()

shorthand for back_binding

# File lib/facets/more/tracepoint.rb, line 108
  def back ; @back_binding ; end
bind()

shorthand for binding

# File lib/facets/more/tracepoint.rb, line 105
  def bind ; @binding ; end
called()

Alias for callee

This method is also aliased as method_name
callee()

Returns the name of the event’s method. This could delegate to the binding if Ruby had an internal way to retrieve the current method name.

This method is also aliased as called
# File lib/facets/more/tracepoint.rb, line 117
  def callee ; @method ; end
event?()

Is the trace point defined or undefined?

# File lib/facets/more/tracepoint.rb, line 145
  def event? ; !! @event ; end
event_map(e)
# File lib/facets/more/tracepoint.rb, line 142
  def event_map(e) ; EVENT_MAP[e] ; end
eventless?()
# File lib/facets/more/tracepoint.rb, line 146
  def eventless? ; ! @event ; end
method_name()

Alias for called

self()

Delegates "self" to the binding which in turn delegates the binding object.

# File lib/facets/more/tracepoint.rb, line 112
  def self ; @binding.self ; end