Guest User

Untitled

a guest
Apr 23rd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. # collect information about a running process
  2. # ruby debug.rb <pid>
  3.  
  4. begin
  5. raise ArgumentError unless pid = ARGV[0]
  6. pid = pid.to_i
  7. raise ArgumentError unless pid > 0
  8. Process.kill(0,pid)
  9. rescue TypeError, ArgumentError
  10. raise 'pid required'
  11. rescue Errno::ESRCH
  12. raise 'invalid pid'
  13. rescue Errno::EPERM
  14. raise 'could not signal process (run as root)'
  15. end
  16.  
  17. require 'fileutils'
  18. dir = FileUtils.mkdir_p "/tmp/debug-#{pid}"
  19. puts "Debugging process #{pid}, results in #{dir}."
  20.  
  21. File.open("#{dir}/proc-status.txt",'w'){ |f| f.write File.read("/proc/#{pid}/status") }
  22.  
  23. 10.times do |i|
  24. out = File.open("#{dir}/gdb-backtrace-#{i}.txt",'w')
  25. r,w = IO.pipe
  26. child = fork{ $stdin.reopen(r); $stdout.reopen(out); $stderr.reopen(out); exec "gdb /proc/#{pid}/exe" }
  27. w.puts "attach #{pid}"
  28. w.puts "backtrace 1024"
  29. w.puts "detach"
  30. w.puts "quit"
  31. Process.wait
  32. out.close
  33.  
  34. sleep 1
  35. end
  36.  
  37. out = File.open("#{dir}/strace-summary.txt",'w')
  38. child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -cp #{pid}" }
  39. sleep 5
  40. Process.kill 'HUP', child
  41. Process.wait
  42. out.close
  43.  
  44. out = File.open("#{dir}/strace-log.txt",'w')
  45. child = fork{ $stdout.reopen(out); $stderr.reopen(out); exec "strace -ttTp #{pid}" }
  46. sleep 5
  47. Process.kill 'HUP', child
  48. Process.wait
  49. out.close
Add Comment
Please, Sign In to add comment