Skip to content

Instantly share code, notes, and snippets.

@sephraim
Last active September 24, 2019 19:34
Show Gist options
  • Save sephraim/0b625b47930f2a1ff6ce12566d56c68b to your computer and use it in GitHub Desktop.
Save sephraim/0b625b47930f2a1ff6ce12566d56c68b to your computer and use it in GitHub Desktop.
Tracing a Ruby app
# Ruby 1.9 using #set_trace_func
trace_log = '/application/log/trace.log'
File.write(trace_log, "#{'#'*150}\n"*5)
set_trace_func proc { |event, file, line, id, binding, classname|
unless file.include?('/usr/') || file.include?('/opt/')
LAST_FUNC ||= {}
unless LAST_FUNC[:id] == id && LAST_FUNC[:classname] = classname
file = file.sub('/application/', '')
file_line = "#{file}:#{line}"
string_format = "%-55s %20s %-60s\n"
File.open(trace_log, 'a') { |f|
f.printf(string_format, file_line, id, classname)
}
printf(string_format, file_line, id, classname)
end
LAST_FUNC[:id] = id
LAST_FUNC[:classname] = classname
end
}
# Ruby 1.9 using TracePoint
require 'tracepoint'
TracePoint.trace do |tp|
pp tp.methods.sort
exit
puts "#{tp.self.class}\t#{tp.callee}\t#{tp.event}\t#{tp.return?}"
end
TracePoint.activate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment