Skip to content

Instantly share code, notes, and snippets.

@shri-zz
Created March 6, 2009 17:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shri-zz/74979 to your computer and use it in GitHub Desktop.
Save shri-zz/74979 to your computer and use it in GitHub Desktop.
# Function to print hierarchical call trees as the program executes.
# Use "ir.exe -trace" for IronRuby
$CALL_DEPTH = 0
p = proc { |op, file, line, method, b, cls|
if op == "call"
filename = file.nil? ? nil : file.gsub('\\','/')
puts "#{$CALL_DEPTH}\t" + ('| ' * $CALL_DEPTH) + "> #{cls}::#{method} #{filename}:#{line}"
locals = eval("local_variables", b)
local_values = locals.each { |l|
val = nil
begin
val = eval(l, b)
rescue
end
puts "#{$CALL_DEPTH}\t" + ('| ' * $CALL_DEPTH) + " #{l}:#{val.inspect}"
}
$CALL_DEPTH += 1
elsif op == "return"
$CALL_DEPTH -= 1 if $CALL_DEPTH > 0
elsif op == "raise"
puts "$!" * 20
end
}
set_trace_func p
# A simpler solution is:
# set_trace_func proc { |*args| p args }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment