Class: Kefka::Tracer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_level = Logger::INFO) ⇒ Tracer

Returns a new instance of Tracer.



109
110
111
112
113
114
115
116
# File 'lib/kefka.rb', line 109

def initialize(log_level = Logger::INFO)
  @method_graph = MethodGraph.new
  @callstack = []
  @local_values = {}
  @event_disable = []
  @logger = Logger.new($stderr)
  @logger.level = log_level
end

Instance Attribute Details

#callstackObject (readonly)

Returns the value of attribute callstack.



106
107
108
# File 'lib/kefka.rb', line 106

def callstack
  @callstack
end

#codeObject (readonly)

Returns the value of attribute code.



106
107
108
# File 'lib/kefka.rb', line 106

def code
  @code
end

#local_valuesObject (readonly)

Returns the value of attribute local_values.



106
107
108
# File 'lib/kefka.rb', line 106

def local_values
  @local_values
end

#loggerObject (readonly)

Returns the value of attribute logger.



106
107
108
# File 'lib/kefka.rb', line 106

def logger
  @logger
end

#method_graphObject (readonly)

Returns the value of attribute method_graph.



106
107
108
# File 'lib/kefka.rb', line 106

def method_graph
  @method_graph
end

Instance Method Details

#callgraph_handler(event, file, line, id, binding, classname) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/kefka.rb', line 142

def callgraph_handler(event, file, line, id, binding, classname)
  return if file == __FILE__

  @logger.debug "#{event} - #{file}:#{line} #{classname} #{id}"

  if disable_event_handlers?
    @event_disable.pop if event == "end"
    return
  end

  case event
  when "class"
    @event_disable << true
  when "call"
    method_caller = @callstack.last

    method = Method.new(
      :classname => classname,
      :id => id,
      :file => file,
      :line => line
    )

    if method_caller
      @method_graph.add_edge(method_caller,method)
    end

    @callstack << method
  when "return"
    @callstack.pop
  else
    # do nothing
  end
rescue Exception => e
  puts "\n#{e.class}: #{e.message} \n    from #{e.backtrace.join("\n")}"
  Process.kill("KILL", $$)
end

#deep_copy(val) ⇒ Object



122
123
124
125
126
# File 'lib/kefka.rb', line 122

def deep_copy(val)
  Marshal.load(Marshal.dump(val))
rescue TypeError
  "_unknown_"
end

#disable_event_handlers?Boolean

Returns:

  • (Boolean)


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

def disable_event_handlers?
  !@event_disable.empty?
end

#get_locals(target) ⇒ Object



118
119
120
# File 'lib/kefka.rb', line 118

def get_locals(target)
  target.eval("local_variables")
end

#get_values_of_locals_from_binding(target) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/kefka.rb', line 128

def get_values_of_locals_from_binding(target)
  locals = get_locals(target)
  locals.inject({}) do |result,l|
    val = target.eval(l.to_s)
    val = deep_copy(val)
    result.merge!({ l => val })
    result
  end
end

#local_values_handler(event, file, line, id, binding, classname) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/kefka.rb', line 180

def local_values_handler(event, file, line, id, binding, classname)
  # skip variables that should not be tracked
  # 1. anything in current lib (i.e __FILE__)
  # 2. all local variables that are in TOP LEVEL BINDING before tracing
  #     - but these variables may be overwritten by the traced program,
  #       excluding them would mean not displaying certain relevant
  #       vars in that program
  return if file == __FILE__

  @logger.debug "#{event} - #{file}:#{line} #{classname} #{id}" if $DEBUG

  if disable_event_handlers?
    @event_disable.pop if event == "end"
    return
  end

  case event
  when "class"
    @event_disable << true
  when "line"
    key = [file, line].join(":")
    @local_values[key] = get_values_of_locals_from_binding(binding)
  else
    # do nothing
  end
end


207
208
209
210
# File 'lib/kefka.rb', line 207

def print_callgraph
  public_dir = "#{File.expand_path(File.dirname(__FILE__))}/../public"
  @method_graph.write_to_graphic_file("png", "#{public_dir}/graph")
end

#trace(file_path, handler = :callgraph_handler) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/kefka.rb', line 212

def trace(file_path, handler = :callgraph_handler)
  file = File.open(file_path)

  thread = Thread.new {
    @code = file.read
    eval(@code, TOPLEVEL_BINDING, file.path, 1)
  }

  thread.set_trace_func method(handler).to_proc
  thread.join
end