Skip to content

Instantly share code, notes, and snippets.

@leandro
Forked from tmm1/debug.rb
Created October 14, 2009 13:58
Show Gist options
  • Save leandro/210092 to your computer and use it in GitHub Desktop.
Save leandro/210092 to your computer and use it in GitHub Desktop.
# collect information about a running process
# ruby debug.rb <pid>
begin
raise ArgumentError unless pid = ARGV[0]
pid = pid.to_i
Process.kill(0,pid)
rescue TypeError, ArgumentError
raise 'pid required'
rescue Errno::ESRCH
raise 'invalid pid'
rescue Errno::EPERM
raise 'could not signal process (run as root)'
end
require 'fileutils'
dir = FileUtils.mkdir_p "/tmp/debug-#{pid}"
puts "Debugging process #{pid}, results in #{dir}."
5.times do |i|
out = File.open("#{dir}/gdb-backtrace-#{i}.txt",'w')
r,w = IO.pipe
child = fork{ $stdin.reopen(r); $stdout.reopen(out); $stderr.reopen(out); exec "gdb /proc/#{pid}/exe" }
w.puts "attach #{pid}"
w.puts "backtrace"
w.puts "detach"
w.puts "quit"
Process.wait
out.close
sleep 1
end
out = File.open("#{dir}/strace-summary.txt",'w')
child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -cp #{pid}" }
sleep 30
Process.kill 'HUP', child
Process.wait
out.close
out = File.open("#{dir}/strace-log.txt",'w')
child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -ttTp #{pid}" }
sleep 30
Process.kill 'HUP', child
Process.wait
out.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment