Class: Kefka::Tracer
- Inherits:
-
Object
- Object
- Kefka::Tracer
- Defined in:
- lib/kefka.rb
Instance Attribute Summary collapse
-
#callstack ⇒ Object
readonly
Returns the value of attribute callstack.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#line_graph ⇒ Object
readonly
Returns the value of attribute line_graph.
-
#local_values ⇒ Object
readonly
Returns the value of attribute local_values.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#method_graph ⇒ Object
readonly
Returns the value of attribute method_graph.
Instance Method Summary collapse
- #create_toplevel_method(file_path) ⇒ Object
- #deep_copy(val) ⇒ Object
- #disable_event_handlers? ⇒ Boolean
- #get_values_of_locals_from_binding(target, line_source) ⇒ Object
-
#initialize(log_level = Logger::INFO) ⇒ Tracer
constructor
A new instance of Tracer.
- #trace(file_path) ⇒ Object
- #trace_handler(event, file, line, id, binding, classname) ⇒ Object
Constructor Details
#initialize(log_level = Logger::INFO) ⇒ Tracer
Returns a new instance of Tracer.
201 202 203 204 205 206 207 208 209 |
# File 'lib/kefka.rb', line 201 def initialize(log_level = Logger::INFO) @method_graph = MethodGraph.new @line_graph = LineGraph.new @callstack = [] @local_values = {} @event_disable = [] @logger = Logger.new($stderr) @logger.level = log_level end |
Instance Attribute Details
#callstack ⇒ Object (readonly)
Returns the value of attribute callstack.
197 198 199 |
# File 'lib/kefka.rb', line 197 def callstack @callstack end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
197 198 199 |
# File 'lib/kefka.rb', line 197 def code @code end |
#line_graph ⇒ Object (readonly)
Returns the value of attribute line_graph.
197 198 199 |
# File 'lib/kefka.rb', line 197 def line_graph @line_graph end |
#local_values ⇒ Object (readonly)
Returns the value of attribute local_values.
197 198 199 |
# File 'lib/kefka.rb', line 197 def local_values @local_values end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
197 198 199 |
# File 'lib/kefka.rb', line 197 def logger @logger end |
#method_graph ⇒ Object (readonly)
Returns the value of attribute method_graph.
197 198 199 |
# File 'lib/kefka.rb', line 197 def method_graph @method_graph end |
Instance Method Details
#create_toplevel_method(file_path) ⇒ Object
311 312 313 314 315 316 317 318 319 320 |
# File 'lib/kefka.rb', line 311 def create_toplevel_method(file_path) method = Method.new( :classname => "Toplevel", :id => nil, :file => file_path, :line => 1 ) method.source = File.readlines(file_path).join return method end |
#deep_copy(val) ⇒ Object
304 305 306 307 308 |
# File 'lib/kefka.rb', line 304 def deep_copy(val) Marshal.load(Marshal.dump(val)) rescue TypeError "_unknown_" end |
#disable_event_handlers? ⇒ Boolean
225 226 227 |
# File 'lib/kefka.rb', line 225 def disable_event_handlers? !@event_disable.empty? end |
#get_values_of_locals_from_binding(target, line_source) ⇒ Object
294 295 296 297 298 299 300 301 302 |
# File 'lib/kefka.rb', line 294 def get_values_of_locals_from_binding(target, line_source) locals = LocalsHelper.get_locals(target,line_source) locals.inject({}) do |result,l| val = target.eval(l.to_s) val = deep_copy(val) result.merge!({ l => val.to_s }) result end end |
#trace(file_path) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/kefka.rb', line 211 def trace(file_path) file = File.open(file_path) thread = Thread.new { @code = file.read toplevel_method = create_toplevel_method(file_path) @callstack << Call.new(:method => toplevel_method, :line => 1) eval(@code, TOPLEVEL_BINDING, file.path, 1) } thread.set_trace_func method(:trace_handler).to_proc thread.join end |
#trace_handler(event, file, line, id, binding, classname) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/kefka.rb', line 229 def trace_handler(event, file, line, id, binding, classname) return if file == __FILE__ @logger.debug "#{event} - #{file}:#{line} #{classname} #{id}" # skip event handling when iseq is happening inside class loading if disable_event_handlers? @event_disable.pop if event == "end" return end case event when "class" @event_disable << true when "call" called_from = @callstack.last caller_method = called_from.method method = Method.new( :classname => classname, :id => id, :file => file, :line => line ) @line_graph.add_edge(called_from.dup,method) @method_graph.add_edge(caller_method,method) @callstack << Call.new(:method => method, :line => line) when "line" # 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 #current_method = @callstack.last # update callstack last method's line if call = @callstack.last call.line = line end # given current file & line, determine what method I am in method = @method_graph.vertices .select { |method| method.contains?(file,line) } .first # skip if not in any previously called method if method line_source = method.source_at_line(line) iseq_key = [id,file,line].join(":") @local_values[iseq_key] = get_values_of_locals_from_binding(binding, line_source) end when "return" @callstack.pop else # do nothing end rescue Exception => e puts "\n#{e.class}: #{e.} \n from #{e.backtrace.join("\n")}" Process.kill("KILL", $$) end |