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.



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

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.



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

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.



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

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.



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

def block_call?
  @name == :b_call
end

#block_return?Boolean

Returns true when a block returns.

Returns:

  • (Boolean)

    Returns true when a block returns.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

def return?
  c_return? || rb_return?
end

#thread_begin?Boolean

Returns true when a thread begins.

Returns:

  • (Boolean)

    Returns true when a thread begins.



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

def thread_begin?
  @name == :thread_begin
end

#thread_end?Boolean

Returns true when a thread ends.

Returns:

  • (Boolean)

    Returns true when a thread ends.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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)


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

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