Class: AppMap::Tracer

Inherits:
Object show all
Defined in:
lib/appmap/trace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_id: nil) ⇒ Tracer

Records the events which happen in a program.



136
137
138
139
140
141
142
143
# File 'lib/appmap/trace.rb', line 136

def initialize(thread_id: nil)
  @events = []
  @last_package_for_thread = {}
  @methods = Set.new
  @stack_printer = StackPrinter.new if StackPrinter.enabled?
  @enabled = false
  @thread_id = thread_id
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



133
134
135
# File 'lib/appmap/trace.rb', line 133

def events
  @events
end

#stacksObject

Returns the value of attribute stacks.



132
133
134
# File 'lib/appmap/trace.rb', line 132

def stacks
  @stacks
end

#thread_idObject (readonly)

Returns the value of attribute thread_id.



133
134
135
# File 'lib/appmap/trace.rb', line 133

def thread_id
  @thread_id
end

Instance Method Details

#disableObject

Private function. Use AppMap.tracing#delete.



156
157
158
# File 'lib/appmap/trace.rb', line 156

def disable # :nodoc:
  @enabled = false
end

#enableObject



145
146
147
# File 'lib/appmap/trace.rb', line 145

def enable
  @enabled = true
end

#enabled?(thread_id: nil) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/appmap/trace.rb', line 149

def enabled?(thread_id: nil)
  return false unless @enabled

  thread_id.nil? || @thread_id.nil? || @thread_id == thread_id
end

#event?Boolean

Whether there is an event available for processing.

Returns:

  • (Boolean)


201
202
203
# File 'lib/appmap/trace.rb', line 201

def event?
  !@events.empty? && @events.first.ready?
end

#event_methodsObject

Gets a unique list of the methods that were invoked by the program.



196
197
198
# File 'lib/appmap/trace.rb', line 196

def event_methods
  @methods.to_a
end

#last_package_for_current_threadObject

Gets the last package which was observed on the current thread.



191
192
193
# File 'lib/appmap/trace.rb', line 191

def last_package_for_current_thread
  @last_package_for_thread[Thread.current.object_id]
end

#next_eventObject

Gets the next available event, if any.



206
207
208
# File 'lib/appmap/trace.rb', line 206

def next_event
  @events.shift if event?
end

#record_event(event, package: nil, defined_class: nil, method: nil) ⇒ Object

Record a program execution event.

The event should be one of the MethodEvent subclasses.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/appmap/trace.rb', line 163

def record_event(event, package: nil, defined_class: nil, method: nil)
  return unless @enabled

  if @thread_id && @thread_id != event.thread_id
    raise "Expected event in thread #{@thread_id}, got #{event.thread_id}"
  end

  @stack_printer.record(event) if @stack_printer
  @last_package_for_thread[Thread.current.object_id] = package if package
  @events << event
  static = event.static if event.respond_to?(:static)
  record_method Trace::RubyMethod.new(package, defined_class, method, static) \
    if package && defined_class && method && (event.event == :call)
end

#record_method(method) ⇒ Object

method should be duck-typed to respond to the following:

  • package

  • defined_class

  • name

  • static

  • comment

  • labels

  • source_location



186
187
188
# File 'lib/appmap/trace.rb', line 186

def record_method(method)
  @methods << method
end