Class TracePoint
In: lib/more/facets/tracepoint.rb
Parent: Object

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

===   active   active=   back   bind   called   callee   event?   event_map   eventless?   method_name   new   self   trace  

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

back_binding  [RW]  — instance ——————-
binding  [RW]  — instance ——————-
event  [RW]  — instance ——————-

Public Class methods

[Source]

    # File lib/more/facets/tracepoint.rb, line 86
86:     def active ; @@active ; end

[Source]

    # File lib/more/facets/tracepoint.rb, line 88
88:     def active=(x)
89:       @@active = x ? true : false
90:       unless @@active
91:         set_trace_func nil
92:       end
93:     end

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

[Source]

     # File lib/more/facets/tracepoint.rb, line 121
121:   def initialize( event, method, bind, back_binding=bind )
122:     @event = event
123:     @method = method
124:     @binding = bind
125:     @back_binding = back_binding
126:   end

Trace execution using a TracePoint.

[Source]

     # File lib/more/facets/tracepoint.rb, line 96
 96:     def trace # :yield:
 97:       if active
 98:         bb_stack = []
 99:         set_trace_func proc{ |e, f, l, m, b, k|
100:           #(p e, f, l, m, b, k, @@bb_stack; puts "---") if $DEBUG
101:           if ['call','c-call','class'].include?(e)
102:             bb_stack << b
103:           elsif ['return','c-return','end'].include?(e)
104:             bb = bb_stack.pop
105:           end
106:           b = bb if ! b    # this sucks!
107:           tp = TracePoint.new(e,m,b,bb)
108:           yield(tp)
109:         }
110:       end
111:     end

Public Instance methods

For use in case conditions

[Source]

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

shorthand for back_binding

[Source]

     # File lib/more/facets/tracepoint.rb, line 132
132:   def back ; @back_binding ; end

shorthand for binding

[Source]

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

Alias for 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.

[Source]

     # File lib/more/facets/tracepoint.rb, line 141
141:   def callee ; @method ; end

Is the trace point defined or undefined?

[Source]

     # File lib/more/facets/tracepoint.rb, line 169
169:   def event? ; !! @event ; end

[Source]

     # File lib/more/facets/tracepoint.rb, line 166
166:   def event_map(e) ; EVENT_MAP[e] ; end

[Source]

     # File lib/more/facets/tracepoint.rb, line 170
170:   def eventless? ; ! @event ; end
method_name()

Alias for called

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

[Source]

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

[Validate]