Class: Trip::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/trip/event.rb

Overview

Trip::Event represents an event generated by Trip. It is yielded to the callable set by Trip#pause_when, and returned by Trip#start and Trip#resume.

An event is known by a name, which can be one of the following:

  • :c_call: when a method implemented in C is called.
  • :c_return: when a method implemented in C returns.
  • :call: when a method implemented in Ruby is called.
  • :return: when a method implemented in Ruby returns.
  • :class: when a module / class is defined or reopened.
  • :end: when a module / class definition or reopen ends.
  • :line: when starting a new expression or statement.
  • :raise: when an exception is raised.
  • :b_call: when a block is called.
  • :b_return: when a block returns.
  • :thread_begin: when a thread begins.
  • :thread_end: when a thread ends.
  • :fiber_switch: when a Fiber switches context.
  • :script_compiled: when Ruby code is compiled by eval, require, or load.

Event properties collapse

Event properties collapse

Event predicates collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, tp) ⇒ Event

Returns a new instance of Event.

Parameters:

  • name (Symbol)

    The name of an event

  • tp (Hash)

    A hash from TracePoint



46
47
48
49
50
# File 'lib/trip/event.rb', line 46

def initialize(name, tp)
  @name = name
  @tp = tp
  @epoch = Integer(Process.clock_gettime(Process::CLOCK_REALTIME))
end

Instance Attribute Details

#nameSymbol (readonly)

Returns the event name

Returns:

  • (Symbol)

    Returns the event name



58
59
60
# File 'lib/trip/event.rb', line 58

def name
  @name
end

#epochInteger (readonly)

Returns the event’s creation time as the number of seconds since epoch

Returns:

  • (Integer)

    Returns the event’s creation time as the number of seconds since epoch



64
65
66
# File 'lib/trip/event.rb', line 64

def epoch
  @epoch
end

Instance Method Details

#__binding__Binding

Returns a binding object for an instance of Trip::Event.

Returns:

  • (Binding)

    Returns a binding object for an instance of Trip::Event.



275
276
277
# File 'lib/trip/event.rb', line 275

def __binding__
  ::Kernel.binding
end

#pathString

Returns the path associated with an event

Returns:

  • (String)

    Returns the path associated with an event



69
70
71
# File 'lib/trip/event.rb', line 69

def path
  @tp[:path]
end

#linenoInteger

Returns the line number associated with an event

Returns:

  • (Integer)

    Returns the line number associated with an event



76
77
78
# File 'lib/trip/event.rb', line 76

def lineno
  @tp[:lineno]
end

#selfObject, BasicObject

Returns the self where an event occurred

Returns:

  • (Object, BasicObject)

    Returns the self where an event occurred



83
84
85
# File 'lib/trip/event.rb', line 83

def self
  @tp[:self]
end

#module_nameString

Returns the module name of self

Returns:

  • (String)

    Returns the module name of self



90
91
92
# File 'lib/trip/event.rb', line 90

def module_name
  (Module === @tp[:self]) ? @tp[:self].name : @tp[:self].class.name
end

#method_idSymbol

Returns the method id associated with an event

Returns:

  • (Symbol)

    Returns the method id associated with an event



97
98
99
# File 'lib/trip/event.rb', line 97

def method_id
  @tp[:method_id]
end

#method_typeString?

Returns the type of method (“singleton_method”, “instance_method”), or nil when Trip::Event#method_id is nil

Returns:

  • (String, nil)

    Returns the type of method (“singleton_method”, “instance_method”), or nil when Trip::Event#method_id is nil



105
106
107
108
# File 'lib/trip/event.rb', line 105

def method_type
  return nil if method_id.nil?
  (Module === @tp[:self]) ? "singleton_method" : "instance_method"
end

#bindingBinding

Returns a Binding object bound to where an event occurred.

Returns:

  • (Binding)

    Returns a Binding object bound to where an event occurred.



113
114
115
# File 'lib/trip/event.rb', line 113

def binding
  @tp[:binding]
end

#module_opened?Boolean

Returns true when a module / class is opened

Returns:

  • (Boolean)

    Returns true when a module / class is opened



124
125
126
# File 'lib/trip/event.rb', line 124

def module_opened?
  @name == :class
end

#module_closed?Boolean

Returns true when a module / class is closed.

Returns:

  • (Boolean)

    Returns true when a module / class is closed.



131
132
133
# File 'lib/trip/event.rb', line 131

def module_closed?
  @name == :end
end

#block_call?Boolean

Returns true when a block is called.

Returns:

  • (Boolean)

    Returns true when a block is called.



138
139
140
# File 'lib/trip/event.rb', line 138

def block_call?
  @name == :b_call
end

#block_return?Boolean

Returns true when a block returns.

Returns:

  • (Boolean)

    Returns true when a block returns.



145
146
147
# File 'lib/trip/event.rb', line 145

def block_return?
  @name == :b_return
end

#rb_call?Boolean

Returns true when a method implemented in Ruby is called.

Returns:

  • (Boolean)

    Returns true when a method implemented in Ruby is called.



152
153
154
# File 'lib/trip/event.rb', line 152

def rb_call?
  @name == :call
end

#rb_return?Boolean

Returns true when a method implemented in Ruby returns.

Returns:

  • (Boolean)

    Returns true when a method implemented in Ruby returns.



159
160
161
# File 'lib/trip/event.rb', line 159

def rb_return?
  @name == :return
end

#c_call?Boolean

Returns true when a method implemented in C is called.

Returns:

  • (Boolean)

    Returns true when a method implemented in C is called.



166
167
168
# File 'lib/trip/event.rb', line 166

def c_call?
  @name == :c_call
end

#c_return?Boolean

Returns true when a method implemented in C returns.

Returns:

  • (Boolean)

    Returns true when a method implemented in C returns.



173
174
175
# File 'lib/trip/event.rb', line 173

def c_return?
  @name == :c_return
end

#call?Boolean

Returns true when a method implemented in either Ruby or C is called.

Returns:

  • (Boolean)

    Returns true when a method implemented in either Ruby or C is called.



181
182
183
# File 'lib/trip/event.rb', line 181

def call?
  c_call? || rb_call?
end

#return?Boolean

Returns true when a method implemented in either Ruby or C returns.

Returns:

  • (Boolean)

    Returns true when a method implemented in either Ruby or C returns.



189
190
191
# File 'lib/trip/event.rb', line 189

def return?
  c_return? || rb_return?
end

#thread_begin?Boolean

Returns true when a thread begins.

Returns:

  • (Boolean)

    Returns true when a thread begins.



196
197
198
# File 'lib/trip/event.rb', line 196

def thread_begin?
  @name == :thread_begin
end

#thread_end?Boolean

Returns true when a thread ends.

Returns:

  • (Boolean)

    Returns true when a thread ends.



203
204
205
# File 'lib/trip/event.rb', line 203

def thread_end?
  @name == :thread_end
end

#fiber_switch?Boolean

Returns true when a Fiber switches context.

Returns:

  • (Boolean)

    Returns true when a Fiber switches context.



210
211
212
# File 'lib/trip/event.rb', line 210

def fiber_switch?
  @name == :fiber_switch
end

#script_compiled?Boolean

Returns true when a script is compiled.

Returns:

  • (Boolean)

    Returns true when a script is compiled.



217
218
219
# File 'lib/trip/event.rb', line 217

def script_compiled?
  @name == :script_compiled
end

#raise?Boolean

Returns true when an exception is raised.

Returns:

  • (Boolean)

    Returns true when an exception is raised.



224
225
226
# File 'lib/trip/event.rb', line 224

def raise?
  @name == :raise
end

#line?Boolean

Returns true when starting a new expression or statement.

Returns:

  • (Boolean)

    Returns true when starting a new expression or statement.



231
232
233
# File 'lib/trip/event.rb', line 231

def line?
  @name == :line
end

#as_jsonHash

Returns a Hash object that can be serialized to JSON.

Returns:

  • (Hash)

    Returns a Hash object that can be serialized to JSON.



239
240
241
242
243
244
245
# File 'lib/trip/event.rb', line 239

def as_json
  {
    "event" => name.to_s, "path" => path,
    "lineno" => lineno, "module_name" => module_name,
    "method_id" => method_id&.to_s, "method_type" => method_type&.to_s
  }
end

#to_json(options = {}) ⇒ String

Returns a string representation of a JSON object.

Returns:

  • (String)

    Returns a string representation of a JSON object.



250
251
252
# File 'lib/trip/event.rb', line 250

def to_json(options = {})
  as_json.to_json(options)
end

#pretty_print(q) ⇒ void

This method returns an undefined value.

REPL support



257
258
259
# File 'lib/trip/event.rb', line 257

def pretty_print(q)
  q.text(inspect)
end

#inspectString

REPL support

Returns:

  • (String)


264
265
266
267
268
269
270
# File 'lib/trip/event.rb', line 264

def inspect
  ["#<",
    to_s.sub!("#<", "").sub!(">", ""),
    " @name=:#{@name}",
    " path='#{path}:#{lineno}'",
    ">"].join
end